newgrounds io shit incoming

This commit is contained in:
Cameron Taylor
2020-10-31 20:42:14 -07:00
parent b16b463dad
commit 321a7d22f9
35 changed files with 3049 additions and 1 deletions

View File

@@ -0,0 +1,23 @@
package io.newgrounds.swf;
import openfl.display.MovieClip;
class LoadingBar extends MovieClip {
public var bar(default, null):MovieClip;
public function new() {
super();
setProgress(0.0);
}
/**
*
* @param value The ratio of bytes loaded to bytes total
*/
public function setProgress(value:Float):Void {
bar.gotoAndStop(1 + Std.int(value * (bar.totalFrames - 1)));
}
}

View File

@@ -0,0 +1,151 @@
package io.newgrounds.swf;
import io.newgrounds.swf.common.BaseAsset;
import io.newgrounds.objects.Medal;
import openfl.text.TextFieldAutoSize;
import openfl.text.TextField;
import openfl.display.DisplayObject;
import openfl.display.Loader;
import openfl.display.MovieClip;
import openfl.net.URLRequest;
import openfl.events.Event;
class MedalPopup extends BaseAsset {
static inline var FRAME_HIDDEN:String = "hidden";
static inline var FRAME_MEDAL_UNLOCKED:String = "medalUnlocked";
static inline var FRAME_INTRO_COMPLETE:String = "introComplete";
static inline var FRAME_UNLOCK_COMPLETE:String = "unlockComplete";
static inline var MIN_TEXT_SIZE:Int = 12;
public var medalIcon(default, null):MovieClip;
public var medalName(default, null):MovieClip;
public var medalPoints(default, null):MovieClip;
public var alwaysOnTop:Bool;
#if !ng_lite
public var requiresSession:Bool;
#end
var _animQueue = new Array<Void->Void>();
var _scrollSpeed:Float;
public function new() {
super();
mouseEnabled = false;
mouseChildren = false;
hide();
addFrameScript(totalFrames - 1, onUnlockAnimComplete);
}
function hide():Void {
visible = false;
gotoAndStop(FRAME_HIDDEN);
}
#if !ng_lite
override function onReady():Void {
super.onReady();
if (NG.core.medals != null)
onMedalsLoaded();
else
NG.core.onLogin.addOnce(NG.core.requestMedals.bind(onMedalsLoaded));
}
function onMedalsLoaded():Void {
for (medal in NG.core.medals)
medal.onUnlock.add(onMedalOnlock.bind(medal));
}
function onMedalOnlock(medal:Medal):Void {
if (requiresSession && !NG.core.loggedIn)
return;
var loader = new Loader();
loader.load(new URLRequest(medal.icon));
playAnim(loader, medal.name, medal.value);
}
#end
public function playAnim(icon:DisplayObject, name:String, value:Int):Void {
if (currentLabel == FRAME_HIDDEN)
playNextAnim(icon, name, value);
else
_animQueue.push(playNextAnim.bind(icon, name, value));
}
function playNextAnim(icon:DisplayObject, name:String, value:Int):Void {
visible = true;
gotoAndPlay(FRAME_MEDAL_UNLOCKED);
if (alwaysOnTop && parent != null) {
parent.setChildIndex(this, parent.numChildren - 1);
}
while(medalIcon.numChildren > 0)
medalIcon.removeChildAt(0);
cast(medalPoints.getChildByName("field"), TextField).text = Std.string(value);
var field:TextField = cast medalName.getChildByName("field");
field.autoSize = TextFieldAutoSize.LEFT;
field.x = 0;
field.text = "";
var oldWidth = medalName.width;
field.text = name;
_scrollSpeed = 0;
if (field.width > oldWidth + 4) {
field.x = oldWidth + 4;
initScroll(field);
}
medalIcon.addChild(icon);
}
function initScroll(field:TextField):Void {
//TODO: Find out why scrollrect didn't work
var animDuration = 0;
for (frame in currentLabels){
if (frame.name == FRAME_INTRO_COMPLETE )
animDuration -= frame.frame;
else if (frame.name == FRAME_UNLOCK_COMPLETE)
animDuration += frame.frame;
}
_scrollSpeed = (field.width + field.x + 4) / animDuration;
field.addEventListener(Event.ENTER_FRAME, updateScroll);
}
function updateScroll(e:Event):Void{
if (currentLabel == FRAME_INTRO_COMPLETE)
cast (e.currentTarget, TextField).x -= _scrollSpeed;
}
function onUnlockAnimComplete():Void {
cast (medalName.getChildByName("field"), TextField).removeEventListener(Event.ENTER_FRAME, updateScroll);
if (_animQueue.length == 0)
hide();
else
(_animQueue.shift())();
}
}

