Browse Source

Better Data driven toolbar with ToolsObject

Jed 4 years ago
parent
commit
677e83d124
3 changed files with 65 additions and 76 deletions
  1. 0 11
      bin/defaultProps.json
  2. 52 1
      hide/comp/Toolbar.hx
  3. 13 64
      hide/view/l3d/Level3D.hx

+ 0 - 11
bin/defaultProps.json

@@ -121,17 +121,6 @@
 	"sceneeditor.icons" : {},
 	"sceneeditor.ranges" : {},
 	"sceneeditor.huds" : {},
-	"sceneeditor.tools" : ["perspectiveCamera", "topCamera", "snapToGroundToggle", "localTransformsToggle", "gridToggle", "bakeLights", "sceneInformationToggle", "backgroundColor", "autoSyncToggle", "sceneSpeed"],
-	"sceneeditor.perspectiveCamera": {"title" : "Perspective camera", "icon" : "video-camera", "type" : "Button"},
-	"sceneeditor.topCamera": {"title" : "Top camera", "icon" : "video-camera", "iconTransform" : "rotateZ(90deg)", "type" : "Button"},
-	"sceneeditor.snapToGroundToggle": {"title" : "Snap to ground", "icon" : "anchor", "type" : "Toggle"},
-	"sceneeditor.localTransformsToggle": {"title" : "Local transforms", "icon" : "compass", "type" : "Toggle"},
-	"sceneeditor.gridToggle": {"title" : "Toggle grid", "icon" : "th", "type" : "Toggle"},
-	"sceneeditor.bakeLights": {"title" : "Bake lights", "icon" : "lightbulb-o", "type" : "Button"},
-	"sceneeditor.sceneInformationToggle" : {"title" : "Scene information", "icon" : "info-circle", "type" : "Toggle", "customRightClick" : true},
-	"sceneeditor.backgroundColor" : {"title" : "Background Color", "type" : "Color"},
-	"sceneeditor.autoSyncToggle" : {"title" : "Auto synchronize", "icon" : "refresh", "type" : "Toggle"},
-	"sceneeditor.sceneSpeed" : {"title" : "Speed", "type" : "Range"},
 
 	// l3d config
 	"l3d.groundLayer": "ground",

+ 52 - 1
hide/comp/Toolbar.hx

@@ -1,5 +1,56 @@
 package hide.comp;
