Bläddra i källkod

Support setting widget states from Javascript, better error handling on exception thrown in button callback, report errors when loading UI files

Josh Engebretson 10 år sedan
förälder
incheckning
2ecede2e4a

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

@@ -18,6 +18,7 @@ void register_freetype_font_renderer();
 using namespace tb;
 
 #include "../Core/CoreEvents.h"
+#include "../IO/Log.h"
 #include "../Input/Input.h"
 #include "../Input/InputEvents.h"
 #include "../Resource/ResourceCache.h"
@@ -321,7 +322,10 @@ void UI::TBFileReader(const char* filename, void** data, unsigned* length)
     ResourceCache* cache = uiContext_->GetSubsystem<ResourceCache>();
     SharedPtr<File> file = cache->GetFile(filename);
     if (!file || !file->IsOpen())
+    {
+        LOGERRORF("UI::TBFileReader: Unable to load file: %s", filename);
         return;
+    }
 
     unsigned size = file->GetSize();
 

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

@@ -281,6 +281,44 @@ void UIWidget::SetId(const String& id)
 
 }
 
+void UIWidget::SetState(/*WIDGET_STATE*/ unsigned state, bool on)
+{
+    if (!widget_)
+        return;
+
+    widget_->SetState((WIDGET_STATE) state, on);
+
+
+}
+
+bool UIWidget::GetState(/*WIDGET_STATE*/ unsigned state)
+{
+    if (!widget_)
+        return false;
+
+    return widget_->GetState((WIDGET_STATE) state);
+
+}
+
+void UIWidget::SetStateRaw(/*WIDGET_STATE*/ unsigned state)
+{
+    if (!widget_)
+        return;
+
+    widget_->SetStateRaw((WIDGET_STATE) state);
+
+}
+
+/*WIDGET_STATE*/ unsigned UIWidget::GetStateRaw()
+{
+    if (!widget_)
+        return false;
+
+    return (unsigned) widget_->GetStateRaw();
+
+}
+
+
 
 bool UIWidget::OnEvent(const tb::TBWidgetEvent &ev)
 {

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

@@ -45,6 +45,11 @@ public:
     void Center();
     void SetGravity(/*WIDGET_GRAVITY*/ unsigned gravity);
 
+    void SetState(/*WIDGET_STATE*/ unsigned state, bool on);
+    bool GetState(/*WIDGET_STATE*/ unsigned state);
+
+    void SetStateRaw(/*WIDGET_STATE*/ unsigned state);
+    /*WIDGET_STATE*/ unsigned GetStateRaw();
 
     void Invalidate();
     void Die();

+ 9 - 4
Source/AtomicJS/Javascript/JSUI.cpp

@@ -322,11 +322,16 @@ void JSUI::HandleWidgetEvent(StringHash eventType, VariantMap& eventData)
         duk_push_heapptr(ctx_, handlerHeapPtr);
         duk_get_prop_string(ctx_, -1, "onClick");
         if (duk_is_callable(ctx_, -1)) {
-            duk_call(ctx_, 0);
-
-            if (duk_is_boolean(ctx_, -1) && duk_to_boolean(ctx_, -1))
-                eventData[P_HANDLED] = true;
 
+            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_));

+ 16 - 1
Source/AtomicJS/Javascript/JSUIAPI.cpp

@@ -154,7 +154,7 @@ int UI_Init(duk_context* ctx)
 
     // TODO: take a config object
     ui->Initialize("DefaultUI/language/lng_en.tb.txt");
-    ui->LoadSkin("DefaultUI/skin/skin.tb.txt", "");
+    ui->LoadSkin("DefaultUI/skin/skin.tb.txt", "Skin/skin.ui.txt");
     ui->AddFont("DefaultUI/fonts/vera.ttf", "Vera");
 
     ui->SetDefaultFont("Vera", 12);
@@ -254,6 +254,21 @@ void jsapi_init_ui(JSVM* vm)
     duk_push_number(ctx, (double) tb::WINDOW_SETTINGS_DEFAULT);
     duk_put_prop_string(ctx, -2, "WINDOW_SETTINGS_DEFAULT");
 
+    duk_push_number(ctx, (double) tb::WIDGET_STATE_NONE);
+    duk_put_prop_string(ctx, -2, "WIDGET_STATE_NONE");
+    duk_push_number(ctx, (double) tb::WIDGET_STATE_DISABLED);
+    duk_put_prop_string(ctx, -2, "WIDGET_STATE_DISABLED");
+    duk_push_number(ctx, (double) tb::WIDGET_STATE_FOCUSED);
+    duk_put_prop_string(ctx, -2, "WIDGET_STATE_FOCUSED");
+    duk_push_number(ctx, (double) tb::WIDGET_STATE_PRESSED);
+    duk_put_prop_string(ctx, -2, "WIDGET_STATE_PRESSED");
+    duk_push_number(ctx, (double) tb::WIDGET_STATE_SELECTED);
+    duk_put_prop_string(ctx, -2, "WIDGET_STATE_SELECTED");
+    duk_push_number(ctx, (double) tb::WIDGET_STATE_HOVERED);
+    duk_put_prop_string(ctx, -2, "WIDGET_STATE_HOVERED");
+    duk_push_number(ctx, (double) tb::WIDGET_STATE_ALL);
+    duk_put_prop_string(ctx, -2, "WIDGET_STATE_ALL");
+
     duk_put_prop_string(ctx, -2, "UI");
 
     duk_pop(ctx);