Modules/3DEditor/private/EditorWidget.cpp
Шурупов Илья Викторович eadb717a26 Add CLion-style docking and use it in Sketch3D and 3DEditor
The old DockWidget let panels dock to a side, resize, undock and be dragged
to another side with a preview. The rewrite had replaced it with a
SplitView, so panels were fixed in place. This brings docking back as a
small tool-window system modeled on CLion.

DockArea surrounds a center widget with stripes that list its DockPanels;
clicking a stripe button toggles that panel. Each side (left, right,
bottom) shows one panel at a time. A panel has three view modes, switched
from its header:

- docked (pinned): takes its zone and pushes the center aside;
- undocked (unpinned): overlays the center and hides when the user
  activates anything outside it;
- float: a window inside the area, moved by its header and resized from
  its corner grip.

Docked panels resize by dragging their inner edge, clamped so the center
stays visible. Dragging a header detaches the panel; a translucent preview
marks the side it will land on when the pointer is over that edge, and
releasing there docks it and moves its stripe button. Releasing elsewhere
floats it.

Unpinned auto-hide needs to know when activity leaves a subtree, so the Ui
now tracks the last pressed widget. Its ancestors get containsActiveWidget()
and onActiveWithinChanged(), reusing the same path diff as hover.

Sketch3D now docks Tools (left) and Brush (right) around the viewport, with
the View menu toggling them. 3DEditor docks Navigation (left) and Render
(right). The gallery gains a Docking tab.

25 DockingTests and a gallery session cover stripes, one panel per side,
docked layout, edge resize and its limit, unpin and auto-hide, stripe
toggling of unpinned panels, drag with preview and drop on each side,
floating move and resize, and panel lifetime.
2026-10-14 16:05:29 +03:00

157 lines
4.7 KiB
C++

#include "EditorWidget.hpp"
#include "BoxLayout.hpp"
#include "Event.hpp"
#include "StackLayout.hpp"
#include "Ui.hpp"
using namespace tp;
using namespace tp::ui;
namespace {
constexpr halnf kOrbitSensitivity = 3;
constexpr halnf kSpinRadiansPerMs = 0.1f / 16;
}
EditorViewport::EditorViewport(Canvas& canvas, Editor& editor) :
mCanvas(canvas),
mEditor(editor),
mImage(canvas.createImageFromTextId(editor.getViewportTexID(), { 0, 0 })) {
setDebugName("editor viewport");
setSizing(AxisSizing::fill(), AxisSizing::fill());
}
EditorViewport::~EditorViewport() { mCanvas.deleteImageHandle(mImage); }
void EditorViewport::setSpinning(bool spinning) {
if (spinning) startTicking();
else stopTicking();
}
void EditorViewport::onEvent(Event& event) {
switch (event.type()) {
case Event::Type::PointerDown:
if (event.button() != PointerButton::Primary) return;
mDragging = true;
mPreviousPointer = event.position();
ui()->capturePointer(*this);
if (mNavigation == Navigation::Select) mEditor.selectObject(normalized(event.position()) * -1);
break;
case Event::Type::PointerMove:
if (!mDragging) return;
navigate(event.position(), event.delta());
mPreviousPointer = event.position();
break;
case Event::Type::PointerUp:
if (!mDragging) return;
mDragging = false;
ui()->releasePointer(*this);
break;
case Event::Type::Wheel: mEditor.navigationZoom(1 - event.wheelAmount() * 0.1f); break;
default: return;
}
event.accept();
requestPaint();
}
void EditorViewport::onTick(halnf deltaMs) {
Object* active = mEditor.getActiveObject();
if (!active) return;
auto rotator = decltype(active->mTopology.Basis)::rotatorDir({ 1, 1, 1 }, kSpinRadiansPerMs * deltaMs);
active->mTopology.Basis = rotator * active->mTopology.Basis;
mEditor.getScene()->updateCache();
requestPaint();
}
void EditorViewport::paint(Painter& painter) {
mEditor.setViewportSize(size());
mEditor.renderViewport();
mCanvas.updateTextureID(mImage, mEditor.getViewportTexID());
painter.image(localRect(), { mImage.id });
}
Vec2F EditorViewport::normalized(Vec2F local) const { return ((local / size()) - 0.5f) * 2.0f; }
void EditorViewport::navigate(Vec2F local, Vec2F delta) {
switch (mNavigation) {
case Navigation::Orbit: mEditor.navigationOrbit(delta / size() * kOrbitSensitivity); break;
case Navigation::Pan: mEditor.navigationPan(normalized(mPreviousPointer), normalized(local)); break;
case Navigation::Zoom: mEditor.navigationZoom(1 + delta.y / size().y); break;
case Navigation::Select: mEditor.selectObject(normalized(local) * -1); break;
}
}
EditorWidget::EditorWidget(Canvas& canvas, Editor& editor) :
Panel(Surface::Window),
mEditor(editor),
mViewport(canvas, editor) {
setDebugName("editor");
setSizing(AxisSizing::fill(), AxisSizing::fill());
emplaceLayout<StackLayout>();
buildRenderPanel();
buildNavigationPanel();
mDock.setCenter(&mViewport);
mDock.addPanel(mNavigationPanel);
mDock.addPanel(mRenderPanel);
mDock.openPanel(mRenderPanel);
addChild(mDock);
}
void EditorWidget::buildRenderPanel() {
mRenderType.add(mRaster);
mRenderType.add(mPathTracer);
mRenderType.selectIndex(0);
mRenderType.setOnChanged([this](size_t index) {
if (index == 1) mEditor.renderPathFrame();
mEditor.setRenderType(index == 1 ? Editor::RenderType::PATH_TRACER : Editor::RenderType::RASTER);
mViewport.requestPaint();
});
mDenoise.setOnActivate([this]() {
mEditor.denoisePathRenderBuffers();
mViewport.requestPaint();
});
mRenderContent.emplaceLayout<ColumnLayout>(6).setCrossAlign(CrossAlign::Start);
for (Widget* item : std::initializer_list<Widget*>{ &mRaster, &mPathTracer, &mDenoise })
mRenderContent.addChild(*item);
mRenderPanel.setContent(&mRenderContent);
mRenderPanel.setDockedLength(260);
}
void EditorWidget::buildNavigationPanel() {
const EditorViewport::Navigation modes[] = { EditorViewport::Navigation::Orbit,
EditorViewport::Navigation::Pan,
EditorViewport::Navigation::Zoom,
EditorViewport::Navigation::Select };
for (RadioButton* option : { &mOrbit, &mPan, &mZoom, &mSelect })
mNavigation.add(*option);
mNavigation.selectIndex(0);
mNavigation.setOnChanged([this, modes](size_t index) { mViewport.setNavigation(modes[index]); });
mReset.setOnActivate([this]() {
mEditor.navigationReset();
mViewport.requestPaint();
});
mSpin.setOnChanged([this](bool spinning) { mViewport.setSpinning(spinning); });
mSpin.setChecked(true);
mNavigationContent.emplaceLayout<ColumnLayout>(6).setCrossAlign(CrossAlign::Start);
for (Widget* item : std::initializer_list<Widget*>{ &mOrbit, &mPan, &mZoom, &mSelect, &mReset, &mSpin }) {
mNavigationContent.addChild(*item);
}
mNavigationPanel.setContent(&mNavigationContent);
mNavigationPanel.setDockedLength(200);
}