Browse Source

Minor code reorganization of the HelloGUI example. Removed SubscribeToEvents() as it was only used for 1 event and events were also subscribed to elsewhere.

Lasse Öörni 12 years ago
parent
commit
fd140ddb32

+ 36 - 43
Bin/Data/LuaScripts/02_HelloGUI.lua

@@ -17,6 +17,8 @@ local engine = GetEngine()
 local input = GetInput()
 local input = GetInput()
 local ui = GetUI()
 local ui = GetUI()
 
 
+local dragBeginPosition = IntVector2(0, 0)
+
 function Start()
 function Start()
     -- Execute the common startup for samples
     -- Execute the common startup for samples
     SampleStart()
     SampleStart()
@@ -38,8 +40,6 @@ function Start()
 
 
     -- Create a draggable Fish
     -- Create a draggable Fish
     CreateDraggableFish()
     CreateDraggableFish()
-
-    SubscribeToEvents()
 end
 end
 
 
 function InitControls()
 function InitControls()
@@ -72,13 +72,13 @@ function InitWindow()
     -- Create the Window and add it to the UI's root node
     -- Create the Window and add it to the UI's root node
     window = Window:new()
     window = Window:new()
     ui.root:AddChild(window)
     ui.root:AddChild(window)
-    
+
     -- Set Window size and layout settings
     -- Set Window size and layout settings
     window:SetMinSize(384, 192)
     window:SetMinSize(384, 192)
     window:SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6))
     window:SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6))
     window:SetAlignment(HA_CENTER, VA_CENTER)
     window:SetAlignment(HA_CENTER, VA_CENTER)
     window:SetName("Window")
     window:SetName("Window")
-    
+
     -- Create Window 'titlebar' container
     -- Create Window 'titlebar' container
     local titleBar = UIElement:new()
     local titleBar = UIElement:new()
     titleBar:SetMinSize(0, 24)
     titleBar:SetMinSize(0, 24)
@@ -89,8 +89,8 @@ function InitWindow()
     local windowTitle = Text:new()
     local windowTitle = Text:new()
     windowTitle.name = "WindowTitle"
     windowTitle.name = "WindowTitle"
     windowTitle.text = "Hello GUI!"
     windowTitle.text = "Hello GUI!"
-    
-    
+
+
     -- Create the Window's close button
     -- Create the Window's close button
     local buttonClose = Button:new()
     local buttonClose = Button:new()
     buttonClose:SetName("CloseButton")
     buttonClose:SetName("CloseButton")
@@ -101,50 +101,20 @@ function InitWindow()
 
 
     -- Add the title bar to the Window
     -- Add the title bar to the Window
     window:AddChild(titleBar)
     window:AddChild(titleBar)
-    
-    
+
+
     -- Apply styles
     -- Apply styles
     window:SetStyleAuto()
     window:SetStyleAuto()
     windowTitle:SetStyleAuto()
     windowTitle:SetStyleAuto()
     buttonClose:SetStyle("CloseButton")
     buttonClose:SetStyle("CloseButton")
-    
-    -- Lastly, subscribe to buttonClose release (following a 'press') events
-    SubscribeToEvent(buttonClose, "Released", "HandleClosePressed")
-end
 
 
-function SubscribeToEvents()
-    -- Subscribe handler invoked whenever a mouse click event is dispatched
-    SubscribeToEvent("UIMouseClick", "HandleControlClicked")
-end
-
-function HandleClosePressed(eventType, eventData)
-    engine:Exit()
-end
-
-function HandleControlClicked(eventType, eventData)
-    -- Get the Text control acting as the Window's title
-    local element = window:GetChild("WindowTitle", true)
-    local windowTitle = tolua.cast(element, 'Text')
+    -- Subscribe to buttonClose release (following a 'press') events
+    SubscribeToEvent(buttonClose, "Released", "HandleClosePressed")
     
     
-    -- Get control that was clicked
-    -- Note difference to C++: in C++ we would call GetPtr() and cast the function pointer to UIElement, here we must specify
-    -- what kind of object we are getting. Null will be returned on type mismatch
-    local clicked = eventData:GetPtr("UIElement", "Element")
-    local name = "...?"
-    if clicked ~= nil then
-        -- Get the name of the control that was clicked
-        name = clicked.name
-    end
-
-    -- Update the Window's title text
-    windowTitle.text = "Hello " .. name .. "!"
+    -- Subscribe also to all UI mouse clicks just to see where we have clicked
+    SubscribeToEvent("UIMouseClick", "HandleControlClicked")
 end
 end
 
 
