ソースを参照

Merge remote-tracking branch 'remotes/mike3d/master'

Lasse Öörni 12 年 前
コミット
e0166ce900

+ 55 - 0
Bin/Data/LuaScripts/02_HelloGUI.lua

@@ -3,11 +3,20 @@
 --     - Creation of controls and building a UI hierarchy
 --     - Creation of controls and building a UI hierarchy
 --     - Loading UI style from XML and applying it to controls
 --     - Loading UI style from XML and applying it to controls
 --     - Handling of global and per-control events
 --     - Handling of global and per-control events
+-- For more advanced users (beginners can skip this section):
+--     - Dragging UIElements
+--     - Displaying tooltips
+--     - Accessing available Events data (eventData)
 
 
 require "LuaScripts/Utilities/Sample"
 require "LuaScripts/Utilities/Sample"
 
 
 local window = nil
 local window = nil
 
 
+local cache = GetCache()
+local engine = GetEngine()
+local input = GetInput()
+local ui = GetUI()
+
 function Start()
 function Start()
     -- Execute the common startup for samples
     -- Execute the common startup for samples
     SampleStart()
     SampleStart()
@@ -27,6 +36,9 @@ function Start()
     -- Create and add some controls to the Window
     -- Create and add some controls to the Window
     InitControls()
     InitControls()
 
 
+    -- Create a draggable Fish
+    CreateDraggableFish()
+
     SubscribeToEvents()
     SubscribeToEvents()
 end
 end
 
 
@@ -127,3 +139,46 @@ function HandleControlClicked(eventType, eventData)
     -- Update the Window's title text
     -- Update the Window's title text
     windowTitle.text = "Hello " .. name .. "!"
     windowTitle.text = "Hello " .. name .. "!"
 end
 end
+
+
+-----------------------------------------------------  Dragging / Tooltips  ----------------------------------------------
+
+local dragBeginPosition = IntVector2(0, 0)
+
+function CreateDraggableFish()
+    -- Create a draggable Fish button
+    local draggableFish = ui.root:CreateChild("Button", "Fish")
+    draggableFish.texture = cache:GetResource("Texture2D", "Textures/UrhoDecal.dds") -- Set texture
+    draggableFish.blendMode = BLEND_ADD
+    draggableFish:SetSize(128, 128)
+    draggableFish:SetPosition((GetGraphics().width - draggableFish.width) / 2, 200)
+
+    -- Add a tooltip to Fish button
+    local toolTip = draggableFish:CreateChild("ToolTip")
+    toolTip.position = IntVector2(draggableFish.width + 5, draggableFish.width/2) -- Slightly offset from fish
+    local textHolder = toolTip:CreateChild("BorderImage")
+    textHolder:SetStyle("ToolTipBorderImage")
+    local toolTipText = textHolder:CreateChild("Text")
+    toolTipText:SetStyle("ToolTipText")
+    toolTipText.text = "Please drag me!"
+
+    -- Subscribe draggableFish to Drag Events (in order to make it draggable)
+    -- See "Event list" in documentation's Main Page for reference on available Events and their eventData
+    SubscribeToEvent(draggableFish, "DragBegin", "HandleDragBegin")
+    SubscribeToEvent(draggableFish, "DragMove", "HandleDragMove")
+    SubscribeToEvent(draggableFish, "DragEnd", "HandleDragEnd")
+end
+
+function HandleDragBegin(eventType, eventData)
+    -- Get UIElement relative position where input (touch or click) occured (top-left = IntVector2(0,0))
+    dragBeginPosition = IntVector2(eventData:GetInt("ElementX"), eventData:GetInt("ElementY"))
+end
+
+function HandleDragMove(eventType, eventData)
+    local dragCurrentPosition = IntVector2(eventData:GetInt("X"), eventData:GetInt("Y"))
+    local draggedElement = eventData:GetPtr("UIElement", "Element")
+    draggedElement:SetPosition(dragCurrentPosition - dragBeginPosition)
+end
+
+function HandleDragEnd(eventType, eventData) -- For reference (not used here)
+end

+ 54 - 0
Bin/Data/Scripts/02_HelloGUI.as

