Prechádzať zdrojové kódy

Add template version of UIElement::GetChild() methods.

Yao Wei Tjong 姚伟忠 9 rokov pred
rodič
commit
fd9db884b4

+ 1 - 1
Source/Samples/02_HelloGUI/HelloGUI.cpp

@@ -208,7 +208,7 @@ void HelloGUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
 void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
 {
     // Get the Text control acting as the Window's title
-    Text* windowTitle = static_cast<Text*>(window_->GetChild("WindowTitle", true));
+    Text* windowTitle = window_->GetChildStaticCast<Text>("WindowTitle", true);
 
     // Get control that was clicked
     UIElement* clicked = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());

+ 2 - 2
Source/Samples/38_SceneAndUILoad/SceneAndUILoad.cpp

@@ -105,10 +105,10 @@ void SceneAndUILoad::CreateUI()
     ui->GetRoot()->AddChild(layoutRoot);
 
     // Subscribe to button actions (toggle scene lights when pressed then released)
-    Button* button = static_cast<Button*>(layoutRoot->GetChild("ToggleLight1", true));
+    Button* button = layoutRoot->GetChildStaticCast<Button>("ToggleLight1", true);
     if (button)
         SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SceneAndUILoad, ToggleLight1));
-    button = static_cast<Button*>(layoutRoot->GetChild("ToggleLight2", true));
+    button = layoutRoot->GetChildStaticCast<Button>("ToggleLight2", true);
     if (button)
         SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SceneAndUILoad, ToggleLight2));
 }

+ 2 - 2
Source/Samples/40_Localization/L10n.cpp

@@ -228,10 +228,10 @@ void L10n::HandleChangeLanguage(StringHash eventType, VariantMap& eventData)
     Localization* l10n = GetSubsystem<Localization>();
     UIElement* uiRoot = GetSubsystem<UI>()->GetRoot();
 
-    Text* windowTitle = static_cast<Text*>(uiRoot->GetChild("WindowTitle", true));
+    Text* windowTitle = uiRoot->GetChildStaticCast<Text>("WindowTitle", true);
     windowTitle->SetText(l10n->Get("title") + " (" + String(l10n->GetLanguageIndex()) + " " + l10n->GetLanguage() + ")");
 
-    Text* buttonText = static_cast<Text*>(uiRoot->GetChild("ButtonTextQuit", true));
+    Text* buttonText = uiRoot->GetChildStaticCast<Text>("ButtonTextQuit", true);
     buttonText->SetText(l10n->Get("quit"));
 
     Text3D* text3D = scene_->GetChild("Text3D")->GetComponent<Text3D>();

+ 3 - 3
Source/Urho3D/Input/Input.cpp

@@ -987,7 +987,7 @@ SDL_JoystickID Input::AddScreenJoystick(XMLFile* layoutFile, XMLFile* styleFile)
             ++numButtons;
 
             // Check whether the button has key binding
-            Text* text = dynamic_cast<Text*>(element->GetChild("KeyBinding", false));
+            Text* text = element->GetChildDynamicCast<Text>("KeyBinding", false);
             if (text)
             {
                 text->SetVisible(false);
@@ -1014,7 +1014,7 @@ SDL_JoystickID Input::AddScreenJoystick(XMLFile* layoutFile, XMLFile* styleFile)
             }
 
             // Check whether the button has mouse button binding
