doodle/pkg/editor_ui_palette.go
Noah Petherbridge 864156da53 Settings Window + Bugfix
* Added a Settings window for game options, such as enabling the
  horizontal toolbars in Edit Mode. The Settings window also has a
  Controls tab showing the gameplay buttons and keyboard shortcuts.
* The Settings window is available as a button on the home screen OR
  from the Edit->Settings menu in the EditScene.
* Bugfix: using WASD to move the player character now works better and
  is considered by the game to be identical to the arrow key inputs. Boy
  now updates his animation based on these keys, and they register as
  boolean on/off keys instead of affected by key-repeat.
* Refactor the boolProps: they are all part of usercfg now, and if you
  run e.g. "boolProp show-all-doodads true" and then cause the user
  settings to save to disk, that boolProp will be permanently enabled
  until turned off again.
2021-06-19 22:14:41 -07:00

118 lines
2.7 KiB
Go

package doodle
import (
"fmt"
"git.kirsle.net/apps/doodle/pkg/balance"
"git.kirsle.net/apps/doodle/pkg/log"
"git.kirsle.net/apps/doodle/pkg/usercfg"
"git.kirsle.net/go/ui"
)
// Width of the panel frame.
var paletteWidth = 50
// SetupPalette sets up the palette panel.
func (u *EditorUI) SetupPalette(d *Doodle) *ui.Window {
window := ui.NewWindow("Palette")
window.ConfigureTitle(balance.TitleConfig)
_, label := window.TitleBar()
label.Font = balance.TitleFont
window.Configure(ui.Config{
Background: balance.WindowBackground,
BorderColor: balance.WindowBorder,
})
// Color Palette Frame.
u.PaletteTab = u.setupPaletteFrame(window)
window.Pack(u.PaletteTab, ui.Pack{
Side: ui.N,
Fill: true,
})
return window
}
// setupPaletteFrame configures the Color Palette tab for Edit Mode.
// This is a subroutine of editor_ui.go#SetupPalette()
func (u *EditorUI) setupPaletteFrame(window *ui.Window) *ui.Frame {
frame := ui.NewFrame("Palette Tab")
frame.SetBackground(balance.WindowBackground)
var (
packAlign = ui.N
packConfig = ui.Pack{
Side: packAlign,
Fill: true,
PadY: 4,
}
tooltipEdge = ui.Left
buttonSize = 32
)
if usercfg.Current.HorizontalToolbars {
packAlign = ui.W
packConfig = ui.Pack{
Side: packAlign,
Fill: true,
PadX: 2,
}
tooltipEdge = ui.Top
buttonSize = 24
}
// Handler function for the radio buttons being clicked.
onClick := func(ed ui.EventData) error {
name := u.selectedSwatch
swatch, ok := u.Canvas.Palette.Get(name)
if !ok {
log.Error("Palette onClick: couldn't get swatch named '%s' from palette", name)
return nil
}
log.Info("Set swatch: %s", swatch)
u.Canvas.SetSwatch(swatch)
return nil
}
// Draw the radio buttons for the palette.
if u.Canvas != nil && u.Canvas.Palette != nil {
for _, swatch := range u.Canvas.Palette.Swatches {
swFrame := ui.NewFrame(fmt.Sprintf("Swatch(%s) Button Frame", swatch.Name))
swFrame.Configure(ui.Config{
Width: buttonSize,
Height: buttonSize,
Background: swatch.Color,
})
btn := ui.NewRadioButton("palette", &u.selectedSwatch, swatch.Name, swFrame)
btn.Handle(ui.Click, onClick)
u.Supervisor.Add(btn)
// Add a tooltip showing the swatch attributes.
ui.NewTooltip(btn, ui.Tooltip{
Text: fmt.Sprintf("Name: %s\nAttributes: %s", swatch.Name, swatch.Attributes()),
Edge: tooltipEdge,
})
btn.Compute(u.d.Engine)
frame.Pack(btn, packConfig)
}
}
// Draw the Edit Palette button.
btn := ui.NewButton("Edit Palette", ui.NewLabel(ui.Label{
Text: "Edit",
Font: balance.MenuFont,
}))
btn.Handle(ui.Click, func(ed ui.EventData) error {
u.OpenPaletteWindow()
return nil
})
u.Supervisor.Add(btn)
btn.Compute(u.d.Engine)
frame.Pack(btn, packConfig)
return frame
}