-
------------------------------------------------------  Dragging / Tooltips  ----------------------------------------------
-
-local dragBeginPosition = IntVector2(0, 0)
-
 function CreateDraggableFish()
 function CreateDraggableFish()
     -- Create a draggable Fish button
     -- Create a draggable Fish button
     local draggableFish = ui.root:CreateChild("Button", "Fish")
     local draggableFish = ui.root:CreateChild("Button", "Fish")
@@ -181,4 +151,27 @@ function HandleDragMove(eventType, eventData)
 end
 end
 
 
 function HandleDragEnd(eventType, eventData) -- For reference (not used here)
 function HandleDragEnd(eventType, eventData) -- For reference (not used here)
-end
+end
+
+function HandleClosePressed(eventType, eventData)
+    engine:Exit()
+end
+
+function HandleControlClicked(eventType, eventData)
+    -- Get the Text control acting as the Window's title
+    local element = window:GetChild("WindowTitle", true)
+    local windowTitle = tolua.cast(element, 'Text')
+
+    -- Get control that was clicked
+    -- Note difference to C++: in C++ we would call GetPtr() and cast the function pointer to UIElement, here we must specify
+    -- what kind of object we are getting. Null will be returned on type mismatch
+    local clicked = eventData:GetPtr("UIElement", "Element")
+    local name = "...?"
+    if clicked ~= nil then
+        -- Get the name of the control that was clicked
+        name = clicked.name
+    end
+
+    -- Update the Window's title text
+    windowTitle.text = "Hello " .. name .. "!"
+end

+ 32 - 41
Bin/Data/Scripts/02_HelloGUI.as

@@ -11,6 +11,7 @@
 #include "Scripts/Utilities/Sample.as"
 #include "Scripts/Utilities/Sample.as"
 
 
 Window@ window;
 Window@ window;
+IntVector2 dragBeginPosition = IntVector2(0, 0);
 
 
 void Start()
 void Start()
 {
 {
@@ -29,13 +30,11 @@ 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();
-
-    SubscribeToEvents();
+    
+    // Create a draggable Fish
+    CreateDraggableFish();
 }
 }
 
 
 void InitControls()
 void InitControls()
@@ -104,47 +103,13 @@ void InitWindow()
     windowTitle.SetStyleAuto();
     windowTitle.SetStyleAuto();
     buttonClose.style = "CloseButton";
     buttonClose.style = "CloseButton";
 
 
-    // Lastly, subscribe to buttonClose release (following a 'press') events
+    // Subscribe to buttonClose release (following a 'press') events
     SubscribeToEvent(buttonClose, "Released", "HandleClosePressed");
     SubscribeToEvent(buttonClose, "Released", "HandleClosePressed");
-}
 
 
-void SubscribeToEvents()
-{
-    // Subscribe handler; invoked whenever a mouse click event is dispatched
+    // Subscribe also to all UI mouse clicks just to see where we have clicked
     SubscribeToEvent("UIMouseClick", "HandleControlClicked");
     SubscribeToEvent("UIMouseClick", "HandleControlClicked");
 }
 }
 
 
