瀏覽代碼

New UI-element style is applied after removing the 'effect' from the old style.

Wei Tjong Yao 12 年之前
父節點
當前提交
a4479c65ec

+ 26 - 16
Bin/Data/Scripts/Editor/EditorActions.as

@@ -615,7 +615,8 @@ class ApplyUIElementStyleAction : EditAction
     Variant parentID;
     XMLFile@ elementData;
     XMLFile@ styleFile;
-    String style;
+    String elementOldStyle;
+    String elementNewStyle;
 
     void Define(UIElement@ element, const String&in newStyle)
     {
@@ -625,39 +626,48 @@ class ApplyUIElementStyleAction : EditAction
         XMLElement rootElem = elementData.CreateRoot("element");
         element.SaveXML(rootElem);
         rootElem.SetUInt("index", element.parent.FindChild(element));
+        rootElem.SetUInt("listItemIndex", GetListIndex(element));
         styleFile = element.defaultStyle;
-        style = newStyle;
+        elementOldStyle = element.appliedStyle;
+        elementNewStyle = newStyle;
     }
 
-    void Undo()
+    void ApplyStyle(const String&in style)
     {
         UIElement@ parent = GetUIElementByID(parentID);
         UIElement@ element = GetUIElementByID(elementID);
         if (parent !is null && element !is null)
         {
-            // Suppress updating hierarchy list as we are "just" reverting the attribute values of the whole structure without changing the structure itself
+            // Apply the style in the XML data
+            elementData.root.SetAttribute("style", style);
+            
+            // Have to update manually because the element ID var is not set yet when the E_ELEMENTADDED event is sent
             suppressUIElementChanges = true;
 
             parent.RemoveChild(element);
-            parent.LoadChildXML(elementData.root, styleFile);
+            if (parent.LoadChildXML(elementData.root, styleFile))
+            {
+                XMLElement rootElem = elementData.root;
+                uint index = rootElem.GetUInt("index");
+                uint listItemIndex = rootElem.GetUInt("listItemIndex");
+                UIElement@ element = parent.children[index];
+                UIElement@ parentItem = hierarchyList.items[GetListIndex(parent)];
+                UpdateHierarchyItem(listItemIndex, element, parentItem);
+                hierarchyUpdateSelections.Push(listItemIndex);
+            }
 
             suppressUIElementChanges = false;
-
             SetSceneModified();
-            // Although the selections are not changed, call to ensure attribute inspector notices reverted element
-            HandleHierarchyListSelectionChange();
         }
     }
+    
+    void Undo()
+    {
+        ApplyStyle(elementOldStyle);
+    }
 
     void Redo()
     {
-        UIElement@ element = GetUIElementByID(elementID);
-        if (element !is null)
-        {
-            element.SetStyle(styleFile, style);
-
-            SetSceneModified();
-            attributesDirty = true;
-        }
+        ApplyStyle(elementNewStyle);
     }
 }

+ 2 - 2
Bin/Data/Scripts/Editor/EditorNodeWindow.as

@@ -702,8 +702,8 @@ void HandleStyleItemSelected(StringHash eventType, VariantMap& eventData)
         ApplyUIElementStyleAction action;
         action.Define(element, autoStyle ? element.typeName : newStyle);
         group.actions.Push(action);
-        
-        // Use the Redo() to actually do the action 
+
+        // Use the Redo() to actually do the action
         action.Redo();
     }
 

+ 2 - 1
Bin/Data/Scripts/Editor/EditorSceneWindow.as

@@ -21,6 +21,7 @@ ListView@ hierarchyList;
 const uint UI_ELEMENT_BASE_ID = 1;
 uint uiElementNextID = UI_ELEMENT_BASE_ID;
 bool showInternalUIElement = false;
+Array<uint> hierarchyUpdateSelections;
 
 Variant GetUIElementID(UIElement@ element)
 {
@@ -515,7 +516,7 @@ void SelectComponent(Component@ component, bool multiselect)
             }
             while (current !is null);
         }
-        
+
         // This causes an event to be sent, in response we set the node/component selections, and refresh editors
         if (!multiselect)
             hierarchyList.selection = componentIndex;

+ 7 - 0
Bin/Data/Scripts/Editor/EditorUI.as

@@ -998,6 +998,13 @@ void SetIconEnabledColor(UIElement@ element, bool enabled, bool partial = false)
 
 void UpdateDirtyUI()
 {
+    // Perform hierarchy selection latently after the new selections are finalized (used in undo/redo action)
+    if (!hierarchyUpdateSelections.empty)
+    {
+        hierarchyList.SetSelections(hierarchyUpdateSelections);
+        hierarchyUpdateSelections.Clear();
+    }
+
     // Perform some event-triggered updates latently in case a large hierarchy was changed
     if (attributesFullDirty || attributesDirty)
         UpdateAttributeInspector(attributesFullDirty);

+ 21 - 10
Engine/UI/UIElement.cpp

@@ -342,6 +342,11 @@ bool UIElement::SaveXML(XMLElement& dest) const
         if (!dest.SetAttribute("style", appliedStyle_))
             return false;
     }
+    else if (internal_)
+    {
+        if (!dest.SetAttribute("style", "none"))
+            return false;
+    }
 
     // Write attributes
     if (!Serializable::SaveXML(dest))
@@ -867,11 +872,9 @@ void UIElement::SetDragDropMode(unsigned mode)
 
 void UIElement::SetStyle(XMLFile* file, const String& typeName)
 {
+    appliedStyle_ = typeName;
     if (typeName == "none")
-    {
-        appliedStyle_ = typeName;
         return;
-    }
 
     if (!file)
         return;
@@ -1639,18 +1642,26 @@ bool UIElement::FilterUIStyleAttributes(XMLElement& dest, const XMLElement& styl
         childElem = childElem.GetNext("element");
     }
 
+    // Remove style attribute when it is the same as its type, however, if it is an internal element then replace it to "none" instead
+    if (!dest.GetAttribute("style").Empty() && dest.GetAttribute("style") == dest.GetAttribute("type"))
+    {
+        if (internal_)
+        {
+            if (!dest.SetAttribute("style", "none"))
+                return false;
+        }
+        else
+        {
+            if (!dest.RemoveAttribute("style"))
+                return false;
+        }
+    }
+
     return true;
 }
 
 bool UIElement::FilterImplicitAttributes(XMLElement& dest) const
 {
-    // Remove style attribute when it is the same as its type
-    if (!dest.GetAttribute("style").Empty() && dest.GetAttribute("style") == dest.GetAttribute("type"))
-    {
-        if (!dest.RemoveAttribute("style"))
-            return false;
-    }
-
     // Remove positioning and sizing attributes when they are under the influence of layout mode
     if (layoutMode_ != LM_FREE && !IsFixedWidth() && !IsFixedHeight())
     {