[update]
This commit is contained in:
27
src/haxe/ru/m/puzzlez/LinuxIcon.hx
Normal file
27
src/haxe/ru/m/puzzlez/LinuxIcon.hx
Normal file
@@ -0,0 +1,27 @@
|
||||
package ru.m.puzzlez;
|
||||
|
||||
import flash.display.BitmapData;
|
||||
import flash.filters.ColorMatrixFilter;
|
||||
import flash.geom.Point;
|
||||
import flash.Lib;
|
||||
import openfl.Assets;
|
||||
|
||||
class LinuxIcon {
|
||||
|
||||
private static function prepareIcon(bitmap:BitmapData):BitmapData {
|
||||
var matrix:Array<Float> = [];
|
||||
matrix = matrix.concat([0, 0, 1, 0, 0]);
|
||||
matrix = matrix.concat([0, 1, 0, 0, 0]);
|
||||
matrix = matrix.concat([1, 0, 0, 0, 0]);
|
||||
matrix = matrix.concat([0, 0, 0, 1, 0]);
|
||||
var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix);
|
||||
var bitmap:BitmapData = bitmap.clone();
|
||||
bitmap.applyFilter(bitmap, bitmap.rect, new Point(0, 0), cmf);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public static function apply() {
|
||||
var icon = Assets.getBitmapData("resources/icon.png");
|
||||
Lib.current.stage.window.setIcon(prepareIcon(icon).image);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,9 @@ class PuzzlezApp extends App {
|
||||
|
||||
public static function main() {
|
||||
L.push(new TraceLogger());
|
||||
#if linux
|
||||
LinuxIcon.apply();
|
||||
#end
|
||||
var app = new PuzzlezApp(new PuzzlezTheme());
|
||||
app.start(new PuzzlezAppView());
|
||||
L.d("Puzzlez", "started");
|
||||
|
||||
@@ -20,14 +20,24 @@ class Game implements IGame {
|
||||
}
|
||||
|
||||
public function start():Void {
|
||||
shuffle();
|
||||
signal.emit(GameEvent.START(state));
|
||||
shuffle();
|
||||
}
|
||||
|
||||
public function shuffle():Void {
|
||||
for (part in state.parts) {
|
||||
part.rect.x = Math.random() * (state.preset.tableRect.width - part.rect.width);
|
||||
part.rect.y = Math.random() * (state.preset.tableRect.height - part.rect.height);
|
||||
var bound = part.rect.width * 0.25;
|
||||
part.rect.x = bound + Math.random() * (state.preset.tableRect.width - part.rect.width - bound * 2);
|
||||
part.rect.y = bound + Math.random() * (state.preset.tableRect.height - part.rect.height - bound * 2);
|
||||
signal.emit(GameEvent.PART_MOVED(part.id, part.rect.topLeft));
|
||||
}
|
||||
}
|
||||
|
||||
public function collect():Void {
|
||||
for (part in state.parts) {
|
||||
part.rect.x = part.position.x;
|
||||
part.rect.y = part.position.y;
|
||||
signal.emit(GameEvent.PART_MOVED(part.id, part.rect.topLeft));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -64,9 +64,10 @@ class GameUtil {
|
||||
|
||||
public static function buildPreset(image:ImageSource):GamePreset {
|
||||
var size = 8;
|
||||
var offset = 100;
|
||||
var imageRect = new Rectangle(offset, offset, 1024, 1024);
|
||||
var tableRect = new Rectangle(0, 0, imageRect.width + offset * 2, imageRect.height + offset * 2);
|
||||
var offsetX = 500;
|
||||
var offsetY = 200;
|
||||
var imageRect = new Rectangle(offsetX, offsetY, 1024, 1024);
|
||||
var tableRect = new Rectangle(0, 0, imageRect.width + offsetX * 2, imageRect.height + offsetY * 2);
|
||||
return {
|
||||
image:image,
|
||||
grid: {width: size, height: size},
|
||||
|
||||
@@ -8,5 +8,7 @@ interface IGame {
|
||||
|
||||
public function start():Void;
|
||||
public function stop():Void;
|
||||
public function shuffle():Void;
|
||||
public function collect():Void;
|
||||
public function dispose():Void;
|
||||
}
|
||||
|
||||
@@ -13,13 +13,21 @@ class PartView extends Sprite {
|
||||
|
||||
private function set_position(value:Point):Point {
|
||||
position = value.clone();
|
||||
x = position.x - (bitmap.bitmapData.width - size.x) / 2;
|
||||
y = position.y - (bitmap.bitmapData.height - size.y) / 2;
|
||||
refresh();
|
||||
return position;
|
||||
}
|
||||
|
||||
public var image(default, set):BitmapData;
|
||||
|
||||
private function set_image(value:BitmapData):BitmapData {
|
||||
image = value;
|
||||
bitmap.bitmapData = image;
|
||||
refresh();
|
||||
return image;
|
||||
}
|
||||
|
||||
private var size:Point;
|
||||
public var bitmap:Bitmap;
|
||||
private var bitmap:Bitmap;
|
||||
|
||||
public function new(id:Int, image:BitmapData, size:Point) {
|
||||
super();
|
||||
@@ -29,4 +37,10 @@ class PartView extends Sprite {
|
||||
addChild(bitmap);
|
||||
}
|
||||
|
||||
private function refresh():Void {
|
||||
if (position != null && image != null) {
|
||||
x = position.x - (bitmap.bitmapData.width - size.x) / 2;
|
||||
y = position.y - (bitmap.bitmapData.height - size.y) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class Render extends SpriteView implements IRender {
|
||||
|
||||
private function set_scale(value:Float):Float {
|
||||
var result = table.scaleX = table.scaleY = value;
|
||||
setSize(table.width, table.height, 'table');
|
||||
//setSize(table.width, table.height, 'table');
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -59,19 +59,13 @@ class Render extends SpriteView implements IRender {
|
||||
private function onStart(state:GameState):Void {
|
||||
clean();
|
||||
this.state = state;
|
||||
RenderUtil.resolveImage(state.preset.image).then(start);
|
||||
}
|
||||
|
||||
private function start(image:BitmapData):Void {
|
||||
this.image = RenderUtil.cropImage(image, state.preset.imageRect);
|
||||
for (part in state.parts) {
|
||||
var partImage = RenderUtil.cropImagePart(this.image, part);
|
||||
var partView = new PartView(part.id, partImage, part.rect.size);
|
||||
var partView = new PartView(part.id, null, part.rect.size);
|
||||
partView.position = part.rect.topLeft;
|
||||
parts.set(part.id, partView);
|
||||
table.addChild(partView);
|
||||
}
|
||||
scale = scale;
|
||||
|
||||
table.graphics.lineStyle(2, 0xCCCCCC);
|
||||
table.graphics.beginFill(0x555555);
|
||||
table.graphics.drawRect(state.preset.tableRect.x, state.preset.tableRect.y, state.preset.tableRect.width, state.preset.tableRect.height);
|
||||
@@ -83,10 +77,26 @@ class Render extends SpriteView implements IRender {
|
||||
table.graphics.drawRect(state.preset.imageRect.x, state.preset.imageRect.y, state.preset.imageRect.width, state.preset.imageRect.height);
|
||||
table.graphics.endFill();
|
||||
table.graphics.lineStyle();
|
||||
|
||||
RenderUtil.resolveImage(state.preset.image).then(onImageResolved);
|
||||
toUpdate();
|
||||
}
|
||||
|
||||
private function onImageResolved(image:BitmapData):Void {
|
||||
this.image = RenderUtil.cropImage(image, state.preset.imageRect);
|
||||
for (part in state.parts) {
|
||||
var partImage = RenderUtil.cropImagePart(this.image, part);
|
||||
var partView = parts[part.id];
|
||||
partView.image = partImage;
|
||||
}
|
||||
}
|
||||
|
||||
override public function update():Void {
|
||||
super.update();
|
||||
if (state != null) {
|
||||
scale = Math.min(width / state.preset.tableRect.width, height / state.preset.tableRect.height);
|
||||
}
|
||||
}
|
||||
|
||||
private function onMouseDown(event:MouseEvent):Void {
|
||||
if (Std.is(event.target, PartView)) {
|
||||
activePart = event.target;
|
||||
@@ -117,9 +127,9 @@ class Render extends SpriteView implements IRender {
|
||||
|
||||
private function save(part:PartView):Void {
|
||||
var file = new FileReference();
|
||||
var bitmapData = part.bitmap.bitmapData;
|
||||
var image = part.image;
|
||||
var data = new ByteArray();
|
||||
bitmapData.encode(new Rectangle(0, 0, bitmapData.width, bitmapData.height), new PNGEncoderOptions(), data);
|
||||
image.encode(new Rectangle(0, 0, image.width, image.height), new PNGEncoderOptions(), data);
|
||||
file.save(data, "icon.png");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package ru.m.puzzlez.view;
|
||||
|
||||
import haxework.view.form.ButtonView;
|
||||
import haxework.net.JsonLoader;
|
||||
import haxework.view.data.DataView;
|
||||
import haxework.view.form.ButtonView;
|
||||
import haxework.view.group.VGroupView;
|
||||
import haxework.view.ImageView;
|
||||
import haxework.view.list.ScrollBarView;
|
||||
import haxework.view.utils.DrawUtil;
|
||||
import openfl.utils.Assets;
|
||||
import openfl.utils.AssetType;
|
||||
@@ -24,16 +23,13 @@ typedef PixabayResponse = {
|
||||
|
||||
@:template class PuzzlezAppView extends VGroupView {
|
||||
|
||||
@:view private var scale:ScrollBarView;
|
||||
@:view private var images:DataView<ImageSource, ImageView>;
|
||||
@:view private var render:IRender;
|
||||
private var game:IGame;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
images.data = [for (name in Assets.list(AssetType.IMAGE)) ASSET(name)];
|
||||
render.scale = 0.75;
|
||||
scale.position = render.scale - scale.ratio;
|
||||
images.data = [for (name in Assets.list(AssetType.IMAGE).filter(function(name:String) return name.substr(0, 15) == "resources/image")) ASSET(name)];
|
||||
}
|
||||
|
||||
private function imageViewFactory(index:Int, image:ImageSource):ImageView {
|
||||
@@ -47,6 +43,7 @@ typedef PixabayResponse = {
|
||||
|
||||
private function moreImages(view:ButtonView):Void {
|
||||
view.visible = false;
|
||||
view.toUpdateParent();
|
||||
new JsonLoader<PixabayResponse>()
|
||||
.GET('https://pixabay.com/api/?key=14915210-5eae157281211e0ad28bc8def&category=nature')
|
||||
.then(function(result:PixabayResponse) {
|
||||
@@ -54,10 +51,6 @@ typedef PixabayResponse = {
|
||||
});
|
||||
}
|
||||
|
||||
public function setScale(value:Float):Void {
|
||||
render.scale = value + scale.ratio;
|
||||
}
|
||||
|
||||
public function start(image:ImageSource):Void {
|
||||
stop();
|
||||
game = new Game(GameUtil.buildPreset(image));
|
||||
|
||||
@@ -12,6 +12,12 @@ views:
|
||||
- $type: haxework.view.form.LabelView
|
||||
text: Puzzle'z
|
||||
font.size: 42
|
||||
- $type: haxework.view.form.ButtonView
|
||||
text: Shuffle
|
||||
+onPress: ~game.shuffle()
|
||||
- $type: haxework.view.form.ButtonView
|
||||
text: Collect
|
||||
+onPress: ~game.collect()
|
||||
- id: images
|
||||
$type: haxework.view.data.DataView
|
||||
layout:
|
||||
@@ -22,20 +28,17 @@ views:
|
||||
+onDataSelect: ~start
|
||||
geometry.margin: 5
|
||||
overflow.y: scroll
|
||||
- id: more
|
||||
$type: haxework.view.form.ButtonView
|
||||
- $type: haxework.view.form.ButtonView
|
||||
text: More
|
||||
+onPress: ~moreImages
|
||||
- id: scale
|
||||
$type: haxework.view.list.VScrollBarView
|
||||
ratio: 0.5
|
||||
+onScroll: ~setScale
|
||||
- $type: haxework.view.group.GroupView
|
||||
style: frame
|
||||
geometry.width: 100%
|
||||
geometry.height: 100%
|
||||
overflow.x: scroll
|
||||
overflow.y: scroll
|
||||
overflow.x: crop
|
||||
overflow.y: crop
|
||||
views:
|
||||
- id: render
|
||||
$type: ru.m.puzzlez.render.Render
|
||||
geometry.width: 100%
|
||||
geometry.height: 100%
|
||||
|
||||
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 23 KiB |
|
Before Width: | Height: | Size: 160 KiB After Width: | Height: | Size: 160 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1.9 MiB |
Binary file not shown.
|
Before Width: | Height: | Size: 1.8 MiB |
Reference in New Issue
Block a user