Browse Source

Fix the API binding and minor code cleanup.
Close #2093

Yao Wei Tjong 姚伟忠 7 years ago
parent
commit
7fce254c22

+ 10 - 0
Source/Urho3D/AngelScript/APITemplates.h

@@ -1292,6 +1292,16 @@ template <class T> void RegisterUIElement(asIScriptEngine* engine, const char* c
     engine->RegisterObjectMethod(className, "Array<String>@ get_tags() const", asFUNCTION(UIElementGetTags), asCALL_CDECL_OBJLAST);
 }
 
+/// Template function for registering a class derived from UISelectable.
+template <class T> void RegisterUISelectable(asIScriptEngine* engine, const char* className)
+{
+    RegisterUIElement<T>(engine, className);
+    engine->RegisterObjectMethod(className, "void set_selectionColor(const Color&in)", asMETHOD(T, SetSelectionColor), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "const Color& get_selectionColor() const", asMETHOD(T, GetSelectionColor), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "void set_hoverColor(const Color&in)", asMETHOD(T, SetHoverColor), asCALL_THISCALL);
+    engine->RegisterObjectMethod(className, "const Color& get_hoverColor() const", asMETHOD(T, GetHoverColor), asCALL_THISCALL);
+}
+
 /// Template function for registering a class derived from BorderImage.
 template <class T> void RegisterBorderImage(asIScriptEngine* engine, const char* className)
 {

+ 7 - 5
Source/Urho3D/AngelScript/UIAPI.cpp

@@ -136,6 +136,11 @@ static void RegisterUIElement(asIScriptEngine* engine)
     engine->RegisterObjectMethod("TouchState", "UIElement@+ get_touchedElement()", asMETHOD(TouchState, GetTouchedElement), asCALL_THISCALL);
 }
 