View File

@@ -0,0 +1,250 @@
package io.newgrounds.swf;
import openfl.events.Event;
import io.newgrounds.swf.common.DropDown;
import io.newgrounds.objects.Score;
import io.newgrounds.objects.events.Result.ScoreBoardResult;
import io.newgrounds.objects.events.Result.ScoreResult;
import io.newgrounds.objects.events.Response;
import io.newgrounds.swf.common.BaseAsset;
import io.newgrounds.swf.common.Button;
import io.newgrounds.components.ScoreBoardComponent.Period;
import openfl.display.MovieClip;
import openfl.text.TextField;
class ScoreBrowser extends BaseAsset {
public var prevButton (default, null):MovieClip;
public var nextButton (default, null):MovieClip;
public var reloadButton (default, null):MovieClip;
public var listBox (default, null):MovieClip;
public var loadingIcon (default, null):MovieClip;
public var errorIcon (default, null):MovieClip;
public var scoreContainer(default, null):MovieClip;
public var titleField (default, null):TextField;
public var pageField (default, null):TextField;
public var period(get, set):Period;
function get_period():Period { return _periodDropDown.value; }
function set_period(value:Period):Period { return _periodDropDown.value = value; }
public var title(get, set):String;
function get_title():String { return titleField.text; }
function set_title(value:String):String { return titleField.text = value; }
public var tag(default, set):String;
function set_tag(value:String):String {
if (this.tag != value) {
this.tag = value;
delayReload();
}
return value;
}
public var social(default, set):Bool;
function set_social(value:Bool):Bool {
if (this.social != value) {
this.social = value;
delayReload();
}
return value;
}
public var boardId(default, set):Int;
function set_boardId(value:Int):Int {
_boardIDSet = true;
if (this.boardId != value) {
this.boardId = value;
delayReload();
}
return value;
}
public var page(default, set):Int;
function set_page(value:Int):Int {
if (this.page != value) {
this.page = value;
delayReload();
}
return value;
}
var _scores:Array<MovieClip>;
var _limit:Int = 0;
var _periodDropDown:DropDown;
var _boardIDSet:Bool;
public function new() { super(); }
override function setDefaults():Void {
super.setDefaults();
boardId = -1;
_boardIDSet = false;
scoreContainer.visible = false;
loadingIcon.visible = false;
reloadButton.visible = false;
errorIcon.visible = false;
errorIcon.addFrameScript(errorIcon.totalFrames - 1, errorIcon.stop);
//TODO: prevent memory leaks?
new Button(prevButton, onPrevClick);
new Button(nextButton, onNextClick);
new Button(reloadButton, reload);
_periodDropDown = new DropDown(listBox, delayReload);
_periodDropDown.addItem("Current day" , Period.DAY );
_periodDropDown.addItem("Current week" , Period.WEEK );
_periodDropDown.addItem("Current month", Period.MONTH);
_periodDropDown.addItem("Current year" , Period.YEAR );
_periodDropDown.addItem("All time" , Period.ALL );
_periodDropDown.value = Period.ALL;
_scores = new Array<MovieClip>();
while(true) {
var score:MovieClip = cast scoreContainer.getChildByName('score${_scores.length}');
if (score == null)
break;
new Button(score);
_scores.push(score);
}
_limit = _scores.length;
}
override function onReady():Void {
super.onReady();
if (boardId == -1 && !_boardIDSet) {
#if ng_lite
NG.core.calls.scoreBoard.getBoards()
.addDataHandler(onBoardsRecieved)
.queue();
#else
if (NG.core.scoreBoards != null)
onBoardsLoaded();
else
NG.core.requestScoreBoards(onBoardsLoaded);
#end
}
reload();
}
#if ng_lite
function onBoardsRecieved(response:Response<ScoreBoardResult>):Void {
if (response.success && response.result.success) {
for (board in response.result.data.scoreboards) {
NG.core.log('No boardId specified defaulting to ${board.name}');
boardId = board.id;
return;
}
}
}
#else
function onBoardsLoaded():Void {
for (board in NG.core.scoreBoards) {
NG.core.log('No boardId specified defaulting to ${board.name}');
boardId = board.id;
return;
}
}
#end
/** Used internally to avoid multiple server requests from various property changes in a small time-frame. **/
function delayReload():Void {
addEventListener(Event.EXIT_FRAME, onDelayComplete);
}
function onDelayComplete(e:Event):Void { reload(); }
public function reload():Void {
removeEventListener(Event.EXIT_FRAME, onDelayComplete);
errorIcon.visible = false;
scoreContainer.visible = false;
pageField.text = 'page ${page + 1}';
if (_coreReady && boardId != -1 && _limit > 0 && period != null) {
loadingIcon.visible = true;
NG.core.calls.scoreBoard.getScores(boardId, _limit, _limit * page, period, social, tag)
.addDataHandler(onScoresReceive)
.send();
}
}
function onScoresReceive(response:Response<ScoreResult>):Void {
loadingIcon.visible = false;
if (response.success && response.result.success) {
scoreContainer.visible = true;
var i = _limit;
while(i > 0) {
i--;
if (i < response.result.data.scores.length)
drawScore(i, response.result.data.scores[i], _scores[i]);
else
drawScore(i, null, _scores[i]);
}
} else {
errorIcon.visible = true;
errorIcon.gotoAndPlay(1);
reloadButton.visible = true;
}
}
inline function drawScore(rank:Int, score:Score, asset:MovieClip):Void {
if (score == null)
asset.visible = false;
else {
asset.visible = true;
cast (asset.getChildByName("nameField" ), TextField).text = score.user.name;
cast (asset.getChildByName("scoreField"), TextField).text = score.formatted_value;
cast (asset.getChildByName("rankField" ), TextField).text = Std.string(rank + 1);
}
}
function onPrevClick():Void {
if (page > 0)
page--;
}
function onNextClick():Void {
page++;
}
}

