Jelajahi Sumber

Ported HelloGUI sample to script.

Lasse Öörni 12 tahun lalu
induk
melakukan
10abc4414b
2 mengubah file dengan 141 tambahan dan 7 penghapusan
  1. 134 0
      Bin/Data/Scripts/02_HelloGUI.as
  2. 7 7
      Source/Samples/02_HelloGUI/HelloGUI.cpp

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

@@ -0,0 +1,134 @@
+// A simple 'HelloWorld' GUI created purely from code.
+// This sample demonstrates:
+//     - Creation of controls and building a UI hierarchy;
+//     - Loading UI style from XML and applying it to controls;
+//     - Handling of global and per-control events;
+
+#include "Utilities/Sample.as"
+
+Window@ window;
+
+void Start()
+{
+    // Execute the common startup for samples
+    SampleStart();
+
+    // Enable OS cursor
+    input.mouseVisible = true;
+
+    // Load XML file containing default UI style sheet
+    XMLFile@ style = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
+
+    // Set the loaded style as default style
+    ui.root.defaultStyle = style;
+
+    // Initialize Window
+    InitWindow();
+
+    // Create and add some controls to the Window
+    InitControls();
+
+    SubscribeToEvents();
+}
+
+void InitControls()
+{
+    // Create a CheckBox
+    CheckBox@ checkBox = CheckBox();
+    checkBox.name = "CheckBox";
+
+    // Create a Button
+    Button@ button = Button();
+    button.name = "Button";
+    button.minHeight = 24;
+
+    // Create a LineEdit
+    LineEdit@ lineEdit = LineEdit();
+    lineEdit.name = "LineEdit";
+    lineEdit.minHeight = 24;
+
+    // Add controls to Window
+    window.AddChild(checkBox);
+    window.AddChild(button);
+    window.AddChild(lineEdit);
+
+    // Apply previously set default style
+    checkBox.SetStyleAuto();
+    button.SetStyleAuto();
+    lineEdit.SetStyleAuto();
+}
+
+void InitWindow()
+{
+    // Create the Window and add it to the UI's root node
+    window = Window();
+    ui.root.AddChild(window);
+
+    // Set Window size and layout settings
+    window.SetMinSize(384, 192);
+    window.SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
+    window.SetAlignment(HA_CENTER, VA_CENTER);
+    window.name = "Window";
+
+    // Create Window 'titlebar' container
+    UIElement@ titleBar = UIElement();
+    titleBar.SetMinSize(0, 24);
+    titleBar.verticalAlignment = VA_TOP;
+    titleBar.layoutMode = LM_HORIZONTAL;
+
+    // Create the Window title Text
+    Text@ windowTitle = Text();
+    windowTitle.name = "WindowTitle";
+    windowTitle.text = "Hello GUI!";
+
+    // Create the Window's close button
+    Button@ buttonClose = Button();
+    buttonClose.name = "CloseButton";
+
+    // Add the controls to the title bar
+    titleBar.AddChild(windowTitle);
+    titleBar.AddChild(buttonClose);
+
+    // Add the title bar to the Window
+    window.AddChild(titleBar);
+
+    // Apply styles
+    window.SetStyleAuto();
+    windowTitle.SetStyleAuto();
+    buttonClose.style = "CloseButton";
+
+    // Lastly, subscribe to buttonClose release (following a 'press') events
+    SubscribeToEvent(buttonClose, "Released", "HandleClosePressed");
+}
+
+void SubscribeToEvents()
+{
+    // Subscribe handler; invoked whenever a mouse click event is dispatched
+    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 + "!";
+}

+ 7 - 7
Source/Samples/02_HelloGUI/HelloGUI.cpp

@@ -57,13 +57,13 @@ void HelloGUI::Start()
     SharedPtr<XMLFile> style(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
 
     // Set the loaded style as default style
-    this->uiRoot_->SetDefaultStyle(style);
+    uiRoot_->SetDefaultStyle(style);
 
     // Initialize Window
-    this->InitWindow();
+    InitWindow();
 
     // Create and add some controls to the Window
-    this->InitControls();
+    InitControls();
 
     SubscribeToEvents();
 }
@@ -98,8 +98,8 @@ void HelloGUI::InitControls()
 void HelloGUI::InitWindow()
 {
     // Create the Window and add it to the UI's root node
-    this->window_ = SharedPtr<Window>(new Window(context_));
-    this->uiRoot_->AddChild(window_);
+    window_ = SharedPtr<Window>(new Window(context_));
+    uiRoot_->AddChild(window_);
 
     // Set Window size and layout settings
     window_->SetMinSize(384, 192);
@@ -152,7 +152,7 @@ void HelloGUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
 void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
 {
     // Get the Text control acting as the Window's title
-    SharedPtr<Text> windowTitle((Text*)window_->GetChild(String("WindowTitle"), true));
+    SharedPtr<Text> windowTitle((Text*)window_->GetChild("WindowTitle", true));
 
     // Get control that was clicked
     UIElement* clicked = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
@@ -165,5 +165,5 @@ void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
     }
 
     // Update the Window's title text
-    windowTitle->SetText(String("Hello " + name + "!"));
+    windowTitle->SetText("Hello " + name + "!");
 }