2021-10-04 04:18:39 +00:00
|
|
|
package windows
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/drawtool"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/level"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/shmem"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/uix"
|
|
|
|
"git.kirsle.net/go/render"
|
|
|
|
"git.kirsle.net/go/render/event"
|
|
|
|
"git.kirsle.net/go/ui"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PiP window.
|
|
|
|
type PiP struct {
|
|
|
|
// Settings passed in by doodle
|
|
|
|
Supervisor *ui.Supervisor
|
|
|
|
Engine render.Engine
|
|
|
|
Level *level.Level
|
|
|
|
Event *event.State
|
2021-10-07 05:22:34 +00:00
|
|
|
Tool *drawtool.Tool
|
|
|
|
BrushSize *int
|
|
|
|
|
|
|
|
// Or sensible defaults:
|
|
|
|
Width int
|
|
|
|
Height int
|
2021-10-04 04:18:39 +00:00
|
|
|
|
|
|
|
OnCancel func()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakePiPWindow initializes a license window for any scene.
|
|
|
|
// The window width/height are the actual SDL2 window dimensions.
|
|
|
|
func MakePiPWindow(windowWidth, windowHeight int, cfg PiP) *ui.Window {
|
|
|
|
win := NewPiPWindow(cfg)
|
|
|
|
win.Compute(cfg.Engine)
|
|
|
|
win.Supervise(cfg.Supervisor)
|
|
|
|
|
|
|
|
// Center the window.
|
|
|
|
size := win.Size()
|
|
|
|
win.MoveTo(render.Point{
|
|
|
|
X: (windowWidth / 2) - (size.W / 2),
|
|
|
|
Y: (windowHeight / 2) - (size.H / 2),
|
|
|
|
})
|
|
|
|
|
|
|
|
return win
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPiPWindow initializes the window.
|
|
|
|
func NewPiPWindow(cfg PiP) *ui.Window {
|
|
|
|
var (
|
|
|
|
windowWidth = 340
|
2021-10-07 05:22:34 +00:00
|
|
|
windowHeight = 300
|
|
|
|
)
|
|
|
|
|
|
|
|
if cfg.Width+cfg.Height > 0 {
|
|
|
|
windowWidth = cfg.Width
|
|
|
|
windowHeight = cfg.Height
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
canvasWidth = windowWidth - 8
|
|
|
|
canvasHeight = windowHeight - 4 - 48 // for the titlebar?
|
2021-10-04 04:18:39 +00:00
|
|
|
)
|
|
|
|
|
2021-10-07 05:22:34 +00:00
|
|
|
window := ui.NewWindow("Viewport")
|
2021-10-04 04:18:39 +00:00
|
|
|
window.SetButtons(ui.CloseButton)
|
|
|
|
window.Configure(ui.Config{
|
2021-10-07 05:22:34 +00:00
|
|
|
Width: windowWidth,
|
|
|
|
Height: windowHeight,
|
2021-10-04 04:18:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
canvas := uix.NewCanvas(128, true)
|
2021-10-07 05:22:34 +00:00
|
|
|
canvas.Name = "Viewport (WIP)"
|
2021-10-04 04:18:39 +00:00
|
|
|
canvas.LoadLevel(cfg.Level)
|
|
|
|
canvas.InstallActors(cfg.Level.Actors)
|
|
|
|
canvas.Scrollable = true
|
|
|
|
canvas.Editable = true
|
2021-10-07 05:22:34 +00:00
|
|
|
canvas.Resize(render.NewRect(canvasWidth, canvasHeight))
|
2021-10-04 04:18:39 +00:00
|
|
|
|
|
|
|
// NOTE: my UI toolkit calls this every tick, if this is "fixed"
|
|
|
|
// in the future make one that does.
|
2021-10-07 05:22:34 +00:00
|
|
|
var (
|
|
|
|
curTool = *cfg.Tool
|
|
|
|
curThicc = *cfg.BrushSize
|
|
|
|
)
|
|
|
|
canvas.Tool = curTool
|
2021-10-04 04:18:39 +00:00
|
|
|
window.Handle(ui.MouseMove, func(ed ui.EventData) error {
|
|
|
|
canvas.Loop(cfg.Event)
|
2021-10-07 05:22:34 +00:00
|
|
|
|
|
|
|
// Check if bound values have modified.
|
|
|
|
if *cfg.Tool != curTool {
|
|
|
|
curTool = *cfg.Tool
|
|
|
|
canvas.Tool = curTool
|
|
|
|
}
|
|
|
|
if *cfg.BrushSize != curThicc {
|
|
|
|
curThicc = *cfg.BrushSize
|
|
|
|
canvas.BrushSize = curThicc
|
|
|
|
}
|
2021-10-04 04:18:39 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
window.Pack(canvas, ui.Pack{
|
|
|
|
Side: ui.N,
|
|
|
|
FillX: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
// Buttons at bottom of window
|
|
|
|
|
|
|
|
bottomFrame := ui.NewFrame("Button Frame")
|
|
|
|
window.Pack(bottomFrame, ui.Pack{
|
|
|
|
Side: ui.N,
|
|
|
|
FillX: true,
|
2021-10-07 05:22:34 +00:00
|
|
|
PadY: 4,
|
2021-10-04 04:18:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
frame := ui.NewFrame("Button frame")
|
|
|
|
buttons := []struct {
|
|
|
|
label string
|
|
|
|
tooltip string
|
|
|
|
f func()
|
|
|
|
}{
|
2021-10-07 05:22:34 +00:00
|
|
|
{"Smaller", "Shrink this viewport window by 20%", func() {
|
|
|
|
// Make a smaller version of the same window, and close.
|
|
|
|
cfg.Width = int(float64(windowWidth) * 0.8)
|
|
|
|
cfg.Height = int(float64(windowHeight) * 0.8)
|
|
|
|
pip := MakePiPWindow(cfg.Width, cfg.Height, cfg)
|
|
|
|
pip.MoveTo(window.Point())
|
|
|
|
window.Close()
|
|
|
|
pip.Show()
|
|
|
|
}},
|
|
|
|
{"Larger", "Grow this viewport window by 20%", func() {
|
|
|
|
// Make a smaller version of the same window, and close.
|
|
|
|
cfg.Width = int(float64(windowWidth) * 1.2)
|
|
|
|
cfg.Height = int(float64(windowHeight) * 1.2)
|
|
|
|
pip := MakePiPWindow(cfg.Width, cfg.Height, cfg)
|
|
|
|
pip.MoveTo(window.Point())
|
|
|
|
window.Close()
|
|
|
|
pip.Show()
|
|
|
|
}},
|
|
|
|
{"Refresh", "Update the state of doodads placed in this level", func() {
|
|
|
|
canvas.ClearActors()
|
|
|
|
canvas.InstallActors(cfg.Level.Actors)
|
|
|
|
}},
|
|
|
|
{"Rename", "Give this viewport window a custom name", func() {
|
|
|
|
shmem.Prompt("Give this viewport a name: ", func(answer string) {
|
|
|
|
if answer == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
window.Title = answer
|
|
|
|
})
|
2021-10-04 04:18:39 +00:00
|
|
|
}},
|
2021-10-07 05:22:34 +00:00
|
|
|
{"???", "Load a different drawing (experimental!)", func() {
|
2021-10-04 04:18:39 +00:00
|
|
|
shmem.Prompt("Filename to open: ", func(answer string) {
|
|
|
|
if answer == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if lvl, err := level.LoadFile(answer); err == nil {
|
|
|
|
canvas.ClearActors()
|
|
|
|
canvas.LoadLevel(lvl)
|
|
|
|
canvas.InstallActors(lvl.Actors)
|
|
|
|
} else {
|
|
|
|
shmem.Flash(err.Error())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}},
|
|
|
|
}
|
2021-10-07 05:22:34 +00:00
|
|
|
for i, button := range buttons {
|
|
|
|
// Start axing buttons if window size is too small.
|
|
|
|
if windowWidth < 150 && i > 1 {
|
|
|
|
break
|
|
|
|
} else if windowWidth < 250 && i > 2 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-10-04 04:18:39 +00:00
|
|
|
button := button
|
|
|
|
|
|
|
|
btn := ui.NewButton(button.label, ui.NewLabel(ui.Label{
|
|
|
|
Text: button.label,
|
2021-10-07 05:22:34 +00:00
|
|
|
Font: balance.SmallFont,
|
2021-10-04 04:18:39 +00:00
|
|
|
}))
|
|
|
|
|
2021-10-07 05:22:34 +00:00
|
|
|
btn.Handle(ui.Click, func(ed ui.EventData) error {
|
|
|
|
button.f()
|
|
|
|
return nil
|
|
|
|
})
|
2021-10-04 04:18:39 +00:00
|
|
|
|
|
|
|
btn.Compute(cfg.Engine)
|
|
|
|
cfg.Supervisor.Add(btn)
|
|
|
|
|
|
|
|
ui.NewTooltip(btn, ui.Tooltip{
|
|
|
|
Text: button.tooltip,
|
|
|
|
Edge: ui.Top,
|
|
|
|
})
|
|
|
|
|
|
|
|
frame.Pack(btn, ui.Pack{
|
|
|
|
Side: ui.W,
|
2021-10-07 05:22:34 +00:00
|
|
|
PadX: 1,
|
2021-10-04 04:18:39 +00:00
|
|
|
Expand: true,
|
|
|
|
Fill: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
bottomFrame.Pack(frame, ui.Pack{
|
|
|
|
Side: ui.N,
|
|
|
|
PadX: 8,
|
2021-10-07 05:22:34 +00:00
|
|
|
PadY: 0,
|
2021-10-04 04:18:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return window
|
|
|
|
}
|