[update] :-)
This commit is contained in:
@@ -4,7 +4,7 @@ import flash.geom.Rectangle;
|
||||
import ru.m.puzzlez.core.Id;
|
||||
|
||||
typedef GamePreset = {
|
||||
var image:ImageId;
|
||||
var imageId:ImageId;
|
||||
var grid:Grid;
|
||||
var tableRect:Rectangle;
|
||||
var imageRect:Rectangle;
|
||||
|
||||
@@ -69,7 +69,7 @@ class GameUtil {
|
||||
return Math.isNaN(size) ? maxValue : size < 1 ? 1 : size > maxValue ? maxValue : size;
|
||||
}
|
||||
|
||||
public static function buildPreset(image:ImageId, width:Int = 8, height:Int = 8):GamePreset {
|
||||
public static function buildPreset(imageId:ImageId, width:Int = 8, height:Int = 8):GamePreset {
|
||||
width = normilizeSize(width);
|
||||
height = normilizeSize(height);
|
||||
var offsetX = 500;
|
||||
@@ -79,7 +79,7 @@ class GameUtil {
|
||||
var imageRect = new Rectangle(offsetX, offsetY, imageSize, imageSize / s);
|
||||
var tableRect = new Rectangle(0, 0, imageRect.width + offsetX * 2, imageRect.height + offsetY * 2);
|
||||
return {
|
||||
image:image,
|
||||
imageId: imageId,
|
||||
grid: {width: width, height: height},
|
||||
tableRect: tableRect,
|
||||
imageRect: imageRect,
|
||||
@@ -130,4 +130,15 @@ class GameUtil {
|
||||
parts: parts,
|
||||
}
|
||||
}
|
||||
|
||||
public static function calcProgress(state:GameState):{complete:Int, total:Int} {
|
||||
var complete = 0;
|
||||
for (part in state.parts) {
|
||||
switch part.location {
|
||||
case PartLocation.IMAGE: complete++;
|
||||
case _:
|
||||
}
|
||||
}
|
||||
return {complete:complete, total:state.parts.length};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ class Render extends SpriteView implements IRender {
|
||||
imageView.graphics.lineStyle();
|
||||
progress.text = "Loading image";
|
||||
content.addChild(progress.content);
|
||||
imageStorage.resolve(state.preset.image).then(onImageResolved);
|
||||
imageStorage.resolve(state.preset.imageId).then(onImageResolved);
|
||||
toUpdate();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,14 +11,18 @@ import ru.m.puzzlez.core.Id.ImageId;
|
||||
}
|
||||
|
||||
public function save(state:GameState):Void {
|
||||
write(state.preset.image, state);
|
||||
write(state.preset.imageId, state);
|
||||
}
|
||||
|
||||
public function load(imageId:ImageId):Null<GameState> {
|
||||
return read(imageId);
|
||||
}
|
||||
|
||||
public function list():Array<ImageId> {
|
||||
public function listIds():Array<ImageId> {
|
||||
return [for (key in Reflect.fields(so.data)) ImageId.fromString(key)];
|
||||
}
|
||||
|
||||
public function list():Array<GameState> {
|
||||
return [for (id in listIds()) load(id)];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,12 +44,7 @@ import ru.m.puzzlez.source.GameStorage;
|
||||
private function onGameEvent(event:GameEvent):Void {
|
||||
switch event {
|
||||
case ACTION(PART_PUT(_, _)):
|
||||
toSave = true;
|
||||
case CHANGE(PART_UPDATE(_, _)):
|
||||
if (toSave) {
|
||||
storage.save(game.state);
|
||||
toSave = false;
|
||||
}
|
||||
storage.save(game.state);
|
||||
case _:
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,15 @@ package ru.m.puzzlez.view;
|
||||
import haxework.view.data.DataView;
|
||||
import haxework.view.frame.FrameSwitcher;
|
||||
import haxework.view.frame.FrameView;
|
||||
import ru.m.puzzlez.core.Id;
|
||||
import ru.m.puzzlez.core.GameState;
|
||||
import ru.m.puzzlez.source.GameStorage;
|
||||
import ru.m.puzzlez.view.PuzzleImageView;
|
||||
|
||||
@:template class GameListFrame extends FrameView<Dynamic> {
|
||||
|
||||
public static var ID(default, never) = "game_list";
|
||||
|
||||
@:view var images:DataView<ImageId, PuzzleImageView>;
|
||||
@:view var images:ActionDataView<GameState, PuzzleImageView, Action>;
|
||||
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var storage:GameStorage;
|
||||
@@ -23,11 +24,18 @@ import ru.m.puzzlez.source.GameStorage;
|
||||
images.data = storage.list();
|
||||
}
|
||||
|
||||
private function start(image:ImageId):Void {
|
||||
var state = storage.load(image);
|
||||
private function start(state:GameState):Void {
|
||||
switcher.change(GameFrame.ID, state);
|
||||
}
|
||||
|
||||
private function onAction(state:GameState, action:Action):Void {
|
||||
switch action {
|
||||
case CLOSE:
|
||||
storage.delete(state.preset.imageId);
|
||||
images.data = storage.list();
|
||||
}
|
||||
}
|
||||
|
||||
private function back():Void {
|
||||
switcher.change(StartFrame.ID);
|
||||
}
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
style: frame
|
||||
views:
|
||||
- id: images
|
||||
$type: haxework.view.data.DataView
|
||||
$type: haxework.view.data.ActionDataView
|
||||
layout:
|
||||
$type: haxework.view.layout.TailLayout
|
||||
margin: 5
|
||||
vAlign: middle
|
||||
geometry.stretch: true
|
||||
factory: ~ru.m.puzzlez.view.PuzzleImageView.factory
|
||||
factory: ~ru.m.puzzlez.view.PuzzleImageView.stateFactory
|
||||
+onDataSelect: ~start
|
||||
+onDataAction: ~onAction
|
||||
geometry.margin: 5
|
||||
overflow.y: scroll
|
||||
- $type: haxework.view.form.ButtonView
|
||||
|
||||
@@ -31,7 +31,7 @@ class PresetView extends GroupView {
|
||||
preset = value;
|
||||
this.image = null;
|
||||
table.graphics.clear();
|
||||
loading.promise = imageStorage.resolve(preset.image).then(function(image) {
|
||||
loading.promise = imageStorage.resolve(preset.imageId).then(function(image) {
|
||||
this.image = RenderUtil.cropImage(image, preset.imageRect);
|
||||
toRedraw();
|
||||
toUpdate();
|
||||
|
||||
@@ -1,41 +1,69 @@
|
||||
package ru.m.puzzlez.view;
|
||||
|
||||
import haxework.view.data.DataView.ActionDataView;
|
||||
import haxework.view.form.LabelView;
|
||||
import haxework.view.group.GroupView;
|
||||
import haxework.view.ImageView;
|
||||
import haxework.view.utils.DrawUtil;
|
||||
import ru.m.puzzlez.core.GameState;
|
||||
import ru.m.puzzlez.core.GameUtil;
|
||||
import ru.m.puzzlez.core.Id;
|
||||
import ru.m.puzzlez.storage.ImageStorage;
|
||||
|
||||
class PuzzleImageView extends GroupView {
|
||||
enum Action {
|
||||
CLOSE;
|
||||
}
|
||||
|
||||
public var imageId(null, set):ImageId;
|
||||
@:template class PuzzleImageView extends GroupView {
|
||||
|
||||
public var imageId(default, set):ImageId;
|
||||
|
||||
private function set_imageId(value:ImageId):ImageId {
|
||||
if (imageId != value) {
|
||||
imageId = value;
|
||||
loading.promise = imageStorage.resolve(imageId, true).then(function(data) image.image = data);
|
||||
loading.promise = imageStorage.resolve(imageId, true).then(function(data) imageView.image = data);
|
||||
}
|
||||
return imageId;
|
||||
}
|
||||
|
||||
private var image:ImageView;
|
||||
private var loading:LoadingWrapper;
|
||||
public var text(default, set):String;
|
||||
|
||||
private function set_text(value:String):String {
|
||||
if (text != value) {
|
||||
text = value;
|
||||
labelView.text = text;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
@:view("image") var imageView:ImageView;
|
||||
@:view("label") var labelView:LabelView;
|
||||
@:provide var imageStorage:ImageStorage;
|
||||
private var loading:LoadingWrapper;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
style = "view";
|
||||
image = new ImageView();
|
||||
image.geometry.stretch = true;
|
||||
image.stretch = false;
|
||||
image.fillType = COVER;
|
||||
addView(image);
|
||||
loading = new LoadingWrapper(this);
|
||||
}
|
||||
|
||||
private function close():Void {
|
||||
var dataView:ActionDataView<Dynamic, PuzzleImageView, Action> = Std.instance(parent, ActionDataView);
|
||||
if (dataView != null) {
|
||||
var index = dataView.dataViews.indexOf(this);
|
||||
dataView.onDataAction.emit(dataView.data[index], CLOSE);
|
||||
}
|
||||
}
|
||||
|
||||
public static function factory(index:Int, imageId:ImageId):PuzzleImageView {
|
||||
var result = new PuzzleImageView();
|
||||
result.imageId = imageId;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static function stateFactory(index:Int, state:GameState):PuzzleImageView {
|
||||
var result = new PuzzleImageView();
|
||||
result.imageId = state.preset.imageId;
|
||||
var progress = GameUtil.calcProgress(state);
|
||||
result.text = '${progress.complete}/${progress.total}';
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
16
src/haxe/ru/m/puzzlez/view/PuzzleImageView.yaml
Normal file
16
src/haxe/ru/m/puzzlez/view/PuzzleImageView.yaml
Normal file
@@ -0,0 +1,16 @@
|
||||
---
|
||||
style: view
|
||||
views:
|
||||
- id: image
|
||||
$type: haxework.view.ImageView
|
||||
geometry.stretch: true
|
||||
stretch: false
|
||||
fillType: COVER
|
||||
- id: label
|
||||
$type: haxework.view.form.LabelView
|
||||
- id: close
|
||||
$type: haxework.view.form.ButtonView
|
||||
propagation: false
|
||||
geometry.hAlign: right
|
||||
text: X
|
||||
+onPress: ~close()
|
||||
Reference in New Issue
Block a user