-
Notifications
You must be signed in to change notification settings - Fork 0
/
Editor.jack
94 lines (83 loc) · 2.31 KB
/
Editor.jack
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
/*
Editor to allow the user to draw their own game starting state.
*/
class Editor {
field Lifecycle lifecycle;
field int x, y;
field boolean cursorColor, editing;
constructor Editor new(Lifecycle theLifecycle) {
let lifecycle = theLifecycle;
let x = 0;
let y = 0;
let editing = false;
return this;
}
// switch to "editing" mode, where the cursor is visible
method void startEditing() {
let editing = true;
do drawCursor();
return;
}
// switch off "editing" mode, so the cursor is hidden
method void stopEditing() {
do hideCursor();
let editing = false;
return;
}
// draw the cursor at the current position; cursor is a black dot on dead grid squares,
// or a white dot on alive grid squares
method void drawCursor() {
var boolean alive;
if (editing) {
let alive = lifecycle.isAlive(x, y);
do Grid.drawCursor(x, y, ~alive);
}
return;
}
// hide the cursor at the current position
method void hideCursor() {
var boolean alive;
let alive = lifecycle.isAlive(x, y);
do Grid.drawCursor(x, y, alive);
return;
}
// switches the current cell from dead to alive or vice versa
method void toggleCell() {
do lifecycle.toggleCell(x, y);
do Grid.fillFromArray(lifecycle.getCurrentIteration());
do drawCursor();
return;
}
// moves the cursor to the left
method void moveLeft() {
do hideCursor();
if (x = 0) { let x = 31; } else { let x = x - 1; }
do drawCursor();
return;
}
// moves the cursor to the right
method void moveRight() {
do hideCursor();
if (x = 31) { let x = 0; } else { let x = x + 1; }
do drawCursor();
return;
}
// moves the cursor up
method void moveUp() {
do hideCursor();
if (y = 0) { let y = 31; } else { let y = y - 1; }
do drawCursor();
return;
}
// moves the cursor down
method void moveDown() {
do hideCursor();
if (y = 31) { let y = 0; } else { let y = y + 1; }
do drawCursor();
return;
}
method void dispose() {
do Memory.deAlloc(this);
return;
}
}