2018-08-05 19:54:57 +00:00
|
|
|
package doodle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/doodle/balance"
|
|
|
|
"git.kirsle.net/apps/doodle/events"
|
|
|
|
"git.kirsle.net/apps/doodle/render"
|
|
|
|
"git.kirsle.net/apps/doodle/ui"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EditorUI manages the user interface for the Editor Scene.
|
|
|
|
type EditorUI struct {
|
2018-08-11 00:19:47 +00:00
|
|
|
d *Doodle
|
|
|
|
Scene *EditorScene
|
2018-08-05 19:54:57 +00:00
|
|
|
|
|
|
|
// Variables
|
2018-08-11 00:19:47 +00:00
|
|
|
StatusMouseText string
|
|
|
|
StatusPaletteText string
|
|
|
|
StatusFilenameText string
|
2018-08-05 19:54:57 +00:00
|
|
|
|
|
|
|
// Widgets
|
|
|
|
Supervisor *ui.Supervisor
|
2018-08-11 00:19:47 +00:00
|
|
|
Palette *ui.Window
|
2018-08-05 19:54:57 +00:00
|
|
|
StatusBar *ui.Frame
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEditorUI initializes the Editor UI.
|
2018-08-11 00:19:47 +00:00
|
|
|
func NewEditorUI(d *Doodle, s *EditorScene) *EditorUI {
|
2018-08-05 19:54:57 +00:00
|
|
|
u := &EditorUI{
|
2018-08-11 00:19:47 +00:00
|
|
|
d: d,
|
|
|
|
Scene: s,
|
|
|
|
Supervisor: ui.NewSupervisor(),
|
|
|
|
StatusMouseText: "Cursor: (waiting)",
|
|
|
|
StatusPaletteText: "Swatch: <none>",
|
|
|
|
StatusFilenameText: "Filename: <none>",
|
2018-08-05 19:54:57 +00:00
|
|
|
}
|
|
|
|
u.StatusBar = u.SetupStatusBar(d)
|
2018-08-11 00:19:47 +00:00
|
|
|
u.Palette = u.SetupPalette(d)
|
2018-08-05 19:54:57 +00:00
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop to process events and update the UI.
|
|
|
|
func (u *EditorUI) Loop(ev *events.State) {
|
2018-08-11 00:19:47 +00:00
|
|
|
u.Supervisor.Loop(ev)
|
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
u.StatusMouseText = fmt.Sprintf("Mouse: (%d,%d)",
|
|
|
|
ev.CursorX.Now,
|
|
|
|
ev.CursorY.Now,
|
|
|
|
)
|
2018-08-11 00:19:47 +00:00
|
|
|
u.StatusPaletteText = fmt.Sprintf("Swatch: %s",
|
|
|
|
u.Scene.Swatch,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Statusbar filename label.
|
|
|
|
filename := "untitled.map"
|
|
|
|
if u.Scene.filename != "" {
|
|
|
|
filename = u.Scene.filename
|
|
|
|
}
|
|
|
|
u.StatusFilenameText = fmt.Sprintf("Filename: %s",
|
|
|
|
filename,
|
|
|
|
)
|
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
u.StatusBar.Compute(u.d.Engine)
|
2018-08-11 00:19:47 +00:00
|
|
|
u.Palette.Compute(u.d.Engine)
|
2018-08-05 19:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Present the UI to the screen.
|
|
|
|
func (u *EditorUI) Present(e render.Engine) {
|
2018-08-11 00:19:47 +00:00
|
|
|
u.Palette.Present(e, u.Palette.Point())
|
2018-08-05 19:54:57 +00:00
|
|
|
u.StatusBar.Present(e, u.StatusBar.Point())
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// SetupPalette sets up the palette panel.
|
|
|
|
func (u *EditorUI) SetupPalette(d *Doodle) *ui.Window {
|
|
|
|
window := ui.NewWindow("Palette")
|
|
|
|
window.Configure(ui.Config{
|
|
|
|
Width: 150,
|
|
|
|
Height: u.d.height - u.StatusBar.Size().H,
|
|
|
|
})
|
|
|
|
window.MoveTo(render.NewPoint(
|
|
|
|
u.d.width-window.BoxSize().W,
|
|
|
|
0,
|
|
|
|
))
|
|
|
|
|
|
|
|
// Handler function for the radio buttons being clicked.
|
|
|
|
onClick := func(p render.Point) {
|
|
|
|
name := u.Scene.Palette.ActiveSwatch
|
|
|
|
swatch, ok := u.Scene.Palette.Get(name)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Palette onClick: couldn't get swatch named '%s' from palette", name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u.Scene.Swatch = swatch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the radio buttons for the palette.
|
|
|
|
for _, swatch := range u.Scene.Palette.Swatches {
|
|
|
|
label := ui.NewLabel(ui.Label{
|
|
|
|
Text: swatch.Name,
|
|
|
|
Font: balance.StatusFont,
|
|
|
|
})
|
|
|
|
label.Font.Color = swatch.Color.Darken(40)
|
|
|
|
|
|
|
|
btn := ui.NewRadioButton("palette", &u.Scene.Palette.ActiveSwatch, swatch.Name, label)
|
|
|
|
btn.Handle("MouseUp", onClick)
|
|
|
|
u.Supervisor.Add(btn)
|
|
|
|
|
|
|
|
window.Pack(btn, ui.Pack{
|
|
|
|
Anchor: ui.N,
|
|
|
|
Fill: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return window
|
|
|
|
}
|
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
// SetupStatusBar sets up the status bar widget along the bottom of the window.
|
|
|
|
func (u *EditorUI) SetupStatusBar(d *Doodle) *ui.Frame {
|
|
|
|
frame := ui.NewFrame("Status Bar")
|
|
|
|
frame.Configure(ui.Config{
|
|
|
|
BorderStyle: ui.BorderRaised,
|
|
|
|
Background: render.Grey,
|
|
|
|
BorderSize: 2,
|
|
|
|
Width: d.width,
|
|
|
|
})
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
style := ui.Config{
|
2018-08-05 19:54:57 +00:00
|
|
|
Background: render.Grey,
|
|
|
|
BorderStyle: ui.BorderSunken,
|
|
|
|
BorderColor: render.Grey,
|
|
|
|
BorderSize: 1,
|
2018-08-11 00:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cursorLabel := ui.NewLabel(ui.Label{
|
|
|
|
TextVariable: &u.StatusMouseText,
|
|
|
|
Font: balance.StatusFont,
|
2018-08-05 19:54:57 +00:00
|
|
|
})
|
2018-08-11 00:19:47 +00:00
|
|
|
cursorLabel.Configure(style)
|
2018-08-05 19:54:57 +00:00
|
|
|
cursorLabel.Compute(d.Engine)
|
|
|
|
frame.Pack(cursorLabel, ui.Pack{
|
|
|
|
Anchor: ui.W,
|
|
|
|
})
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
paletteLabel := ui.NewLabel(ui.Label{
|
|
|
|
TextVariable: &u.StatusPaletteText,
|
|
|
|
Font: balance.StatusFont,
|
2018-08-05 19:54:57 +00:00
|
|
|
})
|
2018-08-11 00:19:47 +00:00
|
|
|
paletteLabel.Configure(style)
|
|
|
|
paletteLabel.Compute(d.Engine)
|
|
|
|
frame.Pack(paletteLabel, ui.Pack{
|
|
|
|
Anchor: ui.W,
|
2018-08-05 19:54:57 +00:00
|
|
|
})
|
2018-08-11 00:19:47 +00:00
|
|
|
|
|
|
|
filenameLabel := ui.NewLabel(ui.Label{
|
|
|
|
TextVariable: &u.StatusFilenameText,
|
|
|
|
Font: balance.StatusFont,
|
|
|
|
})
|
|
|
|
filenameLabel.Configure(style)
|
2018-08-05 19:54:57 +00:00
|
|
|
filenameLabel.Compute(d.Engine)
|
|
|
|
frame.Pack(filenameLabel, ui.Pack{
|
|
|
|
Anchor: ui.W,
|
|
|
|
})
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// TODO: right-aligned labels clip out of bounds
|
|
|
|
// extraLabel := ui.NewLabel(ui.Label{
|
|
|
|
// Text: "blah",
|
|
|
|
// Font: balance.StatusFont,
|
|
|
|
// })
|
|
|
|
// extraLabel.Configure(ui.Config{
|
|
|
|
// Background: render.Grey,
|
|
|
|
// BorderStyle: ui.BorderSunken,
|
|
|
|
// BorderColor: render.Grey,
|
|
|
|
// BorderSize: 1,
|
|
|
|
// })
|
|
|
|
// extraLabel.Compute(d.Engine)
|
|
|
|
// frame.Pack(extraLabel, ui.Pack{
|
|
|
|
// Anchor: ui.E,
|
|
|
|
// })
|
2018-08-05 19:54:57 +00:00
|
|
|
|
|
|
|
frame.Resize(render.Rect{
|
|
|
|
W: d.width,
|
|
|
|
H: cursorLabel.BoxSize().H + frame.BoxThickness(1),
|
|
|
|
})
|
|
|
|
frame.Compute(d.Engine)
|
|
|
|
frame.MoveTo(render.Point{
|
|
|
|
X: 0,
|
|
|
|
Y: d.height - frame.Size().H,
|
|
|
|
})
|
|
|
|
|
|
|
|
return frame
|
|
|
|
}
|