forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictActions.js
More file actions
268 lines (248 loc) · 10.3 KB
/
DictActions.js
File metadata and controls
268 lines (248 loc) · 10.3 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
/**
* @file This contains the action methods of the Turtle's Singer component's Dictionary blocks.
* @author Anindya Kundu
* @author Walter Bender
*
* @copyright 2014-2021 Walter Bender
* @copyright 2020 Anindya Kundu
*
* @license
* This program is free software; you can redistribute it and/or modify it under the terms of the
* The GNU Affero General Public License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* You should have received a copy of the GNU Affero General Public License along with this
* library; if not, write to the Free Software Foundation, 51 Franklin Street, Suite 500 Boston,
* MA 02110-1335 USA.
*
* Utility methods are in PascalCase.
* Action methods are in camelCase.
*/
/*
global
_, Turtle, Singer, getNote, INVALIDPITCH, pitchToNumber,
getTargetTurtle
*/
/*
Global Locations
js/utils/utils.js
_
js/turtle.js
Turtle
js/turtle-singer.js
Singer
js/utils/musicutils.js
getNote, pitchToNumber
js/logo.js
INVALIDPITCH
js/blocks/EnsembleBlocks.js
getTargetTurtle
*/
/* exported setupDictActions */
/**
* Sets up all the methods related to different actions for each block in Dictionary palette.
* @returns {void}
*/
function setupDictActions(activity) {
Turtle.DictActions = class {
/**
* Utility function to get Turtle properties associated with target (used by get value).
*
* @static
* @param {Number} target - target Turtle index in turtle.turtleList
* @param {Number} turtle - Turtle index in turtle.turtleList
* @param {String} key - key
* @param {Number?} blk - block index in blocks.blockList
* @returns {String|Number}
*/
static _GetDict(target, turtle, key, blk) {
const targetTur = activity.turtles.ithTurtle(target);
// This is the internal turtle dictionary that includes the turtle status.
if (key === _("color")) {
return targetTur.painter.color;
} else if (key === _("shade")) {
return targetTur.painter.value;
} else if (key === _("grey")) {
return targetTur.painter.chroma;
} else if (key === _("pen size")) {
return targetTur.painter.pensize;
} else if (key === _("font")) {
return targetTur.painter.font;
} else if (key === _("heading")) {
return targetTur.painter.turtle.orientation;
} else if (key === "x") {
return activity.turtles.screenX2turtleX(targetTur.container.x);
} else if (key === "y") {
return activity.turtles.screenY2turtleY(targetTur.container.y);
} else if (key === _("notes played")) {
return targetTur.singer.notesPlayed[0] / targetTur.singer.notesPlayed[1];
} else if (key === _("note value")) {
return Singer.RhythmActions.getNoteValue(target);
} else if (key === _("current pitch")) {
return targetTur.singer.lastNotePlayed[0];
} else if (key === _("pitch number")) {
let obj;
if (targetTur.singer.lastNotePlayed !== null) {
const len = targetTur.singer.lastNotePlayed[0].length;
const pitch = targetTur.singer.lastNotePlayed[0].slice(0, len - 1);
const octave = parseInt(targetTur.singer.lastNotePlayed[0].slice(len - 1));
obj = [pitch, octave];
} else if (targetTur.singer.notePitches.length > 0) {
obj = getNote(
targetTur.singer.notePitches[0],
targetTur.singer.noteOctaves[0],
0,
targetTur.singer.keySignature,
targetTur.singer.movable,
null,
activity.errorMsg,
activity.logo.synth.inTemperament
);
} else {
activity.errorMsg(INVALIDPITCH, blk);
obj = ["G", 4];
}
return (
pitchToNumber(obj[0], obj[1], targetTur.singer.keySignature) -
targetTur.singer.pitchNumberOffset
);
}
}
/**
* Utility function to set a value corresponding to a key.
*
* @static
* @param {Number} target - target Turtle index in turtle.turtleList
* @param {Number} turtle - Turtle index in turtle.turtleList
* @param {String} key - key
* @param {*} value - value
* @returns {void}
*/
static SetDictValue(target, turtle, key, value) {
const targetTur = activity.turtles.ithTurtle(target);
// This is the internal turtle dictionary that includes the turtle status.
if (key === _("color")) {
targetTur.painter.doSetColor(value);
} else if (key === _("shade")) {
targetTur.painter.doSetValue(value);
} else if (key === _("grey")) {
targetTur.painter.doSetChroma(value);
} else if (key === _("pen size")) {
targetTur.painter.doSetPensize(value);
} else if (key === _("font")) {
targetTur.painter.doSetFont(value);
} else if (key === _("heading")) {
targetTur.painter.doSetHeading(value);
} else if (key === "y") {
const x = activity.turtles.screenX2turtleX(targetTur.container.x);
targetTur.painter.doSetXY(x, value);
} else if (key === "x") {
const y = activity.turtles.screenY2turtleY(targetTur.container.y);
targetTur.painter.doSetXY(value, y);
}
}
/**
* Utility function to display dictionary as JSON.
*
* @static
* @param {Number} target - target Turtle index in turtle.turtleList
* @param {Number} turtle - Turtle index in turtle.turtleList
* @returns {String}
*/
static SerializeDict(target, turtle) {
const targetTur = activity.turtles.ithTurtle(target);
// This is the internal turtle dictionary that includes the turtle status.
const this_dict = {};
this_dict[_("color")] = targetTur.painter.color;
this_dict[_("shade")] = targetTur.painter.value;
this_dict[_("grey")] = targetTur.painter.chroma;
this_dict[_("pen size")] = targetTur.painter.stroke;
this_dict[_("font")] = targetTur.painter.font;
this_dict[_("heading")] = targetTur.painter.orientation;
this_dict["y"] = activity.turtles.screenY2turtleY(targetTur.container.y);
this_dict["x"] = activity.turtles.screenX2turtleX(targetTur.container.x);
if (target in activity.logo.turtleDicts[turtle]) {
for (const key in activity.logo.turtleDicts[turtle][target]) {
this_dict[key] = activity.logo.turtleDicts[turtle][target][key];
}
}
return JSON.stringify(this_dict);
}
/**
* Returns the contents of the queried dictionary.
*
* @static
* @param {String|Number} dict - dictionary name
* @param {Number} turtle - Turtle index in turtles.turtleList
* @returns {String}
*/
static getDict(dict, turtle) {
// Not sure this can happen.
if (!(turtle in activity.logo.turtleDicts)) activity.logo.turtleDicts[turtle] = {};
// Is the dictionary the same as a turtle name?
const target = getTargetTurtle(activity.turtles, dict);
if (target !== null) {
return Turtle.DictActions.SerializeDict(target, turtle);
}
return JSON.stringify(
dict in activity.logo.turtleDicts[turtle]
? activity.logo.turtleDicts[turtle][dict]
: {}
);
}
/**
* Displays the contents of the queried dictionary.
*
* @static
* @param {String|Number} dict - dictionary name
* @param {Number} turtle - Turtle index in turtles.turtleList
* @returns {void}
*/
static showDict(dict, turtle) {
activity.textMsg(Turtle.DictActions.getDict(dict, turtle));
}
/**
* Sets a value in the dictionary for a specified key.
*
* @static
* @param {String|Number} dict - dictionary name
* @param {String|Number} key
* @param {String|Number} value
* @param {Number} turtle - Turtle index in turtles.turtleList
* @returns {void}
*/
static setValue(dict, key, value, turtle) {
if (!(turtle in activity.logo.turtleDicts)) {
activity.logo.turtleDicts[turtle] = {};
}
if (!(dict in activity.logo.turtleDicts[turtle])) {
activity.logo.turtleDicts[turtle][dict] = {};
}
activity.logo.turtleDicts[turtle][dict][key] = value;
console.log(activity.logo.turtleDicts[turtle]);
}
/**
* Returns a value in the dictionary for a specified key.
*
* @static
* @param {String|Number} dict - dictionary name
* @param {String|Number} key
* @param {Number} turtle - Turtle index in turtles.turtleList
* @param {Number?} blk - block index in blocks.blockList
* @returns {String|Number}
*/
static getValue(dict, key, turtle, blk) {
if (!(dict in activity.logo.turtleDicts[turtle])) {
const msg = _("Dictionary with this name does not exist");
return msg;
} else if (!(key in activity.logo.turtleDicts[turtle][dict])) {
const msg = _("Key with this name does not exist in ") + dict;
return msg;
}
return activity.logo.turtleDicts[turtle][dict][key];
}
};
}
if (typeof module !== "undefined" && module.exports) {
module.exports = setupDictActions;
}