This repository was archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGDJS.js
308 lines (265 loc) · 11.3 KB
/
GDJS.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
const initGDevelopJS = require('../../Binaries/Output/libGD.js/Release/libGD.js');
describe('libGD.js - GDJS related tests', function() {
let gd = null;
beforeAll(() => (gd = initGDevelopJS()));
describe('EventsCodeGenerator', function() {
it('can generate code for a layout with generateSceneEventsCompleteCode', function() {
const project = gd.ProjectHelper.createNewGDJSProject();
const layout = project.insertNewLayout('Scene', 0);
// Create a repeat event with a trigger once
const evt = layout
.getEvents()
.insertNewEvent(project, 'BuiltinCommonInstructions::Repeat', 0);
gd.asRepeatEvent(evt).setRepeatExpression('5+4+3+2+1');
const condition = new gd.Instruction();
condition.setType('BuiltinCommonInstructions::Once');
gd.asRepeatEvent(evt)
.getConditions()
.insert(condition, 0);
const code = gd.EventsCodeGenerator.generateSceneEventsCompleteCode(
project,
layout,
layout.getEvents(),
new gd.SetString(),
true
);
// The loop is using a counter somewhere
expect(code).toMatch('repeatCount');
// Trigger once is used in a condition
expect(code).toMatch('runtimeScene.getOnceTriggers().triggerOnce');
condition.delete();
});
it('can generate code for a layout with generateEventsFunctionCode', function() {
const project = new gd.ProjectHelper.createNewGDJSProject();
const includeFiles = new gd.SetString();
const eventsFunction = new gd.EventsFunction();
// Create a function accepting 4 parameters:
const parameters = eventsFunction.getParameters();
const parameter1 = new gd.ParameterMetadata();
parameter1.setType('objectList');
parameter1.setName('MyObject');
parameter1.setDescription('The first object to be used');
const parameter2 = new gd.ParameterMetadata();
parameter2.setType('expression');
parameter2.setName('MyNumber');
parameter2.setDescription('Some number');
const parameter3 = new gd.ParameterMetadata();
parameter3.setType('objectList');
parameter3.setName('MySprite');
parameter3.setDescription('The second object to be used, a sprite');
parameter3.setExtraInfo('Sprite');
const parameter4 = new gd.ParameterMetadata();
parameter4.setType('string');
parameter4.setName('MyString');
parameter4.setDescription('Some string');
parameters.push_back(parameter1);
parameters.push_back(parameter2);
parameters.push_back(parameter3);
parameters.push_back(parameter4);
// Create a repeat event with...
const eventsList = eventsFunction.getEvents();
const evt = eventsList.insertNewEvent(
project,
'BuiltinCommonInstructions::Repeat',
0
);
gd.asRepeatEvent(evt).setRepeatExpression('5+4+3+2+1');
// ...a trigger once condition...
const condition = new gd.Instruction();
condition.setType('BuiltinCommonInstructions::Once');
gd.asRepeatEvent(evt)
.getConditions()
.insert(condition, 0);
// ...and an action to update a variable of MyObject
const action = new gd.Instruction();
action.setType('ModVarObjet');
action.setParametersCount(4);
action.setParameter(0, 'MyObject');
action.setParameter(1, 'ObjectVariable');
action.setParameter(2, '+');
action.setParameter(3, '42');
gd.asRepeatEvent(evt)
.getActions()
.insert(action, 0);
const action2 = new gd.Instruction();
action2.setType('ModVarObjet');
action2.setParametersCount(4);
action2.setParameter(0, 'MyObject');
action2.setParameter(1, 'ObjectVariable2');
action2.setParameter(2, '=');
action2.setParameter(
3,
'GetArgumentAsNumber("MyNumber") + ToNumber(GetArgumentAsString("MyString"))'
);
gd.asRepeatEvent(evt)
.getActions()
.insert(action2, 1);
const namespace = 'gdjs.eventsFunction.myTest';
const code = gd.EventsCodeGenerator.generateEventsFunctionCode(
project,
eventsFunction,
namespace,
includeFiles,
true
);
// Check that the function name is properly generated
expect(code).toMatch(namespace + '.func = function(');
// Check that the context for the events function is here...
expect(code).toMatch('function(runtimeScene, eventsFunctionContext)');
expect(code).toMatch('var eventsFunctionContext =');
// Check that the parameters, with the (optional) context of the parent function,
// are all here
expect(code).toMatch(
'function(runtimeScene, MyObject, MyNumber, MySprite, MyString, parentEventsFunctionContext)'
);
// ...and objects should be able to get queried...
expect(code).toMatch('\"MyObject\": MyObject');
expect(code).toMatch('\"MySprite\": MySprite');
// ...and arguments should be able to get queried too:
expect(code).toMatch('if (argName === "MyNumber") return MyNumber;');
expect(code).toMatch('if (argName === "MyString") return MyString;');
// GetArgumentAsString("MyString") should be generated code to query and cast as a string
// the argument
expect(code).toMatch(
'(typeof eventsFunctionContext !== \'undefined\' ? "" + eventsFunctionContext.getArgument("MyString") : "")'
);
// GetArgumentAsNumber("MyNumber") should be generated code to query and cast as a string
// the argument
expect(code).toMatch(
'(typeof eventsFunctionContext !== \'undefined\' ? Number(eventsFunctionContext.getArgument("MyNumber")) || 0 : 0)'
);
// The loop is using a counter somewhere
expect(code).toMatch('repeatCount');
// Trigger once is used in a condition
expect(code).toMatch('runtimeScene.getOnceTriggers().triggerOnce');
// A variable have 42 added to it
expect(code).toMatch('getVariables().get("ObjectVariable")).add(42)');
condition.delete();
action.delete();
});
it('can generate code for a layout with generateEventsFunctionCode, with groups', function() {
const project = new gd.ProjectHelper.createNewGDJSProject();
const includeFiles = new gd.SetString();
const eventsFunction = new gd.EventsFunction();
// Create a function accepting 2 parameters:
const parameters = eventsFunction.getParameters();
const parameter1 = new gd.ParameterMetadata();
parameter1.setType('objectList');
parameter1.setName('MyObject');
parameter1.setDescription('The first object to be used');
const parameter2 = new gd.ParameterMetadata();
parameter2.setType('objectList');
parameter2.setName('MySprite');
parameter2.setDescription('The second object to be used, a sprite');
parameter2.setExtraInfo('Sprite');
parameters.push_back(parameter1);
parameters.push_back(parameter2);
// And with a group:
const group = eventsFunction.getObjectGroups().insertNew("MyGroup", 0);
group.addObject("MyObject");
group.addObject("MySprite");
// Create a repeat event with...
const eventsList = eventsFunction.getEvents();
const evt = eventsList.insertNewEvent(
project,
'BuiltinCommonInstructions::Repeat',
0
);
gd.asRepeatEvent(evt).setRepeatExpression('5+4+3+2+1');
// ...an action to update a variable of the group of objects
const action = new gd.Instruction();
action.setType('ModVarObjet');
action.setParametersCount(4);
action.setParameter(0, 'MyGroup');
action.setParameter(1, 'ObjectVariable');
action.setParameter(2, '+');
action.setParameter(3, '42');
gd.asRepeatEvent(evt)
.getActions()
.insert(action, 0);
const namespace = 'gdjs.eventsFunction.myTest';
const code = gd.EventsCodeGenerator.generateEventsFunctionCode(
project,
eventsFunction,
namespace,
includeFiles,
true
);
// Check that the function name is properly generated
expect(code).toMatch(namespace + '.func = function(');
// Check that the context for the events function is here...
expect(code).toMatch('function(runtimeScene, eventsFunctionContext)');
expect(code).toMatch('var eventsFunctionContext =');
// Check that the parameters, with the (optional) context of the parent function,
// are all here
expect(code).toMatch(
'function(runtimeScene, MyObject, MySprite, parentEventsFunctionContext)'
);
// ...and objects should be able to get queried...
expect(code).toMatch('\"MyObject\": MyObject');
expect(code).toMatch('\"MySprite\": MySprite');
// Variables of both MyObject and MySprite should have 42 added:
expect(code).toMatch('GDMyObjectObjects2[i].getVariables().get("ObjectVariable")).add(42)');
expect(code).toMatch('GDMySpriteObjects2[i].getVariables().get("ObjectVariable")).add(42)');
action.delete();
});
});
describe('TextObject', function() {
it('should expose TextObject specific methods', function() {
var object = new gd.TextObject('MyTextObject');
object.setString('Hello');
object.setFontName('Hello.ttf');
object.setCharacterSize(10);
object.setBold(true);
object.setColor(1, 2, 3);
expect(object.getString()).toBe('Hello');
expect(object.getFontName()).toBe('Hello.ttf');
expect(object.getCharacterSize()).toBe(10);
expect(object.isBold()).toBe(true);
expect(object.getColorR()).toBe(1);
expect(object.getColorG()).toBe(2);
expect(object.getColorB()).toBe(3);
});
});
describe('TiledSpriteObject', function() {
it('should expose TiledSpriteObject specific methods', function() {
var object = new gd.TiledSpriteObject('MyTiledSpriteObject');
object.setTexture('MyImageName');
expect(object.getTexture()).toBe('MyImageName');
});
});
describe('ShapePainterObject', function() {
it('should expose ShapePainterObject specific methods', function() {
var object = new gd.ShapePainterObject('MyShapePainterObject');
object.setCoordinatesAbsolute();
expect(object.areCoordinatesAbsolute()).toBe(true);
object.setCoordinatesRelative();
expect(object.areCoordinatesAbsolute()).toBe(false);
});
});
describe('TextEntryObject', function() {
it('should expose TextEntryObject', function() {
var object = new gd.TextEntryObject('MyTextEntryObject');
});
});
describe('JsCodeEvent', function() {
it('can store its code', function() {
var event = new gd.JsCodeEvent();
event.setInlineCode('console.log("Hello world");');
expect(event.getInlineCode()).toBe('console.log("Hello world");');
});
it('can store the objects to pass as parameter', function() {
var event = new gd.JsCodeEvent();
event.setParameterObjects('MyObject');
expect(event.getParameterObjects()).toBe('MyObject');
});
it('can be cloned', function() {
var event = new gd.JsCodeEvent();
event.setInlineCode('console.log("Hello world 2");');
event.setParameterObjects('MyObject2');
var cloneEvent = event.clone();
expect(cloneEvent.getParameterObjects()).toBe('MyObject2');
expect(cloneEvent.getInlineCode()).toBe('console.log("Hello world 2");');
});
});
});