diff --git a/src/common/haxe/ru/m/tankz/config/LevelBundle.hx b/src/common/haxe/ru/m/tankz/config/LevelBundle.hx index 4976dbd..c5bcf43 100644 --- a/src/common/haxe/ru/m/tankz/config/LevelBundle.hx +++ b/src/common/haxe/ru/m/tankz/config/LevelBundle.hx @@ -13,7 +13,7 @@ class LevelBundle { return result; } - public static function parse(config:Config, data:String):Array { + public static function loads(config:Config, data:String):Array { var bricks:Array = []; for (line in ~/\s+/g.split(data)) { for (c in line.split('')) { @@ -25,8 +25,12 @@ class LevelBundle { return bricks; } + public static function dumps(config:Config, bricks:Array):String { + return bricks.map(function(brick:BrickConfig) return brick.type).join(''); + } + public static function get(type:GameType, config:Config, level:Int):Array { var data:String = Assets.getText('resources/${type}/levels/level${formatLevel(level)}.txt'); - return parse(config, data); + return loads(config, data); } } diff --git a/src/editor/haxe/ru/m/tankz/editor/Editor.hx b/src/editor/haxe/ru/m/tankz/editor/Editor.hx index 66accb3..ff3819a 100644 --- a/src/editor/haxe/ru/m/tankz/editor/Editor.hx +++ b/src/editor/haxe/ru/m/tankz/editor/Editor.hx @@ -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 _: } diff --git a/src/editor/haxe/ru/m/tankz/editor/Editor.json b/src/editor/haxe/ru/m/tankz/editor/Editor.json deleted file mode 100644 index 27ae2b6..0000000 --- a/src/editor/haxe/ru/m/tankz/editor/Editor.json +++ /dev/null @@ -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" - } - ] -} \ No newline at end of file diff --git a/src/editor/haxe/ru/m/tankz/editor/Editor.yaml b/src/editor/haxe/ru/m/tankz/editor/Editor.yaml new file mode 100644 index 0000000..a4114ed --- /dev/null +++ b/src/editor/haxe/ru/m/tankz/editor/Editor.yaml @@ -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' diff --git a/src/editor/haxe/ru/m/tankz/editor/FileUtil.hx b/src/editor/haxe/ru/m/tankz/editor/FileUtil.hx index d3369bf..73095b3 100644 --- a/src/editor/haxe/ru/m/tankz/editor/FileUtil.hx +++ b/src/editor/haxe/ru/m/tankz/editor/FileUtil.hx @@ -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 { - var d = new Deferred(); + public static function browse():Promise { + var d = new Deferred(); 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); + } + } diff --git a/src/editor/haxe/ru/m/tankz/editor/MapEditView.hx b/src/editor/haxe/ru/m/tankz/editor/MapEditView.hx index 21bea35..53efb5e 100644 --- a/src/editor/haxe/ru/m/tankz/editor/MapEditView.hx +++ b/src/editor/haxe/ru/m/tankz/editor/MapEditView.hx @@ -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; + public var data(get, set):Array; public var map(default, null):LevelMap; private var items:Map>; @@ -93,11 +94,14 @@ class MapEditView extends SpriteView { return config; } + private function get_data():Array { + return map.bricks.map(function(brick:Brick) return brick.config); + } + private function set_data(value:Array):Array { - data = value; reset(); map.setData(value); invalidate(); - return data; + return value; } }