Browse Source

Adding UICheckBox, UIClickLabel, changed event handling

Josh Engebretson 10 years ago
parent
commit
b012d4196e

+ 19 - 0
Source/Atomic/UI/UI.cpp

@@ -34,6 +34,8 @@ using namespace tb;
 #include "UIEditField.h"
 #include "UILayout.h"
 #include "UIImageWidget.h"
+#include "UIClickLabel.h"
+#include "UICheckBox.h"
 
 namespace tb
 {
@@ -442,6 +444,8 @@ UIWidget* UI::WrapWidget(tb::TBWidget* widget)
     if (widgetWrap_.Contains(widget))
         return widgetWrap_[widget];
 
+    // switch this to use a factory?
+
     // this is order dependent as we're using IsOfType which also works if a base class
     if (widget->IsOfType<TBLayout>())
     {
@@ -486,6 +490,21 @@ UIWidget* UI::WrapWidget(tb::TBWidget* widget)
         widgetWrap_[widget] = imagewidget;
         return imagewidget;
     }
+    if (widget->IsOfType<TBClickLabel>())
+    {
+        UIClickLabel* nwidget = new UIClickLabel(context_, false);
+        nwidget->SetWidget(widget);
+        widgetWrap_[widget] = nwidget;
+        return nwidget;
+    }
+
+    if (widget->IsOfType<TBCheckBox>())
+    {
+        UICheckBox* nwidget = new UICheckBox(context_, false);
+        nwidget->SetWidget(widget);
+        widgetWrap_[widget] = nwidget;
+        return nwidget;
+    }
 
     if (widget->IsOfType<TBWidget>())
     {

+ 35 - 0
Source/Atomic/UI/UICheckBox.cpp

@@ -0,0 +1,35 @@
+
+#include <TurboBadger/tb_widgets.h>
+#include <TurboBadger/tb_widgets_common.h>
+
+#include <Atomic/IO/Log.h>
+
+#include "UIEvents.h"
+#include "UI.h"
+#include "UICheckBox.h"
+
+using namespace tb;
+
+namespace Atomic
+{
+
+UICheckBox::UICheckBox(Context* context, bool createWidget) : UIWidget(context, false)
+{
+    if (createWidget)
+    {
+        widget_ = new TBCheckBox();
+        widget_->SetDelegate(this);
+        GetSubsystem<UI>()->WrapWidget(this, widget_);
+    }
+}
+
+UICheckBox::~UICheckBox()
+{
+}
+
+bool UICheckBox::OnEvent(const tb::TBWidgetEvent &ev)
+{
+    return UIWidget::OnEvent(ev);
+}
+
+}

+ 27 - 0
Source/Atomic/UI/UICheckBox.h

@@ -0,0 +1,27 @@
+
+#pragma once
+
+#include "UIWidget.h"
+
+namespace Atomic
+{
+
+
+class UICheckBox : public UIWidget
+{
+    OBJECT(UICheckBox)
+
+public:
+
+    UICheckBox(Context* context, bool createWidget = true);
+    virtual ~UICheckBox();
+
+protected:
+
+    virtual bool OnEvent(const tb::TBWidgetEvent &ev);
+
+private:
+
+};
+
+}

+ 35 - 0
Source/Atomic/UI/UIClickLabel.cpp

@@ -0,0 +1,35 @@
+
+#include <TurboBadger/tb_widgets.h>
+#include <TurboBadger/tb_widgets_common.h>
+
+#include <Atomic/IO/Log.h>
+
+#include "UIEvents.h"
+#include "UI.h"
+#include "UIClickLabel.h"
+
+using namespace tb;
+
+namespace Atomic
+{
+
+UIClickLabel::UIClickLabel(Context* context, bool createWidget) : UIWidget(context, false)
+{
+    if (createWidget)
+    {
+        widget_ = new TBClickLabel();
+        widget_->SetDelegate(this);
+        GetSubsystem<UI>()->WrapWidget(this, widget_);
+    }
+}
+
+UIClickLabel::~UIClickLabel()
+{
+}
+
+bool UIClickLabel::OnEvent(const tb::TBWidgetEvent &ev)
+{
+    return UIWidget::OnEvent(ev);
+}
+
+}

+ 27 - 0
Source/Atomic/UI/UIClickLabel.h

@@ -0,0 +1,27 @@
+
+#pragma once
+
+#include "UIWidget.h"
+
+namespace Atomic
+{
+
+
+class UIClickLabel : public UIWidget
+{
+    OBJECT(UIClickLabel)
+
+public:
+
+    UIClickLabel(Context* context, bool createWidget = true);
+    virtual ~UIClickLabel();
+
+protected:
+
+    virtual bool OnEvent(const tb::TBWidgetEvent &ev);
+
+private:
+
+};
+
+}

+ 33 - 1
Source/Atomic/UI/UIWidget.cpp

@@ -291,6 +291,24 @@ void UIWidget::SetState(/*WIDGET_STATE*/ unsigned state, bool on)
 
 }
 
+void UIWidget::SetValue(double value)
+{
+    if (!widget_)
+        return;
+
+    widget_->SetValueDouble(value);
+}
+
+double UIWidget::GetValue()
+{
+    if (!widget_)
+        return 0.0;
+
+    return widget_->GetValueDouble();
+
+}
+
+
 bool UIWidget::GetState(/*WIDGET_STATE*/ unsigned state)
 {
     if (!widget_)
@@ -324,7 +342,21 @@ bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
 {
     UI* ui = GetSubsystem<UI>();
 
-    if (ev.type == EVENT_TYPE_CLICK)
+    if (ev.type == EVENT_TYPE_CHANGED)
+    {
+        if (!ev.target || ui->IsWidgetWrapped(ev.target))
+        {
+            VariantMap eventData;
+            ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
+            SendEvent(E_WIDGETEVENT, eventData);
+
+            if (eventData[WidgetEvent::P_HANDLED].GetBool())
+                return true;
+
+        }
+
+    }
+    else if (ev.type == EVENT_TYPE_CLICK)
     {
         if (ev.target && ev.target->GetID() == TBID("__popup-menu"))
         {

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

@@ -45,6 +45,9 @@ public:
     void Center();
     void SetGravity(/*WIDGET_GRAVITY*/ unsigned gravity);
 
+    void SetValue(double value);
+    double GetValue();
+
     void SetState(/*WIDGET_STATE*/ unsigned state, bool on);
     bool GetState(/*WIDGET_STATE*/ unsigned state);
 

+ 1 - 1
Source/AtomicJS/JSBind/modules/UI.json

@@ -4,7 +4,7 @@
 	"includes" : ["<Atomic/Graphics/Material.h>", "<Atomic/Scene/Node.h>", "<Atomic/Scene/Scene.h>", "<Atomic/Graphics/Texture2D.h>"],
 	"classes" : ["UIWidget", "UILayout", "UIView", "UIWindow", "UIButton", "UITextField",
 								"UISelectItem", "UISelectItemSource", "UIMenuWindow", "UIEditField",
-								"UIImageWidget"],
+								"UIImageWidget", "UIClickLabel", "UICheckBox"],
 	"overloads" : {
 	}
 }

+ 25 - 0
Source/AtomicJS/Javascript/JSUI.cpp

@@ -44,6 +44,8 @@ JSUI::JSUI(Context* context) : Object(context),
     uiTypes_["UILayout"] = true;
     uiTypes_["UIMenuWindow"] = true;
     uiTypes_["UIWindow"] = true;
+    uiTypes_["UIClickLabel"] = true;
+    uiTypes_["UICheckBox"] = true;
 }
 
 JSUI::~JSUI()
@@ -296,6 +298,29 @@ void JSUI::HandleWidgetEvent(StringHash eventType, VariantMap& eventData)
 
     tb::EVENT_TYPE type = (tb::EVENT_TYPE) eventData[P_TYPE].GetUInt();
 
+    if (type == tb::EVENT_TYPE_CHANGED)
+    {
+        int top = duk_get_top(ctx_);
+        duk_push_heapptr(ctx_, handlerHeapPtr);
+        duk_get_prop_string(ctx_, -1, "onChanged");
+        if (duk_is_callable(ctx_, -1)) {
+
+            if (duk_pcall(ctx_, 0) != 0)
+            {
+                JSVM::GetJSVM(nullptr)->SendJSErrorEvent();
+            }
+            else
+            {
+                if (duk_is_boolean(ctx_, -1) && duk_to_boolean(ctx_, -1))
+                    eventData[P_HANDLED] = true;
+            }
+        }
+        duk_pop_n(ctx_, 2);
+        assert(top == duk_get_top(ctx_));
+        return;
+
+    }
+
     // general event handler, bubbles to (wrapped) parent widgets unless handled (returns true)
     if (type == tb::EVENT_TYPE_CLICK)
     {

+ 1 - 1
Source/ThirdParty/TurboBadger/tb_widgets_common.cpp

@@ -427,7 +427,7 @@ bool TBRadioCheckBox::OnEvent(const TBWidgetEvent &ev)
 			SetValue(!GetValue());
 		}
 	}
-	return false;
+    return TBWidget::OnEvent(ev);
 }
 
 // == TBScrollBar =======================================