-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
734 lines (574 loc) · 29.2 KB
/
Copy pathutils.lua
File metadata and controls
734 lines (574 loc) · 29.2 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
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
728
729
730
731
732
733
734
--[[
The MIT License (MIT)
Copyright (c) 2020 Serkan Aksit
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Repository: Sleepy Utilities
GitHub: https://github.com/sekodev/sleepy-utilities
Version: 0.1
Author: Serkan Aksit - https://sekodev.github.io/
Twitter: https://twitter.com/sekodev
Sleepy Bug Studio
Twitter: https://twitter.com/sleepybugstudio
Facebook: https://www.facebook.com/sleepybugstudio
email: info.sleepybug@gmail.com
This file gathers functions that can be used in different projects
Using those functions in other projects may require little to no modifications
History:
1.0 - Initial release (June 2023)
####################################
Usage instructions
Import:
local utils = require "utils"
Close databases:
tableDatabases = utils.closeDatabases(tableDatabases)
Close files:
tableFiles = utils.closeDatabases(tableFiles)
Resume timers:
utils.resumeTimers(tableTimers)
Pause timers:
utils.pauseTimers(tableTimers)
Cancel timers:
tableTimers = utils.cancelTimers(tableTimers)
Clear display group:
utils.clearDisplayGroup(targetGroup)
Clear table:
targetTable = utils.clearTable(targetTable)
Create slider widget:
local widthSoundButton = contentWidthSafe / 10
local heightSoundButton = widthSoundButton
local ySoundSlider = yStartingPlacement + heightSoundButton * 1.2
local optionsSliderControl = { id = "soundLevel", filePath = "assets/menu/sound.png",
colorBackground = colorBackground, colorButtonDefault = colorButtonDefault,
colorButtonFillDefault = colorButtonFillDefault, colorButtonFillOnPress = colorButtonFillOnPress,
colorButtonStroke = colorButtonStroke, widthButton = widthSoundButton, heightButton = heightSoundButton,
yButton = ySoundSlider, soundSample = tableSoundFiles["answerChosen"] }
local buttonSound = utils.createSliderControl(targetGroup, optionsSliderControl)
Show information box:
local optionsInfoBox = {
infoFont = fontName,
infoText = "Information text shown to the player/user",
isPromptAvailable = isDontShowAgainPromptShown,
stringPromptPreference = variableStringDontShowAgainPreference,
}
local yTopFrame = utils.showInformationBox(infoGroup, optionsInfoBox)
Show dialog box:
local function closeDialogBox()
...
end
local function openURL()
...
end
local optionsDialogBox = {
fontDialog = fontName,
dialogText = "This will take you to www.github.com.",
confirmText = "Confirm - Open URL",
confirmFunction = openURL,
denyText = "Deny",
denyFunction = closeDialogBox,
}
utils.showDialogBox(dialogGroup, optionsDialogBox)
Load sound effects:
local tableSoundFiles = {}
local pathFolder = "assets/soundFX/"
local tableFileNames = { "answerChosen.wav", "answerRight.wav" }
tableSoundFiles = utils.loadSoundFX(tableSoundFiles, pathFolder, tableFileNames)
Unload/free sound effects:
tableSoundFiles = utils.unloadSoundFX(tableSoundFiles)
Show share UI(OS dependent):
local urlLandingPage = "www.YourLandingPageHere.com"
local pathShareAsset = "assets/other/shareAsset.png"
utils.showSystemShareUI(pathShareAsset, urlLandingPage)
Show QR code to a web page:
local pathQRCode = "assets/other/QRCode.png"
utils.showShareQR(qrGroup, pathQRCode)
Show mail UI(OS dependent):
local mailAddress = "user@xyz.com"
local mailSubject = "Enter mail subject here"
local mailBody = "Enter mail body here"
utils.showMailUI(mailAddress, mailSubject, mailBody)
Show store page:
local idAppStore = "1234567890"
utils.showRateUI(idAppStore)
####################################
]]--
local utils = {}
local widget = require ( "widget" )
function utils.closeDatabases(tableDatabases)
for i = #tableDatabases, 1, -1 do
if (tableDatabases[i] and tableDatabases[i]:isopen()) then
tableDatabases[i]:close()
tableDatabases[i] = nil
end
end
return tableDatabases
end
function utils.closeFiles(tableFiles)
for i = #tableFiles, 1, -1 do
if (tableFiles[i]) then
tableFiles[i]:close()
tableFiles[i] = nil
end
end
return tableFiles
end
function utils.resumeTimers(tableTimers)
for i = #tableTimers, 1, -1 do
timer.resume( tableTimers[i] )
end
end
function utils.pauseTimers(tableTimers)
for i = #tableTimers, 1, -1 do
timer.pause( tableTimers[i] )
end
end
function utils.cancelTimers(tableTimers)
for i = #tableTimers, 1, -1 do
timer.cancel( tableTimers[i] )
tableTimers[i] = nil
end
return tableTimers
end
function utils.clearDisplayGroup(targetGroup)
for i = targetGroup.numChildren, 1, -1 do
display.remove( targetGroup[i] )
targetGroup[i] = nil
end
end
function utils.clearTable(targetTable)
for i = #targetTable, 1, -1 do
display.remove(targetTable[i])
targetTable[i] = nil
end
targetTable = {}
return targetTable
end
-- Adjust slider level - turn on/off or fine control
local function controlSliderLevel(event)
if (event.phase == "began") then
if (event.target.id == "sliderControl") then
display.getCurrentStage( ):setFocus( event.target )
event.target:setFillColor( unpack(event.target.colorFillOnPress) )
elseif (event.target.id == "switchOnOff") then
event.target.nodeControl.x = event.target.nodeControl.line.x
-- Quick on/off
if (event.target.nodeControl.levelCurrent <= 0) then
event.target.nodeControl.levelCurrent = event.target.nodeControl.levelBeforeMute
else
event.target.nodeControl.levelBeforeMute = event.target.nodeControl.levelCurrent
event.target.nodeControl.levelCurrent = 0
end
event.target.nodeControl.x = event.target.nodeControl.line.x + (event.target.nodeControl.line.width * event.target.nodeControl.levelCurrent)
local idVariable = event.target.idVariable
if (idVariable == "soundLevel") then
for i = 2, audio.totalChannels do
audio.setVolume( event.target.nodeControl.levelCurrent, {channel = i} )
end
if (not audio.isChannelPlaying( 2 )) then
audio.play( event.target.soundSample, {channel = 2} )
end
elseif (idVariable == "musicLevel") then
audio.setVolume( event.target.nodeControl.levelCurrent, {channel = channelBackgroundMusic} )
end
composer.setVariable( idVariable, event.target.nodeControl.levelCurrent )
end
elseif (event.phase == "moved") then
if (event.target.id == "sliderControl") then
if (event.x >= event.target.line.x and event.x <= event.target.line.x + event.target.line.width) then
event.target.x = event.x
event.target.levelBeforeMute = event.target.levelCurrent
event.target.levelCurrent = (event.target.x - event.target.line.x) / event.target.line.width
if (event.target.levelCurrent < 0.05) then
event.target.levelCurrent = 0
end
local idVariable = event.target.idVariable
if (idVariable == "soundLevel") then
-- Only channels 2 and 3 are actively used in this scene
-- We will set volume for other channels in "ended" phase
audio.setVolume( event.target.levelCurrent, {channel = 2} )
audio.setVolume( event.target.levelCurrent, {channel = 3} )
if (not audio.isChannelPlaying( 2 )) then
audio.play( event.target.soundSample, {channel = 2} )
end
elseif (idVariable == "musicLevel") then
audio.setVolume( event.target.levelCurrent, {channel = channelBackgroundMusic} )
end
end
end
elseif (event.phase == "ended") then
if (event.target.id == "sliderControl") then
display.getCurrentStage( ):setFocus( nil )
local idVariable = event.target.idVariable
if (idVariable == "soundLevel") then
for i = 2, audio.totalChannels do
audio.setVolume( event.target.levelCurrent, {channel = i} )
end
end
event.target:setFillColor( unpack(event.target.colorFillDefault) )
composer.setVariable( idVariable, event.target.levelCurrent )
end
end
return true
end
-- Create slider controls - used for SFX and music levels
function utils.createSliderControl(targetGroup, optionsSliderControl)
local idVariable = optionsSliderControl["id"]
local filePath = optionsSliderControl["filePath"]
local widthButton = optionsSliderControl["widthButton"]
local heightButton = optionsSliderControl["heightButton"]
local yButton = optionsSliderControl["yButton"]
local soundSample = optionsSliderControl["soundSample"]
local colorBackground = optionsSliderControl["colorBackground"]
local colorButtonDefault = optionsSliderControl["colorButtonDefault"]
local colorButtonFillDefault = optionsSliderControl["colorButtonFillDefault"]
local colorButtonFillOnPress = optionsSliderControl["colorButtonFillOnPress"]
local colorButtonStroke = optionsSliderControl["colorButtonStroke"]
local xDistanceSides = contentWidthSafe / 10
local imageButton
local typeSlider, yStartingPlacement
if (optionsSliderControl["typeSlider"]) then
typeSlider = optionsSliderControl["typeSlider"]
yStartingPlacement = optionsSliderControl["yStartingPlacement"]
end
imageButton = display.newImageRect( targetGroup, filePath, widthButton, heightButton )
imageButton:setFillColor( unpack(colorButtonDefault) )
imageButton.id = "switchOnOff"
imageButton.idVariable = idVariable
imageButton.soundSample = soundSample
imageButton.anchorX = 0
imageButton.x = xDistanceSides
imageButton.y = yButton
imageButton:addEventListener( "touch", controlSliderLevel )
local widthLineSlider = contentWidthSafe - xDistanceSides * 2 - imageButton.width * 1.5
local heightLineSlider = imageButton.height / 12
local xLineSlider = imageButton.x + imageButton.width * 1.5
local yLineSlider = imageButton.y
imageButton.nodeControl = display.newCircle( targetGroup, xLineSlider + widthLineSlider / 2, yLineSlider, imageButton.height / 4 )
imageButton.nodeControl:setStrokeColor( unpack(colorButtonStroke) )
imageButton.nodeControl.strokeWidth = 10
imageButton.nodeControl.colorFillDefault = colorButtonFillDefault
imageButton.nodeControl.colorFillOnPress = colorButtonFillOnPress
imageButton.nodeControl:setFillColor( unpack(imageButton.nodeControl.colorFillDefault) )
imageButton.nodeControl.id = "sliderControl"
imageButton.nodeControl.idVariable = idVariable
imageButton.nodeControl.levelCurrent = composer.getVariable(idVariable)
imageButton.nodeControl.levelBeforeMute = imageButton.nodeControl.levelCurrent -- Used to keep last sound level before mute is pressed
imageButton.nodeControl.soundSample = soundSample
imageButton.nodeControl:addEventListener( "touch", controlSliderLevel )
imageButton.nodeControl.line = display.newRect( targetGroup, 0, imageButton.nodeControl.y, 0, heightLineSlider )
imageButton.nodeControl.line:setFillColor( unpack(colorButtonDefault) )
imageButton.nodeControl.line.anchorX = 0
imageButton.nodeControl.line.x = xLineSlider
imageButton.nodeControl.line.width = widthLineSlider
imageButton.nodeControl:toFront( )
imageButton.nodeControl.x = imageButton.nodeControl.line.x + (imageButton.nodeControl.line.width * imageButton.nodeControl.levelCurrent)
if (typeSlider == "clickOpen") then
imageButton.background = display.newRect( targetGroup, display.contentCenterX, 0, contentWidth, 0 )
imageButton.background:setFillColor( unpack(colorBackground) )
imageButton.background:toBack( )
imageButton.background:addEventListener( "touch", function () return true end )
local position = optionsSliderControl["position"]
if (position == "top") then
imageButton.background.height = (imageButton.y + imageButton.height) - yStartingPlacement
imageButton.background.y = yStartingPlacement + imageButton.background.height / 2
elseif (position == "bottom") then
-- Determine the y position for clickOpen type menu's top
local yTopMenu = imageButton.y - imageButton.height
imageButton.background.height = yStartingPlacement - yTopMenu
imageButton.background.y = yTopMenu + imageButton.background.height / 2
end
end
-- Returns the image object of the game setting/option, which is also a button.
-- For example: if you're using this to control sound level,
-- you will want to get the sound image("filePath") shown to player
return imageButton
end
local function handleInfoBoxTouch(event)
if (event.phase == "ended" or "cancelled" == event.phase) then
if (event.target.id == "closeInfoBox") then
utils.clearDisplayGroup(event.target.refGroup)
elseif (event.target.id == "hideInfoForever") then
-- If player chose note to see the warning about lock system, don't show again
if (event.target.markerPrompt.isActivated) then
event.target.markerPrompt.isActivated = false
event.target.markerPrompt.alpha = 0
local stringPromptPreference = event.target.markerPrompt.stringPromptPreference
composer.setVariable( stringPromptPreference, true )
savePreferences()
else
event.target.markerPrompt.isActivated = true
event.target.markerPrompt.alpha = 1
local stringPromptPreference = event.target.markerPrompt.stringPromptPreference
composer.setVariable( stringPromptPreference, false )
savePreferences()
end
end
end
return true
end
-- Create and show dialog box that is used to display information
function utils.showInformationBox(infoGroup, optionsInfoBox)
local infoFont = optionsInfoBox["infoFont"]
local infoText = optionsInfoBox["infoText"]
local isPromptAvailable = optionsInfoBox["isPromptAvailable"] -- This is used to determine if you want to show "Don't show again" prompt
local stringPromptPreference = optionsInfoBox["stringPromptPreference"] -- String value of the Composer variable where you keep track of users' "Don't show again" preference
infoGroup.alpha = 0
local backgroundShade = display.newRect( infoGroup, display.contentCenterX, display.contentCenterY, contentWidth, contentHeight )
backgroundShade:setFillColor( unpack(themeData.colorBackground) )
backgroundShade.alpha = .9
backgroundShade.id = "backgroundShade"
backgroundShade:addEventListener( "touch", function () return true end )
local frameInformation = display.newRect( infoGroup, display.contentCenterX, display.contentCenterY, contentWidthSafe / 1.1, 0 )
frameInformation:setFillColor( unpack(themeData.colorBackgroundPopup) )
local widthButton = frameInformation.width / 1.1
local heightButton = contentHeightSafe / 10
local yDistanceElements = heightButton / 2
local fontSizeInformation = contentHeightSafe / 30
local colorButtonFillDefault = themeData.colorButtonFillDefault
local colorButtonFillOver = themeData.colorButtonFillOver
local colorButtonDefault = themeData.colorButtonDefault
local colorButtonOver = themeData.colorButtonOver
local colorTextDefault = themeData.colorTextDefault
local colorTextOver = themeData.colorTextOver
local colorButtonStroke = themeData.colorButtonStroke
local cornerRadiusButtons = themeData.cornerRadiusButtons
local strokeWidthButtons = themeData.strokeWidthButtons
local optionsLockInformation = { text = infoText, font = infoFont, fontSize = fontSizeInformation,
width = widthButton, height = 0, align = "center" }
local textLockInformation = display.newText( optionsLockInformation )
textLockInformation:setFillColor( unpack(themeData.colorBackground) )
textLockInformation.x = display.contentCenterX
infoGroup:insert(textLockInformation)
local optionsButtonBack =
{
shape = "rect",
fillColor = { default = colorButtonFillOver, over = colorButtonFillOver },
width = frameInformation.width / 6,
height = heightButton / 1.5,
label = "x",
labelColor = { default = colorButtonFillDefault, over = colorButtonOver },
font = infoFont,
fontSize = heightButton / 2,
id = "closeInfoBox",
onEvent = handleInfoBoxTouch,
}
local buttonBack = widget.newButton( optionsButtonBack )
buttonBack.anchorX = 0
buttonBack.x = frameInformation.x + frameInformation.width / 2 - buttonBack.width
infoGroup:insert( buttonBack )
-- Add a reference to display group so we can remove it in handleInfoBoxTouch()
buttonBack.refGroup = infoGroup
--isPromptAvailable = true
if (isPromptAvailable) then
local rectHideInformation = display.newRoundedRect( infoGroup, 0, 0, 1, 1, 5 )
rectHideInformation.id = "hideInfoForever"
rectHideInformation:setFillColor( unpack(themeData.colorBackgroundPopup) )
rectHideInformation.strokeWidth = 5
rectHideInformation:setStrokeColor( unpack(themeData.colorBackground) )
rectHideInformation:addEventListener( "touch", handleInfoBoxTouch )
local optionsTextNotShowAgain = { text = sozluk.getString("lockInformationHide"), font = infoFont, fontSize = fontSizeInformation / 1.1,
height = 0, align = "center" }
rectHideInformation.textNotShowAgain = display.newText( optionsTextNotShowAgain )
rectHideInformation.textNotShowAgain.id = "hideInfoForever"
rectHideInformation.textNotShowAgain:setFillColor( unpack(themeData.colorBackground) )
rectHideInformation.textNotShowAgain:addEventListener( "touch", handleInfoBoxTouch )
infoGroup:insert(rectHideInformation.textNotShowAgain)
rectHideInformation.height = rectHideInformation.textNotShowAgain.height
rectHideInformation.width = rectHideInformation.height
local optionsRectLockMarker = { text = "X", font = infoFont, fontSize = rectHideInformation.height / 1.1 }
rectHideInformation.markerPrompt = display.newText( optionsRectLockMarker )
rectHideInformation.markerPrompt.alpha = 0
rectHideInformation.markerPrompt.isActivated = false
rectHideInformation.markerPrompt:setFillColor( unpack( themeData.colorButtonFillTrue ) )
infoGroup:insert(rectHideInformation.markerPrompt)
-- Add a reference to the variable where you save "Don't show again" preference for this information box
if (stringPromptPreference) then
rectHideInformation.markerPrompt.stringPromptPreference = stringPromptPreference
end
-- Add a reference to marker lock so when player presses the text, it also makes the marker visible
rectHideInformation.textNotShowAgain.markerPrompt = rectHideInformation.markerPrompt
frameInformation.height = textLockInformation.height + rectHideInformation.textNotShowAgain.height + buttonBack.height + yDistanceElements * 2.5
frameInformation.y = display.contentCenterY + frameInformation.height / 3
buttonBack.y = frameInformation.y - frameInformation.height / 2 + buttonBack.height / 2
local xDistanceInfoElements = (frameInformation.width - (rectHideInformation.textNotShowAgain.width + rectHideInformation.width)) / 3
rectHideInformation.x = (frameInformation.x - frameInformation.width / 2) + rectHideInformation.width / 2 + xDistanceInfoElements
rectHideInformation.y = (frameInformation.y + frameInformation.height / 2) - rectHideInformation.height / 2 - yDistanceElements
rectHideInformation.markerPrompt.x = rectHideInformation.x
rectHideInformation.markerPrompt.y = rectHideInformation.y
rectHideInformation.textNotShowAgain.x = rectHideInformation.x + rectHideInformation.width / 1.2 + rectHideInformation.textNotShowAgain.width / 2
rectHideInformation.textNotShowAgain.y = rectHideInformation.y
textLockInformation.y = rectHideInformation.textNotShowAgain.y - rectHideInformation.textNotShowAgain.height / 2 - textLockInformation.height / 2 - yDistanceElements
else
frameInformation.height = textLockInformation.height + buttonBack.height + yDistanceElements * 1.5
frameInformation.y = display.contentCenterY + frameInformation.height / 3
buttonBack.y = frameInformation.y - frameInformation.height / 2 + buttonBack.height / 2
textLockInformation.y = (frameInformation.y + frameInformation.height / 2) - textLockInformation.height / 2 - yDistanceElements
end
infoGroup.alpha = 1
-- Return top y coordinate of information box so you can adjust other elements if necessary
return frameInformation.y - frameInformation.height / 2
end
-- Handle touch events for dialog box
-- Calls assigned function when corresponding option is selected
local function handleDialogTouch(event)
if (event.phase == "ended") then
event.target.methodAssigned()
end
return true
end
-- Create and show dialog box that contains a text and two options - confirm & deny
-- Confirm/Deny strings and assigned functionality will be passed on function call with optionsDialogBox
function utils.showDialogBox(dialogGroup, optionsDialogBox)
local fontDialog = optionsDialogBox["fontDialog"]
local dialogText = optionsDialogBox["dialogText"]
local confirmText = optionsDialogBox["confirmText"]
local denyText = optionsDialogBox["denyText"]
local confirmFunction = optionsDialogBox["confirmFunction"]
local denyFunction = optionsDialogBox["denyFunction"]
local backgroundShade = display.newRect( dialogGroup, display.contentCenterX, display.contentCenterY, contentWidth, contentHeight )
backgroundShade:setFillColor( unpack(themeData.colorBackground) )
backgroundShade.alpha = .9
backgroundShade.id = "backgroundShade"
backgroundShade:addEventListener( "touch", function () return true end )
local fontSizeDialogText = contentHeightSafe / 28
local frameQuestionDialog = display.newRect( dialogGroup, display.contentCenterX, display.contentCenterY, contentWidthSafe / 1.1, 0 )
frameQuestionDialog:setFillColor( unpack(themeData.colorBackgroundPopup) )
local optionsTextDialog = { text = dialogText, width = frameQuestionDialog.width / 1.1, height = 0,
align = "center", font = fontDialog, fontSize = fontSizeDialogText }
frameQuestionDialog.textLabel = display.newText( optionsTextDialog )
frameQuestionDialog.textLabel:setFillColor( unpack(themeData.colorBackground) )
frameQuestionDialog.textLabel.x = frameQuestionDialog.x
dialogGroup:insert(frameQuestionDialog.textLabel)
local widthDialogButton = frameQuestionDialog.width / 1.1
local heightDialogButton = contentHeightSafe / 10
local yDistanceChoices = heightDialogButton / 5
local fontSizeChoices = fontSizeDialogText
local colorButtonFillDefault = themeData.colorButtonFillDefault
local colorButtonFillOver = themeData.colorButtonFillOver
local colorTextDefault = themeData.colorTextDefault
local colorTextOver = themeData.colorTextOver
local optionsButtonConfirm =
{
shape = "rect",
fillColor = { default = colorButtonFillDefault, over = colorButtonFillOver },
width = widthDialogButton,
height = heightDialogButton,
label = confirmText,
labelColor = { default = colorTextDefault, over = colorButtonFillDefault },
font = fontDialog,
fontSize = fontSizeChoices,
id = "confirmDialog",
onEvent = handleDialogTouch,
}
local buttonConfirm = widget.newButton( optionsButtonConfirm )
buttonConfirm.x = display.contentCenterX
dialogGroup:insert( buttonConfirm )
buttonConfirm.methodAssigned = confirmFunction
local optionsButtonDeny =
{
shape = "rect",
fillColor = { default = colorButtonFillDefault, over = colorButtonFillOver },
width = widthDialogButton,
height = heightDialogButton,
label = denyText,
labelColor = { default = colorTextDefault, over = colorButtonFillDefault },
font = fontDialog,
fontSize = fontSizeChoices,
id = "denyDialog",
onEvent = handleDialogTouch,
}
local buttonDeny = widget.newButton( optionsButtonDeny )
buttonDeny.x = display.contentCenterX
dialogGroup:insert( buttonDeny )
buttonDeny.methodAssigned = denyFunction
frameQuestionDialog.height = frameQuestionDialog.textLabel.height + buttonConfirm.height + buttonDeny.height + yDistanceChoices * 4
frameQuestionDialog.y = display.contentCenterY
frameQuestionDialog.textLabel.y = frameQuestionDialog.y - frameQuestionDialog.height / 2 + frameQuestionDialog.textLabel.height / 2 + yDistanceChoices
buttonConfirm.y = frameQuestionDialog.textLabel.y + frameQuestionDialog.textLabel.height / 2 + buttonConfirm.height / 2 + yDistanceChoices
buttonDeny.y = buttonConfirm.y + buttonConfirm.height / 2 + buttonDeny.height / 2 + yDistanceChoices
end
-- Load short, frequently used sound effects into memory
function utils.loadSoundFX(tableSoundFiles, pathFolder, tableFileNames)
-- File names come with extension in case we use different file types
for i = 1, #tableFileNames do
local nameFile = tableFileNames[i]
local keyFile = string.sub(nameFile, 1, -5) -- Strip file extension to use file name as key value
local pathFile = pathFolder .. nameFile
tableSoundFiles[keyFile] = audio.loadSound( pathFile )
end
return tableSoundFiles
end
function utils.unloadSoundFX(tableSoundFiles)
for i = 2, audio.totalChannels do
audio.stop(i)
end
for k, v in pairs ( tableSoundFiles ) do
audio.dispose( tableSoundFiles[k] )
tableSoundFiles[k] = nil
end
return tableSoundFiles
end
local function handleShareTouch(event)
if (event.phase == "ended") then
if (event.target.id == "shareCancel") then
utils.clearDisplayGroup(event.target.refGroup)
end
end
return true
end
-- Show share UI based on the operating system
function utils.showSystemShareUI(pathShareAsset, urlLandingPage)
if (system.getInfo("platform") == "ios" or system.getInfo("platform") == "macos" or system.getInfo("platform") == "tvos") then
-- Use Activity Popup for iOS
-- https://docs.coronalabs.com/plugin/CoronaProvider_native_popup_activity/index.html
else
local itemsSocial = {
image = { filename = pathShareAsset, baseDir = system.resourceDirectory },
url = { urlLandingPage }
}
native.showPopup( "social", itemsSocial )
end
end
-- Show QR code that contains the link to the game
-- Currently links to Google Play page
-- Replace QR code assets to change the link
function utils.showShareQR(shareGroup, pathQRCode)
local backgroundShade = display.newRect( shareGroup, display.contentCenterX, display.contentCenterY, contentWidth, contentHeight )
backgroundShade:setFillColor( unpack(themeData.colorBackground) )
backgroundShade.id = "shareCancel"
backgroundShade.refGroup = shareGroup
backgroundShade:addEventListener( "touch", handleShareTouch )
local widthQRCode = contentHeightSafe / 2
local heightQRCode = widthQRCode
local qrCode = display.newImageRect( shareGroup, pathQRCode, widthQRCode, heightQRCode )
qrCode.x, qrCode.y = display.contentCenterX, display.contentCenterY
end
-- Show mail UI using the operating system functionality
function utils.showMailUI(mailAddress, mailSubject, mailBody)
local mailOptions = { to = mailAddress, subject = mailSubject, body = mailBody }
native.showPopup( "mail", mailOptions )
end
-- Show rating UI depending on the operating system
-- Better approach would be to implement Scott H's Review PopUp Plugin
-- https://solar2dmarketplace.com/plugins?ReviewPopUp_tech-scotth
function utils.showRateUI(appIDiOS)
local optionsRateGame = {
iOSAppId = appIDiOS,
supportedAndroidStores = { "google", "amazon" }
}
native.showPopup( "appStore", optionsRateGame )
end
return utils