/
MenuState.hx
551 lines (447 loc) · 15.4 KB
/
MenuState.hx
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.text.TextFieldType;
import flash.utils.ByteArray;
import flixel.addons.api.FlxGameJolt;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.group.FlxGroup;
import flixel.text.FlxText;
import flixel.text.FlxTextField;
import openfl.utils.ByteArray;
/**
* These lines allow embedding of assets as ByteArrays, which helps to minimize the threat of data being compromised.
* For your own purposes, it is recommended you add "*.privatekey" to your source control ignore list.
* The content of the .privatekey file should be just your private key.
* To see how this file is read and used, look at the bottom of the create() function below.
*/
@:file("assets/example.privatekey") class MyPrivateKey extends #if (lime_legacy || openfl <= "3.4.0") ByteArray #else ByteArrayData #end {}
class MenuState extends FlxState
{
private var _connection:FlxText;
private var _return:FlxText;
private var _main:FlxGroup;
private var _highScores:FlxGroup;
private var _apiTest:FlxGroup;
private var _loginGroup:FlxGroup;
private var _allScreens:FlxGroup;
private var _apiPages:Array<FlxGroup>;
private var _login:Button;
private var _apiCurrentPage:Int;
private var _mainMenuTime:Float = 0.0;
private var _input1:FlxTextField;
private var _input2:FlxTextField;
private var _imageDisplay:FlxSprite;
private static inline function API_TEST_BUTTONS():Array<Array<String>>
{
return [["fetchUser", "authUser", "openSession", "pingSession"],
["closeSession", "fetchTrophy", "addTrophy", "fetchScore"],
["addScore", "getTables", "fetchData", "setData"],
["updateData", "removeData", "getAllKeys", "resetUser"],
["fetchTrophyImage", "fetchAvatarImage", "username", "usertoken"],
["isQuickPlay", "isEmbeddedFlash"]];
}
override public function create():Void
{
Reg.genColors();
FlxG.cameras.bgColor = Reg.lite;
Reg.level = 1;
#if !FLX_NO_MOUSE
var mouseSprite:Sprite = new Sprite();
mouseSprite.graphics.beginFill(Reg.dark, 1);
mouseSprite.graphics.moveTo(0, 0);
mouseSprite.graphics.lineTo(20, 20);
mouseSprite.graphics.lineTo(0, 27.5);
mouseSprite.graphics.endFill();
// using .show( mouseSprite ) doesn't work, so we convert it to bitmapdata
var mouseData:BitmapData = new BitmapData(20, 30, true, 0);
mouseData.draw(mouseSprite);
FlxG.mouse.load(mouseData);
FlxG.mouse.visible = true;
#end
// The background emitter, connection info, version, and blurb are always present.
var em:Emitter = new Emitter(Std.int(FlxG.width / 2), Std.int(FlxG.height / 2), 4);
_connection = new FlxText(1, 1, FlxG.width, "Connecting to GameJolt...");
_connection.color = Reg.med_lite;
var info:FlxText = new FlxText(0, FlxG.height - 14, FlxG.width, "FlxPong is a demo of HaxeFlixel & FlxGameJolt, which interacts with the GameJolt API.");
info.color = Reg.med_lite;
info.alignment = CENTER;
var ver:FlxText = new FlxText(0, 0, FlxG.width, Reg.VERSION);
ver.color = Reg.med_lite;
ver.alignment = RIGHT;
add(em);
add(_connection);
add(info);
add(ver);
// Set up the "main" screen/buttons.
_main = new FlxGroup();
var title:FlxText = new FlxText(0, 20, FlxG.width, "FlxPong!", 16);
title.color = Reg.med_dark;
title.alignment = CENTER;
var play:Button = new Button(0, 50, "Play!", playCallback);
Reg.quarterX(play, 2);
_login = new Button(0, 50, "Log in", switchMenu, 60);
Reg.quarterX(_login, 3);
_login.visible = false;
_login.active = false;
var test:Button = new Button(0, 80, "API Functions", switchMenu);
Reg.quarterX(test, 1);
var high:Button = new Button(0, 80, "High Scores", scoresCallback);
Reg.quarterX(high, 2);
var mine:Button = new Button(0, 80, "My Scores", mineCallback);
Reg.quarterX(mine, 3);
var source:Button = new Button(0, 110, "FlxGameJolt Source", sourceCallback, 120);
Reg.quarterX(source, 1);
var hf:Button = new Button(0, 110, "HaxeFlixel.com", hfCallback);
Reg.quarterX(hf, 2);
var doc:Button = new Button(0, 110, "GameJolt API Doc", docCallback, 120);
Reg.quarterX(doc, 3);
_main.add(title);
_main.add(play);
_main.add(_login);
_main.add(test);
//_main.add( high );
//_main.add( mine );
_main.add(source);
_main.add(hf);
_main.add(doc);
// End main group.
// Set up the "high scores" screen.
_highScores = new FlxGroup();
// End high scores.
// Set up the API test screen.
_apiTest = new FlxGroup();
var xpos:Int = 2;
var ypos:Array<Int> = [20, 42, 64, 86, 108, 130];
var buttonwidth:Int = 100;
// Set up the pages of this screen.
_apiPages = [];
_apiCurrentPage = 0;
for (i in 0...API_TEST_BUTTONS().length)
{
_apiPages.push(new FlxGroup());
var button1:Button;
var button2:Button;
var button3:Button;
var button4:Button;
if (API_TEST_BUTTONS()[i][0] != null)
{
button1 = new Button(xpos, ypos[0], API_TEST_BUTTONS()[i][0], apiCallback, buttonwidth);
_apiPages[i].add(button1);
}
if (API_TEST_BUTTONS()[i][1] != null)
{
button2 = new Button(xpos, ypos[1], API_TEST_BUTTONS()[i][1], apiCallback, buttonwidth);
_apiPages[i].add(button2);
}
if (API_TEST_BUTTONS()[i][2] != null)
{
button3 = new Button(xpos, ypos[2], API_TEST_BUTTONS()[i][2], apiCallback, buttonwidth);
_apiPages[i].add(button3);
}
if (API_TEST_BUTTONS()[i][3] != null)
{
button4 = new Button(xpos, ypos[3], API_TEST_BUTTONS()[i][3], apiCallback, buttonwidth);
_apiPages[i].add(button4);
}
_apiPages[i].visible = false;
_apiPages[i].active = false;
}
// We do want to see the first page, once apiTest is added
_apiPages[0].visible = true;
_apiPages[0].active = true;
// Add elements aside from the per-screen buttons
var prev:Button = new Button(xpos, ypos[4], "<<", testMove, Std.int(buttonwidth / 2 - xpos / 2));
var next:Button = new Button(Std.int(prev.x + prev.width + 2), ypos[4], ">>", testMove, Std.int(buttonwidth / 2 - xpos / 2));
var testSpace:PongSprite = new PongSprite(xpos + buttonwidth + xpos, ypos[0], FlxG.width - (xpos + buttonwidth + xpos * 2), ypos[4] + 20 - ypos[0], Reg.med_lite);
_return = new FlxText(testSpace.x + 4, testSpace.y + 4, Std.int(testSpace.width - 8), "Return data will display here.");
_return.color = Reg.lite;
_imageDisplay = new FlxSprite(testSpace.x + 5, testSpace.y + 5);
_imageDisplay.visible = false;
var exit:Button = new Button(Std.int(testSpace.x + testSpace.width - 40), Std.int(testSpace.y + testSpace.height - 20), "Back", switchMenu, 40);
// Add everything to this screen
for (g in _apiPages)
{
_apiTest.add(g);
}
_apiTest.add(prev);
_apiTest.add(next);
_apiTest.add(testSpace);
_apiTest.add(_return);
_apiTest.add(_imageDisplay);
_apiTest.add(exit);
_apiTest.active = false;
_apiTest.visible = false;
// End API test.
// Login screen
_loginGroup = new FlxGroup();
var instruct:FlxText = new FlxText(0, 50, FlxG.width, "Log in to GameJolt to get trophies and stuff:");
instruct.alignment = CENTER;
instruct.color = Reg.med_dark;
var word1:FlxText = new FlxText(0, 70, 60, "Username:");
var word2:FlxText = new FlxText(0, 90, 60, "Token:");
Reg.quarterX(word1, 1);
Reg.quarterX(word2, 1);
word1.color = word2.color = Reg.med_dark;
_input1 = new FlxTextField(0, 70, 240, " ");
_input2 = new FlxTextField(0, 90, 240, " ");
Reg.quarterX(_input1, 3);
Reg.quarterX(_input2, 3);
_input2.color = _input1.color = Reg.med_lite;
_input2.textField.selectable = _input1.textField.selectable = true;
_input2.textField.multiline = _input1.textField.multiline = false;
_input2.textField.wordWrap = _input1.textField.wordWrap = false;
_input2.textField.maxChars = _input1.textField.maxChars = 30;
#if flash
_input2.textField.restrict = _input1.textField.restrict = "A-Za-z0-9_";
#end
_input2.textField.type = _input1.textField.type = TextFieldType.INPUT;
var input1bg:PongSprite = new PongSprite(Std.int(_input1.x), Std.int(_input1.y), Std.int(_input1.width - 40), Std.int(_input1.height + 4), Reg.dark);
var input2bg:PongSprite = new PongSprite(Std.int(_input2.x), Std.int(_input2.y), Std.int(_input2.width - 40), Std.int(_input2.height + 4), Reg.dark);
#if desktop
_input1.height = input1bg.height;
_input2.height = input2bg.height;
#end
var trylogin:Button = new Button(0, 110, "Log in", loginCallback);
Reg.quarterX(trylogin, 2);
var back:Button = new Button(400, 108, "Back", switchMenu, 40);
_loginGroup.add(word1);
_loginGroup.add(word2);
_loginGroup.add(input1bg);
_loginGroup.add(input2bg);
_loginGroup.add(_input1);
_loginGroup.add(_input2);
_loginGroup.add(instruct);
_loginGroup.add(trylogin);
_loginGroup.add(back);
_loginGroup.active = false;
_loginGroup.visible = false;
// End Login.
_allScreens = new FlxGroup();
_allScreens.add(_main);
_allScreens.add(_apiTest);
_allScreens.add(_loginGroup);
add(_allScreens);
em.start(false);
// Load the privatekey data as a bytearray.
var ba:ByteArray = new MyPrivateKey();
// If we're already initialized (which would happen on returning from the playstate), we don't need to run init().
// If we're not initialized, call init() using the game ID and the private key, which is converted to a string
// with .readUTFBytes( ba.length ). The ba.length ensures that the ByteArray will be read from beginning to end
// and then stop; otherwise, there would be an error when the end of the ByteArray was reached.
if (!FlxGameJolt.initialized)
{
FlxGameJolt.init(19975, ba.readUTFBytes(ba.length), true, null, null, initCallback);
}
else
{
_connection.text = "Welcome back to the main menu, " + FlxGameJolt.username + "!";
}
super.create();
}
override public function update(elapsed:Float):Void
{
_mainMenuTime += elapsed;
#if !FLX_NO_KEYBOARD
if (FlxG.keys.justPressed.ENTER && _loginGroup.visible)
{
loginCallback("Login");
}
#end
super.update(elapsed);
}
#if debug
private function colorCallback(Name:String):Void
{
Reg.genColors();
FlxG.switchState(new MenuState());
}
#end
private function playCallback(Name:String):Void
{
FlxG.switchState(new PlayState());
}
private function hfCallback(Name:String):Void
{
FlxG.openURL("http://www.haxeflixel.com");
}
private function sourceCallback(Name:String):Void
{
FlxG.openURL("https://github.com/HaxeFlixel/flixel-addons/blob/master/flixel/addons/api/FlxGameJolt.hx");
}
private function docCallback(Name:String):Void
{
FlxG.openURL("http://gamejolt.com/api/doc/game/");
}
private function scoresCallback(Name:String):Void
{
// stuff
}
private function switchMenu(Name:String):Void
{
if (_loginGroup.visible)
{
_input1.text = " ";
_input2.text = " ";
}
for (g in _allScreens)
{
g.visible = false;
g.active = false;
}
if (Name == "Back")
{
_main.visible = true;
_main.active = true;
}
if (Name == "API Functions")
{
_apiTest.visible = true;
_apiTest.active = true;
}
if (Name == "Log in")
{
_loginGroup.visible = true;
_loginGroup.active = true;
}
}
private function loginCallback(Name:String):Void
{
_connection.text = "Attempting to log in...";
FlxGameJolt.authUser(StringTools.trim(_input1.text), StringTools.trim(_input2.text), initCallback);
}
private function mineCallback(Name:String):Void
{
}
private function apiCallback(Name:String):Void
{
_imageDisplay.visible = false;
_return.text = "Sending " + Name + " request to GameJolt...";
switch (Name)
{
case "fetchUser":
FlxGameJolt.fetchUser(0, FlxGameJolt.username, [], apiReturn);
case "authUser":
FlxGameJolt.authUser(FlxGameJolt.username, FlxGameJolt.usertoken, authReturn);
case "openSession":
FlxGameJolt.openSession(apiReturn);
case "pingSession":
FlxGameJolt.pingSession(true, apiReturn);
case "closeSession":
FlxGameJolt.closeSession(apiReturn);
case "fetchTrophy":
FlxGameJolt.fetchTrophy(0, apiReturn);
case "addTrophy":
FlxGameJolt.addTrophy(5079, apiReturn);
case "fetchScore":
FlxGameJolt.fetchScore(10, apiReturn);
case "addScore":
FlxGameJolt.addScore(Std.string(Math.round(_mainMenuTime)) + "secondsinmenu", Math.round(_mainMenuTime), 0, false, null, "FlxPongRox", apiReturn);
case "getTables":
FlxGameJolt.getTables(apiReturn);
case "fetchData":
FlxGameJolt.fetchData("testkey", true, apiReturn);
case "setData":
FlxGameJolt.setData("testkey", "IheartBACON", true, apiReturn);
case "updateData":
FlxGameJolt.updateData("testkey", "append", "andSAUSAGE", true, apiReturn);
case "removeData":
FlxGameJolt.removeData("testkey", true, apiReturn);
case "getAllKeys":
FlxGameJolt.getAllKeys(true, apiReturn);
case "resetUser":
FlxGameJolt.resetUser(FlxGameJolt.username, FlxGameJolt.usertoken, authReturn);
case "fetchTrophyImage":
FlxGameJolt.fetchTrophyImage(5072, apiImageReturn);
case "fetchAvatarImage":
FlxGameJolt.fetchAvatarImage(apiImageReturn);
case "username":
_return.text = "User name: " + FlxGameJolt.username;
case "usertoken":
_return.text = "User token: " + FlxGameJolt.usertoken;
case "isQuickPlay":
_return.text = "Was FlxPong loaded via Quick Play? Status: " + FlxGameJolt.isQuickPlay;
case "isEmbeddedFlash":
_return.text = "Was FlxPong loaded as embedded Flash on GameJolt? Status: " + FlxGameJolt.isEmbeddedFlash;
default:
_return.text = "Sorry, there was an error. :(";
}
}
private function apiReturn(ReturnMap:Map<String,String>):Void
{
_return.text = "Received from GameJolt:\n" + ReturnMap.toString();
}
private function apiImageReturn(Bits:BitmapData):Void
{
_return.text = "";
_imageDisplay.loadGraphic(Bits);
_imageDisplay.visible = true;
}
private function authReturn(Success:Bool):Void
{
_return.text = "The user authentication returned: " + Success;
if (!Success)
{
_return.text += ". This is probably because the user is already authenticated! You can use resetUser() to authenticate a new user.";
}
}
private function testMove(Name:String):Void
{
_apiPages[_apiCurrentPage].visible = false;
_apiPages[_apiCurrentPage].active = false;
if (Name.charCodeAt(0) == 60)
{
_apiCurrentPage--;
}
else if (Name.charCodeAt(0) == 62)
{
_apiCurrentPage++;
}
if (_apiCurrentPage < 0)
_apiCurrentPage = _apiPages.length - 1;
if (_apiCurrentPage > _apiPages.length - 1)
_apiCurrentPage = 0;
_apiPages[_apiCurrentPage].visible = true;
_apiPages[_apiCurrentPage].active = true;
}
private function initCallback(Result:Bool):Void
{
if (_connection != null)
{
if (Result)
{
if (_connection != null)
{
_connection.text = "Successfully connected to GameJolt! Hi " + FlxGameJolt.username + "!";
}
FlxGameJolt.addTrophy(5072);
if (_login.visible)
{
_login.visible = false;
_login.active = false;
}
if (_loginGroup.visible == true)
{
switchMenu("Back");
}
//FlxGameJolt.fetchAvatarImage( avatarCallback );
}
else
{
if (_connection != null)
{
_connection.text = "Unable to verify your information with GameJolt.";
}
_login.visible = true;
_login.active = true;
}
}
}
public function createToast(ReturnMap:Dynamic):Void
{
var toast:Toast = new Toast(ReturnMap.get("id"));
add(toast);
}
}