-
Notifications
You must be signed in to change notification settings - Fork 0
/
main 2.lua
1670 lines (1566 loc) · 54.6 KB
/
main 2.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()
local json = require("json")
local save = require("saveexport")
local myPlugin = require("plugin.tinyfiledialogs")
local Service = require("service")
local GUI = require("GUIcontrolFunctions")
local tt = transition.to
-- Set up display width and height
_W = display.contentWidth
_H = display.contentHeight
-- Create display groups
local imageGroup = display.newGroup()
local handleGroup = display.newGroup()
local uiGroup = display.newGroup()
local propertiesGroup = display.newGroup()
-- Ensure the UI group is above the image group
uiGroup:toFront()
local isPanning = false
local multiSelectedImages = {}
--------------------------------------
-- Declarations and predeclarations ---
--------------------------------------
local selectedButton = "resize" -- Resize button selected by default
local removeHandles,
showHandles,
updateHandles,
updateTextColors,
moveImageUp,
moveImageDown,
deleteImage,
selectedImage,
ButtonRotate,
ButtonResize,
moveImageToTop,
moveImageToBottom,
saveWorkspace,
loadWorkspace,
updateImageListOrde,
exportWorkspace,
gatherImageData,
clearWorkspace,
imageTouch,
addImageToList,
reorderImageGroup,
selectResize,
selectRotate,
startPanX, startPanY
local images = {}
local handles = {}
local resizeHandles = {}
local rotateHandles = {}
local createdImmages = 0
local function drawOutline(image)
if not image.outline then
image.outline = display.newRect(image.parent, image.x, image.y, image.width + 2, image.height + 2)
image.outline:setFillColor(0, 0, 0, 0)
image.outline:setStrokeColor(0.5, 0.5, 0.5)
image.outline.strokeWidth = 1
image.outline:toBack()
end
end
local function removeOutline(image)
if image.outline then
image.outline:removeSelf()
image.outline = nil
end
end
--------------------------------------
--- Image Properties Panel ---
--------------------------------------
local PropertiesPanel = display.newRoundedRect(propertiesGroup, _W / 2, _H / 2, 210, 180, 5)
PropertiesPanel:setFillColor(0.8, 0.8, 0.8, 0.8)
PropertiesPanel.x = 15 + PropertiesPanel.width / 2
PropertiesPanel.y = (_H - 5) - PropertiesPanel.height / 2
PropertiesPanel:addEventListener(
"touch",
function()
return true
end
)
local TextOptions = {
parent = propertiesGroup,
text = "txt",
x = 0,
y = 0,
font = native.systemFont,
fontSize = 15 * 2
}
local function createmyText(name, text, x, y)
local myText = display.newText(TextOptions)
myText.x = x
myText.y = y
myText.text = text
myText:setFillColor(0.4)
myText.xScale = 0.5
myText.yScale = 0.5
myText.anchorX = 1
_G[name] = myText -- Store the text object in a global variable with the given name
end
local properties = {
{name = "PropertiesXtext", text = " x =", yOffset = 20},
{name = "PropertiesYtext", text = " y =", yOffset = 40},
{name = "PropertiesScaleXtext", text = "width =", yOffset = 60},
{name = "PropertiesScaleYtext", text = "height =", yOffset = 80},
{name = "PropertiesOpacitytext", text = "alpha =", yOffset = 100},
{name = "PropertiesRotationtext", text = "rotation =", yOffset = 140},
{name = "flipXText", text = "flipX", yOffset = 160},
{name = "flipYText", text = "flipY", yOffset = 160, xOffset = 145}
}
for _, prop in ipairs(properties) do
local x = prop.xOffset or PropertiesPanel.x - PropertiesPanel.width / 2 + 70
local y = PropertiesPanel.y - PropertiesPanel.height / 2 + prop.yOffset
createmyText(prop.name, prop.text, x, y)
end
-- Path to your images
local uncheckedImage = "GFX/checkOFF.png"
local checkedImage = "GFX/checkON.png"
-- Function to handle checkbox state change
local function toggleCheckbox(event)
local checkbox = event.target
if event.phase == "ended" then
if selectedImage then
checkbox:setCheckedState(not checkbox.isChecked)
end
end
return true
end
-- Method to set the checkbox state
local function setCheckedState(self, state)
self.isChecked = state
local imagePath = state and checkedImage or uncheckedImage
-- Update the display object image
self.fill = {type = "image", filename = imagePath}
if selectedImage then
-- Apply the flip logic
if state == true then
if self.id == "checkboxFlipX" then
selectedImage.xScale = -1
else
selectedImage.yScale = -1
end
else
if self.id == "checkboxFlipX" then
selectedImage.xScale = 1
else
selectedImage.yScale = 1
end
end
end
end
-- Create the initial checkboxes
local checkboxFlipX = display.newImage(propertiesGroup, uncheckedImage)
checkboxFlipX.x = flipXText.x + 19
checkboxFlipX.y = flipXText.y
checkboxFlipX:scale(0.2, 0.2)
checkboxFlipX.originalX = checkboxFlipX.x
checkboxFlipX.originalY = checkboxFlipX.y
checkboxFlipX.isChecked = false
checkboxFlipX.id = "checkboxFlipX" -- Assign an ID for identification
checkboxFlipX.setCheckedState = setCheckedState
checkboxFlipX:addEventListener("touch", toggleCheckbox)
local checkboxFlipY = display.newImage(propertiesGroup, uncheckedImage)
checkboxFlipY.x = flipYText.x + 19
checkboxFlipY.y = flipYText.y
checkboxFlipY:scale(0.2, 0.2)
checkboxFlipY.originalX = checkboxFlipY.x
checkboxFlipY.originalY = checkboxFlipY.y
checkboxFlipY.isChecked = false
checkboxFlipY.id = "checkboxFlipY" -- Assign an ID for identification
checkboxFlipY.setCheckedState = setCheckedState
checkboxFlipY:addEventListener("touch", toggleCheckbox)
local PropertiesXinput = native.newTextField(PropertiesXtext.x + 60, PropertiesXtext.y, 100, 15)
local PropertiesYinput = native.newTextField(PropertiesYtext.x + 60, PropertiesYtext.y, 100, 15)
local PropertiesScaleXinput = native.newTextField(PropertiesScaleXtext.x + 60, PropertiesScaleXtext.y, 100, 15)
local PropertiesScaleYinput = native.newTextField(PropertiesScaleYtext.x + 60, PropertiesScaleYtext.y, 100, 15)
local PropertiesAlphainput = native.newTextField(PropertiesOpacitytext.x + 60, PropertiesOpacitytext.y, 100, 15)
local PropertiesRotationinput = native.newTextField(PropertiesRotationtext.x + 60, PropertiesRotationtext.y, 100, 15)
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 = Service.createSlider(SliderOptions)
OpacitySlider.x = PropertiesOpacitytext.x + 60
OpacitySlider.y = PropertiesOpacitytext.y + 20
local OpacityHighImage = display.newImage(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(propertiesGroup, "GFX/opacityLow.png")
OpacityLowImage.x = OpacitySlider.x - 60
OpacityLowImage.y = OpacitySlider.y
OpacityLowImage.xScale = 0.2
OpacityLowImage.yScale = 0.2
propertiesGroup:insert(OpacitySlider)
propertiesGroup:insert(PropertiesXinput)
propertiesGroup:insert(PropertiesYinput)
propertiesGroup:insert(PropertiesScaleXinput)
propertiesGroup:insert(PropertiesScaleYinput)
propertiesGroup:insert(PropertiesAlphainput)
propertiesGroup:insert(PropertiesRotationinput)
local function onAlphaInput(event)
if event.phase == "ended" or event.phase == "submitted" then
local value = tonumber(event.target.text)
if value then
value = math.max(0, math.min(1, value)) -- Clamp the value between 0 and 1
if selectedImage then
selectedImage.alpha = value
OpacitySlider:setValue(value)
end
end
end
end
PropertiesAlphainput:addEventListener("userInput", onAlphaInput)
local function onXInput(event)
if event.phase == "ended" or event.phase == "submitted" then
local value = tonumber(event.target.text)
if value and selectedImage then
selectedImage.x = value
updateHandles()
end
end
end
PropertiesXinput:addEventListener("userInput", onXInput)
local function onYInput(event)
if event.phase == "ended" or event.phase == "submitted" then
local value = tonumber(event.target.text)
if value and selectedImage then
selectedImage.y = value
updateHandles()
end
end
end
PropertiesYinput:addEventListener("userInput", onYInput)
local function onWidthInput(event)
if event.phase == "ended" or event.phase == "submitted" then
local value = tonumber(event.target.text)
if value and selectedImage then
selectedImage.width = value
updateHandles()
end
end
end
PropertiesScaleXinput:addEventListener("userInput", onWidthInput)
local function onHeightInput(event)
if event.phase == "ended" or event.phase == "submitted" then
local value = tonumber(event.target.text)
if value and selectedImage then
selectedImage.height = value
updateHandles()
end
end
end
PropertiesScaleYinput:addEventListener("userInput", onHeightInput)
local function onAlphaInput(event)
if event.phase == "ended" or event.phase == "submitted" then
local value = tonumber(event.target.text)
if value then
value = math.max(0, math.min(1, value)) -- Clamp the value between 0 and 1
if selectedImage then
selectedImage.alpha = value
OpacitySlider:setValue(value)
end
end
end
end
PropertiesAlphainput:addEventListener("userInput", onAlphaInput)
local function onRotationInput(event)
if event.phase == "ended" or event.phase == "submitted" then
local value = tonumber(event.target.text)
if value and selectedImage then
selectedImage.rotation = value
updateHandles()
end
end
end
PropertiesRotationinput:addEventListener("userInput", onRotationInput)
local panelVisible = true
local function updateParameters()
if panelVisible == false then
PropertiesPanel.xScale = 0.8
PropertiesPanel.yScale = 0.8
tt(PropertiesPanel, {xScale = 1, yScale = 1, time = 80, transition = easing.inOutBack})
tt(
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 = selectedImage.x
PropertiesYinput.text = selectedImage.y
PropertiesScaleXinput.text = selectedImage.width
PropertiesScaleYinput.text = selectedImage.height
PropertiesAlphainput.text = selectedImage.alpha
OpacitySlider.alpha = 1
OpacitySlider:setValue(selectedImage.alpha)
PropertiesRotationinput.text = selectedImage.rotation
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
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
makePanelInvisible()
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(
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
-- Event listener for ButtonUp
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
onButtonExportTouch = function(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
-- Function to set button tint
local function setButtonTint(button, isSelected)
if isSelected then
button:setFillColor(1, 0.6, 0) -- Red tint
else
button:setFillColor(1, 1, 1) -- Neutral tint
end
end
-- Function to change handles when button is pressed
local function updateHandlesForm()
if selectedImage then
removeHandles()
showHandles()
end
end
-- Event listener for ButtonResize
local function onButtonResizeTouch(event)
if event.phase == "ended" then
selectResize()
end
return true
end
-- Event listener for ButtonRotate
local function onButtonRotateTouch(event)
if event.phase == "ended" then
selectRotate()
end
return true
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 onButtonPanTouch(event)
if event.phase == "ended" then
if selectedButton == "pan" then
selectedButton = nil
setButtonTint(ButtonPan, false)
else
selectedButton = "pan"
setButtonTint(ButtonPan, true)
setButtonTint(ButtonResize, false)
setButtonTint(ButtonRotate, false)
removeHandles()
end
end
return true
end
-- top buttons
ButtonSave = display.newImage("GFX/save.png")
ButtonSave.xScale = 0.3
ButtonSave.yScale = 0.3
ButtonSave.InitialScaleX = ButtonSave.xScale
ButtonSave.InitialScaleY = ButtonSave.yScale
ButtonSave.x = 20
ButtonSave.y = 20
ButtonSave:addEventListener("touch", onButtonSaveTouch)
ButtonLoad = display.newImage("GFX/load.png")
ButtonLoad.xScale = 0.3
ButtonLoad.yScale = 0.3
ButtonLoad.InitialScaleX = ButtonLoad.xScale
ButtonLoad.InitialScaleY = ButtonLoad.yScale
ButtonLoad.x = 53
ButtonLoad.y = 20
ButtonLoad:addEventListener("touch", onButtonLoadTouch)
ButtonExport = display.newImage("GFX/export.png")
ButtonExport.xScale = 0.3
ButtonExport.yScale = 0.3
ButtonExport.InitialScaleX = ButtonExport.xScale
ButtonExport.InitialScaleY = ButtonExport.yScale
ButtonExport.x = 86
ButtonExport.y = 20
ButtonExport:addEventListener("touch", onButtonExportTouch)
ButtonResize = display.newImage("GFX/resize.png")
ButtonResize.xScale = 0.3
ButtonResize.yScale = 0.3
ButtonResize.x = _W / 2 - 30
ButtonResize.y = 20
ButtonResize:addEventListener("touch", onButtonResizeTouch)
ButtonRotate = display.newImage("GFX/rotate.png")
ButtonRotate.xScale = 0.3
ButtonRotate.yScale = 0.3
ButtonRotate.x = _W / 2 + 3
ButtonRotate.y = 20
ButtonRotate:addEventListener("touch", onButtonRotateTouch)
ButtonPan = display.newImage("GFX/pan.png")
ButtonPan.xScale = 0.3
ButtonPan.yScale = 0.3
ButtonPan.x = _W / 2 + 90
ButtonPan.y = 20
ButtonPan:addEventListener("touch", onButtonPanTouch)
ButtonToTop = display.newImage("GFX/totop.png")
ButtonToTop.xScale = 0.3
ButtonToTop.yScale = 0.3
ButtonToTop.InitialScaleX = ButtonToTop.xScale
ButtonToTop.InitialScaleY = ButtonToTop.yScale
ButtonToTop.x = _W - 285
ButtonToTop.y = 20
ButtonToTop:addEventListener("touch", onButtonToTopTouch)
ButtonDown = display.newImage("GFX/up_arrow.png")
ButtonDown.xScale = 0.3
ButtonDown.yScale = 0.3
ButtonDown.InitialScaleX = ButtonDown.xScale
ButtonDown.InitialScaleY = ButtonDown.yScale
ButtonDown.x = _W - 252
ButtonDown.y = 20
ButtonDown:addEventListener("touch", onButtonDownTouch)
ButtonUp = display.newImage("GFX/down_arrow.png")
ButtonUp.xScale = 0.3
ButtonUp.yScale = 0.3
ButtonUp.InitialScaleX = ButtonUp.xScale
ButtonUp.InitialScaleY = ButtonUp.yScale
ButtonUp.x = _W - 219
ButtonUp.y = 20
ButtonUp:addEventListener("touch", onButtonUpTouch)
ButtonToBottom = display.newImage("GFX/tobottom.png")
ButtonToBottom.xScale = 0.3
ButtonToBottom.yScale = 0.3
ButtonToBottom.InitialScaleX = ButtonToBottom.xScale
ButtonToBottom.InitialScaleY = ButtonToBottom.yScale
ButtonToBottom.x = _W - 186
ButtonToBottom.y = 20
ButtonToBottom:addEventListener("touch", onButtonToBottomTouch)
ButtonAddNew = display.newImage("GFX/addnew.png")
ButtonAddNew.xScale = 0.3
ButtonAddNew.yScale = 0.3
ButtonAddNew.x = _W - 285
ButtonAddNew.y = _H - 20
-- Add other UI elements (buttons, etc.) to the uiGroup
uiGroup:insert(ButtonResize)
uiGroup:insert(ButtonRotate)
uiGroup:insert(ButtonUp)
uiGroup:insert(ButtonDown)
uiGroup:insert(ButtonResize)
uiGroup:insert(ButtonRotate)
uiGroup:insert(ButtonToTop)
uiGroup:insert(ButtonToBottom)
uiGroup:insert(ButtonExport)
uiGroup:insert(ButtonLoad)
uiGroup:insert(ButtonSave)
uiGroup:insert(ButtonPan)
-- Set initial tint for buttons
setButtonTint(ButtonResize, true)
setButtonTint(ButtonRotate, false)
setButtonTint(ButtonUp, false)
setButtonTint(ButtonDown, false)
-- Initialize visibility and movement variables
local visible = false
-- Track the state of the shift key
local multiSelectedImages = {}
local shiftPressed = false
local controlPressed = false
selectResize = function()
if selectedButton == "resize" then
selectedButton = nil
setButtonTint(ButtonResize, false)
else
selectedButton = "resize"
setButtonTint(ButtonResize, true)
setButtonTint(ButtonPan, false)
setButtonTint(ButtonRotate, false)
updateHandlesForm()
updateHandles()
end
end
selectRotate = function()
if selectedButton == "rotate" then
selectedButton = nil
setButtonTint(ButtonRotate, false)
else
selectedButton = "rotate"
setButtonTint(ButtonRotate, true)
setButtonTint(ButtonPan, false)
setButtonTint(ButtonResize, false)
updateHandlesForm()
updateHandles()
end
end
-- Key event listener to track shift key state
local function onKeyEvent(event)
if event.keyName == "leftShift" or event.keyName == "rightShift" then
if event.phase == "down" then
shiftPressed = true
elseif event.phase == "up" then
shiftPressed = false
end
end
if event.keyName == "leftControl" or event.keyName == "rightControl" then
if event.phase == "down" then
controlPressed = true
elseif event.phase == "up" then
controlPressed = false
end
end
-- Add key event handling for "s" and "r"
if event.phase == "down" then
if event.keyName == "s" then
selectResize()
elseif event.keyName == "r" then
selectRotate()
end
end
return false
end
Runtime:addEventListener("key", onKeyEvent)
-- 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
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))
-- Adjust positions based on the imageGroup's position
local groupX, groupY = imageGroup.x, imageGroup.y
local function getRotatedPosition(x, y)
return {
x = selectedImage.x + (x * cosRot - y * sinRot) + groupX,
y = selectedImage.y + (x * sinRot + y * cosRot) + groupY
}
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
for img, _ in pairs(multiSelectedImages) do
img.prevX, img.prevY = img.x, img.y
end
end
local HandleScale = 1.8
local HandleScale = 1.8
-- Touch listener for handles to resize, rotate, or distort the image
-- Touch listener for handles to resize, rotate, or distort the image
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
--handle:scale(HandleScale, HandleScale) -- Scale up the handle being dragged
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, event.y - handle.startY
if selectedButton == "resize" then
local proportion = handle.startWidth / handle.startHeight
Service.updateImageSizeAndPosition(
selectedImage,
resizeHandles,
handle,
dx,
dy,
proportion,
shiftPressed,
controlPressed
)
updateHandles()
updateParameters()
elseif selectedButton == "rotate" then
local imageCenterX, imageCenterY = selectedImage.x, selectedImage.y
local startAngle = math.atan2(handle.startY - imageCenterX, 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
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),
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
image.startX, image.startY = event.x, event.y
image.prevX, image.prevY = image.x, image.y
if shiftPressed then
if selectedImage and selectedImage ~= image then
-- Add outline to the current selected image and add it to multiSelectedImages
drawOutline(selectedImage)
multiSelectedImages[selectedImage] = true
removeHandles()
selectedImage = nil
end
if multiSelectedImages[image] then
-- Deselect the image if it's already selected