[update] Render

This commit is contained in:
2020-01-10 18:00:43 +03:00
parent 9d67f67c6f
commit 99ebedabd6
4 changed files with 70 additions and 30 deletions

View File

@@ -10,7 +10,3 @@ interface IGame {
public function stop():Void;
public function dispose():Void;
}
interface IGameListener {
public function onGameEvent(event:GameEvent):Void;
}

View File

@@ -2,9 +2,7 @@ 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<Dynamic> {
public function draw(state:GameState):Void;
public function onGameEvent(event:GameEvent):Void;
}

View File

@@ -1,9 +1,11 @@
package ru.m.puzzlez.render;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import haxework.view.SpriteView;
import haxework.view.utils.DrawUtil;
import openfl.Assets;
import ru.m.puzzlez.core.GameEvent;
import ru.m.puzzlez.core.GameState;
@@ -13,13 +15,17 @@ class Render extends SpriteView implements IRender {
private var state:GameState;
private var image:BitmapData;
private var table:Sprite;
private var parts:Array<Bitmap>;
private var activePart:Bitmap;
private var activePoint:Point;
public function new() {
super();
}
public function draw(state:GameState):Void {
this.state = state;
toRedraw();
table = new Sprite();
table.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
content.addChild(table);
parts = [];
}
public function onGameEvent(event:GameEvent):Void {
@@ -29,28 +35,57 @@ 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.grid);
draw(state);
var partWidth = image.width / state.preset.grid.width + 2;
var partHeight = image.height / state.preset.grid.height + 2;
for (x in 0...state.preset.grid.width) {
for (y in 0...state.preset.grid.height) {
var partImage = RenderUtil.cropImagePart(image, state.preset.grid, x, y);
var bitmap = new Bitmap(partImage);
bitmap.x = 5 + partWidth * x;
bitmap.y = 5 + partHeight * y;
parts.push(bitmap);
table.addChild(bitmap);
}
}
table.graphics.clear();
table.graphics.beginFill(0xffff00, 1);
table.graphics.drawRect(0, 0, image.width, image.height);
table.graphics.endFill();
table.scaleX = table.scaleY = 0.25;
}
override public function redraw():Void {
super.redraw();
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;
content.graphics.moveTo(x, rect.top);
content.graphics.lineTo(x, rect.bottom);
private function onMouseDown(event:MouseEvent):Void {
for (part in parts) {
if (part.x < event.localX && part.x + part.width > event.localX && part.y < event.localY && part.y + width > event.localY) {
activePart = part;
table.setChildIndex(activePart, table.numChildren - 1);
activePoint = new Point(event.localX - part.x, event.localY - part.y);
table.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
table.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
break;
}
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();
}
}
private function onMouseMove(event:MouseEvent):Void {
activePart.x = event.localX - activePoint.x;
activePart.y = event.localY - activePoint.y;
}
private function onMouseUp(event:MouseEvent):Void {
table.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
table.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
activePart = null;
activePoint = null;
}
private function clean() {
for (bitmap in parts) {
table.removeChild(bitmap);
}
parts = [];
}
}

View File

@@ -28,6 +28,17 @@ class RenderUtil {
return image;
}
public static function cropImagePart(source:BitmapData, grid:Grid, x:Int, y:Int):BitmapData {
var partWidth = source.width / grid.width;
var partHeight = source.height / grid.height;
var rect = new Rectangle(partWidth * x, partHeight * y, partWidth, partHeight);
var image = new BitmapData(Std.int(partWidth), Std.int(partHeight));
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;