doodle/pkg/editor_ui_palette.go
Noah Petherbridge f0101ba048 The Window Manager Update
* Take advantage of the new Window Manager feature of the UI toolkit.
* Move the MenuScene's "New Level" and "Play/Edit Level" windows into
  stand-alone functions in new pkg/windows/ package. The 'windows'
  package is isolated from the rest of Doodle and communicates using
  config variables and callback functions to avoid circular dependency.
* MenuScene calls the window constructors from the new package.
* Add an "Options" button to the Menu Bar in the Editor Scene, which
  opens the "New Level" window to allow changing the wallpaper or
  bounding type of the level currently being edited.
* Move the cheat codes into their own file, cheats.go
2020-04-06 23:21:17 -07:00

123 lines
3.0 KiB
Go

package doodle
import (
"fmt"
"git.kirsle.net/apps/doodle/pkg/balance"
"git.kirsle.net/apps/doodle/pkg/log"
"git.kirsle.net/go/ui"
)
// SetupPalette sets up the palette panel.
func (u *EditorUI) SetupPalette(d *Doodle) *ui.Window {
window := ui.NewWindow("Palette")
window.ConfigureTitle(balance.TitleConfig)
// window.TitleBar().Font = balance.TitleFont
window.Configure(ui.Config{
Background: balance.WindowBackground,
BorderColor: balance.WindowBorder,
})
// Doodad frame.
{
frame, err := u.setupDoodadFrame(d.Engine, window)
if err != nil {
d.Flash(err.Error())
}
// Even if there was an error (userdir.ListDoodads couldn't read the
// config folder on disk or whatever) the Frame is still valid but
// empty, which is still the intended behavior.
u.DoodadTab = frame
u.DoodadTab.Hide()
window.Pack(u.DoodadTab, ui.Pack{
Side: ui.N,
Fill: true,
})
}
// 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)
// 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))
colorFrame := ui.NewFrame(fmt.Sprintf("Swatch(%s) Color Box", swatch.Name))
colorFrame.Configure(ui.Config{
Width: 16,
Height: 16,
Background: swatch.Color,
BorderSize: 1,
BorderStyle: ui.BorderSunken,
})
swFrame.Pack(colorFrame, ui.Pack{
Side: ui.W,
})
label := ui.NewLabel(ui.Label{
Text: swatch.Name,
Font: balance.StatusFont,
})
label.Font.Color = swatch.Color.Darken(128)
swFrame.Pack(label, ui.Pack{
Side: ui.W,
})
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: "Attributes: " + swatch.Attributes(),
Edge: ui.Left,
})
btn.Compute(u.d.Engine)
swFrame.Configure(ui.Config{
Height: label.Size().H,
// TODO: magic number, trying to left-align
// the label by making the frame as wide as possible.
Width: paletteWidth - 16,
})
frame.Pack(btn, ui.Pack{
Side: ui.N,
Fill: true,
PadY: 4,
})
}
}
return frame
}