From b2d510492ef63d1ba964eaff224164798f11cb72 Mon Sep 17 00:00:00 2001 From: shmyga Date: Tue, 14 Jan 2020 22:37:18 +0300 Subject: [PATCH] [add] ImageSource --- .editorconfig | 3 ++ src/haxe/ru/m/puzzlez/core/GamePreset.hx | 2 +- src/haxe/ru/m/puzzlez/core/GameUtil.hx | 2 +- src/haxe/ru/m/puzzlez/core/ImageSource.hx | 6 ++++ src/haxe/ru/m/puzzlez/render/Render.hx | 12 ++++++-- src/haxe/ru/m/puzzlez/render/RenderUtil.hx | 21 +++++++++++--- src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx | 29 ++++++++++++++----- .../ru/m/puzzlez/view/PuzzlezAppView.yaml | 27 +++++++++-------- 8 files changed, 74 insertions(+), 28 deletions(-) create mode 100644 src/haxe/ru/m/puzzlez/core/ImageSource.hx diff --git a/.editorconfig b/.editorconfig index 9b73521..d5efcf5 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,6 +8,9 @@ indent_size = 4 insert_final_newline = true trim_trailing_whitespace = true +[*.yaml] +indent_size = 2 + [*.md] max_line_length = off trim_trailing_whitespace = false diff --git a/src/haxe/ru/m/puzzlez/core/GamePreset.hx b/src/haxe/ru/m/puzzlez/core/GamePreset.hx index 83ef05c..759e269 100644 --- a/src/haxe/ru/m/puzzlez/core/GamePreset.hx +++ b/src/haxe/ru/m/puzzlez/core/GamePreset.hx @@ -3,7 +3,7 @@ package ru.m.puzzlez.core; import flash.geom.Rectangle; typedef GamePreset = { - var image:String; + var image:ImageSource; var grid:Grid; var tableRect:Rectangle; var imageRect:Rectangle; diff --git a/src/haxe/ru/m/puzzlez/core/GameUtil.hx b/src/haxe/ru/m/puzzlez/core/GameUtil.hx index a583ec3..87ceb40 100644 --- a/src/haxe/ru/m/puzzlez/core/GameUtil.hx +++ b/src/haxe/ru/m/puzzlez/core/GameUtil.hx @@ -7,7 +7,7 @@ import ru.m.puzzlez.core.Part.BoundType; class GameUtil { - public static function buildPreset(image:String):GamePreset { + public static function buildPreset(image:ImageSource):GamePreset { return { image:image, grid: {width: 8, height: 8}, diff --git a/src/haxe/ru/m/puzzlez/core/ImageSource.hx b/src/haxe/ru/m/puzzlez/core/ImageSource.hx new file mode 100644 index 0000000..221e186 --- /dev/null +++ b/src/haxe/ru/m/puzzlez/core/ImageSource.hx @@ -0,0 +1,6 @@ +package ru.m.puzzlez.core; + +enum ImageSource { + ASSET(name:String); + URL(url:String); +} diff --git a/src/haxe/ru/m/puzzlez/render/Render.hx b/src/haxe/ru/m/puzzlez/render/Render.hx index 2a5f622..c5ab0a5 100644 --- a/src/haxe/ru/m/puzzlez/render/Render.hx +++ b/src/haxe/ru/m/puzzlez/render/Render.hx @@ -1,5 +1,7 @@ package ru.m.puzzlez.render; +import haxework.net.ImageLoader; +import ru.m.puzzlez.core.ImageSource; import haxework.signal.Signal; import flash.display.BitmapData; import flash.display.Sprite; @@ -55,10 +57,14 @@ class Render extends SpriteView implements IRender { private function onStart(state:GameState):Void { clean(); - image = Assets.getBitmapData(state.preset.image); - image = RenderUtil.cropImage(image, state.preset.imageRect); + this.state = state; + RenderUtil.resolveImage(state.preset.image).then(start); + } + + private function start(image:BitmapData):Void { + this.image = RenderUtil.cropImage(image, state.preset.imageRect); for (part in state.parts) { - var partImage = RenderUtil.cropImagePart(image, part); + var partImage = RenderUtil.cropImagePart(this.image, part); var partView = new PartView(part.id, partImage, part.rect.size); partView.position = part.rect.topLeft; parts.set(part.id, partView); diff --git a/src/haxe/ru/m/puzzlez/render/RenderUtil.hx b/src/haxe/ru/m/puzzlez/render/RenderUtil.hx index fee5df0..e3750a0 100644 --- a/src/haxe/ru/m/puzzlez/render/RenderUtil.hx +++ b/src/haxe/ru/m/puzzlez/render/RenderUtil.hx @@ -1,15 +1,28 @@ package ru.m.puzzlez.render; -import flash.filters.GlowFilter; -import flash.geom.Point; -import ru.m.puzzlez.core.Part; -import flash.display.Shape; import flash.display.BitmapData; +import flash.display.Shape; +import flash.filters.GlowFilter; import flash.geom.Matrix; +import flash.geom.Point; 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; class RenderUtil { + public static function resolveImage(source:ImageSource):Promise { + return switch source { + case ASSET(name): + return Promise.promise(Assets.getBitmapData(name)); + case URL(url): + return new ImageLoader().GET(url); + } + } + public static function cropImage(source:BitmapData, rect:Rectangle):BitmapData { var image = new BitmapData(Std.int(rect.width), Std.int(rect.height)); var matrix = new Matrix(); diff --git a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx index 567e274..f627143 100644 --- a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx +++ b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx @@ -1,5 +1,6 @@ package ru.m.puzzlez.view; +import haxework.net.JsonLoader; import haxework.view.data.DataView; import haxework.view.group.VGroupView; import haxework.view.ImageView; @@ -10,28 +11,42 @@ import openfl.utils.AssetType; import ru.m.puzzlez.core.Game; import ru.m.puzzlez.core.GameUtil; import ru.m.puzzlez.core.IGame; +import ru.m.puzzlez.core.ImageSource; import ru.m.puzzlez.render.IRender; +import ru.m.puzzlez.render.RenderUtil; + +typedef PixabayResponse = { + hits:Array<{ + largeImageURL:String, + }> +} @:template class PuzzlezAppView extends VGroupView { @:view private var scale:ScrollBarView; - @:view private var images:DataView; + @:view private var images:DataView; @:view private var render:IRender; private var game:IGame; public function new() { super(); - images.data = Assets.list(AssetType.IMAGE); + images.data = [for (name in Assets.list(AssetType.IMAGE)) ASSET(name)]; + render.scale = 0.75; scale.position = render.scale - scale.ratio; + + new JsonLoader() + .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)]); + }); } - private function imageViewFactory(index:Int, image:String):ImageView { + private function imageViewFactory(index:Int, image:ImageSource):ImageView { var result = new ImageView(); result.stretch = false; result.fillType = FillType.COVER; - result.geometry.width = 192; - result.geometry.height = 128; - result.image = Assets.getBitmapData(image); + result.setSize(192, 128); + RenderUtil.resolveImage(image).then(function(image) result.image = image); return result; } @@ -39,7 +54,7 @@ import ru.m.puzzlez.render.IRender; render.scale = value + scale.ratio; } - public function start(image:String):Void { + public function start(image:ImageSource):Void { stop(); game = new Game(GameUtil.buildPreset(image)); game.signal.connect(render.onGameEvent); diff --git a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml index 5b49ae3..ca9c561 100644 --- a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml +++ b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml @@ -2,23 +2,26 @@ style: background layout.hAlign: center layout.vAlign: middle views: - - $type: haxework.view.form.LabelView - text: Puzzle'z - font.size: 42 - $type: haxework.view.group.HGroupView geometry.stretch: true geometry.margin: 5 views: - - id: images - $type: haxework.view.data.DataView - layout: - $type: haxework.view.layout.VerticalLayout - margin: 5 + - $type: haxework.view.group.VGroupView geometry.height: 100% - # geometry.height: 100 - factory: ~imageViewFactory - +onDataSelect: ~start - geometry.margin: 5 + views: + - $type: haxework.view.form.LabelView + text: Puzzle'z + font.size: 42 + - id: images + $type: haxework.view.data.DataView + layout: + $type: haxework.view.layout.VerticalLayout + margin: 5 + geometry.height: 100% + factory: ~imageViewFactory + +onDataSelect: ~start + geometry.margin: 5 + overflow.y: scroll - id: scale $type: haxework.view.list.VScrollBarView ratio: 0.5