This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtermbox.go
132 lines (121 loc) · 2.51 KB
/
termbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// +build !tcell,!ansi,!js,!tk
package main
import (
termbox "github.com/nsf/termbox-go"
)
type gameui struct {
g *game
cursor position
small bool
// below unused for this backend
menuHover menu
itemHover int
}
func (ui *gameui) Init() error {
err := termbox.Init()
if err != nil {
return err
}
termbox.SetOutputMode(termbox.Output256)
termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse)
termbox.HideCursor()
ui.HideCursor()
ui.menuHover = -1
return nil
}
func (ui *gameui) Close() {
termbox.Close()
}
var SmallScreen = false
func (ui *gameui) Flush() {
ui.DrawLogFrame()
for _, cdraw := range ui.g.DrawLog[len(ui.g.DrawLog)-1].Draws {
cell := cdraw.Cell
fg := cell.Fg
bg := cell.Bg
if Only8Colors {
fg = Map16ColorTo8Color(fg)
bg = Map16ColorTo8Color(bg)
}
termbox.SetCell(cdraw.X, cdraw.Y, cell.R, termbox.Attribute(fg)+1, termbox.Attribute(bg)+1)
}
termbox.Flush()
w, h := termbox.Size()
if w <= UIWidth-8 || h <= UIHeight-2 {
SmallScreen = true
} else {
SmallScreen = false
}
}
func (ui *gameui) ApplyToggleLayout() {
GameConfig.Small = !GameConfig.Small
if GameConfig.Small {
ui.Clear()
ui.Flush()
UIHeight = 24
UIWidth = 80
} else {
UIHeight = 26
UIWidth = 100
}
ui.g.DrawBuffer = make([]UICell, UIWidth*UIHeight)
ui.Clear()
}
func (ui *gameui) Small() bool {
return GameConfig.Small || SmallScreen
}
func (ui *gameui) Interrupt() {
termbox.Interrupt()
}
func (ui *gameui) PollEvent() (in uiInput) {
switch tev := termbox.PollEvent(); tev.Type {
case termbox.EventKey:
if tev.Ch == 0 {
switch tev.Key {
case termbox.KeyArrowLeft:
in.key = "4"
case termbox.KeyArrowDown:
in.key = "2"
case termbox.KeyArrowUp:
in.key = "8"
case termbox.KeyArrowRight:
in.key = "6"
case termbox.KeyHome:
in.key = "7"
case termbox.KeyEnd:
in.key = "1"
case termbox.KeyPgup:
in.key = "9"
case termbox.KeyPgdn:
in.key = "3"
case termbox.KeyDelete:
in.key = "5"
case termbox.KeyEsc, termbox.KeySpace:
in.key = " "
case termbox.KeyEnter:
in.key = "."
}
}
if tev.Ch != 0 && in.key == "" {
in.key = string(tev.Ch)
}
case termbox.EventMouse:
if tev.Ch == 0 {
in.mouseX, in.mouseY = tev.MouseX, tev.MouseY
switch tev.Key {
case termbox.MouseLeft:
in.mouse = true
in.button = 0
case termbox.MouseMiddle:
in.mouse = true
in.button = 1
case termbox.MouseRight:
in.mouse = true
in.button = 2
}
}
case termbox.EventInterrupt:
in.interrupt = true
}
return in
}