last_light/game/ui/ui_dialog.go

125 lines
3.1 KiB
Go
Raw Normal View History

2024-05-12 23:22:39 +03:00
package ui
import (
"mvvasilev/last_light/engine"
2024-06-06 23:17:22 +03:00
"mvvasilev/last_light/game/systems"
2024-05-12 23:22:39 +03:00
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
"github.com/google/uuid"
)
type UIDialog struct {
window *UIWindow
prompt *UILabel
yesBtn *UISimpleButton
noBtn *UISimpleButton
}
func CreateYesNoDialog(title string, prompt string, yesText string, noText string, lineWidth int, yesAction func(), noAction func()) *UIDialog {
d := new(UIDialog)
numLines := len(prompt) / lineWidth
winWidth := lineWidth + 2
winHeight := numLines + 2
winX := engine.TERMINAL_SIZE_WIDTH - winWidth/2
winY := engine.TERMINAL_SIZE_HEIGHT - winHeight/2
d.window = CreateWindow(winX, winY, winWidth, winHeight, title, tcell.StyleDefault)
d.prompt = CreateUILabel(winX+1, winY+1, lineWidth, numLines, prompt, tcell.StyleDefault)
yesBtnLength := len(yesText) + 4
noBtnLength := len(noText) + 4
yesBtnPosX := winX + winWidth/4 - yesBtnLength/2
d.yesBtn = CreateSimpleButton(yesBtnPosX, winY+winHeight-1, yesText, tcell.StyleDefault, tcell.StyleDefault.Attributes(tcell.AttrBold), yesAction)
noBtnPosX := winX + 3*winWidth/4 - noBtnLength/2
d.noBtn = CreateSimpleButton(noBtnPosX, winY+winHeight-2, noText, tcell.StyleDefault, tcell.StyleDefault.Attributes(tcell.AttrBold), noAction)
d.yesBtn.Highlight()
return d
}
func CreateOkDialog(title string, prompt string, okText string, lineWidth int, okAction func()) *UIDialog {
d := new(UIDialog)
numLines := len(prompt) / lineWidth
winWidth := lineWidth + 2
winHeight := numLines + 5
winX := engine.TERMINAL_SIZE_WIDTH/2 - winWidth/2
winY := engine.TERMINAL_SIZE_HEIGHT/2 - winHeight/2
d.window = CreateWindow(winX, winY, winWidth, winHeight, title, tcell.StyleDefault)
d.prompt = CreateUILabel(winX+1, winY+1, lineWidth, numLines, prompt, tcell.StyleDefault)
yesBtnLength := len(okText) + 4
yesBtnPosX := winX + winWidth/2 - yesBtnLength/2
d.yesBtn = CreateSimpleButton(yesBtnPosX, winY+winHeight-2, okText, tcell.StyleDefault, tcell.StyleDefault.Attributes(tcell.AttrBold), okAction)
d.yesBtn.Highlight()
return d
}
func (d *UIDialog) Select() {
if d.yesBtn.IsHighlighted() {
d.yesBtn.Select()
} else if d.noBtn != nil && d.noBtn.IsHighlighted() {
d.noBtn.Select()
}
}
func (d *UIDialog) OnSelect(f func()) {
d.yesBtn.OnSelect(f)
}
func (d *UIDialog) MoveTo(x int, y int) {
}
func (d *UIDialog) Position() engine.Position {
return d.window.Position()
}
func (d *UIDialog) Size() engine.Size {
return d.window.Size()
}
2024-06-06 23:17:22 +03:00
func (d *UIDialog) Input(inputAction systems.InputAction) {
if inputAction == systems.InputAction_Menu_HighlightLeft {
2024-05-12 23:22:39 +03:00
if !d.yesBtn.IsHighlighted() {
d.noBtn.Unhighlight()
d.yesBtn.Highlight()
}
2024-06-06 23:17:22 +03:00
} else if inputAction == systems.InputAction_Menu_HighlightRight {
2024-05-12 23:22:39 +03:00
if d.noBtn == nil {
return
}
if !d.noBtn.IsHighlighted() {
d.noBtn.Highlight()
d.yesBtn.Unhighlight()
}
}
}
func (d *UIDialog) UniqueId() uuid.UUID {
return d.window.UniqueId()
}
func (d *UIDialog) Draw(v views.View) {
d.window.Draw(v)
d.prompt.Draw(v)
d.yesBtn.Draw(v)
if d.noBtn != nil {
d.noBtn.Draw(v)
}
}