[update] Render
This commit is contained in:
@@ -10,7 +10,3 @@ interface IGame {
|
|||||||
public function stop():Void;
|
public function stop():Void;
|
||||||
public function dispose():Void;
|
public function dispose():Void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IGameListener {
|
|
||||||
public function onGameEvent(event:GameEvent):Void;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,9 +2,7 @@ package ru.m.puzzlez.render;
|
|||||||
|
|
||||||
import haxework.view.IView;
|
import haxework.view.IView;
|
||||||
import ru.m.puzzlez.core.GameEvent;
|
import ru.m.puzzlez.core.GameEvent;
|
||||||
import ru.m.puzzlez.core.GameState;
|
|
||||||
|
|
||||||
interface IRender extends IView<Dynamic> {
|
interface IRender extends IView<Dynamic> {
|
||||||
public function draw(state:GameState):Void;
|
|
||||||
public function onGameEvent(event:GameEvent):Void;
|
public function onGameEvent(event:GameEvent):Void;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package ru.m.puzzlez.render;
|
package ru.m.puzzlez.render;
|
||||||
|
|
||||||
|
import flash.display.Bitmap;
|
||||||
import flash.display.BitmapData;
|
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.SpriteView;
|
||||||
import haxework.view.utils.DrawUtil;
|
|
||||||
import openfl.Assets;
|
import openfl.Assets;
|
||||||
import ru.m.puzzlez.core.GameEvent;
|
import ru.m.puzzlez.core.GameEvent;
|
||||||
import ru.m.puzzlez.core.GameState;
|
import ru.m.puzzlez.core.GameState;
|
||||||
@@ -13,13 +15,17 @@ class Render extends SpriteView implements IRender {
|
|||||||
private var state:GameState;
|
private var state:GameState;
|
||||||
private var image:BitmapData;
|
private var image:BitmapData;
|
||||||
|
|
||||||
|
private var table:Sprite;
|
||||||
|
private var parts:Array<Bitmap>;
|
||||||
|
private var activePart:Bitmap;
|
||||||
|
private var activePoint:Point;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
super();
|
super();
|
||||||
}
|
table = new Sprite();
|
||||||
|
table.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
|
||||||
public function draw(state:GameState):Void {
|
content.addChild(table);
|
||||||
this.state = state;
|
parts = [];
|
||||||
toRedraw();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onGameEvent(event:GameEvent):Void {
|
public function onGameEvent(event:GameEvent):Void {
|
||||||
@@ -29,28 +35,57 @@ class Render extends SpriteView implements IRender {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function onStart(state:GameState):Void {
|
private function onStart(state:GameState):Void {
|
||||||
|
clean();
|
||||||
image = Assets.getBitmapData(state.preset.image);
|
image = Assets.getBitmapData(state.preset.image);
|
||||||
image = RenderUtil.cropImage(image, state.preset.grid);
|
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 {
|
private function onMouseDown(event:MouseEvent):Void {
|
||||||
super.redraw();
|
for (part in parts) {
|
||||||
if (state != null && image != null) {
|
if (part.x < event.localX && part.x + part.width > event.localX && part.y < event.localY && part.y + width > event.localY) {
|
||||||
var rect = RenderUtil.containRectangle(new Rectangle(0, 0, image.width, image.height), rect);
|
activePart = part;
|
||||||
DrawUtil.draw(content.graphics, image, rect, FillType.CONTAIN);
|
table.setChildIndex(activePart, table.numChildren - 1);
|
||||||
content.graphics.lineStyle(1, 0x00ff00);
|
activePoint = new Point(event.localX - part.x, event.localY - part.y);
|
||||||
for (i in 0...state.preset.grid.width + 1) {
|
table.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
|
||||||
var x = rect.x + i * rect.width / state.preset.grid.width;
|
table.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
|
||||||
content.graphics.moveTo(x, rect.top);
|
break;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 = [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,17 @@ class RenderUtil {
|
|||||||
return image;
|
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 {
|
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 s = Math.min(1, Math.min(target.width / source.width, target.height / source.height));
|
||||||
var width = source.width * s;
|
var width = source.width * s;
|
||||||
|
|||||||
Reference in New Issue
Block a user