[editor] added save button

This commit is contained in:
2018-02-05 17:54:16 +03:00
parent 421f925a60
commit b36fd77d74
6 changed files with 85 additions and 50 deletions

View File

@@ -1,6 +1,8 @@
package ru.m.tankz.editor;
import ru.m.tankz.editor.FileUtil.FileContent;
import haxework.gui.LabelView;
import ru.m.tankz.config.Config;
import ru.m.tankz.config.LevelBundle;
import ru.m.tankz.game.ClassicGame;
@@ -20,11 +22,13 @@ import haxework.log.SocketLogger;
interface EditorViewLayout {
var openButton(default, null):ButtonView;
var saveButton(default, null):ButtonView;
var fileNameLabel(default, null):LabelView;
var mapView(default, null):MapEditView;
}
@:template("ru/m/tankz/editor/Editor.json")
@:template("ru/m/tankz/editor/Editor.yaml")
class EditorView extends GroupView implements ViewBuilder implements EditorViewLayout {}
class Editor {
@@ -62,6 +66,7 @@ class Editor {
view.content.stage.stageFocusRect = false;
view.openButton.onPress = this;
view.saveButton.onPress = this;
config = ConfigBundle.get(ClassicGame.TYPE);
view.mapView.config = config.map;
@@ -71,8 +76,15 @@ class Editor {
switch (v.id) {
case 'openButton':
L.d(TAG, 'OPEN');
FileUtil.browse().then(function(data) {
view.mapView.data = LevelBundle.parse(config, data);
FileUtil.browse().then(function(content:FileContent) {
view.fileNameLabel.text = content.name;
view.mapView.data = LevelBundle.loads(config, content.content);
});
case 'saveButton':
L.d(TAG, 'SAVE');
FileUtil.save({
name: view.fileNameLabel.text,
content: LevelBundle.dumps(config, view.mapView.data),
});
case _:
}

View File

@@ -1,37 +0,0 @@
{
"@type": "haxework.gui.GroupView",
"pWidth": 100, "pHeight": 100,
"views": [
{
"@type": "haxework.gui.VGroupView",
"pWidth": 100, "pHeight": 100,
"views": [
{
"@type": "haxework.gui.HGroupView",
"pWidth": 100, "height": 20,
"views": [
{
"id": "openButton",
"@type": "haxework.gui.ButtonView",
"text": "Open", "contentSize": true,
"skin": {"@type": "haxework.gui.skin.ButtonColorSkin", "color": "#aaff00"}
}
]
},
{
"id": "mapView",
"@type": "ru.m.tankz.editor.MapEditView",
"contentSize": true
}
]
},
{
"@type": "haxework.gui.LabelView",
"inLayout": false,
"contentSize": true,
"vAlign": "BOTTOM",
"hAlign": "RIGHT",
"text": "@res:text:version"
}
]
}

View File

@@ -0,0 +1,38 @@
$type: haxework.gui.GroupView
pWidth: 100
pHeight: 100
views:
- $type: haxework.gui.VGroupView
pWidth: 100
pHeight: 100
views:
- $type: haxework.gui.HGroupView
pWidth: 100
height: 20
views:
- id: openButton
$type: haxework.gui.ButtonView
text: Open
contentSize: true
skin:
$type: haxework.gui.skin.ButtonColorSkin
color: 0xaaff00
- id: saveButton
$type: haxework.gui.ButtonView
text: Save
contentSize: true
skin:
$type: haxework.gui.skin.ButtonColorSkin
color: 0xaaff00
- id: fileNameLabel
$type: haxework.gui.LabelView
contentSize: true
- id: mapView
$type: ru.m.tankz.editor.MapEditView
contentSize: true
- $type: haxework.gui.LabelView
inLayout: false
contentSize: true
vAlign: BOTTOM
hAlign: RIGHT
text: '@res:text:version'

View File

@@ -8,10 +8,16 @@ import flash.net.FileReference;
import promhx.Promise;
typedef FileContent = {
var name:String;
var content:String;
}
class FileUtil {
public static function browse():Promise<String> {
var d = new Deferred<String>();
public static function browse():Promise<FileContent> {
var d = new Deferred<FileContent>();
var file = new FileReference();
file.addEventListener(Event.SELECT, function(event:Event) {
cast(event.target, FileReference).load();
@@ -23,12 +29,20 @@ class FileUtil {
trace('progress', '${event}');
});
file.addEventListener(Event.COMPLETE, function(event:Event) {
var bytes = cast(event.target, FileReference).data;
var data = bytes.readUTFBytes(bytes.length);
d.resolve(data);
var f:FileReference = cast event.target;
var data = f.data.readUTFBytes(f.data.length);
d.resolve({
name: f.name,
content: data
});
});
file.browse();
return d.promise();
}
public static function save(content:FileContent):Void {
var file = new FileReference();
file.save(content.content, content.name);
}
}

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.editor;
import ru.m.tankz.map.Brick;
import flash.display.DisplayObjectContainer;
import flash.display.Graphics;
import flash.display.Sprite;
@@ -13,7 +14,7 @@ import haxework.gui.SpriteView;
class MapEditView extends SpriteView {
public var config(default, set):MapConfig;
public var data(default, set):Array<BrickConfig>;
public var data(get, set):Array<BrickConfig>;
public var map(default, null):LevelMap;
private var items:Map<String, RenderItem<Dynamic, Dynamic>>;
@@ -93,11 +94,14 @@ class MapEditView extends SpriteView {
return config;
}
private function get_data():Array<BrickConfig> {
return map.bricks.map(function(brick:Brick) return brick.config);
}
private function set_data(value:Array<BrickConfig>):Array<BrickConfig> {
data = value;
reset();
map.setData(value);
invalidate();
return data;
return value;
}
}