void Urho3DQtApplication::MoveCamera(float timeStep) { // Do not move if the UI has a focused element (the console) if (GetSubsystem<UI>()->GetFocusElement()) return; Input* input = GetSubsystem<Input>(); // 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; if (input->GetMouseButtonDown(1)) { // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees 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 // Use the TranslateRelative() function to move relative to the node's orientation. Alternatively we could // multiply the desired direction with the node's orientation quaternion, and use just Translate() 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 Level00::HandleInput(Input& input) { // Player Mouse if (input.GetKeyDown(KEYCODE_1)) { Application::SharedInstance().LoadScene<MainMenuScene>(); } if (input.GetMouseButtonDown(MOUSEBUTTON_LEFT)) { float x = (2.0f * input.mousePosition.x) / mRenderer->GetWindowWidth() - 1.0f; float y = 1.0f - (2.0f * input.mousePosition.y) / mRenderer->GetWindowHeight(); mat4f toWorld = (mQuadShaderData.Projection * mQuadShaderData.View).inverse(); vec3f worldPos = vec4f(x, y, 0.0f, 1.0f) * toWorld; vec3f worldDir = vec3f(0.0f, 0.0f, 1.0f); worldPos.z = -30.0f; Ray<vec3f> ray = { worldPos, worldDir }; RayCastHit<vec3f> hit; if (RayCast(&hit, ray, mLightColliders, mCircleCount)) { mCircleColorWeights[hit.index] = mCircleColorWeights[hit.index] > 0 ? 0.0f : 1.0f; } } }
void SceneView3D::MoveCamera(float timeStep) { if (!enabled_) return; Input* input = GetSubsystem<Input>(); // Movement speed as world units per second float MOVE_SPEED = 20.0f; // Mouse sensitivity as degrees per pixel const float MOUSE_SENSITIVITY = 0.2f; if (input->GetKeyDown(KEY_LSHIFT) || input->GetKeyDown(KEY_RSHIFT)) MOVE_SPEED *= 3.0f; // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees if (input->GetMouseButtonDown(MOUSEB_RIGHT)) { IntVector2 mouseMove = input->GetMouseMove(); yaw_ += MOUSE_SENSITIVITY * mouseMove.x_; pitch_ += MOUSE_SENSITIVITY * mouseMove.y_; pitch_ = Clamp(pitch_, -90.0f, 90.0f); // Not working on OSX //input->SetMouseMode(MM_RELATIVE); } else { // Not working on OSX /* if (input->GetMouseMode() != MM_ABSOLUTE) input->SetMouseMode(MM_ABSOLUTE); */ } // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f)); //Vector3 pos = cameraNode_->GetWorldPosition(); //Quaternion q = cameraNode_->GetWorldRotation(); //LOGINFOF("%f %f %f : %f %f %f %f", pos.x_, pos.y_, pos.z_, q.x_, q.y_, q.z_, q.w_ ); // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed // Use the Translate() function (default local space) to move relative to the node's orientation. 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 Navigation::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 (the console) 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_->TranslateRelative(Vector3::FORWARD * MOVE_SPEED * timeStep); if (input->GetKeyDown('S')) cameraNode_->TranslateRelative(Vector3::BACK * MOVE_SPEED * timeStep); if (input->GetKeyDown('A')) cameraNode_->TranslateRelative(Vector3::LEFT * MOVE_SPEED * timeStep); if (input->GetKeyDown('D')) cameraNode_->TranslateRelative(Vector3::RIGHT * MOVE_SPEED * timeStep); // Set route start/endpoint with left mouse button, recalculate route if applicable if (input->GetMouseButtonPress(MOUSEB_LEFT)) SetPathPoint(); // Add or remove objects with middle mouse button, then rebuild navigation mesh partially if (input->GetMouseButtonPress(MOUSEB_MIDDLE)) AddOrRemoveObject(); // Toggle debug geometry with space if (input->GetKeyPress(KEY_SPACE)) drawDebug_ = !drawDebug_; }
void DebugCameraController::Update(float timeStep) { // Do not move if the UI has a focused element if (GetUI()->GetFocusElement()) return; // Do not move if interacting with UI controls if (GetSystemUI()->IsAnyItemActive()) return; Input* input = GetInput(); // Movement speed as world units per second float moveSpeed_ = speed_; if (input->GetKeyDown(KEY_SHIFT)) { moveSpeed_ *= 2; if (input->GetKeyPress(KEY_KP_PLUS)) speed_ += 1.f; else if (input->GetKeyPress(KEY_KP_MINUS)) speed_ -= 1.f; } if (input->GetMouseButtonDown(MOUSEB_RIGHT)) { IntVector2 delta = input->GetMouseMove(); if (input->IsMouseVisible() && delta != IntVector2::ZERO) input->SetMouseVisible(false); auto yaw = GetNode()->GetRotation().EulerAngles().x_; if ((yaw > -90.f && yaw < 90.f) || (yaw <= -90.f && delta.y_ > 0) || (yaw >= 90.f && delta.y_ < 0)) GetNode()->RotateAround(Vector3::ZERO, Quaternion(mouseSensitivity_ * delta.y_, Vector3::RIGHT), TS_LOCAL); GetNode()->RotateAround(GetNode()->GetPosition(), Quaternion(mouseSensitivity_ * delta.x_, Vector3::UP), TS_WORLD); } else if (!input->IsMouseVisible()) input->SetMouseVisible(true); // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed if (input->GetKeyDown(KEY_W)) GetNode()->Translate(Vector3::FORWARD * moveSpeed_ * timeStep); if (input->GetKeyDown(KEY_S)) GetNode()->Translate(Vector3::BACK * moveSpeed_ * timeStep); if (input->GetKeyDown(KEY_A)) GetNode()->Translate(Vector3::LEFT * moveSpeed_ * timeStep); if (input->GetKeyDown(KEY_D)) GetNode()->Translate(Vector3::RIGHT * moveSpeed_ * timeStep); }
void UIDragDrop::HandleUIUpdate(StringHash eventType, VariantMap& eventData) { Input* input = GetSubsystem<Input>(); if (dragSourceWidget_.NotNull() || !input->IsMouseVisible() || !input->GetMouseButtonDown(MOUSEB_LEFT)) { return; } if (TBWidget::hovered_widget) { // see if we have a widget with a drag object TBWidget* tbw = TBWidget::hovered_widget; UIWidget* widget = nullptr; while(tbw) { if (tbw->GetDelegate()) { widget = (UIWidget*) tbw->GetDelegate(); if (widget->GetDragObject()) { // TODO: check if we're in widget bounds // this is going to need to be updated for drag/drop multiselect break; } widget = nullptr; } tbw = tbw->GetParent(); } if (!widget) return; currentTargetWidget_ = widget; dragSourceWidget_ = widget; mouseDownPosition_ = input->GetMousePosition(); } }
void DebugCameraController2D::Update(float timeStep) { // Do not move if the UI has a focused element if (GetUI()->GetFocusElement()) return; // Do not move if interacting with UI controls if (GetSystemUI()->IsAnyItemActive()) return; Input* input = GetInput(); // Movement speed as world units per second float moveSpeed_ = speed_; if (input->GetKeyDown(KEY_SHIFT)) { moveSpeed_ *= 2; if (input->GetKeyPress(KEY_KP_PLUS)) speed_ += 1.f; else if (input->GetKeyPress(KEY_KP_MINUS)) speed_ -= 1.f; } if (input->GetMouseButtonDown(MOUSEB_RIGHT)) { IntVector2 delta = input->GetMouseMove(); if (input->IsMouseVisible() && delta != IntVector2::ZERO) input->SetMouseVisible(false); GetNode()->Translate2D(Vector2{(float)delta.x_ * -1.f, (float)delta.y_} * moveSpeed_ * timeStep); } else if (!input->IsMouseVisible()) input->SetMouseVisible(true); // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed if (input->GetKeyDown(KEY_W)) GetNode()->Translate(Vector3::UP * moveSpeed_ * timeStep); if (input->GetKeyDown(KEY_S)) GetNode()->Translate(Vector3::DOWN * moveSpeed_ * timeStep); if (input->GetKeyDown(KEY_A)) GetNode()->Translate(Vector3::LEFT * moveSpeed_ * timeStep); if (input->GetKeyDown(KEY_D)) GetNode()->Translate(Vector3::RIGHT * moveSpeed_ * timeStep); }
void Urho3DTemplate::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 (the console) 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(); cameraYaw_ += MOUSE_SENSITIVITY * mouseMove.x_; cameraPitch_ += MOUSE_SENSITIVITY * mouseMove.y_; cameraPitch_ = Clamp(cameraPitch_, -90.0f, 90.0f); //Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero cameraNode_->SetRotation(Quaternion(cameraPitch_, cameraYaw_, 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); //Add or remove objects with left mouse button if (input->GetMouseButtonPress(MOUSEB_LEFT)) AddOrRemoveObject(); }
void SceneReplication::MoveCamera() { // 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)); // 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 and 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_, 1.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)); // Only move the camera / show instructions if we have a controllable object bool showInstructions = false; if (clientObjectID_) { Node* ballNode = scene_->GetNode(clientObjectID_); if (ballNode) { const float CAMERA_DISTANCE = 5.0f; // Move camera some distance away from the ball cameraNode_->SetPosition(ballNode->GetPosition() + cameraNode_->GetRotation() * Vector3::BACK * CAMERA_DISTANCE); showInstructions = true; } } instructionsText_->SetVisible(showInstructions); }
void Gizmo3D::Use() { if (gizmo_.Null() || !gizmo_->IsEnabled() || editMode_ == EDIT_SELECT) { //StoreGizmoEditActions(); //previousGizmoDrag = false; return; } ResourceCache* cache = GetSubsystem<ResourceCache>(); Input* input = GetSubsystem<Input>(); Ray cameraRay = view3D_->GetCameraRay(); float scale = gizmoNode_->GetScale().x_; // Recalculate axes only when not left-dragging bool drag = input->GetMouseButtonDown(MOUSEB_LEFT);// && (Abs(input->GetMouseMoveX()) > 3 || Abs(input->GetMouseMoveY()) > 3); if (!drag) CalculateGizmoAxes(); gizmoAxisX_.Update(cameraRay, scale, drag, camera_->GetNode()); gizmoAxisY_.Update(cameraRay, scale, drag, camera_->GetNode()); gizmoAxisZ_.Update(cameraRay, scale, drag, camera_->GetNode()); if (!editNodes_->Size() || editNodes_->At(0) == scene_) { gizmoAxisX_.selected_ = gizmoAxisY_.selected_ = gizmoAxisZ_.selected_ = false; // this just forces an update gizmoAxisX_.lastSelected_ = gizmoAxisY_.lastSelected_ = gizmoAxisZ_.lastSelected_ = true; } if (gizmoAxisX_.selected_ != gizmoAxisX_.lastSelected_) { gizmo_->SetMaterial(0, cache->GetResource<Material>( gizmoAxisX_.selected_ ? "AtomicEditor/Materials/BrightRedUnlit.xml" : "AtomicEditor/Materials/RedUnlit.xml")); gizmoAxisX_.lastSelected_ = gizmoAxisX_.selected_; } if (gizmoAxisY_.selected_ != gizmoAxisY_.lastSelected_) { gizmo_->SetMaterial(1, cache->GetResource<Material>( gizmoAxisY_.selected_ ? "AtomicEditor/Materials/BrightGreenUnlit.xml" : "AtomicEditor/Materials/GreenUnlit.xml")); gizmoAxisY_.lastSelected_ = gizmoAxisY_.selected_; } if (gizmoAxisZ_.selected_ != gizmoAxisZ_.lastSelected_) { gizmo_->SetMaterial(2, cache->GetResource<Material>( gizmoAxisZ_.selected_ ? "AtomicEditor/Materials/BrightBlueUnlit.xml" : "AtomicEditor/Materials/BlueUnlit.xml")); gizmoAxisZ_.lastSelected_ = gizmoAxisZ_.selected_; } if (drag) Drag(); }
void GizmoScene3D::UseGizmo() { if (gizmo == NULL || !gizmo->IsEnabled() || epScene3D_->editMode == EDIT_SELECT) { // StoreGizmoEditActions(); // previousGizmoDrag = false; return; } UI* ui = GetSubsystem<UI>(); IntVector2 pos = ui->GetCursorPosition(); UIElement* e = ui->GetElementAt(pos); if (e != epScene3D_->activeView) return; const IntVector2& screenpos = epScene3D_->activeView->GetScreenPosition(); float posx = float(pos.x_ - screenpos.x_) / float(epScene3D_->activeView->GetWidth()); float posy = float(pos.y_ - screenpos.y_) / float(epScene3D_->activeView->GetHeight()); Ray cameraRay = epScene3D_->camera_->GetScreenRay(posx, posy); float scale = gizmoNode->GetScale().x_; Input* input = GetSubsystem<Input>(); // Recalculate axes only when not left-dragging bool drag = input->GetMouseButtonDown(MOUSEB_LEFT); if (!drag) CalculateGizmoAxes(); gizmoAxisX->Update(cameraRay, scale, drag, epScene3D_->cameraNode_->GetPosition()); gizmoAxisY->Update(cameraRay, scale, drag, epScene3D_->cameraNode_->GetPosition()); gizmoAxisZ->Update(cameraRay, scale, drag, epScene3D_->cameraNode_->GetPosition()); ResourceCache* cache = GetSubsystem<ResourceCache>(); if (gizmoAxisX->selected != gizmoAxisX->lastSelected) { gizmo->SetMaterial(0, cache->GetResource<Material>(gizmoAxisX->selected ? "Materials/Editor/BrightRedUnlit.xml" : "Materials/Editor/RedUnlit.xml")); gizmoAxisX->lastSelected = gizmoAxisX->selected; } if (gizmoAxisY->selected != gizmoAxisY->lastSelected) { gizmo->SetMaterial(1, cache->GetResource<Material>(gizmoAxisY->selected ? "Materials/Editor/BrightGreenUnlit.xml" : "Materials/Editor/GreenUnlit.xml")); gizmoAxisY->lastSelected = gizmoAxisY->selected; } if (gizmoAxisZ->selected != gizmoAxisZ->lastSelected) { gizmo->SetMaterial(2, cache->GetResource<Material>(gizmoAxisZ->selected ? "Materials/Editor/BrightBlueUnlit.xml" : "Materials/Editor/BlueUnlit.xml")); gizmoAxisZ->lastSelected = gizmoAxisZ->selected; }; if (drag) { // Store initial transforms for undo when gizmo drag started // if (!previousGizmoDrag) // { // oldGizmoTransforms.Resize(editNodes.length); // for (uint i = 0; i < editNodes.length; ++i) // oldGizmoTransforms[i].Define(editNodes[i]); // } bool moved = false; if (epScene3D_->editMode == EDIT_MOVE) { Vector3 adjust(0, 0, 0); if (gizmoAxisX->selected) adjust += Vector3(1, 0, 0) * (gizmoAxisX->t - gizmoAxisX->lastT); if (gizmoAxisY->selected) adjust += Vector3(0, 1, 0) * (gizmoAxisY->t - gizmoAxisY->lastT); if (gizmoAxisZ->selected) adjust += Vector3(0, 0, 1) * (gizmoAxisZ->t - gizmoAxisZ->lastT); moved = epScene3D_->MoveNodes(adjust); } else if (epScene3D_->editMode == EDIT_ROTATE) { Vector3 adjust(0, 0, 0); if (gizmoAxisX->selected) adjust.x_ = (gizmoAxisX->d - gizmoAxisX->lastD) * rotSensitivity / scale; if (gizmoAxisY->selected) adjust.y_ = -(gizmoAxisY->d - gizmoAxisY->lastD) * rotSensitivity / scale; if (gizmoAxisZ->selected) adjust.z_ = (gizmoAxisZ->d - gizmoAxisZ->lastD) * rotSensitivity / scale; moved = epScene3D_->RotateNodes(adjust); } else if (epScene3D_->editMode == EDIT_SCALE) { Vector3 adjust(0, 0, 0); if (gizmoAxisX->selected) adjust += Vector3(1, 0, 0) * (gizmoAxisX->t - gizmoAxisX->lastT); if (gizmoAxisY->selected) adjust += Vector3(0, 1, 0) * (gizmoAxisY->t - gizmoAxisY->lastT); if (gizmoAxisZ->selected) adjust += Vector3(0, 0, 1) * (gizmoAxisZ->t - gizmoAxisZ->lastT); // Special handling for uniform scale: use the unmodified X-axis movement only if (epScene3D_->editMode == EDIT_SCALE && gizmoAxisX->selected && gizmoAxisY->selected && gizmoAxisZ->selected) { float x = gizmoAxisX->t - gizmoAxisX->lastT; adjust = Vector3(x, x, x); } moved = epScene3D_->ScaleNodes(adjust); } if (moved) { GizmoMoved(); // UpdateNodeAttributes(); // needGizmoUndo = true; } } else { // if (previousGizmoDrag) // StoreGizmoEditActions(); } /* previousGizmoDrag = drag;*/ }
void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData) { // Visualize the currently selected nodes if (selectedNode_.NotNull()) { DrawNodeDebug(selectedNode_, debugRenderer_); } if (!MouseInView()) return; Input* input = GetSubsystem<Input>(); mouseLeftDown_ = false; if (input->GetMouseButtonPress(MOUSEB_LEFT)) { if (!mouseMoved_ && !sceneEditor_->GetGizmo()->Selected()) { Ray camRay = GetCameraRay(); PODVector<RayQueryResult> result; RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff); octree_->RaycastSingle(query); if (query.result_.Size()) { const RayQueryResult& r = result[0]; if (r.drawable_) { VariantMap neventData; neventData[EditorActiveNodeChange::P_NODE] = r.drawable_->GetNode(); SendEvent(E_EDITORACTIVENODECHANGE, neventData); } } } mouseMoved_ = false; } else if (!input->GetMouseButtonDown(MOUSEB_LEFT)) { Ray camRay = GetCameraRay(); PODVector<RayQueryResult> result; mouseMoved_ = false; /* Array<int> pickModeDrawableFlags = { DRAWABLE_GEOMETRY, DRAWABLE_LIGHT, DRAWABLE_ZONE }; */ RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff); octree_->RaycastSingle(query); if (query.result_.Size()) { const RayQueryResult& r = result[0]; if (r.drawable_) { debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false); r.drawable_->DrawDebugGeometry(debugRenderer_, false); } } } else { mouseLeftDown_ = true; if (Abs(input->GetMouseMoveX() > 3 || input->GetMouseMoveY() > 3)) { mouseMoved_ = true; } } }