-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tron.html
237 lines (202 loc) · 6.4 KB
/
test_tron.html
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
<!DOCTYPE html>
<html>
<head>
<style>
canvas {
border-style: solid;
border-width: 1px;
background-color: #EEE;
float: left;
clear: left;
}
#ui{
margin-left: 30px;
width: 200px;
float: left;
}
div.ui_group {
border-width: 1px;
border-color: black;
border-style: solid;
margin: 4px;
padding: 4px;
}
.float{
float: left;
}
input.num_input{
width: 50px;
}
.num_input input{
width: 50px;
}
</style>
<meta charset="utf-8"/>
</head>
<body>
<canvas id="main_canvas" width="1000" height="800"></canvas>
<div id="ui">
<div class="ui_group">
<input id="play_pause_button" value="Play" type="button"/>
<input id="clear_button" value="Clear" type="button"/>
</div>
<div id="resize" class="ui_group">
<form id="xy_resize">
<div class="">Resize grid: </div>
<div class="num_input"> x <input class="x" type="number"/> </div>
<div class="num_input"> y <input class="y" type="number"/> </div>
</form>
</div>
<div id="rle" class="ui_group">
<div class="">Import Pattern</div>
<div>
<input class="" type="button" id="rle_export" value="export RLE" />
<input class="" type="button" id="rle_import" value="import RLE" />
</div>
<form id="xy_import">
<input type="radio" name="import_type" value="whole" checked="true">Import whole</input> </br>
<input type="radio" name="import_type" value="at_coords">Import at coords</input>
<div class="num_input"> x <input class="x" type="number" value="0"/> </div>
<div class="num_input"> y <input class="y" type="number" value="0"/> </div>
</form>
<textarea id="rle_data"></textarea>
</div>
<div class="ui_group">
<div>
Cell size: <input id="cellsize" class="num_input" type="number"/>
Draw Grid: <input id="drawGrid" type="checkbox"></div>
</div>
<div class="ui_group">
FPS: <span id="fps_counter"></span>
</div>
<div class="ui_group">
DEBUG: <span id="debug"></span>
</div>
</div>
<script type="text/javascript" src="jquery-1.12.1.js"></script>
<script type="text/javascript" src="timer.js"></script>
<script type="text/javascript" src="vector.js"></script>
<script type="text/javascript" src="tron.js"></script>
<script type="text/javascript">
console.log("Loading main js...")
var DEFAULTS = {
gridSize: new Vector(32, 32),
cellSize: 16,
drawGrid: true,
};
var UI = {};
//get/set x/y val as a vector
function xyFormProp(formObj, vect_val) {
if(formObj == null) { throw "Null ptr in xyFormProp";}
if(vect_val === undefined) { //Getter
return new Vector(
0|formObj.find("input.x").val(),
0|formObj.find("input.y").val());
} else { //Setter
formObj.find("input.x").val(vect_val.x);
formObj.find("input.y").val(vect_val.y);
}
}
function inputProp(inputObj, form_val) {
if(inputObj == null) { throw "Null ptr in inputProp";}
if(form_val === undefined) { //Getter
return 0|inputObj.val();
} else { //Setter
inputObj.val(form_val);
}
}
//Returns a vector with canvas click coords
function getCanvasClick(j_canvas, event) {
var rect = UI.canvas[0].getBoundingClientRect();
var x = event.clientX - rect.left;
var y = event.clientY - rect.top;
return new Vector(x, y);
}
function init() {
//================================
//Setup canvas mouse events
UI.canvas = $("canvas#main_canvas");
UI.canvas.click(function(event){
Wrapper.doClick(getCanvasClick(UI.canvas, event));});
UI.canvas.mousemove(function(event){
Wrapper.doMove(getCanvasClick(UI.canvas, event));});
UI.canvas.mouseout(function(event){
Wrapper.doMouseOut();});
UI.canvas.on("wheel", function(event){
var dy = event.originalEvent.deltaY;
if(Math.abs(dy) < 0.0001) { dy = 0;}
else if(dy < 0) { dy = 1;}
else {dy = -1;}
var lastSize = Wrapper.cellSizeProp();
var newSize = Wrapper.cellSizeProp(lastSize + dy);
inputProp(UI.cellsize_input, newSize);
});
//=================================
//Setup events for main UI elements
//Button: play
UI.play_pause_button = $("input#play_pause_button");
UI.play_pause_button.click(function(event) {
var wasPlaying = Wrapper.playingProp();
var playing = Wrapper.playingProp(!wasPlaying);
UI.play_pause_button.val(playing ? "Pause" : "Play");
});
//Button: clear
UI.clear_button = $("input#clear_button");
UI.clear_button.click(function(event) {
Wrapper.clear()});
//Form submit: Import RLE string
UI.rle_import_input = $("input#rle_import");
UI.xy_import_form = $("form#xy_import");
UI.rle_import_type = $("#xy_import input:radio[name=import_type]");
UI.rle_data = $("textarea#rle_data");
UI.rle_import_input.click(function(event) {
var importType = UI.rle_import_type.filter(":checked").val();
var importCoords = xyFormProp(UI.xy_import_form);
if(importType == "whole") {
Wrapper.importWhole(UI.rle_data.val());
} else if(importType == "at_coords") {
Wrapper.importAtCoords(UI.rle_data.val(), importCoords);
} else { throw "invalid import option"}});
//Button: Export RLE string
UI.rle_export_input = $("input#rle_export");
UI.rle_export_input.click(function(event) {
UI.rle_data.val(Wrapper.exportRLE());});
//XY field change: Grid size
UI.resize_form = $("form#xy_resize");
UI.resize_form.change(function(event){
var rval = Wrapper.gridSizeProp(xyFormProp(UI.resize_form));
xyFormProp(UI.resize_form, rval);
});
//Checkbox: drawGrid
UI.drawGrid_input = $("input#drawGrid");
UI.drawGrid_input.change(function(event){
var rval = Wrapper.gridDrawProp(UI.drawGrid_input[0].checked);
UI.drawGrid_input[0].checked = rval;
});
//Field: cellSize
UI.cellsize_input = $("input#cellsize");
UI.cellsize_input.change(function(event){
inputProp(UI.cellsize_input, Wrapper.cellSizeProp(UI.cellsize_input.val()));
});
//update FPS counter
UI.fps_counter = $("span#fps_counter");
setInterval(function() {
UI.fps_counter.text(Wrapper.getFPS());},
1000);
//Init UI fields to reflect values
initProperties()
//===============================
//Start main code
//Init Wrapper
Wrapper.init(DEFAULTS.gridSize, UI.canvas[0], DEFAULTS.cellSize)
}
function initProperties() {
xyFormProp(UI.resize_form, DEFAULTS.gridSize);
inputProp (UI.cellsize_input, DEFAULTS.cellSize);
UI.drawGrid_input[0].checked = DEFAULTS.drawGrid;
}
console.log("Done");
$(document).ready(init);
</script>
</body>
</html>