[add] PresetView

This commit is contained in:
2020-01-23 12:41:56 +03:00
parent 843a0e840d
commit c81821fa95
5 changed files with 127 additions and 19 deletions

View File

@@ -7,6 +7,7 @@ class PuzzlezTheme extends Theme {
public function new() {
super({name: "Georgia"}, {light: "gray"}, {base: 22});
data.get("frame").data.set("geometry.padding", Box.fromFloat(8));
register(new Style("view", [
"skin.background.color" => colors.light,
"skin.border.color" => colors.border,

View File

@@ -63,7 +63,13 @@ class BoundsMap {
class GameUtil {
private static function normilizeSize(size:Int, maxValue:Int = 8):Int {
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 {
width = normilizeSize(width);
height = normilizeSize(height);
var offsetX = 500;
var offsetY = 200;
var s = width / height;

View File

@@ -1,7 +1,5 @@
package ru.m.puzzlez.view;
import haxework.view.form.InputView;
import openfl.Assets;
import haxework.view.ImageView;
import haxework.view.frame.FrameSwitcher;
import haxework.view.frame.FrameView;
import ru.m.puzzlez.core.GameUtil;
@@ -11,7 +9,7 @@ import ru.m.puzzlez.storage.ImageStorage;
@:template class PresetFrame extends FrameView<ImageId> {
public static var ID = "preset";
@:view("image") var imageView:ImageView;
@:view("image") var imageView:PresetView;
@:view("width") var widthView:InputView;
@:view("height") var heightView:InputView;
@@ -24,18 +22,22 @@ import ru.m.puzzlez.storage.ImageStorage;
super(ID);
widthView.text = "8";
heightView.text = "8";
widthView.update();
heightView.update();
}
override public function onShow(data:ImageId):Void {
super.onShow(data);
imageId = data;
imageView.image = Assets.getBitmapData("resources/icon.png");
imageStorage.resolve(imageId).then(function(image) imageView.image = image);
updatePreset();
}
private function updatePreset():Void {
imageView.preset = GameUtil.buildPreset(imageId, Std.parseInt(widthView.text), Std.parseInt(heightView.text));
}
private function start():Void {
var preset = GameUtil.buildPreset(imageId, Std.parseInt(widthView.text), Std.parseInt(heightView.text));
switcher.change(GameFrame.ID, preset);
switcher.change(GameFrame.ID, imageView.preset);
}
private function back():Void {

View File

@@ -1,20 +1,28 @@
---
style: frame
views:
- $type: haxework.view.group.HGroupView
geometry.width: 100%
layout.hAlign: center
layout.vAlign: middle
views:
- id: width
$type: haxework.view.form.InputView
geometry.width: 100
+onChange: ~updatePreset()
- $type: haxework.view.form.LabelView
text: x
- id: height
$type: haxework.view.form.InputView
geometry.width: 100
+onChange: ~updatePreset()
- $type: haxework.view.form.ButtonView
geometry.margin.left: 15
text: Start
+onPress: ~start()
- id: image
$type: haxework.view.ImageView
stretch: false
fillType: cover
$type: ru.m.puzzlez.view.PresetView
geometry.stretch: true
- id: width
$type: haxework.view.form.InputView
geometry.width: 100
- id: height
$type: haxework.view.form.InputView
geometry.width: 100
- $type: haxework.view.form.ButtonView
text: Start
+onPress: ~start()
- $type: haxework.view.form.ButtonView
text: Back
geometry.position: absolute

View File

@@ -0,0 +1,91 @@
package ru.m.puzzlez.view;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Shape;
import flash.geom.Matrix;
import haxework.view.SpriteView;
import ru.m.puzzlez.core.GamePreset;
import ru.m.puzzlez.render.RenderUtil;
import ru.m.puzzlez.storage.ImageStorage;
class PresetView extends SpriteView {
@:provide var imageStorage:ImageStorage;
public var scale(get, set):Float;
private function get_scale():Float {
return table.scaleX;
}
private function set_scale(value:Float):Float {
var result = table.scaleX = table.scaleY = value;
table.x = (width - preset.tableRect.width * value) / 2;
table.y = (height - preset.tableRect.height * value) / 2;
return result;
}
public var preset(default, set):GamePreset;
private function set_preset(value:GamePreset) {
preset = value;
table.graphics.clear();
imageStorage.resolve(preset.image).then(function(image) {
this.image = RenderUtil.cropImage(image, preset.imageRect);
toRedraw();
toUpdate();
});
return preset;
}
public function new() {
super();
table = new Shape();
content.addChild(table);
}
private var table:Shape;
private var image:BitmapData;
override public function redraw():Void {
if (preset == null || image == null) {
return;
}
var partWidth = preset.imageRect.width / preset.grid.width;
var partHeight = preset.imageRect.height / preset.grid.height;
var graphics:Graphics = table.graphics;
graphics.clear();
var matrix = new Matrix();
matrix.translate(preset.imageRect.x, preset.imageRect.y);
graphics.beginBitmapFill(image, matrix, false, true);
graphics.drawRect(preset.imageRect.x, preset.imageRect.y, preset.imageRect.width, preset.imageRect.height);
graphics.endFill();
graphics.lineStyle(1, 0x555555);
for (x in 0...preset.grid.width + 1) {
graphics.moveTo(preset.imageRect.x + x * partWidth, preset.imageRect.y);
graphics.lineTo(preset.imageRect.x + x * partWidth, preset.imageRect.y + preset.imageRect.height);
}
for (y in 0...preset.grid.height + 1) {
graphics.moveTo(preset.imageRect.x, preset.imageRect.y + y * partHeight);
graphics.lineTo(preset.imageRect.x + preset.imageRect.width, preset.imageRect.y + y * partHeight);
}
graphics.lineStyle(1, 0xcccccc);
var offset = 1;
for (x in 0...preset.grid.width + 1) {
graphics.moveTo(offset + preset.imageRect.x + x * partWidth, preset.imageRect.y);
graphics.lineTo(offset + preset.imageRect.x + x * partWidth, preset.imageRect.y + preset.imageRect.height);
}
for (y in 0...preset.grid.height + 1) {
graphics.moveTo(preset.imageRect.x, offset + preset.imageRect.y + y * partHeight);
graphics.lineTo(preset.imageRect.x + preset.imageRect.width, offset + preset.imageRect.y + y * partHeight);
}
graphics.lineStyle();
}
override public function update():Void {
super.update();
if (preset != null) {
scale = Math.min(width / preset.tableRect.width, height / preset.tableRect.height);
}
}
}