-void HandleClosePressed(StringHash eventType, VariantMap& eventData)
-{
-    engine.Exit();
-}
-
-void HandleControlClicked(StringHash eventType, VariantMap& eventData)
-{
-    // Get the Text control acting as the Window's title
-    Text@ windowTitle = window.GetChild("WindowTitle", true);
-
-    // Get control that was clicked
-    // Note difference to C++: in C++ we would call GetPtr() and cast the void pointer to UIElement, here we must specify
-    // what kind of object we are getting. Null will be returned on type mismatch
-    UIElement@ clicked = eventData["Element"].GetUIElement();
-
-    String name = "...?";
-    if (clicked !is null)
-    {
-        // Get the name of the control that was clicked
-        name = clicked.name;
-    }
-
-    // Update the Window's title text
-    windowTitle.text = "Hello " + name + "!";
-}
-
-
-//-----------------------------------------------------  Draging / Tooltips  ----------------------------------------------
-
-IntVector2 dragBeginPosition = IntVector2(0, 0);
-
 void CreateDraggableFish()
 void CreateDraggableFish()
 {
 {
     // Create a draggable Fish button
     // Create a draggable Fish button
@@ -186,3 +151,29 @@ void HandleDragMove(StringHash eventType, VariantMap& eventData)
 void HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
 void HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
 {
 {
 }
 }
+
+void HandleClosePressed(StringHash eventType, VariantMap& eventData)
+{
+    engine.Exit();
+}
+
+void HandleControlClicked(StringHash eventType, VariantMap& eventData)
+{
+    // Get the Text control acting as the Window's title
+    Text@ windowTitle = window.GetChild("WindowTitle", true);
+
+    // Get control that was clicked
+    // Note difference to C++: in C++ we would call GetPtr() and cast the void pointer to UIElement, here we must specify
+    // what kind of object we are getting. Null will be returned on type mismatch
+    UIElement@ clicked = eventData["Element"].GetUIElement();
+
+    String name = "...?";
+    if (clicked !is null)
+    {
+        // Get the name of the control that was clicked
+        name = clicked.name;
+    }
+
+    // Update the Window's title text
+    windowTitle.text = "Hello " + name + "!";
+}

+ 34 - 44
Source/Samples/02_HelloGUI/HelloGUI.cpp

@@ -46,7 +46,7 @@ DEFINE_APPLICATION_MAIN(HelloGUI)
 HelloGUI::HelloGUI(Context* context) :
 HelloGUI::HelloGUI(Context* context) :
     Sample(context),
     Sample(context),
     uiRoot_(GetSubsystem<UI>()->GetRoot()),
     uiRoot_(GetSubsystem<UI>()->GetRoot()),
-    window_()
+    dragBeginPosition_(IntVector2::ZERO)
 {
 {
 }
 }
 
 
@@ -67,14 +67,12 @@ 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();
 
 
-    SubscribeToEvents();
+    // Create a draggable Fish
+    CreateDraggableFish();
 }
 }
 
 
 void HelloGUI::InitControls()
 void HelloGUI::InitControls()
@@ -143,45 +141,13 @@ void HelloGUI::InitWindow()
     windowTitle->SetStyleAuto();
     windowTitle->SetStyleAuto();
     buttonClose->SetStyle("CloseButton");
     buttonClose->SetStyle("CloseButton");
 
 
-    // Lastly, subscribe to buttonClose release (following a 'press') events
+    // Subscribe to buttonClose release (following a 'press') events
     SubscribeToEvent(buttonClose, E_RELEASED, HANDLER(HelloGUI, HandleClosePressed));
     SubscribeToEvent(buttonClose, E_RELEASED, HANDLER(HelloGUI, HandleClosePressed));
-}
-
-void HelloGUI::SubscribeToEvents()
-{
-    // Subscribe handler; invoked whenever a mouse click event is dispatched
+    
+    // Subscribe also to all UI mouse clicks just to see where we have clicked
     SubscribeToEvent(E_UIMOUSECLICK, HANDLER(HelloGUI, HandleControlClicked));
     SubscribeToEvent(E_UIMOUSECLICK, HANDLER(HelloGUI, HandleControlClicked));
 }
 }
 
 
