[add] ImageSource
This commit is contained in:
@@ -8,6 +8,9 @@ indent_size = 4
|
|||||||
insert_final_newline = true
|
insert_final_newline = true
|
||||||
trim_trailing_whitespace = true
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[*.yaml]
|
||||||
|
indent_size = 2
|
||||||
|
|
||||||
[*.md]
|
[*.md]
|
||||||
max_line_length = off
|
max_line_length = off
|
||||||
trim_trailing_whitespace = false
|
trim_trailing_whitespace = false
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package ru.m.puzzlez.core;
|
|||||||
import flash.geom.Rectangle;
|
import flash.geom.Rectangle;
|
||||||
|
|
||||||
typedef GamePreset = {
|
typedef GamePreset = {
|
||||||
var image:String;
|
var image:ImageSource;
|
||||||
var grid:Grid;
|
var grid:Grid;
|
||||||
var tableRect:Rectangle;
|
var tableRect:Rectangle;
|
||||||
var imageRect:Rectangle;
|
var imageRect:Rectangle;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import ru.m.puzzlez.core.Part.BoundType;
|
|||||||
|
|
||||||
class GameUtil {
|
class GameUtil {
|
||||||
|
|
||||||
public static function buildPreset(image:String):GamePreset {
|
public static function buildPreset(image:ImageSource):GamePreset {
|
||||||
return {
|
return {
|
||||||
image:image,
|
image:image,
|
||||||
grid: {width: 8, height: 8},
|
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;
|
package ru.m.puzzlez.render;
|
||||||
|
|
||||||
|
import haxework.net.ImageLoader;
|
||||||
|
import ru.m.puzzlez.core.ImageSource;
|
||||||
import haxework.signal.Signal;
|
import haxework.signal.Signal;
|
||||||
import flash.display.BitmapData;
|
import flash.display.BitmapData;
|
||||||
import flash.display.Sprite;
|
import flash.display.Sprite;
|
||||||
@@ -55,10 +57,14 @@ class Render extends SpriteView implements IRender {
|
|||||||
|
|
||||||
private function onStart(state:GameState):Void {
|
private function onStart(state:GameState):Void {
|
||||||
clean();
|
clean();
|
||||||
image = Assets.getBitmapData(state.preset.image);
|
this.state = state;
|
||||||
image = RenderUtil.cropImage(image, state.preset.imageRect);
|
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) {
|
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);
|
var partView = new PartView(part.id, partImage, part.rect.size);
|
||||||
partView.position = part.rect.topLeft;
|
partView.position = part.rect.topLeft;
|
||||||
parts.set(part.id, partView);
|
parts.set(part.id, partView);
|
||||||
|
|||||||
@@ -1,15 +1,28 @@
|
|||||||
package ru.m.puzzlez.render;
|
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.BitmapData;
|
||||||
|
import flash.display.Shape;
|
||||||
|
import flash.filters.GlowFilter;
|
||||||
import flash.geom.Matrix;
|
import flash.geom.Matrix;
|
||||||
|
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;
|
||||||
|
|
||||||
class RenderUtil {
|
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 {
|
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();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
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.group.VGroupView;
|
import haxework.view.group.VGroupView;
|
||||||
import haxework.view.ImageView;
|
import haxework.view.ImageView;
|
||||||
@@ -10,28 +11,42 @@ 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.render.IRender;
|
import ru.m.puzzlez.render.IRender;
|
||||||
|
import ru.m.puzzlez.render.RenderUtil;
|
||||||
|
|
||||||
|
typedef PixabayResponse = {
|
||||||
|
hits:Array<{
|
||||||
|
largeImageURL:String,
|
||||||
|
}>
|
||||||
|
}
|
||||||
|
|
||||||
@:template class PuzzlezAppView extends VGroupView {
|
@:template class PuzzlezAppView extends VGroupView {
|
||||||
|
|
||||||
@:view private var scale:ScrollBarView;
|
@:view private var scale:ScrollBarView;
|
||||||
@:view private var images:DataView<String, ImageView>;
|
@:view private var images:DataView<ImageSource, ImageView>;
|
||||||
@:view private var render:IRender;
|
@:view private var render:IRender;
|
||||||
private var game:IGame;
|
private var game:IGame;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
super();
|
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;
|
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();
|
var result = new ImageView();
|
||||||
result.stretch = false;
|
result.stretch = false;
|
||||||
result.fillType = FillType.COVER;
|
result.fillType = FillType.COVER;
|
||||||
result.geometry.width = 192;
|
result.setSize(192, 128);
|
||||||
result.geometry.height = 128;
|
RenderUtil.resolveImage(image).then(function(image) result.image = image);
|
||||||
result.image = Assets.getBitmapData(image);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +54,7 @@ import ru.m.puzzlez.render.IRender;
|
|||||||
render.scale = value + scale.ratio;
|
render.scale = value + scale.ratio;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start(image:String):Void {
|
public function start(image:ImageSource):Void {
|
||||||
stop();
|
stop();
|
||||||
game = new Game(GameUtil.buildPreset(image));
|
game = new Game(GameUtil.buildPreset(image));
|
||||||
game.signal.connect(render.onGameEvent);
|
game.signal.connect(render.onGameEvent);
|
||||||
|
|||||||
@@ -2,23 +2,26 @@ style: background
|
|||||||
layout.hAlign: center
|
layout.hAlign: center
|
||||||
layout.vAlign: middle
|
layout.vAlign: middle
|
||||||
views:
|
views:
|
||||||
- $type: haxework.view.form.LabelView
|
|
||||||
text: Puzzle'z
|
|
||||||
font.size: 42
|
|
||||||
- $type: haxework.view.group.HGroupView
|
- $type: haxework.view.group.HGroupView
|
||||||
geometry.stretch: true
|
geometry.stretch: true
|
||||||
geometry.margin: 5
|
geometry.margin: 5
|
||||||
views:
|
views:
|
||||||
|
- $type: haxework.view.group.VGroupView
|
||||||
|
geometry.height: 100%
|
||||||
|
views:
|
||||||
|
- $type: haxework.view.form.LabelView
|
||||||
|
text: Puzzle'z
|
||||||
|
font.size: 42
|
||||||
- id: images
|
- id: images
|
||||||
$type: haxework.view.data.DataView
|
$type: haxework.view.data.DataView
|
||||||
layout:
|
layout:
|
||||||
$type: haxework.view.layout.VerticalLayout
|
$type: haxework.view.layout.VerticalLayout
|
||||||
margin: 5
|
margin: 5
|
||||||
geometry.height: 100%
|
geometry.height: 100%
|
||||||
# geometry.height: 100
|
|
||||||
factory: ~imageViewFactory
|
factory: ~imageViewFactory
|
||||||
+onDataSelect: ~start
|
+onDataSelect: ~start
|
||||||
geometry.margin: 5
|
geometry.margin: 5
|
||||||
|
overflow.y: scroll
|
||||||
- id: scale
|
- id: scale
|
||||||
$type: haxework.view.list.VScrollBarView
|
$type: haxework.view.list.VScrollBarView
|
||||||
ratio: 0.5
|
ratio: 0.5
|
||||||
|
|||||||
Reference in New Issue
Block a user