HelloWorld.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. // TO BE REMOVED
  30. // @{
  31. #include <Urho3D/Core/Context.h>
  32. static const char* enumNames[] =
  33. {
  34. "Enum1",
  35. "Enum2",
  36. "Enum3"
  37. };
  38. enum class TestEnum
  39. {
  40. Enum1,
  41. Enum2,
  42. Enum3,
  43. };
  44. class TestSerializable : public Urho3D::Serializable
  45. {
  46. URHO3D_OBJECT(TestSerializable, Urho3D::Serializable);
  47. public:
  48. TestSerializable(Urho3D::Context* context) : Urho3D::Serializable(context) { }
  49. static void RegisterObject(Urho3D::Context* context)
  50. {
  51. context->RegisterFactory<TestSerializable>();
  52. URHO3D_ATTRIBUTE("attribute", Urho3D::String, attribute_, "attribute", Urho3D::AM_DEFAULT);
  53. URHO3D_ATTRIBUTE_EX("attributeEx", Urho3D::String, attributeEx_, OnAttributeExSet, "attributeEx", Urho3D::AM_DEFAULT);
  54. URHO3D_ENUM_ATTRIBUTE("enumAttribute", enumAttribute_, enumNames, TestEnum::Enum2, Urho3D::AM_DEFAULT);
  55. URHO3D_ENUM_ATTRIBUTE_EX("enumAttributeEx", enumAttributeEx_, OnEnumAttributeExSet, enumNames, TestEnum::Enum2, Urho3D::AM_DEFAULT);
  56. URHO3D_ACCESSOR_ATTRIBUTE("accessorAttribute", GetAccessorAttribute, SetAccessorAttribute, Urho3D::String, "accessorAttribute", Urho3D::AM_DEFAULT);
  57. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("enumAccessorAttribute", GetEnumAccessorAttribute, SetEnumAccessorAttribute, TestEnum, enumNames, TestEnum::Enum3, Urho3D::AM_DEFAULT);
  58. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("mixedAccessorAttribute", GetMixedAccessorAttribute, SetMixedAccessorAttribute, Urho3D::String, "mixedAccessorAttribute", Urho3D::AM_DEFAULT);
  59. {
  60. Urho3D::String suffix = "_temp";
  61. auto getter = [=](const TestSerializable& self, Urho3D::Variant& value)
  62. {
  63. value = self.customAttribute_ + suffix;
  64. };
  65. auto setter = [=](TestSerializable& self, const Urho3D::Variant& value)
  66. {
  67. self.customAttribute_ = value.GetString() + suffix;
  68. };
  69. URHO3D_CUSTOM_ATTRIBUTE("customAttribute", getter, setter, Urho3D::String, "customAttribute", Urho3D::AM_DEFAULT);
  70. }
  71. {
  72. auto getter = [=](const TestSerializable& self, Urho3D::Variant& value)
  73. {
  74. value = static_cast<int>(self.customEnumAttribute_);
  75. };
  76. auto setter = [=](TestSerializable& self, const Urho3D::Variant& value)
  77. {
  78. self.customEnumAttribute_ = static_cast<TestEnum>(value.GetInt());
  79. };
  80. URHO3D_CUSTOM_ENUM_ATTRIBUTE("customAttribute", getter, setter, enumNames, TestEnum::Enum1, Urho3D::AM_DEFAULT);
  81. }
  82. }
  83. Urho3D::String attribute_;
  84. Urho3D::String attributeEx_;
  85. void OnAttributeExSet()
  86. {
  87. attributeEx_ = attributeEx_.ToUpper();
  88. }
  89. TestEnum enumAttribute_ = TestEnum::Enum1;
  90. TestEnum enumAttributeEx_ = TestEnum::Enum1;
  91. void OnEnumAttributeExSet()
  92. {
  93. enumAttributeEx_ = static_cast<TestEnum>(static_cast<int>(enumAttributeEx_) + 1);
  94. }
  95. Urho3D::String accessorAttribute_;
  96. const Urho3D::String& GetAccessorAttribute() const { return accessorAttribute_; }
  97. void SetAccessorAttribute(const Urho3D::String& value) { accessorAttribute_ = value; }
  98. TestEnum enumAccessorAttribute_ = TestEnum::Enum1;
  99. TestEnum GetEnumAccessorAttribute() const { return enumAccessorAttribute_; }
  100. void SetEnumAccessorAttribute(TestEnum value) { enumAccessorAttribute_ = value; }
  101. Urho3D::String mixedAccessorAttribute_;
  102. Urho3D::String GetMixedAccessorAttribute() const { return mixedAccessorAttribute_; }
  103. void SetMixedAccessorAttribute(const Urho3D::String& value) { mixedAccessorAttribute_ = value; }
  104. Urho3D::String customAttribute_;
  105. TestEnum customEnumAttribute_ = TestEnum::Enum1;
  106. };
  107. // @}
  108. #include "HelloWorld.h"
  109. #include <Urho3D/DebugNew.h>
  110. // Expands to this example's entry-point
  111. URHO3D_DEFINE_APPLICATION_MAIN(HelloWorld)
  112. HelloWorld::HelloWorld(Context* context) :
  113. Sample(context)
  114. {
  115. // TO BE REMOVED
  116. // @{
  117. TestSerializable::RegisterObject(context);
  118. auto obj = MakeShared<TestSerializable>(context_);
  119. obj->ResetToDefault();
  120. VectorBuffer buf;
  121. XMLFile xml(context_);
  122. XMLElement root = xml.CreateRoot("test");
  123. obj->SaveXML(root);
  124. xml.Save(buf);
  125. String text;
  126. text.Append(reinterpret_cast<const char*>(buf.GetData()), buf.GetSize());
  127. obj.Reset();
  128. // @}
  129. }
  130. void HelloWorld::Start()
  131. {
  132. // Execute base class startup
  133. Sample::Start();
  134. // Create "Hello World" Text
  135. CreateText();
  136. // Finally subscribe to the update event. Note that by subscribing events at this point we have already missed some events
  137. // like the ScreenMode event sent by the Graphics subsystem when opening the application window. To catch those as well we
  138. // could subscribe in the constructor instead.
  139. SubscribeToEvents();
  140. // Set the mouse mode to use in the sample
  141. Sample::InitMouseMode(MM_FREE);
  142. }
  143. void HelloWorld::CreateText()
  144. {
  145. ResourceCache* cache = GetSubsystem<ResourceCache>();
  146. // Construct new Text object
  147. SharedPtr<Text> helloText(new Text(context_));
  148. // Set String to display
  149. helloText->SetText("Hello World from Urho3D!");
  150. // Set font and text color
  151. helloText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
  152. helloText->SetColor(Color(0.0f, 1.0f, 0.0f));
  153. // Align Text center-screen
  154. helloText->SetHorizontalAlignment(HA_CENTER);
  155. helloText->SetVerticalAlignment(VA_CENTER);
  156. // Add Text instance to the UI root element
  157. GetSubsystem<UI>()->GetRoot()->AddChild(helloText);
  158. }
  159. void HelloWorld::SubscribeToEvents()
  160. {
  161. // Subscribe HandleUpdate() function for processing update events
  162. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HelloWorld, HandleUpdate));
  163. }
  164. void HelloWorld::HandleUpdate(StringHash eventType, VariantMap& eventData)
  165. {
  166. // Do nothing for now, could be extended to eg. animate the display
  167. }