-
Notifications
You must be signed in to change notification settings - Fork 3
/
dfdesigner.js
1256 lines (1002 loc) · 41.4 KB
/
dfdesigner.js
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
// DFDesigner - 2013
// Jos van Egmond / info@josvanegmond.nl / @manadar
// Todo's:
// - Small indicator of size of selection: Format: XxY. Eg: 7x6
// map is a 3d array which can be accessed in this way: map[z][x][y]
// z = elevation, top to bottom (0 = top, map_size_z = bottom)
// x = west to east (0 = west, map_size_x = east)
// y = north to south direction (0 = north, map_size_y = south)
var map;
var map_background; // for bg1 and bg2 and bg3
var map_temp; // for doing other things on maps, like tool preview
// SETTINGS //
var menu_width_tiles = 23; // makes the right side menu wider or smaller. its width is defined in a number of tiles (of size tile_size). You probably don't want to change this.
// END OF SETTINGS //
// tiles
var tile_size = 16; // in pixels
// the current viewport (eg browser)
var container_element;
var viewport_zlevel, viewport_zlevel_drawcontext;
var viewport_zlevel_background, viewport_zlevel_background_drawcontext;
var viewport_cursor, viewport_cursor_drawcontext;
var viewport_chrome, viewport_chrome_drawcontext;
var viewport_menu, viewport_menu_drawcontext;
var viewports = [];
var viewport_width = 800;
var viewport_height = 600;
var viewport_height_tiles = Math.floor(viewport_height / tile_size) - 1;
var viewport_width_tiles = Math.floor(viewport_width / tile_size) - 1;
// screen is only redrawed when necessary. tools, keyboard and mouse input request the redraw.
// the fps indicates here is how often the redraw request is checked.
var max_fps = 60;
var requestDrawZlevel = false;
var requestDrawZlevelBackground = false;
var requestDrawMenu = false;
var requestDrawChrome = false;
// size of the map, set to arbitrary defaults which are almost immediately overriden
var map_size_z = 40;
var map_size_x = 200;
var map_size_y = 200;
// current camera position
var camera_z = 19;
var camera_x = 0;
var camera_y = 0;
// these go into map[][][]
var tile_types = {
hidden: -1,
bg1: 'bg1',
bg2: 'bg2',
bg3: 'bg3',
dig: 'dig',
channel: 'channel',
stairs_up: 'stairs_up',
stairs_down: 'stairs_down',
stairs_updown: 'stairs_updown',
ramp_up: 'ramp_up'
};
// images variable stores loaded images in a dictionary
// TODO: Use a single sprite containing all the images for faster loading
var images = {
bg1: 'bg1.png',
bg2: 'bg2.png',
bg3: 'bg3.png',
dig: 'des_dig.png',
channel: 'des_channel.png',
stairs_updown: 'des_updownstairs.png',
stairs_down: 'des_upstairs.png',
stairs_up: 'des_downstairs.png',
ramp_up: 'des_channelup.png',
cursor: 'cursor.png',
cursor_blink: 'cursor_blink.png',
chrome: 'chrome.png'
};
// current cursor position (it is always on the same z-level as the camera)
var cursor_x = 0;
var cursor_y = 0;
var cursor_down = false;
var cursor_start_z = 0;
var cursor_start_x = 0;
var cursor_start_y = 0;
var cursor_tool;
var cursor_blink = false; // indicates whether or not the start point of an area is currently visible or visible (temp variable for draw() function)
var cursor_in_menu = false; // indicates whether or not the cursor is currently in the menu area of the UI
// create tools
var tools = []; // tools array is populated using the loadTools() function
var tool_type_none = 0; // A tool where none of the below fit.
var tool_type_area_square = 1; // An area tool which makes a square shape. This type of tool accepts 2 points. For example: The dig tool.
var tool_type_point = 2; // A point tool is a tool which takes 1 point. No tools use this yet, but buildings might use this in the future. For example, a chair tool.
var tool_type_area_circle = 4; // An area tool which makes a circle shape. This type of tool accepts 1 point (center) and a radius. For example: The dig tool.
var tool_type_level = 8; // A level tool is a tool which works on an entire z-level. For example: Clear this z-level
var tool_current_shape = tool_type_area_square;
// text properties
var text_line_height = 22;
var text_font_color_active = "white";
var text_font_color = "#CCC";
var text_font = "12pt sans-serif";
// polyfill for requestAnimationFrame
(function() {
var lastTime = 0;
var vendors = ['webkit', 'moz'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame =
window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());
function loadDesigner() {
WTF.trace.timeStamp('loadDesigner');
container_element = document.getElementById("drawregion");
// Create a viewport for the current z-level background
viewport_zlevel_background = document.createElement("canvas");
viewport_zlevel_background_drawcontext = viewport_zlevel_background.getContext("2d");
container_element.appendChild(viewport_zlevel_background);
// Create a viewport for the current z-level
viewport_zlevel = document.createElement("canvas");
viewport_zlevel_drawcontext = viewport_zlevel.getContext("2d");
container_element.appendChild(viewport_zlevel);
// Create a viewport for the cursor
viewport_cursor = document.createElement("canvas");
viewport_cursor_drawcontext = viewport_cursor.getContext("2d");
container_element.appendChild(viewport_cursor);
// Create a viewport for the chrome
viewport_chrome = document.createElement("canvas");
viewport_chrome_drawcontext = viewport_chrome.getContext("2d");
container_element.appendChild(viewport_chrome);
// Create a viewport for the menu
viewport_menu = document.createElement("canvas");
viewport_menu_drawcontext = viewport_menu.getContext("2d");
container_element.appendChild(viewport_menu);
viewports = [viewport_zlevel_background, viewport_zlevel, viewport_cursor, viewport_chrome, viewport_menu];
setCanvasSize(true);
loadTools();
window.onresize = function(event) {
setCanvasSize(false);
}
logmessage("loading images");
loadImages();
logmessage("creating world");
createWorld();
logmessage("starting renderer");
startDrawLoop();
}
function startDrawLoop() {
(function drawLoop() {
requestAnimationFrame(drawLoop);
draw();
})();
setInterval(function() {
tick();
}, 100);
requestDrawZlevelBackground = true;
requestDrawZlevel = true;
}
function tick() {
WTF.trace.timeStamp('tick');
cursor_blink = !cursor_blink;
}
function loadImages() {
var scope = WTF.trace.enterScope('loadImages');
var loadedImages = 0;
var numImages = 0;
for(var src in images) {
numImages++;
}
// load the images and catch onload events
for(var src in images) {
var url = images[src];
images[src] = new Image();
images[src].onload = function() {
loadedImages += 1;
if (loadedImages == numImages) {
WTF.trace.timeStamp('loadImages.completed');
requestDrawZlevel = true;
requestDrawZlevelBackground = true;
requestDrawChrome = true;
requestDrawMenu = true;
}
};
images[src].src = "img/" + url;
};
WTF.trace.leaveScope(scope);
}
function createWorld() {
var scope = WTF.trace.enterScope('createWorld');
map = []; map_background = []; map_temp = [];
// initialize map as a series of arrays containing arrays (making a "3 dimensional"-array)
for (z = 0; z < map_size_z; z += 1) {
map[z] = []; map_background[z] = []; map_temp[z] = [];
for (x = 0; x < map_size_x; x += 1) {
map[z][x] = []; map_background[z][x] = []; map_temp[z][x] = [];
for (y = 0; y < map_size_y; y += 1) {
map[z][x][y] = tile_types.hidden;
map_background[z][x][y] = tile_types.hidden;
map_temp[z][x][y] = tile_types.hidden;
// randomly distribute some background tiles (bg1 through bg3) to those tiles
var ran = Math.floor((Math.random()*450)+1); // 450 chosen as magic number through a bit of experimentation
if (ran == 1) {
map_background[z][x][y] = tile_types.bg1;
}
if (ran == 2) {
map_background[z][x][y] = tile_types.bg2;
}
if (ran == 3) {
map_background[z][x][y] = tile_types.bg3;
}
}
}
}
WTF.trace.leaveScope(scope);
}
function loadTools() {
WTF.trace.timeStamp('loadTools');
// Creates a list of tools which are supported by the designer.
// They are shown (in menus and such) in the order of appearance here.
// The required keycode property is taken from a list found here: http://unixpapa.com/js/key.html (Example: Space is 32)
// Alternatively, you can use this program here: http://jsfiddle.net/vWx8V/
// Dig designation tool
tools.dig = {
name: "Dig",
type: tool_type_area_square | tool_type_area_circle,
hotkey: "d",
preview: true,
keycode: 68,
run: function(map, tool_selector) {
tool_selector(function(x,y,z) {
map[z][x][y] = tile_types.dig;
});
}
};
// Channel designation tool
tools.channel = {
name: "Channel",
type: tool_type_area_square | tool_type_area_circle,
hotkey: "h",
preview: true,
keycode: 72,
run: function(map, tool_selector) {
tool_selector(function(x,y,z) {
map[z][x][y] = tile_types.channel;
});
}
};
// Upward stairway designation tool
tools.stairs_up = {
name: "Upward stairway",
type: tool_type_area_square | tool_type_area_circle,
hotkey: "u",
preview: true,
keycode: 85,
run: function(map, tool_selector) {
tool_selector(function(x,y,z) {
map[z][x][y] = tile_types.stairs_up;
});
}
};
// Downward stairway designation tool
tools.stairs_down = {
name: "Downward stairway",
type: tool_type_area_square | tool_type_area_circle,
hotkey: "j",
preview: true,
keycode: 74,
run: function(map, tool_selector) {
tool_selector(function(x,y,z) {
map[z][x][y] = tile_types.stairs_down;
});
}
};
// Up/Down stairs designation tool
tools.updownstairs = {
name: "Up/Down Stairway",
type: tool_type_area_square | tool_type_area_circle,
hotkey: "i",
preview: true,
keycode: 73,
run: function(map, tool_selector) {
tool_selector(function(x,y,z) {
map[z][x][y] = tile_types.stairs_updown;
});
}
};
// Upward ramp designation tool
tools.ramp_up = {
name: "Upward ramp",
type: tool_type_area_square | tool_type_area_circle,
hotkey: "r",
preview: true,
keycode: 82,
run: function(map, tool_selector) {
tool_selector(function(x,y,z) {
map[z][x][y] = tile_types.ramp_up;
});
}
};
tools.space1 = { name: "Spacer", select: function(oldtool) { cursor_tool = oldtool; } };
// Inverse
tools.inverse = {
name: "Invert designation",
type: tool_type_area_square | tool_type_area_circle,
hotkey: "v",
preview: true,
keycode: 86,
run: function(temp_map, tool_selector) {
tool_selector(function(x,y,z) {
if (map[z][x][y] === tile_types.hidden) {
temp_map[z][x][y] = tile_types.dig;
} else {
temp_map[z][x][y] = tile_types.hidden;
}
});
}
};
// Remove designation tool
tools.removedes = {
name: "Remove designation",
type: tool_type_area_square | tool_type_area_circle,
hotkey: "x",
preview: true,
keycode: 88,
run: function(map, tool_selector) {
tool_selector(function(x,y,z) {
map[z][x][y] = tile_types.hidden;
});
}
};
// Export tool
tools.export_quickfort = {
name: "Export to QuickFort",
type: tool_type_area_square,
hotkey: "q",
preview: false,
keycode: 81,
run: function(map, tool_selector) {
var exportmap = {
dig: 'd',
channel: 'h',
stairs_up: 'u',
stairs_down: 'j',
stairs_updown: 'i',
ramp_up: 'r'
};
var seperator = ',';
var output = '';
var min_x = map_size_x + 1;
var max_x = -1;
var min_y = map_size_y + 1;
var max_y = -1;
var min_z = map_size_z + 1;
var max_z = -1;
// find boundaries for this tool using tool_selector
tool_selector(function(x,y,z) {
if (map[z][x][y] !== tile_types.hidden) {
min_x = Math.min(min_x, x);
max_x = Math.max(max_x, x);
min_y = Math.min(min_y, y);
max_y = Math.max(max_y, y);
min_z = Math.min(min_z, z);
max_z = Math.max(max_z, z);
}
});
for (z = min_z; z <= max_z; z += 1) {
output += "#dig Blueprint generated by DFDesigner. Z-level: " + (z + 1).toString() + ".\n\n";
for (y = min_y; y <= max_y; y += 1) {
for (x = min_x; x <= max_x; x += 1) {
if (map[z][x][y] !== tile_types.hidden) {
output += exportmap[map[z][x][y]];
}
output += seperator;
}
output += '\n';
}
output += '\n';
}
var splash = document.getElementById('export');
splash.style.display = 'block';
var exporttext = document.getElementById('exporttext');
exporttext.innerHTML = output;
}
};
tools.space2 = { name: "Spacer", select: function(oldtool) { cursor_tool = oldtool; } };
tools.switch_tool_shape = {
name: "Switch tool shape [Square]",
type: tool_type_none,
hotkey: "b",
preview: false,
keycode: 66,
run: function(map, tool_selector) {
},
select: function(oldtool) {
if (tool_current_shape === tool_type_area_square) {
logmessage("tool shape = circle");
tool_current_shape = tool_type_area_circle;
this.name = "Switch tool shape [Circle]";
} else {
logmessage("tool shape = square");
tool_current_shape = tool_type_area_square;
this.name = "Switch tool shape [Square]";
}
cursor_tool = oldtool;
}
}
// The default tool is set here:
cursor_tool = tools.dig;
}
// when you want to call the run function of a tool with specific coordinates but aren't sure if you are inside the boundary of the map or not
function toolwboundary(tool, x, y, z) {
if (x < 0 || x >= map_size_x) { return; }
if (y < 0 || y >= map_size_y) { return; }
if (z < 0 || z >= map_size_z) { return; }
tool(x, y, z);
}
function create_selector_area_circle(start_z, end_z, center_x, center_y, radius) {
if (start_z > end_z) { var t = end_z; end_z = start_z; start_z = t; }
// This is the integer-only midpoint circle algorithm from Wikipedia. It is a variant of the C++/C# implementation here: http://en.wikipedia.org/wiki/Midpoint_circle_algorithm#Examples
// Instead of drawing an hollow circle, this draws a filled circle which I derived myself once I understood the logic after reading this great SO answer: http://stackoverflow.com/a/10878576
return function(tool) {
if (radius < 0) { return; }
for (z = start_z; z <= end_z; z += 1) {
var x = radius, y = 0;
var radiusError = 1 - x;
while (x >= y) {
horizontalLine(tool, -x + center_x, x + center_x, y + center_y, z);
horizontalLine(tool, -y + center_x, y + center_x, x + center_y, z);
horizontalLine(tool, -x + center_x, x + center_x, -y + center_y, z);
horizontalLine(tool, -y + center_x, y + center_x, -x + center_y, z);
y = y + 1;
if (radiusError < 0) {
radiusError = radiusError + (2 * y + 1);
} else {
x = x - 1;
radiusError = radiusError + (2 * (y - x + 1));
}
}
}
}
}
function horizontalLine(tool, start_x, end_x, y, z) {
for (x = start_x; x <= end_x; x += 1) {
toolwboundary(tool, x, y, z);
}
}
function create_selector_area_square(start_z, end_z, start_x, end_x, start_y, end_y) {
if (start_x > end_x) { var t = end_x; end_x = start_x; start_x = t; }
if (start_y > end_y) { var t = end_y; end_y = start_y; start_y = t; }
if (start_z > end_z) { var t = end_z; end_z = start_z; start_z = t; }
return function(tool) {
for (z = start_z; z <= end_z; z += 1) {
for (x = start_x; x <= end_x; x += 1) {
for (y = start_y; y <= end_y; y += 1) {
tool(x, y, z);
}
}
}
}
}
function setCanvasSize(set_world_size) {
var scope = WTF.trace.enterScope('setCanvasSize');
viewport_width = document.body.clientWidth;
viewport_height = document.body.clientHeight;
for (var i = 0; i < viewports.length; i++) {
var viewport = viewports[i];
viewport.width = viewport_width;
viewport.height = viewport_height;
};
viewport_height_tiles = Math.floor(viewport_height / tile_size) - 1;
viewport_width_tiles = Math.floor(viewport_width / tile_size) - 1;
if (set_world_size) {
var map_size_x_min = 100;
var map_size_y_min = 80;
map_size_x = Math.max(viewport_width_tiles - menu_width_tiles, map_size_x_min);
map_size_y = map_size_x;
logmessage("map size (z, x, y): " + map_size_z.toString() + ", " + map_size_x.toString() + ", " + map_size_y.toString());
camera_x = Math.round((map_size_x - - menu_width_tiles - viewport_width_tiles) / 2);
camera_y = Math.round((map_size_y - viewport_height_tiles) / 2);
}
logmessage("viewport size (w, h): " + viewport_width.toString() + ", " + viewport_height.toString());
requestDrawMenu = true;
requestDrawZlevel = true;
requestDrawChrome = true;
requestDrawZlevelBackground = true;
WTF.trace.leaveScope(scope);
}
function switchTool(tool) {
var oldtool = cursor_tool;
cursor_tool = tool;
if (tool.select) {
tool.select(oldtool);
}
requestDrawMenu = true;
logmessage("selected tool: " + cursor_tool.name);
}
function enableDesigner() {
WTF.trace.timeStamp('enableDesigner');
logmessage("attaching UI events");
container_element.onkeydown = function(evt) {
evt = evt || window.event;
// Indicates or not whether this keypress is handled. Supresses default browser behavior.
var handled = false;
// step is a variable that indicates how much to move the cursor by. It is ignored for anything other than the arrow keys.
var step = 1;
if (evt.shiftKey) {
step = 10;
}
if (evt.ctrlKey) {
step = 2;
}
switch (evt.keyCode) {
case 27: // escape
// menu up (or close notices etc.)
cursor_down = false;
requestDrawZlevel = true;
break;
case 39: // right arrow
moveCursor(camera_z, cursor_x + step, cursor_y);
handled = true;
break;
case 37: // left arrow
moveCursor(camera_z, cursor_x - step, cursor_y);
handled = true;
break;
case 38: // up arrow
moveCursor(camera_z, cursor_x, cursor_y - step);
handled = true;
break;
case 40: // down arror
moveCursor(camera_z, cursor_x, cursor_y + step);
handled = true;
break;
case 33: // page up
case 188: // ,
moveCursor(camera_z + 1, cursor_x, cursor_y);
handled = true;
break;
case 34: // page down
case 190: // .
moveCursor(camera_z - 1, cursor_x, cursor_y);
handled = true;
break;
case 13: // enter
if (cursor_tool.type & tool_type_area_square || cursor_tool.type & tool_type_area_circle) {
if (cursor_down) {
logmessage("finishing area select");
// finish rectangle
handle_endAreaSelect();
} else {
logmessage("starting area select");
cursor_start_z = camera_z;
cursor_start_x = cursor_x;
cursor_start_y = cursor_y;
cursor_down = true;
}
} else {
handle_endAreaSelect();
}
handled = true;
break;
}
// Check if any of the tools have the currently pushed key set as the hotkey
for(var tool in tools) {
tool = tools[tool];
if (evt.keyCode == tool.keycode) {
switchTool(tool);
if (cursor_tool.type & tool_type_none) {
cursor_down = false;
}
handled = true;
}
}
if (handled) {
requestDrawZlevel = true;
requestDrawMenu = true;
return false; // suppress default behavior
} else {
return true;
}
}
container_element.onmousedown = function(evt) {
handle_onMouseDown(evt.clientX, evt.clientY);
return false; // prevents the default browser behavior
};
container_element.addEventListener('touchstart', function(evt) {
// If there's exactly one finger inside this element
if (evt.targetTouches.length == 1) {
logmessage("touch down");
// Place element where the finger is
var touch = evt.targetTouches[0];
handle_onMouseDown(touch.pageX, touch.pageY);
}
evt.preventDefault();
return false;
});
var oldClientX = 0;
var oldClientY = 0;
container_element.onmousemove = function(evt) {
var clientX = evt.clientX;
var clientY = evt.clientY;
if (oldClientX == clientX && oldClientY == clientY) { return; }
handle_onMouseMove(clientX, clientY);
return false; // prevents the default behavior. Fixes problem with cursor changes and broken mouse functionality.
};
container_element.addEventListener('touchmove', function(evt) {
// If there's exactly one finger inside this element
if (evt.targetTouches.length == 1) {
// Place element where the finger is
var touch = evt.targetTouches[0];
handle_onMouseMove(touch.pageX, touch.pageY);
}
evt.preventDefault();
return false;
});
container_element.onmouseup = function(evt) {
handle_onMouseUp(evt.clientX, evt.clientY);
return false; // prevents the default behavior
};
container_element.addEventListener('touchend', function(evt) {
// If there's exactly one finger inside this element
if (evt.changedTouches.length > 0) {
logmessage("touch up");
// Place element where the finger is
var touch = evt.changedTouches[0];
handle_onMouseUp(touch.pageX, touch.pageY);
}
evt.preventDefault();
return false;
});
// TODO: Functionality for scroll wheel. What makes sense? Menu item switch or z-level change or ...?
}
function handle_onMouseDown(x, y) {
WTF.trace.timeStamp('handle_onMouseDown');
var menu_border = (1 + viewport_width_tiles - menu_width_tiles) * tile_size;
if (x > menu_border) { return; }
// TODO: Make function out of this
var tile_x = Math.floor(x / tile_size) - 1; // minus 1 because of the chrome
var tile_y = Math.floor(y / tile_size) - 1;
moveCursor(camera_z, tile_x + camera_x, tile_y + camera_y);
if (cursor_tool.type & tool_type_area_square) {
cursor_start_x = cursor_x;
cursor_start_y = cursor_y;
cursor_start_z = camera_z;
cursor_down = true;
}
requestDrawZlevel = true;
}
function handle_onMouseUp(x, y) {
WTF.trace.timeStamp('handle_onMouseUp');
// Check if the mouse is clicked inside the menu
var menu_border = (1 + viewport_width_tiles - menu_width_tiles) * tile_size;
if (x > menu_border) {
// check if ending a drag in the menu border
if (cursor_down) {
handle_endAreaSelect();
} else {
// clicked a menu item, the menu items start at: tile_size * 4 and are then evenly spaced apart by text_line_height pixels
var menu_start = tile_size * 4;
var menu_index = Math.round((y - menu_start) / text_line_height);
if (menu_index >= 0) {
var n = 0;
for (var tool in tools)
{
tool = tools[tool];
if (n == menu_index) {
switchTool(tool);
if (cursor_tool.type & tool_type_none) {
cursor_down = false;
}
}
n += 1;
}
}
}
} else {
handle_endAreaSelect();
}
requestDrawZlevel = true;
}
function handle_endAreaSelect() {
if (!cursor_down) return;
WTF.trace.timeStamp('handle_endAreaSelect');
cursor_down = false;
logmessage("Running tool: " + cursor_tool.name);
var selector = getSelector();
cursor_tool.run(map, selector);
requestDrawZlevel = true;
}
function handle_onMouseMove(x, y) {
WTF.trace.timeStamp('handle_onMouseMove');
// ignore mouse moves inside the menu
var menu_bar_x = (Math.floor(viewport_width / tile_size) - menu_width_tiles) * tile_size;
if (x > menu_bar_x) {
cursor_in_menu = true;
return;
} else {
cursor_in_menu = false;
}
var tile_x = camera_x + Math.floor(x / tile_size) - 1; // minus 1 because of the chrome
var tile_y = camera_y + Math.floor(y / tile_size) - 1;
// move the cursor, but dont follow the camera immediately
var moved = moveCursor(camera_z, tile_x, tile_y, true);
// if we didnt move the cursor, there's no point in moving the camera
if (!moved) { return; }
var oldz = camera_z;
var moveCameraLater = function() {
WTF.trace.timeStamp("moveCameraLater");
if (oldz != camera_z) { return; }
if (cursor_x != tile_x) { return; }
if (cursor_y != tile_y) { return; }
// delayed camera movement is disabled while the cursor is in the menu (because the user intended to use the menu, and not move camera)
if (cursor_in_menu) { return; }
moveCameraToCursor();
// and repeat the process in order to keep the camera moving
tile_x = camera_x + Math.floor(x / tile_size) - 1; // minus 1 because of the chrome
tile_y = camera_y + Math.floor(y / tile_size) - 1;
moved = moveCursor(camera_z, tile_x, tile_y, true);
setTimeout(moveCameraLater, 300);
};
// after N seconds, check if the cursor is still in the same spot, and move the camera
setTimeout(moveCameraLater, 300);
}
// move the cursor, with a flag to indicate if the camera should follow the cursor.
// returns whether or not the cursor has moved
function moveCursor(z, x, y, dont_move_camera) {
WTF.trace.timeStamp('moveCursor');
if (camera_z == z && cursor_x == x && cursor_y == y) { return false; }
var old_camera_z = camera_z;
camera_z = Math.min(Math.max(z, 0), map_size_z - 1);
cursor_x = Math.min(Math.max(x, 0), map_size_x - 1);
cursor_y = Math.min(Math.max(y, 0), map_size_y - 1);
if (!dont_move_camera) {
moveCameraToCursor();
}
requestDrawZlevel = true;
requestDrawZlevelBackground = true;
return true;
}
function moveCameraToCursor() {
WTF.trace.timeStamp('moveCameraToCursor');
var camera_step = 10;
var rel_x = cursor_x - camera_x;
var rel_y = cursor_y - camera_y;
var diff_x = rel_x - viewport_width_tiles + menu_width_tiles - 1;
var diff_y = rel_y - viewport_height_tiles;
// idk why this works but it does
// Check camera up
if (rel_y < 3) {
camera_y = Math.max(0, camera_y - camera_step);
}
// Check camera down
if (diff_y > -5) {
camera_y = Math.min(map_size_y, camera_y + camera_step);
}
// Check camera left
if (rel_x < 3) {
camera_x = Math.max(0, camera_x - camera_step);
}
// Check camera right
if (diff_x > -5) {
camera_x = Math.min(map_size_x, camera_x + camera_step);
}
}
// based on the current context (namely: selected tool, preferred tool shape) returns the proper selector
function getSelector() {
WTF.trace.timeStamp('getSelector');
var preferred = tool_current_shape;
var supported = cursor_tool.type;
// level select
if (cursor_tool.type & tool_type_level) {
return selector = create_selector_area_square(camera_z, camera_z, 0, map_size_x - 1, 0, map_size_y - 1);
}
// if the preferred tool is supported, use the preferred tool
if (supported & preferred) {
// if preferred tool is circle select, use circle select
if (preferred & tool_type_area_circle) {
var radius = Math.max(Math.abs(cursor_start_x - cursor_x), Math.abs(cursor_start_y - cursor_y));
return create_selector_area_circle(camera_z, cursor_start_z, cursor_start_x, cursor_start_y, radius);
}
// if preferred tool is square select, use square select
if (preferred & tool_type_area_square) {
return create_selector_area_square(camera_z, cursor_start_z, cursor_x, cursor_start_x, cursor_y, cursor_start_y);
}
} else {
// if selected tool is not available, defaulting to square select
if (supported & tool_type_area_square) {
return create_selector_area_square(camera_z, cursor_start_z, cursor_x, cursor_start_x, cursor_y, cursor_start_y);
}
}
// if we don't have a selector, we default to a point select. This covers adequatly the case: cursor_tool.type & tool_type_none || cursor_tool.type & tool_type_point
return create_selector_area_square(camera_z, camera_z, cursor_x, cursor_x, cursor_y, cursor_y);
}
function drawCurZlevel() {
if (!requestDrawZlevel) return;
requestDrawZlevel = false;
// calculations to aid drawing process this makes sure we don't render (much) more than is necessary
var max_render_x = Math.min(camera_x + viewport_width_tiles, map_size_x) - 1;
var max_render_y = Math.min(camera_y + viewport_height_tiles, map_size_y) - 1;
/* START CLEAR */
var scope_draw_clearrect = WTF.trace.enterScope('draw.clearrect');
viewport_zlevel_drawcontext.clearRect(tile_size, tile_size, (viewport_width_tiles - menu_width_tiles) * tile_size, (viewport_height_tiles - 1) * tile_size);
WTF.trace.leaveScope(scope_draw_clearrect);
/* END CLEAR */