-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.nut
231 lines (197 loc) · 5.88 KB
/
module.nut
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
// shuffle-module
// by Keil Miller Jr
// https://github.com/keilmillerjr/shuffle-module
const SHUFFLE_VERSION = "2.2.0";
::SHUFFLE_VERSION <- SHUFFLE_VERSION;
class Shuffle {
__fatalError = null;
_ignoreNewSelection = null;
_hide = null;
_loop = null;
_reset = null;
_save = null;
_selected = null;
_slots = null;
constructor(opts) {
this.__fatalError = false;
this._ignoreNewSelection = false;
this._selected = 0;
// hide validation - defaulting argument
try {
assert(__validateBool(opts.hide));
this._hide = opts.hide;
}
catch(e) {
if ("hide" in opts) print("ERROR in an instance of Shuffle: constructor - improper hide argument, switching to default value\n");
this._hide = false;
}
// loop validation - defaulting argument
try {
assert(__validateBool(opts.loop));
this._loop = opts.loop;
}
catch(e) {
if ("loop" in opts) print("ERROR in an instance of Shuffle: constructor - improper reset argument, switching to default value\n");
this._loop = true;
}
// reset validation - defaulting argument
try {
assert(__validateBool(opts.reset));
this._reset = opts.reset;
}
catch(e) {
if ("reset" in opts) print("ERROR in an instance of Shuffle: constructor - improper reset argument, switching to default value\n");
this._reset = true;
}
// save validation - optional argument
if ("save" in opts) {
try {
assert(__validateSave(opts.save));
this._save = opts.save;
}
catch(e) {
print("ERROR in an instance of Shuffle: constructor - improper save argument\n");
this._save = null;
}
}
// slots validation - required argument
try {
assert(__validateSlots(opts.slots))
this._slots = opts.slots;
}
catch(e) {
print("ERROR in an instance of Shuffle: constructor - improper slots argument\n");
this.__fatalError = true;
}
// callbacks and handlers
if (!this.__fatalError) {
fe.add_signal_handler(this, "_signals");
fe.add_transition_callback(this, "_transitions");
}
}
# public
function getSelected() { return this._selected }
function getSlots() { return this._slots }
function getVersion() { return SHUFFLE_VERSION }
function setSelected(slot) { this._selected = slot }
# private
function _refresh() {
for (local i=0; i<this._slots.len(); i++) {
// hide slots
if (this._hide) {
if (i > fe.list.size-1) this._slots[i].visible = false;
else this._slots[i].visible = true;
}
// easy extendable functions
_refreshAll(this._slots[i]);
-(this._selected-i)==0 ? _refreshSelected(this._slots[i]) : _refreshDeselected(this._slots[i]);
}
}
function _refreshAll(slot) {}
function _refreshDeselected(slot) {}
function _refreshSelected(slot) {}
function _signals(signal_str) {
switch(signal_str) {
// ignore signals at start or end of list when looping is false
case "prev_game":
if (this._loop == false && fe.list.index == 0) return true;
break;
case "next_game":
if (this._loop == false && fe.list.index == fe.list.size-1) return true;
break;
// do not update selection for these signals
case "random_game":
case "prev_letter":
case "next_letter":
case "add_favourite":
case "prev_favourite":
case "next_favorite":
this._ignoreNewSelection = true;
break;
}
return false;
}
function _transitions(ttype, var, ttime) {
// from old selection
if (ttype == Transition.FromOldSelection) {
// do not update new selection
if (this._ignoreNewSelection == true) this._ignoreNewSelection = false;
// update selected
else __updateSelected(var);
}
// to new list
else if (ttype == Transition.ToNewList) {
// do not update new selection
if (this._ignoreNewSelection == true) this._ignoreNewSelection = false;
else {
// reset
if (this._reset == true) {
this._selected = 0;
fe.list.index = 0;
}
// load selected from fe.nv
else if (this._save != null) {
try {
assert(__validatesSelected(fe.nv.shuffle[this._save][fe.list.name]));
this._selected = fe.nv.shuffle[this._save][fe.list.name];
}
catch(e) { print("ERROR in an instance of Shuffle: save - improper save data\n"); }
}
}
}
// save selected in fe.nv
else if (ttype == Transition.EndNavigation && this._save) {
if (!("shuffle" in fe.nv)) fe.nv.shuffle <- {};
if (!(this._save in fe.nv.shuffle)) fe.nv.shuffle[this._save] <- {};
if (!(fe.list.name in fe.nv.shuffle[this._save])) fe.nv.shuffle[this._save][fe.list.name] <- this._selected;
else fe.nv.shuffle[this._save][fe.list.name] = this._selected;
}
// update index offsets and refresh
if (ttype == Transition.ToNewList || ttype == Transition.FromOldSelection) {
_updateIndexOffsets();
_refresh();
}
return false;
}
function _updateIndexOffsets() {
for (local i=0; i<this._slots.len(); i++) {
try { this._slots[i].index_offset = -(this._selected-i) } catch(e) {}
try { this._slots[i].art.index_offset = -(this._selected-i); } catch(e) {}
}
}
# protected
function __updateSelected(position) {
if (position<0 && this._selected<(this._slots.len()-1)) this._selected++;
if (position>0 && this._selected>0) this._selected--;
}
function __validateBool(var) {
try { assert(typeof(var) == "bool"); }
catch(e) { return false; }
return true;
}
function __validateSave(save) {
try {
assert(typeof(save) == "string");
assert(save.len()>0);
}
catch(e) { return false; }
return true;
}
function __validatesSelected(selected) {
try {
assert(typeof(selected) == "integer");
assert(selected>=0 && selected<this._slots.len());
}
catch(e) { return false; }
return true;
}
function __validateSlots(slots) {
if (typeof(slots) != "array") return false;
if (slots.len()<1) return false;
for (local i=0; i<slots.len(); i++) {
try { assert(typeof(slots[i].index_offset) == "integer" || typeof(slots[i].art.index_offset) == "integer"); }
catch(e) { return false; }
}
return true
}
}