Browse Source

fix up the uiwidget search functions

JimMarlowe 8 years ago
parent
commit
c79b2f3a99

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

@@ -25,7 +25,10 @@
 		    "getWidget<T extends UIWidget>(id: string): T;",
 		    "getWidget<T extends UIWidget>(id: string): T;",
 		    "findWidget<T extends UIWidget>(id: string): T;",
 		    "findWidget<T extends UIWidget>(id: string): T;",
 		    "onEvent: (eventData:UIWidgetEvent) => void;",
 		    "onEvent: (eventData:UIWidgetEvent) => void;",
-		    "onChanged: () => void;"
+		    "onChanged: () => void;",
+		    "searchWidgetClass(name:string):UIWidget[];",
+		    "searchWidgetId(name:string):UIWidget[];",
+		    "searchWidgetText(name:string):UIWidget[];"
 		]
 		]
 	}
 	}
 
 

+ 27 - 36
Source/Atomic/UI/UIWidget.cpp

@@ -373,71 +373,62 @@ void UIWidget::PrintPrettyTree()
 }
 }
 
 
 /// return all of the widgets of the specified classname
 /// 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 ) 
+void UIWidget::SearchWidgetClass ( const String& className, PODVector<UIWidget*> &results ) 
 {
 {
      if (!widget_)
      if (!widget_)
         return;
         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
+    tb::TBValue tbval(TBValue::TYPE_ARRAY); // TB array of values
+    tbval.SetArray(new tb::TBValueArray(), TBValue::SET_AS_STATIC); // dont delete pointers on destruction
+    widget_->SearchWidgetClass(className.CString(), tbval ); // visit all children for search 
+
+    UI* ui = GetSubsystem<UI>();
     int nn=0;
     int nn=0;
-    for ( nn=0; nn<myuis->GetNumItems(); nn++ )
+    for ( nn=0; nn<tbval.GetArrayLength(); nn++ ) // copy tbwidget ptr to uiwidget ptr
     {
     {
-        String idstr;
-        ui->GetTBIDString( myuis->GetItemID(nn), idstr);
-        if ( !idstr.Empty() )
-        {
-            results->AddItem ( new UISelectItem (context_, idstr, idstr) );
-        }
-    } 
+        tb::TBWidget *tbw = (tb::TBWidget *)tbval.GetArray()->GetValue(nn)->GetObject();
+        UIWidget *wrp = ui->WrapWidget(tbw);
+        results.Push( wrp );
+    }
 }
 }
 
 
 ///  return all of the widgets of the specified id
 ///  return all of the widgets of the specified id
-void UIWidget::SearchWidgetId ( const String& searchid, UISelectItemSource *results )
+void UIWidget::SearchWidgetId ( const String& searchid, PODVector<UIWidget*> &results )
 {
 {
      if (!widget_)
      if (!widget_)
         return;
         return;
 
 
-    tb::TBGenericStringItemSource *myuis = (tb::TBGenericStringItemSource*)results->GetTBItemSource();
-    widget_->SearchWidgetId(TBID(searchid.CString()),myuis );
+    tb::TBValue tbval(TBValue::TYPE_ARRAY);
+    tbval.SetArray(new tb::TBValueArray(), TBValue::SET_AS_STATIC);
+    widget_->SearchWidgetId(TBID(searchid.CString()), tbval );
 
 
     UI* ui = GetSubsystem<UI>();
     UI* ui = GetSubsystem<UI>();
     int nn=0;
     int nn=0;
-    for ( nn=0; nn<myuis->GetNumItems(); nn++ )
+    for ( nn=0; nn<tbval.GetArrayLength(); nn++ )
     {
     {
-        String idstr;
-        ui->GetTBIDString( myuis->GetItemID(nn), idstr);
-        if ( !idstr.Empty() )
-        {
-            results->AddItem ( new UISelectItem (context_, idstr, idstr) );
-        }
+        tb::TBWidget *tbw = (tb::TBWidget *)tbval.GetArray()->GetValue(nn)->GetObject();
+        UIWidget *wrp = ui->WrapWidget(tbw);
+        results.Push( wrp );
     }
     }
 }
 }
 
 
 /// return all of the widgets with the specified text
 /// return all of the widgets with the specified text
-void UIWidget::SearchWidgetText ( const String& searchText, UISelectItemSource *results )
+void UIWidget::SearchWidgetText ( const String& searchText, PODVector<UIWidget*> &results )
 {
 {
      if (!widget_)
      if (!widget_)
         return;
         return;
 
 
-    tb::TBGenericStringItemSource *myuis = (tb::TBGenericStringItemSource*)results->GetTBItemSource();
-    widget_->SearchWidgetText(searchText.CString(), myuis );
+    tb::TBValue tbval(TBValue::TYPE_ARRAY);
+    tbval.SetArray(new tb::TBValueArray(), TBValue::SET_AS_STATIC);
+    widget_->SearchWidgetText(searchText.CString(), tbval );
 
 
     UI* ui = GetSubsystem<UI>();
     UI* ui = GetSubsystem<UI>();
     int nn=0;
     int nn=0;
-    for ( nn=0; nn<myuis->GetNumItems(); nn++ )
+    for ( nn=0; nn<tbval.GetArrayLength(); nn++ )
     {
     {
-        String idstr;
-        ui->GetTBIDString( myuis->GetItemID(nn), idstr);
-        if ( !idstr.Empty() )
-        {
-            results->AddItem ( new UISelectItem (context_, idstr, idstr) );
-        }
+        tb::TBWidget *tbw = (tb::TBWidget *)tbval.GetArray()->GetValue(nn)->GetObject();
+        UIWidget *wrp = ui->WrapWidget(tbw);
+        results.Push( wrp );
     }
     }
 }
 }
 
 

