diff --git a/src/haxe/ru/m/puzzlez/core/Game.hx b/src/haxe/ru/m/puzzlez/core/Game.hx index 1d07e32..866ddd4 100644 --- a/src/haxe/ru/m/puzzlez/core/Game.hx +++ b/src/haxe/ru/m/puzzlez/core/Game.hx @@ -1,13 +1,25 @@ package ru.m.puzzlez.core; +import haxework.signal.Signal; + class Game implements IGame { public var state(default, null):GameState; + public var signal(default, null):Signal; public function new(preset:GamePreset) { state = GameUtil.buildState(preset); + signal = new Signal(); } public function start():Void { + signal.emit(GameEvent.START(state)); + } + + public function stop():Void { } + + public function dispose():Void { + signal.dispose(); + } } diff --git a/src/haxe/ru/m/puzzlez/core/GameEvent.hx b/src/haxe/ru/m/puzzlez/core/GameEvent.hx new file mode 100644 index 0000000..9385a8a --- /dev/null +++ b/src/haxe/ru/m/puzzlez/core/GameEvent.hx @@ -0,0 +1,5 @@ +package ru.m.puzzlez.core; + +enum GameEvent { + START(state:GameState); +} diff --git a/src/haxe/ru/m/puzzlez/core/GamePreset.hx b/src/haxe/ru/m/puzzlez/core/GamePreset.hx index 4b5f991..1a4ae6f 100644 --- a/src/haxe/ru/m/puzzlez/core/GamePreset.hx +++ b/src/haxe/ru/m/puzzlez/core/GamePreset.hx @@ -1,8 +1,6 @@ package ru.m.puzzlez.core; -import flash.display.BitmapData; - typedef GamePreset = { - var image:BitmapData; + var image:String; var grid:Grid; } diff --git a/src/haxe/ru/m/puzzlez/core/GameState.hx b/src/haxe/ru/m/puzzlez/core/GameState.hx index ff27804..38fcd32 100644 --- a/src/haxe/ru/m/puzzlez/core/GameState.hx +++ b/src/haxe/ru/m/puzzlez/core/GameState.hx @@ -1,8 +1,5 @@ package ru.m.puzzlez.core; -import flash.display.BitmapData; - typedef GameState = { var preset:GamePreset; - var image:BitmapData; } diff --git a/src/haxe/ru/m/puzzlez/core/GameUtil.hx b/src/haxe/ru/m/puzzlez/core/GameUtil.hx index 848362b..ee7c0b7 100644 --- a/src/haxe/ru/m/puzzlez/core/GameUtil.hx +++ b/src/haxe/ru/m/puzzlez/core/GameUtil.hx @@ -1,31 +1,10 @@ package ru.m.puzzlez.core; -import flash.geom.Matrix; -import flash.display.BitmapData; -import flash.geom.Rectangle; - class GameUtil { - public static function buildRectangle(source:Rectangle, grid:Grid):Rectangle { - var r1 = grid.width / grid.height; - var r2 = source.width / source.height; - if (r1 > r2) { - var height = source.width * r1; - return new Rectangle(0, (source.height - height) / 2, source.width, height); - } else { - var width = source.height * r1; - return new Rectangle((source.width - width) / 2, 0, width, source.height); - } - } public static function buildState(preset:GamePreset):GameState { - var rect = buildRectangle(new Rectangle(0, 0, preset.image.width, preset.image.height), preset.grid); - var image = new BitmapData(Std.int(rect.width), Std.int(rect.height)); - var matrix = new Matrix(); - matrix.translate(-rect.x, -rect.y); - image.draw(preset.image, matrix); return { preset: preset, - image: image, } } } diff --git a/src/haxe/ru/m/puzzlez/core/IGame.hx b/src/haxe/ru/m/puzzlez/core/IGame.hx index 4706a2d..f14c2a3 100644 --- a/src/haxe/ru/m/puzzlez/core/IGame.hx +++ b/src/haxe/ru/m/puzzlez/core/IGame.hx @@ -1,7 +1,16 @@ package ru.m.puzzlez.core; +import haxework.signal.Signal; + interface IGame { public var state(default, null):GameState; + public var signal(default, null):Signal; public function start():Void; + public function stop():Void; + public function dispose():Void; +} + +interface IGameListener { + public function onGameEvent(event:GameEvent):Void; } diff --git a/src/haxe/ru/m/puzzlez/core/Part.hx b/src/haxe/ru/m/puzzlez/core/Part.hx new file mode 100644 index 0000000..c7cf989 --- /dev/null +++ b/src/haxe/ru/m/puzzlez/core/Part.hx @@ -0,0 +1,6 @@ +package ru.m.puzzlez.core; + +typedef Part = { + var x:Int; + var y:Int; +} diff --git a/src/haxe/ru/m/puzzlez/render/IRender.hx b/src/haxe/ru/m/puzzlez/render/IRender.hx index d682a3e..6668ce8 100644 --- a/src/haxe/ru/m/puzzlez/render/IRender.hx +++ b/src/haxe/ru/m/puzzlez/render/IRender.hx @@ -1,8 +1,10 @@ package ru.m.puzzlez.render; import haxework.view.IView; +import ru.m.puzzlez.core.GameEvent; import ru.m.puzzlez.core.GameState; interface IRender extends IView { public function draw(state:GameState):Void; + public function onGameEvent(event:GameEvent):Void; } diff --git a/src/haxe/ru/m/puzzlez/render/Render.hx b/src/haxe/ru/m/puzzlez/render/Render.hx index 00050bf..a8f0ac6 100644 --- a/src/haxe/ru/m/puzzlez/render/Render.hx +++ b/src/haxe/ru/m/puzzlez/render/Render.hx @@ -1,13 +1,17 @@ package ru.m.puzzlez.render; +import flash.display.BitmapData; import flash.geom.Rectangle; import haxework.view.SpriteView; import haxework.view.utils.DrawUtil; +import openfl.Assets; +import ru.m.puzzlez.core.GameEvent; import ru.m.puzzlez.core.GameState; class Render extends SpriteView implements IRender { private var state:GameState; + private var image:BitmapData; public function new() { super(); @@ -18,11 +22,23 @@ class Render extends SpriteView implements IRender { toRedraw(); } + public function onGameEvent(event:GameEvent):Void { + switch event { + case START(state): onStart(state); + } + } + + private function onStart(state:GameState):Void { + image = Assets.getBitmapData(state.preset.image); + image = RenderUtil.cropImage(image, state.preset.grid); + draw(state); + } + override public function redraw():Void { super.redraw(); - if (state != null) { - var rect = RenderUtil.containRectangle(new Rectangle(0, 0, state.image.width, state.image.height), rect); - DrawUtil.draw(content.graphics, state.image, rect, FillType.CONTAIN); + if (state != null && image != null) { + var rect = RenderUtil.containRectangle(new Rectangle(0, 0, image.width, image.height), rect); + DrawUtil.draw(content.graphics, image, rect, FillType.CONTAIN); content.graphics.lineStyle(1, 0x00ff00); for (i in 0...state.preset.grid.width + 1) { var x = rect.x + i * rect.width / state.preset.grid.width; diff --git a/src/haxe/ru/m/puzzlez/render/RenderUtil.hx b/src/haxe/ru/m/puzzlez/render/RenderUtil.hx index a761a0a..e7e32c2 100644 --- a/src/haxe/ru/m/puzzlez/render/RenderUtil.hx +++ b/src/haxe/ru/m/puzzlez/render/RenderUtil.hx @@ -1,8 +1,33 @@ package ru.m.puzzlez.render; +import flash.geom.Matrix; +import flash.display.BitmapData; +import ru.m.puzzlez.core.Grid; import flash.geom.Rectangle; class RenderUtil { + + public static function buildRectangle(source:Rectangle, grid:Grid):Rectangle { + var r1 = grid.width / grid.height; + var r2 = source.width / source.height; + if (r1 > r2) { + var height = source.width * r1; + return new Rectangle(0, (source.height - height) / 2, source.width, height); + } else { + var width = source.height * r1; + return new Rectangle((source.width - width) / 2, 0, width, source.height); + } + } + + public static function cropImage(source:BitmapData, grid:Grid):BitmapData { + var rect = buildRectangle(new Rectangle(0, 0, source.width, source.height), grid); + var image = new BitmapData(Std.int(rect.width), Std.int(rect.height)); + var matrix = new Matrix(); + matrix.translate(-rect.x, -rect.y); + image.draw(source, matrix); + return image; + } + public static function containRectangle(source:Rectangle, target:Rectangle):Rectangle { var s = Math.min(1, Math.min(target.width / source.width, target.height / source.height)); var width = source.width * s; diff --git a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx index 81274dc..6100a67 100644 --- a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx +++ b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx @@ -1,23 +1,48 @@ package ru.m.puzzlez.view; +import haxework.view.utils.DrawUtil.FillType; +import haxework.view.data.DataView; import haxework.view.group.VGroupView; +import haxework.view.ImageView; import openfl.utils.Assets; +import openfl.utils.AssetType; import ru.m.puzzlez.core.Game; import ru.m.puzzlez.core.IGame; import ru.m.puzzlez.render.IRender; @:template class PuzzlezAppView extends VGroupView { + @:view private var images:DataView; @:view private var render:IRender; private var game:IGame; public function new() { super(); + images.data = Assets.list(AssetType.IMAGE); } - public function start() { - var image = Assets.getBitmapData('resources/warty-final-ubuntu.png'); + private function imageViewFactory(index:Int, image:String):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); + return result; + } + + public function start(image:String):Void { + stop(); game = new Game({image:image, grid: {width: 7, height: 5}}); - render.draw(game.state); + game.signal.connect(render.onGameEvent); + game.start(); + } + + public function stop():Void { + if (game != null) { + game.stop(); + game.dispose(); + game = null; + } } } diff --git a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml index 7500511..2cfa446 100644 --- a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml +++ b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml @@ -1,15 +1,19 @@ style: background layout.hAlign: center layout.vAlign: middle -layout.margin: 20 -# geometry.padding: 5 views: - $type: haxework.view.form.LabelView text: Puzzle'z font.size: 42 - - $type: haxework.view.form.ButtonView - text: Start - +onPress: ~start() + - id: images + $type: haxework.view.data.DataView + layout: + $type: haxework.view.layout.HorizontalLayout + geometry.width: 100% + # geometry.height: 100 + factory: ~imageViewFactory + +onDataSelect: ~start + geometry.margin: 5 - id: render $type: ru.m.puzzlez.render.Render geometry.width: 100% diff --git a/src/resources/warty-final-ubuntu-blue.png b/src/resources/warty-final-ubuntu-blue.png new file mode 100644 index 0000000..1598959 Binary files /dev/null and b/src/resources/warty-final-ubuntu-blue.png differ