@@ -3,6 +3,10 @@
 //     - Creation of controls and building a UI hierarchy
 //     - Creation of controls and building a UI hierarchy
 //     - Loading UI style from XML and applying it to controls
 //     - Loading UI style from XML and applying it to controls
 //     - Handling of global and per-control events
 //     - Handling of global and per-control events
+// For more advanced users (beginners can skip this section):
+//     - Dragging UIElements
+//     - Displaying tooltips
+//     - Accessing available Events data (eventData)
 
 
 #include "Scripts/Utilities/Sample.as"
 #include "Scripts/Utilities/Sample.as"
 
 
@@ -25,6 +29,9 @@ void Start()
     // Initialize Window
     // Initialize Window
     InitWindow();
     InitWindow();
 
 
+    // Create a draggable Fish
+    CreateDraggableFish();
+
     // Create and add some controls to the Window
     // Create and add some controls to the Window
     InitControls();
     InitControls();
 
 
@@ -132,3 +139,50 @@ void HandleControlClicked(StringHash eventType, VariantMap& eventData)
     // Update the Window's title text
     // Update the Window's title text
     windowTitle.text = "Hello " + name + "!";
     windowTitle.text = "Hello " + name + "!";
 }
 }
+
+
+//-----------------------------------------------------  Draging / Tooltips  ----------------------------------------------
+
+IntVector2 dragBeginPosition = IntVector2(0, 0);
+
+void CreateDraggableFish()
+{
+    // Create a draggable Fish button
+    Button@ draggableFish = ui.root.CreateChild("Button", "Fish");
+    draggableFish.texture = cache.GetResource("Texture2D", "Textures/UrhoDecal.dds"); // Set texture
+    draggableFish.blendMode = BLEND_ADD;
+    draggableFish.SetSize(128, 128);
+    draggableFish.SetPosition((graphics.width - draggableFish.width) / 2, 200);
+
+    // Add a tooltip to Fish button
+    ToolTip@ toolTip = draggableFish.CreateChild("ToolTip");
+    toolTip.position = IntVector2(draggableFish.width + 5, draggableFish.width/2); // slightly offset from fish
+    BorderImage@ textHolder = toolTip.CreateChild("BorderImage");
+    textHolder.SetStyle("ToolTipBorderImage");
+    Text@ toolTipText = textHolder.CreateChild("Text");
+    toolTipText.SetStyle("ToolTipText");
+    toolTipText.text = "Please drag me!";
+
+    // Subscribe draggableFish to Drag Events (in order to make it draggable)
+    // See "Event list" in documentation's Main Page for reference on available Events and their eventData
+    SubscribeToEvent(draggableFish, "DragBegin", "HandleDragBegin");
+    SubscribeToEvent(draggableFish, "DragMove", "HandleDragMove");
+    SubscribeToEvent(draggableFish, "DragEnd", "HandleDragEnd");
+}
+
+void HandleDragBegin(StringHash eventType, VariantMap& eventData)
+{
+    // Get UIElement relative position where input (touch or click) occured (top-left = IntVector2(0,0))
+    dragBeginPosition = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
+}
+
+void HandleDragMove(StringHash eventType, VariantMap& eventData)
+{
+    IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
+    UIElement@ draggedElement = eventData["Element"].GetUIElement();
+    draggedElement.position = dragCurrentPosition - dragBeginPosition;
+}
+
+void HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
+{
+}

+ 1 - 1
Source/Engine/LuaScript/pkgs/Graphics/AnimatedModel.pkg

@@ -30,7 +30,7 @@ class AnimatedModel : public StaticModel
     float GetMorphWeight(StringHash nameHash) const;
     float GetMorphWeight(StringHash nameHash) const;
     float GetMorphWeight(unsigned index) const;
     float GetMorphWeight(unsigned index) const;
     bool IsMaster() const;
     bool IsMaster() const;
-    
+
     tolua_property__get_set Model* model;
     tolua_property__get_set Model* model;
     tolua_readonly tolua_property__get_set Skeleton& skeleton;
     tolua_readonly tolua_property__get_set Skeleton& skeleton;
     tolua_readonly tolua_property__get_set unsigned numAnimationStates;
     tolua_readonly tolua_property__get_set unsigned numAnimationStates;

