diff --git a/package.json b/package.json index 35829fb..4a26d1f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "puzzlez", - "version": "0.3.2", + "version": "0.3.3", "private": true, "devDependencies": { "dateformat": "^3.0.3", diff --git a/src/haxe/ru/m/Device.hx b/src/haxe/ru/m/Device.hx index 99bf839..c9a1660 100644 --- a/src/haxe/ru/m/Device.hx +++ b/src/haxe/ru/m/Device.hx @@ -1,9 +1,12 @@ package ru.m; -@:enum abstract Platform(String) from String to String { +enum abstract Platform(String) from String to String { var ANDROID = "android"; var LINUX = "linux"; var WINDOWS = "windows"; + var FLASH = "flash"; + var HTML5 = "html5"; + var UNKNOWN = "unknown"; } class Device { @@ -17,8 +20,12 @@ class Device { return LINUX; #elseif windows return WINDOWS; + #elseif flash + return FLASH; + #elseif html5 + return HTML5; #else - return null; + return UNKNOWN; #end } diff --git a/src/haxe/ru/m/puzzlez/PuzzlezApp.hx b/src/haxe/ru/m/puzzlez/PuzzlezApp.hx index 410ac89..1e51633 100644 --- a/src/haxe/ru/m/puzzlez/PuzzlezApp.hx +++ b/src/haxe/ru/m/puzzlez/PuzzlezApp.hx @@ -15,6 +15,7 @@ class PuzzlezApp extends App { // ToDo: fix @:provide macro GameStorage; ImageStorage; + Const.init(); L.push(new TraceLogger()); updater = new Updater(Const.VERSION, "https://shmyga.ru/repo/puzzlez/packages.json"); var app = new PuzzlezApp(new PuzzlezTheme(), openfl.Assets.getBitmapData("resources/icon.png")); diff --git a/src/haxe/ru/m/puzzlez/PuzzlezTheme.hx b/src/haxe/ru/m/puzzlez/PuzzlezTheme.hx index 8156bac..bfa72ab 100644 --- a/src/haxe/ru/m/puzzlez/PuzzlezTheme.hx +++ b/src/haxe/ru/m/puzzlez/PuzzlezTheme.hx @@ -56,5 +56,12 @@ class PuzzlezTheme extends Theme { register(new Style("button.red", [ "skin.color" => 0xcc0000, ], "button")); + + register(new Style("label.header", [ + "font.size" => 36, + "geometry.hAlign" => HAlign.CENTER, + "geometry.margin.top" => 10, + "geometry.margin.bottom" => 10, + ], "label")); } } diff --git a/src/haxe/ru/m/puzzlez/render/Render.hx b/src/haxe/ru/m/puzzlez/render/Render.hx index b87191b..af02bc7 100644 --- a/src/haxe/ru/m/puzzlez/render/Render.hx +++ b/src/haxe/ru/m/puzzlez/render/Render.hx @@ -135,6 +135,10 @@ class Render extends SpriteView implements IRender { private function onMouseDown(event:MouseEvent):Void { if (Std.is(event.target, PartView)) { var part:PartView = event.target; + if (event.ctrlKey) { + save(part); + return; + } if (part.completed) { return; } @@ -160,7 +164,6 @@ class Render extends SpriteView implements IRender { signal.emit(ACTION(PART_PUT(activePart.id, partPosition.clone()))); tableView.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); tableView.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp); - //save(activePart); activePart = null; activePoint = null; } diff --git a/src/haxe/ru/m/puzzlez/source/PixabaySource.hx b/src/haxe/ru/m/puzzlez/source/PixabaySource.hx index 3777ff7..55a9cc2 100644 --- a/src/haxe/ru/m/puzzlez/source/PixabaySource.hx +++ b/src/haxe/ru/m/puzzlez/source/PixabaySource.hx @@ -1,10 +1,13 @@ package ru.m.puzzlez.source; +import haxe.io.Bytes; import flash.display.BitmapData; -import haxework.net.ImageLoader; +import flash.utils.ByteArray; +import haxework.net.BytesLoader; import haxework.net.JsonLoader; import promhx.Promise; import ru.m.puzzlez.core.Id; +import ru.m.puzzlez.storage.CacheStorage; typedef PixabayImage = { var id:Int; @@ -57,6 +60,8 @@ class PixabaySource implements IImageSource { private var cache:Map>; + @:provide static var imageCache:CacheStorage; + public function new() { cache = new Map(); } @@ -85,7 +90,17 @@ class PixabaySource implements IImageSource { } public function loadImage(id:ImageId, preview:Bool = false):Promise { - return getImage(id) - .pipe((data:PixabayImage) -> return new ImageLoader().GET(preview ? data.webformatURL : data.largeImageURL)); + var key = '${id}_${preview}'; + if (imageCache.exists(key)) { + return ImageUtil.bytesToImage(imageCache.read(key)); + } else { + return getImage(id) + .pipe((data:PixabayImage) -> new BytesLoader().GET(preview ? data.webformatURL : data.largeImageURL)) + .pipe((data:ByteArray) -> { + var bytes = Bytes.ofData(data); + imageCache.write(key, bytes); + return ImageUtil.bytesToImage(bytes); + }); + } } } diff --git a/src/haxe/ru/m/puzzlez/storage/CacheStorage.hx b/src/haxe/ru/m/puzzlez/storage/CacheStorage.hx new file mode 100644 index 0000000..edfbe0c --- /dev/null +++ b/src/haxe/ru/m/puzzlez/storage/CacheStorage.hx @@ -0,0 +1,66 @@ +package ru.m.puzzlez.storage; + +import flash.net.SharedObject; +import haxe.io.Bytes; + +#if html5 +@:provide class CacheStorage { + public function new() { + } + + public function exists(key:String):Bool { + return false; + } + + public function read(key:String):Null { + return null; + } + + public function write(key:String, data:Bytes):Void { + } + + public function remove(key:String):Void { + } + + public function clean():Void { + } +} +#else +@:provide class CacheStorage { + + public function new() { + } + + private inline function getSharedObject(key:String):SharedObject { + key = StringTools.replace(key, "/", "_"); + key = StringTools.replace(key, ":", "_"); + key = StringTools.replace(key, ".", "_"); + return SharedObject.getLocal('cache/${key}'); + } + + public function exists(key:String):Bool { + var so = getSharedObject(key); + return so.size > 0 && Reflect.hasField(so.data, "data"); + } + + public function read(key:String):Null { + var so = getSharedObject(key); + return Bytes.ofData(Reflect.field(so.data, "data")); + } + + public function write(key:String, data:Bytes):Void { + var so = getSharedObject(key); + so.setProperty("data", data.getData()); + so.flush(); + } + + public function remove(key:String):Void { + var so = getSharedObject(key); + so.clear(); + } + + public function clean():Void { + //SharedObject.deleteAll("cache/"); + } +} +#end diff --git a/src/haxe/ru/m/puzzlez/storage/ImageStorage.hx b/src/haxe/ru/m/puzzlez/storage/ImageStorage.hx index a686eec..17afc46 100644 --- a/src/haxe/ru/m/puzzlez/storage/ImageStorage.hx +++ b/src/haxe/ru/m/puzzlez/storage/ImageStorage.hx @@ -1,15 +1,7 @@ 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.net.SharedObject; -import flash.utils.ByteArray; -import promhx.Deferred; import promhx.Promise; import Reflect; import ru.m.puzzlez.core.Id; @@ -47,22 +39,6 @@ import ru.m.puzzlez.source.PixabaySource; so.flush(); } - 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 { - var def = new Deferred(); - var loader = new Loader(); - loader.contentLoaderInfo.addEventListener(Event.COMPLETE, (event:Event) -> { - def.resolve(cast(cast(event.target, LoaderInfo).content, Bitmap).bitmapData); - }); - loader.loadBytes(data); - return def.promise(); - } - public function resolve(id:ImageId, preview:Bool = false):Promise { var key = '${id}_${preview}'; if (cache.exists(key)) { diff --git a/src/haxe/ru/m/puzzlez/view/PresetFrame.yaml b/src/haxe/ru/m/puzzlez/view/PresetFrame.yaml index 0eba2ec..498bbe6 100644 --- a/src/haxe/ru/m/puzzlez/view/PresetFrame.yaml +++ b/src/haxe/ru/m/puzzlez/view/PresetFrame.yaml @@ -2,27 +2,32 @@ style: frame layout.margin: 10 views: + - $type: haxework.view.form.LabelView + text: Puzzle configure + style: label.header - $type: haxework.view.group.HGroupView geometry.width: 100% layout.hAlign: center layout.vAlign: middle views: + - id: sizes + $type: haxework.view.data.DataView + layout: + $type: haxework.view.layout.TailLayout + hAlign: center + stretch: true + margin: 5 + factory: ~factory + +onDataSelect: ~selectSize - $type: haxework.view.form.ButtonView + style: button.active geometry.margin.left: 15 text: Start +onPress: ~start() - - id: sizes - $type: haxework.view.data.DataView - geometry.width: 100% - layout: - $type: haxework.view.layout.TailLayout - hAlign: center - margin: 5 - factory: ~factory - +onDataSelect: ~selectSize - id: image $type: ru.m.puzzlez.view.PresetView geometry.stretch: true + geometry.margin: 15 - $type: haxework.view.form.ButtonView text: Back geometry.position: absolute diff --git a/src/haxe/ru/m/puzzlez/view/PresetView.hx b/src/haxe/ru/m/puzzlez/view/PresetView.hx index 156f892..67b8ece 100644 --- a/src/haxe/ru/m/puzzlez/view/PresetView.hx +++ b/src/haxe/ru/m/puzzlez/view/PresetView.hx @@ -3,7 +3,6 @@ package ru.m.puzzlez.view; import flash.display.BitmapData; import flash.display.Graphics; import flash.display.Shape; -import flash.geom.Matrix; import haxework.view.group.GroupView; import ru.m.puzzlez.core.GameState; import ru.m.puzzlez.render.RenderUtil; @@ -20,8 +19,8 @@ class PresetView extends GroupView { private function set_scale(value:Float):Float { var result = table.scaleX = table.scaleY = value; - table.x = (width - state.preset.tableRect.width * value) / 2; - table.y = (height - state.preset.tableRect.height * value) / 2; + table.x = (width - state.preset.imageRect.width * value) / 2; + table.y = (height - state.preset.imageRect.height * value) / 2; return result; } @@ -60,16 +59,14 @@ class PresetView extends GroupView { var partHeight = preset.imageRect.height / preset.grid.height; var graphics:Graphics = table.graphics; graphics.clear(); - var matrix = new Matrix(); - matrix.translate(preset.imageRect.x, preset.imageRect.y); - graphics.beginBitmapFill(image, matrix, false, true); - graphics.drawRect(preset.imageRect.x, preset.imageRect.y, preset.imageRect.width, preset.imageRect.height); + graphics.beginBitmapFill(image, null, false, true); + graphics.drawRect(0, 0, preset.imageRect.width, preset.imageRect.height); graphics.endFill(); for (part in state.parts) { var rect = part.rect.clone(); - rect.x = preset.imageRect.x + part.gridX * part.rect.width; - rect.y = preset.imageRect.y + part.gridY * part.rect.height; + rect.x = part.gridX * part.rect.width; + rect.y = part.gridY * part.rect.height; var path = RenderUtil.builder.build(rect, part.bounds); for (value in RenderUtil.borderSettings) { graphics.lineStyle(1, value.color, value.opacity); @@ -82,7 +79,7 @@ class PresetView extends GroupView { override public function update():Void { super.update(); if (state != null) { - scale = Math.min(width / state.preset.tableRect.width, height / state.preset.tableRect.height); + scale = Math.min(width / state.preset.imageRect.width, height / state.preset.imageRect.height); } } } diff --git a/src/haxe/ru/m/puzzlez/view/StartFrame.yaml b/src/haxe/ru/m/puzzlez/view/StartFrame.yaml index a792521..c724ff4 100644 --- a/src/haxe/ru/m/puzzlez/view/StartFrame.yaml +++ b/src/haxe/ru/m/puzzlez/view/StartFrame.yaml @@ -1,11 +1,20 @@ --- style: frame views: - - $type: haxework.view.form.LabelView - text: $r:text:name - geometry.margin.top: 15 + - $type: haxework.view.group.HGroupView geometry.hAlign: center - font.size: 40 + geometry.margin.top: 15 + layout.vAlign: middle + views: + - $type: haxework.view.ImageView + image: $a:image:resources/icon.png + stretch: false + fillType: CONTAIN + geometry.width: 96 + geometry.height: 96 + - $type: haxework.view.form.LabelView + text: $r:text:name + font.size: 50 - id: sources $type: haxework.view.data.DataView layout: diff --git a/src/haxe/ru/m/update/Updater.hx b/src/haxe/ru/m/update/Updater.hx index 119d3ac..4997d6e 100644 --- a/src/haxe/ru/m/update/Updater.hx +++ b/src/haxe/ru/m/update/Updater.hx @@ -46,7 +46,7 @@ class Updater { private var url:String; private var version:Version; - public function new(version:String, url:String) { + public function new(version:Version, url:String) { this.url = url; this.version = version; } diff --git a/src/resources/icon.png b/src/resources/icon.png index d23bbf9..9934e00 100644 Binary files a/src/resources/icon.png and b/src/resources/icon.png differ diff --git a/work.md b/work.md index 694e008..4281e7b 100644 --- a/work.md +++ b/work.md @@ -1,3 +1,4 @@ * background settings -* preset frame improve * parts groups +* navigation +* version compare bug