-void HelloGUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
-{
-    GetSubsystem<Engine>()->Exit();
-}
-
-void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
-{
-    // Get the Text control acting as the Window's title
-    SharedPtr<Text> windowTitle((Text*)window_->GetChild("WindowTitle", true));
-
-    // Get control that was clicked
-    UIElement* clicked = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
-
-    String name = "...?";
-    if (clicked)
-    {
-        // Get the name of the control that was clicked
-        name = clicked->GetName();
-    }
-
-    // Update the Window's title text
-    windowTitle->SetText("Hello " + name + "!");
-}
-
-
-//-----------------------------------------------------  Dragging / Tooltips  ----------------------------------------------
-
-IntVector2 dragBeginPosition = IntVector2(0, 0);
-
 void HelloGUI::CreateDraggableFish()
 void HelloGUI::CreateDraggableFish()
 {
 {
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     ResourceCache* cache = GetSubsystem<ResourceCache>();
@@ -218,16 +184,40 @@ void HelloGUI::CreateDraggableFish()
 void HelloGUI::HandleDragBegin(StringHash eventType, VariantMap& eventData)
 void HelloGUI::HandleDragBegin(StringHash eventType, VariantMap& eventData)
 {
 {
     // Get UIElement relative position where input (touch or click) occured (top-left = IntVector2(0,0))
     // Get UIElement relative position where input (touch or click) occured (top-left = IntVector2(0,0))
-    dragBeginPosition = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
+    dragBeginPosition_ = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
 }
 }
 
 
 void HelloGUI::HandleDragMove(StringHash eventType, VariantMap& eventData)
 void HelloGUI::HandleDragMove(StringHash eventType, VariantMap& eventData)
 {
 {
     IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
     IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
     UIElement* draggedElement = static_cast<UIElement*>(eventData["Element"].GetPtr());
     UIElement* draggedElement = static_cast<UIElement*>(eventData["Element"].GetPtr());
-    draggedElement->SetPosition(dragCurrentPosition - dragBeginPosition);
+    draggedElement->SetPosition(dragCurrentPosition - dragBeginPosition_);
 }
 }
 
 
 void HelloGUI::HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
 void HelloGUI::HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
 {
 {
-}
+}
+
+void HelloGUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
+{
+    GetSubsystem<Engine>()->Exit();
+}
+
+void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
+{
+    // Get the Text control acting as the Window's title
+    SharedPtr<Text> windowTitle((Text*)window_->GetChild("WindowTitle", true));
+
+    // Get control that was clicked
+    UIElement* clicked = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
+
+    String name = "...?";
+    if (clicked)
+    {
+        // Get the name of the control that was clicked
+        name = clicked->GetName();
+    }
+
+    // Update the Window's title text
+    windowTitle->SetText("Hello " + name + "!");
+}

+ 11 - 11
Source/Samples/02_HelloGUI/HelloGUI.h

@@ -56,25 +56,25 @@ private:
     void InitWindow();
     void InitWindow();
     /// Create and add various common controls for demonstration purposes.
     /// Create and add various common controls for demonstration purposes.
     void InitControls();
     void InitControls();
-    /// Subscribe to UI events.
-    void SubscribeToEvents();
-    /// Handle any UI control being clicked.
-    void HandleControlClicked(StringHash eventType, VariantMap& eventData);
-    /// Handle close button pressed and released.
-    void HandleClosePressed(StringHash eventType, VariantMap& eventData);
-    /// Create a draggable fish button
+    /// Create a draggable fish button.
     void CreateDraggableFish();
     void CreateDraggableFish();
-    /// Handle drag begin
+    /// Handle drag begin for the fish button.
     void HandleDragBegin(StringHash eventType, VariantMap& eventData);
     void HandleDragBegin(StringHash eventType, VariantMap& eventData);
-    /// Handle drag move
+    /// Handle drag move for the fish button.
     void HandleDragMove(StringHash eventType, VariantMap& eventData);
     void HandleDragMove(StringHash eventType, VariantMap& eventData);
-    /// Handle drag end
+    /// Handle drag end for the fish button.
     void HandleDragEnd(StringHash eventType, VariantMap& eventData);
     void HandleDragEnd(StringHash eventType, VariantMap& eventData);
-
+    /// Handle any UI control being clicked.
+    void HandleControlClicked(StringHash eventType, VariantMap& eventData);
+    /// Handle close button pressed and released.
+    void HandleClosePressed(StringHash eventType, VariantMap& eventData);
+    
     /// The Window.
     /// The Window.
     SharedPtr<Window> window_;
     SharedPtr<Window> window_;
     /// The UI's root UIElement.
     /// The UI's root UIElement.
     SharedPtr<UIElement> uiRoot_;
     SharedPtr<UIElement> uiRoot_;
+    /// Remembered drag begin position.
+    IntVector2 dragBeginPosition_;
 };
 };