+ 2 - 2
Source/Engine/LuaScript/pkgs/Graphics/Animation.pkg

@@ -36,11 +36,11 @@ class Animation : public Resource
     StringHash GetAnimationNameHash() const;
     StringHash GetAnimationNameHash() const;
     float GetLength() const;
     float GetLength() const;
     unsigned GetNumTracks() const;
     unsigned GetNumTracks() const;
-    const AnimationTrack* GetTrack(unsigned index) const;
     const AnimationTrack* GetTrack(const String name) const;
     const AnimationTrack* GetTrack(const String name) const;
     const AnimationTrack* GetTrack(StringHash nameHash) const;
     const AnimationTrack* GetTrack(StringHash nameHash) const;
+    const AnimationTrack* GetTrack(unsigned index) const;
     unsigned GetNumTriggers() const;
     unsigned GetNumTriggers() const;
-    
+
     tolua_readonly tolua_property__get_set String animationName;
     tolua_readonly tolua_property__get_set String animationName;
     tolua_readonly tolua_property__get_set StringHash animationNameHash;
     tolua_readonly tolua_property__get_set StringHash animationNameHash;
     tolua_readonly tolua_property__get_set float length;
     tolua_readonly tolua_property__get_set float length;

+ 2 - 2
Source/Engine/LuaScript/pkgs/Graphics/AnimationState.pkg

@@ -10,18 +10,18 @@ class AnimationState
     void SetLooped(bool looped);
     void SetLooped(bool looped);
     void SetWeight(float weight);
     void SetWeight(float weight);
     void SetTime(float time);
     void SetTime(float time);
-    void SetBoneWeight(unsigned index, float weight, bool recursive = false);
     void SetBoneWeight(const String name, float weight, bool recursive = false);
     void SetBoneWeight(const String name, float weight, bool recursive = false);
     void SetBoneWeight(StringHash nameHash, float weight, bool recursive = false);
     void SetBoneWeight(StringHash nameHash, float weight, bool recursive = false);
+    void SetBoneWeight(unsigned index, float weight, bool recursive = false);
     void AddWeight(float delta);
     void AddWeight(float delta);
     void AddTime(float delta);
     void AddTime(float delta);
     void SetLayer(unsigned char layer);
     void SetLayer(unsigned char layer);
 
 
     Animation* GetAnimation() const;
     Animation* GetAnimation() const;
     Bone* GetStartBone() const;
     Bone* GetStartBone() const;
-    float GetBoneWeight(unsigned index) const;
     float GetBoneWeight(const String name) const;
     float GetBoneWeight(const String name) const;
     float GetBoneWeight(StringHash nameHash) const;
     float GetBoneWeight(StringHash nameHash) const;
+    float GetBoneWeight(unsigned index) const;
     unsigned GetTrackIndex(const String name) const;
     unsigned GetTrackIndex(const String name) const;
     unsigned GetTrackIndex(StringHash nameHash) const;
     unsigned GetTrackIndex(StringHash nameHash) const;
     bool IsEnabled() const;
     bool IsEnabled() const;

+ 1 - 1
Source/Engine/LuaScript/pkgs/Graphics/Model.pkg

@@ -8,9 +8,9 @@ class Model : public Resource
     unsigned GetNumGeometryLodLevels(unsigned index) const;
     unsigned GetNumGeometryLodLevels(unsigned index) const;
     Geometry* GetGeometry(unsigned index, unsigned lodLevel) const;
     Geometry* GetGeometry(unsigned index, unsigned lodLevel) const;
     unsigned GetNumMorphs() const;
     unsigned GetNumMorphs() const;
-    const ModelMorph* GetMorph(unsigned index) const;
     const ModelMorph* GetMorph(const String name) const;
     const ModelMorph* GetMorph(const String name) const;
     const ModelMorph* GetMorph(StringHash nameHash) const;
     const ModelMorph* GetMorph(StringHash nameHash) const;
