forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign.lua
More file actions
1583 lines (1419 loc) · 58.9 KB
/
design.lua
File metadata and controls
1583 lines (1419 loc) · 58.9 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
-- A GUI front-end for creating designs
--@ module = true
-- TODOS ====================
-- Refactor duplicated code into functions
-- File is getting long... might be time to consider creating additional modules
-- All the various states are getting hard to keep track of, e.g. placing extra/mirror/mark/etc...
-- Should consolidate the states into a single state attribute with enum values
-- Keyboard support
-- Grid view without slowness (can ignore if next TODO is done, since normal mining mode has grid view)
-- Lags when drawing the full screen grid on each frame render
-- Integrate with default mining mode for designation type, priority, etc... (possible?)
-- Figure out how to remove dug stairs with mode (nothing seems to work, include 'dig ramp')
-- 'No overwrite' mode to not overwrite existing designations
-- Snap to grid, or angle, like 45 degrees, or some kind of tools to assist with symmetrical designs
-- Nice To Haves
-----------------------------
-- Exploration pattern ladder https://dwarffortresswiki.org/index.php/DF2014:Exploratory_mining#Ladder_Rows
-- Stretch Goals
-----------------------------
-- Shape preview in panel
-- Shape designer in preview panel to draw repeatable shapes i'e' 2x3 room with door
-- 3D shapes, would allow stuff like spiral staircases/minecart tracks and other neat stuff, probably not too hard
-- END TODOS ================
local gui = require('gui')
local guidm = require('gui.dwarfmode')
local overlay = require('plugins.overlay')
local plugin = require('plugins.design')
local quickfort = reqscript('quickfort')
local shapes = reqscript('internal/design/shapes')
local textures = require('gui.textures')
local util = reqscript('internal/design/util')
local utils = require('utils')
local widgets = require('gui.widgets')
local Point = util.Point
local getMousePoint = util.getMousePoint
local to_pen = dfhack.pen.parse
local guide_tile_pen = to_pen{
ch='+',
fg=COLOR_YELLOW,
tile=dfhack.screen.findGraphicsTile('CURSORS', 0, 22),
}
local mirror_guide_pen = to_pen{
ch='+',
fg=COLOR_YELLOW,
tile=dfhack.screen.findGraphicsTile('CURSORS', 1, 22),
}
-- ----------------- --
-- DimensionsOverlay --
-- ----------------- --
DimensionsOverlay = defclass(DimensionsOverlay, overlay.OverlayWidget)
DimensionsOverlay.ATTRS{
desc='Adds a tooltip that shows the selected dimensions when drawing boxes.',
default_pos={x=1,y=1},
default_enabled=true,
fullscreen=true, -- not player-repositionable
viewscreens={
'dwarfmode/Designate',
'dwarfmode/Burrow/Paint',
'dwarfmode/Stockpile/Paint',
'dwarfmode/Zone/Paint',
'dwarfmode/Building/Placement',
},
}
local main_interface = df.global.game.main_interface
local selection_rect = df.global.selection_rect
local uibs = df.global.buildreq
local function get_selection_rect_pos()
if selection_rect.start_x < 0 then return end
return xyz2pos(selection_rect.start_x, selection_rect.start_y, selection_rect.start_z)
end
local function get_uibs_pos()
if uibs.selection_pos.x < 0 then return end
return uibs.selection_pos
end
function DimensionsOverlay:init()
self:addviews{
widgets.DimensionsTooltip{
get_anchor_pos_fn=function()
if dfhack.gui.matchFocusString('dwarfmode/Building/Placement',
dfhack.gui.getDFViewscreen(true))
then
return get_uibs_pos()
else
return get_selection_rect_pos()
end
end,
},
}
end
-- don't imply that stockpiles or zones will be 3d
local function check_stockpile_dims()
if selection_rect.start_x > 0 and
(main_interface.bottom_mode_selected == df.main_bottom_mode_type.STOCKPILE_PAINT or
main_interface.bottom_mode_selected == df.main_bottom_mode_type.ZONE_PAINT)
then
selection_rect.start_z = df.global.window_z
end
end
function DimensionsOverlay:render(dc)
check_stockpile_dims()
DimensionsOverlay.super.render(self, dc)
end
function DimensionsOverlay:preUpdateLayout(parent_rect)
self.frame.w = parent_rect.width
self.frame.h = parent_rect.height
end
function DimensionsOverlay:overlay_onenable()
df.global.d_init.display.flags.SHOW_RECTANGLE_DIMENSIONS = false
end
function DimensionsOverlay:overlay_ondisable()
df.global.d_init.display.flags.SHOW_RECTANGLE_DIMENSIONS = true
end
---
--- RightClickOverlay
---
RightClickOverlay = defclass(RightClickOverlay, overlay.OverlayWidget)
RightClickOverlay.ATTRS{
desc='When drawing boxes, makes right click cancel selection instead of exiting.',
default_enabled=true,
fullscreen=true,
viewscreens={
'dwarfmode/Designate',
'dwarfmode/Burrow/Paint',
'dwarfmode/Stockpile/Paint',
'dwarfmode/Zone/Paint',
'dwarfmode/Building/Placement'
},
}
function RightClickOverlay:onInput(keys)
if keys._MOUSE_R or keys.LEAVESCREEN then
-- building mode
if dfhack.gui.matchFocusString('dwarfmode/Building/Placement',
dfhack.gui.getDFViewscreen(true))
then
if uibs.selection_pos.x >= 0 then
uibs.selection_pos:clear()
return true
end
-- all other modes
elseif selection_rect.start_x >= 0 then
selection_rect.start_x = -30000
selection_rect.start_y = -30000
selection_rect.start_z = -30000
return true
end
end
end
OVERLAY_WIDGETS = {
dimensions=DimensionsOverlay,
rightclick=RightClickOverlay,
}
---
--- HelpWindow
---
CONSTRUCTION_HELP = {
'Building filters',
'================',
NEWLINE,
'Use the DFHack building planner to configure filters for the desired construction types. This tool will use the current buildingplan filters for a building type.'
}
HelpWindow = defclass(HelpWindow, widgets.Window)
HelpWindow.ATTRS{
frame_title='gui/design Help',
frame={w=43, h=20, t=10, l=10},
resizable=true,
resize_min={h=10},
message='',
}
function HelpWindow:init()
self:addviews{
widgets.WrappedLabel{
auto_height=false,
text_to_wrap=function() return self.message end,
},
}
end
-- Utilities
local BUTTON_PEN_LEFT = to_pen{fg=COLOR_CYAN, tile=curry(textures.tp_control_panel, 7) or nil, ch=string.byte('[')}
local HELP_PEN_CENTER = to_pen{tile=curry(textures.tp_control_panel, 9) or nil, ch=string.byte('?')}
local BUTTON_PEN_RIGHT = to_pen{fg=COLOR_CYAN, tile=curry(textures.tp_control_panel, 8) or nil, ch=string.byte(']')}
-- Debug window
SHOW_DEBUG_WINDOW = SHOW_DEBUG_WINDOW or false
local function table_to_string(tbl, indent)
indent = indent or ''
local result = {}
for k, v in pairs(tbl) do
local key = type(k) == 'number' and ('[%d]'):format(k) or tostring(k)
if type(v) == 'table' then
table.insert(result, indent .. key .. ' = {')
local subTable = table_to_string(v, indent .. ' ')
for _, line in ipairs(subTable) do
table.insert(result, line)
end
table.insert(result, indent .. '},')
else
local val = utils.getval(v)
local value = type(val) == 'string' and ('"%s"'):format(val) or tostring(val)
table.insert(result, indent .. key .. ' = ' .. value .. ',')
end
end
return result
end
DesignDebugWindow = defclass(DesignDebugWindow, widgets.Window)
DesignDebugWindow.ATTRS {
frame_title='Debug',
frame={w=47, h=40, l=10, t=8},
resizable=true,
resize_min={w=20, h=30},
autoarrange_subviews=true,
autoarrange_gap=1,
design_window=DEFAULT_NIL,
}
function DesignDebugWindow:init()
local attrs = {
'needs_update',
'placing_mark',
'#marks',
'placing_mirror',
'mirror',
'mirror_point',
'placing_extra',
'extra_points',
'last_mouse_point',
'prev_center',
'start_center',
}
for _, attr in ipairs(attrs) do
self:addviews{
widgets.WrappedLabel{
text_to_wrap=function()
local want_size = attr:startswith('#')
local field = want_size and attr:sub(2) or attr
if type(self.design_window[field]) ~= 'table' then
return ('%s: %s'):format(field, self.design_window[field])
end
if want_size then
return ('%s: %d'):format(attr, #self.design_window[field])
else
return ('%s: %s'):format(attr,
table.concat(table_to_string(self.design_window[attr], ' ')))
end
end,
},
}
end
end
function DesignDebugWindow:render(dc)
self:updateLayout()
DesignDebugWindow.super.render(self, dc)
end
--
-- Design
--
Design = defclass(Design, widgets.Window)
Design.ATTRS {
frame_title = 'Design',
frame={w=40, h=48, r=2, t=18},
resizable=true,
autoarrange_subviews=true,
autoarrange_gap=1,
}
local function make_mode_option(desig, mode, ch1, ch2, ch1_color, ch2_color, x, y, x_selected, y_selected)
y_selected = y_selected or y
return {
desig=desig,
mode=mode,
button_spec=util.make_button_spec(ch1, ch2, ch1_color, ch2_color, COLOR_GRAY, COLOR_WHITE, x, y),
button_selected_spec=util.make_button_spec(ch1, ch2, ch1_color, ch2_color, COLOR_YELLOW, COLOR_YELLOW, x_selected, y_selected),
}
end
function Design:init()
self.needs_update = true
self.marks = {}
self.extra_points = {}
self.placing_extra = {active=false, index=nil}
self.placing_mark = {active=true, index=1, continue=true}
self.placing_mirror = false
self.mirror = {horizontal=false, vertical=false}
local mode_options = {
{label='Dig', value=make_mode_option('d', 'dig', '-', ')', COLOR_BROWN, COLOR_GRAY, 0, 22, 4)},
{label='Stairs', value=make_mode_option('i', 'dig', '>', '<', COLOR_GRAY, COLOR_GRAY, 8, 22, 12)},
{label='Ramp', value=make_mode_option('r', 'dig', 30, 30, COLOR_GRAY, COLOR_GRAY, 0, 25, 4)},
{label='Channel', value=make_mode_option('h', 'dig', 31, 31, COLOR_GRAY, COLOR_GRAY, 8, 25, 12)},
{label='Smooth', value=make_mode_option('s', 'dig', 177, 219, COLOR_GRAY, COLOR_WHITE, 0, 55, 4)},
{label='Engrave', value=make_mode_option('e', 'dig', 219, 1, COLOR_GRAY, {fg=COLOR_WHITE, bg=COLOR_GRAY}, 0, 58, 4)},
-- TODO: get matching selected version of erase icon
{label='Remove Designation', value=make_mode_option('x', 'dig', 'X', 'X', COLOR_LIGHTRED, COLOR_LIGHTRED, 24, 28, 12)},
{label='Building', value=make_mode_option('b', 'build', 210, 229, COLOR_BROWN, COLOR_DARKGRAY, 16, 31, 20)},
}
local mode_button_specs, mode_button_specs_selected = {}, {}
for _, mode_option in ipairs(mode_options) do
table.insert(mode_button_specs, mode_option.value.button_spec)
table.insert(mode_button_specs_selected, mode_option.value.button_selected_spec)
end
local DESIGN_ICONS_WIDTH = 224 -- Must match design.png image width
local DESIGN_ICONS_HEIGHT = 72 -- Must match design.png image height
local DESIGN_ICON_ROW_COUNT = 2
local DESIGN_CHAR_WIDTH = 8
local DESIGN_CHAR_HEIGHT = 12
local shape_tileset = dfhack.textures.loadTileset('hack/data/art/design.png', DESIGN_CHAR_WIDTH, DESIGN_CHAR_HEIGHT, true)
local STRIDE = DESIGN_ICONS_WIDTH / DESIGN_CHAR_WIDTH
local CHARS_PER_ROW = DESIGN_ICONS_HEIGHT / (DESIGN_ICON_ROW_COUNT * DESIGN_CHAR_HEIGHT)
local shape_options, shape_button_specs, shape_button_specs_selected = {}, {}, {}
for _, shape in ipairs(shapes.all_shapes) do
table.insert(shape_options, {label=shape.name, value=shape})
table.insert(shape_button_specs, {
chars=shape.button_chars,
tileset=shape_tileset,
tileset_offset=shape.texture_offset,
tileset_stride=STRIDE,
})
table.insert(shape_button_specs_selected, {
chars=shape.button_chars,
pens=COLOR_YELLOW,
tileset=shape_tileset,
tileset_offset=shape.texture_offset+(STRIDE*CHARS_PER_ROW),
tileset_stride=STRIDE,
})
end
local build_options = {
{label='Walls', value='Cw'},
{label='Floor', value='Cf'},
{label='Fortification', value='CF'},
{label='Ramps', value='Cr'},
{label='None', value='`'},
}
self:addviews{
widgets.ButtonGroup{
view_id='mode',
key='CUSTOM_F',
key_back='CUSTOM_SHIFT_F',
label='Designation:',
options=mode_options,
on_change=function() self.needs_update = true end,
button_specs=mode_button_specs,
button_specs_selected=mode_button_specs_selected,
},
widgets.ResizingPanel{
autoarrange_subviews=true,
subviews={
widgets.Panel{
frame={h=2},
visible=function() return self.subviews.mode:getOptionValue().desig == 'i' end,
subviews={
widgets.CycleHotkeyLabel{
view_id='stairs_top_subtype',
frame={t=0, l=0},
key='CUSTOM_R',
label=' Top stair type:',
visible=function()
local bounds = self:get_view_bounds()
return bounds and bounds.z1 ~= bounds.z2 or false
end,
options={
{label='Auto', value='auto'},
{label='UpDown', value='i'},
{label='Down', value='j'},
},
},
widgets.CycleHotkeyLabel {
view_id='stairs_bottom_subtype',
frame={t=1, l=0},
key='CUSTOM_SHIFT_B',
label='Bottom Stair Type:',
visible=function()
local bounds = self:get_view_bounds()
return bounds and bounds.z1 ~= bounds.z2 or false
end,
options={
{label='Auto', value='auto'},
{label='UpDown', value='i'},
{label='Up', value='u'},
},
},
widgets.CycleHotkeyLabel{
view_id='stairs_only_subtype',
frame={t=0, l=0},
key='CUSTOM_R',
label='Single level stair:',
visible=function()
local bounds = self:get_view_bounds()
return not bounds or bounds.z1 == bounds.z2
end,
options={
{label='Up', value='u'},
{label='UpDown', value='i'},
{label='Down', value='j'},
},
},
}
},
widgets.Panel{
frame={h=2},
visible=function() return self.subviews.mode:getOptionValue().mode == 'build' end,
subviews={
widgets.Label{
frame={t=0, l=0},
text={{tile=BUTTON_PEN_LEFT}, {tile=HELP_PEN_CENTER}, {tile=BUTTON_PEN_RIGHT}},
on_click=self:callback('show_help', CONSTRUCTION_HELP),
},
widgets.CycleHotkeyLabel{
view_id='building_outer_tiles',
frame={t=0, l=4},
key='CUSTOM_R',
label='Outer Tiles:',
initial_option='Cw',
options=build_options,
},
widgets.CycleHotkeyLabel {
view_id='building_inner_tiles',
frame={t=1, l=4},
key='CUSTOM_G',
label='Inner Tiles:',
initial_option='Cf',
options=build_options,
},
},
},
widgets.CycleHotkeyLabel{
view_id='priority',
key='CUSTOM_SHIFT_P',
key_back='CUSTOM_P',
label='Priority:',
options={1, 2, 3, 4, 5, 6, 7},
initial_option=4,
visible=function()
local mode = self.subviews.mode:getOptionValue()
return mode.mode == 'dig' and mode.desig ~= 'x'
end,
},
widgets.HotkeyLabel{
key='CUSTOM_CTRL_X',
label='Clear entire z-level',
on_activate=function()
local map = df.global.world.map
quickfort.apply_blueprint{
mode='dig',
data=('x(%dx%d)'):format(map.x_count, map.y_count),
pos=xyz2pos(0, 0, df.global.window_z),
}
end,
visible=function()
local mode = self.subviews.mode:getOptionValue()
return mode.mode == 'dig' and mode.desig == 'x'
end,
},
},
},
widgets.Divider{
frame={h=1},
frame_style=gui.FRAME_THIN,
frame_style_l=false,
frame_style_r=false,
},
widgets.ButtonGroup{
view_id='shape',
key='CUSTOM_Z',
key_back='CUSTOM_SHIFT_Z',
label='Shape:',
options=shape_options,
on_change=function(shape, prev_shape)
self.needs_update = true
if shape.max_points ~= prev_shape.max_points then
self:reset()
end
end,
button_specs=shape_button_specs,
button_specs_selected=shape_button_specs_selected,
},
}
-- Currently only supports "bool" aka toggle and "plusminus" which creates
-- a pair of HotKeyLabel's to increment/decrement a value
-- Will need to update as needed to add more option types
local shape_options_panel = widgets.ResizingPanel{
autoarrange_subviews=true,
}
for _, shape in ipairs(shapes.all_shapes) do
for _, option in pairs(shape.options) do
if option.type ~= 'bool' then goto continue end
shape_options_panel:addviews{
widgets.ToggleHotkeyLabel{
frame={h=1},
auto_height=false,
key=option.key,
label=option.name..':',
initial_option=option.value,
enabled=option.enabled and function()
return shape.options[option.enabled[1]].value == option.enabled[2]
end or nil,
on_change=function(val)
option.value = val
self.needs_update = true
end,
visible=function() return self.subviews.shape:getOptionValue() == shape end,
}
}
::continue::
end
for _, option in pairs(shape.options) do
if option.type ~= 'plusminus' then goto continue end
shape_options_panel:addviews{
widgets.Panel{
frame={h=1},
visible=function() return self.subviews.shape:getOptionValue() == shape end,
subviews={
widgets.HotkeyLabel{
frame={t=0, l=0, w=1},
key=option.keys[1],
key_sep='',
enabled=function()
if option.enabled then
if shape.options[option.enabled[1]].value ~= option.enabled[2] then
return false
end
end
local min = utils.getval(option.min, shape)
return not min or option.value > min
end,
on_activate=function()
option.value = option.value - 1
self.needs_update = true
end,
},
widgets.HotkeyLabel{
frame={t=0, l=1},
key=option.keys[2],
label=function() return ('%s: %d'):format(option.name, option.value) end,
enabled=function()
if option.enabled then
if shape.options[option.enabled[1]].value ~= option.enabled[2] then
return false
end
end
local max = utils.getval(option.max, shape)
return not max or option.value <= max
end,
on_activate=function()
option.value = option.value + 1
self.needs_update = true
end,
}
},
},
}
::continue::
end
if shape.invertable then
shape_options_panel:addviews{
widgets.ToggleHotkeyLabel{
key='CUSTOM_I',
label='Invert:',
initial_option=shape.invert,
on_change=function(val)
shape.invert = val
self.needs_update = true
end,
visible=function() return self.subviews.shape:getOptionValue() == shape end,
},
}
end
end
local mirror_options = {
{label='Off', value=1},
{label='On (odd)', value=2},
{label='On (even)', value=3}
}
self:addviews{
shape_options_panel,
widgets.Divider{
frame={h=1},
frame_style=gui.FRAME_THIN,
frame_style_l=false,
frame_style_r=false,
},
widgets.ResizingPanel{
autoarrange_subviews=true,
subviews={
widgets.HotkeyLabel {
key='CUSTOM_B',
label=function()
return self.placing_mark.active and 'Stop placing points' or 'Start placing more points'
end,
visible=function() return not self.subviews.shape:getOptionValue().max_points end,
enabled=function() return not self.prev_center end,
on_activate=function()
self.placing_mark.active = not self.placing_mark.active
self.placing_mark.index = self.placing_mark.active and #self.marks + 1 or nil
if not self.placing_mark.active then
table.remove(self.marks, #self.marks)
else
self.placing_mark.continue = true
end
self.needs_update=true
end,
},
widgets.HotkeyLabel {
key='CUSTOM_V',
label=function()
local msg='Add: '
local shape = self.subviews.shape:getOptionValue()
if #self.extra_points < #shape.extra_points then
return msg .. shape.extra_points[#self.extra_points + 1].label
end
return msg .. 'N/A'
end,
enabled=function()
return #self.marks > 1 and
#self.extra_points < #self.subviews.shape:getOptionValue().extra_points
end,
visible=function() return #self.subviews.shape:getOptionValue().extra_points > 0 end,
on_activate=function()
if not self.placing_mark.active then
self.placing_extra.active=true
self.placing_extra.index=#self.extra_points + 1
elseif #self.marks > 0 then
local mouse_pos = getMousePoint()
if mouse_pos then table.insert(self.extra_points, mouse_pos) end
end
self.needs_update = true
end,
},
widgets.Panel{
frame={h=1},
subviews={
widgets.HotkeyLabel{
frame={t=0, l=0, w=1},
key='STRING_A040',
key_sep='',
enabled=function() return #self.marks > 1 end,
on_activate=self:callback('on_transform', 'ccw'),
},
widgets.HotkeyLabel{
frame={t=0, l=1},
key='STRING_A041',
label='Rotate',
auto_width=true,
enabled=function() return #self.marks > 1 end,
on_activate=self:callback('on_transform', 'cw'),
},
widgets.HotkeyLabel{
frame={t=0, l=14, w=1},
key='STRING_A095',
key_sep='',
enabled=function() return #self.marks > 1 end,
on_activate=self:callback('on_transform', 'flipv'),
},
widgets.HotkeyLabel {
frame={t=0, l=15},
key='STRING_A061',
label='Flip',
auto_width=true,
enabled=function() return #self.marks > 1 end,
on_activate=self:callback('on_transform', 'fliph'),
},
}
},
widgets.HotkeyLabel{
key='CUSTOM_M',
visible=function() return self.subviews.shape:getOptionValue().can_mirror end,
label=function()
if not self.placing_mirror and not self.mirror_point then
return 'Mirror across axis'
else
return 'Cancel mirror'
end
end,
enabled=function()
if #self.marks < 2 then return false end
return not self.placing_extra.active and
not self.placing_mark.active and not self.prev_center
end,
on_activate=function()
if not self.mirror_point then
self.placing_mark.active = false
self.placing_extra.active = false
self.placing_extra.active = false
self.placing_mirror = true
else
self.placing_mirror = false
self.mirror_point = nil
end
self.needs_update = true
end
},
widgets.CycleHotkeyLabel {
view_id='mirror_horiz_label',
frame={l=1},
key='CUSTOM_SHIFT_J',
label='Mirror horizontal: ',
options=mirror_options,
on_change=function() self.needs_update = true end,
visible=function() return self.placing_mirror or self.mirror_point end,
},
widgets.CycleHotkeyLabel {
view_id='mirror_diag_label',
frame={l=1},
key='CUSTOM_SHIFT_O',
label='Mirror diagonal: ',
options=mirror_options,
on_change=function() self.needs_update = true end,
visible=function() return self.placing_mirror or self.mirror_point end,
},
widgets.CycleHotkeyLabel {
view_id='mirror_vert_label',
frame={l=1},
key='CUSTOM_SHIFT_K',
label='Mirror vertical: ',
options=mirror_options,
on_change=function() self.needs_update = true end,
visible=function() return self.placing_mirror or self.mirror_point end,
},
widgets.HotkeyLabel {
frame={l=1},
key='CUSTOM_SHIFT_M',
label='Commit mirror changes',
on_activate=function()
self.marks = self:get_mirrored_points(self.marks)
self.mirror_point = nil
self.needs_update = true
end,
visible=function() return self.placing_mirror or self.mirror_point end,
},
widgets.HotkeyLabel {
key='CUSTOM_X',
label='Reset',
enabled=function() return #self.marks > 0 or #self.extra_points > 0 end,
on_activate=self:callback('reset'),
},
}
},
widgets.Panel{
frame={b=0},
subviews={
widgets.Panel{
frame={t=0, b=3},
frame_style=gui.FRAME_INTERIOR,
subviews={
widgets.Panel{
-- area expands with window
frame={t=0, b=2},
autoarrange_subviews=true,
autoarrange_gap=1,
subviews={
widgets.WrappedLabel{
text_to_wrap=self:callback('get_action_text'),
text_pen=COLOR_YELLOW,
},
widgets.WrappedLabel{
text_to_wrap=self:callback('get_area_text'),
},
widgets.WrappedLabel{
view_id='mark_text',
text_to_wrap=self:callback('get_mark_text'),
},
},
},
widgets.HotkeyLabel{
frame={b=0, l=0},
key='SELECT',
label='Commit shape to the map',
enabled=function() return #self.marks >= self.subviews.shape:getOptionValue().min_points end,
on_activate=function()
self:commit()
self.needs_update=true
end,
},
},
},
widgets.ToggleHotkeyLabel{
view_id='show_guides',
frame={b=1, l=0},
key='CUSTOM_SHIFT_G',
label='Show alignment guides:',
initial_option=true,
},
widgets.ToggleHotkeyLabel{
view_id='autocommit',
frame={b=0, l=0},
key='CUSTOM_ALT_C',
label='Auto-commit on click:',
initial_option=false,
},
},
},
}
end
function Design:reset()
self.needs_update = true
self.marks = {}
self.extra_points = {}
self.placing_extra = {active=false, index=nil}
self.placing_mark = {active=true, index=1, continue=true}
self.placing_mirror = false
self.mirror = {horizontal=false, vertical=false}
self.prev_center = nil
self.start_center = nil
end
function Design:get_action_text()
local text = ''
if self.marks[2] and self.placing_mark.active then
text = 'Click to place the point'
elseif not self.marks[2] then
text = 'Click to place the first point'
elseif not self.placing_extra.active and not self.prev_center then
text = 'Move any draggable points'
elseif self.placing_extra.active then
text = 'Place any extra points'
elseif self.prev_center then
text = 'Move the center point'
else
text = 'Move any draggable points'
end
return text .. ' with the mouse. Use right-click to dismiss points in order.'
end
function Design:get_area_text()
local bounds = self:get_view_bounds()
local label = 'Area: '
if not bounds then return label .. 'N/A' end
local width = math.abs(bounds.x2 - bounds.x1) + 1
local height = math.abs(bounds.y2 - bounds.y1) + 1
local depth = math.abs(bounds.z2 - bounds.z1) + 1
local tiles = self.subviews.shape:getOptionValue().num_tiles * depth
local plural = tiles == 1 and '' or 's'
return label .. ('%dx%dx%d (%d tile%s)'):format(width, height, depth, tiles, plural)
end
function Design:get_mark_text()
local label_text = {}
local marks = self.marks
local num_marks = #marks
if num_marks >= 1 then
local first_mark = marks[1]
table.insert(label_text, ('First Mark (%d): %d, %d, %d')
:format(1, first_mark.x, first_mark.y, first_mark.z))
end
if num_marks > 1 then
local last_index = num_marks - (self.placing_mark.active and 1 or 0)
local last_mark = marks[last_index]
if last_mark then
table.insert(label_text, ('Last Mark (%d): %d, %d, %d')
:format(last_index, last_mark.x, last_mark.y, last_mark.z))
end
end
local mouse_pos = getMousePoint()
if mouse_pos then
table.insert(label_text, ('Mouse: %d, %d, %d'):format(mouse_pos.x, mouse_pos.y, mouse_pos.z))
end
local mirror = self.mirror_point
if mirror then
table.insert(label_text, ('Mirror Point: %d, %d, %d'):format(mirror.x, mirror.y, mirror.z))
end
return label_text
end
-- Check to see if we're moving a point, or some change was made that implies we need to update the shape
-- This stops us needing to update the shape geometery every frame which can tank FPS
function Design:shape_needs_update()
if self.needs_update then return true end
local mouse_pos = getMousePoint()
if mouse_pos then
local mouse_moved = not self.last_mouse_point and mouse_pos or
(self.last_mouse_point ~= mouse_pos)
if self.placing_mark.active and mouse_moved then
return true
end
if self.placing_extra.active and mouse_moved then
return true
end
end
return false
end
function Design:on_transform(val)
local shape = self.subviews.shape:getOptionValue()
local center = shape:get_center()
-- Save mirrored points first
if self.mirror_point then
local points = self:get_mirrored_points(self.marks)
self.marks = points
self.mirror_point = nil
end
-- Transform marks
for i, mark in ipairs(self.marks) do
local x, y = mark.x, mark.y
if val == 'cw' then
x, y = center.x - (y - center.y), center.y + (x - center.x)
elseif val == 'ccw' then
x, y = center.x + (y - center.y), center.y - (x - center.x)
elseif val == 'fliph' then
x = center.x - (x - center.x)
elseif val == 'flipv' then
y = center.y - (y - center.y)
end
self.marks[i] = Point{x=math.floor(x + 0.5), y=math.floor(y + 0.5), z=self.marks[i].z}
end
-- Transform extra points
for i, point in ipairs(self.extra_points) do
local x, y = point.x, point.y
if val == 'cw' then
x, y = center.x - (y - center.y), center.y + (x - center.x)
elseif val == 'ccw' then
x, y = center.x + (y - center.y), center.y - (x - center.x)
elseif val == 'fliph' then
x = center.x - (x - center.x)
elseif val == 'flipv' then
y = center.y - (y - center.y)
end
self.extra_points[i] = Point{x=math.floor(x + 0.5), y=math.floor(y + 0.5), z=self.extra_points[i].z}
end
-- Calculate center point after transformation
shape:update(self.marks, self.extra_points)
local new_center = shape:get_center()
-- Calculate delta between old and new center points
local delta = center - new_center
-- Adjust marks and extra points based on delta
for i, mark in ipairs(self.marks) do
self.marks[i] = mark + Point{x=delta.x, y=delta.y, z=0}
end
for i, point in ipairs(self.extra_points) do
self.extra_points[i] = point + Point{x=delta.x, y=delta.y, z=0}
end
self.needs_update = true
end
function Design:get_view_bounds()
if #self.marks == 0 then return nil end
local min_x = self.marks[1].x