-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooltip.go
More file actions
275 lines (251 loc) · 10.4 KB
/
tooltip.go
File metadata and controls
275 lines (251 loc) · 10.4 KB
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
package consolizer
import (
"github.com/supercom32/consolizer/memory"
"github.com/supercom32/consolizer/stringformat"
"time"
"github.com/supercom32/consolizer/constants"
"github.com/supercom32/consolizer/types"
)
type TooltipInstanceType struct {
BaseControlInstanceType
}
type tooltipType struct{}
var Tooltip tooltipType
var Tooltips = memory.NewControlMemoryManager[types.TooltipEntryType]()
// ============================================================================
// REGULAR ENTRY
// ============================================================================
func (shared *TooltipInstanceType) SetEnabled(enabled bool) *TooltipInstanceType {
tooltipEntry := Tooltips.Get(shared.layerAlias, shared.controlAlias)
if tooltipEntry != nil {
tooltipEntry.IsEnabled = enabled
}
return shared
}
func (shared *tooltipType) Add(layerAlias string, tooltipAlias string, tooltipText string, styleEntry types.TuiStyleEntryType, hotspotXLocation int, hotspotYLocation int, hotspotWidth int, hotspotHeight int, tooltipXLocation int, tooltipYLocation int, tooltipWidth int, tooltipHeight int, isLocationAbsolute bool, isBorderDrawn bool, hoverTime int) TooltipInstanceType {
tooltipEntry := types.NewTooltipEntry()
tooltipEntry.StyleEntry = styleEntry
tooltipEntry.Alias = tooltipAlias
tooltipEntry.Text = tooltipText
tooltipEntry.HotspotXLocation = hotspotXLocation
tooltipEntry.HotspotYLocation = hotspotYLocation
tooltipEntry.HotspotWidth = hotspotWidth
tooltipEntry.HotspotHeight = hotspotHeight
tooltipEntry.TooltipXLocation = tooltipXLocation
tooltipEntry.TooltipYLocation = tooltipYLocation
tooltipEntry.TooltipWidth = tooltipWidth
tooltipEntry.TooltipHeight = tooltipHeight
tooltipEntry.IsLocationAbsolute = isLocationAbsolute
tooltipEntry.IsBorderDrawn = isBorderDrawn
tooltipEntry.HoverDisplayDelay = hoverTime
Tooltips.Add(layerAlias, tooltipAlias, &tooltipEntry)
var tooltipInstance TooltipInstanceType
tooltipInstance.layerAlias = layerAlias
tooltipInstance.controlAlias = tooltipAlias
tooltipInstance.controlType = "tooltip"
return tooltipInstance
}
/*
DeleteButton allows you to remove a button from a text layer. In addition,
the following information should be noted:
- If you attempt to delete a button which does not exist, then the request
will simply be ignored.
*/
func (shared *tooltipType) DeleteTooltip(layerAlias string, labelAlias string) {
Tooltips.Remove(layerAlias, labelAlias)
}
func (shared *tooltipType) DeleteAllTooltips(layerAlias string) {
Tooltips.RemoveAll(layerAlias)
}
/*
drawButtonsOnLayer allows you to draw all buttons on a given text layer.
*/
func (shared *tooltipType) drawTooltipHotspotZonesOnLayer(layerEntry types.LayerEntryType) {
layerAlias := layerEntry.LayerAlias
for _, currentTooltipEntry := range Tooltips.GetAllEntries(layerAlias) {
tooltipEntry := currentTooltipEntry
shared.drawTooltipHotspot(&layerEntry, tooltipEntry)
}
}
// TOOD: This method should really just take in a tooltip entry instead?
func (shared *tooltipType) drawTooltipHotspot(layerEntry *types.LayerEntryType, tooltipEntry *types.TooltipEntryType) {
if !tooltipEntry.IsEnabled {
return
}
attributeEntry := types.NewAttributeEntry()
attributeEntry.ForegroundColor = tooltipEntry.StyleEntry.Tooltip.ForegroundColor
attributeEntry.BackgroundColor = tooltipEntry.StyleEntry.Tooltip.BackgroundColor
attributeEntry.CellType = constants.CellTypeTooltip
attributeEntry.CellControlAlias = tooltipEntry.Alias
if tooltipEntry.ParentControlAlias == "" { // If a parent exists, do not overwrite the parent's attributes.
fillAreaWithControlAlias(layerEntry, attributeEntry.CellType, attributeEntry.CellControlAlias, tooltipEntry.HotspotXLocation, tooltipEntry.HotspotYLocation, tooltipEntry.HotspotWidth, tooltipEntry.HotspotHeight, constants.NullCellControlLocation)
}
}
func (shared *tooltipType) renderAllTooltips(layerEntry types.LayerEntryType) {
for _, currentTooltipEntry := range Tooltips.GetAllEntriesOverall() {
tooltipEntry := currentTooltipEntry
shared.renderTooltip(&layerEntry, tooltipEntry)
}
}
func (shared *tooltipType) renderTooltip(layerEntry *types.LayerEntryType, tooltipEntry *types.TooltipEntryType) {
if !tooltipEntry.IsEnabled {
return
}
if !tooltipEntry.IsDrawn {
return
}
attributeEntry := types.NewAttributeEntry()
attributeEntry.ForegroundColor = tooltipEntry.StyleEntry.Tooltip.ForegroundColor
attributeEntry.BackgroundColor = tooltipEntry.StyleEntry.Tooltip.BackgroundColor
attributeEntry.CellType = constants.CellTypeTooltip
attributeEntry.CellControlAlias = tooltipEntry.Alias
calculatedXLocation := tooltipEntry.TooltipXLocation - 2
calculatedYLocation := tooltipEntry.TooltipYLocation - 1
calculatedWidth := tooltipEntry.TooltipWidth + 1
calculatedHeight := tooltipEntry.TooltipHeight
if !tooltipEntry.IsLocationAbsolute {
mouseXLocation, mouseYLocation, _, _ := GetMouseStatus()
calculatedXLocation = mouseXLocation + tooltipEntry.TooltipXLocation - 2
calculatedYLocation = mouseYLocation + tooltipEntry.TooltipYLocation - 1
calculatedWidth = tooltipEntry.TooltipWidth + 1
calculatedHeight = tooltipEntry.TooltipHeight
}
fillArea(layerEntry, attributeEntry, " ", calculatedXLocation, calculatedYLocation, calculatedWidth, calculatedHeight, constants.NullCellControlLocation)
if tooltipEntry.IsBorderDrawn {
drawBorder(layerEntry, tooltipEntry.StyleEntry, attributeEntry, calculatedXLocation, calculatedYLocation, calculatedWidth, calculatedHeight, false)
}
formattedLabel := tooltipEntry.Text
arrayOfRunes := stringformat.GetRunesFromString(formattedLabel)
printLayerWithWordWrap(layerEntry, attributeEntry, calculatedXLocation+2, calculatedYLocation+1, calculatedWidth-1, arrayOfRunes)
}
func (shared *tooltipType) getTooltipFromCharacterEntry(entry types.CharacterEntryType) *types.TooltipEntryType {
layer := entry.LayerAlias
alias := entry.AttributeEntry.CellControlAlias
switch entry.AttributeEntry.CellType {
case constants.CellTypeButton:
if Buttons.IsExists(layer, alias) {
button := Buttons.Get(layer, alias)
if button.TooltipAlias != "" {
return Tooltips.Get(layer, button.TooltipAlias)
}
}
case constants.CellTypeLabel:
if Labels.IsExists(layer, alias) {
label := Labels.Get(layer, alias)
if label.TooltipAlias != "" {
return Tooltips.Get(layer, label.TooltipAlias)
}
}
case constants.CellTypeCheckbox:
if Checkboxes.IsExists(layer, alias) {
checkbox := Checkboxes.Get(layer, alias)
if checkbox.TooltipAlias != "" {
return Tooltips.Get(layer, checkbox.TooltipAlias)
}
}
case constants.CellTypeRadioButton:
if RadioButtons.IsExists(layer, alias) {
radio := RadioButtons.Get(layer, alias)
if radio.TooltipAlias != "" {
return Tooltips.Get(layer, radio.TooltipAlias)
}
}
case constants.CellTypeTextField:
if TextFields.IsExists(layer, alias) {
textField := TextFields.Get(layer, alias)
if textField.TooltipAlias != "" {
return Tooltips.Get(layer, textField.TooltipAlias)
}
}
case constants.CellTypeTextbox:
if Textboxes.IsExists(layer, alias) {
textbox := Textboxes.Get(layer, alias)
if textbox.TooltipAlias != "" {
return Tooltips.Get(layer, textbox.TooltipAlias)
}
}
case constants.CellTypeProgressBar:
if ProgressBars.IsExists(layer, alias) {
progressBar := ProgressBars.Get(layer, alias)
if progressBar.TooltipAlias != "" {
return Tooltips.Get(layer, progressBar.TooltipAlias)
}
}
case constants.CellTypeSelectorItem:
if Selectors.IsExists(layer, alias) {
selector := Selectors.Get(layer, alias)
if selector.TooltipAlias != "" {
return Tooltips.Get(layer, selector.TooltipAlias)
}
}
case constants.CellTypeTooltip:
if Tooltips.IsExists(layer, alias) {
return Tooltips.Get(layer, alias)
}
}
return nil
}
func (shared *tooltipType) updateMouseEvent() bool {
isScreenUpdateRequired := false
mouseXLocation, mouseYLocation, _, _ := GetMouseStatus()
characterEntry := getCellInformationUnderMouseCursor(mouseXLocation, mouseYLocation)
if eventStateMemory.stateId != constants.EventStateNone {
return false
}
tooltipEntry := shared.getTooltipFromCharacterEntry(characterEntry)
if tooltipEntry != nil {
mouseXLocation, mouseYLocation, _, _ = GetMouseStatus()
if tooltipEntry.HoverStartTime.IsZero() {
setPreviouslyHighlightedControl(characterEntry.LayerAlias, characterEntry.AttributeEntry.CellControlAlias, constants.CellTypeTooltip)
tooltipEntry.HoverStartTime = time.Now()
tooltipEntry.HoverXLocation = mouseXLocation
tooltipEntry.HoverYLocation = mouseYLocation
return isScreenUpdateRequired
}
if tooltipEntry.HoverXLocation != mouseXLocation || tooltipEntry.HoverYLocation != mouseYLocation {
tooltipEntry.HoverStartTime = time.Time{}
return isScreenUpdateRequired
}
if time.Since(tooltipEntry.HoverStartTime) >= time.Duration(tooltipEntry.HoverDisplayDelay)*time.Millisecond {
setPreviouslyHighlightedControl(characterEntry.LayerAlias, characterEntry.AttributeEntry.CellControlAlias, constants.CellTypeTooltip)
tooltipEntry.IsDrawn = true
isScreenUpdateRequired = true
}
} else {
for _, currentTooltipEntry := range Tooltips.GetAllEntriesOverall() {
if currentTooltipEntry.IsDrawn == true {
// Only update if a change was detected.
isScreenUpdateRequired = true
}
currentTooltipEntry.IsDrawn = false
currentTooltipEntry.HoverStartTime = time.Time{}
}
if eventStateMemory.previouslyHighlightedControl.controlType == constants.CellTypeTooltip {
setPreviouslyHighlightedControl("", "", constants.NullControlType)
}
}
return isScreenUpdateRequired
}
func (shared *TooltipInstanceType) setParentControlAlias(parentControlAlias string) *TooltipInstanceType {
tooltipEntry := Tooltips.Get(shared.layerAlias, shared.controlAlias)
if tooltipEntry != nil {
tooltipEntry.ParentControlAlias = parentControlAlias
}
return shared
}
/*
SetTooltipValue allows you to set the value of the tooltip associated with the TooltipInstanceType.
This function updates the value of the tooltip label identified by the layerAlias and tooltipAlias fields.
*/
func (shared *TooltipInstanceType) SetTooltipValue(text string) *TooltipInstanceType {
labelEntry := Labels.Get(shared.layerAlias, shared.controlAlias)
labelEntry.Text = text
return shared
}
/*
SetText allows you to set the text of the tooltip. This is an alias for SetTooltipValue
for consistency with other controls.
*/
func (shared *TooltipInstanceType) SetText(text string) *TooltipInstanceType {
return shared.SetTooltipValue(text)
}