-
+enum ToolType {
+	Button;
+	Toggle;
+	Range;
+	Color;
+}
+class ToolsObject {
+	static public var level3D : hide.view.l3d.Level3D;
+	static var texContent : Element = null;
+	static public var tools : Map<String, {title : String, ?icon : String, type : ToolType, ?iconTransform : String, ?rightClick : Void -> Void, ?buttonFunction : Void -> Void, ?toggleFunction : Bool -> Void, ?rangeFunction : Float -> Void, ?colorFunction : Int -> Void}> = [
+		"perspectiveCamera" => {title : "Perspective camera", icon : "video-camera", type : Button, buttonFunction : () -> @:privateAccess level3D.resetCamera(false)},
+		"topCamera" => {title : "Top camera", icon : "video-camera", type : Button, iconTransform : "rotateZ(90deg)", buttonFunction : () -> @:privateAccess level3D.resetCamera(true)},
+		"snapToGroundToggle" => {title : "Snap to ground", icon : "anchor", type : Toggle, toggleFunction : (v) -> level3D.sceneEditor.snapToGround = v},
+		"localTransformsToggle"=> {title : "Local transforms", icon : "compass", type : Toggle, toggleFunction : (v) -> level3D.sceneEditor.localTransform = v},
+		"gridToggle" => {title : "Toggle grid", icon : "th", type : Toggle, toggleFunction : (v) -> { @:privateAccess level3D.showGrid = v; @:privateAccess level3D.updateGrid(); }},
+		"bakeLights" => {title : "Bake lights", icon : "lightbulb-o", type : Button, buttonFunction : () -> @:privateAccess level3D.bakeLights()},
+		"sceneeditor.sceneInformationToggle" => {title : "Scene information", icon : "info-circle", type : Toggle, toggleFunction : (b) -> @:privateAccess level3D.statusText.visible = b, rightClick : () -> {
+			if( texContent != null ) {
+				texContent.remove();
+				texContent = null;
+			}
+			new hide.comp.ContextMenu([
+				{
+					label : "Show Texture Details",
+					click : function() {
+						var memStats = @:privateAccess level3D.scene.engine.mem.stats();
+						var texs = @:privateAccess level3D.scene.engine.mem.textures;
+						var list = [for(t in texs) {
+							n: '${t.width}x${t.height}  ${t.format}  ${t.name}',
+							size: t.width * t.height
+						}];
+						list.sort((a, b) -> Reflect.compare(b.size, a.size));
+						var content = new Element('<div tabindex="1" class="overlay-info"><h2>Scene info</h2><pre></pre></div>');
+						new Element(@:privateAccess level3D.element[0].ownerDocument.body).append(content);
+						var pre = content.find("pre");
+						pre.text([for(l in list) l.n].join("\n"));
+						texContent = content;
+						content.blur(function(_) {
+							content.remove();
+							texContent = null;
+						});
+					}
+				}
+			]);
+		}},
+		"sceneeditor.autoSyncToggle" => {title : "Auto synchronize", icon : "refresh", type : Toggle, toggleFunction : (b) -> @:privateAccess level3D.autoSync = b},
+		"sceneeditor.backgroundColor" => {title : "Background Color", type : Color, colorFunction :  function(v) {
+			@:privateAccess level3D.scene.engine.backgroundColor = v;
+			@:privateAccess level3D.updateGrid();}},
+		"sceneeditor.sceneSpeed" => {title : "Speed", type : Range, rangeFunction : function(v) @:privateAccess level3D.scene.speed = v}
+	];
+}
 typedef ToolToggle = {
 	var element : Element;
 	function toggle( v : Bool ) : Void;

+ 13 - 64
hide/view/l3d/Level3D.hx

@@ -404,85 +404,34 @@ class Level3D extends FileView {
 
 	public function onSceneReady() {
 		tools.saveDisplayKey = "Prefab/toolbar";
-
-		var toolButtonFunctions = new Map<String, Void -> Void>();
-		toolButtonFunctions.set("perspectiveCamera", () -> resetCamera(false));
-		toolButtonFunctions.set("topCamera", () -> resetCamera(true));
-		toolButtonFunctions.set("bakeLights", () -> bakeLights());
-
-		var toolToggleFunctions = new Map<String, Bool -> Void>();
-		toolToggleFunctions.set("snapToGroundToggle", (v) -> sceneEditor.snapToGround = v);
-		toolToggleFunctions.set("localTransformsToggle", (v) -> sceneEditor.localTransform = v);
-		toolToggleFunctions.set("gridToggle", function(v) { showGrid = v; updateGrid(); });
 		statusText = new h2d.Text(hxd.res.DefaultFont.get(), scene.s2d);
 		statusText.setPosition(5, 5);
 		statusText.visible = false;
-		toolToggleFunctions.set("sceneInformationToggle", function(b) statusText.visible = b);
-		toolToggleFunctions.set("autoSyncToggle", function(b) autoSync = b);
-		var toolToggleRightClickFunctions = new Map<String, Void -> Void>();
-		var toolColorFunctions = new Map<String, Int -> Void>();
-		toolColorFunctions.set("backgroundColor", function(v) {
-			scene.engine.backgroundColor = v;
-			updateGrid();
-		});
-		var toolRangeFunctions = new Map<String, Float -> Void>();
-		toolRangeFunctions.set("sceneSpeed", function(v) scene.speed = v);
-
-		var texContent : Element = null;
-		toolToggleRightClickFunctions.set("sceneInformationToggle", function() {
-				if( texContent != null ) {
-					texContent.remove();
-					texContent = null;
-				}
-				new hide.comp.ContextMenu([
-					{
-						label : "Show Texture Details",
-						click : function() {
-							var memStats = scene.engine.mem.stats();
-							var texs = @:privateAccess scene.engine.mem.textures;
-							var list = [for(t in texs) {
-								n: '${t.width}x${t.height}  ${t.format}  ${t.name}',
-								size: t.width * t.height
-							}];
-							list.sort((a, b) -> Reflect.compare(b.size, a.size));
-							var content = new Element('<div tabindex="1" class="overlay-info"><h2>Scene info</h2><pre></pre></div>');
-							new Element(element[0].ownerDocument.body).append(content);
-							var pre = content.find("pre");
-							pre.text([for(l in list) l.n].join("\n"));
-							texContent = content;
-							content.blur(function(_) {
-								content.remove();
-								texContent = null;
-							});
-						}
-					}
-				]);
-			});
-
-		var toolsNames : Array<String> = config.get("sceneeditor.tools");
+		hide.comp.Toolbar.ToolsObject.level3D = this;
+		var toolsNames : Iterator<String> = hide.comp.Toolbar.ToolsObject.tools.keys();
 		for (toolName in toolsNames) {
-			var tool = config.get("sceneeditor." + toolName);
+			var tool = hide.comp.Toolbar.ToolsObject.tools.get(toolName);
 			if (tool != null) {
 				var shortcut = (config.get("key.sceneeditor." + toolName) != null)? " (" + config.get("key.sceneeditor." + toolName) + ")" : "";
 				switch(tool.type) {
-					case "Button":
-						var button = tools.addButton(tool.icon, tool.title + shortcut, toolButtonFunctions[toolName]);
+					case Button:
+						var button = tools.addButton(tool.icon, tool.title + shortcut, tool.buttonFunction);
 						if (tool.iconTransform != null) {
 							button.find(".icon").css({transform: tool.iconTransform});
 						}
-					case "Toggle":
-						var toggle = tools.addToggle(tool.icon, tool.title + shortcut, toolToggleFunctions[toolName]);
+					case Toggle:
+						var toggle = tools.addToggle(tool.icon, tool.title + shortcut, tool.toggleFunction);
 						if (tool.iconTransform != null) {
 							toggle.element.find(".icon").css({transform: tool.iconTransform});
 						}
 						keys.register("sceneeditor." + toolName, () -> toggle.toggle(!toggle.isDown()));
-						if (tool.customRightClick != null && tool.customRightClick) {
-							toggle.rightClick(toolToggleRightClickFunctions[toolName]);
+						if (tool.rightClick != null) {
+							toggle.rightClick(tool.rightClick);
 						}
-					case "Color":
-						tools.addColor(tool.title, toolColorFunctions[toolName]);
-					case "Range":
-						tools.addRange(tool.title, toolRangeFunctions[toolName], 1.);
+					case Color:
+						tools.addColor(tool.title, tool.colorFunction);
+					case Range:
+						tools.addRange(tool.title, tool.rangeFunction, 1.);
 
 				}
 			}