-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: edit package settings, create new pack.json
Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com>
- Loading branch information
Showing
16 changed files
with
988 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,250 @@ | ||
package gui | ||
|
||
import ( | ||
"strings" | ||
|
||
"fyne.io/fyne/v2" | ||
"fyne.io/fyne/v2/container" | ||
"fyne.io/fyne/v2/data/binding" | ||
"fyne.io/fyne/v2/dialog" | ||
"fyne.io/fyne/v2/lang" | ||
"fyne.io/fyne/v2/layout" | ||
"fyne.io/fyne/v2/theme" | ||
"fyne.io/fyne/v2/widget" | ||
"github.com/ryex/dungeondraft-gopackager/internal/gui/bindings" | ||
"github.com/ryex/dungeondraft-gopackager/internal/gui/widgets" | ||
"github.com/ryex/dungeondraft-gopackager/internal/utils" | ||
"github.com/ryex/dungeondraft-gopackager/pkg/ddpackage" | ||
"github.com/ryex/dungeondraft-gopackager/pkg/structures" | ||
) | ||
|
||
type PackJSONDialog struct { | ||
Path string | ||
|
||
Name string | ||
Author string | ||
Version string | ||
Keywords []string | ||
|
||
id *string | ||
|
||
Allow3rdParty bool | ||
sourceHas3rdParty bool | ||
|
||
ColorOverrides *structures.CustomColorOverrides | ||
|
||
content *fyne.Container | ||
parent fyne.Window | ||
dialog *dialog.CustomDialog | ||
|
||
callback func(options ddpackage.SavePackageJSONOptions) | ||
|
||
editable bool | ||
} | ||
|
||
func NewPackJSONDialogPkg( | ||
pkg *ddpackage.Package, | ||
editable bool, | ||
callback func(options ddpackage.SavePackageJSONOptions), | ||
parent fyne.Window, | ||
) *PackJSONDialog { | ||
dlg := &PackJSONDialog{ | ||
parent: parent, | ||
editable: editable, | ||
callback: callback, | ||
} | ||
if pkg != nil { | ||
id := pkg.ID() | ||
dlg.id = &id | ||
dlg.Path = pkg.UnpackedPath() | ||
dlg.Name = pkg.Name() | ||
dlg.Author = pkg.Info().Author | ||
dlg.Version = pkg.Info().Version | ||
keywords := make([]string, len(pkg.Info().Keywords)) | ||
copy(keywords, pkg.Info().Keywords) | ||
dlg.Keywords = keywords | ||
if pkg.Info().Allow3rdParty != nil { | ||
dlg.sourceHas3rdParty = true | ||
dlg.Allow3rdParty = *pkg.Info().Allow3rdParty | ||
} | ||
overrides := pkg.Info().ColorOverrides | ||
|
||
dlg.ColorOverrides = &overrides | ||
} | ||
if dlg.ColorOverrides == nil { | ||
dlg.ColorOverrides = structures.DefaultCustomColorOverrides() | ||
} | ||
if dlg.Version == "" { | ||
dlg.Version = "1" | ||
} | ||
dlg.buildUi() | ||
return dlg | ||
} | ||
|
||
func NewPackJSONDialog( | ||
path string, | ||
callback func(options ddpackage.SavePackageJSONOptions), | ||
parent fyne.Window, | ||
) *PackJSONDialog { | ||
dlg := &PackJSONDialog{ | ||
Path: path, | ||
parent: parent, | ||
callback: callback, | ||
Version: "1", | ||
ColorOverrides: structures.DefaultCustomColorOverrides(), | ||
editable: true, | ||
} | ||
dlg.buildUi() | ||
return dlg | ||
} | ||
|
||
func (dlg *PackJSONDialog) buildUi() { | ||
nameLbl := widget.NewLabel(lang.X("packJson.name.label", "Name")) | ||
nameEntry := widget.NewEntryWithData(binding.BindString(&dlg.Name)) | ||
nameEntry.SetPlaceHolder(lang.X("packJson.name.placeholder", "Package name")) | ||
|
||
authorLbl := widget.NewLabel(lang.X("packJson.author.label", "Author")) | ||
authorEntry := widget.NewEntryWithData(binding.BindString(&dlg.Author)) | ||
authorEntry.SetPlaceHolder(lang.X("packJson.author.placeholder", "Package author")) | ||
|
||
versionLbl := widget.NewLabel(lang.X("packJson.version.label", "Version")) | ||
versionEntry := widgets.NewSpinner(1, 10000, 0.1) | ||
versionEntry.Bind(binding.StringToFloat(binding.BindString(&dlg.Version))) | ||
|
||
keywordsLbl := widget.NewLabel(lang.X("packJson.keywords.label", "Keywords")) | ||
keywordsEntry := widget.NewEntryWithData(bindings.NewReversableMapping( | ||
binding.BindStringList(&dlg.Keywords), | ||
func(list []string) (string, error) { | ||
return strings.Join(list, ","), nil | ||
}, | ||
func(csv string) ([]string, error) { | ||
words := utils.Map(strings.Split(csv, ","), func(s string) string { | ||
return strings.TrimSpace(s) | ||
}) | ||
words = utils.Filter(words, func(s string) bool { | ||
return s != "" | ||
}) | ||
return words, nil | ||
}, | ||
)) | ||
keywordsEntry.SetPlaceHolder(lang.X("packJson.keywords.placeholder", "split keywords with commas")) | ||
|
||
thirdPartyCheck := widget.NewCheck(lang.X("packJson.thirdParty.label", "Allow 3rd party mapping software to use this pack"), func(checked bool) { | ||
dlg.Allow3rdParty = checked | ||
}) | ||
|
||
customColorsMsg := multilineCanvasText( | ||
lang.X( | ||
"packJson.customColors.msg", | ||
"These settings affect the detection of the red color for custom color objects.\n"+ | ||
"Most users will want to leave these settings alone.", | ||
), | ||
12, | ||
fyne.TextStyle{}, | ||
fyne.TextAlignLeading, | ||
theme.Color(theme.ColorNameForeground), | ||
) | ||
|
||
minRednessLbl := widget.NewLabel(lang.X("packJson.minRedness.label", "Minimum Redness")) | ||
minRednessEntry := widgets.NewSpinner(-1, 1, 0.001) | ||
minRednessEntry.Bind(binding.BindFloat(&dlg.ColorOverrides.MinRedness)) | ||
|
||
minSaturationLbl := widget.NewLabel(lang.X("packJson.minSaturation.label", "Minimum Saturation")) | ||
minSaturationEntry := widgets.NewSpinner(-1, 1, 0.001) | ||
minSaturationEntry.Bind(binding.BindFloat(&dlg.ColorOverrides.MinSaturation)) | ||
|
||
rednessToleranceLbl := widget.NewLabel(lang.X("packJson.redTolerance.label", "Redness Tolerance")) | ||
rednessToleranceEntry := widgets.NewSpinner(-1, 1, 0.001) | ||
rednessToleranceEntry.Bind(binding.BindFloat(&dlg.ColorOverrides.RedTolerance)) | ||
|
||
customColorsContainer := container.New( | ||
layout.NewFormLayout(), | ||
minRednessLbl, minRednessEntry, | ||
minSaturationLbl, minSaturationEntry, | ||
rednessToleranceLbl, rednessToleranceEntry, | ||
) | ||
|
||
if !dlg.ColorOverrides.Enabled { | ||
customColorsContainer.Hide() | ||
} | ||
|
||
customColorsCheck := widget.NewCheck(lang.X("packJson.customColors.enable.label", "Override custom color settings"), func(checked bool) { | ||
dlg.ColorOverrides.Enabled = checked | ||
if checked { | ||
customColorsContainer.Show() | ||
} else { | ||
customColorsContainer.Hide() | ||
} | ||
}) | ||
|
||
if !dlg.editable { | ||
nameEntry.Disable() | ||
authorEntry.Disable() | ||
versionEntry.Disable() | ||
keywordsEntry.Disable() | ||
thirdPartyCheck.Disable() | ||
customColorsCheck.Disable() | ||
minRednessEntry.Disable() | ||
minSaturationEntry.Disable() | ||
rednessToleranceEntry.Disable() | ||
} | ||
|
||
dlg.content = container.NewVBox( | ||
container.New( | ||
layout.NewFormLayout(), | ||
|
||
nameLbl, nameEntry, | ||
authorLbl, authorEntry, | ||
versionLbl, versionEntry, | ||
keywordsLbl, keywordsEntry, | ||
), | ||
thirdPartyCheck, | ||
customColorsMsg, | ||
customColorsCheck, | ||
customColorsContainer, | ||
) | ||
|
||
dlg.dialog = dialog.NewCustom( | ||
lang.X("packJson.dialog.title", "Package settings"), | ||
lang.X("packJson.dismis", "Close"), | ||
dlg.content, | ||
dlg.parent, | ||
) | ||
|
||
defaultButtons := []fyne.CanvasObject{ | ||
widget.NewButton(lang.X("packJson.dialog.dismis", "Cancel"), dlg.dialog.Hide), | ||
widget.NewButtonWithIcon(lang.X("packJson.dialog.save", "Save"), theme.DocumentSaveIcon(), dlg.onSave), | ||
} | ||
|
||
if dlg.editable { | ||
dlg.dialog.SetButtons(defaultButtons) | ||
} | ||
} | ||
|
||
func (dlg *PackJSONDialog) onSave() { | ||
dlg.dialog.Hide() | ||
options := ddpackage.SavePackageJSONOptions{ | ||
Path: dlg.Path, | ||
Name: dlg.Name, | ||
ID: dlg.id, | ||
Author: dlg.Author, | ||
Version: dlg.Version, | ||
Keywords: dlg.Keywords, | ||
ColorOverides: *dlg.ColorOverrides, | ||
} | ||
if dlg.Allow3rdParty || dlg.sourceHas3rdParty { | ||
allow := dlg.Allow3rdParty | ||
options.Allow3rdParty = &allow | ||
} | ||
if dlg.callback != nil { | ||
dlg.callback(options) | ||
} | ||
} | ||
|
||
func (dlg *PackJSONDialog) Show() { | ||
dlg.dialog.Show() | ||
} | ||
|
||
func (dlg *PackJSONDialog) Hide() { | ||
dlg.dialog.Hide() | ||
} |
Oops, something went wrong.