// // Copyright (c) 2008-2015 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 #include #include #include #include #include "SceneAndUILoad.h" #include 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(); } void SceneAndUILoad::CreateScene() { ResourceCache* 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() { ResourceCache* cache = GetSubsystem(); UI* ui = GetSubsystem(); // Set up global UI style into the root UI element XMLFile* 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 Graphics* 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) Button* button = static_cast(layoutRoot->GetChild("ToggleLight1", true)); if (button) SubscribeToEvent(button, E_RELEASED, HANDLER(SceneAndUILoad, ToggleLight1)); button = static_cast(layoutRoot->GetChild("ToggleLight2", true)); if (button) SubscribeToEvent(button, E_RELEASED, HANDLER(SceneAndUILoad, ToggleLight2)); } void SceneAndUILoad::SetupViewport() { Renderer* renderer = GetSubsystem(); // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen SharedPtr viewport(new Viewport(context_, scene_, cameraNode_->GetComponent())); renderer->SetViewport(0, viewport); } void SceneAndUILoad::SubscribeToEvents() { // Subscribe HandleUpdate() function for camera motion SubscribeToEvent(E_UPDATE, HANDLER(SceneAndUILoad, HandleUpdate)); } void SceneAndUILoad::MoveCamera(float timeStep) { // Right mouse button controls mouse cursor visibility: hide when pressed UI* ui = GetSubsystem(); Input* input = GetSubsystem(); ui->GetCursor()->SetVisible(!input->GetMouseButtonDown(MOUSEB_RIGHT)); // Do not move if the UI has a focused element if (ui->GetFocusElement()) return; // Movement speed as world units per second const float MOVE_SPEED = 20.0f; // Mouse sensitivity as degrees per pixel const float MOUSE_SENSITIVITY = 0.1f; // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees // Only move the camera when the cursor is hidden if (!ui->GetCursor()->IsVisible()) { IntVector2 mouseMove = input->GetMouseMove(); yaw_ += MOUSE_SENSITIVITY * mouseMove.x_; pitch_ += MOUSE_SENSITIVITY * mouseMove.y_; pitch_ = Clamp(pitch_, -90.0f, 90.0f); // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f)); } // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed if (input->GetKeyDown('W')) cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep); if (input->GetKeyDown('S')) cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep); if (input->GetKeyDown('A')) cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep); if (input->GetKeyDown('D')) cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep); } void SceneAndUILoad::HandleUpdate(StringHash eventType, VariantMap& eventData) { using namespace Update; // Take the frame time step, which is stored as a float float timeStep = eventData[P_TIMESTEP].GetFloat(); // Move the camera, scale movement with time step MoveCamera(timeStep); } void SceneAndUILoad::ToggleLight1(StringHash eventType, VariantMap& eventData) { Node* lightNode = scene_->GetChild("Light1", true); if (lightNode) lightNode->SetEnabled(!lightNode->IsEnabled()); } void SceneAndUILoad::ToggleLight2(StringHash eventType, VariantMap& eventData) { Node* lightNode = scene_->GetChild("Light2", true); if (lightNode) lightNode->SetEnabled(!lightNode->IsEnabled()); }