| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- // Copyright (c) 2008-2023 the Urho3D project
- // License: MIT
- #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 "HelloWorld.h"
- #include <Urho3D/DebugNew.h>
- // Expands to this example's entry-point
- URHO3D_DEFINE_APPLICATION_MAIN(HelloWorld)
- HelloWorld::HelloWorld(Context* context) :
- Sample(context)
- {
- }
- 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()
- {
- auto* 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
- }
|