Explorar el Código

Updated the ProgressBar PR to include Lua and AngelScript bindings
Added the ability to show or hide the percent text

Victor Holt hace 9 años
padre
commit
a2261f5a56

+ 18 - 0
Source/Urho3D/AngelScript/UIAPI.cpp

@@ -31,6 +31,7 @@
 #include "../UI/LineEdit.h"
 #include "../UI/ListView.h"
 #include "../UI/MessageBox.h"
+#include "../UI/ProgressBar.h"
 #include "../UI/ScrollBar.h"
 #include "../UI/Slider.h"
 #include "../UI/Sprite.h"
@@ -513,6 +514,22 @@ static void RegisterMessageBox(asIScriptEngine* engine)
     engine->RegisterObjectMethod("MessageBox", "UIElement@+ get_window() const", asMETHOD(MessageBox, GetWindow), asCALL_THISCALL);
 }
 
+static void RegisterProgressBar(asIScriptEngine* engine)
+{
+    RegisterObject<ProgressBar>(engine, "ProgressBar");
+    engine->RegisterObjectMethod("ProgressBar", "void set_orientation(Orientation)", asMETHOD(ProgressBar, SetOrientation), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ProgressBar", "Orientation get_orientation() const", asMETHOD(ProgressBar, GetOrientation), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ProgressBar", "void set_range(float)", asMETHOD(ProgressBar, SetRange), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ProgressBar", "float get_range() const", asMETHOD(ProgressBar, GetRange), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ProgressBar", "void set_value(float)", asMETHOD(ProgressBar, SetValue), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ProgressBar", "float get_value() const", asMETHOD(ProgressBar, GetValue), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ProgressBar", "void set_showPercentText(bool)", asMETHOD(ProgressBar, SetShowPercentText), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ProgressBar", "bool get_showPercentText() const", asMETHOD(ProgressBar, GetShowPercentText), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ProgressBar", "void ChangeValue(float)", asMETHOD(ProgressBar, ChangeValue), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ProgressBar", "BorderImage@+ get_knob() const", asMETHOD(ProgressBar, GetKnob), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ProgressBar", "void SetLoadingPercentStyle(const String&in)", asMETHOD(ProgressBar, SetLoadingPercentStyle), asCALL_THISCALL);
+}
+
 static CScriptArray* DropDownListGetItems(DropDownList* ptr)
 {
     PODVector<UIElement*> result = ptr->GetItems();
@@ -778,6 +795,7 @@ void RegisterUIAPI(asIScriptEngine* engine)
     RegisterLineEdit(engine);
     RegisterMenu(engine);
     RegisterMessageBox(engine);
+    RegisterProgressBar(engine);
     RegisterDropDownList(engine);
     RegisterWindow(engine);
     RegisterView3D(engine);

+ 42 - 0
Source/Urho3D/LuaScript/pkgs/UI/ProgressBar.pkg

@@ -0,0 +1,42 @@
+$#include "UI/ProgressBar.h"
+
+class ProgressBar : public BorderImage
+{
+    ProgressBar();
+    virtual ~ProgressBar();
+
+    void SetOrientation(Orientation orientation);
+    void SetRange(float range);
+    void SetValue(float value);
+    void ChangeValue(float delta);
+    void SetLoadingPercentStyle(const String style);
+    void SetShowPercentText(bool showPercentText);
+
+    Orientation GetOrientation() const;
+    float GetRange() const;
+    float GetValue() const;
+    BorderImage* GetKnob() const;
+    const String GetLoadingPercentStyle();
+    bool GetShowPercentText() const;
+
+    tolua_property__get_set Orientation orientation;
+    tolua_property__get_set float range;
+    tolua_property__get_set float value;
+    tolua_readonly tolua_property__get_set BorderImage* knob;
+    tolua_readonly tolua_property__get_set String loadingPercentStyle;
+    tolua_property__get_set bool showPercentText;
+};
+
+${
+#define TOLUA_DISABLE_tolua_UILuaAPI_ProgressBar_new00
+static int tolua_UILuaAPI_ProgressBar_new00(lua_State* tolua_S)
+{
+    return ToluaNewObject<ProgressBar>(tolua_S);
+}
+
+#define TOLUA_DISABLE_tolua_UILuaAPI_ProgressBar_new00_local
+static int tolua_UILuaAPI_ProgressBar_new00_local(lua_State* tolua_S)
+{
+    return ToluaNewObjectGC<ProgressBar>(tolua_S);
+}
+$}

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

@@ -8,6 +8,7 @@ $pfile "UI/Font.pkg"
 $pfile "UI/LineEdit.pkg"
 $pfile "UI/Menu.pkg"
 $pfile "UI/MessageBox.pkg"
+$pfile "UI/ProgressBar.pkg"
 $pfile "UI/DropDownList.pkg"
 $pfile "UI/Slider.pkg"
 $pfile "UI/ScrollBar.pkg"

+ 171 - 0
Source/Urho3D/UI/ProgressBar.cpp

@@ -0,0 +1,171 @@
+//
+// Copyright (c) 2008-2016 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 "ProgressBar.h"
+
+#include "../Core/Context.h"
+#include "../Input/InputEvents.h"
+#include "../IO/Log.h"
+#include "UIEvents.h"
+
+namespace Urho3D
+{
+
+extern const char* orientations[];
+extern const char* UI_CATEGORY;
+
+ProgressBar::ProgressBar(Context * context) :
+        BorderImage(context),
+        orientation_(O_HORIZONTAL),
+        loadingPercentStyle_("Text"),
+        range_(1.0f),
+        value_(0.0f),
+        showPercentText_(true)
+{
+    SetEnabled(false);
+    SetEditable(false);
+    SetFocus(false);
+    knob_ = CreateChild<BorderImage>("S_Knob");
+    knob_->SetInternal(true);
+
+    loadingText_ = CreateChild<Text>("S_Text");
+    loadingText_->SetInternal(true);
+
+    UpdateProgressBar();
+}
+
+ProgressBar::~ProgressBar()
+{
+}
+
+void ProgressBar::RegisterObject(Context * context)
+{
+    context->RegisterFactory<ProgressBar>(UI_CATEGORY);
+
+    URHO3D_COPY_BASE_ATTRIBUTES(BorderImage);
+    URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
+    URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Orientation", GetOrientation, SetOrientation, Orientation, orientations, O_HORIZONTAL, AM_FILE);
+    URHO3D_ACCESSOR_ATTRIBUTE("Range", GetRange, SetRange, float, 1.0f, AM_FILE);
+    URHO3D_ACCESSOR_ATTRIBUTE("Value", GetValue, SetValue, float, 0.0f, AM_FILE);
+    URHO3D_ACCESSOR_ATTRIBUTE("Show Percent Text", GetShowPercentText, SetShowPercentText, bool, true, AM_FILE);
+}
+
+void ProgressBar::OnResize()
+{
+    UpdateProgressBar();
+}
+
+void ProgressBar::SetOrientation(Orientation type)
+{
+    orientation_ = type;
+    UpdateProgressBar();
+}
+
+void ProgressBar::SetRange(float range)
+{
+    range = Max(range, 0.0f);
+    if (range != range_)
+    {
+        range_ = range;
+        UpdateProgressBar();
+    }
+}
+
+void ProgressBar::SetValue(float value)
+{
+    value = Clamp(value, 0.0f, range_);
+    if (value != value_)
+    {
+        value_ = value;
+        UpdateProgressBar();
+
+        using namespace ProgressBarChanged;
+
+        VariantMap& eventData = GetEventDataMap();
+        eventData[P_ELEMENT] = this;
+        eventData[P_VALUE] = value_;
+        SendEvent(E_PROGRESSBARCHANGED, eventData);
+    }
+}
+
+void ProgressBar::ChangeValue(float delta)
+{
+    SetValue(value_ + delta);
+}
+
+bool ProgressBar::FilterImplicitAttributes(XMLElement &dest) const
+{
+    if (!BorderImage::FilterImplicitAttributes(dest))
+        return false;
+
+    XMLElement childElem = dest.GetChild("element");
+    if (!childElem)
+        return false;
+    if (!RemoveChildXML(childElem, "Name", "S_Knob"))
+        return false;
+    if (!RemoveChildXML(childElem, "Name", "S_Text"))
+        return false;
+    if (!RemoveChildXML(childElem, "Position"))
+        return false;
+    if (!RemoveChildXML(childElem, "Size"))
+        return false;
+
+    return true;
+}
+
+void ProgressBar::UpdateProgressBar()
+{
+    const IntRect &border = knob_->GetBorder();
+
+    if (range_ > 0.0f)
+    {
+        if (orientation_ == O_HORIZONTAL)
+        {
+            int loadingBarLength = (int) Max((float) GetWidth() * value_ / range_,
+                                             (float) (border.left_ + border.right_));
+            knob_->SetSize(loadingBarLength, GetHeight());
+            knob_->SetPosition(Clamp(0, 0, GetWidth() - knob_->GetWidth()), 0);
+        }
+        else
+        {
+            int loadingBarLength = (int) Max((float) GetHeight() * value_ / range_,
+                                             (float) (border.top_ + border.bottom_));
+            knob_->SetSize(GetWidth(), loadingBarLength);
+            knob_->SetPosition(0, Clamp(0, 0, GetHeight() - knob_->GetHeight()));
+        }
+    }
+    else
+    {
+        knob_->SetSize(GetSize());
+        knob_->SetPosition(0, 0);
+    }
+
+    // Display the percent text.
+    loadingText_->SetVisible(showPercentText_);
+
+    // Update the text.
+    loadingText_->SetStyle(loadingPercentStyle_);
+    loadingText_->SetAlignment(HA_CENTER, VA_CENTER);
+    loadingText_->SetText(Urho3D::ToString("%f %%", value_));
+}
+
+}

+ 117 - 0
Source/Urho3D/UI/ProgressBar.h

@@ -0,0 +1,117 @@
+//
+// Copyright (c) 2008-2016 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.
+//
+
+#pragma once
+
+#include "BorderImage.h"
+#include "../Math/Vector2.h"
+#include "Text.h"
+
+namespace Urho3D
+{
+
+/// %ProgressBar bar %UI element.
+class URHO3D_API ProgressBar : public BorderImage
+{
+    URHO3D_OBJECT(ProgressBar, BorderImage);
+
+public:
+    /// Construct.
+    ProgressBar(Context *context);
+
+    /// Destruct.
+    virtual ~ProgressBar();
+
+    /// Register object factory.
+    static void RegisterObject(Context *context);
+
+    /// React to resize.
+    virtual void OnResize();
+
+    /// Set orientation type.
+    void SetOrientation(Orientation orientation);
+
+    /// Set ProgressBar range maximum value (minimum value is always 0.)
+    void SetRange(float range);
+
+    /// Set ProgressBar current value.
+    void SetValue(float value);
+
+    /// Change value by a delta.
+    void ChangeValue(float delta);
+
+    /// Return orientation type.
+    Orientation GetOrientation() const
+    { return orientation_; }
+
+    /// Return ProgressBar range.
+    float GetRange() const
+    { return range_; }
+
+    /// Return ProgressBar current value.
+    float GetValue() const
+    { return value_; }
+
+    /// Return knob element.
+    BorderImage *GetKnob() const
+    { return knob_; }
+
+    /// Sets the loading percent style.
+    void SetLoadingPercentStyle(const String &style)
+    { loadingPercentStyle_ = style; }
+
+    /// Returns the loading percent style.
+    const String& GetLoadingPercentStyle() const
+    { return loadingPercentStyle_; }
+
+    /// Sets the flag to display the percent text.
+    void SetShowPercentText(bool showPercentText)
+    { showPercentText_ = showPercentText; }
+
+    /// Returns the flag to display the percent text.
+    bool GetShowPercentText() const
+    { return showPercentText_; }
+
+protected:
+    /// Filter implicit attributes in serialization process.
+    virtual bool FilterImplicitAttributes(XMLElement &dest) const;
+
+    /// Update ProgressBar knob position & size.
+    void UpdateProgressBar();
+
+    /// ProgressBar knob.
+    SharedPtr <BorderImage> knob_;
+    /// ProgressBar text
+    SharedPtr <Text> loadingText_;
+    /// Orientation.
+    Orientation orientation_;
+    /// ProgressBar text style
+    String loadingPercentStyle_;
+    /// ProgressBar range.
+    float range_;
+    /// ProgressBar current value.
+    float value_;
+    /// Flag to show the percent text.
+    bool showPercentText_;
+};
+
+}

+ 2 - 0
Source/Urho3D/UI/UI.cpp

@@ -45,6 +45,7 @@
 #include "../UI/LineEdit.h"
 #include "../UI/ListView.h"
 #include "../UI/MessageBox.h"
+#include "../UI/ProgressBar.h"
 #include "../UI/ScrollBar.h"
 #include "../UI/Slider.h"
 #include "../UI/Sprite.h"
@@ -1897,6 +1898,7 @@ void RegisterUILibrary(Context* context)
     DropDownList::RegisterObject(context);
     FileSelector::RegisterObject(context);
     MessageBox::RegisterObject(context);
+    ProgressBar::RegisterObject(context);
     ToolTip::RegisterObject(context);
 }
 

