void GizmoScene3D::UseGizmo() { if (gizmo == NULL || !gizmo->IsEnabled() || epScene3D_->editMode == EDIT_SELECT) { // StoreGizmoEditActions(); // previousGizmoDrag = false; return; } Urho3D::UI* ui = GetSubsystem<Urho3D::UI>(); Urho3D::IntVector2 pos = ui->GetCursorPosition(); Urho3D::UIElement* e = ui->GetElementAt(pos); if (e != epScene3D_->activeView) return; const Urho3D::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()); Urho3D::Ray cameraRay = epScene3D_->camera_->GetScreenRay(posx, posy); float scale = gizmoNode->GetScale().x_; Urho3D::Input* input = GetSubsystem<Urho3D::Input>(); // Recalculate axes only when not left-dragging bool drag = input->GetMouseButtonDown(Urho3D::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()); Urho3D::ResourceCache* cache = GetSubsystem<Urho3D::ResourceCache>(); if (gizmoAxisX->selected != gizmoAxisX->lastSelected) { gizmo->SetMaterial(0, cache->GetResource<Urho3D::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<Urho3D::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<Urho3D::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) { Urho3D::Vector3 adjust(0, 0, 0); if (gizmoAxisX->selected) adjust += Urho3D::Vector3(1, 0, 0) * (gizmoAxisX->t - gizmoAxisX->lastT); if (gizmoAxisY->selected) adjust += Urho3D::Vector3(0, 1, 0) * (gizmoAxisY->t - gizmoAxisY->lastT); if (gizmoAxisZ->selected) adjust += Urho3D::Vector3(0, 0, 1) * (gizmoAxisZ->t - gizmoAxisZ->lastT); moved = epScene3D_->MoveNodes(adjust); } else if (epScene3D_->editMode == EDIT_ROTATE) { Urho3D::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) { Urho3D::Vector3 adjust(0, 0, 0); if (gizmoAxisX->selected) adjust += Urho3D::Vector3(1, 0, 0) * (gizmoAxisX->t - gizmoAxisX->lastT); if (gizmoAxisY->selected) adjust += Urho3D::Vector3(0, 1, 0) * (gizmoAxisY->t - gizmoAxisY->lastT); if (gizmoAxisZ->selected) adjust += Urho3D::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 = Urho3D::Vector3(x, x, x); } moved = epScene3D_->ScaleNodes(adjust); } if (moved) { GizmoMoved(); // UpdateNodeAttributes(); // needGizmoUndo = true; } } else { // if (previousGizmoDrag) // StoreGizmoEditActions(); } /* previousGizmoDrag = drag;*/ }
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(); }