瀏覽代碼

add searching functions

JimMarlowe 8 年之前
父節點
當前提交
174f13a586

+ 1 - 0
Script/Packages/Atomic/UI.json

@@ -23,6 +23,7 @@
 		],
 		"UIWidget": [
 		    "getWidget<T extends UIWidget>(id: string): T;",
+		    "findWidget<T extends UIWidget>(id: string): T;",
 		    "onEvent: (eventData:UIWidgetEvent) => void;",
 		    "onChanged: () => void;"
 		]

+ 93 - 0
Source/Atomic/UI/UIWidget.cpp

@@ -29,6 +29,7 @@
 #include "UILayout.h"
 #include "UIFontDescription.h"
 #include "UIView.h"
+#include "UISelectItem.h"
 
 using namespace tb;
 
@@ -348,6 +349,98 @@ void UIWidget::Invalidate()
     widget_->Invalidate();
 }
 
+/// searches for specified widget ID from the top of the widget tree, returns the 1st one found.
+UIWidget *UIWidget::FindWidget ( const String& searchid )
+{
+    if (!widget_)
+        return NULL;
+
+    TBWidget* child = widget_->FindWidget(TBID(searchid.CString()));
+
+    if (!child)
+        return 0;
+
+    UI* ui = GetSubsystem<UI>();
+    return ui->WrapWidget(child);
+}
+
+void UIWidget::PrintPrettyTree()
+{
+    if (!widget_)
+        return;
+
+    widget_->PrintPretty("", true);
+}
+
+/// return all of the widgets of the specified classname
+/// only cpp can get full access to the widget pointers by using the
+/// TBGenericStringItemSource that is inside the UISelectItemSource 
+/// and pulling outthe tag.GetObject().
+void UIWidget::SearchWidgetClass ( const String& className, UISelectItemSource *results ) 
+{
+     if (!widget_)
+        return;
+    // we are going to fill out the TBGenericStringItemSource with all the data
+    tb::TBGenericStringItemSource *myuis = (tb::TBGenericStringItemSource*)results->GetTBItemSource();
+    widget_->SearchWidgetClass(className.CString(), myuis );
+
+    UI* ui = GetSubsystem<UI>(); // but copy the named id'd ones back into the UISelectItemSource
+    int nn=0;
+    for ( nn=0; nn<myuis->GetNumItems(); nn++ )
+    {
+        String idstr;
+        ui->GetTBIDString( myuis->GetItemID(nn), idstr);
+        if ( !idstr.Empty() )
+        {
+            results->AddItem ( new UISelectItem (context_, idstr, idstr) );
+        }
+    } 
+}
+
+///  return all of the widgets of the specified id
+void UIWidget::SearchWidgetId ( const String& searchid, UISelectItemSource *results )
+{
+     if (!widget_)
+        return;
+
+    tb::TBGenericStringItemSource *myuis = (tb::TBGenericStringItemSource*)results->GetTBItemSource();
+    widget_->SearchWidgetId(TBID(searchid.CString()),myuis );
+
+    UI* ui = GetSubsystem<UI>();
+    int nn=0;
+    for ( nn=0; nn<myuis->GetNumItems(); nn++ )
+    {
+        String idstr;
+        ui->GetTBIDString( myuis->GetItemID(nn), idstr);
+        if ( !idstr.Empty() )
+        {
+            results->AddItem ( new UISelectItem (context_, idstr, idstr) );
+        }
+    }
+}
+
+/// return all of the widgets with the specified text
+void UIWidget::SearchWidgetText ( const String& searchText, UISelectItemSource *results )
+{
+     if (!widget_)
+        return;
+
+    tb::TBGenericStringItemSource *myuis = (tb::TBGenericStringItemSource*)results->GetTBItemSource();
+    widget_->SearchWidgetText(searchText.CString(), myuis );
+
+    UI* ui = GetSubsystem<UI>();
+    int nn=0;
+    for ( nn=0; nn<myuis->GetNumItems(); nn++ )
+    {
+        String idstr;
+        ui->GetTBIDString( myuis->GetItemID(nn), idstr);
+        if ( !idstr.Empty() )
+        {
+            results->AddItem ( new UISelectItem (context_, idstr, idstr) );
+        }
+    }
+}
+
 void UIWidget::Center()
 {
     if (!widget_)

+ 12 - 0
Source/Atomic/UI/UIWidget.h

@@ -166,6 +166,7 @@ enum UI_AXIS {
 class UIView;
 class UILayoutParams;
 class UIFontDescription;
+class UISelectItemSource;
 
 /// Wraps a TurboBadger widget in our Object model
 class ATOMIC_API UIWidget : public Object, public tb::TBWidgetDelegate
@@ -203,6 +204,17 @@ class ATOMIC_API UIWidget : public Object, public tb::TBWidgetDelegate
 
     void DeleteAllChildren();
 
+    /// searches for specified widget ID from the top of the widget tree, returns the 1st one found.
+    virtual UIWidget *FindWidget ( const String& searchid );
+    /// return all of the widgets of the specified classname and id that is not 0 from the current widget
+    virtual void SearchWidgetClass ( const String& className, UISelectItemSource *results );  
+    ///  return all of the widgets of the specified id and id that is not 0 from the current widget
+    virtual void SearchWidgetId ( const String& searchid, UISelectItemSource *results );
+    /// return all of the widgets with the specified text and id that is not 0 from the current widget
+    virtual void SearchWidgetText ( const String& searchText, UISelectItemSource *results );
+    /// print out the widget tree to stdout from the current widget
+    virtual void PrintPrettyTree();
+
     // String ID
     virtual void SetId(const String& id);
 

+ 1 - 0
Source/ThirdParty/TurboBadger/tb_atomic_widgets.cpp

@@ -764,6 +764,7 @@ void TBPulldownMenu::OnInflate(const INFLATE_INFO &info)
 
 TBDockWindow::TBDockWindow( TBStr title, TBWidget *contentptr, int minwidth, int minheight ) : TBWindow()
 {
+    SetID(TBID(title));
     m_mover.AddChild(&m_redock_button);
     m_redock_button.SetSkinBg(TBIDC("TBWindow.redock"));
     m_redock_button.SetIsFocusable(false);

+ 62 - 0
Source/ThirdParty/TurboBadger/tb_widgets.cpp

@@ -18,6 +18,7 @@
 #endif // TB_ALWAYS_SHOW_EDIT_FOCUS
 
 #include <stdio.h>  // for printfs in PrintPretty
+#include "tb_select_item.h"
 
 namespace tb {
 
@@ -813,6 +814,67 @@ void TBWidget::PrintPretty( TBStr indent, bool last)
     }
 }
 
+/// searches for specified widget ID from the top of the widget tree, returns the 1st one found.
+TBWidget *TBWidget::FindWidget ( TBID searchid )
+{
+    TBWidget *rootwidget = GetParentRoot(true);
+    if(rootwidget)
+        return rootwidget->GetWidgetByID( searchid );
+    return NULL;
+}
+
+/// return all of the widgets of the specified classname
+void TBWidget::SearchWidgetClass ( TBStr className, TBGenericStringItemSource *results )
+{
+    if ( className.Equals(GetClassName()) )
+    {
+        TBGenericStringItem *matched = new TBGenericStringItem( "", GetID()); // capture ID
+        matched->tag.SetObject(this); // add widget ptr to the tag
+        results->AddItem ( matched ); // and add to the list
+    }
+    int num = numChildren();
+    for (int ii = 0; ii < num; ii++)
+    {
+        TBWidget *child = GetChildFromIndex(ii);
+        child->SearchWidgetClass( className, results);
+    }
+}
+
+///  return all of the widgets of the specified id
+void TBWidget::SearchWidgetId ( TBID searchId, TBGenericStringItemSource *results )
+{
+    if ( searchId == GetID() )
+    {
+        TBGenericStringItem *matched = new TBGenericStringItem( "", GetID());
+        matched->tag.SetObject(this);
+        results->AddItem ( matched );
+    }
+    int num = numChildren();
+    for (int ii = 0; ii < num; ii++)
+    {
+        TBWidget *child = GetChildFromIndex(ii);
+        child->SearchWidgetId( searchId, results);
+    }
+}
+
+/// return all of the widgets with the specified text
+void TBWidget::SearchWidgetText ( TBStr searchText, TBGenericStringItemSource *results )
+{
+    if ( searchText.Equals(GetText()) )
+    {
+        TBGenericStringItem *matched = new TBGenericStringItem( "", GetID());
+        matched->tag.SetObject(this);
+        results->AddItem ( matched );
+    }
+    int num = numChildren();
+    for (int ii = 0; ii < num; ii++)
+    {
+        TBWidget *child = GetChildFromIndex(ii);
+        child->SearchWidgetText( searchText, results);
+    }
+}
+
+
 // ATOMIC END
 
 

+ 10 - 1
Source/ThirdParty/TurboBadger/tb_widgets.h

@@ -22,6 +22,7 @@ class TBFontFace;
 class TBScroller;
 class TBWidgetListener;
 class TBLongClickTimer;
+class TBGenericStringItemSource;
 struct INFLATE_INFO;
 
 // == Generic widget stuff =================================================
@@ -623,9 +624,17 @@ public:
 
     /** Returns the number of children this widget contains. */
     int numChildren();
+    /// searches for specified widget ID from the top of the widget tree, returns the 1st one found.
+    TBWidget *FindWidget ( TBID searchid );
 
-    /** print out the widget tree to stdout */
+    /// print out the widget tree to stdout
     void PrintPretty( TBStr indent, bool last);
+    /// return all of the widgets of the specified classname
+    void SearchWidgetClass ( TBStr className, TBGenericStringItemSource *results );  
+    ///  return all of the widgets of the specified id
+    void SearchWidgetId ( TBID searchid, TBGenericStringItemSource *results );
+    /// return all of the widgets with the specified text
+    void SearchWidgetText ( TBStr searchText, TBGenericStringItemSource *results );
 
 // ATOMIC END