-            text = dynamic_cast<Text*>(element->GetChild("MouseButtonBinding", false));
+            text = element->GetChildDynamicCast<Text>("MouseButtonBinding", false);
             if (text)
             {
                 text->SetVisible(false);
@@ -1039,7 +1039,7 @@ SDL_JoystickID Input::AddScreenJoystick(XMLFile* layoutFile, XMLFile* styleFile)
         {
             ++numHats;
 
-            Text* text = dynamic_cast<Text*>(element->GetChild("KeyBinding", false));
+            Text* text = element->GetChildDynamicCast<Text>("KeyBinding", false);
             if (text)
             {
                 text->SetVisible(false);

+ 2 - 2
Source/Urho3D/UI/DropDownList.cpp

@@ -189,7 +189,7 @@ void DropDownList::SetSelection(unsigned index)
 
 void DropDownList::SetPlaceholderText(const String& text)
 {
-    static_cast<Text*>(placeholder_->GetChild(0))->SetText(text);
+    placeholder_->GetChildStaticCast<Text>(0)->SetText(text);
 }
 
 void DropDownList::SetResizePopup(bool enable)
@@ -224,7 +224,7 @@ UIElement* DropDownList::GetSelectedItem() const
 
 const String& DropDownList::GetPlaceholderText() const
 {
-    return static_cast<Text*>(placeholder_->GetChild(0))->GetText();
+    return placeholder_->GetChildStaticCast<Text>(0)->GetText();
 }
 
 void DropDownList::SetSelectionAttr(unsigned index)

+ 1 - 1
Source/Urho3D/UI/ListView.cpp

@@ -99,7 +99,7 @@ public:
         for (unsigned i = 0; i < children_.Size(); ++i)
         {
             const IntVector2& position = children_[i]->GetPosition();
-            CheckBox* overlay = static_cast<CheckBox*>(overlayContainer_->GetChild(i));
+            CheckBox* overlay = overlayContainer_->GetChildStaticCast<CheckBox>(i);
             bool visible = children_[i]->IsVisible() && GetItemHierarchyParent(children_[i]);
             overlay->SetVisible(visible);
             if (visible)

+ 1 - 1
Source/Urho3D/UI/Menu.cpp

@@ -91,7 +91,7 @@ void Menu::OnHover(const IntVector2& position, const IntVector2& screenPosition,
 {
     Button::OnHover(position, screenPosition, buttons, qualifiers, cursor);
 
-    Menu* sibling = static_cast<Menu*>(parent_->GetChild(VAR_SHOW_POPUP, true));
+    Menu* sibling = parent_->GetChildStaticCast<Menu>(VAR_SHOW_POPUP, true);
     if (popup_ && !showPopup_)
     {
         // Check if popup is shown by one of the siblings

+ 5 - 5
Source/Urho3D/UI/MessageBox.cpp

@@ -64,10 +64,10 @@ MessageBox::MessageBox(Context* context, const String& messageString, const Stri
     }
 
     // Set the title and message strings if they are given
-    titleText_ = dynamic_cast<Text*>(window_->GetChild("TitleText", true));
+    titleText_ = window_->GetChildDynamicCast<Text>("TitleText", true);
     if (titleText_ && !titleString.Empty())
         titleText_->SetText(titleString);
-    messageText_ = dynamic_cast<Text*>(window_->GetChild("MessageText", true));
+    messageText_ = window_->GetChildDynamicCast<Text>("MessageText", true);
     if (messageText_ && !messageString.Empty())
         messageText_->SetText(messageString);
 
@@ -82,16 +82,16 @@ MessageBox::MessageBox(Context* context, const String& messageString, const Stri
     }
 
     // Bind the buttons (if any in the loaded UI layout) to event handlers
-    okButton_ = dynamic_cast<Button*>(window_->GetChild("OkButton", true));
+    okButton_ = window_->GetChildDynamicCast<Button>("OkButton", true);
     if (okButton_)
     {
         ui->SetFocusElement(okButton_);
         SubscribeToEvent(okButton_, E_RELEASED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
     }
-    Button* cancelButton = dynamic_cast<Button*>(window_->GetChild("CancelButton", true));
+    Button* cancelButton = window_->GetChildDynamicCast<Button>("CancelButton", true);
     if (cancelButton)
         SubscribeToEvent(cancelButton, E_RELEASED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
-    Button* closeButton = dynamic_cast<Button*>(window_->GetChild("CloseButton", true));
+    Button* closeButton = window_->GetChildDynamicCast<Button>("CloseButton", true);
     if (closeButton)
         SubscribeToEvent(closeButton, E_RELEASED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
 

+ 44 - 2
Source/Urho3D/UI/UIElement.h

@@ -358,6 +358,18 @@ public:
 
     /// Template version of creating a child element.
     template <class T> T* CreateChild(const String& name = String::EMPTY, unsigned index = M_MAX_UNSIGNED);
+    /// Template version of returning child element by index using static cast.
+    template <class T> T* GetChildStaticCast(unsigned index) const;
+    /// Template version of returning child element by name using static cast.
+    template <class T> T* GetChildStaticCast(const String& name, bool recursive = false) const;
+    /// Template version of returning child element by variable using static cast. If only key is provided, return the first child having the matching variable key. If value is also provided then the actual variable value would also be checked against.
+    template <class T> T* GetChildStaticCast(const StringHash& key, const Variant& value = Variant::EMPTY, bool recursive = false) const;
+    /// Template version of returning child element by index using dynamic cast. May return 0 when casting failed.
+    template <class T> T* GetChildDynamicCast(unsigned index) const;
+    /// Template version of returning child element by name using dynamic cast. May return 0 when casting failed.
+    template <class T> T* GetChildDynamicCast(const String& name, bool recursive = false) const;
+    /// Template version of returning child element by variable. If only key is provided, return the first child having the matching variable key. If value is also provided then the actual variable value would also be checked against using dynamic cast. May return 0 when casting failed.
+    template <class T> T* GetChildDynamicCast(const StringHash& key, const Variant& value = Variant::EMPTY, bool recursive = false) const;
 
     /// Return name.
     const String& GetName() const { return name_; }
@@ -457,7 +469,7 @@ public:
 
     /// Return whether element itself should be visible. Elements can be also hidden due to the parent being not visible, use IsVisibleEffective() to check.
     bool IsVisible() const { return visible_; }
-    
+
     /// Return whether element is effectively visible (parent element chain is visible.)
     bool IsVisibleEffective() const;
 
@@ -580,7 +592,7 @@ public:
 
     /// Return effective minimum size, also considering layout. Used internally.
     IntVector2 GetEffectiveMinSize() const;
-    
+
 protected:
     /// Handle attribute animation added.
     virtual void OnAttributeAnimationAdded();
@@ -741,4 +753,34 @@ template <class T> T* UIElement::CreateChild(const String& name, unsigned index)
     return static_cast<T*>(CreateChild(T::GetTypeStatic(), name, index));
 }
 
+template <class T> T* UIElement::GetChildStaticCast(unsigned index) const
+{
+    return static_cast<T*>(GetChild(index));
+}
+
+template <class T> T* UIElement::GetChildStaticCast(const String& name, bool recursive) const
+{
+    return static_cast<T*>(GetChild(name, recursive));
+}
+
+template <class T> T* UIElement::GetChildStaticCast(const StringHash& key, const Variant& value, bool recursive) const
+{
+    return static_cast<T*>(GetChild(key, value, recursive));
+}
+
+template <class T> T* UIElement::GetChildDynamicCast(unsigned index) const
+{
+    return dynamic_cast<T*>(GetChild(index));
+}
+
+template <class T> T* UIElement::GetChildDynamicCast(const String& name, bool recursive) const
+{
+    return dynamic_cast<T*>(GetChild(name, recursive));
+}
+
+template <class T> T* UIElement::GetChildDynamicCast(const StringHash& key, const Variant& value, bool recursive) const
+{
+    return dynamic_cast<T*>(GetChild(key, value, recursive));
+}
+
 }