-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialogue.go
223 lines (195 loc) · 4.92 KB
/
dialogue.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
// Copyright 2016 Josh Deprez
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package awakengine
import (
"log"
"github.com/DrJosh9000/vec"
)
type ButtonSpec struct {
Label string
Action func()
}
// DialogueLine is information for displaying a singe line of dialogue in a display.
type DialogueLine struct {
Avatar *SheetFrame
Text string
Buttons []*ButtonSpec
AutoNext bool
Slowness int
}
// Dialogue is all the things needed for displaying blocking dialogue text.
type DialogueDisplay struct {
*View
avatar *ImageView
bubble *Bubble
buttons []*Button
text *Text
complete bool
frame int // frame number for this dialogue.
line *DialogueLine
}
// NewDialogueDisplay creates a new DialogueDisplay.
func NewDialogueDisplay(scene *Scene) *DialogueDisplay {
bk, _ := game.BubbleKey()
//_, bk := game.BubbleKey()
d := &DialogueDisplay{
View: &View{},
avatar: &ImageView{
View: &View{},
},
text: &Text{
View: &View{},
Font: game.Font(),
},
bubble: &Bubble{
View: &View{},
Key: bk,
},
}
camSize := scene.View.Size()
size := vec.I2{camSize.X - 20, 74}
d.SetParent(scene.HUD)
d.SetPositionAndSize(vec.I2{10, camSize.Y - 84}, size)
d.SetZ(100) // The topmost of the top...
d.bubble.SetParent(d.View)
d.bubble.SetSize(size)
d.bubble.SetZ(1)
d.avatar.SetParent(d.bubble.View)
d.avatar.SetPosition(bubblePartSize)
d.avatar.SetZ(1)
d.text.SetParent(d.bubble.View)
d.text.SetZ(1)
return d
}
// Layout rearranges views
func (d *DialogueDisplay) Layout(line *DialogueLine) {
// Dispose of any old buttons first.
for _, b := range d.buttons {
b.Dispose()
}
d.buttons = nil
// Reset things...
d.complete = false
d.frame = 0
// Refresh properties from the line.
d.avatar.SheetFrame = line.Avatar
d.frame = 0
d.line = line
d.text.Text = line.Text
size := d.Size()
textPos := vec.I2{10, 10}
if line.Avatar != nil {
// Provide space for the avatar.
textPos.X += line.Avatar.Sheet.FrameSize.X + 5
d.avatar.SetSize(line.Avatar.Sheet.FrameSize)
}
d.avatar.SetVisible(line.Avatar != nil)
d.text.SetPositionAndSize(textPos, vec.I2{size.X - textPos.X - 15, 0})
d.text.Layout(line.Slowness < 0)
p := vec.I2{textPos.X, size.Y - 40}
for _, s := range line.Buttons {
btn := NewButton(
s.Label,
s.Action,
vec.Rect{p, p.Add(vec.I2{65, 25})},
d.bubble.View)
d.buttons = append(d.buttons, btn)
p.X += 75
}
}
func (d *DialogueDisplay) AddToScene(scene *Scene) {
d.bubble.AddToScene(scene)
d.text.AddToScene(scene)
for _, b := range d.buttons {
b.AddToScene(scene)
}
scene.AddPart(d.avatar)
}
func (d *DialogueDisplay) finish() {
d.complete = true
for d.text.next < len(d.text.chars) {
d.text.Advance()
}
}
// Update updates things in the dialogue, based on user input or passage of time.
// Returns true if the event is handled.
func (d *DialogueDisplay) Handle(event *Event) bool {
for _, b := range d.buttons {
if b.Handle(event) {
// log.Printf("dialogue: button handled event")
return true
}
}
if d.complete && d.line.AutoNext {
// log.Printf("dialogue: complete and autonext")
return true
}
if event.Type == EventMouseUp {
if d.complete && len(d.buttons) == 0 {
// log.Printf("dialogue: clicked, complete, and no buttons")
return true
}
if !d.line.AutoNext {
d.finish()
}
}
if !d.complete {
if d.line.Slowness < 0 {
d.finish()
}
if d.line.Slowness == 0 || d.frame%d.line.Slowness == 0 {
d.text.Advance()
if d.text.next >= len(d.text.chars) {
d.complete = true
}
}
}
d.frame++
return false
}
// Management of global dialogue state (dialogue, dialogueStack)
func playNextDialogue() {
if len(dialogueStack) == 0 {
if dialogue != nil {
if config.Debug {
log.Printf("disposing a dialogue")
}
dialogue.Dispose()
}
dialogue = nil
return
}
if dialogue == nil {
if config.Debug {
log.Printf("creating a dialogue")
}
dialogue = NewDialogueDisplay(scene)
}
if config.Debug {
log.Printf("laying out a dialogue")
}
dialogue.Layout(dialogueStack[0])
dialogue.AddToScene(scene)
dialogueStack = dialogueStack[1:]
}
// PushDialogueToBack makes some dialogue the dialogue to play after all the current dialogue is finished.
func PushDialogueToBack(dl ...*DialogueLine) {
player.GoIdle()
dialogueStack = append(dialogueStack, dl...)
}
// PushDialogue makes some dialogue the next dialogue to play.
func PushDialogue(dl ...*DialogueLine) {
player.GoIdle()
dialogueStack = append(dl, dialogueStack...)
}