Urho3D/bin/Data/Scripts/38_SceneAndUILoad.as
2017-04-09 22:17:34 +03:00

155 lines
5.1 KiB
ActionScript

// 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();
// Set the mouse mode to use in the sample
SampleInitMouseMode(MM_RELATIVE);
// 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)
{
input.mouseVisible = input.mouseMode != MM_RELATIVE;
bool mouseDown = input.mouseButtonDown[MOUSEB_RIGHT];
// Override the MM_RELATIVE mouse grabbed settings, to allow interaction with UI
input.mouseGrabbed = mouseDown;
// Right mouse button controls mouse cursor visibility: hide when pressed
ui.cursor.visible = !mouseDown;
// 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[KEY_W])
cameraNode.Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
if (input.keyDown[KEY_S])
cameraNode.Translate(Vector3::BACK * MOVE_SPEED * timeStep);
if (input.keyDown[KEY_A])
cameraNode.Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
if (input.keyDown[KEY_D])
cameraNode.Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
}
// Create XML patch instructions for screen joystick layout specific to this sample app
String patchInstructions = "";