+    const ModelMorph* GetMorph(unsigned index) const;
     unsigned GetMorphRangeStart(unsigned bufferIndex) const;
     unsigned GetMorphRangeStart(unsigned bufferIndex) const;
     unsigned GetMorphRangeCount(unsigned bufferIndex) const;
     unsigned GetMorphRangeCount(unsigned bufferIndex) const;
     
     

+ 1 - 1
Source/Engine/LuaScript/pkgs/Graphics/RenderPath.pkg

@@ -12,9 +12,9 @@ class RenderPath
     void ToggleEnabled(const String tag);
     void ToggleEnabled(const String tag);
     void SetRenderTarget(unsigned index, const RenderTargetInfo& info);
     void SetRenderTarget(unsigned index, const RenderTargetInfo& info);
     void AddRenderTarget(const RenderTargetInfo& info);
     void AddRenderTarget(const RenderTargetInfo& info);
-    void RemoveRenderTarget(unsigned index);
 
 
     void RemoveRenderTarget(const String name);
     void RemoveRenderTarget(const String name);
+    void RemoveRenderTarget(unsigned index);
     void RemoveRenderTargets(const String tag);
     void RemoveRenderTargets(const String tag);
     
     
     void SetCommand(unsigned index, const RenderPathCommand& command);
     void SetCommand(unsigned index, const RenderPathCommand& command);

+ 2 - 2
Source/Engine/LuaScript/pkgs/Scene/Node.pkg

@@ -159,11 +159,11 @@ class Node : public Serializable
     Vector3 WorldToLocal(const Vector3& position) const;
     Vector3 WorldToLocal(const Vector3& position) const;
     Vector3 WorldToLocal(const Vector4& vector) const;
     Vector3 WorldToLocal(const Vector4& vector) const;
     bool IsDirty() const;
     bool IsDirty() const;
-    
+
     unsigned GetNumChildren(bool recursive = false) const;
     unsigned GetNumChildren(bool recursive = false) const;
-    Node* GetChild(unsigned index) const;
     Node* GetChild(const String name, bool recursive = false) const;
     Node* GetChild(const String name, bool recursive = false) const;
     Node* GetChild(StringHash nameHash, bool recursive = false) const;
     Node* GetChild(StringHash nameHash, bool recursive = false) const;
+    Node* GetChild(unsigned index) const;
 
 
     unsigned GetNumComponents() const;
     unsigned GetNumComponents() const;
     unsigned GetNumNetworkComponents() const;
     unsigned GetNumNetworkComponents() const;

+ 10 - 10
Source/Engine/LuaScript/pkgs/UI/UIElement.pkg

@@ -71,7 +71,7 @@ class UIElement : public Serializable
     bool FilterAttributes(XMLElement& dest) const;
     bool FilterAttributes(XMLElement& dest) const;
 
 
     void SetName(const String name);
     void SetName(const String name);
-    
+
     void SetPosition(const IntVector2& position);
     void SetPosition(const IntVector2& position);
     void SetPosition(int x, int y);
     void SetPosition(int x, int y);
     void SetSize(const IntVector2& size);
     void SetSize(const IntVector2& size);
@@ -112,13 +112,13 @@ class UIElement : public Serializable
     void SetDragDropMode(unsigned mode);
     void SetDragDropMode(unsigned mode);
 
 
     bool SetStyle(const String styleName, XMLFile* file = 0);
     bool SetStyle(const String styleName, XMLFile* file = 0);
-    
+
     bool SetStyle(const XMLElement& element);
     bool SetStyle(const XMLElement& element);
     bool SetStyleAuto(XMLFile* file = 0);
     bool SetStyleAuto(XMLFile* file = 0);
     void SetDefaultStyle(XMLFile* style);
     void SetDefaultStyle(XMLFile* style);
-    
+
     void SetLayout(LayoutMode mode, int spacing = 0, const IntRect& border = IntRect::ZERO);
     void SetLayout(LayoutMode mode, int spacing = 0, const IntRect& border = IntRect::ZERO);
-    
+
     void SetLayoutMode(LayoutMode mode);
     void SetLayoutMode(LayoutMode mode);
     void SetLayoutSpacing(int spacing);
     void SetLayoutSpacing(int spacing);
     void SetLayoutBorder(const IntRect& border);
     void SetLayoutBorder(const IntRect& border);
