Skip to content

Commit 6c8efef

Browse files
committed
Proper mouse scale.
1 parent 53d5de3 commit 6c8efef

1 file changed

Lines changed: 49 additions & 21 deletions

File tree

Examples/ExamplePaint/ExamplePaint.cpp

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,45 @@ static void textSymmetryMirrorFlags(bbe::SymmetryMode mode, size_t symIndex, boo
165165
}
166166
}
167167

168-
static void runPaintEditorUpdate(PaintEditor &editor, bbe::Game &g, float timeSinceLastFrame)
168+
static float getPaintContentScale(bbe::Game &g)
169+
{
170+
bbe::Window *window = g.getWindow();
171+
if (window == nullptr) return 1.f;
172+
const float scale = window->getScale();
173+
return scale > 0.f ? scale : 1.f;
174+
}
175+
176+
static PaintWindowMetrics getPaintWindowMetrics(bbe::Game &g)
169177
{
178+
const float contentScale = getPaintContentScale(g);
170179
PaintWindowMetrics w{};
171-
w.width = g.getWindowWidth();
172-
w.height = g.getWindowHeight();
173-
w.scale = g.getWindow()->getDpiScale();
180+
w.width = (int32_t)std::round((float)g.getScaledWindowWidth() / contentScale);
181+
w.height = (int32_t)std::round((float)g.getScaledWindowHeight() / contentScale);
182+
if (w.width <= 0) w.width = g.getWindowWidth();
183+
if (w.height <= 0) w.height = g.getWindowHeight();
184+
bbe::Window *window = g.getWindow();
185+
w.scale = window != nullptr ? window->getDpiScale() : 1.f;
186+
return w;
187+
}
188+
189+
static bbe::Vector2 toPaintScreenPos(const PaintWindowMetrics &w, bbe::Game &g, const bbe::Vector2 &rawPos)
190+
{
191+
const float sx = w.width > 0 ? (float)g.getWindowWidth() / (float)w.width : 1.f;
192+
const float sy = w.height > 0 ? (float)g.getWindowHeight() / (float)w.height : 1.f;
193+
return {
194+
sx > 0.f ? rawPos.x / sx : rawPos.x,
195+
sy > 0.f ? rawPos.y / sy : rawPos.y
196+
};
197+
}
198+
199+
static void runPaintEditorUpdate(PaintEditor &editor, bbe::Game &g, float timeSinceLastFrame)
200+
{
201+
PaintWindowMetrics w = getPaintWindowMetrics(g);
174202
editor.setViewportMetrics(w);
203+
const bbe::Vector2 rawMousePos = g.getMouse();
204+
const bbe::Vector2 mouseScreenPos = toPaintScreenPos(w, g, rawMousePos);
205+
const bbe::Vector2 prevMouseScreenPos = toPaintScreenPos(w, g, g.getMousePrevious());
206+
const bbe::Vector2 mouseScreenDelta = mouseScreenPos - prevMouseScreenPos;
175207

176208
bbe::Vector2 currMousePos{};
177209
bool drawMode = false;
@@ -235,7 +267,7 @@ static void runPaintEditorUpdate(PaintEditor &editor, bbe::Game &g, float timeSi
235267
discardTransientWorkArea();
236268
}
237269

238-
const bbe::Vector2 prevMousePos = editor.screenToCanvas(g.getMousePrevious());
270+
const bbe::Vector2 prevMousePos = editor.screenToCanvas(prevMouseScreenPos);
239271
const int32_t modeBeforeInput = editor.mode;
240272
bool refreshRectangleDraft = false;
241273
if (g.isKeyPressed(bbe::Key::SPACE))
@@ -245,7 +277,7 @@ static void runPaintEditorUpdate(PaintEditor &editor, bbe::Game &g, float timeSi
245277
if (g.isKeyPressed(bbe::Key::F1) && editor.symmetryMode != bbe::SymmetryMode::None)
246278
{
247279
editor.symmetryOffsetCustom = true;
248-
editor.symmetryOffset = editor.screenToCanvas(g.getMouse());
280+
editor.symmetryOffset = editor.screenToCanvas(mouseScreenPos);
249281
}
250282

251283
constexpr float CAM_WASD_SPEED = 400;
@@ -268,7 +300,7 @@ static void runPaintEditorUpdate(PaintEditor &editor, bbe::Game &g, float timeSi
268300

269301
if (g.isMouseDown(bbe::MouseButton::MIDDLE))
270302
{
271-
editor.offset += g.getMouseDelta();
303+
editor.offset += mouseScreenDelta;
272304
if (editor.tiled)
273305
{
274306
if (editor.offset.x < 0) editor.offset.x += editor.getCanvasWidth() * editor.zoomLevel;
@@ -280,13 +312,13 @@ static void runPaintEditorUpdate(PaintEditor &editor, bbe::Game &g, float timeSi
280312

281313
if (g.getMouseScrollY() < 0)
282314
{
283-
editor.changeZoom(1.0f / 1.1f, g.getMouse());
315+
editor.changeZoom(1.0f / 1.1f, mouseScreenPos);
284316
}
285317
else if (g.getMouseScrollY() > 0)
286318
{
287-
editor.changeZoom(1.1f, g.getMouse());
319+
editor.changeZoom(1.1f, mouseScreenPos);
288320
}
289-
currMousePos = editor.screenToCanvas(g.getMouse());
321+
currMousePos = editor.screenToCanvas(mouseScreenPos);
290322

291323
if (!ctrlDown)
292324
{
@@ -497,12 +529,12 @@ static void runPaintEditorUpdate(PaintEditor &editor, bbe::Game &g, float timeSi
497529

498530
if (!mouseOnNavigator && (g.isMousePressed(bbe::MouseButton::LEFT) || g.isMousePressed(bbe::MouseButton::RIGHT)))
499531
{
500-
editor.startMousePos = editor.screenToCanvas(g.getMouse());
532+
editor.startMousePos = editor.screenToCanvas(mouseScreenPos);
501533
}
502534

503535
if (!mouseOnNavigator && !editor.canvasResizeActive && g.isMousePressed(bbe::MouseButton::LEFT))
504536
{
505-
const int32_t hitHandle = editor.getCanvasResizeHitHandle(g.getMouse());
537+
const int32_t hitHandle = editor.getCanvasResizeHitHandle(mouseScreenPos);
506538
if (hitHandle >= 0)
507539
{
508540
editor.canvasResizeActive = true;
@@ -628,7 +660,7 @@ static void runPaintEditorUpdate(PaintEditor &editor, bbe::Game &g, float timeSi
628660
const bool selectionTransformClick = editor.selection.moveActive || editor.selection.resizeActive || editor.selection.rotationHandleActive;
629661
if (!selectionTransformClick && !editor.consumeMagicWandSuppressedPick())
630662
{
631-
bbe::Vector2 pos = editor.screenToCanvas(g.getMouse());
663+
bbe::Vector2 pos = editor.screenToCanvas(mouseScreenPos);
632664
if (editor.toTiledPos(pos))
633665
{
634666
editor.applyMagicWandAt(editor.toCanvasPixel(pos), ctrlDown);
@@ -831,7 +863,7 @@ static void runPaintEditorUpdate(PaintEditor &editor, bbe::Game &g, float timeSi
831863
{
832864
const bool leftDown = g.isMouseDown(bbe::MouseButton::LEFT) && !editor.suppressCanvasInputUntilMouseUp;
833865
const bool rightDown = g.isMouseDown(bbe::MouseButton::RIGHT) && !editor.suppressCanvasInputUntilMouseUp;
834-
bbe::Vector2 pos = editor.screenToCanvas(g.getMouse());
866+
bbe::Vector2 pos = editor.screenToCanvas(mouseScreenPos);
835867
if (editor.toTiledPos(pos))
836868
{
837869
const auto symPositions = editor.getSymmetryPositions(pos);
@@ -862,7 +894,7 @@ static void runPaintEditorUpdate(PaintEditor &editor, bbe::Game &g, float timeSi
862894
}
863895
else if (editor.mode == PaintEditor::MODE_PIPETTE)
864896
{
865-
auto pos = editor.screenToCanvas(g.getMouse());
897+
auto pos = editor.screenToCanvas(mouseScreenPos);
866898
if (editor.toTiledPos(pos))
867899
{
868900
const size_t x = (size_t)pos.x;
@@ -1029,11 +1061,7 @@ class MyGame : public bbe::Game
10291061
colorHistoryPersist.writeToFile();
10301062
};
10311063

1032-
PaintWindowMetrics w{};
1033-
w.width = getWindowWidth();
1034-
w.height = getWindowHeight();
1035-
w.scale = getWindow()->getDpiScale();
1036-
editor.onStart(w);
1064+
editor.onStart(getPaintWindowMetrics(*this));
10371065

10381066
if (!initialDocumentPath.empty())
10391067
{
@@ -1087,7 +1115,7 @@ class MyGame : public bbe::Game
10871115

10881116
void draw2D(bbe::PrimitiveBrush2D &brush) override
10891117
{
1090-
drawExamplePaintGui(editor, brush, getMouse());
1118+
drawExamplePaintGui(editor, brush, toPaintScreenPos(editor.viewport, *this, getMouse()));
10911119
}
10921120

10931121
void onEnd() override

0 commit comments

Comments
 (0)