// // Copyright (c) 2008-2017 the Urho3D project. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #include #include #include #include #include #include #include // TO BE REMOVED #include "HelloWorld.h" #include // Expands to this example's entry-point URHO3D_DEFINE_APPLICATION_MAIN(HelloWorld) // TO BE REMOVED // @{ static const char* enumNames[] = { "Enum1", "Enum2", "Enum3" }; enum class TestEnum { Enum1, Enum2, Enum3, }; class TestSerializable : public Serializable { URHO3D_OBJECT(TestSerializable, Serializable); public: TestSerializable(Context* context) : Serializable(context) { } static void RegisterObject(Context* context) { context->RegisterFactory(); URHO3D_ATTRIBUTE("attribute", String, attribute_, "attribute", AM_DEFAULT); URHO3D_ATTRIBUTE_EX("attributeEx", String, attributeEx_, OnAttributeExSet, "attributeEx", AM_DEFAULT); URHO3D_ENUM_ATTRIBUTE("enumAttribute", enumAttribute_, enumNames, TestEnum::Enum2, AM_DEFAULT); URHO3D_ENUM_ATTRIBUTE_EX("enumAttributeEx", enumAttributeEx_, OnEnumAttributeExSet, enumNames, TestEnum::Enum2, AM_DEFAULT); URHO3D_ACCESSOR_ATTRIBUTE("accessorAttribute", GetAccessorAttribute, SetAccessorAttribute, String, "accessorAttribute", AM_DEFAULT); URHO3D_ENUM_ACCESSOR_ATTRIBUTE("enumAccessorAttribute", GetEnumAccessorAttribute, SetEnumAccessorAttribute, TestEnum, enumNames, TestEnum::Enum3, AM_DEFAULT); URHO3D_MIXED_ACCESSOR_ATTRIBUTE("mixedAccessorAttribute", GetMixedAccessorAttribute, SetMixedAccessorAttribute, String, "mixedAccessorAttribute", AM_DEFAULT); { String suffix = "_temp"; auto getter = [=](const TestSerializable& self, Variant& value) { value = self.customAttribute_ + suffix; }; auto setter = [=](TestSerializable& self, const Variant& value) { self.customAttribute_ = value.GetString() + suffix; }; URHO3D_CUSTOM_ATTRIBUTE("customAttribute", getter, setter, String, "customAttribute", AM_DEFAULT); } { auto getter = [=](const TestSerializable& self, Variant& value) { value = static_cast(self.customEnumAttribute_); }; auto setter = [=](TestSerializable& self, const Variant& value) { self.customEnumAttribute_ = static_cast(value.GetInt()); }; URHO3D_CUSTOM_ENUM_ATTRIBUTE("customAttribute", getter, setter, enumNames, TestEnum::Enum1, AM_DEFAULT); } } String attribute_; String attributeEx_; void OnAttributeExSet() { attributeEx_ = attributeEx_.ToUpper(); } TestEnum enumAttribute_ = TestEnum::Enum1; TestEnum enumAttributeEx_ = TestEnum::Enum1; void OnEnumAttributeExSet() { enumAttributeEx_ = static_cast(static_cast(enumAttributeEx_) + 1); } String accessorAttribute_; const String& GetAccessorAttribute() const { return accessorAttribute_; } void SetAccessorAttribute(const String& value) { accessorAttribute_ = value; } TestEnum enumAccessorAttribute_ = TestEnum::Enum1; TestEnum GetEnumAccessorAttribute() const { return enumAccessorAttribute_; } void SetEnumAccessorAttribute(TestEnum value) { enumAccessorAttribute_ = value; } String mixedAccessorAttribute_; String GetMixedAccessorAttribute() const { return mixedAccessorAttribute_; } void SetMixedAccessorAttribute(const String& value) { mixedAccessorAttribute_ = value; } String customAttribute_; TestEnum customEnumAttribute_ = TestEnum::Enum1; }; // @} HelloWorld::HelloWorld(Context* context) : Sample(context) { // TO BE REMOVED // @{ TestSerializable::RegisterObject(context); auto obj = MakeShared(context_); obj->ResetToDefault(); VectorBuffer buf; XMLFile xml(context_); obj->SaveXML(xml.CreateRoot("test")); xml.Save(buf); String text; text.Append(reinterpret_cast(buf.GetData()), buf.GetSize()); obj.Reset(); // @} } void HelloWorld::Start() { // Execute base class startup Sample::Start(); // Create "Hello World" Text CreateText(); // Finally subscribe to the update event. Note that by subscribing events at this point we have already missed some events // like the ScreenMode event sent by the Graphics subsystem when opening the application window. To catch those as well we // could subscribe in the constructor instead. SubscribeToEvents(); // Set the mouse mode to use in the sample Sample::InitMouseMode(MM_FREE); } void HelloWorld::CreateText() { ResourceCache* cache = GetSubsystem(); // Construct new Text object SharedPtr helloText(new Text(context_)); // Set String to display helloText->SetText("Hello World from Urho3D!"); // Set font and text color helloText->SetFont(cache->GetResource("Fonts/Anonymous Pro.ttf"), 30); helloText->SetColor(Color(0.0f, 1.0f, 0.0f)); // Align Text center-screen helloText->SetHorizontalAlignment(HA_CENTER); helloText->SetVerticalAlignment(VA_CENTER); // Add Text instance to the UI root element GetSubsystem()->GetRoot()->AddChild(helloText); } void HelloWorld::SubscribeToEvents() { // Subscribe HandleUpdate() function for processing update events SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HelloWorld, HandleUpdate)); } void HelloWorld::HandleUpdate(StringHash eventType, VariantMap& eventData) { // Do nothing for now, could be extended to eg. animate the display }