2021-07-30 22:29:20 +00:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-01-16 02:18:08 +00:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2023-01-16 02:11:07 +00:00
|
|
|
"tuxpa.in/t/erm/app/darktile/font"
|
|
|
|
"tuxpa.in/t/erm/app/darktile/gui/popup"
|
|
|
|
"tuxpa.in/t/erm/app/darktile/hinters"
|
|
|
|
termutil2 "tuxpa.in/t/erm/app/darktile/termutil"
|
2021-07-30 22:29:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
type GUI struct {
|
|
|
|
mouseStateLeft MouseState
|
|
|
|
mouseStateRight MouseState
|
|
|
|
mouseStateMiddle MouseState
|
|
|
|
mouseDrag bool
|
|
|
|
size image.Point // pixels
|
2023-01-16 02:11:07 +00:00
|
|
|
terminal *termutil2.Terminal
|
2021-07-30 22:29:20 +00:00
|
|
|
updateChan chan struct{}
|
|
|
|
lastClick time.Time
|
|
|
|
clickCount int
|
|
|
|
fontManager *font.Manager
|
2023-01-16 02:11:07 +00:00
|
|
|
mousePos termutil2.Position
|
2021-07-30 22:29:20 +00:00
|
|
|
hinters []hinters.Hinter
|
|
|
|
activeHinter int
|
2021-08-02 19:55:04 +00:00
|
|
|
popupMessages []popup.Message
|
2021-07-30 22:29:20 +00:00
|
|
|
screenshotRequested bool
|
|
|
|
screenshotFilename string
|
|
|
|
startupFuncs []func(g *GUI)
|
|
|
|
keyState *keyState
|
2021-07-31 16:35:26 +00:00
|
|
|
opacity float64
|
2021-08-02 19:55:04 +00:00
|
|
|
enableLigatures bool
|
|
|
|
cursorImage *ebiten.Image
|
2021-07-30 22:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MouseState uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
MouseStateNone MouseState = iota
|
|
|
|
MouseStatePressed
|
|
|
|
)
|
|
|
|
|
2023-01-16 02:11:07 +00:00
|
|
|
func New(terminal *termutil2.Terminal, options ...Option) (*GUI, error) {
|
2021-07-30 22:29:20 +00:00
|
|
|
|
|
|
|
g := &GUI{
|
2021-08-02 19:55:04 +00:00
|
|
|
terminal: terminal,
|
|
|
|
size: image.Point{80, 30},
|
|
|
|
updateChan: make(chan struct{}),
|
|
|
|
fontManager: font.NewManager(),
|
|
|
|
activeHinter: -1,
|
|
|
|
keyState: newKeyState(),
|
|
|
|
enableLigatures: true,
|
2021-07-30 22:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
if err := option(g); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
terminal.SetWindowManipulator(NewManipulator(g))
|
|
|
|
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GUI) Run() error {
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := g.terminal.Run(g.updateChan, uint16(g.size.X), uint16(g.size.Y)); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Fatal error: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
|
|
|
|
ebiten.SetScreenTransparent(true)
|
2021-07-31 16:35:26 +00:00
|
|
|
ebiten.SetScreenClearedEveryFrame(true)
|
2021-07-30 22:29:20 +00:00
|
|
|
ebiten.SetWindowResizable(true)
|
|
|
|
ebiten.SetRunnableOnUnfocused(true)
|
|
|
|
ebiten.SetFPSMode(ebiten.FPSModeVsyncOffMinimum)
|
|
|
|
|
|
|
|
for _, f := range g.startupFuncs {
|
|
|
|
go f(g)
|
|
|
|
}
|
|
|
|
|
|
|
|
go g.watchForUpdate()
|
|
|
|
|
|
|
|
return ebiten.RunGame(g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GUI) watchForUpdate() {
|
|
|
|
for range g.updateChan {
|
|
|
|
ebiten.ScheduleFrame()
|
2022-05-10 08:18:01 +00:00
|
|
|
if g.keyState.AnythingPressed() {
|
|
|
|
go func() {
|
2021-07-30 22:29:20 +00:00
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
ebiten.ScheduleFrame()
|
2022-05-10 08:18:01 +00:00
|
|
|
}()
|
|
|
|
}
|
2021-07-30 22:29:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GUI) CellSize() image.Point {
|
|
|
|
return g.fontManager.CharSize()
|
|
|
|
}
|
|
|
|
|
2023-01-16 02:11:07 +00:00
|
|
|
func (g *GUI) Highlight(start termutil2.Position, end termutil2.Position, label string, img image.Image) {
|
2021-07-30 22:29:20 +00:00
|
|
|
|
|
|
|
if label == "" && img == nil {
|
|
|
|
g.terminal.GetActiveBuffer().Highlight(start, end, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-16 02:11:07 +00:00
|
|
|
annotation := &termutil2.Annotation{
|
2021-07-30 22:29:20 +00:00
|
|
|
Text: label,
|
|
|
|
Image: img,
|
|
|
|
}
|
|
|
|
|
|
|
|
if label != "" {
|
|
|
|
lines := strings.Split(label, "\n")
|
|
|
|
annotation.Height = float64(len(lines))
|
|
|
|
for _, line := range lines {
|
|
|
|
if float64(len(line)) > annotation.Width {
|
|
|
|
annotation.Width = float64(len(line))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if img != nil {
|
|
|
|
annotation.Height += float64(img.Bounds().Dy() / g.fontManager.CharSize().Y)
|
|
|
|
if label != "" {
|
|
|
|
annotation.Height += 0.5 // half line spacing between image + text
|
|
|
|
}
|
|
|
|
imgCellWidth := img.Bounds().Dx() / g.fontManager.CharSize().X
|
|
|
|
if float64(imgCellWidth) > annotation.Width {
|
|
|
|
annotation.Width = float64(imgCellWidth)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g.terminal.GetActiveBuffer().Highlight(start, end, annotation)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GUI) ClearHighlight() {
|
|
|
|
g.terminal.GetActiveBuffer().ClearHighlight()
|
|
|
|
}
|