01_HelloWorld.as 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // Set the mouse mode to use in the sample
  14. SampleInitMouseMode(MM_FREE);
  15. // Finally, hook-up this HelloWorld instance to handle update events
  16. SubscribeToEvents();
  17. }
  18. void CreateText()
  19. {
  20. // Construct new Text object
  21. Text@ helloText = Text();
  22. // Set String to display
  23. helloText.text = "Hello World from Urho3D!";
  24. // Set font and text color
  25. helloText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 30);
  26. helloText.color = Color(0.0f, 1.0f, 0.0f);
  27. // Align Text center-screen
  28. helloText.horizontalAlignment = HA_CENTER;
  29. helloText.verticalAlignment = VA_CENTER;
  30. // Add Text instance to the UI root element
  31. ui.root.AddChild(helloText);
  32. }
  33. void SubscribeToEvents()
  34. {
  35. // Subscribe HandleUpdate() function for processing update events
  36. SubscribeToEvent("Update", "HandleUpdate");
  37. }
  38. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  39. {
  40. // Do nothing for now, could be extended to eg. animate the display
  41. }
  42. // Create XML patch instructions for screen joystick layout specific to this sample app
  43. String patchInstructions =
  44. "<patch>" +
  45. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  46. " <attribute name=\"Is Visible\" value=\"false\" />" +
  47. " </add>" +
  48. "</patch>";