+ 3 - 3
Source/Atomic/UI/UIWidget.h

@@ -207,11 +207,11 @@ class ATOMIC_API UIWidget : public Object, public tb::TBWidgetDelegate
     /// searches for specified widget ID from the top of the widget tree, returns the 1st one found.
     /// searches for specified widget ID from the top of the widget tree, returns the 1st one found.
     virtual UIWidget *FindWidget ( const String& searchid );
     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
     /// 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 );  
+    virtual void SearchWidgetClass ( const String& className, PODVector<UIWidget*> &results );
     ///  return all of the widgets of the specified id and id that is not 0 from the current widget
     ///  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 );
+    virtual void SearchWidgetId ( const String& searchid, PODVector<UIWidget*> &results );
     /// return all of the widgets with the specified text and id that is not 0 from the current widget
     /// 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 );
+    virtual void SearchWidgetText ( const String& searchText, PODVector<UIWidget*> &results );
     /// print out the widget tree to stdout from the current widget
     /// print out the widget tree to stdout from the current widget
     virtual void PrintPrettyTree();
     virtual void PrintPrettyTree();
 
 

+ 72 - 0
Source/AtomicJS/Javascript/JSUIAPI.cpp

@@ -127,6 +127,70 @@ int UI_DebugGetWrappedWidgetCount(duk_context* ctx)
     return 1;
     return 1;
 }
 }
 
 
+static int UIWidget_SearchWidgetClass(duk_context* ctx)
+{
+    const char* clssName = duk_require_string(ctx, 0);
+    duk_push_this(ctx);
+
+    UIWidget* uiwidget = js_to_class_instance<UIWidget>(ctx, -1, 0);
+
+    PODVector<UIWidget*> dest;
+    uiwidget->SearchWidgetClass( clssName, dest );
+
+    duk_push_array(ctx);
+
+    for (unsigned ii = 0; ii < dest.Size(); ii++)
+    {
+        js_push_class_object_instance(ctx, dest[ii], "UIWidget");
+        duk_put_prop_index(ctx, -2, ii);
+    }
+
+    return 1;
+}
+
+static int UIWidget_SearchWidgetId(duk_context* ctx)
+{
+    const char* idName = duk_require_string(ctx, 0);
+    duk_push_this(ctx);
+ 
+    UIWidget* uiwidget = js_to_class_instance<UIWidget>(ctx, -1, 0);
+
+    PODVector<UIWidget*> dest;
+    uiwidget->SearchWidgetId( idName, dest );
+
+    duk_push_array(ctx);
+
+    for (unsigned i = 0; i < dest.Size(); i++)
+    {
+        js_push_class_object_instance(ctx, dest[i], "UIWidget");
+        duk_put_prop_index(ctx, -2, i);
+    }
+
+    return 1;
+}
+
+static int UIWidget_SearchWidgetText(duk_context* ctx)
+{
+    const char* textName = duk_require_string(ctx, 0);
+    duk_push_this(ctx);
+
+    UIWidget* uiwidget = js_to_class_instance<UIWidget>(ctx, -1, 0);
+
+    PODVector<UIWidget*> dest;
+    uiwidget->SearchWidgetText( textName, dest );
+
+    duk_push_array(ctx);
+
+    for (unsigned i = 0; i < dest.Size(); i++)
+    {
+        js_push_class_object_instance(ctx, dest[i], "UIWidget");
+        duk_put_prop_index(ctx, -2, i);
+    }
+
+    return 1;
+}
+
+
 void jsapi_init_ui(JSVM* vm)
 void jsapi_init_ui(JSVM* vm)
 {
 {
     duk_context* ctx = vm->GetJSContext();
     duk_context* ctx = vm->GetJSContext();
@@ -150,6 +214,14 @@ void jsapi_init_ui(JSVM* vm)
     duk_put_prop_string(ctx, -2, "getResizeToFitContentRect");
     duk_put_prop_string(ctx, -2, "getResizeToFitContentRect");
     duk_pop(ctx);
     duk_pop(ctx);
 
 
+    js_class_get_prototype(ctx, "Atomic", "UIWidget");
+    duk_push_c_function(ctx, UIWidget_SearchWidgetClass, 1);
+    duk_put_prop_string(ctx, -2, "searchWidgetClass");
+    duk_push_c_function(ctx, UIWidget_SearchWidgetId, 1);
+    duk_put_prop_string(ctx, -2, "searchWidgetId");
+    duk_push_c_function(ctx, UIWidget_SearchWidgetText, 1);
+    duk_put_prop_string(ctx, -2, "searchWidgetText");
+    duk_pop(ctx);
 
 
 }
 }
 
 

