diff --git a/gulpfile.js b/gulpfile.js index 397f555..00ec5d5 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -49,6 +49,9 @@ const app = new Project( sources: [ 'src/haxe', ], + assets: [ + 'src/resources', + ], main: 'ru.m.puzzlez.PuzzlezApp', meta: { width: 1024, diff --git a/src/haxe/ru/m/puzzlez/core/Game.hx b/src/haxe/ru/m/puzzlez/core/Game.hx new file mode 100644 index 0000000..1d07e32 --- /dev/null +++ b/src/haxe/ru/m/puzzlez/core/Game.hx @@ -0,0 +1,13 @@ +package ru.m.puzzlez.core; + +class Game implements IGame { + public var state(default, null):GameState; + + public function new(preset:GamePreset) { + state = GameUtil.buildState(preset); + } + + public function start():Void { + + } +} diff --git a/src/haxe/ru/m/puzzlez/core/GamePreset.hx b/src/haxe/ru/m/puzzlez/core/GamePreset.hx new file mode 100644 index 0000000..4b5f991 --- /dev/null +++ b/src/haxe/ru/m/puzzlez/core/GamePreset.hx @@ -0,0 +1,8 @@ +package ru.m.puzzlez.core; + +import flash.display.BitmapData; + +typedef GamePreset = { + var image:BitmapData; + var grid:Grid; +} diff --git a/src/haxe/ru/m/puzzlez/core/GameState.hx b/src/haxe/ru/m/puzzlez/core/GameState.hx new file mode 100644 index 0000000..ff27804 --- /dev/null +++ b/src/haxe/ru/m/puzzlez/core/GameState.hx @@ -0,0 +1,8 @@ +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 new file mode 100644 index 0000000..848362b --- /dev/null +++ b/src/haxe/ru/m/puzzlez/core/GameUtil.hx @@ -0,0 +1,31 @@ +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/Grid.hx b/src/haxe/ru/m/puzzlez/core/Grid.hx new file mode 100644 index 0000000..4e0456d --- /dev/null +++ b/src/haxe/ru/m/puzzlez/core/Grid.hx @@ -0,0 +1,6 @@ +package ru.m.puzzlez.core; + +typedef Grid = { + var width:Int; + var height:Int; +} diff --git a/src/haxe/ru/m/puzzlez/core/IGame.hx b/src/haxe/ru/m/puzzlez/core/IGame.hx new file mode 100644 index 0000000..4706a2d --- /dev/null +++ b/src/haxe/ru/m/puzzlez/core/IGame.hx @@ -0,0 +1,7 @@ +package ru.m.puzzlez.core; + +interface IGame { + public var state(default, null):GameState; + + public function start():Void; +} diff --git a/src/haxe/ru/m/puzzlez/render/IRender.hx b/src/haxe/ru/m/puzzlez/render/IRender.hx new file mode 100644 index 0000000..d682a3e --- /dev/null +++ b/src/haxe/ru/m/puzzlez/render/IRender.hx @@ -0,0 +1,8 @@ +package ru.m.puzzlez.render; + +import haxework.view.IView; +import ru.m.puzzlez.core.GameState; + +interface IRender extends IView { + public function draw(state:GameState):Void; +} diff --git a/src/haxe/ru/m/puzzlez/render/Render.hx b/src/haxe/ru/m/puzzlez/render/Render.hx new file mode 100644 index 0000000..00050bf --- /dev/null +++ b/src/haxe/ru/m/puzzlez/render/Render.hx @@ -0,0 +1,40 @@ +package ru.m.puzzlez.render; + +import flash.geom.Rectangle; +import haxework.view.SpriteView; +import haxework.view.utils.DrawUtil; +import ru.m.puzzlez.core.GameState; + +class Render extends SpriteView implements IRender { + + private var state:GameState; + + public function new() { + super(); + } + + public function draw(state:GameState):Void { + this.state = state; + toRedraw(); + } + + 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); + 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; + content.graphics.moveTo(x, rect.top); + content.graphics.lineTo(x, rect.bottom); + } + for (i in 0...state.preset.grid.height + 1) { + var y = rect.y + i * rect.height / state.preset.grid.height; + content.graphics.moveTo(rect.left, y); + content.graphics.lineTo(rect.right, y); + } + content.graphics.lineStyle(); + } + } +} diff --git a/src/haxe/ru/m/puzzlez/render/RenderUtil.hx b/src/haxe/ru/m/puzzlez/render/RenderUtil.hx new file mode 100644 index 0000000..a761a0a --- /dev/null +++ b/src/haxe/ru/m/puzzlez/render/RenderUtil.hx @@ -0,0 +1,12 @@ +package ru.m.puzzlez.render; + +import flash.geom.Rectangle; + +class RenderUtil { + 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; + var height = source.height * s; + return new Rectangle((target.width - width) / 2, (target.height - height) / 2, width, height); + } +} diff --git a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx index 1ea4b5e..81274dc 100644 --- a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx +++ b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.hx @@ -1,10 +1,23 @@ package ru.m.puzzlez.view; import haxework.view.group.VGroupView; +import openfl.utils.Assets; +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 render:IRender; + private var game:IGame; + public function new() { super(); } + + public function start() { + var image = Assets.getBitmapData('resources/warty-final-ubuntu.png'); + game = new Game({image:image, grid: {width: 7, height: 5}}); + render.draw(game.state); + } } diff --git a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml index 38c8b25..7500511 100644 --- a/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml +++ b/src/haxe/ru/m/puzzlez/view/PuzzlezAppView.yaml @@ -2,9 +2,17 @@ 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 \ No newline at end of file + text: Start + +onPress: ~start() + - id: render + $type: ru.m.puzzlez.render.Render + geometry.width: 100% + geometry.height: 100% + geometry.margin: 5 + style: frame diff --git a/src/resources/warty-final-ubuntu.png b/src/resources/warty-final-ubuntu.png new file mode 100644 index 0000000..9a72aa0 Binary files /dev/null and b/src/resources/warty-final-ubuntu.png differ