2
0
Эх сурвалжийг харах

Added UIElement::CreateChild().
Added internal flag to UI elements, meaning a child element has been programmatically created (for example the slider of a scroll bar.)

Lasse Öörni 13 жил өмнө
parent
commit
e586349114

+ 14 - 0
Docs/ScriptAPI.dox

@@ -2755,6 +2755,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -2835,6 +2836,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -2921,6 +2923,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3014,6 +3017,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3103,6 +3107,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3191,6 +3196,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3282,6 +3288,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3374,6 +3381,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3463,6 +3471,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3579,6 +3588,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3675,6 +3685,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3771,6 +3782,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3871,6 +3883,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)
@@ -3982,6 +3995,7 @@ Methods:<br>
 - void DisableLayoutUpdate()
 - void EnableLayoutUpdate()
 - void BringToFront()
+- UIElement@ CreateChild(const String&, const String& arg1 = String ( ))
 - void AddChild(UIElement@)
 - void InsertChild(uint, UIElement@)
 - void RemoveChild(UIElement@)

+ 6 - 0
Engine/Engine/APITemplates.h

@@ -690,6 +690,11 @@ template <class T> void RegisterTexture(asIScriptEngine* engine, const char* cla
     engine->RegisterObjectMethod(className, "bool get_dataLost() const", asMETHODPR(T, IsDataLost, () const, bool), asCALL_THISCALL);
 }
 