+ 9 - 13
Source/ThirdParty/TurboBadger/tb_widgets.cpp

@@ -18,7 +18,6 @@
 #endif // TB_ALWAYS_SHOW_EDIT_FOCUS
 #endif // TB_ALWAYS_SHOW_EDIT_FOCUS
 
 
 #include <stdio.h>  // for printfs in PrintPretty
 #include <stdio.h>  // for printfs in PrintPretty
-#include "tb_select_item.h"
 
 
 namespace tb {
 namespace tb {
 
 
@@ -824,13 +823,12 @@ TBWidget *TBWidget::FindWidget ( TBID searchid )
 }
 }
 
 
 /// return all of the widgets of the specified classname
 /// return all of the widgets of the specified classname
-void TBWidget::SearchWidgetClass ( TBStr className, TBGenericStringItemSource *results )
+void TBWidget::SearchWidgetClass ( TBStr className, TBValue &results )
 {
 {
     if ( className.Equals(GetClassName()) )
     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
+        TBValue *new_val = results.GetArray()->AddValue();
+        new_val->SetObject(this); 
     }
     }
     int num = numChildren();
     int num = numChildren();
     for (int ii = 0; ii < num; ii++)
     for (int ii = 0; ii < num; ii++)
@@ -841,13 +839,12 @@ void TBWidget::SearchWidgetClass ( TBStr className, TBGenericStringItemSource *r
 }
 }
 
 
 ///  return all of the widgets of the specified id
 ///  return all of the widgets of the specified id
-void TBWidget::SearchWidgetId ( TBID searchId, TBGenericStringItemSource *results )
+void TBWidget::SearchWidgetId ( TBID searchId, TBValue &results )
 {
 {
     if ( searchId == GetID() )
     if ( searchId == GetID() )
     {
     {
-        TBGenericStringItem *matched = new TBGenericStringItem( "", GetID());
-        matched->tag.SetObject(this);
-        results->AddItem ( matched );
+        TBValue *new_val = results.GetArray()->AddValue();
+        new_val->SetObject(this); 
     }
     }
     int num = numChildren();
     int num = numChildren();
     for (int ii = 0; ii < num; ii++)
     for (int ii = 0; ii < num; ii++)
@@ -858,13 +855,12 @@ void TBWidget::SearchWidgetId ( TBID searchId, TBGenericStringItemSource *result
 }
 }
 
 
 /// return all of the widgets with the specified text
 /// return all of the widgets with the specified text
-void TBWidget::SearchWidgetText ( TBStr searchText, TBGenericStringItemSource *results )
+void TBWidget::SearchWidgetText ( TBStr searchText, TBValue &results )
 {
 {
     if ( searchText.Equals(GetText()) )
     if ( searchText.Equals(GetText()) )
     {
     {
-        TBGenericStringItem *matched = new TBGenericStringItem( "", GetID());
-        matched->tag.SetObject(this);
-        results->AddItem ( matched );
+        TBValue *new_val = results.GetArray()->AddValue();
+        new_val->SetObject(this); 
     }
     }
     int num = numChildren();
     int num = numChildren();
     for (int ii = 0; ii < num; ii++)
     for (int ii = 0; ii < num; ii++)

+ 3 - 4
Source/ThirdParty/TurboBadger/tb_widgets.h

@@ -22,7 +22,6 @@ class TBFontFace;
 class TBScroller;
 class TBScroller;
 class TBWidgetListener;
 class TBWidgetListener;
 class TBLongClickTimer;
 class TBLongClickTimer;
-class TBGenericStringItemSource;
 struct INFLATE_INFO;
 struct INFLATE_INFO;
 
 
 // == Generic widget stuff =================================================
 // == Generic widget stuff =================================================
@@ -630,11 +629,11 @@ public:
     /// print out the widget tree to stdout
     /// print out the widget tree to stdout
     void PrintPretty( TBStr indent, bool last);
     void PrintPretty( TBStr indent, bool last);
     /// return all of the widgets of the specified classname
     /// return all of the widgets of the specified classname
-    void SearchWidgetClass ( TBStr className, TBGenericStringItemSource *results );  
+    void SearchWidgetClass ( TBStr className, TBValue &results );  
     ///  return all of the widgets of the specified id
     ///  return all of the widgets of the specified id
-    void SearchWidgetId ( TBID searchid, TBGenericStringItemSource *results );
+    void SearchWidgetId ( TBID searchid, TBValue &results );
     /// return all of the widgets with the specified text
     /// return all of the widgets with the specified text
-    void SearchWidgetText ( TBStr searchText, TBGenericStringItemSource *results );
+    void SearchWidgetText ( TBStr searchText, TBValue &results );
 
 
 // ATOMIC END
 // ATOMIC END