Add the ui.ColorPicker

This commit is contained in:
Noah 2022-01-01 18:48:34 -08:00
parent fa5f303dad
commit 690fdedb91
3 changed files with 87 additions and 34 deletions

View File

@ -138,6 +138,11 @@ func NewCanvas(size int, editable bool) *Canvas {
return w return w
} }
// Destroy the canvas.
//
// TODO: Not implemented, here to satisfy ui.Widget, should free up textures tho.
func (w *Canvas) Destroy() {}
// Load initializes the Canvas using an existing Palette and Grid. // Load initializes the Canvas using an existing Palette and Grid.
func (w *Canvas) Load(p *level.Palette, g *level.Chunker) { func (w *Canvas) Load(p *level.Palette, g *level.Chunker) {
w.Palette = p w.Palette = p

View File

@ -180,6 +180,14 @@ func NewPaletteEditor(config PaletteEditor) *ui.Window {
Height: 24, Height: 24,
}) })
btnColor.Handle(ui.Click, func(ed ui.EventData) error { btnColor.Handle(ui.Click, func(ed ui.EventData) error {
// Open a ColorPicker widget.
picker, err := ui.NewColorPicker(ui.ColorPicker{
Title: "Select a color",
Supervisor: config.Supervisor,
Engine: config.Engine,
Color: swatch.Color,
OnManualInput: func(callback func(render.Color)) {
// Prompt the user to enter a hex color using the developer shell.
shmem.Prompt(fmt.Sprintf( shmem.Prompt(fmt.Sprintf(
"New color in hex notation [%s]: ", swatch.Color.ToHex()), func(answer string) { "New color in hex notation [%s]: ", swatch.Color.ToHex()), func(answer string) {
if answer != "" { if answer != "" {
@ -194,6 +202,17 @@ func NewPaletteEditor(config PaletteEditor) *ui.Window {
return return
} }
callback(color)
}
})
},
})
if err != nil {
log.Error("Couldn't open ColorPicker: %s", err)
return err
}
picker.Then(func(color render.Color) {
swatch.Color = color swatch.Color = color
// TODO: redundant from above, consolidate these // TODO: redundant from above, consolidate these
@ -210,8 +229,11 @@ func NewPaletteEditor(config PaletteEditor) *ui.Window {
if config.OnChange != nil { if config.OnChange != nil {
config.OnChange() config.OnChange()
} }
}
}) })
picker.Center(shmem.CurrentRenderEngine.WindowSize())
picker.Show()
return nil return nil
}) })
config.Supervisor.Add(btnColor) config.Supervisor.Add(btnColor)

View File

@ -300,16 +300,38 @@ func (c Settings) makeOptionsTab(tabFrame *ui.TabFrame, Width, Height int) *ui.F
style.HoverBackground = style.Background.Lighten(20) style.HoverBackground = style.Background.Lighten(20)
btn.SetStyle(&style) btn.SetStyle(&style)
btn.Handle(ui.Click, func(ed ui.EventData) error { btn.Handle(ui.Click, func(ed ui.EventData) error {
shmem.Prompt("Enter color in hexadecimal notation: ", func(answer string) { // Open a ColorPicker widget.
if answer == "" { picker, err := ui.NewColorPicker(ui.ColorPicker{
return Title: "Select a color",
Supervisor: c.Supervisor,
Engine: c.Engine,
Color: *row.Color,
OnManualInput: func(callback func(render.Color)) {
// Prompt the user to enter a hex color using the developer shell.
shmem.Prompt("New color in hex notation: ", func(answer string) {
if answer != "" {
// XXX: pure white renders as invisible, fudge it a bit.
if answer == "FFFFFF" {
answer = "FFFFFE"
} }
color, err := render.HexColor(answer) color, err := render.HexColor(answer)
if err != nil { if err != nil {
shmem.FlashError("Invalid color value: %s", err) shmem.Flash("Error with that color code: %s", err)
return
} }
callback(color)
}
})
},
})
if err != nil {
log.Error("Couldn't open ColorPicker: %s", err)
return err
}
picker.Then(func(color render.Color) {
*row.Color = color *row.Color = color
style.Background = color style.Background = color
style.HoverBackground = style.Background.Lighten(20) style.HoverBackground = style.Background.Lighten(20)
@ -317,6 +339,10 @@ func (c Settings) makeOptionsTab(tabFrame *ui.TabFrame, Width, Height int) *ui.F
// call onClick to save change to disk now // call onClick to save change to disk now
onClick(ed) onClick(ed)
}) })
picker.Center(shmem.CurrentRenderEngine.WindowSize())
picker.Show()
return nil return nil
}) })