widget manager refact

This commit is contained in:
2026-08-30 22:27:51 +02:00
parent 258cbf85c0
commit 3890edc946
26 changed files with 413 additions and 242 deletions
+2 -1
View File
@@ -12,6 +12,7 @@ var (
SceneManager = inkwell.NewManager[Scene]()
ScriptManager = inkwell.NewManager[Script]()
ThemeManager = inkwell.NewManager[Theme]()
WidgetManager = inkwell.NewManager[Widget]()
)
type Opts struct {
@@ -43,11 +44,11 @@ func New(o Opts) *inkwell.Game {
g.ItemManager = ItemManager
g.SceneManager = SceneManager
g.ScriptManager = ScriptManager
g.WidgetManager = WidgetManager
ThemeManager.Each(g.ThemeManager.Register)
World.attach(g)
g.UseTheme(RealWorld)
registerUI()
g.StartAt(start)
g.OnStart(ScriptOpening)
-199
View File
@@ -1,199 +0,0 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
const (
ScreenW = 640
ScreenH = 380
LineH = glyphH + 2
Pad = 6
TopBarH = LineH + 2
DividerX = 394
HUDH = Pad + LineH + 4 + invSlot*2 + invGap + Pad
HUDTop = ScreenH - HUDH
statusY = HUDTop + Pad
slotsY = HUDTop + Pad + LineH
slotsH = LineH*2 + Pad*2
invY = slotsY + 4
invSlot = 40
invGap = 4
)
func registerUI() {
g := World.G
g.UIManager.Register(&useWithGuard{
Name: "usewith_guard",
})
g.UIManager.Register(&actionPump{
Name: "pump",
})
g.UIManager.Register(&sceneNav{
Name: "scene_nav",
})
g.UIManager.Register(&windowSizer{
Name: "window",
Scale: 2,
})
g.UIManager.Register(&inkwell.HotspotDebug{
Name: "hotspot_debug",
})
top := &inkwell.TopBar{
Name: "topbar",
Height: TopBarH,
TimeVar: VarArmillaStrip,
}
World.SetTopBar(top)
g.UIManager.Register(gated("topbar_gate", top))
g.UIManager.Register(&letterbox{
Name: "letterbox",
Bar: 36,
})
g.UIManager.Register(&hudFrame{
Name: "hudframe",
})
g.UIManager.Register(gated("status_gate", &inkwell.StatusLine{
Name: "status",
Y: statusY,
Align: inkwell.AlignLeft,
ScreenWidth: DividerX,
}))
g.UIManager.Register(gated("inventory_gate", &inkwell.InventoryBar{
Name: "inventory",
Origin: inkwell.Point{
X: Pad + 2,
Y: invY,
},
Slots: 8,
Cols: 4,
SlotSize: invSlot,
Gap: invGap,
}))
g.UIManager.Register(&tapeSlots{
Name: "tapes",
Bounds: inkwell.Rect(192, slotsY, 194, slotsH),
})
g.UIManager.Register(&tapeChannel{
Name: "channel",
Bounds: inkwell.Rect(DividerX+2, HUDTop+2, ScreenW-DividerX-4, ScreenH-HUDTop-4),
})
g.UIManager.Register(&inkwell.SpeechBubble{
Name: "speech",
MaxWidth: 360,
Padding: Pad,
OffsetY: 8,
FallbackY: TopBarH + Pad,
})
g.UIManager.Register(&inkwell.DialogBox{
Name: "dialog",
Bounds: inkwell.Rect(0, ScreenH-LineH*9, DividerX, LineH*9),
LineHeight: LineH,
Padding: Pad,
})
g.UIManager.Register(&inkwell.RadialVerbs{
Name: "verbs",
Trigger: inkwell.MouseButtonRight,
Radius: 58,
Labels: map[string]string{
"look": "Look", "use": "Use", "talk": "Talk", "take": "Take",
},
})
g.UIManager.Register(&inkwell.EndCard{
Name: "endcard",
})
g.UIManager.Register(&inkwell.Cursor{
Name: "cursor",
})
}
type windowSizer struct {
Name string
Scale int
done bool
}
func (s *windowSizer) GetName() string { return s.Name }
func (s *windowSizer) Draw(dst *ebiten.Image, ctx *inkwell.UICtx) {}
func (s *windowSizer) Tick(ctx *inkwell.UICtx) {
if s.done {
return
}
s.done = true
scale := s.Scale
if scale <= 0 {
scale = 2
}
ebiten.SetWindowSize(ctx.Game.Width*scale, ctx.Game.Height*scale)
}
type gate struct {
Name string
Inner inkwell.Widget
}
func gated(name string, inner inkwell.Widget) inkwell.Widget {
return &gate{
Name: name,
Inner: inner,
}
}
func (n *gate) GetName() string { return n.Name }
func (n *gate) Tick(ctx *inkwell.UICtx) {
if World.HUDVisible() {
n.Inner.Tick(ctx)
}
}
func (n *gate) Draw(dst *ebiten.Image, ctx *inkwell.UICtx) {
if World.HUDVisible() {
n.Inner.Draw(dst, ctx)
}
}
func (n *gate) BlocksClickAt(p inkwell.Point) bool {
if !World.HUDVisible() {
return false
}
b, ok := n.Inner.(interface{ BlocksClickAt(inkwell.Point) bool })
return ok && b.BlocksClickAt(p)
}
type hudFrame struct {
Name string
}
func (h *hudFrame) GetName() string { return h.Name }
func (h *hudFrame) Tick(ctx *inkwell.UICtx) {}
func (h *hudFrame) Draw(dst *ebiten.Image, ctx *inkwell.UICtx) {
if !World.HUDVisible() {
return
}
th := ctx.Game.Theme()
vector.DrawFilledRect(dst, 0, HUDTop+1, ScreenW, ScreenH-HUDTop-1, th.PanelBG, false)
vector.StrokeLine(dst, 0, HUDTop, ScreenW, HUDTop, 1, th.CharacterPanelBorder, false)
vector.StrokeLine(dst, DividerX, HUDTop+1, DividerX, ScreenH, 1, th.CharacterPanelBorder, false)
}
func (h *hudFrame) BlocksClickAt(p inkwell.Point) bool {
return World.HUDVisible() && p.Y >= HUDTop
}
+21
View File
@@ -0,0 +1,21 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
"github.com/hajimehoshi/ebiten/v2"
)
func init() {
WidgetManager.Register(&actionPump{
Name: "pump",
})
}
type actionPump struct {
Name string
}
func (p *actionPump) GetName() string { return p.Name }
func (p *actionPump) Layer() inkwell.Layer { return inkwell.LayerScene }
func (p *actionPump) Tick(ctx *inkwell.UICtx) { World.PumpTick(ctx.DT) }
func (p *actionPump) Draw(dst *ebiten.Image, ctx *inkwell.UICtx) {}
+11
View File
@@ -0,0 +1,11 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
)
func init() {
WidgetManager.Register(&inkwell.Cursor{
Name: "cursor",
})
}
+14
View File
@@ -0,0 +1,14 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
)
func init() {
WidgetManager.Register(&inkwell.DialogBox{
Name: "dialog",
Bounds: inkwell.Rect(0, ScreenH-LineH*9, DividerX, LineH*9),
LineHeight: LineH,
Padding: Pad,
})
}
+11
View File
@@ -0,0 +1,11 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
)
func init() {
WidgetManager.Register(&inkwell.EndCard{
Name: "endcard",
})
}
+11
View File
@@ -0,0 +1,11 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
)
func init() {
WidgetManager.Register(&inkwell.HotspotDebug{
Name: "hotspot_debug",
})
}
+37
View File
@@ -0,0 +1,37 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
func init() {
WidgetManager.Register(&hudFrame{
Name: "hudframe",
})
}
type hudFrame struct {
Name string
}
func (h *hudFrame) GetName() string { return h.Name }
func (h *hudFrame) Layer() inkwell.Layer { return inkwell.LayerPanel }
func (h *hudFrame) Tick(ctx *inkwell.UICtx) {}
func (h *hudFrame) Draw(dst *ebiten.Image, ctx *inkwell.UICtx) {
if !World.HUDVisible() {
return
}
th := ctx.Game.Theme()
vector.DrawFilledRect(dst, 0, HUDTop+1, ScreenW, ScreenH-HUDTop-1, th.PanelBG, false)
vector.StrokeLine(dst, 0, HUDTop, ScreenW, HUDTop, 1, th.CharacterPanelBorder, false)
vector.StrokeLine(dst, DividerX, HUDTop+1, DividerX, ScreenH, 1, th.CharacterPanelBorder, false)
}
func (h *hudFrame) BlocksClickAt(p inkwell.Point) bool {
return World.HUDVisible() && p.Y >= HUDTop
}
+19
View File
@@ -0,0 +1,19 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
)
func init() {
WidgetManager.Register(gated("inventory", &inkwell.InventoryBar{
Name: "inventory",
Origin: inkwell.Point{
X: Pad + 2,
Y: invY,
},
Slots: 8,
Cols: 4,
SlotSize: invSlot,
Gap: invGap,
}))
}
@@ -7,12 +7,20 @@ import (
"github.com/hajimehoshi/ebiten/v2/vector"
)
func init() {
WidgetManager.Register(&letterbox{
Name: "letterbox",
Bar: 36,
})
}
type letterbox struct {
Name string
Bar float64
}
func (l *letterbox) GetName() string { return l.Name }
func (l *letterbox) GetName() string { return l.Name }
func (l *letterbox) Layer() inkwell.Layer { return inkwell.LayerScene }
func (l *letterbox) Tick(ctx *inkwell.UICtx) {
if World.Mode() != ModeCutscene || !World.TapeWaiting() {
+65
View File
@@ -0,0 +1,65 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
"github.com/hajimehoshi/ebiten/v2"
)
type Widget = inkwell.Widget
const (
ScreenW = 640
ScreenH = 380
LineH = glyphH + 2
Pad = 6
TopBarH = LineH + 2
DividerX = 394
HUDH = Pad + LineH + 4 + invSlot*2 + invGap + Pad
HUDTop = ScreenH - HUDH
statusY = HUDTop + Pad
slotsY = HUDTop + Pad + LineH
slotsH = LineH*2 + Pad*2
invY = slotsY + 4
invSlot = 40
invGap = 4
)
type gate struct {
Name string
Inner Widget
}
func gated(name string, inner Widget) Widget {
return &gate{
Name: name,
Inner: inner,
}
}
func (n *gate) GetName() string { return n.Name }
func (n *gate) Layer() inkwell.Layer { return inkwell.LayerOf(n.Inner) }
func (n *gate) Tick(ctx *inkwell.UICtx) {
if World.HUDVisible() {
n.Inner.Tick(ctx)
}
}
func (n *gate) Draw(dst *ebiten.Image, ctx *inkwell.UICtx) {
if World.HUDVisible() {
n.Inner.Draw(dst, ctx)
}
}
func (n *gate) BlocksClickAt(p inkwell.Point) bool {
if !World.HUDVisible() {
return false
}
b, ok := n.Inner.(interface{ BlocksClickAt(inkwell.Point) bool })
return ok && b.BlocksClickAt(p)
}
+16
View File
@@ -0,0 +1,16 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
)
func init() {
WidgetManager.Register(&inkwell.RadialVerbs{
Name: "verbs",
Trigger: inkwell.MouseButtonRight,
Radius: 58,
Labels: map[string]string{
"look": "Look", "use": "Use", "talk": "Talk", "take": "Take",
},
})
}
@@ -6,11 +6,18 @@ import (
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
func init() {
WidgetManager.Register(&sceneNav{
Name: "scene_nav",
})
}
type sceneNav struct {
Name string
}
func (n *sceneNav) GetName() string { return n.Name }
func (n *sceneNav) Layer() inkwell.Layer { return inkwell.LayerScene }
func (n *sceneNav) Draw(dst *ebiten.Image, ctx *inkwell.UICtx) {}
func (n *sceneNav) Tick(ctx *inkwell.UICtx) {
+15
View File
@@ -0,0 +1,15 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
)
func init() {
WidgetManager.Register(&inkwell.SpeechBubble{
Name: "speech",
MaxWidth: 360,
Padding: Pad,
OffsetY: 8,
FallbackY: TopBarH + Pad,
})
}
+14
View File
@@ -0,0 +1,14 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
)
func init() {
WidgetManager.Register(gated("status", &inkwell.StatusLine{
Name: "status",
Y: statusY,
Align: inkwell.AlignLeft,
ScreenWidth: DividerX,
}))
}
@@ -8,12 +8,20 @@ import (
"github.com/hajimehoshi/ebiten/v2/vector"
)
func init() {
WidgetManager.Register(&tapeChannel{
Name: "channel",
Bounds: inkwell.Rect(DividerX+2, HUDTop+2, ScreenW-DividerX-4, ScreenH-HUDTop-4),
})
}
type tapeChannel struct {
Name string
Bounds inkwell.Rectangle
}
func (t *tapeChannel) GetName() string { return t.Name }
func (t *tapeChannel) GetName() string { return t.Name }
func (t *tapeChannel) Layer() inkwell.Layer { return inkwell.LayerHUD }
func (t *tapeChannel) Tick(ctx *inkwell.UICtx) {}
@@ -6,12 +6,20 @@ import (
"github.com/hajimehoshi/ebiten/v2/vector"
)
func init() {
WidgetManager.Register(&tapeSlots{
Name: "tapes",
Bounds: inkwell.Rect(192, slotsY, 194, slotsH),
})
}
type tapeSlots struct {
Name string
Bounds inkwell.Rectangle
}
func (t *tapeSlots) GetName() string { return t.Name }
func (t *tapeSlots) GetName() string { return t.Name }
func (t *tapeSlots) Layer() inkwell.Layer { return inkwell.LayerHUD }
func (t *tapeSlots) BlocksClickAt(p inkwell.Point) bool {
return World.HUDVisible() && t.Bounds.Contains(p)
+27
View File
@@ -0,0 +1,27 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
)
func init() {
WidgetManager.Register(gated("topbar", &titleBar{
TopBar: &inkwell.TopBar{
Name: "topbar",
Height: TopBarH,
TimeVar: VarArmillaStrip,
},
}))
}
type titleBar struct {
*inkwell.TopBar
}
func (t *titleBar) Tick(ctx *inkwell.UICtx) {
t.LeftText = World.SceneTitle()
if World.Paused() {
t.LeftText += " — paused"
}
t.TopBar.Tick(ctx)
}
@@ -7,11 +7,18 @@ import (
"github.com/hajimehoshi/ebiten/v2"
)
func init() {
WidgetManager.Register(&useWithGuard{
Name: "usewith_guard",
})
}
type useWithGuard struct {
Name string
}
func (u *useWithGuard) GetName() string { return u.Name }
func (u *useWithGuard) Layer() inkwell.Layer { return inkwell.LayerScene }
func (u *useWithGuard) Draw(dst *ebiten.Image, ctx *inkwell.UICtx) {}
func (u *useWithGuard) Tick(ctx *inkwell.UICtx) {
+35
View File
@@ -0,0 +1,35 @@
package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
"github.com/hajimehoshi/ebiten/v2"
)
func init() {
WidgetManager.Register(&windowSizer{
Name: "window",
Scale: 2,
})
}
type windowSizer struct {
Name string
Scale int
done bool
}
func (s *windowSizer) GetName() string { return s.Name }
func (s *windowSizer) Layer() inkwell.Layer { return inkwell.LayerScene }
func (s *windowSizer) Draw(dst *ebiten.Image, ctx *inkwell.UICtx) {}
func (s *windowSizer) Tick(ctx *inkwell.UICtx) {
if s.done {
return
}
s.done = true
scale := s.Scale
if scale <= 0 {
scale = 2
}
ebiten.SetWindowSize(ctx.Game.Width*scale, ctx.Game.Height*scale)
}
+1 -19
View File
@@ -2,7 +2,6 @@ package inc
import (
inkwell "git.teletypegames.org/engines/inkwell"
"github.com/hajimehoshi/ebiten/v2"
)
func (w *world) Do(a inkwell.Action) {
@@ -12,15 +11,6 @@ func (w *world) Do(a inkwell.Action) {
}
func (w *world) PumpTick(dt float64) {
if w.top != nil {
title := w.sceneTitle()
if w.paused {
title += " — paused"
}
w.top.LeftText = title
}
if w.running == nil {
if len(w.queue) == 0 {
return
@@ -40,7 +30,7 @@ func (w *world) PumpTick(dt float64) {
}
}
func (w *world) sceneTitle() string {
func (w *world) SceneTitle() string {
s, ok := w.G.SceneManager.Get(w.G.CurrentScene())
if !ok {
return ""
@@ -51,14 +41,6 @@ func (w *world) sceneTitle() string {
return s.Name
}
type actionPump struct {
Name string
}
func (p *actionPump) GetName() string { return p.Name }
func (p *actionPump) Tick(ctx *inkwell.UICtx) { World.PumpTick(ctx.DT) }
func (p *actionPump) Draw(dst *ebiten.Image, ctx *inkwell.UICtx) {}
type fnAction struct{ fn func(*inkwell.Ctx) }
func (a *fnAction) Start() inkwell.Runner {
-4
View File
@@ -21,7 +21,6 @@ type world struct {
pending string
queue []inkwell.Action
running inkwell.Runner
top *inkwell.TopBar
tapeWaiting bool
tapeLine inkwell.Action
@@ -36,7 +35,6 @@ func (w *world) attach(g *inkwell.Game) {
w.pending = ""
w.queue = nil
w.running = nil
w.top = nil
w.tapeWaiting = false
w.tapeLine = nil
}
@@ -49,8 +47,6 @@ func (w *world) HUDVisible() bool { return w.mode == ModePlay }
func (w *world) Paused() bool { return w.paused }
func (w *world) SetTopBar(t *inkwell.TopBar) { w.top = t }
func (w *world) Slot2() string {
if v, ok := w.G.State.Var(VarSlot2).(string); ok {
return v