-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
2847 lines (2383 loc) · 67.1 KB
/
Copy pathmain.lua
File metadata and controls
2847 lines (2383 loc) · 67.1 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
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
local utf8 = require('utf8')
local bit = require('bit')
local picolove = require('lib.picolove')
--local inspect = require('lib.inspect')
require('class.colorflash')
require('util.fillp')
-- define global constants
CANVAS_W = 128
CANVAS_H = 128
PATTERN_SWATCH_CANVAS_W = 10
PATTERN_SWATCH_CANVAS_H = 38
PATTERN_SWATCH_W = 8
PATTERN_SWATCH_H = 8
PATTERN_SWATCH_ROW_H = 10
PATTERN_BIT_BOX_SIZE = 20
POINT_MIN_X = 0
POINT_MAX_X = 255
POINT_MIN_Y = -1
POINT_MAX_Y = 254
MAX_POLYGON_POINTS = 64
MODES = {
NORMAL = 1,
SAVE = 2,
EDIT_FILL_PATTERN = 3,
}
TOOLS = {
BG_IMAGE = 1,
CHANGE_COLOR = 2,
CHANGE_FILL_PATTERN = 3,
DRAW = 4,
MOVE = 5,
SELECT_POINT = 6,
SELECT_SHAPE = 7,
}
TOOL_NAMES = {
[TOOLS.BG_IMAGE] = 'adjust backgroud image',
[TOOLS.CHANGE_COLOR] = 'change shape color',
[TOOLS.CHANGE_FILL_PATTERN] = 'change shape fill-pattern index',
[TOOLS.DRAW] = 'draw',
[TOOLS.MOVE] = 'move',
[TOOLS.SELECT_POINT] = 'select point(s)',
[TOOLS.SELECT_SHAPE] = 'select shape(s)',
}
MAX_UNDO = 500
MAX_FILL_PATTERN_INDEX = 3
FILL_PATTERN_SCANCODES = {
['1'] = 1,
['2'] = 2,
['3'] = 3,
['4'] = 4,
['q'] = 5,
['w'] = 6,
['e'] = 7,
['r'] = 8,
['a'] = 9,
['s'] = 10,
['d'] = 11,
['f'] = 12,
['z'] = 13,
['x'] = 14,
['c'] = 15,
['v'] = 16,
}
function love.load(arg)
love.graphics.setFont(love.graphics.newFont(14))
love.keyboard.setKeyRepeat(true)
love.window.setDisplaySleepEnabled(true)
canvasMargin = 50
palette = {}
palette[0] = {0, 0, 0}
palette[1] = {29, 43, 83}
palette[2] = {126, 37, 83}
palette[3] = {0, 135, 81}
palette[4] = {171, 82, 54}
palette[5] = {95, 87, 79}
palette[6] = {194, 195, 199}
palette[7] = {255, 241, 232}
palette[8] = {255, 0, 77}
palette[9] = {255, 163, 0}
palette[10] = {255, 236, 39}
palette[11] = {0, 228, 54}
palette[12] = {41, 173, 255}
palette[13] = {131, 118, 156}
palette[14] = {255, 119, 168}
palette[15] = {255, 204, 170}
-- convert palette colors to 0-1 scale for LÖVE 11.0+
for _, p in pairs(palette) do
for c = 1, 3 do
p[c] = p[c] / 255
end
end
-- create a table to store positions of palette color boxes in the UI
paletteBox = {}
-- create a data structures for storing positions of the fill-pattern
-- selector UI
fillPatternSelector = {
boxes = { [0] = {}, [1] = {}, [2] = {}, [3] = {} },
outlineMargin = 2
}
-- create a table to hold the positions of the boxes which toggle pattern
-- bits when in Fill-Pattern Edit mode
patternBitBoxes = {}
for i = 1, 16 do
patternBitBoxes[i] = {}
end
fillPatterns = {}
for i = 1, MAX_FILL_PATTERN_INDEX do
fillPatterns[i] = create_default_fill_pattern()
end
-- make sure we get ALL the pixels
love.graphics.setDefaultFilter('nearest', 'nearest')
love.graphics.setLineStyle('rough')
-- create a canvas to draw on
canvas = love.graphics.newCanvas(CANVAS_W, CANVAS_H)
-- create a canvas for the tools overlay
toolsCanvas = love.graphics.newCanvas(CANVAS_W, CANVAS_H)
-- create a canvas for the fill-pattern swatches
fillPatternSwatchCanvas = love.graphics.newCanvas(
PATTERN_SWATCH_CANVAS_W,
PATTERN_SWATCH_CANVAS_H)
canvasOpacity = 1
bg = {
image = nil,
offset = {x = 0, y = 0},
scale = 1,
}
cursor = {
x = math.floor(CANVAS_W / 2),
y = math.floor(CANVAS_H / 2),
vx = 0,
vy = 0,
tool = TOOLS.DRAW,
color = 14,
bgColor = 10,
patternIndex = 0,
isVisible = true
}
cursor.flash = ColorFlash.new(.25, {
palette[0],
palette[5],
palette[6],
palette[7]
})
selectedShapes = {}
selectedPoints = {}
drawingPoints = {}
hoverFlash = ColorFlash.new(.1, {
{1, 1, 1, .25},
{1, 1, 1, .5},
{1, 1, 1, .75},
{1, 1, 1, .5},
{1, 1, 1, .25},
{1, 1, 1, 0},
{0, 0, 0, .25},
{0, 0, 0, .5},
{0, 0, 0, .75},
{0, 0, 0, .5},
{0, 0, 0, .25},
{1, 1, 1, 0},
})
selectionFlash = ColorFlash.new(.5, {
{1, 1, 1, .5},
{1, 1, 1, 0},
{0, 0, 0, .5},
{1, 1, 1, 0},
})
patternSelectionFlash = ColorFlash.new(.5, {
{1, 1, 1},
{0, 0, 0},
})
mode = MODES.NORMAL
mouseOnlyMode = true
currentFilename = ''
polygons = {}
undoHistory = {}
undoIndex = 1
redoStack = {}
status = {
byteCount = 0
}
find_best_canvas_scale()
-- If a command-line argument is given, treat it as a filename to load
if #arg > 0 then
load_painting(arg[1])
end
end
function optimize_unused_fill_patterns(shapes, fillPatterns)
local usedFillPatterns = {}
for _, shape in pairs(shapes) do
if shape.patternIndex > 0 then
local pattern = fillPatterns[shape.patternIndex]
if not pattern.isUsed then
pattern.isUsed = true
pattern.oldIndex = shape.patternIndex
table.insert(usedFillPatterns, pattern)
end
end
end
print('used fill-pattern count: ' .. #usedFillPatterns)
-- Sort the used patterns by current index so the order is deterministic
table.sort(usedFillPatterns, function (a, b)
return a.oldIndex < b.oldIndex
end)
if #usedFillPatterns > 0 then
local patternIndexTranslationTable = {}
for i, pattern in pairs(usedFillPatterns) do
patternIndexTranslationTable[pattern.oldIndex] = i
end
-- update shapes' pattern indexes
for _, shape in pairs(shapes) do
if shape.patternIndex > 0 then
shape.patternIndex = patternIndexTranslationTable[shape.patternIndex]
end
end
end
return usedFillPatterns
end
function get_painting_data()
local bytes = {}
local shapes = copy_table(polygons)
local fillPatterns = copy_table(fillPatterns)
local usedFillPatterns = optimize_unused_fill_patterns(shapes, fillPatterns)
-- add each shape
for _, shape in ipairs(shapes) do
assert(shape.patternIndex <= MAX_FILL_PATTERN_INDEX)
assert(#shape.points <= MAX_POLYGON_POINTS)
-- add the shape's fill-pattern index and point count
local byte1 = bit.bor(bit.lshift(shape.patternIndex, 6), #shape.points)
table.insert(bytes, byte1)
-- add the shape's color
if shape.patternIndex == 0 then
-- bgColor is not used, so write zeros (which is more compressable)
shape.bgColor = 0
end
table.insert(bytes, bit.bor(bit.lshift(shape.bgColor, 4), shape.color))
-- add each point
for j = 1, #shape.points do
local point = shape.points[j]
table.insert(bytes, point.x)
table.insert(bytes, point.y + 1) -- add 1 to y to allow -1 without sign
end
end
if #usedFillPatterns > 0 then
-- add used fill-patterns
for _, fillPattern in ipairs(usedFillPatterns) do
local patternBytes = convert_pattern_array_to_bytes(fillPattern.pattern)
local byte1 = bit.rshift(
bit.band(patternBytes, 0xff00),
8)
local byte2 = bit.band(patternBytes, 0x00ff)
table.insert(bytes, byte1)
table.insert(bytes, byte2)
end
-- add transparency bits for fill-patterns
local transparencyByte = 0
for i, fillPattern in ipairs(usedFillPatterns) do
if fillPattern.isTransparent then
local newBit = bit.rshift(0x80, i - 1)
transparencyByte = bit.bor(transparencyByte, newBit)
end
end
table.insert(bytes, transparencyByte)
end
return convert_to_hex(bytes)
end
function convert_to_hex(bytes)
local hex = ''
for i = 1, #bytes do
hex = hex .. bit.tohex(bytes[i], 2)
end
return hex
end
function convert_pattern_array_to_bytes(pattern)
local bytes = 0
for i, pbit in ipairs(pattern) do
if pbit == 1 then
local mask = bit.rshift(0x8000, i - 1)
bytes = bit.bor(bytes, mask)
end
end
return bytes
end
function convert_pattern_bytes_to_array(pattern)
local array = {}
for i = 1, 16 do
local mask = bit.rshift(0x8000, i - 1)
local b = bit.rshift(bit.band(mask, pattern), 16 - i)
table.insert(array, b)
end
return array
end
function load_painting(filename)
print('loading painting from "' .. filename .. '"')
local data = love.filesystem.read(filename)
if data then
load_painting_data(data)
set_current_filename(filename)
end
end
function set_current_filename(filename)
currentFilename = filename
-- remove all but the filename
currentFilename = currentFilename:match("([^/]+)$")
end
function save_painting(filename)
local data = get_painting_data()
-- set this to true if the directory is a symlink (in which case we have to
-- use io.open in order to support writing)
local supportSymlinkDirectory = false
if supportSymlinkDirectory then
local dir = love.filesystem.getSaveDirectory()
local fullPath = love.filesystem.getSaveDirectory() .. '/' .. filename
local f, msg = io.open(fullPath, 'w')
if f then
f:write(data)
f:close()
print('saved to ' .. fullPath)
else
love.window.showMessageBox('nooooo :(', 'ERROR SAVING: ' .. msg, 'error')
end
else
local success, msg = love.filesystem.write(filename, data)
if not success then
love.window.showMessageBox('nooooo :(', 'ERROR SAVING: ' .. msg, 'error')
end
end
end
function create_painting_reader(data)
return {
i = 1,
data = data,
get_next_byte = function(self)
local byte = ('0x' .. string.sub(self.data, self.i, self.i + 1)) + 0
self.i = self.i + 2
return byte
end,
eof = function(self)
return (self.i > #data)
end,
end_of_shapes = function(self, patternCount)
if patternCount > 0 then
return self.i == #data - (patternCount * 4) - 1
else
return self:eof()
end
end
}
end
function rtrim(s)
local n = #s
while n > 0 and s:find("^%s", n) do
n = n - 1
end
return s:sub(1, n)
end
function load_painting_data(data)
data = rtrim(data)
if #data == 0 then
love.window.showMessageBox('hey!', 'that file is empty; nothing to load!')
return
end
save_undo_state()
-- clear the existing painting
polygons = {}
selectedShapes = {}
selectedPoints = {}
local reader = create_painting_reader(data)
polygons, fillPatterns = parse_painting(reader)
-- convert patterns to editing format (i.e. array of 1s and 0s)
for i, fillPattern in pairs(fillPatterns) do
fillPattern.pattern = convert_pattern_bytes_to_array(fillPattern.pattern)
end
-- add any missing fill-patterns to make a total of 3
for i = #fillPatterns, 3 do
table.insert(fillPatterns, create_default_fill_pattern())
end
-- re-render all polygons
set_dirty_flag()
end
function parse_painting(reader)
local shapes = {}
local fillPatterns = {}
local patternCount = 0
-- read each shape
repeat
local shape = {
points = {}
}
-- read the fill-pattern index and point count
local byte1 = reader:get_next_byte()
shape.patternIndex = bit.rshift(bit.band(byte1, 0xc0), 6)
local pointCount = bit.band(byte1, 0x3f)
-- update running pattern count
patternCount = math.max(patternCount, shape.patternIndex)
assert(patternCount >= 0 and patternCount <= MAX_FILL_PATTERN_INDEX)
-- read the color
local colorByte = reader:get_next_byte()
shape.color = bit.band(colorByte, 0x0f)
shape.bgColor = bit.rshift(bit.band(colorByte, 0xf0), 4)
-- read each point
for i = 1, pointCount do
local x = reader:get_next_byte()
local y = reader:get_next_byte()
-- adjust y back to its actual value since it is saved 1 higher than its
-- actual value to allow for -1 without needing a sign bit
y = y - 1
table.insert(shape.points, {x = x, y = y})
end
table.insert(shapes, shape)
until reader:end_of_shapes(patternCount)
if patternCount > 0 then
for i = 1, patternCount do
local byte1 = reader:get_next_byte()
local byte2 = reader:get_next_byte()
local pattern = bit.bor(bit.lshift(byte1, 8), byte2)
table.insert(fillPatterns, {pattern = pattern})
end
local byte = reader:get_next_byte()
for i = 1, patternCount do
local mask = bit.rshift(0x80, i - 1)
local b = bit.band(byte, mask)
fillPatterns[i].isTransparent = (b > 0 and true or false)
end
end
return shapes, fillPatterns
end
function create_default_fill_pattern()
return {
pattern = {1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1},
isTransparent = false
}
end
function set_dirty_flag()
canvasIsDirty = true
end
function shallow_copy_table(t)
local t2 = {}
for k, v in pairs(t) do
t2[k] = v
end
return t2
end
function copy_table(t)
local t2 = {}
for k, v in pairs(t) do
if type(v) == 'table' then
t2[k] = copy_table(v)
else
t2[k] = v
end
end
return t2
end
function update_cursor()
cursor.flash:update()
if mode == MODES.NORMAL then
selectionFlash:update()
hoverFlash:update()
elseif mode == MODES.EDIT_FILL_PATTERN then
patternSelectionFlash:update()
end
local delta = .25
local friction = .80
if not mouseOnlyMode then
cursor.isVisible = true
if not cursor.fineMode and cursor.tool ~= TOOLS.MOVE and
cursor.tool ~= TOOLS.BG_IMAGE then
if love.keyboard.isDown('left') then
cursor.vx = cursor.vx - delta
end
if love.keyboard.isDown('right') then
cursor.vx = cursor.vx + delta
end
if love.keyboard.isDown('up') then
cursor.vy = cursor.vy - delta
end
if love.keyboard.isDown('down') then
cursor.vy = cursor.vy + delta
end
end
-- apply cursor velocity
cursor.x = cursor.x + cursor.vx
cursor.y = cursor.y + cursor.vy
-- apply friction
cursor.vx = cursor.vx * friction
cursor.vy = cursor.vy * friction
end
-- enforce cursor boundaries
if cursor.x < 0 then
cursor.x = 0
elseif cursor.x > CANVAS_W - 1 then
cursor.x = CANVAS_W - 1
end
if cursor.y < 0 then
cursor.y = 0
elseif cursor.y > CANVAS_H - 1 then
cursor.y = CANVAS_H - 1
end
cursor.hoveredPolygon = nil
cursor.hoveredPoint = nil
cursor.isQuickSelecting = false
if cursor.isVisible then
if cursor.tool ~= TOOLS.SELECT_SHAPE and cursor.tool ~= TOOLS.SELECT_POINT
and shift_is_down() then
cursor.isQuickSelecting = true
end
if cursor.tool == TOOLS.SELECT_SHAPE or cursor.isQuickSelecting then
-- find the top polygon under the cursor
local topPoly = find_top_poly(cursor)
if topPoly then
cursor.hoveredPolygon = topPoly
end
elseif cursor.tool == TOOLS.SELECT_POINT then
-- find the top point under the cursor
local point, poly = find_nearest_point(cursor)
if poly then
cursor.hoveredPoint = {
point = point,
poly = poly
}
cursor.hoveredPoly = poly
end
end
-- handle dragging
cursor.dragRect = nil
if cursor.dragStart and distance(cursor, cursor.dragStart) > 2 then
if cursor.tool == TOOLS.SELECT_SHAPE or cursor.isQuickSelecting then
cursor.dragRect = {
x1 = math.min(cursor.dragStart.x, cursor.x),
y1 = math.min(cursor.dragStart.y, cursor.y),
x2 = math.max(cursor.dragStart.x, cursor.x),
y2 = math.max(cursor.dragStart.y, cursor.y)
}
elseif cursor.tool == TOOLS.SELECT_POINT and #selectedPoints == 1 then
move_selected_point(selectedPoints[1], cursor)
end
end
if cursor.dragRect then
local targetShapes = find_shapes_in_rect(cursor.dragRect)
if shift_is_down() and not cursor.isQuickSelecting then
for _, shape in ipairs(targetShapes) do
add_shape_to_selection(shape)
end
else
set_selected_shapes(targetShapes)
end
end
end
end
-- returns all shapes that overlap with the given rect
function find_shapes_in_rect(rect)
local foundShapes = {}
for _, shape in ipairs(polygons) do
for _, p in pairs(shape.points) do
if point_rect_overlap(p, rect) then
table.insert(foundShapes, shape)
break
end
end
end
return foundShapes
end
function point_rect_overlap(point, rect)
if point.x >= rect.x1 and point.x <= rect.x2 and
point.y >= rect.y1 and point.y <= rect.y2 then
return true
end
end
function point_on_line(point, a, b)
points = picolove.line(a.x, a.y, b.x, b.y)
for _, p in pairs(points) do
local x, y = math.floor(p[1]), math.floor(p[2])
if points_are_equal(point, {x = x, y = y}) then
return true
end
end
end
function points_are_equal(a, b)
if a.x == b.x and a.y == b.y then
return true
end
end
function find_top_poly(point)
for i = #polygons, 1, -1 do
local poly = polygons[i]
if #poly.points == 1 then
if points_are_equal(point, poly.points[1]) then
return poly
end
elseif #poly.points == 2 then
if point_on_line(point, poly.points[1], poly.points[2]) then
return poly
end
else
if point_in_polygon(point, poly) then
return poly
end
end
end
end
function distance(a, b)
return math.sqrt(((a.x - b.x) ^ 2) + (a.y - b.y) ^ 2)
end
function manhattan_distance(a, b)
return math.abs(a.x - b.x) + math.abs(b.y - a.y)
end
function find_nearest_point(cursorPos)
local maxDist = 5
local closest
for i, poly in pairs(polygons) do
for j, point in pairs(poly.points) do
local dist = manhattan_distance(cursorPos, point)
if dist <= maxDist then
if closest == nil or dist < closest.dist then
closest = {
dist = dist,
point = point,
poly = poly
}
end
end
end
end
if closest then
return closest.point, closest.poly
end
end
function move_selected_point(selectedPoint, dest)
local point = selectedPoint.point
if point.x ~= dest.x or point.y ~= dest.y then
save_undo_state()
point.x = dest.x
point.y = dest.y
set_dirty_flag()
end
end
function move_selected_points(xDelta, yDelta)
local pointsToMove = {}
if #selectedPoints > 0 then
for _, sp in pairs(selectedPoints) do
table.insert(pointsToMove, sp.point)
end
-- if there are no specifically selected points, but there are whole polygons
-- selected, move all points in the selected polygons
elseif #selectedPoints == 0 and #selectedShapes > 0 then
for _, poly in pairs(selectedShapes) do
for _, point in pairs(poly.points) do
table.insert(pointsToMove, point)
end
end
end
if #pointsToMove > 0 then
if xDelta ~= 0 or yDelta ~= 0 then
-- make sure this movement would not result in any values outside the
-- range which can be saved
for _, point in pairs(pointsToMove) do
if point.x + xDelta < POINT_MIN_X or point.x + xDelta > POINT_MAX_X or
point.y + yDelta < POINT_MIN_Y or point.y + yDelta > POINT_MAX_Y
then
return
end
end
save_undo_state()
for _, point in pairs(pointsToMove) do
point.x = point.x + xDelta
point.y = point.y + yDelta
end
set_dirty_flag()
end
end
end
function set_selected_shapes(shapes)
selectedShapes = shapes
selectedPoints = {}
selectionFlash:reset()
if #shapes > 0 then
oldSelectedShapes = shapes
end
end
function set_selected_points(pointRefs)
selectedPoints = pointRefs
selectedShapes = {}
selectionFlash:reset()
end
function choose_existing_file()
-- TODO
--local pressed = love.window.showMessageBox
--return filenames[pressed]
end
function copy(shapes)
-- make sure the shapes are in the same order as the source shapes' indicies
local indexesAndShapes = {}
for _, shape in pairs(shapes) do
local index = find_polygon_index(shape)
local copiedShape = copy_table(shape)
table.insert(indexesAndShapes, {index = index, shape = copiedShape})
end
table.sort(indexesAndShapes, function (a, b)
return a.index < b.index
end)
clipboard = {}
for _, entry in ipairs(indexesAndShapes) do
table.insert(clipboard, entry.shape)
end
print('copied ' .. #clipboard .. ' shapes')
end
-- paste so that the top-left point is at (x,y)
function paste(x, y)
-- Find the point which is farthest to the top-left
local topLeft = {x = CANVAS_W, y = CANVAS_H}
for _, shape in pairs(clipboard) do
local x1, _, y1, _ = find_bounds(shape.points)
topLeft.x = math.min(x1, topLeft.x)
topLeft.y = math.min(y1, topLeft.y)
end
-- Copy each shape in the clipboard and shift it over based on the target
-- position
local newShapes = {}
for _, shape in pairs(clipboard) do
local newShape = copy_table(shape)
for _, point in pairs(newShape.points) do
point.x = point.x - topLeft.x + x
point.y = point.y - topLeft.y + y
end
table.insert(newShapes, newShape)
table.insert(polygons, newShape)
end
set_selected_shapes(newShapes)
print('pasted ' .. #newShapes .. ' shapes')
set_dirty_flag()
end
function find_center(points)
local sum = {x = 0, y = 0}
for _, p in pairs(points) do
sum.x = sum.x + p.x
sum.y = sum.y + p.y
end
return {
x = sum.x / #points,
y = sum.y / #points
}
end
function round(n)
return math.floor(n + 0.5)
end
function scale_shapes(shapes, scale)
for _, shape in pairs(shapes) do
local center = find_center(shape.points)
for _, p in pairs(shape.points) do
p.x = round((scale * (p.x - center.x)) + center.x)
p.y = round((scale * (p.y - center.y)) + center.y)
end
end
set_dirty_flag()
end
function love.textinput(t)
if mode == MODES.SAVE then
if not currentFilename then
currentFilename = ''
end
currentFilename = currentFilename .. t
end
end
function rtrim(s)
local n = #s
while n > 0 and s:find("^%s", n) do n = n - 1 end
return s:sub(1, n)
end
function ctrl_is_down()
if love.system.getOS() == 'OS X' then
if love.keyboard.isDown('lgui', 'rgui') then
return true
end
end
return love.keyboard.isDown('lctrl', 'rctrl')
end
function get_file_extension(filename)
return filename:match("^.+(%..+)$")
end
function love.filedropped(file)
local filename = file:getFilename()
print('received dropped file "' .. filename .. '"')
local ext = get_file_extension(filename)
if ext then
ext = string.sub(ext, 2, #ext)
end
file:open('r')
local data = file:read()
file:close()
if ext == 'jpg' or ext == 'jpeg' or ext == 'png' then
print('loading dropped file as background image')
save_undo_state()
bg.image = love.graphics.newImage(
love.image.newImageData(
love.filesystem.newFileData(data, filename)
)
)
canvasOpacity = 0.5
else
print('loading dropped file as painting')
if data then
load_painting_data(data)
set_current_filename(file:getFilename())
end
end
end
function set_mouse_only_mode(enabled)
mouseOnlyMode = enabled
if mouseOnlyMode then
-- halt the cursor's current momentum
cursor.vx = 0
cursor.vy = 0
end
end
function love.keypressed(key, scancode)
local shiftIsDown = shift_is_down()
local ctrlIsDown = ctrl_is_down()
if ctrlIsDown and key == 'q' then
love.event.quit()
end
if key == 'f11' then
toggle_fullscreen()
end
if mode == MODES.SAVE then
if key == 'return' then
currentFilename = rtrim(currentFilename)
save_painting(currentFilename)
mode = MODES.NORMAL
elseif key == 'escape' then
mode = MODES.NORMAL
elseif key == 'backspace' and #currentFilename > 0 then
local byteoffset = utf8.offset(currentFilename, -1)
if byteoffset then
currentFilename = string.sub(currentFilename, 1, byteoffset - 1)
end
end
return
end
if ctrlIsDown and shiftIsDown and key == 'c' then
love.system.setClipboardText(get_painting_data())
end
-- allow certain commands during EDIT_FILL_PATTERN mode