[add] storages
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
package ru.m.puzzlez.render;
|
package ru.m.puzzlez.render;
|
||||||
|
|
||||||
import haxe.Timer;
|
|
||||||
import flash.display.BitmapData;
|
import flash.display.BitmapData;
|
||||||
import flash.display.PNGEncoderOptions;
|
import flash.display.PNGEncoderOptions;
|
||||||
import flash.display.Sprite;
|
import flash.display.Sprite;
|
||||||
@@ -9,10 +8,12 @@ import flash.geom.Point;
|
|||||||
import flash.geom.Rectangle;
|
import flash.geom.Rectangle;
|
||||||
import flash.net.FileReference;
|
import flash.net.FileReference;
|
||||||
import flash.utils.ByteArray;
|
import flash.utils.ByteArray;
|
||||||
|
import haxe.Timer;
|
||||||
import haxework.signal.Signal;
|
import haxework.signal.Signal;
|
||||||
import haxework.view.SpriteView;
|
import haxework.view.SpriteView;
|
||||||
import ru.m.puzzlez.core.GameEvent;
|
import ru.m.puzzlez.core.GameEvent;
|
||||||
import ru.m.puzzlez.core.GameState;
|
import ru.m.puzzlez.core.GameState;
|
||||||
|
import ru.m.puzzlez.storage.ImageStorage;
|
||||||
|
|
||||||
class Render extends SpriteView implements IRender {
|
class Render extends SpriteView implements IRender {
|
||||||
|
|
||||||
@@ -40,6 +41,8 @@ class Render extends SpriteView implements IRender {
|
|||||||
private var activePart:PartView;
|
private var activePart:PartView;
|
||||||
private var activePoint:Point;
|
private var activePoint:Point;
|
||||||
|
|
||||||
|
@:provide var imageStorage:ImageStorage;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
super();
|
super();
|
||||||
progress = new ProgressView();
|
progress = new ProgressView();
|
||||||
@@ -83,7 +86,7 @@ class Render extends SpriteView implements IRender {
|
|||||||
table.graphics.lineStyle();
|
table.graphics.lineStyle();
|
||||||
progress.setProgress(0, state.parts.length);
|
progress.setProgress(0, state.parts.length);
|
||||||
content.addChild(progress.content);
|
content.addChild(progress.content);
|
||||||
RenderUtil.resolveImage(state.preset.image).then(onImageResolved);
|
imageStorage.resolve(state.preset.image).then(onImageResolved);
|
||||||
toUpdate();
|
toUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,24 +7,11 @@ import flash.filters.DropShadowFilter;
|
|||||||
import flash.geom.Matrix;
|
import flash.geom.Matrix;
|
||||||
import flash.geom.Point;
|
import flash.geom.Point;
|
||||||
import flash.geom.Rectangle;
|
import flash.geom.Rectangle;
|
||||||
import haxework.net.ImageLoader;
|
|
||||||
import openfl.Assets;
|
|
||||||
import promhx.Promise;
|
|
||||||
import ru.m.puzzlez.core.ImageSource;
|
|
||||||
import ru.m.puzzlez.core.Part;
|
import ru.m.puzzlez.core.Part;
|
||||||
import ru.m.puzzlez.render.mask.PartMask;
|
import ru.m.puzzlez.render.mask.PartMask;
|
||||||
|
|
||||||
class RenderUtil {
|
class RenderUtil {
|
||||||
|
|
||||||
public static function resolveImage(source:ImageSource):Promise<BitmapData> {
|
|
||||||
return switch source {
|
|
||||||
case ASSET(name):
|
|
||||||
Promise.promise(Assets.getBitmapData(name));
|
|
||||||
case URL(url):
|
|
||||||
new ImageLoader().GET(url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function cropImage(source:BitmapData, rect:Rectangle):BitmapData {
|
public static function cropImage(source:BitmapData, rect:Rectangle):BitmapData {
|
||||||
var image = new BitmapData(Std.int(rect.width), Std.int(rect.height));
|
var image = new BitmapData(Std.int(rect.width), Std.int(rect.height));
|
||||||
var matrix = new Matrix();
|
var matrix = new Matrix();
|
||||||
|
|||||||
21
src/haxe/ru/m/puzzlez/storage/AssetStorage.hx
Normal file
21
src/haxe/ru/m/puzzlez/storage/AssetStorage.hx
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package ru.m.puzzlez.storage;
|
||||||
|
|
||||||
|
import openfl.Assets;
|
||||||
|
import openfl.utils.AssetType;
|
||||||
|
import promhx.Promise;
|
||||||
|
import ru.m.puzzlez.core.ImageSource;
|
||||||
|
|
||||||
|
@:provide class AssetStorage implements ISourceStorage {
|
||||||
|
|
||||||
|
var data:Promise<Array<ImageSource>>;
|
||||||
|
|
||||||
|
public function new() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolve(?type:String):Promise<Array<ImageSource>> {
|
||||||
|
if (data == null) {
|
||||||
|
data = Promise.promise([for (name in Assets.list(AssetType.IMAGE).filter(function(name:String) return name.substr(0, 15) == "resources/image")) ASSET(name)]);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/haxe/ru/m/puzzlez/storage/ISourceStorage.hx
Normal file
8
src/haxe/ru/m/puzzlez/storage/ISourceStorage.hx
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package ru.m.puzzlez.storage;
|
||||||
|
|
||||||
|
import ru.m.puzzlez.core.ImageSource;
|
||||||
|
import promhx.Promise;
|
||||||
|
|
||||||
|
interface ISourceStorage {
|
||||||
|
public function resolve(?type:String):Promise<Array<ImageSource>>;
|
||||||
|
}
|
||||||
66
src/haxe/ru/m/puzzlez/storage/ImageStorage.hx
Normal file
66
src/haxe/ru/m/puzzlez/storage/ImageStorage.hx
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package ru.m.puzzlez.storage;
|
||||||
|
|
||||||
|
import flash.display.Bitmap;
|
||||||
|
import flash.display.BitmapData;
|
||||||
|
import flash.display.Loader;
|
||||||
|
import flash.display.LoaderInfo;
|
||||||
|
import flash.display.PNGEncoderOptions;
|
||||||
|
import flash.events.Event;
|
||||||
|
import flash.geom.Rectangle;
|
||||||
|
import flash.utils.ByteArray;
|
||||||
|
import haxework.net.ImageLoader;
|
||||||
|
import haxework.storage.SharedObjectStorage;
|
||||||
|
import openfl.utils.Assets;
|
||||||
|
import promhx.Deferred;
|
||||||
|
import promhx.Promise;
|
||||||
|
import ru.m.puzzlez.core.ImageSource;
|
||||||
|
|
||||||
|
@:provide class ImageStorage extends SharedObjectStorage {
|
||||||
|
|
||||||
|
private var cache:Map<ImageSource, Promise<BitmapData>>;
|
||||||
|
private var enabled:Bool;
|
||||||
|
|
||||||
|
public function new() {
|
||||||
|
super("image_1");
|
||||||
|
cache = new Map();
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function serialize(image:BitmapData):ByteArray {
|
||||||
|
var data = new ByteArray();
|
||||||
|
return image.encode(new Rectangle(0, 0, image.width, image.height), new PNGEncoderOptions(), data);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function unserialize(data:ByteArray):Promise<BitmapData> {
|
||||||
|
var def = new Deferred();
|
||||||
|
var loader = new Loader();
|
||||||
|
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event) {
|
||||||
|
def.resolve(cast(cast(event.target, LoaderInfo).content, Bitmap).bitmapData);
|
||||||
|
});
|
||||||
|
loader.loadBytes(data);
|
||||||
|
return def.promise();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolve(source:ImageSource):Promise<BitmapData> {
|
||||||
|
if (cache.exists(source)) {
|
||||||
|
return cache.get(source);
|
||||||
|
}
|
||||||
|
var key = Std.string(source);
|
||||||
|
var result:Promise<BitmapData> = enabled && exists(key) ?
|
||||||
|
unserialize(Reflect.field(so.data, key)) :
|
||||||
|
(switch source {
|
||||||
|
case ASSET(name):
|
||||||
|
Promise.promise(Assets.getBitmapData(name));
|
||||||
|
case URL(url):
|
||||||
|
new ImageLoader().GET(url);
|
||||||
|
}).then(function(image) {
|
||||||
|
if (enabled) {
|
||||||
|
so.setProperty(key, serialize(image));
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
});
|
||||||
|
cache.set(source, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
38
src/haxe/ru/m/puzzlez/storage/PixabayStorage.hx
Normal file
38
src/haxe/ru/m/puzzlez/storage/PixabayStorage.hx
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package ru.m.puzzlez.storage;
|
||||||
|
|
||||||
|
import haxework.net.JsonLoader;
|
||||||
|
import haxework.storage.SharedObjectStorage;
|
||||||
|
import promhx.Promise;
|
||||||
|
import ru.m.puzzlez.core.ImageSource;
|
||||||
|
|
||||||
|
typedef PixabayResponse = {
|
||||||
|
hits:Array<{
|
||||||
|
largeImageURL:String,
|
||||||
|
}>
|
||||||
|
}
|
||||||
|
|
||||||
|
@:provide class PixabayStorage extends SharedObjectStorage implements ISourceStorage {
|
||||||
|
|
||||||
|
private var key:String;
|
||||||
|
private var enabled:Bool;
|
||||||
|
|
||||||
|
public function new() {
|
||||||
|
super("pixabay_1");
|
||||||
|
key = "14915210-5eae157281211e0ad28bc8def";
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resolve(?type:String):Promise<Array<ImageSource>> {
|
||||||
|
return enabled && exists(type) ?
|
||||||
|
Promise.promise(read(type)) :
|
||||||
|
new JsonLoader<PixabayResponse>()
|
||||||
|
.GET('https://pixabay.com/api/?key=${key}&category=${type}')
|
||||||
|
.then(function(result:PixabayResponse) {
|
||||||
|
var result = [for (item in result.hits) URL(item.largeImageURL)];
|
||||||
|
if (enabled) {
|
||||||
|
write(type, result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,25 +1,19 @@
|
|||||||
package ru.m.puzzlez.view;
|
package ru.m.puzzlez.view;
|
||||||
|
|
||||||
import haxework.net.JsonLoader;
|
|
||||||
import haxework.view.data.DataView;
|
import haxework.view.data.DataView;
|
||||||
import haxework.view.form.ButtonView;
|
import haxework.view.form.ButtonView;
|
||||||
import haxework.view.group.VGroupView;
|
import haxework.view.group.VGroupView;
|
||||||
import haxework.view.ImageView;
|
import haxework.view.ImageView;
|
||||||
import haxework.view.utils.DrawUtil;
|
import haxework.view.utils.DrawUtil;
|
||||||
import openfl.utils.Assets;
|
import openfl.utils.Assets;
|
||||||
import openfl.utils.AssetType;
|
|
||||||
import ru.m.puzzlez.core.Game;
|
import ru.m.puzzlez.core.Game;
|
||||||
import ru.m.puzzlez.core.GameUtil;
|
import ru.m.puzzlez.core.GameUtil;
|
||||||
import ru.m.puzzlez.core.IGame;
|
import ru.m.puzzlez.core.IGame;
|
||||||
import ru.m.puzzlez.core.ImageSource;
|
import ru.m.puzzlez.core.ImageSource;
|
||||||
import ru.m.puzzlez.render.IRender;
|
import ru.m.puzzlez.render.IRender;
|
||||||
import ru.m.puzzlez.render.RenderUtil;
|
import ru.m.puzzlez.storage.AssetStorage;
|
||||||
|
import ru.m.puzzlez.storage.ImageStorage;
|
||||||
typedef PixabayResponse = {
|
import ru.m.puzzlez.storage.PixabayStorage;
|
||||||
hits:Array<{
|
|
||||||
largeImageURL:String,
|
|
||||||
}>
|
|
||||||
}
|
|
||||||
|
|
||||||
@:template class PuzzlezAppView extends VGroupView {
|
@:template class PuzzlezAppView extends VGroupView {
|
||||||
|
|
||||||
@@ -27,9 +21,13 @@ typedef PixabayResponse = {
|
|||||||
@:view private var render:IRender;
|
@:view private var render:IRender;
|
||||||
private var game:IGame;
|
private var game:IGame;
|
||||||
|
|
||||||
|
@:provide var imageStorage:ImageStorage;
|
||||||
|
@:provide var assetStorage:AssetStorage;
|
||||||
|
@:provide var pixabayStorage:PixabayStorage;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
super();
|
super();
|
||||||
images.data = [for (name in Assets.list(AssetType.IMAGE).filter(function(name:String) return name.substr(0, 15) == "resources/image")) ASSET(name)];
|
assetStorage.resolve().then(function(data) images.data = data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function imageViewFactory(index:Int, image:ImageSource):ImageView {
|
private function imageViewFactory(index:Int, image:ImageSource):ImageView {
|
||||||
@@ -39,18 +37,14 @@ typedef PixabayResponse = {
|
|||||||
result.fillType = FillType.COVER;
|
result.fillType = FillType.COVER;
|
||||||
result.setSize(192, 128);
|
result.setSize(192, 128);
|
||||||
result.image = Assets.getBitmapData("resources/icon.png");
|
result.image = Assets.getBitmapData("resources/icon.png");
|
||||||
RenderUtil.resolveImage(image).then(function(image) result.image = image);
|
imageStorage.resolve(image).then(function(image) result.image = image);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function moreImages(view:ButtonView):Void {
|
private function moreImages(view:ButtonView):Void {
|
||||||
view.visible = false;
|
view.visible = false;
|
||||||
view.toUpdateParent();
|
view.toUpdateParent();
|
||||||
new JsonLoader<PixabayResponse>()
|
pixabayStorage.resolve("nature").then(function(data) images.data = images.data.concat(data));
|
||||||
.GET('https://pixabay.com/api/?key=14915210-5eae157281211e0ad28bc8def&category=nature')
|
|
||||||
.then(function(result:PixabayResponse) {
|
|
||||||
images.data = images.data.concat([for (item in result.hits) URL(item.largeImageURL)]);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start(image:ImageSource):Void {
|
public function start(image:ImageSource):Void {
|
||||||
|
|||||||
Reference in New Issue
Block a user