@@ -128,9 +128,9 @@ class UIElement : public Serializable
     void DisableLayoutUpdate();
     void DisableLayoutUpdate();
     void EnableLayoutUpdate();
     void EnableLayoutUpdate();
     void BringToFront();
     void BringToFront();
-    
+
     UIElement* CreateChild(const String type, const String name = String::EMPTY, unsigned index = M_MAX_UNSIGNED);
     UIElement* CreateChild(const String type, const String name = String::EMPTY, unsigned index = M_MAX_UNSIGNED);
-    
+
     void AddChild(UIElement* element);
     void AddChild(UIElement* element);
     void InsertChild(unsigned index, UIElement* element);
     void InsertChild(unsigned index, UIElement* element);
     void RemoveChild(UIElement* element, unsigned index = 0);
     void RemoveChild(UIElement* element, unsigned index = 0);
@@ -187,10 +187,10 @@ class UIElement : public Serializable
     int GetLayoutSpacing() const;
     int GetLayoutSpacing() const;
     const IntRect& GetLayoutBorder() const;
     const IntRect& GetLayoutBorder() const;
     unsigned GetNumChildren(bool recursive = false) const;
     unsigned GetNumChildren(bool recursive = false) const;
-    
-    UIElement* GetChild(unsigned index) const;
+
     UIElement* GetChild(const String name, bool recursive = false) const;
     UIElement* GetChild(const String name, bool recursive = false) const;
-    
+    UIElement* GetChild(unsigned index) const;
+
     UIElement* GetParent() const;
     UIElement* GetParent() const;
     UIElement* GetRoot() const;
     UIElement* GetRoot() const;
     const Color& GetDerivedColor() const;
     const Color& GetDerivedColor() const;
@@ -213,7 +213,7 @@ class UIElement : public Serializable
     TraversalMode GetTraversalMode() const;
     TraversalMode GetTraversalMode() const;
     bool IsElementEventSender() const;
     bool IsElementEventSender() const;
     UIElement* GetElementEventSender() const;
     UIElement* GetElementEventSender() const;
-    
+
     tolua_readonly tolua_property__get_set IntVector2& screenPosition;
     tolua_readonly tolua_property__get_set IntVector2& screenPosition;
     tolua_property__get_set String name;
     tolua_property__get_set String name;
     tolua_property__get_set IntVector2& position;
     tolua_property__get_set IntVector2& position;

+ 64 - 0
Source/Samples/02_HelloGUI/HelloGUI.cpp

@@ -21,13 +21,19 @@
 //
 //
 
 
 #include "Button.h"
 #include "Button.h"
+#include "BorderImage.h"
 #include "CheckBox.h"
 #include "CheckBox.h"
 #include "CoreEvents.h"
 #include "CoreEvents.h"
 #include "Engine.h"
 #include "Engine.h"
+#include "Graphics.h"
 #include "Input.h"
 #include "Input.h"
 #include "LineEdit.h"
 #include "LineEdit.h"
+#include "ResourceCache.h"
 #include "Text.h"
 #include "Text.h"
+#include "Texture2D.h"
+#include "ToolTip.h"
 #include "UI.h"
 #include "UI.h"
+#include "UIElement.h"
 #include "UIEvents.h"
 #include "UIEvents.h"
 #include "Window.h"
 #include "Window.h"
 
 
@@ -62,6 +68,9 @@ void HelloGUI::Start()
     // Initialize Window
     // Initialize Window
     InitWindow();
     InitWindow();
 
 
+    // Create a draggable Fish
+    CreateDraggableFish();
+
     // Create and add some controls to the Window
     // Create and add some controls to the Window
     InitControls();
     InitControls();
 
 
@@ -167,3 +176,58 @@ void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
     // Update the Window's title text
     // Update the Window's title text
     windowTitle->SetText("Hello " + name + "!");
     windowTitle->SetText("Hello " + name + "!");
 }
 }
