[editor] added editor module
This commit is contained in:
81
src/editor/haxe/ru/m/tankz/editor/Editor.hx
Normal file
81
src/editor/haxe/ru/m/tankz/editor/Editor.hx
Normal file
@@ -0,0 +1,81 @@
|
||||
package ru.m.tankz.editor;
|
||||
|
||||
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.config.LevelBundle;
|
||||
import ru.m.tankz.game.ClassicGame;
|
||||
import ru.m.tankz.config.ConfigBundle;
|
||||
import haxework.gui.ButtonView;
|
||||
import haxework.gui.Root;
|
||||
import flash.text.Font;
|
||||
import haxework.resources.Resources;
|
||||
import haxework.resources.IResources;
|
||||
import haxework.provider.Provider;
|
||||
import haxework.log.TraceLogger;
|
||||
import haxework.gui.ViewBuilder;
|
||||
import haxework.gui.GroupView;
|
||||
import haxework.log.JSLogger;
|
||||
import haxework.log.SocketLogger;
|
||||
|
||||
|
||||
interface EditorViewLayout {
|
||||
var openButton(default, null):ButtonView;
|
||||
var mapView(default, null):MapEditView;
|
||||
}
|
||||
|
||||
|
||||
@:template("ru/m/tankz/editor/Editor.json")
|
||||
class EditorView extends GroupView implements ViewBuilder implements EditorViewLayout {}
|
||||
|
||||
class Editor {
|
||||
|
||||
private static inline var TAG = "Tankz.Editor";
|
||||
|
||||
public static function main() {
|
||||
L.push(new TraceLogger());
|
||||
#if flash
|
||||
L.push(new JSLogger());
|
||||
#end
|
||||
#if debug
|
||||
L.push(new SocketLogger());
|
||||
#end
|
||||
Const.init();
|
||||
L.d(TAG, "Debug: " + Const.DEBUG);
|
||||
L.i(TAG, "Version: " + Const.VERSION);
|
||||
L.i(TAG, "Build: " + Const.BUILD);
|
||||
new Editor();
|
||||
}
|
||||
|
||||
|
||||
private var view:EditorView;
|
||||
private var config:Config;
|
||||
|
||||
public function new() {
|
||||
Provider.setFactory(IResources, Resources);
|
||||
|
||||
var font:Font = Font.enumerateFonts()[0];
|
||||
Provider.get(IResources).text.put("font", "Bookman Old Style");
|
||||
Provider.get(IResources).text.put("version", 'v${Const.VERSION} b${Const.BUILD}');
|
||||
|
||||
view = new EditorView();
|
||||
Root.bind(view);
|
||||
view.content.stage.stageFocusRect = false;
|
||||
|
||||
view.openButton.onPress = this;
|
||||
|
||||
config = ConfigBundle.get(ClassicGame.TYPE);
|
||||
view.mapView.config = config.map;
|
||||
}
|
||||
|
||||
public function onPress(v:ButtonView):Void {
|
||||
switch (v.id) {
|
||||
case 'openButton':
|
||||
L.d(TAG, 'OPEN');
|
||||
FileUtil.browse().then(function(data) {
|
||||
view.mapView.data = LevelBundle.parse(config, data);
|
||||
});
|
||||
case _:
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
37
src/editor/haxe/ru/m/tankz/editor/Editor.json
Normal file
37
src/editor/haxe/ru/m/tankz/editor/Editor.json
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"@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"
|
||||
}
|
||||
]
|
||||
}
|
||||
34
src/editor/haxe/ru/m/tankz/editor/FileUtil.hx
Normal file
34
src/editor/haxe/ru/m/tankz/editor/FileUtil.hx
Normal file
@@ -0,0 +1,34 @@
|
||||
package ru.m.tankz.editor;
|
||||
|
||||
import promhx.Deferred;
|
||||
import flash.events.ProgressEvent;
|
||||
import flash.events.IOErrorEvent;
|
||||
import flash.events.Event;
|
||||
import flash.net.FileReference;
|
||||
import promhx.Promise;
|
||||
|
||||
|
||||
class FileUtil {
|
||||
|
||||
public static function browse():Promise<String> {
|
||||
var d = new Deferred<String>();
|
||||
var file = new FileReference();
|
||||
file.addEventListener(Event.SELECT, function(event:Event) {
|
||||
cast(event.target, FileReference).load();
|
||||
});
|
||||
file.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent) {
|
||||
d.throwError(event);
|
||||
});
|
||||
file.addEventListener(ProgressEvent.PROGRESS, function(event:ProgressEvent) {
|
||||
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);
|
||||
});
|
||||
file.browse();
|
||||
return d.promise();
|
||||
}
|
||||
|
||||
}
|
||||
103
src/editor/haxe/ru/m/tankz/editor/MapEditView.hx
Normal file
103
src/editor/haxe/ru/m/tankz/editor/MapEditView.hx
Normal file
@@ -0,0 +1,103 @@
|
||||
package ru.m.tankz.editor;
|
||||
|
||||
import flash.display.DisplayObjectContainer;
|
||||
import flash.display.Graphics;
|
||||
import flash.display.Sprite;
|
||||
import ru.m.tankz.render.RenderItem;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.map.LevelMap;
|
||||
import haxework.gui.SpriteView;
|
||||
|
||||
|
||||
//ToDo: copy paste from ru.m.tankz.render.Render
|
||||
class MapEditView extends SpriteView {
|
||||
|
||||
public var config(default, set):MapConfig;
|
||||
public var data(default, set):Array<BrickConfig>;
|
||||
public var map(default, null):LevelMap;
|
||||
|
||||
private var items:Map<String, RenderItem<Dynamic, Dynamic>>;
|
||||
|
||||
private var backgroundLayer:Sprite;
|
||||
private var upLayer:Sprite;
|
||||
private var groundLayer:Sprite;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
items = new Map();
|
||||
map = null;
|
||||
backgroundLayer = new Sprite();
|
||||
upLayer = new Sprite();
|
||||
groundLayer = new Sprite();
|
||||
contentAsSprite.addChild(backgroundLayer);
|
||||
contentAsSprite.addChild(groundLayer);
|
||||
contentAsSprite.addChild(upLayer);
|
||||
reset();
|
||||
}
|
||||
|
||||
private function clearLayer(layer:DisplayObjectContainer) {
|
||||
while (layer.numChildren > 0) layer.removeChildAt(0);
|
||||
}
|
||||
|
||||
public function reset():Void {
|
||||
for (item in items.iterator()) {
|
||||
item.dispose();
|
||||
}
|
||||
items = new Map();
|
||||
clearLayer(groundLayer);
|
||||
clearLayer(upLayer);
|
||||
}
|
||||
|
||||
private function drawBackground():Void {
|
||||
var mapWidth = map.gridWidth * map.cellWidth;
|
||||
var mapHeight = map.gridHeight * map.cellHeight;
|
||||
var g:Graphics = backgroundLayer.graphics;
|
||||
g.clear();
|
||||
g.beginFill(0x000000);
|
||||
g.drawRect(0, 0, mapWidth, mapHeight);
|
||||
g.endFill();
|
||||
if (contentSize) {
|
||||
width = mapWidth;
|
||||
height = mapHeight;
|
||||
}
|
||||
}
|
||||
|
||||
override public function update():Void {
|
||||
if (this.map != null) {
|
||||
drawBackground();
|
||||
drawMap();
|
||||
}
|
||||
super.update();
|
||||
}
|
||||
|
||||
private function drawMap() {
|
||||
for (brick in map.bricks) if (brick.config.type > 0) {
|
||||
if (!items.exists(brick.key)) {
|
||||
items[brick.key] = new BrickItem(brick);
|
||||
if (brick.config.layer > 2) {
|
||||
upLayer.addChild(items[brick.key].view);
|
||||
} else {
|
||||
groundLayer.addChild(items[brick.key].view);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (item in items) {
|
||||
item.update();
|
||||
}
|
||||
}
|
||||
|
||||
private function set_config(value:MapConfig):MapConfig {
|
||||
config = value;
|
||||
map = new LevelMap(config);
|
||||
invalidate();
|
||||
return config;
|
||||
}
|
||||
|
||||
private function set_data(value:Array<BrickConfig>):Array<BrickConfig> {
|
||||
data = value;
|
||||
reset();
|
||||
map.setData(value);
|
||||
invalidate();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user