[add] ImageSource
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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},
|
||||
|
||||
6
src/haxe/ru/m/puzzlez/core/ImageSource.hx
Normal file
6
src/haxe/ru/m/puzzlez/core/ImageSource.hx
Normal file
@@ -0,0 +1,6 @@
|
||||
package ru.m.puzzlez.core;
|
||||
|
||||
enum ImageSource {
|
||||
ASSET(name:String);
|
||||
URL(url:String);
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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<BitmapData> {
|
||||
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();
|
||||
|
||||
@@ -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<String, ImageView>;
|
||||
@:view private var images:DataView<ImageSource, ImageView>;
|
||||
@: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<PixabayResponse>()
|
||||
.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);
|
||||
|
||||
@@ -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:
|
||||
- $type: haxework.view.group.VGroupView
|
||||
geometry.height: 100%
|
||||
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%
|
||||
# geometry.height: 100
|
||||
factory: ~imageViewFactory
|
||||
+onDataSelect: ~start
|
||||
geometry.margin: 5
|
||||
overflow.y: scroll
|
||||
- id: scale
|
||||
$type: haxework.view.list.VScrollBarView
|
||||
ratio: 0.5
|
||||
|
||||
Reference in New Issue
Block a user