From 690fdedb919f831182d728bfeb4219527e0e546f Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sat, 1 Jan 2022 18:48:34 -0800 Subject: [PATCH] Add the ui.ColorPicker --- pkg/uix/canvas.go | 5 +++ pkg/windows/palette_editor.go | 74 +++++++++++++++++++++++------------ pkg/windows/settings.go | 42 ++++++++++++++++---- 3 files changed, 87 insertions(+), 34 deletions(-) diff --git a/pkg/uix/canvas.go b/pkg/uix/canvas.go index 11f1f97..8a7874a 100644 --- a/pkg/uix/canvas.go +++ b/pkg/uix/canvas.go @@ -138,6 +138,11 @@ func NewCanvas(size int, editable bool) *Canvas { 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. func (w *Canvas) Load(p *level.Palette, g *level.Chunker) { w.Palette = p diff --git a/pkg/windows/palette_editor.go b/pkg/windows/palette_editor.go index 99d846e..de64137 100644 --- a/pkg/windows/palette_editor.go +++ b/pkg/windows/palette_editor.go @@ -180,38 +180,60 @@ func NewPaletteEditor(config PaletteEditor) *ui.Window { Height: 24, }) btnColor.Handle(ui.Click, func(ed ui.EventData) error { - shmem.Prompt(fmt.Sprintf( - "New color in hex notation [%s]: ", swatch.Color.ToHex()), func(answer string) { - if answer != "" { - // XXX: pure white renders as invisible, fudge it a bit. - if answer == "FFFFFF" { - answer = "FFFFFE" - } + // 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( + "New color in hex notation [%s]: ", swatch.Color.ToHex()), 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) - if err != nil { - shmem.Flash("Error with that color code: %s", err) - return - } + color, err := render.HexColor(answer) + if err != nil { + shmem.Flash("Error with that color code: %s", err) + return + } - swatch.Color = color - - // TODO: redundant from above, consolidate these - fmt.Printf("Set button style to: %s\n", swatch.Color) - btnColor.SetStyle(&style.Button{ - Background: swatch.Color, - HoverBackground: swatch.Color.Lighten(40), - OutlineColor: render.Black, - OutlineSize: 1, - BorderStyle: style.BorderRaised, - BorderSize: 2, + callback(color) + } }) + }, + }) + if err != nil { + log.Error("Couldn't open ColorPicker: %s", err) + return err + } - if config.OnChange != nil { - config.OnChange() - } + picker.Then(func(color render.Color) { + swatch.Color = color + + // TODO: redundant from above, consolidate these + fmt.Printf("Set button style to: %s\n", swatch.Color) + btnColor.SetStyle(&style.Button{ + Background: swatch.Color, + HoverBackground: swatch.Color.Lighten(40), + OutlineColor: render.Black, + OutlineSize: 1, + BorderStyle: style.BorderRaised, + BorderSize: 2, + }) + + if config.OnChange != nil { + config.OnChange() } }) + + picker.Center(shmem.CurrentRenderEngine.WindowSize()) + picker.Show() + return nil }) config.Supervisor.Add(btnColor) diff --git a/pkg/windows/settings.go b/pkg/windows/settings.go index 948d5c4..c186512 100644 --- a/pkg/windows/settings.go +++ b/pkg/windows/settings.go @@ -300,16 +300,38 @@ func (c Settings) makeOptionsTab(tabFrame *ui.TabFrame, Width, Height int) *ui.F style.HoverBackground = style.Background.Lighten(20) btn.SetStyle(&style) btn.Handle(ui.Click, func(ed ui.EventData) error { - shmem.Prompt("Enter color in hexadecimal notation: ", func(answer string) { - if answer == "" { - return - } + // Open a ColorPicker widget. + picker, err := ui.NewColorPicker(ui.ColorPicker{ + 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) - if err != nil { - shmem.FlashError("Invalid color value: %s", err) - } + color, err := render.HexColor(answer) + if err != nil { + 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 style.Background = color 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 onClick(ed) }) + + picker.Center(shmem.CurrentRenderEngine.WindowSize()) + picker.Show() + return nil })