Skip to content

Commit

Permalink
zoom around center of the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
counter185 committed May 16, 2024
1 parent f469c83 commit 6a35cab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
19 changes: 17 additions & 2 deletions freesprite/maineditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,22 @@ bool MainEditor::requestSafeClose() {
return false;
}

void MainEditor::zoom(int how_much)
{
XY screenCenterPoint = XY{
(canvasCenterPoint.x - g_windowW/2) / -scale,
(canvasCenterPoint.y - g_windowH/2) / -scale
};
scale += how_much;
scale = scale < 1 ? 1 : scale;
XY onscreenPointNow = XY{
canvasCenterPoint.x + screenCenterPoint.x * scale,
canvasCenterPoint.y + screenCenterPoint.y * scale
};
XY pointDiff = xySubtract(XY{ g_windowW / 2, g_windowH / 2 }, onscreenPointNow);
canvasCenterPoint = xyAdd(canvasCenterPoint, pointDiff);
}

void MainEditor::takeInput(SDL_Event evt) {

if (evt.type == SDL_MOUSEBUTTONDOWN && evt.button.state) {
Expand Down Expand Up @@ -322,8 +338,7 @@ void MainEditor::takeInput(SDL_Event evt) {
}
break;
case SDL_MOUSEWHEEL:
scale += evt.wheel.y;
scale = scale < 1 ? 1 : scale;
zoom(evt.wheel.y);
break;
case SDL_KEYDOWN:
switch (evt.key.keysym.sym) {
Expand Down
1 change: 1 addition & 0 deletions freesprite/maineditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class MainEditor : public BaseScreen, public EventCallbackListener
void trySaveAsImage();
void recenterCanvas();
bool requestSafeClose();
void zoom(int how_much);

void checkAndDiscardEndOfUndoStack();
void commitStateToCurrentLayer();
Expand Down

0 comments on commit 6a35cab

Please sign in to comment.