// Copyright (c) 2008-2023 the Urho3D project // License: MIT #include #include #include #include #include #include #include #include #include #include #include "SceneAndUILoad.h" #include URHO3D_DEFINE_APPLICATION_MAIN(SceneAndUILoad) SceneAndUILoad::SceneAndUILoad(Context* context) : Sample(context) { } void SceneAndUILoad::Start() { // Execute base class startup Sample::Start(); // Create the scene content CreateScene(); // Create the UI content CreateUI(); // Setup the viewport for displaying the scene SetupViewport(); // Subscribe to global events for camera movement SubscribeToEvents(); // Set the mouse mode to use in the sample Sample::InitMouseMode(MM_RELATIVE); } void SceneAndUILoad::CreateScene() { auto* cache = GetSubsystem(); scene_ = new Scene(context_); // Load scene content prepared in the editor (XML format). GetFile() returns an open file from the resource system // which scene.LoadXML() will read SharedPtr file = cache->GetFile("Scenes/SceneLoadExample.xml"); scene_->LoadXML(*file); // Create the camera (not included in the scene file) cameraNode_ = scene_->CreateChild("Camera"); cameraNode_->CreateComponent(); // Set an initial position for the camera scene node above the plane cameraNode_->SetPosition(Vector3(0.0f, 2.0f, -10.0f)); } void SceneAndUILoad::CreateUI() { auto* cache = GetSubsystem(); auto* ui = GetSubsystem(); // Set up global UI style into the root UI element auto* style = cache->GetResource("UI/DefaultStyle.xml"); ui->GetRoot()->SetDefaultStyle(style); // Create a Cursor UI element because we want to be able to hide and show it at will. When hidden, the mouse cursor will // control the camera, and when visible, it will interact with the UI SharedPtr cursor(new Cursor(context_)); cursor->SetStyleAuto(); ui->SetCursor(cursor); // Set starting position of the cursor at the rendering window center auto* graphics = GetSubsystem(); cursor->SetPosition(graphics->GetWidth() / 2, graphics->GetHeight() / 2); // Load UI content prepared in the editor and add to the UI hierarchy SharedPtr layoutRoot = ui->LoadLayout(cache->GetResource("UI/UILoadExample.xml")); ui->GetRoot()->AddChild(layoutRoot); // Subscribe to button actions (toggle scene lights when pressed then released) auto* button = layoutRoot->GetChildStaticCast