01_HelloWorld.as 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // This first example, maintaining tradition, prints a "Hello World" message.
  2. // Furthermore it shows:
  3. // - Using the Sample utility functions as a base for the application
  4. // - Adding a Text element to the graphical user interface
  5. // - Subscribing to and handling of update events
  6. #include "Scripts/Utilities/Sample.as"
  7. void Start()
  8. {
  9. // Execute the common startup for samples
  10. SampleStart();
  11. // Create "Hello World" Text
  12. CreateText();
  13. // Finally, hook-up this HelloWorld instance to handle update events
  14. SubscribeToEvents();
  15. }
  16. void CreateText()
  17. {
  18. // Construct new Text object
  19. Text@ helloText = Text();
  20. // Set String to display
  21. helloText.text = "Hello World from Urho3D!";
  22. // Set font and text color
  23. helloText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 30);
  24. helloText.color = Color(0.0f, 1.0f, 0.0f);
  25. // Align Text center-screen
  26. helloText.horizontalAlignment = HA_CENTER;
  27. helloText.verticalAlignment = VA_CENTER;
  28. // Add Text instance to the UI root element
  29. ui.root.AddChild(helloText);
  30. }
  31. void SubscribeToEvents()
  32. {
  33. // Subscribe HandleUpdate() function for processing update events
  34. SubscribeToEvent("Update", "HandleUpdate");
  35. }
  36. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  37. {
  38. // Do nothing for now, could be extended to eg. animate the display
  39. }
  40. // Create XML patch instructions for screen joystick layout specific to this sample app
  41. String patchInstructions =
  42. "<patch>" +
  43. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  44. " <attribute name=\"Is Visible\" value=\"false\" />" +
  45. " </add>" +
  46. "</patch>";