-
Notifications
You must be signed in to change notification settings - Fork 2
/
screen_clientScreen.go
727 lines (619 loc) · 29.2 KB
/
screen_clientScreen.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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
package main
import (
"fmt"
"github.com/ably-labs/Ableye/button"
colour "github.com/ably-labs/Ableye/colours"
font "github.com/ably-labs/Ableye/fonts"
"github.com/ably-labs/Ableye/text"
"github.com/ably-labs/Ableye/textbox"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
type connectionElements struct {
id connectionID
createRealtimeClient button.Button
createRestClient button.Button
clientLabel button.Button
closeClient button.Button
setChannel button.Button
channelName text.Text
channelStatus text.Text
channelDetach button.Button
channelAttach button.Button
channelSubscribeAll button.Button
getChannelStatus button.Button
presenceInfo text.Text
enterPresence button.Button
getPresence button.Button
leavePresence button.Button
eventInfo text.Text
channelNameLabel text.Text
channelNameInput textbox.TextBox
messageNameLabel text.Text
messageNameInput textbox.TextBox
messageDataLabel text.Text
messageDataInput textbox.TextBox
channelPublish button.Button
}
// The elements of the realtime screen.
var (
infoBar button.Button
//Connection Elements
connectionA, connectionB, connectionC, connectionD connectionElements
)
func initialiseRealtimeScreen() {
infoBar = button.NewButton(screenWidth, 35, "", 22, 22, colour.Black, font.MplusSmallFont, colour.White, 0, 25)
//Initialise connection elements.
initialiseConnectionElements(&connectionA, clientA, 0, screenHeight/6)
initialiseConnectionElements(&connectionB, clientB, screenWidth/2, screenHeight/6)
initialiseConnectionElements(&connectionC, clientC, 0, (screenHeight/2)+75)
initialiseConnectionElements(&connectionD, clientD, screenWidth/2, (screenHeight/2)+75)
}
// initialiseConnectionElements, creates all the button, text and text box elements
// that are required for a connection with correct colours, sizes, fonts and settings
// and saves the connectionElement information in a global variable
func initialiseConnectionElements(elements *connectionElements, id connectionID, x int, y int) {
elements.id = id
elements.createRealtimeClient = button.NewButton(150, 35, createRealtimeClientText, 16, 22, colour.Black, font.MplusSmallFont, colour.White, x, y)
elements.createRestClient = button.NewButton(150, 35, createRestClientText, 32, 22, colour.Black, font.MplusSmallFont, colour.White, x+151, y)
elements.clientLabel = button.NewButton(400, 35, "", 12, 22, colour.Black, font.MplusSmallFont, colour.ZingyGreen, x, y)
elements.closeClient = button.NewButton(35, 35, "X", 12, 22, colour.Black, font.MplusSmallFont, colour.BrightRed, 0, 0)
elements.channelName = text.NewText("", colour.ZingyGreen, font.MplusSmallFont, 0, 0)
elements.channelStatus = text.NewText("", colour.ZingyGreen, font.MplusSmallFont, 0, 0)
elements.channelDetach = button.NewButton(75, 30, detachText, 10, 20, colour.Black, font.MplusSmallFont, colour.ZingyGreen, 0, 0)
elements.channelAttach = button.NewButton(75, 30, attachText, 10, 20, colour.Black, font.MplusSmallFont, colour.ZingyGreen, 0, 0)
elements.getChannelStatus = button.NewButton(100, 30, getChannelStatusText, 10, 20, colour.Black, font.MplusSmallFont, colour.ZingyGreen, 0, 0)
elements.channelSubscribeAll = button.NewButton(115, 30, subscribeAllText, 10, 20, colour.Black, font.MplusSmallFont, colour.ZingyGreen, 0, 0)
elements.presenceInfo = text.NewText("", colour.ElectricCyan, font.MplusSmallFont, 0, 0)
elements.enterPresence = button.NewButton(65, 30, enterPresenceText, 12, 20, colour.Black, font.MplusSmallFont, colour.ElectricCyan, 0, 0)
elements.getPresence = button.NewButton(50, 30, getPresenceText, 12, 20, colour.Black, font.MplusSmallFont, colour.ElectricCyan, 0, 0)
elements.leavePresence = button.NewButton(70, 30, leavePresenceText, 12, 20, colour.Black, font.MplusSmallFont, colour.ElectricCyan, 0, 0)
elements.eventInfo = text.NewText("", colour.White, font.MplusSmallFont, 0, 0)
elements.channelNameLabel = text.NewText(fmt.Sprintf("%s :", channelNameText), colour.ZingyGreen, font.MplusSmallFont, 0, 0)
elements.channelNameInput = textbox.NewTextBox(200, 36, 4, defaultChannelName, 9, 12, 22, colour.ZingyGreen, font.MplusSmallFont, colour.Black, colour.ZingyGreen, 0, 0)
elements.setChannel = button.NewButton(150, 35, setChannelText, 22, 22, colour.Black, font.MplusSmallFont, colour.ZingyGreen, 0, 0)
elements.messageNameLabel = text.NewText(fmt.Sprintf("%s :", messageNameText), colour.JazzyPink, font.MplusSmallFont, 0, 0)
elements.messageNameInput = textbox.NewTextBox(200, 36, 4, defaultMessageName, 12, 12, 22, colour.JazzyPink, font.MplusSmallFont, colour.Black, colour.JazzyPink, 0, 0)
elements.messageDataLabel = text.NewText(fmt.Sprintf("%s :", messageDataText), colour.JazzyPink, font.MplusSmallFont, 0, 0)
elements.messageDataInput = textbox.NewTextBox(200, 36, 4, defaultMessageData, 12, 12, 22, colour.JazzyPink, font.MplusSmallFont, colour.Black, colour.JazzyPink, 0, 0)
elements.channelPublish = button.NewButton(80, 30, publishText, 12, 20, colour.Black, font.MplusSmallFont, colour.JazzyPink, 0, 0)
}
// drawRealtimeScreen draws the realtime screen.
func drawRealtimeScreen(screen *ebiten.Image) {
// Info bar is used to display log messages and error messages.
infoBar.Draw(screen)
//Draw elements for each connection.
drawConnectionElements(screen, &connectionA)
drawConnectionElements(screen, &connectionB)
drawConnectionElements(screen, &connectionC)
drawConnectionElements(screen, &connectionD)
}
// drawConnectionElements draws all the elements associated with a connection to the screen.
func drawConnectionElements(screen *ebiten.Image, elements *connectionElements) {
id := elements.id
// if there is no connection
if connections[id] == nil {
// Draw the buttons that can create a connection with a client.
elements.createRealtimeClient.Draw(screen)
elements.createRestClient.Draw(screen)
}
// if client has been created
if connections[id] != nil && (connections[id].realtimeClient != nil || connections[id].restClient != nil) {
// Move the client label to the location of the create realtime button.
elements.clientLabel.X = elements.createRealtimeClient.X
elements.clientLabel.Y = elements.createRealtimeClient.Y
if elements.clientLabel.GetText() == "" {
if isRealtimeClient(id) {
elements.clientLabel.SetText(fmt.Sprintf("%s - %s", connections[id].realtimeClient.Auth.ClientID(), connections[id].connectionType.string()))
}
if isRestClient(id) {
elements.clientLabel.SetText(fmt.Sprintf("%s - %s", connections[id].restClient.Auth.ClientID(), connections[id].connectionType.string()))
}
}
elements.clientLabel.Draw(screen)
drawClientInfo(screen, elements.clientLabel, &elements.closeClient)
elements.closeClient.Draw(screen)
// If a channel has not been set for this client, draw the elements required to set the channel.
if (isRealtimeClient(id) && connections[id].realtimeChannel == nil) || (isRestClient(id) && connections[id].restChannel == nil) {
drawSetChannel(screen, elements.clientLabel, elements.channelNameLabel, &elements.channelNameInput, &elements.setChannel)
}
}
// If client channel has been created.
if (isRealtimeClient(id) && connections[id].realtimeChannel != nil) || (isRestClient(id) && connections[id].restChannel != nil) {
drawChannelInfo(screen, elements)
}
// If a channel has been subscribed an unsubscribe function will be saved in memory.
// If an unsubscribe function exists, draw event info.
if connections[id] != nil && connections[id].unsubscribe != nil {
drawEventInfo(screen, elements.clientLabel, &elements.eventInfo)
}
}
// drawClientInfo draws the client window and the close client button.
func drawClientInfo(screen *ebiten.Image, clientLabel button.Button, closeClient *button.Button) {
// Elements are drawn in locations that are calculated from the location of the client label.
button := clientLabel
// Draw a window which is made from two overlapping images.
ebitenutil.DrawRect(screen, float64(button.X), float64(button.Y)+float64(button.Height), (screenWidth/2)-10, screenHeight/3, colour.ZingyGreen)
ebitenutil.DrawRect(screen, float64(button.X)+1, float64(button.Y)+float64(button.Height)+1, (screenWidth/2)-12, (screenHeight/3)-2, colour.Black)
// Draw the close client button.
closeClient.SetX(button.X + 638)
closeClient.SetY(button.Y)
closeClient.Draw(screen)
}
func drawSetChannel(screen *ebiten.Image, clientLabel button.Button, channelNameLabel text.Text, channelNameInput *textbox.TextBox, setChannel *button.Button) {
// Elements are drawn in locations that are calculated from the location of the client label.
button := clientLabel
// Channel name label.
channelNameLabel.SetX(button.X + 10)
channelNameLabel.SetY(button.Y + button.Height + 40)
channelNameLabel.Draw(screen)
// Channel name input text box.
channelNameInput.SetX(button.X + 150)
channelNameInput.SetY(button.Y + button.Height + 18)
channelNameInput.Draw(screen)
// Set channel button.
setChannel.SetX(button.X + 375)
setChannel.SetY(button.Y + button.Height + 18)
setChannel.Draw(screen)
}
// drawChannelInfo draws the channel window, the presence window and message controls.
func drawChannelInfo(screen *ebiten.Image, elements *connectionElements) {
// Elements are drawn in locations that are calculated from the location of the client label.
button := elements.clientLabel
id := elements.id
// Draw the channel window.
ebitenutil.DrawRect(screen, float64(button.X)+4, float64(button.Y)+float64(button.Height)+3, (screenWidth/2)-18, screenHeight/10, colour.ZingyGreen)
ebitenutil.DrawRect(screen, float64(button.X)+5, float64(button.Y)+float64(button.Height)+4, (screenWidth/2)-20, (screenHeight/10)-2, colour.Black)
// Draw the channel name text box.
elements.channelName.SetX(button.X + 10)
elements.channelName.SetY(button.Y + button.Height + 25)
if isRealtimeClient(id) {
elements.channelName.SetText(fmt.Sprintf("%s : %s", channelNameText, connections[id].realtimeChannel.Name))
}
if isRestClient(id) {
elements.channelName.SetText(fmt.Sprintf("%s : %s", channelNameText, connections[id].restChannel.Name))
}
elements.channelName.Draw(screen)
// Channel status, attach, detach and subscribe are only applicable to the realtime client.
if isRealtimeClient(id) {
// Draw the channel status text box.
elements.channelStatus.SetX(button.X + 230)
elements.channelStatus.SetY(button.Y + button.Height + 25)
elements.channelStatus.SetText(fmt.Sprintf("Status : %s", connections[id].realtimeChannel.State()))
elements.channelStatus.Draw(screen)
//Draw the channel detach button.
elements.channelDetach.SetX(button.X + 401)
elements.channelDetach.SetY(button.Y + button.Height + 4)
elements.channelDetach.Draw(screen)
//Draw the channel attach button.
elements.channelAttach.SetX(button.X + 477)
elements.channelAttach.SetY(button.Y + button.Height + 4)
elements.channelAttach.Draw(screen)
// Draw the channel subscribe/unsubscribe button.
elements.channelSubscribeAll.SetX(button.X + 553)
elements.channelSubscribeAll.SetY(button.Y + button.Height + 4)
elements.channelSubscribeAll.Draw(screen)
}
// GetChannelStatus is currently only applicable to the rest client.
if isRestClient(id) {
// draw a get channel status button.
elements.getChannelStatus.SetX(button.X + 230)
elements.getChannelStatus.SetY(button.Y + button.Height + 4)
elements.getChannelStatus.Draw(screen)
}
// Draw the presence window.
ebitenutil.DrawRect(screen, float64(button.X)+8, float64(button.Y)+float64(button.Height)+42, (screenWidth/2)-26, screenHeight/24, colour.ElectricCyan)
ebitenutil.DrawRect(screen, float64(button.X)+9, float64(button.Y)+float64(button.Height)+43, (screenWidth/2)-28, (screenHeight/24)-2, colour.Black)
// If presence information is being drawn in its initisalised location.
if elements.presenceInfo.X == 0 && elements.presenceInfo.Y == 0 {
// Initalise the text in the presence information.
elements.presenceInfo.SetText("Presence :")
}
// Draw the presence information.
elements.presenceInfo.SetX(button.X + 12)
elements.presenceInfo.SetY(button.Y + button.Height + 62)
elements.presenceInfo.Draw(screen)
// Draw the get presence button.
elements.getPresence.SetX(button.X + 543)
elements.getPresence.SetY(button.Y + button.Height + 43)
elements.getPresence.Draw(screen)
// Enter and leave presence are only applicable to the realtime client.
if isRealtimeClient(id) {
// Draw the enter presence button.
elements.enterPresence.SetX(button.X + 477)
elements.enterPresence.SetY(button.Y + button.Height + 43)
elements.enterPresence.Draw(screen)
// Draw the leave presence button.
elements.leavePresence.SetX(button.X + 594)
elements.leavePresence.SetY(button.Y + button.Height + 43)
elements.leavePresence.Draw(screen)
}
// Draw the message name label.
elements.messageNameLabel.SetX(button.X + 10)
elements.messageNameLabel.SetY(button.Y + button.Height + 105)
elements.messageNameLabel.Draw(screen)
// Draw the message name input text box.
elements.messageNameInput.SetX(button.X + 10)
elements.messageNameInput.SetY(button.Y + button.Height + 120)
elements.messageNameInput.Draw(screen)
// Draw the message data label.
elements.messageDataLabel.SetX(button.X + 300)
elements.messageDataLabel.SetY(button.Y + button.Height + 105)
elements.messageDataLabel.Draw(screen)
// Draw the message data input text box.
elements.messageDataInput.SetX(button.X + 300)
elements.messageDataInput.SetY(button.Y + button.Height + 120)
elements.messageDataInput.Draw(screen)
// Draw the channel publish button.
elements.channelPublish.SetX(button.X + 550)
elements.channelPublish.SetY(button.Y + button.Height + 125)
elements.channelPublish.Draw(screen)
}
// drawEventInfo draws the event window.
func drawEventInfo(screen *ebiten.Image, clientLabel button.Button, eventInfo *text.Text) {
// Elements are drawn in locations that are calculated from the location of the client label.
button := clientLabel
// Draw the event window.
ebitenutil.DrawRect(screen, float64(button.X)+4, float64(button.Y)+float64(button.Height)+182, (screenWidth/2)-18, screenHeight/12, colour.White)
ebitenutil.DrawRect(screen, float64(button.X)+5, float64(button.Y)+float64(button.Height)+183, (screenWidth/2)-20, (screenHeight/12)-2, colour.Black)
// If event information is being drawn in its initisalised location.
if eventInfo.X == 0 && eventInfo.Y == 0 {
// Initalise the text in the event information.
eventInfo.SetText(eventInfoText)
}
// Draw the event information.
eventInfo.SetX(button.X + 12)
eventInfo.SetY(button.Y + button.Height + 200)
eventInfo.Draw(screen)
}
// updateClientScreen updates the realtime screen.
func updateClientScreen() {
// If the Escape key is pressed, return to the title screen.
if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
state = titleScreen
}
// Update elements for each connection.
updateConnectionElements(&connectionA)
updateConnectionElements(&connectionB)
updateConnectionElements(&connectionC)
updateConnectionElements(&connectionD)
}
// updateConnectionElements updates all the elements for a connection.
func updateConnectionElements(elements *connectionElements) {
// Client elements.
updateCreateRealtimeClientButton(&elements.createRealtimeClient, elements.id)
updateCreateRestClientButton(&elements.createRestClient, elements.id)
updateCloseClientButton(&elements.closeClient, &elements.clientLabel, &elements.presenceInfo, &elements.eventInfo, elements.id)
// Set channel elements.
updateTextInputBox(&elements.channelNameInput, elements.id)
updateSetChannelButton(&elements.setChannel, elements.channelNameInput.GetText(), elements.id)
// Channel controls.
updateChannelDetachButton(&elements.channelDetach, elements.id)
updateChannelAttachButton(&elements.channelAttach, elements.id)
updateSubscribeChannelButton(&elements.channelSubscribeAll, &elements.eventInfo, elements.id)
updateGetChannelStatusButton(&elements.getChannelStatus, elements.id)
// Presence controls.
updateEnterPresenceButton(&elements.enterPresence, elements.id)
updateGetPresenceButton(&elements.getPresence, &elements.presenceInfo, elements.id)
updateLeavePresenceButton(&elements.leavePresence, elements.id)
// Message controls.
updateTextInputBox(&elements.messageNameInput, elements.id)
updateTextInputBox(&elements.messageDataInput, elements.id)
updateChannelPublishButton(&elements.channelPublish, elements.messageNameInput.GetText(), elements.messageDataInput.GetText(), elements.id)
}
// updateCreateRealtimeClientButton contains the update logic for each realtime client creation button.
func updateCreateRealtimeClientButton(button *button.Button, id connectionID) {
// Handle mouseover interaction with create button while a connection does not exist.
if button.IsMouseOver() && connections[id] == nil {
button.SetBgColour(colour.ZingyGreen)
}
// if the button is not moused over and there is no connection.
if !button.IsMouseOver() && connections[id] == nil {
button.SetBgColour(colour.White)
}
// Handle mouse click on a create realtime client button.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
if connections[id] == nil {
if err := createRealtimeClient(id); err != nil {
infoBar.SetText(err.Error())
}
infoBar.SetText(createRealtimeClientSuccess)
}
}
}
// updateCreateRestClientButton contains the update logic for each realtime client creation button.
func updateCreateRestClientButton(button *button.Button, id connectionID) {
// Handle mouseover interaction with create button while a connection does not exist.
if button.IsMouseOver() && connections[id] == nil {
button.SetBgColour(colour.ZingyGreen)
}
// if the button is not moused over and there is no connection.
if !button.IsMouseOver() && connections[id] == nil {
button.SetBgColour(colour.White)
}
// Handle mouse click on a create rest client button.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
if connections[id] == nil {
if err := createRestClient(id); err != nil {
infoBar.SetText(err.Error())
}
infoBar.SetText(createRestClientSuccess)
}
}
}
// updateCloseClientButton contains the update logic for each close client button.
// a clientLabel, presenceInfo and eventInfo are passed to this function so they
// can be reset when a client is closed.
func updateCloseClientButton(closeButton *button.Button, clientLabel *button.Button, presenceInfo *text.Text, eventInfo *text.Text, id connectionID) {
// Handle mouseover interaction with a close client button.
if closeButton.IsMouseOver() {
closeButton.SetTextColour(colour.White)
} else {
closeButton.SetTextColour(colour.Black)
}
// Handle mouse click on a close client button.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && closeButton.IsMouseOver() {
// If a realtime client is being closed.
if isRealtimeClient(id) {
closeRealtimeClient(id)
infoBar.SetText(closeRealtimeClientSuccess)
}
// If a rest client is being closed.
if isRestClient(id) {
closeRestClient(id)
infoBar.SetText(closeRealtimeClientSuccess)
}
// Reset the client label.
clientLabel.SetText("")
// Reset the presence text once a client is closed.
presenceInfo.Reset()
// Reset the eventInfo text
eventInfo.Reset()
}
}
// updateSetChannelButton contains the update logic for each set channel button.
func updateSetChannelButton(button *button.Button, channelName string, id connectionID) {
// Handle mouseover interaction with a set channel button.
if button.IsMouseOver() {
button.SetBgColour(colour.White)
} else {
button.SetBgColour(colour.ZingyGreen)
}
// Handle mouse click on set channel button.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
if isRealtimeClient(id) && connections[id].realtimeChannel == nil {
realtimeSetChannel(channelName, id)
infoBar.SetText(setRealtimeChannelSuccess)
}
if isRestClient(id) && connections[id].restChannel == nil {
restSetChannel(channelName, id)
infoBar.SetText(setRestChannelSuccess)
}
}
}
// updateGetChannelStatusButton contains the update logic for each get channel status button.
func updateGetChannelStatusButton(button *button.Button, id connectionID) {
// Handle mouseover interaction with a set channel button.
if button.IsMouseOver() {
button.SetBgColour(colour.White)
} else {
button.SetBgColour(colour.ZingyGreen)
}
// Handle mouse click on set channel button.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
// this button is currently only available to rest clients.
if isRestClient(id) && connections[id].restChannel != nil {
if err := restGetChannelStatus(id); err != nil {
infoBar.SetText(err.Error())
return
}
channelInfo := fmt.Sprintf("%s IsActive: %t, Connections: %d, PresenceConnections: %d, PresenceMembers: %d, PresenceSubscribers: %d, Publishers: %d, Subscribers: %d",
successText,
connections[id].channelDetails.Status.IsActive,
connections[id].channelDetails.Status.Occupancy.Metrics.Connections,
connections[id].channelDetails.Status.Occupancy.Metrics.PresenceConnections,
connections[id].channelDetails.Status.Occupancy.Metrics.PresenceMembers,
connections[id].channelDetails.Status.Occupancy.Metrics.PresenceSubscribers,
connections[id].channelDetails.Status.Occupancy.Metrics.Publishers,
connections[id].channelDetails.Status.Occupancy.Metrics.Subscribers,
)
infoBar.SetText(channelInfo)
}
}
}
// updateChannelPublishButton contains the update logic for each channel publish button.
func updateChannelPublishButton(button *button.Button, messageName string, messageData interface{}, id connectionID) {
// Handle mouseover interaction with a channel publish button.
if button.IsMouseOver() {
button.SetBgColour(colour.White)
} else {
button.SetBgColour(colour.JazzyPink)
}
// If this button has been clicked.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
// Publish to the realtime channel if the client is a realtime client and a channel exists.
if isRealtimeClient(id) && connections[id].realtimeChannel != nil {
if err := publishToRealtimeChannel(id, messageName, messageData); err != nil {
infoBar.SetText(err.Error())
return
}
}
// Publish to the rest channel if the client is a rest client and a channel exists.
if isRestClient(id) && connections[id].restChannel != nil {
if err := publishToRestChannel(id, messageName, messageData); err != nil {
infoBar.SetText(err.Error())
return
}
}
infoBar.SetText(publishSuccess)
}
}
// updateChannelDetachButton contains the update logic for each channel attach button.
func updateChannelDetachButton(button *button.Button, id connectionID) {
// Handle mouseover interaction with a channel attach button.
if button.IsMouseOver() {
button.SetBgColour(colour.White)
} else {
button.SetBgColour(colour.ZingyGreen)
}
// If the detach channel button has been clicked.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
// If a connection exists that has a realtime channel.
if connections[id] != nil && connections[id].realtimeChannel != nil {
err := detachChannel(id)
if err != nil {
infoBar.SetText(err.Error())
return
}
infoBar.SetText(detachChannelSuccess)
}
}
}
// updateChannelAttachButton contains the update logic for each channel attach button.
func updateChannelAttachButton(button *button.Button, id connectionID) {
// Handle mouseover interaction with a channel attach button.
if button.IsMouseOver() {
button.SetBgColour(colour.White)
} else {
button.SetBgColour(colour.ZingyGreen)
}
// If the attach channel button has been clicked.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
// if a connection exists that has a realtime channel.
if connections[id] != nil && connections[id].realtimeChannel != nil {
err := attachChannel(id)
if err != nil {
infoBar.SetText(err.Error())
return
}
infoBar.SetText(attachChannelSuccess)
}
}
}
//updateSubscribeChannelButton contains the logic to update a subscribe button.
// An event info text box is passed to this function so events that occur while
// subscribed can be drawn to the screen.
func updateSubscribeChannelButton(button *button.Button, eventInfo *text.Text, id connectionID) {
// If a connection exists and no unsubscribe function is saved
if connections[id] != nil && connections[id].unsubscribe == nil {
button.SetText(subscribeAllText)
}
// Handle mouseover interaction with subscribe all button while the channel is not subscribed.
if button.IsMouseOver() && connections[id] != nil && connections[id].unsubscribe == nil {
button.SetBgColour(colour.White)
}
// if the button is not moused over and the channel is not subscribed.
if !button.IsMouseOver() && connections[id] != nil && connections[id].unsubscribe == nil {
button.SetBgColour(colour.ZingyGreen)
}
// if the button is clicked
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
// if a channel exists and the connection has no unsubscribe function saved
if connections[id] != nil && connections[id].realtimeChannel != nil && connections[id].unsubscribe == nil {
unsubscribeAll, err := realtimeSubscribeAll(id, eventInfo)
if err != nil {
infoBar.SetText(err.Error())
return
}
// Save the unsubscribe function.
connections[id].unsubscribe = &unsubscribeAll
infoBar.SetText(subscribeAllSuccess)
// Change the SubscribeAll button into an Unsubscribe button.
button.SetBgColour(colour.BrightRed)
button.SetText(unsubscribeText)
return
}
// if there is an unsubscribe function saved
if connections[id] != nil && connections[id].unsubscribe != nil {
unsubscribe(id)
infoBar.SetText(unsubscribeSuccess)
// tear down channel unsubcribe function
connections[id].unsubscribe = nil
eventInfo.Reset()
return
}
}
}
// updateEnterPresenceButton contains the update logic for each enter presence button.
func updateEnterPresenceButton(button *button.Button, id connectionID) {
// Handle mouseover interaction with an enter presence button.
if button.IsMouseOver() {
button.SetBgColour(colour.White)
} else {
button.SetBgColour(colour.ElectricCyan)
}
// If the enter presence button is clicked.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
// and a connection exists that has a realtime channel
if connections[id] != nil && connections[id].realtimeChannel != nil {
err := enterPresence(id)
if err != nil {
infoBar.SetText(err.Error())
return
}
infoBar.SetText(enterPresenceSuccess)
}
}
}
// updateGetPresenceButton contains the update logic for each get presence button
func updateGetPresenceButton(button *button.Button, text *text.Text, id connectionID) {
// Handle mouseover interaction with a get presence button.
if button.IsMouseOver() {
button.SetBgColour(colour.White)
} else {
button.SetBgColour(colour.ElectricCyan)
}
// If the get presence button is clicked
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
// Get presence for the realtime channel if the client is a realtime client and a channel exists.
if isRealtimeClient(id) && connections[id].realtimeChannel != nil {
go getRealtimePresence(id, text)
return
}
if isRestClient(id) && connections[id].restChannel != nil {
go getRestPresence(id, text)
}
}
}
// updateLeavePresenceButton contains the update logic for each leave presence button.
func updateLeavePresenceButton(button *button.Button, id connectionID) {
// Handle mouseover interaction with a leave presence button.
if button.IsMouseOver() {
button.SetBgColour(colour.White)
} else {
button.SetBgColour(colour.ElectricCyan)
}
// If the leave presence button is clicked.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && button.IsMouseOver() {
// and a connection exists that has a realtime channel.
if connections[id] != nil && connections[id].realtimeChannel != nil {
err := leavePresence(id)
if err != nil {
infoBar.SetText(err.Error())
return
}
infoBar.SetText(leavePresenceSuccess)
}
}
}
// updateTextInputBox contains the update logic for text input text boxs
func updateTextInputBox(textBox *textbox.TextBox, id connectionID) {
// a mouse click anywhere which is not over the text box will remove focus from it.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && !textBox.IsMouseOver() {
textBox.RemoveFocus()
}
// a mouse click on the text box will set focus to it.
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) && textBox.IsMouseOver() {
textBox.SetFocus()
}
textBox.Update()
}
// isRealtimeClient is a helper function that returns true if a connection exists for an ID and it's type is realtime.
func isRealtimeClient(id connectionID) bool {
return connections[id] != nil && connections[id].connectionType != nil && *connections[id].connectionType == realtime
}
// isRestClient is a helper function that returns true if a connection exists for an ID and it's type is rest.
func isRestClient(id connectionID) bool {
return connections[id] != nil && connections[id].connectionType != nil && *connections[id].connectionType == rest
}