-
Notifications
You must be signed in to change notification settings - Fork 7
/
base.framework.toastmessage.bmx
executable file
·588 lines (424 loc) · 16.1 KB
/
base.framework.toastmessage.bmx
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
Rem
====================================================================
class providing a simple toast message functionality
====================================================================
Toast messages are small message boxes in the corners of the screen
(or a manual position) to show the user some information. They might
auto close after a given amount of time or react on clicks.
====================================================================
If not otherwise stated, the following code is available under the
following licence:
LICENCE: zlib/libpng
Copyright (C) 2002-2015 Ronny Otto, digidea.de
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
====================================================================
EndRem
SuperStrict
Import "base.framework.entity.bmx"
Import "base.gfx.bitmapfont.bmx"
Import "base.gfx.renderconfig.bmx"
Import "base.util.event.bmx"
Import "base.util.input.bmx"
Type TToastMessageCollection extends TRenderableEntity
'spawnPoints contain the messages
Field spawnPoints:TMap = CreateMap()
Global _instance:TToastMessageCollection
Global _eventsRegistered:Int
Global eventKey_ToastMessageCollection_onAddMessage:TEventKey = EventManager.GetEventKey("ToastMessageCollection.onAddMessage", True)
Global eventKey_ToastMessage_onClose:TEventKey = EventManager.GetEventKey("ToastMessage.onClose", True)
Global eventKey_ToastMessage_onOpen:TEventKey = EventManager.GetEventKey("ToastMessage.onOpen", True)
Global eventKey_ToastMessage_onClick:TEventKey = EventManager.GetEventKey("ToastMessage.onClick", True)
Method New()
if not _eventsRegistered then RegisterEvents()
'occupy all/no space (different to 0,0 !!)
self.area.dimension.SetXY(-1,-1)
End Method
Function GetInstance:TToastMessageCollection()
if not _instance then _instance = new TToastMessageCollection
return _instance
End Function
Function RegisterEvents:Int()
if _eventsRegistered then return False
'remove closed messages
EventManager.registerListenerFunction(eventKey_ToastMessage_onClose, onCloseToastMessage)
_eventsRegistered = True
return True
End Function
'=== EVENT FUNCTIONS ===
'remove a closed message from all potential spawning points
Function onCloseToastMessage:Int(triggerEvent:TEventBase)
local toastMessage:TToastMessage = TToastMessage(triggerEvent.GetSender())
if not toastMessage then return False
return GetInstance().RemoveMessage(toastMessage)
End Function
'=== UTILITY FUNCTIONS ===
'removes all message in all spawn points
Method RemoveAllMessages:Int()
for local spawnPoint:TToastMessageSpawnPoint = EachIn spawnPoints.Values()
spawnPoint.RemoveAllMessages()
next
End Method
'add a point for messages to spawn from
Method AddSpawnPoint:Int(spawnPoint:TToastMessageSpawnPoint)
spawnPoint.SetParent(self)
spawnPoints.insert(spawnPoint.name.toUpper(), spawnPoint)
return True
End Method
'add a point for messages to spawn from
Method AddNewSpawnPoint:Int(area:TRectangle, alignment:TVec2D, name:String)
local spawnPoint:TToastMessageSpawnPoint = new TToastMessageSpawnPoint
spawnPoint.area.copyFrom(area)
if alignment then spawnPoint.alignment = alignment.copy()
spawnPoint.name = name
return AddSpawnPoint(spawnPoint)
End Method
'add a message after others
Method AddMessage:Int(message:TToastMessage, addToSpawnPoint:String, skipDuplicates:Int = True)
if skipDuplicates and ContainsMessage(message) then return False
'get spawnpoint
local spawnPoint:TToastMessageSpawnPoint = TToastMessageSpawnPoint(spawnPoints.ValueForKey(addToSpawnPoint.ToUpper()))
if not spawnPoint then return False
spawnPoint.AddMessage(message)
'send out event - eg for sounds
TriggerBaseEvent(eventKey_ToastMessageCollection_onAddMessage, new TData.Add("spawnPoint", spawnPoint), null, message )
return True
End Method
'add a message on top of others
Method AddMessageFirst:Int(message:TToastMessage, addToSpawnPoint:String, skipDuplicates:Int = True)
if skipDuplicates and ContainsMessage(message) then return False
'get spawnpoint
local spawnPoint:TToastMessageSpawnPoint = TToastMessageSpawnPoint(spawnPoints.ValueForKey(addToSpawnPoint.ToUpper()))
if not spawnPoint then return False
spawnPoint.AddMessageFirst(message)
'send out event - eg for sounds
TriggerBaseEvent(eventKey_ToastMessageCollection_onAddMessage, new TData.Add("spawnPoint", spawnPoint), null, message )
return True
End Method
'checks if the message list contains the given message
Method ContainsMessage:Int(message:TToastMessage)
for local spawnPoint:TToastMessageSpawnPoint = EachIn spawnPoints.Values()
if spawnPoint.ContainsMessage(message) then return True
next
return False
End Method
'removes a message from the list
Method RemoveMessage:Int(message:TToastMessage)
for local spawnPoint:TToastMessageSpawnPoint = EachIn spawnPoints.Values()
if spawnPoint.RemoveMessage(message) then return True
next
return False
End Method
Method GetMessageByGUID:TToastMessage(guid:string)
local m:TToastMessage = null
for local spawnPoint:TToastMessageSpawnPoint = EachIn spawnPoints.Values()
m = spawnPoint.GetMessageByGUID(guid)
if m then return m
next
return Null
End Method
Method Render:Int(xOffset:Float = 0, yOffset:Float=0, alignment:TVec2D = Null)
For local spawnPoint:TToastMessageSpawnPoint = EachIn spawnPoints.Values()
spawnPoint.Render(xOffset, yOffset)
Next
'=== DRAW CHILDREN ===
RenderChildren(xOffset, yOffset, alignment)
End Method
Method Update:Int()
if spawnPoints
'using "copy()" leads to segfaults in "TMap.NextObject() -> NextNode()"
'For local spawnPoint:TToastMessageSpawnPoint = EachIn spawnPoints.Copy().Values()
For local spawnPoint:TToastMessageSpawnPoint = EachIn spawnPoints.Values()
spawnPoint.Update()
Next
endif
'=== UPDATE CHILDREN ===
UpdateChildren()
End Method
End Type
'===== CONVENIENCE ACCESSOR =====
'return collection instance
Function GetToastMessageCollection:TToastMessageCollection()
Return TToastMessageCollection.GetInstance()
End Function
Type TToastMessageSpawnPoint extends TEntity
'alignment of messages according to the position of the spawnpoint
Field alignment:TVec2D
'messages of the spawnpoint
Field messages:TObjectList = new TObjectList
'=== CONFIGURATION ===
'vector describing spaces between two messages
Field spacerSize:TVec2D = New TVec2D.Init(0,5)
'render a background?
Field showBackground:Int = False
Global GROW_DOWN:int = 0
Global GROW_UP:int = 1
'add a message after others
Method AddMessage:Int(message:TToastMessage, skipDuplicates:Int = True)
if skipDuplicates and messages.contains(message) then return False
message.SetParent(self)
messages.AddLast(message)
return True
End Method
'add a message on top of others
Method AddMessageFirst:Int(message:TToastMessage, skipDuplicates:Int = True)
if skipDuplicates and messages.contains(message) then return False
message.SetParent(self)
messages.AddFirst(message)
return True
End Method
Method RemoveAllMessages:Int()
messages.Clear()
End Method
Method RemoveMessage:Int(message:TToastMessage)
return messages.Remove(message)
End Method
Method ContainsMessage:Int(message:TToastMessage)
return messages.Contains(message)
End Method
Method GetMessageByGUID:TToastMessage(guid:string)
For local m:TToastMessage = EachIn messages
if m.GetGUID() = guid then return m
Next
return Null
End Method
'override to allow alignment
Method GetChildX:Float(child:TRenderableEntity = Null)
if not child then return Super.GetChildX()
return alignment.GetX() * (GetScreenRect().GetW() - child.area.GetW())
End Method
'override to displace if there are other entities
Method GetChildY:Float(child:TRenderableEntity = Null)
if not child then return Super.GetChildY()
local result:Float = 0
'if alignment.y = GROW_DOWN then result = 0
if alignment.y = GROW_UP then result :+ area.GetH()
For local message:TToastMessage = EachIn messages
if message = child
if alignment.y = GROW_UP then result :- message.area.GetH()
return result
endif
Select alignment.y
case GROW_DOWN
result :+ message.area.GetH()
result :+ spacerSize.GetY()
case GROW_UP
result :- message.area.GetH()
result :- spacerSize.GetY()
End Select
Next
return result
End Method
Method RenderBackground:Int(xOffset:Float=0, yOffset:Float=0)
local oldAlpha:Float = GetAlpha()
SetAlpha oldAlpha * 0.3
SetColor 255,0,0
DrawRect(GetScreenRect().GetX(), GetScreenRect().GetY(), GetScreenRect().GetW(), GetScreenRect().GetH())
SetAlpha oldAlpha
SetColor 255,255,255
End Method
Method Render:Int(xOffset:Float = 0, yOffset:Float = 0, alignment:TVec2D = Null)
'store old render config and adjust to our needs
local renderConfig:TRenderconfig = TRenderConfig.Push()
if HasSize() then GetGraphicsManager().SetViewPort(int(GetScreenRect().GetX()), int(GetScreenRect().GetY()), int(GetScreenRect().GetW()), int(GetScreenRect().GetH()))
if showBackground then RenderBackground(xOffset, yOffset)
For local message:TToastMessage = EachIn messages
message.Render(xOffset, yOffset, alignment)
Next
'=== DRAW CHILDREN ===
RenderChildren(xOffset, yOffset)
'restore old render config
TRenderConfig.Pop()
End Method
Method Update:Int()
For local message:TToastMessage = EachIn messages
message.Update()
Next
'=== UPDATE CHILDREN ===
UpdateChildren()
End Method
End Type
Const TOASTMESSAGE_CLOSED:Int = 1
Const TOASTMESSAGE_OPEN:Int = 2
Const TOASTMESSAGE_OPENING_OR_CLOSING:Int = 4
Type TToastMessage extends TEntity
'a potential canvas to draw things on
Field canvasImage:TImage = null
Field _status:Int = 1 'closed
'time the animation for opening/closing takes, 0 to disable
Field _openCloseDuration:Float = 0.25
Field _openCloseTimeGone:Float = 0
Field _lifeTime:Float = -1
Field _lifeTimeStartValue:Float = -1
Field _lifeTimeBarHeight:int = 5
Field _lifeTimeBarColor:TColor
Field _lifeTimeBarBottomY:int = 15
Field _textOffset:TVec2D
'additional data
Field _data:TData
Field _onCloseFunction:int(sender:TToastMessage)
Global defaultDimension:TVec2D = new TVec2D.Init(200,50)
Global defaultLifeTimeBarHeight:int = 10
Global defaultLifeTimeBarColor:TColor = TColor.clWhite
Global defaultLifeTimeBarBottomY:int = 15
Global defaultTextOffset:TVec2D = new TVec2D.Init(5,5)
Method New()
area.dimension = defaultDimension.Copy()
_lifeTimeBarHeight = defaultLifeTimeBarHeight
_lifeTimeBarColor = defaultLifeTimeBarColor.Copy()
_lifeTimeBarBottomY = defaultLifeTimeBarBottomY
_textOffset = defaultTextOffset.Copy()
Open()
End Method
Method GenerateGUID:string()
return "toastmessage-"+id
End Method
'sets a function to call when the message gets closed
Method SetOnCloseFunction(onCloseFunction:int(sender:TToastMessage))
_onCloseFunction = onCloseFunction
End Method
'sets the data the close function gets as param
Method SetData(data:TData)
_data = data
End Method
Method GetData:TData()
if not _data then _data = new TData
return _data
End Method
Method SetDimension:Int(w:int = 0, h:int = 0)
self.area.dimension.SetXY(w, h)
End Method
Method SetStatus(statusCode:Int, enable:Int=True)
If enable
_status :| statusCode
Else
_status :& ~statusCode
EndIf
End Method
Method HasStatus:Int(statusCode:Int)
Return _status & statusCode
End Method
Method Close:Int()
if HasStatus(TOASTMESSAGE_OPEN)
SetStatus(TOASTMESSAGE_OPENING_OR_CLOSING, True)
_openCloseTimeGone = 0
endif
End Method
Method Open:Int()
if HasStatus(TOASTMESSAGE_CLOSED)
SetStatus(TOASTMESSAGE_OPENING_OR_CLOSING, True)
_openCloseTimeGone = 0
endif
End Method
Method IsOpen:Int()
return HasStatus(TOASTMESSAGE_OPEN) and not HasStatus(TOASTMESSAGE_OPENING_OR_CLOSING)
End Method
Method IsClosed:Int()
return HasStatus(TOASTMESSAGE_CLOSED) and not HasStatus(TOASTMESSAGE_OPENING_OR_CLOSING)
End Method
Method SetClosed:Int()
SetStatus(TOASTMESSAGE_OPEN, False)
SetStatus(TOASTMESSAGE_CLOSED, True)
SetStatus(TOASTMESSAGE_OPENING_OR_CLOSING, False)
'fire event so others can handle it (eg. remove from list)
TriggerBaseEvent(TToastMessageCollection.eventKey_ToastMessage_onClose, null, Self)
if _onCloseFunction then _onCloseFunction(self)
End Method
Method SetOpen:Int()
SetStatus(TOASTMESSAGE_OPEN, True)
SetStatus(TOASTMESSAGE_CLOSED, False)
SetStatus(TOASTMESSAGE_OPENING_OR_CLOSING, False)
'fire event so others can handle it
TriggerBaseEvent(TToastMessageCollection.eventKey_ToastMessage_onOpen, null, Self)
End Method
Method SetLifeTime:Int(lifeTime:Float = -1)
_lifeTime = lifeTime
_lifeTimeStartValue = lifeTime
End Method
Method GetLifeTimeProgress:Float()
return _lifeTime / _lifeTimeStartValue
End Method
Method GetOpeningClosingProgress:Float()
return Max(0, Min(1, _openCloseTimeGone / _openCloseDuration))
End Method
'override default to add extra handling
Method Update:Int()
'check if lifetime is running out - close message then
if _lifeTime > 0
_lifeTime :- GetDeltaTimer().GetDelta()
if _lifeTime < 0 then Close()
endif
'check if closing/opening finished
if HasStatus(TOASTMESSAGE_OPENING_OR_CLOSING)
_openCloseTimeGone :+ GetDeltaTimer().GetDelta()
if _openCloseTimeGone > _openCloseDuration
if HasStatus(TOASTMESSAGE_CLOSED)
SetOpen()
else
SetClosed()
return True
endif
endif
endif
'check clicked state
If GetScreenArea().containsXY(MouseManager.x, MouseManager.y)
If MouseManager.IsClicked(1)
Close()
'fire event (eg. to play sound)
TriggerBaseEvent(TToastMessageCollection.eventKey_ToastMessage_onClick, new TData.AddNumber("mouseButton", 1), Self)
'handled single click
MouseManager.SetClickHandled(1)
Endif
Endif
End Method
Method RenderBackground:Int(xOffset:Float=0, yOffset:Float=0)
if canvasImage
DrawImage(canvasImage, xOffset + GetScreenRect().GetX(), yOffset + GetScreenRect().GetY())
'rem
else
DrawRect(xOffset + GetScreenRect().GetX(), yOffset + GetScreenRect().GetY(), area.GetW(), area.GetH())
_lifeTimeBarColor.SetRGB()
if _lifeTime > 0
local lifeTimeWidth:int = GetScreenRect().GetW() - 2 * _textOffset.GetIntX()
lifeTimeWidth :* GetLifeTimeProgress()
DrawRect(xOffset + GetScreenRect().GetX() + _textOffset.GetIntX(), yOffset + GetScreenRect().GetY() + area.GetH() - _lifeTimeBarBottomY, lifeTimeWidth, _lifeTimeBarHeight)
endif
SetColor 255,255,255
DrawText(name+" "+id, xOffset + GetScreenRect().GetX() + _textOffset.GetIntX(), yOffset + GetScreenRect().GetY() + _textOffset.GetIntY())
'endrem
endif
End Method
Method RenderForeground:Int(xOffset:Float=0, yOffset:Float=0)
'
End Method
Method RenderContent:Int(xOffset:Float=0, yOffset:Float=0)
RenderBackground(xOffset, yOffset)
RenderForeground(xOffset, yOffset)
End Method
Method Render:Int(xOffset:Float = 0, yOffset:Float = 0, alignment:TVec2D = Null)
'do not draw closed and unanimated elements
if HasStatus(TOASTMESSAGE_CLOSED) and not HasStatus(TOASTMESSAGE_OPENING_OR_CLOSING) then return False
'prepare visuals (alpha)
local progress:Float = GetOpeningClosingProgress()
if HasStatus(TOASTMESSAGE_OPEN) then progress = 1 - progress
local oldAlpha:Float = GetAlpha()
if HasStatus(TOASTMESSAGE_OPENING_OR_CLOSING) then SetAlpha(oldAlpha * progress)
'draw the message container itself
RenderContent(xOffset, yOffset)
SetAlpha(oldAlpha)
'=== DRAW CHILDREN ===
RenderChildren(xOffset, yOffset)
End Method
End Type