| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- //
- // 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 <Urho3D/Core/CoreEvents.h>
- #include <Urho3D/Core/ProcessUtils.h>
- #include <Urho3D/Input/Input.h>
- #include <Urho3D/UI/Font.h>
- #include <Urho3D/UI/Text.h>
- #include <Urho3D/UI/UI.h>
- #include <Urho3D/IO/VectorBuffer.h> // TO BE REMOVED
- #include "HelloWorld.h"
- #include <Urho3D/DebugNew.h>
- // 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<TestSerializable>();
- 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<int>(self.customEnumAttribute_);
- };
- auto setter = [=](TestSerializable& self, const Variant& value)
- {
- self.customEnumAttribute_ = static_cast<TestEnum>(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<TestEnum>(static_cast<int>(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<TestSerializable>(context_);
- obj->ResetToDefault();
- VectorBuffer buf;
- XMLFile xml(context_);
- obj->SaveXML(xml.CreateRoot("test"));
- xml.Save(buf);
- String text;
- text.Append(reinterpret_cast<const char*>(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<ResourceCache>();
- // Construct new Text object
- SharedPtr<Text> helloText(new Text(context_));
- // Set String to display
- helloText->SetText("Hello World from Urho3D!");
- // Set font and text color
- helloText->SetFont(cache->GetResource<Font>("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<UI>()->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
- }
|