+ 7 - 0
Source/Urho3D/UI/UIEvents.h

@@ -202,6 +202,13 @@ URHO3D_EVENT(E_SLIDERPAGED, SliderPaged)
     URHO3D_PARAM(P_PRESSED, Pressed);              // bool
 }
 
+/// UI progressbar value changed
+URHO3D_EVENT(E_PROGRESSBARCHANGED, ProgressBarChanged)
+{
+    URHO3D_PARAM(P_ELEMENT, Element);              // UIElement pointer
+    URHO3D_PARAM(P_VALUE, Value);                  // float
+}
+
 /// UI scrollbar value changed.
 URHO3D_EVENT(E_SCROLLBARCHANGED, ScrollBarChanged)
 {

BIN
bin/Data/Textures/Editor/EditorIcons.png


+ 10 - 0
bin/Data/UI/DefaultStyle.xml

@@ -182,6 +182,16 @@
             <attribute name="Clip Border" value="2 2 2 2" />
         </element>
     </element>
+    <element type="ProgressBar" style="BorderImage">
+        <attribute name="Size" value="16 16" />
+        <attribute name="Image Rect" value="48 0 64 16" />
+        <attribute name="Border" value="4 4 4 4" />
+        <element type="BorderImage" internal="true">
+            <attribute name="Image Rect" value="16 0 32 16" />
+            <attribute name="Border" value="4 4 4 4" />
+            <attribute name="Hover Image Offset" value="0 16" />
+        </element>
+    </element>
     <element type="Slider" style="BorderImage">
         <attribute name="Size" value="16 16" />
         <attribute name="Image Rect" value="48 0 64 16" />

+ 4 - 0
bin/Data/UI/EditorIcons.xml

@@ -271,6 +271,10 @@
         <attribute name="Texture" value="Texture2D;Textures/Editor/EditorIcons.png" />
         <attribute name="Image Rect" value="240 32 254 46" />
     </element>
+    <element type="ProgressBar">
+        <attribute name="Texture" value="Texture2D;Textures/Editor/EditorIcons.png" />
+        <attribute name="Image Rect" value="0 48 14 62" />
+    </element>
     <element type="Slider">
         <attribute name="Texture" value="Texture2D;Textures/Editor/EditorIcons.png" />
         <attribute name="Image Rect" value="112 32 126 46" />