[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

@@ -13,7 +13,7 @@ class LevelBundle {
return result; return result;
} }
public static function parse(config:Config, data:String):Array<BrickConfig> { public static function loads(config:Config, data:String):Array<BrickConfig> {
var bricks:Array<BrickConfig> = []; var bricks:Array<BrickConfig> = [];
for (line in ~/\s+/g.split(data)) { for (line in ~/\s+/g.split(data)) {
for (c in line.split('')) { for (c in line.split('')) {
@@ -25,8 +25,12 @@ class LevelBundle {
return bricks; return bricks;
} }
public static function dumps(config:Config, bricks:Array<BrickConfig>):String {
return bricks.map(function(brick:BrickConfig) return brick.type).join('');
}
public static function get(type:GameType, config:Config, level:Int):Array<BrickConfig> { public static function get(type:GameType, config:Config, level:Int):Array<BrickConfig> {
var data:String = Assets.getText('resources/${type}/levels/level${formatLevel(level)}.txt'); var data:String = Assets.getText('resources/${type}/levels/level${formatLevel(level)}.txt');
return parse(config, data); return loads(config, data);
} }
} }

View File

@@ -1,6 +1,8 @@
package ru.m.tankz.editor; 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.Config;
import ru.m.tankz.config.LevelBundle; import ru.m.tankz.config.LevelBundle;
import ru.m.tankz.game.ClassicGame; import ru.m.tankz.game.ClassicGame;
@@ -20,11 +22,13 @@ import haxework.log.SocketLogger;
interface EditorViewLayout { interface EditorViewLayout {
var openButton(default, null):ButtonView; var openButton(default, null):ButtonView;
var saveButton(default, null):ButtonView;
var fileNameLabel(default, null):LabelView;
var mapView(default, null):MapEditView; 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 EditorView extends GroupView implements ViewBuilder implements EditorViewLayout {}
class Editor { class Editor {
@@ -62,6 +66,7 @@ class Editor {
view.content.stage.stageFocusRect = false; view.content.stage.stageFocusRect = false;
view.openButton.onPress = this; view.openButton.onPress = this;
view.saveButton.onPress = this;
config = ConfigBundle.get(ClassicGame.TYPE); config = ConfigBundle.get(ClassicGame.TYPE);
view.mapView.config = config.map; view.mapView.config = config.map;
@@ -71,8 +76,15 @@ class Editor {
switch (v.id) { switch (v.id) {
case 'openButton': case 'openButton':
L.d(TAG, 'OPEN'); L.d(TAG, 'OPEN');
FileUtil.browse().then(function(data) { FileUtil.browse().then(function(content:FileContent) {
view.mapView.data = LevelBundle.parse(config, data); 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 _: 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; import promhx.Promise;
typedef FileContent = {
var name:String;
var content:String;
}
class FileUtil { class FileUtil {
public static function browse():Promise<String> { public static function browse():Promise<FileContent> {
var d = new Deferred<String>(); var d = new Deferred<FileContent>();
var file = new FileReference(); var file = new FileReference();
file.addEventListener(Event.SELECT, function(event:Event) { file.addEventListener(Event.SELECT, function(event:Event) {
cast(event.target, FileReference).load(); cast(event.target, FileReference).load();
@@ -23,12 +29,20 @@ class FileUtil {
trace('progress', '${event}'); trace('progress', '${event}');
}); });
file.addEventListener(Event.COMPLETE, function(event:Event) { file.addEventListener(Event.COMPLETE, function(event:Event) {
var bytes = cast(event.target, FileReference).data; var f:FileReference = cast event.target;
var data = bytes.readUTFBytes(bytes.length); var data = f.data.readUTFBytes(f.data.length);
d.resolve(data); d.resolve({
name: f.name,
content: data
});
}); });
file.browse(); file.browse();
return d.promise(); 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; package ru.m.tankz.editor;
import ru.m.tankz.map.Brick;
import flash.display.DisplayObjectContainer; import flash.display.DisplayObjectContainer;
import flash.display.Graphics; import flash.display.Graphics;
import flash.display.Sprite; import flash.display.Sprite;
@@ -13,7 +14,7 @@ import haxework.gui.SpriteView;
class MapEditView extends SpriteView { class MapEditView extends SpriteView {
public var config(default, set):MapConfig; 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; public var map(default, null):LevelMap;
private var items:Map<String, RenderItem<Dynamic, Dynamic>>; private var items:Map<String, RenderItem<Dynamic, Dynamic>>;
@@ -93,11 +94,14 @@ class MapEditView extends SpriteView {
return config; 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> { private function set_data(value:Array<BrickConfig>):Array<BrickConfig> {
data = value;
reset(); reset();
map.setData(value); map.setData(value);
invalidate(); invalidate();
return data; return value;
} }
} }