| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- //
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- // LICENSE: Atomic Game Engine Editor and Tools EULA
- // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
- // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
- //
- import ScriptWidget = require("ui/ScriptWidget");
- import ComponentInspector = require("./ComponentInspector");
- import DataBinding = require("./DataBinding");
- import CreateComponentButton = require("./CreateComponentButton");
- interface ComponentState {
- expanded: boolean;
- }
- interface NodeState {
- expanded: boolean;
- componentStates: { [id: number]: ComponentState };
- }
- class NodeInspector extends ScriptWidget {
- constructor() {
- super();
- this.subscribeToEvent(this, "WidgetEvent", (ev) => this.handleWidgetEvent(ev));
- this.subscribeToEvent("GizmoMoved", (ev) => this.handleGizmoModed(ev));
- this.subscribeToEvent("Update", (ev) => this.handleUpdate(ev));
- }
- handleUpdate(ev) {
- // to keep from spamming UI update we have a little delta on the gizmo updates
- if (!this.node) {
- this.gizmoMoved = false;
- return;
- }
- if (this.gizmoMoved) {
- if (this.updateDelta > 1.0) {
- this.updateDelta = 0.0;
- }
- else {
- this.updateDelta -= ev.timeStep;
- }
- if (this.updateDelta <= 0) {
- for (var i in this.bindings) {
- this.bindings[i].setWidgetValueFromObject();
- }
- this.gizmoMoved = false;
- this.updateDelta = 0;
- }
- }
- }
- handleGizmoModed(ev) {
- if (!this.node) return;
- this.gizmoMoved = true;
- this.updateDelta += .3;
- }
- handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
- var handled = false;
- for (var i = 0; i < this.bindings.length; i++) {
- if (this.bindings[i].handleWidgetEvent(ev)) {
- handled = true;
- }
- }
- // return handled
- return handled;
- }
- getPrefabComponent(node: Atomic.Node): Atomic.PrefabComponent {
- if (node.getComponent("PrefabComponent"))
- return <Atomic.PrefabComponent>node.getComponent("PrefabComponent");
- if (node.parent)
- return this.getPrefabComponent(node.parent);
- return null;
- }
- detectPrefab(node: Atomic.Node): boolean {
- if (node.getComponent("PrefabComponent"))
- return true;
- if (node.parent)
- return this.detectPrefab(node.parent);
- return false;
- }
- inspect(node: Atomic.Node) {
- this.bindings = new Array();
- this.node = node;
- node.scene.sendEvent("SceneEditSerializable", { serializable: node, operation: 0 });
- this.subscribeToEvent(node, "SceneEditStateChange", (data) => this.handleSceneEditStateChangeEvent(data));
- this.isPrefab = this.detectPrefab(node);
- var fd = new Atomic.UIFontDescription();
- fd.id = "Vera";
- fd.size = 11;
- var nlp = new Atomic.UILayoutParams();
- nlp.width = 304;
- var nodeLayout = this.nodeLayout = new Atomic.UILayout();
- nodeLayout.spacing = 4;
- nodeLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
- nodeLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
- nodeLayout.layoutParams = nlp;
- nodeLayout.axis = Atomic.UI_AXIS_Y;
- // node attr layout
- var nodeSection = new Atomic.UISection();
- nodeSection.id = "node_section";
- nodeSection.text = "Node";
- nodeSection.value = 1;
- nodeLayout.addChild(nodeSection);
- var attrsVerticalLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
- attrsVerticalLayout.spacing = 3;
- attrsVerticalLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
- attrsVerticalLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
- nodeSection.contentRoot.addChild(attrsVerticalLayout);
- var attrs = node.getAttributes();
- for (var i in attrs) {
- var attr = <Atomic.AttributeInfo>attrs[i];
- if (attr.mode & Atomic.AM_NOEDIT)
- continue;
- var binding = DataBinding.createBinding(node, attr);
- if (!binding)
- continue;
- var attrLayout = new Atomic.UILayout();
- attrLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
- var name = new Atomic.UITextField();
- name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
- name.skinBg = "InspectorTextAttrName";
- if (attr.type == Atomic.VAR_VECTOR3 || attr.type == Atomic.VAR_COLOR ||
- attr.type == Atomic.VAR_QUATERNION) {
- attrLayout.axis = Atomic.UI_AXIS_Y;
- attrLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
- attrLayout.skinBg = "InspectorVectorAttrLayout";
- }
- var bname = attr.name;
- if (bname == "Is Enabled")
- bname = "Enabled";
- name.text = bname;
- name.fontDescription = fd;
- attrLayout.addChild(name);
- attrLayout.addChild(binding.widget);
- attrsVerticalLayout.addChild(attrLayout);
- this.bindings.push(binding);
- }
- // PREFAB
- if (this.isPrefab) {
- var name = new Atomic.UITextField();
- name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
- name.skinBg = "InspectorTextAttrName";
- name.text = "Prefab"
- name.fontDescription = fd;
- var prefabLayout = new Atomic.UILayout();
- prefabLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
- var saveButton = new Atomic.UIButton();
- saveButton.text = "Save";
- saveButton.fontDescription = fd;
- saveButton.onClick = () => {
- var prefabComponent = this.getPrefabComponent(this.node);
- if (prefabComponent) {
- prefabComponent.savePrefab();
- this.sendEvent("EditorActiveNodeChange", { node: this.node });
- return true;
- }
- }
- var undoButton = new Atomic.UIButton();
- undoButton.text = "Undo";
- undoButton.fontDescription = fd;
- undoButton.onClick = () => {
- var prefabComponent = this.getPrefabComponent(this.node);
- if (prefabComponent) {
- prefabComponent.undoPrefab();
- this.sendEvent("EditorActiveNodeChange", { node: this.node });
- return true;
- }
- }
- var breakButton = new Atomic.UIButton();
- breakButton.text = "Break";
- breakButton.fontDescription = fd;
- breakButton.onClick = () => {
- var prefabComponent = this.getPrefabComponent(this.node);
- if (prefabComponent) {
- prefabComponent.breakPrefab();
- this.sendEvent("EditorActiveNodeChange", { node: this.node });
- return true;
- }
- }
- prefabLayout.addChild(name);
- prefabLayout.addChild(saveButton);
- prefabLayout.addChild(undoButton);
- prefabLayout.addChild(breakButton);
- attrsVerticalLayout.addChild(prefabLayout);
- }
- // COMPONENTS
- var components = node.getComponents();
- for (var i in components) {
- var component = components[i];
- //if (component.isTemporary())
- // continue;
- var ci = new ComponentInspector();
- ci.id = "component_section_" + component.id;
- ci.inspect(component);
- nodeLayout.addChild(ci);
- }
- this.addChild(nodeLayout);
- var button = new CreateComponentButton(node);
- nodeLayout.addChild(button);
- for (var i in this.bindings) {
- this.bindings[i].setWidgetValueFromObject();
- this.bindings[i].objectLocked = false;
- }
- this.loadState();
- }
- handleSceneEditStateChangeEvent(ev) {
- for (var i in this.bindings) {
- this.bindings[i].objectLocked = true;
- this.bindings[i].setWidgetValueFromObject();
- this.bindings[i].objectLocked = false;
- }
- }
- saveState() {
- var node = this.node;
- if (!node.scene)
- return;
- var nodeStates = NodeInspector.nodeStates[node.scene.id];
- if (!nodeStates)
- return;
- var state = nodeStates[node.id];
- if (!state) {
- state = nodeStates[node.id] = { expanded: true, componentStates: {} };
- }
- var section: Atomic.UISection = <Atomic.UISection>this.nodeLayout.getWidget("node_section");
- state.expanded = section.value ? true : false;
- var components = node.getComponents();
- for (var i in components) {
- var component = components[i];
- var cstate = state.componentStates[component.id];
- if (!cstate) {
- cstate = state.componentStates[component.id] = { expanded: false };
- }
- section = <Atomic.UISection>this.nodeLayout.getWidget("component_section_" + component.id);
- if (section)
- cstate.expanded = section.value ? true : false;
- }
- }
- loadState() {
- var node = this.node;
- // lookup in node states via scene id
- var nodeStates = NodeInspector.nodeStates[node.scene.id];
- if (!nodeStates) {
- nodeStates = NodeInspector.nodeStates[node.scene.id] = {};
- }
- // lookup by node id
- var state = nodeStates[node.id];
- if (!state) {
- // we don't have a state, so save default state
- this.saveState();
- } else {
- var section: Atomic.UISection = <Atomic.UISection>this.nodeLayout.getWidget("node_section");
- section.value = state.expanded ? 1 : 0;
- var components = node.getComponents();
- for (var i in components) {
- var component = components[i];
- var cstate = state.componentStates[component.id];
- section = <Atomic.UISection>this.nodeLayout.getWidget("component_section_" + component.id);
- if (cstate && section) {
- section.value = cstate.expanded ? 1 : 0;
- }
- }
- }
- }
- isPrefab: boolean;
- node: Atomic.Node;
- nodeLayout: Atomic.UILayout;
- bindings: Array<DataBinding>;
- gizmoMoved = false;
- updateDelta = 0;
- static nodeStates: { [sceneID: number]: { [nodeId: number]: NodeState } } = {};
- }
- export = NodeInspector;
|