-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
1436 lines (1359 loc) · 50.1 KB
/
main.lua
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-----------------------------------------------------------------------------------------
-- main.lua
-----------------------------------------------------------------------------------------
require "ssk2.loadSSK"
_G.ssk.init()
_W = display.contentWidth
_H = display.contentHeight
local json = require("json")
local save = require("saveexport")
local myPlugin = require("plugin.tinyfiledialogs")
local Service = require("service")
local GUI = require("GUIcontrolFunctions")
local Checkbox = require("Checkbox")
-----------------------------------------------------------------------------------------
-- Declarations and predeclarations --
-----------------------------------------------------------------------------------------
local tt = transition.to
local selectedButton = "resize"
local removeHandles, showHandles, updateHandles, updateTextColors, moveImageUp,
moveImageDown, deleteImage, selectedImage, ButtonRotate, ButtonResize,
moveImageToTop, moveImageToBottom, saveWorkspace, loadWorkspace,
updateImageListOrde, exportWorkspace, gatherImageData, clearWorkspace,
imageTouch, addImageToList, reorderImageGroup, selectResize,
selectRotate, selectPan, startPanX, startPanY, LoadFileFunction
local images, handles, resizeHandles, rotateHandles, multiSelectedImages, imageOrder, multiSelectedImages = {}, {}, {}, {}, {}, {}, {}
local isPanning, panelVisible, visible, shiftPressed, controlPressed = false, true, false, false, false
local createdImmages = 0
local canvasZoomSize = 1
local zoomFactor = 1
-- Function to get the currently selected image
local function getSelectedImage()
return selectedImage
end
local checkboxFlipX = Checkbox:new{ id = "checkboxFlipX", x = flipXText.x + 19, y = flipXText.y, parentGroup = GUI.propertiesGroup, getSelectedImage = getSelectedImage }
local checkboxFlipY = Checkbox:new{ id = "checkboxFlipY", x = flipYText.x + 19, y = flipYText.y, parentGroup = GUI.propertiesGroup, getSelectedImage = getSelectedImage }
local PropertiesXinput = GUI.createTextField(PropertiesXtext.x + 60, PropertiesXtext.y, 100, 15, GUI.propertiesGroup)
local PropertiesYinput = GUI.createTextField(PropertiesYtext.x + 60, PropertiesYtext.y, 100, 15, GUI.propertiesGroup)
local PropertiesScaleXinput = GUI.createTextField(PropertiesScaleXtext.x + 60, PropertiesScaleXtext.y, 100, 15, GUI.propertiesGroup)
local PropertiesScaleYinput = GUI.createTextField(PropertiesScaleYtext.x + 60, PropertiesScaleYtext.y, 100, 15, GUI.propertiesGroup)
local PropertiesAlphainput = GUI.createTextField(PropertiesOpacitytext.x + 60, PropertiesOpacitytext.y, 100, 15, GUI.propertiesGroup)
local PropertiesRotationinput = GUI.createTextField(PropertiesRotationtext.x + 60, PropertiesRotationtext.y, 100, 15, GUI.propertiesGroup)
local function SliderChanged(value)
if selectedImage then
selectedImage.alpha = value
PropertiesAlphainput.text = string.format("%.2f", value)
end
end
local SliderOptions = {
width = 95,
height = 3,
thumbRadius = 6,
minValue = 0,
maxValue = 1,
startValue = 1,
onChange = function(value)
SliderChanged(value)
end
}
local OpacitySlider = GUI.createSlider(SliderOptions)
OpacitySlider.x = PropertiesOpacitytext.x + 60
OpacitySlider.y = PropertiesOpacitytext.y + 20
local OpacityHighImage = display.newImage(GUI.propertiesGroup, "GFX/opacityHigh.png")
OpacityHighImage.x = OpacitySlider.x + OpacitySlider.width - 40
OpacityHighImage.y = OpacitySlider.y
OpacityHighImage.xScale = 0.2
OpacityHighImage.yScale = 0.2
local OpacityLowImage = display.newImage(GUI.propertiesGroup, "GFX/opacityLow.png")
OpacityLowImage.x = OpacitySlider.x - 60
OpacityLowImage.y = OpacitySlider.y
OpacityLowImage.xScale = 0.2
OpacityLowImage.yScale = 0.2
GUI.propertiesGroup:insert(OpacitySlider)
GUI.propertiesGroup:insert(PropertiesXinput)
GUI.propertiesGroup:insert(PropertiesYinput)
GUI.propertiesGroup:insert(PropertiesScaleXinput)
GUI.propertiesGroup:insert(PropertiesScaleYinput)
GUI.propertiesGroup:insert(PropertiesAlphainput)
GUI.propertiesGroup:insert(PropertiesRotationinput)
-- Generic Input Handler Function
local function handleInput(event, property, min, max, callback)
if event.phase == "ended" or event.phase == "submitted" then
local value = tonumber(event.target.text)
if value then
if min and max then
value = math.max(min, math.min(max, value)) -- Clamp the value between min and max
end
if selectedImage then
selectedImage[property] = value
if callback then
callback(value)
end
updateHandles()
end
end
end
end
-- Add Event Listeners with Inline Functions
PropertiesAlphainput:addEventListener("userInput", function(event)
handleInput(event, "alpha", 0, 1, function(value) OpacitySlider:setValue(value) end)
end)
PropertiesXinput:addEventListener("userInput", function(event)
handleInput(event, "x")
end)
PropertiesYinput:addEventListener("userInput", function(event)
handleInput(event, "y")
end)
PropertiesScaleXinput:addEventListener("userInput", function(event)
handleInput(event, "width")
end)
PropertiesScaleYinput:addEventListener("userInput", function(event)
handleInput(event, "height")
end)
PropertiesRotationinput:addEventListener("userInput", function(event)
handleInput(event, "rotation")
end)
local function updateParameters()
if panelVisible == false then
GUI.PropertiesPanel.xScale = 0.8
GUI.PropertiesPanel.yScale = 0.8
tt(GUI.PropertiesPanel, {xScale = 1, yScale = 1, time = 80, transition = easing.inOutBack})
tt(
GUI.propertiesGroup,
{
alpha = 1,
time = 150,
onComplete = function()
PropertiesXinput.isVisible = true
PropertiesRotationinput.isVisible = true
PropertiesAlphainput.isVisible = true
PropertiesScaleYinput.isVisible = true
PropertiesScaleXinput.isVisible = true
PropertiesYinput.isVisible = true
end
}
)
end
panelVisible = true
PropertiesXinput.text = tostring(selectedImage.x)
PropertiesYinput.text = tostring(selectedImage.y)
PropertiesScaleXinput.text = tostring(selectedImage.width)
PropertiesScaleYinput.text = tostring(selectedImage.height)
PropertiesAlphainput.text = tostring(selectedImage.alpha)
PropertiesRotationinput.text = tostring(selectedImage.rotation)
OpacitySlider.alpha = 1
OpacitySlider:setValue(selectedImage.alpha)
if selectedImage.xScale == -1 then
checkboxFlipX:setCheckedState(true)
else
checkboxFlipX:setCheckedState(false)
end
if selectedImage.yScale == -1 then
checkboxFlipY:setCheckedState(true)
else
checkboxFlipY:setCheckedState(false)
end
end
local function makePanelInvisible()
PropertiesXinput.isVisible = false
PropertiesRotationinput.isVisible = false
PropertiesAlphainput.isVisible = false
PropertiesScaleYinput.isVisible = false
PropertiesScaleXinput.isVisible = false
PropertiesYinput.isVisible = false
GUI.propertiesGroup.alpha = 0
PropertiesXinput.text = ""
PropertiesYinput.text = ""
PropertiesScaleXinput.text = ""
PropertiesScaleYinput.text = ""
PropertiesAlphainput.text = ""
OpacitySlider.alpha = 0.1
PropertiesRotationinput.text = ""
checkboxFlipX:setCheckedState(false)
checkboxFlipY:setCheckedState(false)
panelVisible = false
end
local function clearParameters()
if panelVisible == true then
PropertiesXinput.isVisible = false
PropertiesRotationinput.isVisible = false
PropertiesAlphainput.isVisible = false
PropertiesScaleYinput.isVisible = false
PropertiesScaleXinput.isVisible = false
PropertiesYinput.isVisible = false
tt(
GUI.propertiesGroup,
{
alpha = 0,
time = 150,
onComplete = function()
end
}
)
end
PropertiesXinput.text = ""
PropertiesYinput.text = ""
PropertiesScaleXinput.text = ""
PropertiesScaleYinput.text = ""
PropertiesAlphainput.text = ""
OpacitySlider.alpha = 0.1
PropertiesRotationinput.text = ""
panelVisible = false
end
local function onButtonUpTouch(event)
local self = event.target
local InitialScaleX = self.InitialScaleX
local InitialScaleY = self.InitialScaleY
if event.phase == "began" then
display.getCurrentStage():setFocus(self, event.id)
self.xScale = InitialScaleX - 0.05
self.yScale = InitialScaleY - 0.05
self.isFocus = true
elseif self.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
self.xScale = InitialScaleX
self.yScale = InitialScaleY
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false
if selectedImage then
moveImageUp(selectedImage.ID)
end
end
end
return true
end
local function onButtonToBottomTouch(event)
local self = event.target
local InitialScaleX = self.InitialScaleX
local InitialScaleY = self.InitialScaleY
if event.phase == "began" then
display.getCurrentStage():setFocus(self, event.id)
self.xScale = InitialScaleX - 0.05
self.yScale = InitialScaleY - 0.05
self.isFocus = true
elseif self.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
self.xScale = InitialScaleX
self.yScale = InitialScaleY
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false
if selectedImage then
moveImageToBottom(selectedImage.ID)
end
end
end
return true
end
local function onButtonToTopTouch(event)
local self = event.target
local InitialScaleX = self.InitialScaleX
local InitialScaleY = self.InitialScaleY
if event.phase == "began" then
display.getCurrentStage():setFocus(self, event.id)
self.xScale = InitialScaleX - 0.05
self.yScale = InitialScaleY - 0.05
self.isFocus = true
elseif self.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
self.xScale = InitialScaleX
self.yScale = InitialScaleY
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false
if selectedImage then
moveImageToTop(selectedImage.ID)
end
end
end
return true
end
local function onButtonDownTouch(event)
local self = event.target
local InitialScaleX = self.InitialScaleX
local InitialScaleY = self.InitialScaleY
if event.phase == "began" then
display.getCurrentStage():setFocus(self, event.id)
self.xScale = InitialScaleX - 0.05
self.yScale = InitialScaleY - 0.05
self.isFocus = true
elseif self.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
self.xScale = InitialScaleX
self.yScale = InitialScaleY
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false
if selectedImage then
moveImageDown(selectedImage.ID)
end
end
end
return true
end
local function onButtonExportTouch(event)
local self = event.target
local InitialScaleX = self.InitialScaleX
local InitialScaleY = self.InitialScaleY
if event.phase == "began" then
display.getCurrentStage():setFocus(self, event.id)
self.xScale = InitialScaleX - 0.05
self.yScale = InitialScaleY - 0.05
self.isFocus = true
elseif self.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
self.xScale = InitialScaleX
self.yScale = InitialScaleY
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false
save.exportWorkspace(gatherImageData)
end
end
return true
end
local function onButtonResizeTouch(event)
if event.phase == "ended" then
selectResize()
end
return true
end
local function onButtonRotateTouch(event)
if event.phase == "ended" then
selectRotate()
end
return true
end
local function onButtonPanTouch(event)
if event.phase == "ended" then
selectPan()
end
return true
end
local function updateHandlesForm()
if selectedImage then
removeHandles()
showHandles()
end
end
local function onButtonSaveTouch(event)
local self = event.target
local InitialScaleX = self.InitialScaleX
local InitialScaleY = self.InitialScaleY
if event.phase == "began" then
display.getCurrentStage():setFocus(self, event.id)
self.xScale = InitialScaleX - 0.05
self.yScale = InitialScaleY - 0.05
self.isFocus = true
elseif self.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
self.xScale = InitialScaleX
self.yScale = InitialScaleY
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false
timer.performWithDelay(100, save.saveWorkspace(gatherImageData))
end
end
return true
end
local function onButtonLoadTouch(event)
local self = event.target
local InitialScaleX = self.InitialScaleX
local InitialScaleY = self.InitialScaleY
if event.phase == "began" then
display.getCurrentStage():setFocus(self, event.id)
self.xScale = InitialScaleX - 0.05
self.yScale = InitialScaleY - 0.05
self.isFocus = true
elseif self.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
self.xScale = InitialScaleX
self.yScale = InitialScaleY
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false
timer.performWithDelay(100, loadWorkspace)
end
end
return true
end
local function initializeImageOrder()
imageOrder = {}
for i, img in ipairs(images) do
table.insert(imageOrder, img.ID)
end
end
local function nextStep(FileToProcessPath)
local uniqueID = os.time() + math.random(1, 1000) -- Ensure a more unique ID
createdImmages = createdImmages + 1
local newImage = display.newImage(Service.get_file_name(FileToProcessPath), system.TemporaryDirectory)
newImage.pathToSave = FileToProcessPath
newImage.x = _W / 2
newImage.y = _H / 2
newImage.ID = uniqueID -- Unique internal ID
newImage.name = Service.get_file_name_no_extension(FileToProcessPath) -- Name for display purposes
newImage:addEventListener("touch", imageTouch)
GUI.imageGroup:insert(newImage) -- Add the new image to the GUI.imageGroup
table.insert(images, newImage)
-- Add the new image to the list
addImageToList(newImage.ID)
-- Select the new image and update text colors
if selectedImage then
removeHandles()
end
selectedImage = newImage
showHandles()
updateHandles()
updateTextColors() -- Update text colors
-- Initialize the image order table
initializeImageOrder()
end
local function LoadFileFN()
local opts = {
title = "Choose image(s) (PNG) to process",
filter_patterns = "*.png",
filter_description = "PNG FILES",
allow_multiple_selects = true -- Allow multiple file selections
}
local FileToProcessPaths = myPlugin.openFileDialog(opts)
if FileToProcessPaths then
for _, FileToProcessPath in ipairs(FileToProcessPaths) do
print("path is " .. FileToProcessPath)
Service.copyFileToSB(
Service.get_file_name(FileToProcessPath),
Service.getPath(FileToProcessPath),
Service.get_file_name(FileToProcessPath),
system.TemporaryDirectory,
true
)
nextStep(FileToProcessPath)
end
end
end
local function LoadFileFunction(event)
local self = event.target
if event.phase == "began" then
display.getCurrentStage():setFocus(self, event.id)
self.xScale = self.currentXScale - 0.05
self.yScale = self.currentYScale - 0.05
self.isFocus = true
elseif self.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
self.xScale = self.currentXScale
self.yScale = self.currentYScale
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false
LoadFileFN()
end
end
return true
end
local function onZoomInFunction(event)
local self = event.target
if event.phase == "began" then
display.getCurrentStage():setFocus(self, event.id)
self.xScale = self.currentXScale - 0.05
self.yScale = self.currentYScale - 0.05
self.isFocus = true
-- Update zoom factor and scale imageGroup
local scaleFactor = 1.1
zoomFactor = zoomFactor * scaleFactor
local centerX, centerY = _W / 2, _H / 2
local dx = (centerX - GUI.imageGroup.x) * (scaleFactor - 1)
local dy = (centerY - GUI.imageGroup.y) * (scaleFactor - 1)
GUI.imageGroup:scale(scaleFactor, scaleFactor)
GUI.imageGroup.x = GUI.imageGroup.x - dx
GUI.imageGroup.y = GUI.imageGroup.y - dy
-- Update handles
updateHandles()
elseif self.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
self.xScale = self.currentXScale
self.yScale = self.currentYScale
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false
end
end
return true
end
local function onZoomOutFunction(event)
local self = event.target
if event.phase == "began" then
display.getCurrentStage():setFocus(self, event.id)
self.xScale = self.currentXScale - 0.05
self.yScale = self.currentYScale - 0.05
self.isFocus = true
-- Update zoom factor and scale imageGroup
local scaleFactor = 0.9
zoomFactor = zoomFactor * scaleFactor
local centerX, centerY = _W / 2, _H / 2
local dx = (centerX - GUI.imageGroup.x) * (1 - scaleFactor)
local dy = (centerY - GUI.imageGroup.y) * (1 - scaleFactor)
GUI.imageGroup:scale(scaleFactor, scaleFactor)
GUI.imageGroup.x = GUI.imageGroup.x + dx
GUI.imageGroup.y = GUI.imageGroup.y + dy
-- Update handles
updateHandles()
elseif self.isFocus then
if event.phase == "ended" or event.phase == "cancelled" then
self.xScale = self.currentXScale
self.yScale = self.currentYScale
display.getCurrentStage():setFocus(self, nil)
self.isFocus = false
end
end
return true
end
local function createButton(imagePath, xScale, yScale, x, y, touchListener)
local button = display.newImage(imagePath)
button.xScale = xScale
button.yScale = yScale
button.InitialScaleX = xScale
button.InitialScaleY = yScale
button.x = x
button.y = y
button:addEventListener("touch", touchListener)
GUI.uiGroup:insert(button)
return button
end
-- Create Buttons using the Factory Function
local ButtonSave = createButton("GFX/save.png", 0.3, 0.3, 20, 20, onButtonSaveTouch)
local ButtonLoad = createButton("GFX/load.png", 0.3, 0.3, 53, 20, onButtonLoadTouch)
local ButtonExport = createButton("GFX/export.png", 0.3, 0.3, 86, 20, onButtonExportTouch)
local ButtonResize = createButton("GFX/select.png", 0.3, 0.3, _W / 2 - 30, 20, onButtonResizeTouch)
local ButtonRotate = createButton("GFX/rotate.png", 0.3, 0.3, _W / 2 + 3, 20, onButtonRotateTouch)
local ButtonZoomIn = createButton("GFX/zoomin.png", 0.3, 0.3, _W / 2 + 45, 20, onZoomInFunction)
local ButtonZoomOut = createButton("GFX/zoomout.png", 0.3, 0.3, _W / 2 + 77, 20, onZoomOutFunction)
local ButtonPan = createButton("GFX/pan.png", 0.3, 0.3, _W / 2 + 119, 20, onButtonPanTouch)
local ButtonToTop = createButton("GFX/totop.png", 0.3, 0.3, _W - 285, 20, onButtonToTopTouch)
local ButtonDown = createButton("GFX/up_arrow.png", 0.3, 0.3, _W - 252, 20, onButtonDownTouch)
local ButtonUp = createButton("GFX/down_arrow.png", 0.3, 0.3, _W - 219, 20, onButtonUpTouch)
local ButtonToBottom = createButton("GFX/tobottom.png", 0.3, 0.3, _W - 186, 20, onButtonToBottomTouch)
local ButtonAddNew = createButton("GFX/addnew.png", 0.3, 0.3, _W - 285, _H - 20, LoadFileFunction)
-- Set initial tint for buttons
GUI.setButtonTint(ButtonResize, true)
GUI.setButtonTint(ButtonRotate, false)
GUI.setButtonTint(ButtonUp, false)
GUI.setButtonTint(ButtonDown, false)
selectResize = function()
if selectedButton == "resize" then
selectedButton = nil
GUI.setButtonTint(ButtonResize, false)
else
selectedButton = "resize"
GUI.setButtonTint(ButtonResize, true)
GUI.setButtonTint(ButtonPan, false)
GUI.setButtonTint(ButtonRotate, false)
updateHandlesForm()
updateHandles()
end
end
selectRotate = function()
if selectedButton == "rotate" then
selectedButton = nil
GUI.setButtonTint(ButtonRotate, false)
else
selectedButton = "rotate"
GUI.setButtonTint(ButtonRotate, true)
GUI.setButtonTint(ButtonPan, false)
GUI.setButtonTint(ButtonResize, false)
updateHandlesForm()
updateHandles()
end
end
selectPan = function()
if selectedButton == "rotate" then
selectedButton = nil
GUI.setButtonTint(ButtonPan, false)
else
selectedButton = "pan"
GUI.setButtonTint(ButtonRotate, false)
GUI.setButtonTint(ButtonPan, true)
GUI.setButtonTint(ButtonResize, false)
updateHandlesForm()
updateHandles()
end
end
-- Function to create handles for resizing, rotating, or quadrilateral distortion
local function createHandle(x, y)
local handle
if selectedButton == "rotate" then
handle = display.newCircle(x, y, 5)
handle:setFillColor(1, 0, 0, 0.7)
else
handle = display.newRect(x, y, 10, 10)
handle:setFillColor(0, 1, 0, 0.7)
end
GUI.handleGroup:insert(handle)
handle:toFront()
return handle
end
-- Function to update handle positions based on the image size, position, and rotation
updateHandles = function()
if selectedImage then
local halfWidth = selectedImage.width / 2
local halfHeight = selectedImage.height / 2
local cosRot = math.cos(math.rad(selectedImage.rotation))
local sinRot = math.sin(math.rad(selectedImage.rotation))
local function getRotatedPosition(x, y)
return {
x = (selectedImage.x + (x * cosRot - y * sinRot)) * zoomFactor + GUI.imageGroup.x,
y = (selectedImage.y + (x * sinRot + y * cosRot)) * zoomFactor + GUI.imageGroup.y
}
end
if selectedButton == "resize" then
local topLeft = getRotatedPosition(-halfWidth, -halfHeight)
local topRight = getRotatedPosition(halfWidth, -halfHeight)
local bottomLeft = getRotatedPosition(-halfWidth, halfHeight)
local bottomRight = getRotatedPosition(halfWidth, halfHeight)
resizeHandles.topLeft.x, resizeHandles.topLeft.y = topLeft.x, topLeft.y
resizeHandles.topRight.x, resizeHandles.topRight.y = topRight.x, topRight.y
resizeHandles.bottomLeft.x, resizeHandles.bottomLeft.y = bottomLeft.x, bottomLeft.y
resizeHandles.bottomRight.x, resizeHandles.bottomRight.y = bottomRight.x, bottomRight.y
elseif selectedButton == "rotate" then
local topLeft = getRotatedPosition(-halfWidth, -halfHeight)
local topRight = getRotatedPosition(halfWidth, -halfHeight)
local bottomLeft = getRotatedPosition(-halfWidth, halfHeight)
local bottomRight = getRotatedPosition(halfWidth, halfHeight)
rotateHandles.topLeft.x, rotateHandles.topLeft.y = topLeft.x, topLeft.y
rotateHandles.topRight.x, rotateHandles.topRight.y = topRight.x, topRight.y
rotateHandles.bottomLeft.x, rotateHandles.bottomLeft.y = bottomLeft.x, bottomLeft.y
rotateHandles.bottomRight.x, rotateHandles.bottomRight.y = bottomRight.x, bottomRight.y
end
end
end
local HandleScale = 1.8
local HandleScale = 1.8
local function handleTouch(event)
local handle = event.target
if event.phase == "began" then
display.getCurrentStage():setFocus(handle, event.id)
handle.isFocus = true
handle.startX, handle.startY = event.x, event.y
handle.startWidth, handle.startHeight = selectedImage.width, selectedImage.height
handle.startImageX, handle.startImageY = selectedImage.x, selectedImage.y
handle.startRotation = selectedImage.rotation
transition.cancel("scaleHandles")
tt(handle, {xScale = HandleScale, yScale = HandleScale, time = 150, tag = "scaleHandles"})
elseif handle.isFocus then
if event.phase == "moved" then
local dx, dy = (event.x - handle.startX) / zoomFactor, (event.y - handle.startY) / zoomFactor
if selectedButton == "resize" then
local proportion = handle.startWidth / handle.startHeight
Service.updateImageSizeAndPosition(
selectedImage,
resizeHandles,
handle,
dx,
dy,
proportion,
shiftPressed,
controlPressed,
zoomFactor
)
updateHandles()
updateParameters()
elseif selectedButton == "rotate" then
local imageCenterX, imageCenterY = selectedImage:localToContent(0, 0)
local startAngle = math.atan2(handle.startY - imageCenterY, handle.startX - imageCenterX)
local currentAngle = math.atan2(event.y - imageCenterY, event.x - imageCenterX)
local angleDelta = math.deg(currentAngle - startAngle)
selectedImage.rotation = handle.startRotation + angleDelta
updateHandles()
updateParameters()
end
elseif event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus(handle, nil)
handle.isFocus = false
--handle:scale(1 / HandleScale, 1 / HandleScale) -- Scale back the handle to its original size
transition.cancel("scaleHandles")
tt(handle, {xScale = 1, yScale = 1, time = 150, tag = "scaleHandles"})
end
end
return true
end
-- Function to add touch listeners to handles
local function addHandleListeners()
for _, handle in pairs(handles) do
handle:addEventListener("touch", handleTouch)
end
end
-- Function to remove handles from the display
removeHandles = function()
for _, handle in pairs(resizeHandles) do
handle:removeSelf()
end
resizeHandles = {}
for _, handle in pairs(rotateHandles) do
handle:removeSelf()
end
rotateHandles = {}
clearParameters()
end
-- Function to create and show handles around the selected image
showHandles = function()
if selectedImage then
if selectedButton == "resize" then
-- Create resize handles (ignoring rotation)
resizeHandles = {
topLeft = createHandle(
selectedImage.x - selectedImage.width / 2,
selectedImage.y - selectedImage.height / 2
),
topRight = createHandle(
selectedImage.x + selectedImage.width / 2,
selectedImage.y - selectedImage.height / 2
),
bottomLeft = createHandle(
selectedImage.x - selectedImage.width / 2,
selectedImage.y + selectedImage.height / 2
),
bottomRight = createHandle(
selectedImage.x + selectedImage.width / 2,
selectedImage.y + selectedImage.height / 2
)
}
-- Add touch listeners to resize handles
for _, handle in pairs(resizeHandles) do
handle:addEventListener("touch", handleTouch)
end
elseif selectedButton == "rotate" then
-- Create rotation handles
local halfWidth = selectedImage.width / 2 * zoomFactor
local halfHeight = selectedImage.height / 2 * zoomFactor
local cosRot = math.cos(math.rad(selectedImage.rotation))
local sinRot = math.sin(math.rad(selectedImage.rotation))
local function getRotatedPosition(x, y)
return {
x = selectedImage.x + (x * cosRot - y * sinRot),
y = selectedImage.y + (x * sinRot + y * cosRot)
}
end
local topLeft = getRotatedPosition(-halfWidth, -halfHeight)
local topRight = getRotatedPosition(halfWidth, -halfHeight)
local bottomLeft = getRotatedPosition(-halfWidth, halfHeight)
local bottomRight = getRotatedPosition(halfWidth, halfHeight)
rotateHandles = {
topLeft = createHandle(topLeft.x, topLeft.y),
topRight = createHandle(topRight.x, topRight.y),
bottomLeft = createHandle(bottomLeft.x, bottomLeft.y),
bottomRight = createHandle(bottomRight.x, bottomRight.y)
}
-- Set rotation for rotation handles
for _, handle in pairs(rotateHandles) do
handle.rotation = selectedImage.rotation
handle:addEventListener("touch", handleTouch)
end
end
updateParameters()
end
end
-- Touch listener for selecting an image
imageTouch = function(event)
-- Ignore touch events on images if pan mode is active
if selectedButton == "pan" then
return false
end
local image = event.target
if event.phase == "began" then
display.getCurrentStage():setFocus(image, event.id)
image.isFocus = true
-- Reset the initial positions and offsets for the touched image
image.startX, image.startY = image.x, image.y
--image.prevX, image.prevY = image.x, image.y
image.offsetX = event.x - image.x
image.offsetY = event.y - image.y
if shiftPressed then
if selectedImage and selectedImage ~= image then
-- Add outline to the current selected image and add it to multiSelectedImages
GUI.drawOutline(selectedImage)
multiSelectedImages[selectedImage] = true
removeHandles()
selectedImage = nil
end
if multiSelectedImages[image] then
-- Deselect the image if it's already selected
GUI.removeOutline(image)
multiSelectedImages[image] = nil
else
-- Select the image if it's not already selected
GUI.drawOutline(image)
multiSelectedImages[image] = true
end
else
-- If clicking on one of the multi-selected images without pressing shift, do nothing
if multiSelectedImages[image] then
-- Keep the multi-selected images as they are
return true
end
-- Existing single selection logic
if selectedImage ~= image then
removeHandles()
selectedImage = image
showHandles()
updateTextColors() -- Update text colors
end
end
if image == selectedImage then
updateHandles()
updateParameters()
end
-- Store initial positions for all selected images
for img, _ in pairs(multiSelectedImages) do
img.startX = img.x
img.startY = img.y
end
elseif image.isFocus then
if event.phase == "moved" then
local dx = (event.x - image.startX - image.offsetX) / zoomFactor
local dy = (event.y - image.startY - image.offsetY) / zoomFactor
-- Move the touched image
image.x = image.startX + dx
image.y = image.startY + dy
if image.outline then
image.outline.x = image.x
image.outline.y = image.y
end
-- Move all other selected images by the same amount
if next(multiSelectedImages) ~= nil then
for img, _ in pairs(multiSelectedImages) do
if img ~= image then -- Skip the touched image
print("image start is " .. img.startX)
img.x = img.startX + dx
img.y = img.startY + dy
print("image x is ".. img.x)
if img.outline then
img.outline.x = img.x
img.outline.y = img.y
end
end
end
end
if image == selectedImage then
updateHandles()
updateParameters()
end
elseif event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus(image, nil)
image.isFocus = false
end
end
if event.phase == "ended" then
for img, _ in pairs(multiSelectedImages) do
img.startX = img.x
img.startY = img.y
end
-- Keep the selected images intact
if not shiftPressed then
if selectedImage then
updateHandles()
updateParameters()
end
end
end
return true
end
-- Create a ScrollView for the list of images
local scrollViewHeight = _H - 83
local widget = require("widget")
local scrollView =
widget.newScrollView(
{
width = 300,
height = scrollViewHeight,
scrollWidth = 300,
scrollHeight = scrollViewHeight,
verticalScrollDisabled = false,
horizontalScrollDisabled = true,
backgroundColor = {0.9, 0.9, 1, 0.5}
}
)
scrollView.x = _W - 150 -- Adjusted the x position to center the scroll view
scrollView.y = _H / 2
GUI.uiGroup:insert(scrollView)
local function showRenamePopup(imageID, textElement)
local image = nil
for i, img in ipairs(images) do
if img.ID == imageID then
image = img
break
end
end
if not image then
return
end -- Prevent the function from continuing if the image is nil
local renameGroup = display.newGroup()
local background = display.newRoundedRect(renameGroup, _W / 2, _H / 2, 300, 200, 5)
background:setFillColor(0.8, 0.8, 0.8, 0.8)
background:addEventListener(
"touch",
function()
return true
end
)
local renameText =
display.newText(
{
parent = renameGroup,
text = "Rename Image",
x = _W / 2,
y = _H / 2 - 60,
font = native.systemFont,
fontSize = 24 * 2
}
)
renameText:setFillColor(0)
renameText.xScale = 0.5
renameText.yScale = 0.5
local nameInput = native.newTextField(_W / 2, _H / 2, 200, 40)
nameInput.text = image.name -- Set the initial text to the current image name
renameGroup:insert(nameInput)
local function onRenameComplete(event)
if event.phase == "ended" then
image.name = nameInput.text
textElement.text = nameInput.text
nameInput:removeSelf()
renameGroup:removeSelf()
end
return true
end
local renameButton =
display.newText(
{
parent = renameGroup,
text = "OK",
x = _W / 2,
y = _H / 2 + 60,
font = native.systemFont,
fontSize = 20 * 2
}
)
renameButton.xScale = 0.5
renameButton.yScale = 0.5
renameButton:setFillColor(0, 0, 1)
renameButton:addEventListener("touch", onRenameComplete)
GUI.uiGroup:insert(renameGroup)
end
local textElements = {} -- Table to store text elements
updateTextColors = function()
for _, element in pairs(textElements) do
if selectedImage and element.id == selectedImage.ID then
element.text:setFillColor(0, 0, 1) -- Blue color for selected image
elseif multiSelectedImages[element] then
element.text:setFillColor(0.5, 0.5, 0.5) -- Gray color for multi-selected images
else
element.text:setFillColor(0) -- Black color for unselected images
end
end
end
-- Function to reorder the GUI.imageGroup based on the order table
reorderImageGroup = function()
for i, imageID in ipairs(imageOrder) do
for j, img in ipairs(images) do
if img.ID == imageID then