243 lines
7.9 KiB
Haxe
243 lines
7.9 KiB
Haxe
package ru.m.puzzlez.render;
|
|
|
|
import flash.display.BitmapData;
|
|
import flash.display.PNGEncoderOptions;
|
|
import flash.display.Sprite;
|
|
import flash.events.MouseEvent;
|
|
import flash.geom.Point as FlashPoint;
|
|
import flash.geom.Rectangle;
|
|
import flash.net.FileReference;
|
|
import flash.utils.ByteArray;
|
|
import hw.signal.Signal;
|
|
import hw.view.popup.AlertView;
|
|
import hw.view.SpriteView;
|
|
import ru.m.puzzlez.image.ImageData;
|
|
import ru.m.puzzlez.proto.event.gameaction.Action;
|
|
import ru.m.puzzlez.proto.event.GameAction;
|
|
import ru.m.puzzlez.proto.event.GameEvent;
|
|
import ru.m.puzzlez.proto.game.GameState;
|
|
import ru.m.puzzlez.proto.game.PartLocation;
|
|
import ru.m.puzzlez.render.ImagePartBuilder;
|
|
import ru.m.puzzlez.settings.Settings;
|
|
import ru.m.puzzlez.wrap.PointExt;
|
|
|
|
class Render extends SpriteView implements IRender {
|
|
public var actions(default, null):Signal<GameAction>;
|
|
public var scale(get, set):Float;
|
|
public var manager(default, null):RenderManager;
|
|
|
|
@:provide static var settings:Settings;
|
|
|
|
private var playerId(get, never):String;
|
|
|
|
private function get_playerId():String {
|
|
// ToDo: network user
|
|
return "local";
|
|
}
|
|
|
|
private function get_scale():Float {
|
|
return tableView.scaleX;
|
|
}
|
|
|
|
private function set_scale(value:Float):Float {
|
|
var result = tableView.scaleX = tableView.scaleY = value;
|
|
tableView.x = (width - state.preset.tableRect.width * value) / 2;
|
|
tableView.y = (height - state.preset.tableRect.height * value) / 2;
|
|
// setSize(table.width, table.height, 'table');
|
|
return result;
|
|
}
|
|
|
|
private var state:GameState;
|
|
private var image:BitmapData;
|
|
|
|
private var progress:ProgressView;
|
|
private var container:Sprite;
|
|
private var tableView:Sprite;
|
|
private var imageView:CompleteView;
|
|
private var parts:Map<Int, PartView>;
|
|
private var activePart:PartView;
|
|
private var activePoint:PointExt;
|
|
|
|
private var movePoint:FlashPoint;
|
|
|
|
public function new() {
|
|
super();
|
|
container = new Sprite();
|
|
content.addChild(container);
|
|
manager = new RenderManager(content, container);
|
|
manager.locked = settings.locked;
|
|
progress = new ProgressView();
|
|
actions = new Signal();
|
|
tableView = new Sprite();
|
|
tableView.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
|
|
imageView = new CompleteView();
|
|
tableView.addChild(imageView);
|
|
container.addChild(tableView);
|
|
parts = new Map();
|
|
}
|
|
|
|
public function onGameEvent(event:GameEvent):Void {
|
|
if (event.hasStart()) {
|
|
onStart(event.start.state);
|
|
} else if (event.hasChange()) {
|
|
var part:PartView = parts[event.change.partId];
|
|
// TODO:
|
|
if (part == null) {
|
|
return;
|
|
}
|
|
part.playerId = event.change.playerId;
|
|
part.position = event.change.position;
|
|
switch event.change.location {
|
|
case PartLocation.IMAGE:
|
|
part.complete();
|
|
imageView.addChild(part);
|
|
imageView.redraw();
|
|
case _:
|
|
}
|
|
} else if (event.hasComplete()) {
|
|
AlertView.alert("Complete!");
|
|
}
|
|
}
|
|
|
|
private function onStart(state:GameState):Void {
|
|
clean();
|
|
this.state = state;
|
|
for (part in state.parts) {
|
|
var partView = PartView.factory(part);
|
|
parts.set(part.id, partView);
|
|
switch part.location {
|
|
case PartLocation.TABLE:
|
|
partView.position = part.position;
|
|
tableView.addChild(partView);
|
|
case PartLocation.IMAGE:
|
|
partView.complete();
|
|
imageView.addChild(partView);
|
|
case _:
|
|
}
|
|
}
|
|
|
|
imageView.x = state.preset.imageRect.x;
|
|
imageView.y = state.preset.imageRect.y;
|
|
imageView.preset = state.preset;
|
|
imageView.redraw();
|
|
progress.text = "Loading image";
|
|
content.addChild(progress.content);
|
|
ImageData.fromImageId(state.preset.image).resolve().then(onImageResolved);
|
|
toUpdate();
|
|
}
|
|
|
|
private function onImageResolved(image:BitmapData):Void {
|
|
this.image = RenderUtil.cropImage(image, state.preset.imageRect);
|
|
var builder = new ImagePartBuilder(this.image);
|
|
var i = 0;
|
|
builder.build(state.parts).then((result:Result) -> {
|
|
parts[result.part.id].image = result.image;
|
|
progress.setProgress(++i, state.parts.length);
|
|
if (i >= state.parts.length - 1) {
|
|
if (progress.content.parent != null) {
|
|
progress.content.parent.removeChild(progress.content);
|
|
}
|
|
imageView.redraw();
|
|
}
|
|
});
|
|
}
|
|
|
|
override public function update():Void {
|
|
super.update();
|
|
progress.x = (width - progress.width) / 2;
|
|
progress.y = (height - progress.height) / 2;
|
|
progress.update();
|
|
if (state != null) {
|
|
scale = Math.min(width / state.preset.tableRect.width, height / state.preset.tableRect.height);
|
|
}
|
|
}
|
|
|
|
override public function redraw():Void {
|
|
switch settings.background {
|
|
case Background.NONE:
|
|
super.redraw();
|
|
case Background.COLOR(color):
|
|
content.graphics.clear();
|
|
content.graphics.beginFill(color);
|
|
content.graphics.drawRect(0, 0, width, height);
|
|
content.graphics.endFill();
|
|
case Background.IMAGE(id):
|
|
ImageData.fromImageId(id).resolve().then(result -> {
|
|
content.graphics.clear();
|
|
content.graphics.beginBitmapFill(result);
|
|
content.graphics.drawRect(0, 0, width, height);
|
|
content.graphics.endFill();
|
|
});
|
|
}
|
|
}
|
|
|
|
private function onMouseDown(event:MouseEvent):Void {
|
|
var point:FlashPoint = new FlashPoint(event.stageX, event.stageY);
|
|
var objects = tableView.getObjectsUnderPoint(point);
|
|
objects.reverse();
|
|
var pointPart:PartView = null;
|
|
for (object in objects) {
|
|
if (Std.is(object, PartView)) {
|
|
var part:PartView = cast object;
|
|
var partPoint = part.globalToLocal(point);
|
|
var color = part.image.shadedImage.getPixel(Std.int(partPoint.x), Std.int(partPoint.y));
|
|
if (color > 0) {
|
|
pointPart = part;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (pointPart != null) {
|
|
event.stopImmediatePropagation();
|
|
if (event.ctrlKey) {
|
|
save(pointPart);
|
|
return;
|
|
}
|
|
if (pointPart.completed) {
|
|
return;
|
|
}
|
|
activePart = pointPart;
|
|
tableView.setChildIndex(activePart, tableView.numChildren - 1);
|
|
activePoint = tableView.globalToLocal(point);
|
|
tableView.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
|
|
tableView.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
|
|
actions.emit(new GameAction().setAction(Action.TAKE).setPlayerId(playerId).setPartId(activePart.id));
|
|
}
|
|
}
|
|
|
|
private function onMouseMove(event:MouseEvent):Void {
|
|
var newPoint:PointExt = tableView.globalToLocal(new FlashPoint(event.stageX, event.stageY));
|
|
var partPosition = cast(activePart.position, PointExt).add(newPoint).subtract(activePoint);
|
|
actions.emit(new GameAction().setAction(Action.MOVE).setPlayerId(playerId).setPartId(activePart.id).setPosition(partPosition));
|
|
activePoint = newPoint;
|
|
}
|
|
|
|
private function onMouseUp(event:MouseEvent):Void {
|
|
var newPoint:PointExt = tableView.globalToLocal(new FlashPoint(event.stageX, event.stageY));
|
|
var partPosition = cast(activePart.position, PointExt).add(newPoint).subtract(activePoint);
|
|
actions.emit(new GameAction().setAction(Action.PUT).setPlayerId(playerId).setPartId(activePart.id).setPosition(partPosition));
|
|
tableView.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
|
|
tableView.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
|
|
activePart = null;
|
|
activePoint = null;
|
|
}
|
|
|
|
private function save(part:PartView):Void {
|
|
var file = new FileReference();
|
|
var image = part.image.shadedImage;
|
|
var data = new ByteArray();
|
|
image.encode(new Rectangle(0, 0, image.width, image.height), new PNGEncoderOptions(), data);
|
|
file.save(data, "icon.png");
|
|
}
|
|
|
|
public function clean() {
|
|
for (partView in parts) {
|
|
if (partView.parent != null) {
|
|
partView.parent.removeChild(partView);
|
|
}
|
|
}
|
|
parts = new Map();
|
|
imageView.clean();
|
|
}
|
|
}
|