+
+
+//-----------------------------------------------------  Dragging / Tooltips  ----------------------------------------------
+
+IntVector2 dragBeginPosition = IntVector2(0, 0);
+
+void HelloGUI::CreateDraggableFish()
+{
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    Graphics* graphics = GetSubsystem<Graphics>();
+
+    // Create a draggable Fish button
+    Button* draggableFish = new Button(context_);
+    draggableFish->SetTexture(cache->GetResource<Texture2D>("Textures/UrhoDecal.dds")); // Set texture
+    draggableFish->SetBlendMode(BLEND_ADD);
+    draggableFish->SetSize(128, 128);
+    draggableFish->SetPosition((graphics->GetWidth() - draggableFish->GetWidth()) / 2, 200);
+    draggableFish->SetName("Fish");
+    uiRoot_->AddChild(draggableFish);
+
+    // Add a tooltip to Fish button
+    ToolTip* toolTip = new ToolTip(context_);
+    draggableFish->AddChild(toolTip);
+    toolTip->SetPosition(IntVector2(draggableFish->GetWidth() + 5, draggableFish->GetWidth() / 2)); // slightly offset from close button
+    BorderImage* textHolder = new BorderImage(context_);
+    toolTip->AddChild(textHolder);
+    textHolder->SetStyle("ToolTipBorderImage");
+    Text* toolTipText = new Text(context_);
+    textHolder->AddChild(toolTipText);
+    toolTipText->SetStyle("ToolTipText");
+    toolTipText->SetText("Please drag me!");
+
+    // Subscribe draggableFish to Drag Events (in order to make it draggable)
+    // See "Event list" in documentation's Main Page for reference on available Events and their eventData
+    SubscribeToEvent(draggableFish, E_DRAGBEGIN, HANDLER(HelloGUI, HandleDragBegin));
+    SubscribeToEvent(draggableFish, E_DRAGMOVE, HANDLER(HelloGUI, HandleDragMove));
+    SubscribeToEvent(draggableFish, E_DRAGEND, HANDLER(HelloGUI, HandleDragEnd));
+}
+
+void HelloGUI::HandleDragBegin(StringHash eventType, VariantMap& eventData)
+{
+    // Get UIElement relative position where input (touch or click) occured (top-left = IntVector2(0,0))
+    dragBeginPosition = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
+}
+
+void HelloGUI::HandleDragMove(StringHash eventType, VariantMap& eventData)
+{
+    IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
+    UIElement* draggedElement = static_cast<UIElement*>(eventData["Element"].GetPtr());
+    draggedElement->SetPosition(dragCurrentPosition - dragBeginPosition);
+}
+
+void HelloGUI::HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
+{
+}

+ 12 - 0
Source/Samples/02_HelloGUI/HelloGUI.h

@@ -36,6 +36,10 @@ class Window;
 ///     - Creation of controls and building a UI hierarchy
 ///     - Creation of controls and building a UI hierarchy
 ///     - Loading UI style from XML and applying it to controls
 ///     - Loading UI style from XML and applying it to controls
 ///     - Handling of global and per-control events
 ///     - Handling of global and per-control events
+/// For more advanced users (beginners can skip this section):
+///     - Dragging UIElements
+///     - Displaying tooltips
+///     - Accessing available Events data (eventData)
 class HelloGUI : public Sample
 class HelloGUI : public Sample
 {
 {
     OBJECT(HelloGUI);
     OBJECT(HelloGUI);
@@ -58,6 +62,14 @@ private:
     void HandleControlClicked(StringHash eventType, VariantMap& eventData);
     void HandleControlClicked(StringHash eventType, VariantMap& eventData);
     /// Handle close button pressed and released.
     /// Handle close button pressed and released.
     void HandleClosePressed(StringHash eventType, VariantMap& eventData);
     void HandleClosePressed(StringHash eventType, VariantMap& eventData);
+    /// Create a draggable fish button
+    void CreateDraggableFish();
+    /// Handle drag begin
+    void HandleDragBegin(StringHash eventType, VariantMap& eventData);
+    /// Handle drag move
+    void HandleDragMove(StringHash eventType, VariantMap& eventData);
+    /// Handle drag end
+    void HandleDragEnd(StringHash eventType, VariantMap& eventData);
 
 
     /// The Window.
     /// The Window.
     SharedPtr<Window> window_;
     SharedPtr<Window> window_;