Skip to content

Commit

Permalink
backend rework
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Sep 22, 2020
1 parent 4fe474c commit 561a3b1
Show file tree
Hide file tree
Showing 93 changed files with 1,849 additions and 1,755 deletions.
Empty file added extraParams.hxml
Empty file.
4 changes: 2 additions & 2 deletions haxe/ui/backend/AppImpl.hx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package haxe.ui.backend;

import flixel.FlxGame;
import haxe.ui.backend.flixel.FlxHaxeUIState;
import haxe.ui.backend.flixel.FlxHaxeUIAppState;
import openfl.Lib;

class AppImpl extends AppBase {
public function new() {
}

private override function build() {
Lib.current.stage.addChild(new FlxGame(0, 0, FlxHaxeUIState, 1, 60, 60, true));
Lib.current.stage.addChild(new FlxGame(0, 0, FlxHaxeUIAppState, 1, 60, 60, true));
}
}
139 changes: 61 additions & 78 deletions haxe/ui/backend/AssetsImpl.hx
Original file line number Diff line number Diff line change
@@ -1,109 +1,92 @@
package haxe.ui.backend;

import flash.display.Bitmap;
import flash.display.Loader;
import flash.events.Event;
import flash.utils.ByteArray;
import flixel.graphics.FlxGraphic;
import flixel.graphics.frames.FlxFrame;
import flixel.graphics.frames.FlxFramesCollection;
import flixel.graphics.frames.FlxImageFrame;
import haxe.io.Bytes;
import haxe.ui.assets.FontInfo;
import haxe.ui.assets.ImageInfo;
import haxe.ui.util.ByteConverter;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.Loader;
import openfl.events.Event;
import openfl.utils.AssetType;
import openfl.utils.ByteArray;

class AssetsImpl extends AssetsBase {

var frames(get, never):FlxFramesCollection;

function get_frames():FlxFramesCollection {
if (Toolkit.assets.options != null && Toolkit.assets.options.spritesheet != null) return Toolkit.assets.options.spritesheet;
return null;
}

override function getImageInternal(resourceId:String, callback:ImageInfo->Void):Void {

private override function getImageInternal(resourceId:String, callback:ImageInfo->Void):Void {
var graphic:FlxGraphic = null;
var frame:FlxFrame = null;

if (Assets.exists(resourceId)) {
graphic = FlxGraphic.fromAssetKey(resourceId);
}

if (graphic == null) {
var fr = frames;
if (fr != null && fr.framesHash.exists(resourceId)) frame = fr.getByName(resourceId);
}

else {
frame = FlxImageFrame.fromGraphic(graphic).frame;
}

if (frame != null) {

if (Assets.exists(resourceId)) {
graphic = FlxGraphic.fromAssetKey(resourceId);
frame = FlxImageFrame.fromGraphic(graphic).frame;
}

if (frame != null) {
frame.parent.persist = true;
frame.parent.destroyOnNoUse = false;
callback( { data : frame, width : Std.int(frame.sourceSize.x), height : Std.int(frame.sourceSize.y) } );
}

else callback(null);
}

override function getImageFromHaxeResource(resourceId:String, callback:String->ImageInfo->Void):Void {
var bytes = Resource.getBytes(resourceId);
imageFromBytes(bytes, callback.bind(resourceId));
}

public override function imageFromBytes(bytes:Bytes, callback:ImageInfo->Void):Void {

callback({
data : frame,
width : Std.int(frame.sourceSize.x),
height : Std.int(frame.sourceSize.y)
});
} else {
callback(null);
}
}

private override function getImageFromHaxeResource(resourceId:String, callback:String->ImageInfo->Void):Void {
if (Resource.listNames().indexOf(resourceId) == -1) {
callback(resourceId, null);
} else {
var bytes = Resource.getBytes(resourceId);
imageFromBytes(bytes, callback.bind(resourceId));
}
}

public override function imageFromBytes(bytes:Bytes, callback:ImageInfo->Void):Void {
var ba:ByteArray = ByteConverter.fromHaxeBytes(bytes);

var loader:Loader = new Loader();

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e) {

if (loader.content != null) {
var frame = FlxImageFrame.fromImage(cast(loader.content, Bitmap).bitmapData).frame;
frame.parent.persist = true; // these two booleans will screw up the UI unless changed from the default values
frame.parent.destroyOnNoUse = false;
callback( { data : frame, width : Std.int(frame.sourceSize.x), height : Std.int(frame.sourceSize.y) } );
}
});

loader.loadBytes(ba);
}

override function getFontInternal(resourceId:String, callback:FontInfo->Void):Void {

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e) {
if (loader.content != null) {
var frame = FlxImageFrame.fromImage(cast(loader.content, Bitmap).bitmapData).frame;
frame.parent.persist = true; // these two booleans will screw up the UI unless changed from the default values
frame.parent.destroyOnNoUse = false;
callback({
data : frame,
width : Std.int(frame.sourceSize.x),
height : Std.int(frame.sourceSize.y)
});
} else {
callback(null);
}
});

loader.loadBytes(ba);
}

private override function getFontInternal(resourceId:String, callback:FontInfo->Void):Void {
var fontName:String = null;

if (isEmbeddedFont(resourceId) && Assets.exists(resourceId, AssetType.FONT)) {
fontName = Assets.getFont(resourceId).fontName;
}

else {
} else {
fontName = resourceId;
}

callback( { data : fontName } );
}

override function getFontFromHaxeResource(resourceId:String, callback:String->FontInfo->Void):Void {
callback(resourceId, null);
callback({
data : fontName
});
}

override function getTextDelegate(resourceId:String):String {


private override function getTextDelegate(resourceId:String):String {
if (Assets.exists(resourceId)) {
return Assets.getText(resourceId);
}

return null;
}
static inline function isEmbeddedFont(fontName:String):Bool {
private static inline function isEmbeddedFont(fontName:String):Bool {
return fontName != "_sans" && fontName != "_serif" && fontName != "_typewriter";
}
}
}
2 changes: 1 addition & 1 deletion haxe/ui/backend/CallLaterImpl.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package haxe.ui.backend;
import flixel.FlxG;

class CallLaterImpl extends TimerImpl {

public function new(callback:Void->Void) {
super(0, callback);
}
Expand Down
Loading

0 comments on commit 561a3b1

Please sign in to comment.