+static UIElement* UIElementCreateChild(const String& typeName, const String& name, UIElement* ptr)
+{
+    return ptr->CreateChild(ShortStringHash(typeName), name);
+}
+
 static CScriptArray* UIElementGetChildren(bool recursive, UIElement* ptr)
 {
     PODVector<UIElement*> elements;
@@ -735,6 +740,7 @@ template <class T> void RegisterUIElement(asIScriptEngine* engine, const char* c
     engine->RegisterObjectMethod(className, "void DisableLayoutUpdate()", asMETHOD(T, DisableLayoutUpdate), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void EnableLayoutUpdate()", asMETHOD(T, EnableLayoutUpdate), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void BringToFront()", asMETHOD(T, BringToFront), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "UIElement@+ CreateChild(const String&in, const String&in name = String())", asFUNCTION(UIElementCreateChild), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod(className, "void AddChild(UIElement@+)", asMETHOD(T, AddChild), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void InsertChild(uint, UIElement@+)", asMETHOD(T, InsertChild), asCALL_THISCALL);
     engine->RegisterObjectMethod(className, "void RemoveChild(UIElement@+)", asMETHOD(T, RemoveChild), asCALL_THISCALL);

+ 4 - 2
Engine/UI/DropDownList.cpp

@@ -40,15 +40,17 @@ DropDownList::DropDownList(Context* context) :
     resizePopup_(false)
 {
     Window* window = new Window(context_);
+    window->SetInternal(true);
     SetPopup(window);
     
     listView_ = new ListView(context_);
+    listView_->SetInternal(true);
     listView_->SetScrollBarsVisible(false, false);
     listView_->SetFocusMode(FM_NOTFOCUSABLE);
     popup_->SetLayout(LM_VERTICAL);
     popup_->AddChild(listView_);
-    placeholder_ = new UIElement(context_);
-    AddChild(placeholder_);
+    placeholder_ = CreateChild<UIElement>();
+    placeholder_->SetInternal(true);
     
     SubscribeToEvent(listView_, E_ITEMSELECTED, HANDLER(DropDownList, HandleItemSelected));
 }

+ 4 - 4
Engine/UI/LineEdit.cpp

@@ -54,11 +54,11 @@ LineEdit::LineEdit(Context* context) :
     active_ = true;
     focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
     
-    text_ = new Text(context_);
-    cursor_ = new BorderImage(context_);
+    text_ = CreateChild<Text>();
+    text_->SetInternal(true);
+    cursor_ = CreateChild<BorderImage>();
+    cursor_->SetInternal(true);
     cursor_->SetPriority(1); // Show over text
-    AddChild(text_);
-    AddChild(cursor_);
     
     SubscribeToEvent(this, E_FOCUSED, HANDLER(LineEdit, HandleFocused));
     SubscribeToEvent(this, E_DEFOCUSED, HANDLER(LineEdit, HandleDefocused));

+ 6 - 7
Engine/UI/ScrollBar.cpp

@@ -48,15 +48,14 @@ ScrollBar::ScrollBar(Context* context) :
 {
     active_ = true;
     
-    backButton_ = new Button(context_);
+    backButton_ = CreateChild<Button>();
+    backButton_->SetInternal(true);
     backButton_->SetRepeat(DEFAULT_REPEAT_DELAY, DEFAULT_REPEAT_RATE);
-    forwardButton_ = new Button(context_);
+    slider_ = CreateChild<Slider>();
+    slider_->SetInternal(true);
+    forwardButton_ = CreateChild<Button>();
+    forwardButton_->SetInternal(true);
     forwardButton_->SetRepeat(DEFAULT_REPEAT_DELAY, DEFAULT_REPEAT_RATE);
-    slider_ = new Slider(context_);
-    
-    AddChild(backButton_);
-    AddChild(slider_);
-    AddChild(forwardButton_);
     
     SubscribeToEvent(backButton_, E_PRESSED, HANDLER(ScrollBar, HandleBackButtonPressed));
     SubscribeToEvent(forwardButton_, E_PRESSED, HANDLER(ScrollBar, HandleForwardButtonPressed));

+ 6 - 7
Engine/UI/ScrollView.cpp

@@ -48,20 +48,19 @@ ScrollView::ScrollView(Context* context) :
     active_ = true;
     focusMode_ = FM_FOCUSABLE_DEFOCUSABLE;
     
-    horizontalScrollBar_ = new ScrollBar(context_);
+    horizontalScrollBar_ = CreateChild<ScrollBar>();
+    horizontalScrollBar_->SetInternal(true);
     horizontalScrollBar_->SetAlignment(HA_LEFT, VA_BOTTOM);
     horizontalScrollBar_->SetOrientation(O_HORIZONTAL);
-    verticalScrollBar_ = new ScrollBar(context_);
+    verticalScrollBar_ = CreateChild<ScrollBar>();
+    verticalScrollBar_->SetInternal(true);
     verticalScrollBar_->SetAlignment(HA_RIGHT, VA_TOP);
     verticalScrollBar_->SetOrientation(O_VERTICAL);
-    scrollPanel_ = new BorderImage(context_);
+    scrollPanel_ = CreateChild<BorderImage>();
+    scrollPanel_->SetInternal(true);
     scrollPanel_->SetActive(true);
     scrollPanel_->SetClipChildren(true);
     
-    AddChild(horizontalScrollBar_);
-    AddChild(verticalScrollBar_);
-    AddChild(scrollPanel_);
-    
     SubscribeToEvent(horizontalScrollBar_, E_SCROLLBARCHANGED, HANDLER(ScrollView, HandleScrollBarChanged));
     SubscribeToEvent(horizontalScrollBar_, E_VISIBLECHANGED, HANDLER(ScrollView, HandleScrollBarVisibleChanged));
     SubscribeToEvent(verticalScrollBar_, E_SCROLLBARCHANGED, HANDLER(ScrollView, HandleScrollBarChanged));

+ 2 - 2
Engine/UI/Slider.cpp

@@ -43,8 +43,8 @@ Slider::Slider(Context* context) :
     dragSlider_(false)
 {
     active_ = true;
-    knob_ = new BorderImage(context_);
-    AddChild(knob_);
+    knob_ = CreateChild<BorderImage>();
+    knob_->SetInternal(true);
     
     UpdateSlider();
 }

+ 2 - 2
Engine/UI/UI.cpp

@@ -376,7 +376,7 @@ SharedPtr<UIElement> UI::LoadLayout(XMLFile* file, XMLFile* styleFile)
     root = DynamicCast<UIElement>(context_->CreateObject(ShortStringHash(type)));
     if (!root)
     {
-        LOGERROR("Could not create UI element " + type);
+        LOGERROR("Could not create unknown UI element " + type);
         return root;
     }
     root->SetName(rootElem.GetAttribute("name"));
@@ -655,7 +655,7 @@ void UI::LoadLayout(UIElement* current, const XMLElement& elem, XMLFile* styleFi
         SharedPtr<UIElement> child = DynamicCast<UIElement>(context_->CreateObject(ShortStringHash(type)));
         if (!child)
         {
-            LOGERROR("Could not create UI element " + type);
+            LOGERROR("Could not create unknown UI element " + type);
             childElem = childElem.GetNext("element");
             continue;
         }

+ 23 - 0
Engine/UI/UIElement.cpp

@@ -23,6 +23,7 @@
 
 #include "Precompiled.h"
 #include "Context.h"
+#include "Log.h"
 #include "ResourceCache.h"
 #include "Sort.h"
 #include "StringUtils.h"
@@ -89,6 +90,7 @@ UIElement::UIElement(Context* context) :
     selected_(false),
     visible_(true),
     hovering_(false),
+    internal_(false),
     focusMode_(FM_NOTFOCUSABLE),
     dragDropMode_(DD_DISABLED),
     layoutMode_(LM_FREE),
@@ -775,6 +777,22 @@ void UIElement::BringToFront()
         ptr->SetPriority(maxPriority);
 }
 
+UIElement* UIElement::CreateChild(ShortStringHash type, const String& name)
+{
+    // Check that creation succeeds and that the object in fact is a UI element
+    SharedPtr<UIElement> newElement = DynamicCast<UIElement>(context_->CreateObject(type));
+    if (!newElement)
+    {
+        LOGERROR("Could not create unknown UI element type " + type.ToString());
+        return 0;
+    }
+    
+    newElement->SetName(name);
+    
+    AddChild(newElement);
+    return newElement;
+}
+
 void UIElement::AddChild(UIElement* element)
 {
     InsertChild(children_.Size(), element);
@@ -854,6 +872,11 @@ void UIElement::SetVar(ShortStringHash key, const Variant& value)
     vars_[key] = value;
 }
 
+void UIElement::SetInternal(bool enable)
+{
+    internal_ = enable;
+}
+
 IntVector2 UIElement::GetScreenPosition()
 {
     if (positionDirty_)

+ 13 - 0
Engine/UI/UIElement.h

@@ -237,6 +237,8 @@ public:
     void EnableLayoutUpdate();
     /// Bring UI element to front.
     void BringToFront();
+    /// Create and add a child element and return it.
+    UIElement* CreateChild(ShortStringHash type, const String& name = String());
     /// Add a child element.
     void AddChild(UIElement* element);
     /// Insert a child element into a specific position in the child list.
@@ -251,6 +253,10 @@ public:
     void SetParent(UIElement* parent);
     /// Set a user variable.
     void SetVar(ShortStringHash key, const Variant& value);
+    /// Mark as internally created. Internally created elements are not re-created during deserialization.
+    void SetInternal(bool enable);
+    /// Template version of creating a child element.
+    template <class T> T* CreateChild(const String& name = String());
     
     /// Return name.
     const String& GetName() const { return name_; }
@@ -310,6 +316,8 @@ public:
     bool IsVisible() const { return visible_; }
     /// Return whether the cursor is hovering on this element.
     bool IsHovering() const { return hovering_; }
+    /// Return whether is internally created.
+    bool IsInternal() const { return internal_; }
     /// Return whether has different color in at least one corner.
     bool HasColorGradient() const { return colorGradient_; }
     /// Return focus mode.
@@ -404,6 +412,8 @@ protected:
     bool visible_;
     /// Hovering flag.
     bool hovering_;
+    /// Internally created flag.
+    bool internal_;
     /// Focus mode.
     FocusMode focusMode_;
     /// Drag and drop flags.
@@ -465,4 +475,7 @@ private:
     bool colorGradient_;
 };
 
+template <class T> T* UIElement::CreateChild(const String& name) { return static_cast<T*>(CreateChild(T::GetTypeStatic(), name)); }
+
 }
+