View File

@@ -0,0 +1,35 @@
package io.newgrounds.swf.common;
import openfl.events.Event;
import openfl.display.MovieClip;
class BaseAsset extends MovieClip {
var _coreReady:Bool = false;
public function new() {
super();
setDefaults();
if (stage != null)
onAdded(null);
else
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
function setDefaults():Void { }
function onAdded(e:Event):Void {
if (NG.core != null)
onReady();
else
NG.onCoreReady.add(onReady);
}
function onReady():Void {
_coreReady = true;
}
}

View File

@@ -0,0 +1,151 @@
package io.newgrounds.swf.common;
import openfl.display.Stage;
import openfl.events.Event;
import openfl.events.MouseEvent;
import openfl.display.MovieClip;
class Button {
var _enabled:Bool;
public var enabled(get, set):Bool;
function get_enabled():Bool { return _enabled; }
function set_enabled(value:Bool):Bool {
if (value != _enabled) {
_enabled = value;
updateEnabled();
}
return value;
}
public var onClick:Void->Void;
public var onOver:Void->Void;
public var onOut:Void->Void;
var _target:MovieClip;
var _down:Bool;
var _over:Bool;
var _foundLabels:Array<String>;
public function new(target:MovieClip, onClick:Void->Void = null, onOver:Void->Void = null, onOut:Void->Void = null) {
_target = target;
this.onClick = onClick;
this.onOver = onOver;
this.onOut = onOut;
_foundLabels = new Array<String>();
for (label in _target.currentLabels)
_foundLabels.push(label.name);
_target.stop();
_target.addEventListener(Event.ADDED_TO_STAGE, onAdded);
if (target.stage != null)
onAdded(null);
enabled = true;
}
function onAdded(e:Event):Void {
var stage = _target.stage;
stage.addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
_target.addEventListener(MouseEvent.MOUSE_OVER, mouseHandler);
_target.addEventListener(MouseEvent.MOUSE_OUT, mouseHandler);
_target.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
_target.addEventListener(MouseEvent.CLICK, mouseHandler);
function selfRemoveEvent(e:Event):Void {
_target.removeEventListener(Event.REMOVED_FROM_STAGE, selfRemoveEvent);
onRemove(e, stage);
}
_target.addEventListener(Event.REMOVED_FROM_STAGE, selfRemoveEvent);
}
function onRemove(e:Event, stage:Stage):Void {
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseHandler);
_target.removeEventListener(MouseEvent.MOUSE_OVER, mouseHandler);
_target.removeEventListener(MouseEvent.MOUSE_OUT, mouseHandler);
_target.removeEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
_target.removeEventListener(MouseEvent.CLICK, mouseHandler);
}
function mouseHandler(event:MouseEvent):Void {
switch(event.type) {
case MouseEvent.MOUSE_OVER:
_over = true;
if (onOver != null)
onOver();
case MouseEvent.MOUSE_OUT:
_over = false;
if (onOut != null)
onOut();
case MouseEvent.MOUSE_DOWN:
_down = true;
case MouseEvent.MOUSE_UP:
_down = false;
case MouseEvent.CLICK:
if (enabled && onClick != null)
onClick();
}
updateState();
}
function updateEnabled():Void {
updateState();
_target.useHandCursor = enabled;
_target.buttonMode = enabled;
}
function updateState():Void {
var state = determineState();
if (_target.currentLabel != state && _foundLabels.indexOf(state) != -1)
_target.gotoAndStop(state);
}
function determineState():String {
if (enabled) {
if (_over)
return _down ? "down" : "over";
return "up";
}
return "disabled";
}
public function destroy():Void {
_target.removeEventListener(Event.ADDED_TO_STAGE, onAdded);
_target = null;
onClick = null;
onOver = null;
onOut = null;
_foundLabels = null;
}
}

