-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_event.go
102 lines (83 loc) · 2.64 KB
/
text_event.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
package bus
import "github.com/vorpalgame/core/lib"
type TextEventListener interface {
OnTextEvent(textChannel <-chan TextEvent)
}
//TODO Builder pattern isn't quite right for this yet. Refactor to correct.
//TODO Set/Get Location and Reinitialize should be implemented
//in a generic way to return the actual event. For example,
//MultilineTextEvent is a textEvent but it should return that
//interface and not textEvent only. Otherwise the builder pattern
//is broken.
// ////////////////////////////////////////////////
// // textEvent
// ////////////////////////////////////////////////
type TextEvent interface {
Reinitialize() TextEvent
GetLocation() (x, y int32)
SetLocation(x, y int32) TextEvent
}
type textEventData struct {
X, Y int32
}
func (ted *textEventData) GetLocation() (x, y int32) {
return ted.X, ted.Y
}
func (ted *textEventData) SetLocation(x, y int32) TextEvent {
ted.X = x
ted.Y = y
return ted
}
func (ted *textEventData) Reinitialize() TextEvent {
//More specific events can override in whatever way
//make sense. At this level there isn't much to do
//nil'ing the location is potentially dangerous.
return ted
}
//////////////////////////////////////////////////
//// MultilineTextEvent
//////////////////////////////////////////////////
type MultilineTextEvent interface {
TextEvent
lib.Font
GetText() []lib.TextLine
AddTextLine(lib.TextLine) MultilineTextEvent
AddText(string) MultilineTextEvent
GetId() int32
}
// ////
// Text event can have a font specified for defaults.
// TODO switch to using Point for location.
type multilineTextEventData struct {
textEventData
lib.Font
text []lib.TextLine
id int32
}
var nextTextEventId = int32(0)
func NewMultilineTextEvent(font string, fontSize, x, y int32) MultilineTextEvent {
nextTextEventId++ //Oddly can't do this in the struct
return &multilineTextEventData{textEventData{x, y}, lib.NewFont(font, fontSize), make([]lib.TextLine, 0), nextTextEventId}
}
// Reuse the text event with font/size information but clear the slice and update the id
// This is problematic for the builder pattern.
func (e *multilineTextEventData) Reinitialize() TextEvent {
e.text = make([]lib.TextLine, 0)
e.id = e.id + 1
return e
}
func (e *multilineTextEventData) AddText(text string) MultilineTextEvent {
e.text = append(e.text, lib.NewTextLine(text, e.Font))
return e
}
// If one wishes to specify diferent font or font size.
func (e *multilineTextEventData) AddTextLine(text lib.TextLine) MultilineTextEvent {
e.text = append(e.text, text)
return e
}
func (e *multilineTextEventData) GetText() []lib.TextLine {
return e.text
}
func (p *multilineTextEventData) GetId() int32 {
return p.id
}