+static void RegisterUISelectable(asIScriptEngine* engine)
+{
+    RegisterUISelectable<UISelectable>(engine, "UISelectable");
+}
+
 static void RegisterBorderImage(asIScriptEngine* engine)
 {
     RegisterBorderImage<BorderImage>(engine, "BorderImage");
@@ -384,7 +389,7 @@ static void RegisterText(asIScriptEngine* engine)
     engine->RegisterEnumValue("TextEffect", "TE_SHADOW", TE_SHADOW);
     engine->RegisterEnumValue("TextEffect", "TE_STROKE", TE_STROKE);
 
-    RegisterUIElement<Text>(engine, "Text");
+    RegisterUISelectable<Text>(engine, "Text");
     engine->RegisterObjectMethod("Text", "bool SetFont(const String&in, float)", asMETHODPR(Text, SetFont, (const String&, float), bool), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text", "bool SetFont(Font@+, float)", asMETHODPR(Text, SetFont, (Font*, float), bool), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text", "void SetSelection(uint, uint arg1 = M_MAX_UNSIGNED)", asMETHOD(Text, SetSelection), asCALL_THISCALL);
@@ -404,10 +409,6 @@ static void RegisterText(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Text", "bool get_autoLocalizable() const", asMETHOD(Text, GetAutoLocalizable), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text", "uint get_selectionStart() const", asMETHOD(Text, GetSelectionStart), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text", "uint get_selectionLength() const", asMETHOD(Text, GetSelectionLength), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Text", "void set_selectionColor(const Color&in)", asMETHOD(Text, SetSelectionColor), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Text", "const Color& get_selectionColor() const", asMETHOD(Text, GetSelectionColor), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Text", "void set_hoverColor(const Color&in)", asMETHOD(Text, SetHoverColor), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Text", "const Color& get_hoverColor() const", asMETHOD(Text, GetHoverColor), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text", "void set_textEffect(TextEffect)", asMETHOD(Text, SetTextEffect), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text", "TextEffect get_textEffect() const", asMETHOD(Text, GetTextEffect), asCALL_THISCALL);
     engine->RegisterObjectMethod("Text", "void set_effectShadowOffset(const IntVector2&in)", asMETHOD(Text, SetEffectShadowOffset), asCALL_THISCALL);
@@ -823,6 +824,7 @@ void RegisterUIAPI(asIScriptEngine* engine)
 {
     RegisterFont(engine);
     RegisterUIElement(engine);
+    RegisterUISelectable(engine);
     RegisterBorderImage(engine);
     RegisterSprite(engine);
     RegisterButton(engine);

+ 1 - 7
Source/Urho3D/LuaScript/pkgs/UI/Text.pkg

@@ -7,7 +7,7 @@ enum TextEffect
     TE_STROKE
 };
 
-class Text : public UIElement
+class Text : public UISelectable
 {
     Text();
     virtual ~Text();
@@ -23,8 +23,6 @@ class Text : public UIElement
     void SetWordwrap(bool enable);
     void SetSelection(unsigned start, unsigned length = M_MAX_UNSIGNED);
     void ClearSelection();
-    void SetSelectionColor(const Color& color);
-    void SetHoverColor(const Color& color);
     void SetTextEffect(TextEffect textEffect);
     void SetEffectShadowOffset(const IntVector2& offset);
     void SetEffectStrokeThickness(int thickness);
@@ -42,8 +40,6 @@ class Text : public UIElement
     bool GetWordwrap() const;
     unsigned GetSelectionStart() const;
     unsigned GetSelectionLength() const;
-    const Color& GetSelectionColor() const;
-    const Color& GetHoverColor() const;
     TextEffect GetTextEffect() const;
     const IntVector2& GetEffectShadowOffset() const;
     int GetEffectStrokeThickness() const;
@@ -68,8 +64,6 @@ class Text : public UIElement
     tolua_property__get_set bool autoLocalizable;
     tolua_readonly tolua_property__get_set unsigned selectionStart;
     tolua_readonly tolua_property__get_set unsigned selectionLength;
-    tolua_property__get_set Color& selectionColor;
-    tolua_property__get_set Color& hoverColor;
     tolua_property__get_set TextEffect textEffect;
     tolua_property__get_set IntVector2& effectShadowOffset;
     tolua_property__get_set int effectStrokeThickness;

+ 30 - 0
Source/Urho3D/LuaScript/pkgs/UI/UISelectable.pkg

@@ -0,0 +1,30 @@
+$#include "UI/UISelectable.h"
+
+class UISelectable : public UIElement
+{
+    UISelectable();
+    virtual ~UISelectable();
+
+    void SetSelectionColor(const Color& color);
+    void SetHoverColor(const Color& color);
+
+    const Color& GetSelectionColor() const;
+    const Color& GetHoverColor() const;
+
+    tolua_property__get_set Color& selectionColor;
+    tolua_property__get_set Color& hoverColor;
+};
+
+${
+#define TOLUA_DISABLE_tolua_UILuaAPI_UISelectable_new00
+static int tolua_UILuaAPI_UISelectable_new00(lua_State* tolua_S)
+{
+    return ToluaNewObject<UISelectable>(tolua_S);
+}
+
+#define TOLUA_DISABLE_tolua_UILuaAPI_UISelectable_new00_local
+static int tolua_UILuaAPI_UISelectable_new00_local(lua_State* tolua_S)
+{
+    return ToluaNewObjectGC<UISelectable>(tolua_S);
+}
+$}

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/UILuaAPI.pkg

@@ -1,4 +1,5 @@
 $pfile "UI/UIElement.pkg"
+$pfile "UI/UISelectable.pkg"
 $pfile "UI/BorderImage.pkg"
 $pfile "UI/Button.pkg"
 $pfile "UI/CheckBox.pkg"

+ 1 - 1
Source/Urho3D/UI/Text.h

@@ -22,7 +22,7 @@
 
 #pragma once
 
-#include "../UI/Selectable.h"
+#include "../UI/UISelectable.h"
 
 namespace Urho3D
 {

+ 24 - 2
Source/Urho3D/UI/Selectable.cpp → Source/Urho3D/UI/UISelectable.cpp

@@ -1,7 +1,29 @@
-#include "Selectable.h"
+//
+// Copyright (c) 2008-2018 the Urho3D project.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include "../Precompiled.h"
 
-#include "../UI/UI.h"
 #include "../Core/Context.h"
+#include "../UI/UISelectable.h"
 
 namespace Urho3D
 {

+ 8 - 4
Source/Urho3D/UI/Selectable.h → Source/Urho3D/UI/UISelectable.h

@@ -22,6 +22,7 @@
 
 #pragma once
 
+#include "../Math/Color.h"
 #include "../UI/UIElement.h"
 
 namespace Urho3D
@@ -32,7 +33,9 @@ class URHO3D_API UISelectable : public UIElement
 public:
     URHO3D_OBJECT(UISelectable, UIElement);
 
+    /// Construct.
     using UIElement::UIElement;
+    /// Destruct.
     ~UISelectable() override = default;
 
     /// Register object factory.
@@ -48,13 +51,14 @@ public:
 
     /// Return selection background color.
     const Color& GetSelectionColor() const { return selectionColor_; }
-
     /// Return hover background color.
     const Color& GetHoverColor() const { return hoverColor_; }
 
 protected:
-    Color selectionColor_{ Color::TRANSPARENT };
-    Color hoverColor_{ Color::TRANSPARENT };
+    /// Selection background color.
+    Color selectionColor_{Color::TRANSPARENT};
+    /// Hover background color.
+    Color hoverColor_{Color::TRANSPARENT};
 };
 
-}
+}