Add the ui.ColorPicker

pull/84/head
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,38 +180,60 @@ 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 {
shmem.Prompt(fmt.Sprintf( // Open a ColorPicker widget.
"New color in hex notation [%s]: ", swatch.Color.ToHex()), func(answer string) { picker, err := ui.NewColorPicker(ui.ColorPicker{
if answer != "" { Title: "Select a color",
// XXX: pure white renders as invisible, fudge it a bit. Supervisor: config.Supervisor,
if answer == "FFFFFF" { Engine: config.Engine,
answer = "FFFFFE" 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) color, err := render.HexColor(answer)
if err != nil { if err != nil {
shmem.Flash("Error with that color code: %s", err) shmem.Flash("Error with that color code: %s", err)
return return
} }
swatch.Color = color callback(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 err != nil {
log.Error("Couldn't open ColorPicker: %s", err)
return err
}
if config.OnChange != nil { picker.Then(func(color render.Color) {
config.OnChange() 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 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
}) })