View File

@@ -0,0 +1,88 @@
package io.newgrounds.swf.common;
import haxe.ds.StringMap;
import openfl.display.MovieClip;
import openfl.display.Sprite;
import openfl.text.TextField;
class DropDown {
public var value(default, set):String;
function set_value(v:String):String {
if (this.value == v)
return v;
this.value = v;
_selectedLabel.text = _values.get(v);
if (_onChange != null)
_onChange();
return v;
}
var _choiceContainer:Sprite;
var _selectedLabel:TextField;
var _onChange:Void->Void;
var _values:StringMap<String>;
var _unusedChoices:Array<MovieClip>;
public function new(target:MovieClip, label:String = "", onChange:Void->Void = null) {
_onChange = onChange;
_selectedLabel = cast cast(target.getChildByName("currentItem"), MovieClip).getChildByName("label");
_selectedLabel.text = label;
_values = new StringMap<String>();
new Button(cast target.getChildByName("button"), onClickExpand);
new Button(cast target.getChildByName("currentItem"), onClickExpand);
_choiceContainer = new Sprite();
_choiceContainer.visible = false;
target.addChild(_choiceContainer);
_unusedChoices = new Array<MovieClip>();
while(true) {
var item:MovieClip = cast target.getChildByName('item${_unusedChoices.length}');
if (item == null)
break;
target.removeChild(item);
_unusedChoices.push(item);
}
}
public function addItem(name:String, value:String):Void {
_values.set(value, name);
if (_unusedChoices.length == 0) {
NG.core.logError('cannot create another dropBox item max=${_choiceContainer.numChildren}');
return;
}
var button = _unusedChoices.shift();
cast(button.getChildByName("label"), TextField).text = name;
_choiceContainer.addChild(button);
new Button(button, onChoiceClick.bind(value));
}
function onClickExpand():Void {
_choiceContainer.visible = !_choiceContainer.visible;
}
function onChoiceClick(name:String):Void {
value = name;
_choiceContainer.visible = false;
}
}