-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.go
208 lines (166 loc) · 3.88 KB
/
graphics.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package ui
import (
"fmt"
"os"
"github.com/gdamore/tcell"
"github.com/mattn/go-runewidth"
"github.com/TemirkhanN/rpg/internal/game/data"
)
type Drawer interface {
Icon() rune
Style() tcell.Style
Position() data.Position
}
type Pos struct {
x int
y int
}
func NewPosition(x int, y int) Pos {
return Pos{
x: x,
y: y,
}
}
func (p Pos) X() int {
return p.x
}
func (p Pos) Y() int {
return p.y
}
type Box struct {
title Text
leftTop Pos
rightBottom Pos
}
type Text struct {
text string
position Pos
}
func CalculateTextWidth(text string) int {
width := 0
for _, c := range text {
width += runewidth.RuneWidth(c)
}
return width
}
func NewBox(x1 int, y1 int, x2 int, y2 int, title ...string) Box {
if y2 < y1 {
y1, y2 = y2, y1
}
if x2 < x1 {
x1, x2 = x2, x1
}
boxTitle := NewText("", 0, 0)
if len(title) == 1 {
titleLen := len(title[0])
titleStartAt := (x2-x1-titleLen)/2 + x1
if titleStartAt < x1 {
titleStartAt = x1
}
boxTitle = NewText(title[0], titleStartAt, y1)
}
return Box{
title: boxTitle,
leftTop: Pos{x: x1, y: y1},
rightBottom: Pos{x: x2, y: y2},
}
}
func NewText(value string, posX int, posY int) Text {
return Text{
text: value,
position: Pos{x: posX, y: posY},
}
}
func CreateScreen() tcell.Screen {
screen, e := tcell.NewScreen()
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
if e := screen.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
}
defStyle := tcell.StyleDefault.
Background(tcell.ColorBlack).
Foreground(tcell.ColorWhite)
screen.SetStyle(defStyle)
return screen
}
func (t Text) Draw(on tcell.Screen, useStyle ...tcell.Style) {
style := TextStyle
if len(useStyle) == 1 {
style = useStyle[0]
}
horizontalOffset := t.position.X()
for _, c := range t.text {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
on.SetContent(horizontalOffset, t.position.Y(), c, comb, style)
horizontalOffset += w
}
}
func (b Box) Draw(on tcell.Screen, useStyle ...tcell.Style) {
style := BoxStyle
if len(useStyle) == 1 {
style = useStyle[0]
}
x1 := b.leftTop.X()
x2 := b.rightBottom.X()
y1 := b.leftTop.Y()
y2 := b.rightBottom.Y()
hasTitle := false
titleStartAt := b.title.position.X()
titleEndAt := titleStartAt + len(b.title.text) - 1
if b.title.text != "" {
hasTitle = true
b.title.Draw(on, TextStyle)
}
for col := x1; col <= x2; col++ {
if !hasTitle || col < titleStartAt-1 || col > titleEndAt+1 {
on.SetContent(col, y1, tcell.RuneHLine, nil, style)
}
on.SetContent(col, y2, tcell.RuneHLine, nil, style)
}
for row := y1 + 1; row < y2; row++ {
on.SetContent(x1, row, tcell.RuneVLine, nil, style)
on.SetContent(x2, row, tcell.RuneVLine, nil, style)
}
if y1 != y2 && x1 != x2 {
on.SetContent(x1, y1, tcell.RuneULCorner, nil, style)
on.SetContent(x2, y1, tcell.RuneURCorner, nil, style)
on.SetContent(x1, y2, tcell.RuneLLCorner, nil, style)
on.SetContent(x2, y2, tcell.RuneLRCorner, nil, style)
}
}
func DrawUnit(unit Drawer, on tcell.Screen) {
NewText(string(unit.Icon()), unit.Position().X(), unit.Position().Y()).Draw(on, unit.Style())
}
func DrawLocation(location data.Location, on tcell.Screen) {
NewBox(
location.LeftTop().X(),
location.LeftTop().Y(),
location.RightBottom().X(),
location.RightBottom().Y(),
location.Name(),
).Draw(on, BoxStyle)
for _, gameNpc := range location.Npcs() {
DrawUnit(gameNpc, on)
}
for _, passage := range location.Passages() {
DrawUnit(passage, on)
}
for _, object := range location.Objects() {
DrawUnit(object, on)
}
}
var (
TextStyle = tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite)
InfoTextStyle = tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorCadetBlue)
BoxStyle = tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite)
)