Added example of loading scene & UI content. Closes #583.

This commit is contained in:
Lasse Öörni 2014-12-29 22:33:15 +02:00
Родитель c8f82c1327
Коммит 50e2493b97
9 изменённых файлов: 718 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,145 @@
-- Scene & UI load example.
-- This sample demonstrates:
-- - Loading a scene from a file and showing it
-- - Loading a UI layout from a file and showing it
-- - Subscribing to the UI layout's events
require "LuaScripts/Utilities/Sample"
function Start()
-- Execute the common startup for samples
SampleStart()
-- 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()
end
function CreateScene()
scene_ = Scene()
-- Load scene content prepared in the editor (XML format). GetFile() returns an open file from the resource system
-- which scene.LoadXML() will read
local file = cache:GetFile("Scenes/SceneLoadExample.xml")
scene_:LoadXML(file)
-- In Lua the file returned by GetFile() needs to be deleted manually
file:delete()
-- Create the camera (not included in the scene file)
cameraNode = scene_:CreateChild("Camera")
cameraNode:CreateComponent("Camera")
-- Set an initial position for the camera scene node above the plane
cameraNode.position = Vector3(0.0, 2.0, -10.0)
end
function CreateUI()
-- Set up global UI style into the root UI element
local style = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
ui.root.defaultStyle = 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
local cursor = ui.root:CreateChild("Cursor")
cursor:SetStyleAuto()
ui.cursor = cursor
-- Set starting position of the cursor at the rendering window center
cursor:SetPosition(graphics.width / 2, graphics.height / 2)
-- Load UI content prepared in the editor and add to the UI hierarchy
local layoutRoot = ui:LoadLayout(cache:GetResource("XMLFile", "UI/UILoadExample.xml"))
ui.root:AddChild(layoutRoot)
-- Subscribe to button actions (toggle scene lights when pressed then released)
local button = layoutRoot:GetChild("ToggleLight1", true)
if button ~= nil then
SubscribeToEvent(button, "Released", "ToggleLight1")
end
button = layoutRoot:GetChild("ToggleLight2", true)
if button ~= nil then
SubscribeToEvent(button, "Released", "ToggleLight2")
end
end
function ToggleLight1()
local lightNode = scene_:GetChild("Light1", true)
if lightNode ~= nil then
lightNode.enabled = not lightNode.enabled
end
end
function ToggleLight2()
local lightNode = scene_:GetChild("Light2", true)
if lightNode ~= nil then
lightNode.enabled = not lightNode.enabled
end
end
function SetupViewport()
-- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
renderer:SetViewport(0, viewport)
end
function SubscribeToEvents()
-- Subscribe HandleUpdate() function for camera motion
SubscribeToEvent("Update", "HandleUpdate")
end
function HandleUpdate(eventType, eventData)
-- Take the frame time step, which is stored as a float
local timeStep = eventData:GetFloat("TimeStep")
-- Move the camera, scale movement with time step
MoveCamera(timeStep)
end
function MoveCamera(timeStep)
-- Right mouse button controls mouse cursor visibility: hide when pressed
ui.cursor.visible = not input:GetMouseButtonDown(MOUSEB_RIGHT)
-- Do not move if the UI has a focused element
if ui.focusElement ~= nil then
return
end
-- Movement speed as world units per second
local MOVE_SPEED = 20.0
-- Mouse sensitivity as degrees per pixel
local MOUSE_SENSITIVITY = 0.1
-- 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 not ui.cursor.visible then
local mouseMove = input.mouseMove
yaw = yaw + MOUSE_SENSITIVITY * mouseMove.x
pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
pitch = Clamp(pitch, -90.0, 90.0)
-- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
end
-- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
if input:GetKeyDown(KEY_W) then
cameraNode:Translate(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
end
if input:GetKeyDown(KEY_S) then
cameraNode:Translate(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
end
if input:GetKeyDown(KEY_A) then
cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
end
if input:GetKeyDown(KEY_D) then
cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
end
end

Просмотреть файл

@ -0,0 +1,97 @@
<?xml version="1.0"?>
<scene id="1">
<attribute name="Name" value="" />
<attribute name="Time Scale" value="1" />
<attribute name="Smoothing Constant" value="50" />
<attribute name="Snap Threshold" value="5" />
<attribute name="Elapsed Time" value="0" />
<attribute name="Next Replicated Node ID" value="8" />
<attribute name="Next Replicated Component ID" value="9" />
<attribute name="Next Local Node ID" value="16777216" />
<attribute name="Next Local Component ID" value="16777216" />
<attribute name="Variables" />
<attribute name="Variable Names" value="" />
<component type="Octree" id="1" />
<component type="DebugRenderer" id="2" />
<node id="2">
<attribute name="Is Enabled" value="true" />
<attribute name="Name" value="Plane" />
<attribute name="Position" value="0 0.0792121 0" />
<attribute name="Rotation" value="1 0 0 0" />
<attribute name="Scale" value="5 1 5" />
<attribute name="Variables" />
<component type="StaticModel" id="3">
<attribute name="Model" value="Model;Models/Plane.mdl" />
<attribute name="Material" value="Material;Materials/Stone.xml" />
</component>
</node>
<node id="3">
<attribute name="Is Enabled" value="true" />
<attribute name="Name" value="Box" />
<attribute name="Position" value="1.32267 0.594299 -1.57271" />
<attribute name="Rotation" value="0.92388 0 0.382683 0" />
<attribute name="Scale" value="1 1 1" />
<attribute name="Variables" />
<component type="StaticModel" id="4">
<attribute name="Model" value="Model;Models/Box.mdl" />
<attribute name="Material" value="Material;Materials/Stone.xml" />
<attribute name="Cast Shadows" value="true" />
</component>
</node>
<node id="4">
<attribute name="Is Enabled" value="true" />
<attribute name="Name" value="Box" />
<attribute name="Position" value="-1.30641 0.594299 -1.3801" />
<attribute name="Rotation" value="0.966853 0 -0.255333 0" />
<attribute name="Scale" value="1 1 1" />
<attribute name="Variables" />
<component type="StaticModel" id="5">
<attribute name="Model" value="Model;Models/Box.mdl" />
<attribute name="Material" value="Material;Materials/Stone.xml" />
<attribute name="Cast Shadows" value="true" />
</component>
</node>
<node id="5">
<attribute name="Is Enabled" value="true" />
<attribute name="Name" value="Light1" />
<attribute name="Position" value="0.275269 1.54593 -5.1247" />
<attribute name="Rotation" value="0.996465 0.0840061 0 0" />
<attribute name="Scale" value="1 1 1" />
<attribute name="Variables" />
<component type="Light" id="6">
<attribute name="Light Type" value="Spot" />
<attribute name="Color" value="1 0.5 0.5 1" />
<attribute name="Spot FOV" value="60" />
<attribute name="Cast Shadows" value="true" />
<attribute name="Depth Constant Bias" value="0.0001" />
</component>
</node>
<node id="6">
<attribute name="Is Enabled" value="true" />
<attribute name="Name" value="Box" />
<attribute name="Position" value="0.055109 0.594299 0.653738" />
<attribute name="Rotation" value="0.999947 0 0.0103017 0" />
<attribute name="Scale" value="1 1 1" />
<attribute name="Variables" />
<component type="StaticModel" id="7">
<attribute name="Model" value="Model;Models/Box.mdl" />
<attribute name="Material" value="Material;Materials/Stone.xml" />
<attribute name="Cast Shadows" value="true" />
</component>
</node>
<node id="7">
<attribute name="Is Enabled" value="true" />
<attribute name="Name" value="Light2" />
<attribute name="Position" value="-2.55712 0.969277 4.03673" />
<attribute name="Rotation" value="0.346269 0.0588016 0.923076 -0.156752" />
<attribute name="Scale" value="1 1 1" />
<attribute name="Variables" />
<component type="Light" id="8">
<attribute name="Light Type" value="Spot" />
<attribute name="Color" value="0.5 0.5 1 1" />
<attribute name="Spot FOV" value="60" />
<attribute name="Cast Shadows" value="true" />
<attribute name="Depth Constant Bias" value="0.0001" />
</component>
</node>
</scene>

Просмотреть файл

@ -0,0 +1,145 @@
// Scene & UI load example.
// This sample demonstrates:
// - Loading a scene from a file and showing it
// - Loading a UI layout from a file and showing it
// - Subscribing to the UI layout's events
#include "Scripts/Utilities/Sample.as"
void Start()
{
// Execute the common startup for samples
SampleStart();
// Create the scene content
CreateScene();
// Create the UI content and subscribe to UI events
CreateUI();
// Setup the viewport for displaying the scene
SetupViewport();
// Subscribe to global events for camera movement
SubscribeToEvents();
}
void CreateScene()
{
scene_ = Scene();
// Load scene content prepared in the editor (XML format). GetFile() returns an open file from the resource system
// which scene.LoadXML() will read
scene_.LoadXML(cache.GetFile("Scenes/SceneLoadExample.xml"));
// Create the camera (not included in the scene file)
cameraNode = scene_.CreateChild("Camera");
cameraNode.CreateComponent("Camera");
// Set an initial position for the camera scene node above the plane
cameraNode.position = Vector3(0.0f, 2.0f, -10.0f);
}
void CreateUI()
{
// Set up global UI style into the root UI element
XMLFile@ style = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
ui.root.defaultStyle = 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
Cursor@ cursor = Cursor();
cursor.SetStyleAuto();
ui.cursor = cursor;
// Set starting position of the cursor at the rendering window center
cursor.SetPosition(graphics.width / 2, graphics.height / 2);
// Load UI content prepared in the editor and add to the UI hierarchy
UIElement@ layoutRoot = ui.LoadLayout(cache.GetResource("XMLFile", "UI/UILoadExample.xml"));
ui.root.AddChild(layoutRoot);
// Subscribe to button actions (toggle scene lights when pressed then released)
Button@ button = layoutRoot.GetChild("ToggleLight1", true);
if (button !is null)
SubscribeToEvent(button, "Released", "ToggleLight1");
button = layoutRoot.GetChild("ToggleLight2", true);
if (button !is null)
SubscribeToEvent(button, "Released", "ToggleLight2");
}
void ToggleLight1()
{
Node@ lightNode = scene_.GetChild("Light1", true);
if (lightNode !is null)
lightNode.enabled = !lightNode.enabled;
}
void ToggleLight2()
{
Node@ lightNode = scene_.GetChild("Light2", true);
if (lightNode !is null)
lightNode.enabled = !lightNode.enabled;
}
void SetupViewport()
{
// Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
renderer.viewports[0] = viewport;
}
void SubscribeToEvents()
{
// Subscribe HandleUpdate() function for camera motion
SubscribeToEvent("Update", "HandleUpdate");
}
void HandleUpdate(StringHash eventType, VariantMap& eventData)
{
// Take the frame time step, which is stored as a float
float timeStep = eventData["TimeStep"].GetFloat();
// Move the camera, scale movement with time step
MoveCamera(timeStep);
}
void MoveCamera(float timeStep)
{
// Right mouse button controls mouse cursor visibility: hide when pressed
ui.cursor.visible = !input.mouseButtonDown[MOUSEB_RIGHT];
// Do not move if the UI has a focused element
if (ui.focusElement !is null)
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.cursor.visible)
{
IntVector2 mouseMove = input.mouseMove;
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.rotation = Quaternion(pitch, yaw, 0.0f);
}
// Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
if (input.keyDown['W'])
cameraNode.Translate(Vector3(0.0f, 0.0f, 1.0f) * MOVE_SPEED * timeStep);
if (input.keyDown['S'])
cameraNode.Translate(Vector3(0.0f, 0.0f, -1.0f) * MOVE_SPEED * timeStep);
if (input.keyDown['A'])
cameraNode.Translate(Vector3(-1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
if (input.keyDown['D'])
cameraNode.Translate(Vector3(1.0f, 0.0f, 0.0f) * MOVE_SPEED * timeStep);
}
// Create XML patch instructions for screen joystick layout specific to this sample app
String patchInstructions = "";

Просмотреть файл

@ -0,0 +1,33 @@
<?xml version="1.0"?>
<element type="Window">
<attribute name="Position" value="100 100" />
<attribute name="Size" value="180 120" />
<element type="Button">
<attribute name="Name" value="ToggleLight1" />
<attribute name="Position" value="20 20" />
<attribute name="Size" value="140 30" />
<element type="Text">
<attribute name="Horiz Alignment" value="Center" />
<attribute name="Vert Alignment" value="Center" />
<attribute name="Top Left Color" value="0.85 0.85 0.85 1" />
<attribute name="Top Right Color" value="0.85 0.85 0.85 1" />
<attribute name="Bottom Left Color" value="0.85 0.85 0.85 1" />
<attribute name="Bottom Right Color" value="0.85 0.85 0.85 1" />
<attribute name="Text" value="Toggle light 1" />
</element>
</element>
<element type="Button">
<attribute name="Name" value="ToggleLight2" />
<attribute name="Position" value="20 70" />
<attribute name="Size" value="140 30" />
<element type="Text">
<attribute name="Horiz Alignment" value="Center" />
<attribute name="Vert Alignment" value="Center" />
<attribute name="Top Left Color" value="0.85 0.85 0.85 1" />
<attribute name="Top Right Color" value="0.85 0.85 0.85 1" />
<attribute name="Bottom Left Color" value="0.85 0.85 0.85 1" />
<attribute name="Bottom Right Color" value="0.85 0.85 0.85 1" />
<attribute name="Text" value="Toggle light 2" />
</element>
</element>
</element>

Просмотреть файл

@ -564,7 +564,7 @@ All the \ref Running_Commandline "command line options" supported by Urho3D play
-scene </path/to/scene.xml> Load a scene after starting up
\endverbatim
Hint: to get some content to look at, run the Physics sample application (Bin/Data/Scripts/11_Physics.as), and press F5. This saves a scene file called Physics.xml into the Data/Scenes subdirectory, which can be loaded in the editor. The NinjaSnowWar scene also exists in the Data/Scenes subdirectory, and the NinjaSnowWar object "prefabs" are in the Data/Objects subdirectory.
Hint: to get more content to look at, run the Physics sample application (Bin/Data/Scripts/11_Physics.as), and press F5. This saves a scene file called Physics.xml into the Data/Scenes subdirectory, which can be loaded in the editor. Scenes utilized by the NinjaSnowWar and SceneAndUILoad examples also exist in the Data/Scenes subdirectory, and the NinjaSnowWar object "prefabs" are in the Data/Objects subdirectory.
\section EditorInstructions_Controls Controls

Просмотреть файл

@ -0,0 +1,33 @@
#
# Copyright (c) 2008-2014 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.
#
# Define target name
set (TARGET_NAME 38_SceneAndUILoad)
# Define source files
define_source_files (EXTRA_H_FILES ${COMMON_SAMPLE_H_FILES})
# Setup target with resource copying
setup_main_executable ()
# Setup test cases
add_test (NAME ${TARGET_NAME} COMMAND ${TARGET_NAME} -timeout ${URHO3D_TEST_TIME_OUT})

Просмотреть файл

@ -0,0 +1,194 @@
//
// Copyright (c) 2008-2014 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 "Button.h"
#include "Camera.h"
#include "CoreEvents.h"
#include "Cursor.h"
#include "Engine.h"
#include "Graphics.h"
#include "Input.h"
#include "ResourceCache.h"
#include "Scene.h"
#include "UI.h"
#include "UIEvents.h"
#include "XMLFile.h"
#include "Zone.h"
#include "SceneAndUILoad.h"
#include "DebugNew.h"
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<ResourceCache>();
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> file = cache->GetFile("Scenes/SceneLoadExample.xml");
scene_->LoadXML(*file);
// Create the camera (not included in the scene file)
cameraNode_ = scene_->CreateChild("Camera");
cameraNode_->CreateComponent<Camera>();
// 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<ResourceCache>();
UI* ui = GetSubsystem<UI>();
// Set up global UI style into the root UI element
XMLFile* style = cache->GetResource<XMLFile>("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> cursor(new Cursor(context_));
cursor->SetStyleAuto();
ui->SetCursor(cursor);
// Set starting position of the cursor at the rendering window center
Graphics* graphics = GetSubsystem<Graphics>();
cursor->SetPosition(graphics->GetWidth() / 2, graphics->GetHeight() / 2);
// Load UI content prepared in the editor and add to the UI hierarchy
SharedPtr<UIElement> layoutRoot = ui->LoadLayout(cache->GetResource<XMLFile>("UI/UILoadExample.xml"));
ui->GetRoot()->AddChild(layoutRoot);
// Subscribe to button actions (toggle scene lights when pressed then released)
Button* button = static_cast<Button*>(layoutRoot->GetChild("ToggleLight1", true));
if (button)
SubscribeToEvent(button, E_RELEASED, HANDLER(SceneAndUILoad, ToggleLight1));
button = static_cast<Button*>(layoutRoot->GetChild("ToggleLight2", true));
if (button)
SubscribeToEvent(button, E_RELEASED, HANDLER(SceneAndUILoad, ToggleLight2));
}
void SceneAndUILoad::SetupViewport()
{
Renderer* renderer = GetSubsystem<Renderer>();
// Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
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<UI>();
Input* input = GetSubsystem<Input>();
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());
}

Просмотреть файл

@ -0,0 +1,69 @@
//
// Copyright (c) 2008-2014 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.
//
#pragma once
#include "Sample.h"
namespace Urho3D
{
class Drawable;
class Node;
class Scene;
}
/// Scene & UI load example.
/// This sample demonstrates:
/// - Loading a scene from a file and showing it
/// - Loading a UI layout from a file and showing it
/// - Subscribing to the UI layout's events
class SceneAndUILoad : public Sample
{
OBJECT(SceneAndUILoad);
public:
/// Construct.
SceneAndUILoad(Context* context);
/// Setup after engine initialization and before running the main loop.
virtual void Start();
private:
/// Construct the scene content.
void CreateScene();
/// Construct user interface elements.
void CreateUI();
/// Set up a viewport for displaying the scene.
void SetupViewport();
/// Subscribe to application-wide logic update event.
void SubscribeToEvents();
/// Reads input and moves the camera.
void MoveCamera(float timeStep);
/// Handle the logic update event.
void HandleUpdate(StringHash eventType, VariantMap& eventData);
/// Handle toggle button 1 being pressed.
void ToggleLight1(StringHash eventType, VariantMap& eventData);
/// Handle toggle button 2 being pressed.
void ToggleLight2(StringHash eventType, VariantMap& eventData);
};

Просмотреть файл

@ -85,3 +85,4 @@ add_subdirectory (31_MaterialAnimation)
add_subdirectory (34_DynamicGeometry)
add_subdirectory (35_SignedDistanceFieldText)
add_subdirectory (37_UIDrag)
add_subdirectory (38_SceneAndUILoad)