/
textbox.go
393 lines (329 loc) · 11 KB
/
textbox.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package gui
import (
"fmt"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"github.com/steelx/go-rpg-cgm/animation"
"github.com/steelx/go-rpg-cgm/utilz"
"math"
"strings"
)
/* e.g.
tBox := TextboxCreateFixed(
"A nation can survive its fools, and even the ambitious. But it cannot survive treason from within. An enemy at the gates is less formidable, for he is known and carries his banner openly. But the traitor moves amongst those within the gate freely, his sly whispers rustling through all the alleys, heard in the very halls of government itself. For the traitor appears not a traitor; he speaks in accents familiar to his victims, and he wears their face and their arguments, he appeals to the baseness that lies deep in the hearts of all men. He rots the soul of a nation, he works secretly and unknown in the night to undermine the pillars of the city, he infects the body politic so that it can no longer resist. A murderer is less to fear. Jai Hind I Love India <3 ",
pixel.V(-150, 200), 300, 100,
"Ajinkya",
avatarPng,
)
*/
var (
ContinueCaretPng pixel.Picture
CursorPng pixel.Picture
BasicAtlas12 *text.Atlas
BasicAtlasAscii *text.Atlas
)
func init() {
var err error
ContinueCaretPng, err = utilz.LoadPicture("../resources/continue_caret.png")
utilz.PanicIfErr(err)
CursorPng, err = utilz.LoadPicture("../resources/cursor.png")
utilz.PanicIfErr(err)
fontFace12, err := utilz.LoadTTF("../resources/font/joystix.ttf", 12)
utilz.PanicIfErr(err)
BasicAtlas12 = text.NewAtlas(fontFace12, text.ASCII)
fontFace14, err := utilz.LoadTTF("../resources/font/LcdSolid-VPzB.ttf", 14)
BasicAtlasAscii = text.NewAtlas(fontFace14, text.ASCII)
}
type Textbox struct {
Stack *StateStack
text string
textScale, size, topPadding float64
Position pixel.Vec
textBounds pixel.Rect
textBase *text.Text
textAtlas *text.Atlas
Panel Panel
continueMark pixel.Picture
Width, Height float64
textBlocks []string
textBlockLimitIndex int
textRowLimit int
avatarName string
avatarImg pixel.Picture
AppearTween animation.Tween
time float64
isFixed, isDead, hasMenu bool
menu *SelectionMenu
}
func TextboxNew(stack *StateStack, txt string, size float64, atlas *text.Atlas, avatarName string, avatarImg pixel.Picture) Textbox {
return Textbox{
Stack: stack,
text: txt,
textScale: 1,
size: size,
continueMark: ContinueCaretPng,
avatarName: avatarName,
avatarImg: avatarImg,
textAtlas: atlas,
time: 0,
}
}
func TextboxWithMenuCreate(stack *StateStack, textBoxText string, panelPos pixel.Vec, panelWidth, panelHeight float64,
choices []string, onSelection func(int, interface{}), showColumns bool) *Textbox {
textbox := TextboxCreateFixed(
stack,
textBoxText,
panelPos, panelWidth, panelHeight,
"",
nil,
true,
)
textBounds := textbox.getTextBound()
menu := SelectionMenuCreate(24, 128, 0,
choices, showColumns,
pixel.V(textbox.Position.X-10, textbox.Position.Y-textBounds.H()-10), func(i int, s interface{}) {
onSelection(i, s)
textbox.isDead = true
}, nil)
textbox.menu = &menu
return &textbox
}
func TextboxFITMenuCreate(stack *StateStack, x, y float64, textBoxText string, choices []string, onSelection func(int, interface{})) *Textbox {
panelPos := pixel.V(x, y)
t := TextboxNew(stack, textBoxText, 14, BasicAtlas12, "", nil)
t.AppearTween = animation.TweenCreate(1, 1, 1)
t.isFixed = false
t.hasMenu = true
if t.hasMenu {
t.topPadding = 10
}
textBounds := t.getTextBound()
menu := SelectionMenuCreate(24, 128, 0,
choices, true,
pixel.V(t.Position.X, t.Position.Y-textBounds.H()-10), func(i int, s interface{}) {
onSelection(i, s)
t.isDead = true
}, nil)
panel := PanelCreate(panelPos, menu.GetWidth(), menu.GetHeight())
t.menu = &menu
t.Panel = panel
t.textBounds = panel.mBounds
t.makeTextColumns()
t.buildTextBlocks()
return &t
}
func TextboxFITPassedMenuCreate(stack *StateStack, x, y float64, textBoxText string, menu *SelectionMenu) *Textbox {
panelPos := pixel.V(x, y)
t := TextboxNew(stack, textBoxText, 14, BasicAtlas12, "", nil)
t.AppearTween = animation.TweenCreate(1, 1, 1)
t.isFixed = false
t.hasMenu = true
if t.hasMenu {
t.topPadding = 10
}
textBounds := t.getTextBound()
menu.SetPosition(panelPos.X-(menu.GetWidth()/2), panelPos.Y-(textBounds.H()/2)+35-t.topPadding)
panel := PanelCreate(panelPos, menu.GetWidth(), menu.GetHeight()+30)
t.menu = menu
t.Panel = panel
t.textBounds = panel.mBounds
t.makeTextColumns()
t.buildTextBlocks()
return &t
}
func TextboxCreateFixed(stack *StateStack, txt string, panelPos pixel.Vec, panelWidth, panelHeight float64, avatarName string, avatarImg pixel.Picture, hasMenu bool) Textbox {
panel := PanelCreate(panelPos, panelWidth, panelHeight)
t := TextboxNew(stack, txt, 14, BasicAtlas12, avatarName, avatarImg)
t.AppearTween = animation.TweenCreate(1, 0, 1)
t.isFixed = true
t.Panel = panel
t.textBounds = panel.mBounds
t.hasMenu = hasMenu
if hasMenu {
t.topPadding = 10
}
t.makeTextColumns()
t.buildTextBlocks()
return t
}
//TextboxCreateFitted are good for small chats
// height and width gets set automatically
func TextboxCreateFitted(stack *StateStack, txt string, panelPos pixel.Vec, hasMenu bool) Textbox {
const padding = 20.0
tBox := TextboxNew(stack, txt, 14, BasicAtlasAscii, "", nil)
tBox.AppearTween = animation.TweenCreate(0.9, 1, 0.3)
tBox.textBase = text.New(panelPos, tBox.textAtlas)
tBox.textBase.LineHeight = padding
textBounds := tBox.getTextBound()
tBox.hasMenu = hasMenu
panelHeight := textBounds.H() + padding
if hasMenu {
panelHeight = panelHeight + 100 //+menu height
}
panel := PanelCreate(panelPos, textBounds.W()+(padding*2), panelHeight)
topLeft, _, _, _ := panel.GetCorners()
textPos := pixel.V(topLeft.X+padding, topLeft.Y-padding)
tBox.textBase = text.New(textPos, tBox.textAtlas) //reset text position to bounds
tBox.Panel = panel
tBox.makeTextColumns()
return tBox
}
func (t *Textbox) makeTextColumns() {
var makeColumns bool
if len(t.avatarName) != 0 {
makeColumns = true
}
var textColumnWidth = t.Panel.mBounds.W() - (t.size * 2)
var textColumnHeight = t.Panel.mBounds.H() - (t.size * 2)
var topLeft, _, _, _ = t.Panel.GetCorners()
var textPos = pixel.V(topLeft.X+t.size, topLeft.Y-t.size-t.topPadding)
if makeColumns {
textColumnWidth -= t.avatarImg.Bounds().W()
textColumnHeight -= t.size
textPos.X += t.avatarImg.Bounds().W() - t.size/2
textPos.Y -= t.size / 2
}
if t.hasMenu {
textColumnHeight = textColumnHeight / 2
}
t.Position = textPos
t.Width = textColumnWidth
t.Height = textColumnHeight
}
func (t Textbox) getTextBound() pixel.Rect {
if t.textBase == nil {
t.textBase = text.New(t.Position, t.textAtlas)
}
return t.textBase.BoundsOf(t.text)
}
func (t *Textbox) buildTextBlocks() {
t.textBase = text.New(t.Position, t.textAtlas)
t.textBase.LineHeight = t.size
t.textBlocks = make([]string, 0) //imp to avoid null reference
blocks := math.Abs(t.getTextBound().W() / t.Width)
eachBlockWidth := math.Ceil(t.textBase.BoundsOf(t.text).W()) / blocks
splitTextAt := math.Ceil(eachBlockWidth / (t.size))
var tempTxtLine = ""
ss := strings.Fields(t.text)
for i, word := range ss {
tempTxtLine += word + " "
if len(ss)-1 == i || (len(tempTxtLine) > int(splitTextAt)) {
t.textBlocks = append(t.textBlocks, tempTxtLine)
tempTxtLine = ""
}
}
}
func (t *Textbox) IsDead() bool {
return t.isDead
}
func (t Textbox) HasReachedLimit() bool {
return t.textBlockLimitIndex >= len(t.textBlocks)
}
func (t Textbox) drawAvatar(renderer pixel.Target) {
if len(t.avatarName) == 0 {
return
}
avatarSprite := pixel.NewSprite(t.avatarImg, t.avatarImg.Bounds())
topLeft := pixel.V(
t.Panel.mBounds.Min.X+(t.avatarImg.Bounds().W()/2)+t.size/2,
t.Panel.mBounds.Max.Y-(t.avatarImg.Bounds().H()/2)-5)
titlePos := pixel.V(t.Panel.mBounds.Min.X+t.size, t.Panel.mBounds.Min.Y+t.avatarImg.Bounds().H()-(t.size/2)-2)
title := text.New(titlePos, BasicAtlasAscii)
fmt.Fprintln(title, t.avatarName)
title.Draw(renderer, pixel.IM.Scaled(titlePos, 1))
avatarSprite.Draw(renderer, pixel.IM.Moved(topLeft).Scaled(topLeft, 0.9))
}
func (t Textbox) drawContinueArrow(renderer pixel.Target) {
if t.textBlockLimitIndex+t.textRowLimit < len(t.textBlocks) {
mat := pixel.IM
bottomRight := pixel.V(t.Panel.mBounds.Max.X-t.size, t.Panel.mBounds.Min.Y+t.size)
sprite := pixel.NewSprite(t.continueMark, t.continueMark.Bounds())
sprite.Draw(renderer, mat.Moved(bottomRight))
title := text.New(bottomRight, BasicAtlasAscii)
keyHintTxt, padding := "spacebar", 20.0
textPos := bottomRight.Sub(
pixel.V(title.BoundsOf(keyHintTxt).W(), -padding),
)
fmt.Fprintln(title, keyHintTxt)
title.Draw(renderer, pixel.IM.Moved(textPos))
}
}
func (t *Textbox) Next() {
t.textBlockLimitIndex += t.textRowLimit
}
func (t *Textbox) renderFitted(renderer pixel.Target) {
scale := t.AppearTween.Value()
t.textBase.Clear()
t.Panel.Draw(renderer)
fmt.Fprintln(t.textBase, t.text)
t.textBase.Draw(renderer, pixel.IM.Scaled(t.Position, scale))
}
//RenderWithPanel will render text based on Panel height and divide rows
//based on available height, it will destroy at the end of Next last user input
func (t *Textbox) renderFixed(renderer pixel.Target) {
t.textBase.Clear()
//limit prints
eachBlockHeight := math.Abs(t.textBase.BoundsOf(t.text).H())
t.textRowLimit = int(math.Ceil(t.Height / eachBlockHeight))
lastIndex := utilz.MinInt(t.textBlockLimitIndex+t.textRowLimit, len(t.textBlocks))
firstIndex := t.textBlockLimitIndex
if t.HasReachedLimit() {
//reached limit
t.isDead = true
return
}
readFrom := t.textBlocks[firstIndex:lastIndex]
for _, line := range readFrom {
_, err := fmt.Fprintln(t.textBase, line)
utilz.PanicIfErr(err)
}
t.Panel.Draw(renderer)
t.textBase.Draw(renderer, pixel.IM)
t.drawAvatar(renderer)
t.drawContinueArrow(renderer)
}
func (t *Textbox) Render(renderer *pixelgl.Window) {
if t.hasMenu {
t.renderFitted(renderer)
t.menu.Render(renderer)
return
}
if t.isFixed {
t.renderFixed(renderer)
} else {
t.renderFitted(renderer)
}
}
func (t *Textbox) Update(dt float64) bool {
t.time = t.time + dt
t.AppearTween.Update(dt)
if t.IsDead() {
t.Stack.Pop()
}
return t.IsDead()
}
func (t *Textbox) Enter() {
}
func (t *Textbox) Exit() {
}
//HandleInput takes care of 3 types of textbox's
//1 textbox with menu
//2 Fixed then we let the blocks render
//3 Fitted users marks as read and goes to next text popup
func (t *Textbox) HandleInput(window *pixelgl.Window) {
if t.hasMenu {
t.menu.HandleInput(window)
}
if window.JustPressed(pixelgl.KeySpace) {
if t.isFixed {
t.Next()
return
}
t.OnClick()
}
}
func (t *Textbox) OnClick() {
t.AppearTween = animation.TweenCreate(1, 0, 0.2)
t.isDead = true
}