HelloWorld.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Core/CoreEvents.h>
  23. #include <Urho3D/Core/ProcessUtils.h>
  24. #include <Urho3D/Input/Input.h>
  25. #include <Urho3D/UI/Font.h>
  26. #include <Urho3D/UI/Text.h>
  27. #include <Urho3D/UI/UI.h>
  28. #include <Urho3D/IO/VectorBuffer.h> // TO BE REMOVED
  29. #include "HelloWorld.h"
  30. #include <Urho3D/DebugNew.h>
  31. // Expands to this example's entry-point
  32. URHO3D_DEFINE_APPLICATION_MAIN(HelloWorld)
  33. // TO BE REMOVED
  34. // @{
  35. static const char* enumNames[] =
  36. {
  37. "Enum1",
  38. "Enum2",
  39. "Enum3"
  40. };
  41. enum class TestEnum
  42. {
  43. Enum1,
  44. Enum2,
  45. Enum3,
  46. };
  47. class TestSerializable : public Serializable
  48. {
  49. URHO3D_OBJECT(TestSerializable, Serializable);
  50. public:
  51. TestSerializable(Context* context) : Serializable(context) { }
  52. static void RegisterObject(Context* context)
  53. {
  54. context->RegisterFactory<TestSerializable>();
  55. URHO3D_ATTRIBUTE("attribute", String, attribute_, "attribute", AM_DEFAULT);
  56. URHO3D_ATTRIBUTE_EX("attributeEx", String, attributeEx_, OnAttributeExSet, "attributeEx", AM_DEFAULT);
  57. URHO3D_ENUM_ATTRIBUTE("enumAttribute", enumAttribute_, enumNames, TestEnum::Enum2, AM_DEFAULT);
  58. URHO3D_ENUM_ATTRIBUTE_EX("enumAttributeEx", enumAttributeEx_, OnEnumAttributeExSet, enumNames, TestEnum::Enum2, AM_DEFAULT);
  59. URHO3D_ACCESSOR_ATTRIBUTE("accessorAttribute", GetAccessorAttribute, SetAccessorAttribute, String, "accessorAttribute", AM_DEFAULT);
  60. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("enumAccessorAttribute", GetEnumAccessorAttribute, SetEnumAccessorAttribute, TestEnum, enumNames, TestEnum::Enum3, AM_DEFAULT);
  61. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("mixedAccessorAttribute", GetMixedAccessorAttribute, SetMixedAccessorAttribute, String, "mixedAccessorAttribute", AM_DEFAULT);
  62. {
  63. String suffix = "_temp";
  64. auto getter = [=](const TestSerializable& self, Variant& value)
  65. {
  66. value = self.customAttribute_ + suffix;
  67. };
  68. auto setter = [=](TestSerializable& self, const Variant& value)
  69. {
  70. self.customAttribute_ = value.GetString() + suffix;
  71. };
  72. URHO3D_CUSTOM_ATTRIBUTE("customAttribute", getter, setter, String, "customAttribute", AM_DEFAULT);
  73. }
  74. {
  75. auto getter = [=](const TestSerializable& self, Variant& value)
  76. {
  77. value = static_cast<int>(self.customEnumAttribute_);
  78. };
  79. auto setter = [=](TestSerializable& self, const Variant& value)
  80. {
  81. self.customEnumAttribute_ = static_cast<TestEnum>(value.GetInt());
  82. };
  83. URHO3D_CUSTOM_ENUM_ATTRIBUTE("customAttribute", getter, setter, enumNames, TestEnum::Enum1, AM_DEFAULT);
  84. }
  85. }
  86. String attribute_;
  87. String attributeEx_;
  88. void OnAttributeExSet()
  89. {
  90. attributeEx_ = attributeEx_.ToUpper();
  91. }
  92. TestEnum enumAttribute_ = TestEnum::Enum1;
  93. TestEnum enumAttributeEx_ = TestEnum::Enum1;
  94. void OnEnumAttributeExSet()
  95. {
  96. enumAttributeEx_ = static_cast<TestEnum>(static_cast<int>(enumAttributeEx_) + 1);
  97. }
  98. String accessorAttribute_;
  99. const String& GetAccessorAttribute() const { return accessorAttribute_; }
  100. void SetAccessorAttribute(const String& value) { accessorAttribute_ = value; }
  101. TestEnum enumAccessorAttribute_ = TestEnum::Enum1;
  102. TestEnum GetEnumAccessorAttribute() const { return enumAccessorAttribute_; }
  103. void SetEnumAccessorAttribute(TestEnum value) { enumAccessorAttribute_ = value; }
  104. String mixedAccessorAttribute_;
  105. String GetMixedAccessorAttribute() const { return mixedAccessorAttribute_; }
  106. void SetMixedAccessorAttribute(const String& value) { mixedAccessorAttribute_ = value; }
  107. String customAttribute_;
  108. TestEnum customEnumAttribute_ = TestEnum::Enum1;
  109. };
  110. // @}
  111. HelloWorld::HelloWorld(Context* context) :
  112. Sample(context)
  113. {
  114. // TO BE REMOVED
  115. // @{
  116. TestSerializable::RegisterObject(context);
  117. auto obj = MakeShared<TestSerializable>(context_);
  118. obj->ResetToDefault();
  119. VectorBuffer buf;
  120. XMLFile xml(context_);
  121. obj->SaveXML(xml.CreateRoot("test"));
  122. xml.Save(buf);
  123. String text;
  124. text.Append(reinterpret_cast<const char*>(buf.GetData()), buf.GetSize());
  125. obj.Reset();
  126. // @}
  127. }
  128. void HelloWorld::Start()
  129. {
  130. // Execute base class startup
  131. Sample::Start();
  132. // Create "Hello World" Text
  133. CreateText();
  134. // Finally subscribe to the update event. Note that by subscribing events at this point we have already missed some events
  135. // like the ScreenMode event sent by the Graphics subsystem when opening the application window. To catch those as well we
  136. // could subscribe in the constructor instead.
  137. SubscribeToEvents();
  138. // Set the mouse mode to use in the sample
  139. Sample::InitMouseMode(MM_FREE);
  140. }
  141. void HelloWorld::CreateText()
  142. {
  143. ResourceCache* cache = GetSubsystem<ResourceCache>();
  144. // Construct new Text object
  145. SharedPtr<Text> helloText(new Text(context_));
  146. // Set String to display
  147. helloText->SetText("Hello World from Urho3D!");
  148. // Set font and text color
  149. helloText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
  150. helloText->SetColor(Color(0.0f, 1.0f, 0.0f));
  151. // Align Text center-screen
  152. helloText->SetHorizontalAlignment(HA_CENTER);
  153. helloText->SetVerticalAlignment(VA_CENTER);
  154. // Add Text instance to the UI root element
  155. GetSubsystem<UI>()->GetRoot()->AddChild(helloText);
  156. }
  157. void HelloWorld::SubscribeToEvents()
  158. {
  159. // Subscribe HandleUpdate() function for processing update events
  160. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HelloWorld, HandleUpdate));
  161. }
  162. void HelloWorld::HandleUpdate(StringHash eventType, VariantMap& eventData)
  163. {
  164. // Do nothing for now, could be extended to eg. animate the display
  165. }