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
}
// 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

View File

@ -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)

View File

@ -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
})