Skip to content

Commit

Permalink
simulator fix tools with zoom etc
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Sep 18, 2024
1 parent 49b60e5 commit 78119a2
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 26 deletions.
24 changes: 20 additions & 4 deletions src/sim/Simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ void CSimulator::setTool(Tool_Base *tb) {
}
Coord camera(0, 0);
float zoomFactor = 1.0f;

Coord GetMousePosWorld() {
Coord r;
int mx, my;
//SDL_GetGlobalMouseState(&mx, &my);
SDL_GetMouseState(&mx, &my);
// No longer needed after resize event was introduced
// BUGFIX FOR MENUBAR OFFSET
//my += WINDOWS_MOUSE_MENUBAR_OFFSET;
r.set(mx, my);
float ndcX = (r.getX() / WinWidth);
float ndcY = (r.getY() / WinHeight);
r.setX(camera.getX() + (ndcX * WinWidth / (zoomFactor)));
r.setY(camera.getY() + (ndcY * WinHeight / (zoomFactor)));
return r;
}
void CSimulator::drawWindow() {
char buffer[256];
const char *projectPathDisp = projectPath.c_str();
Expand Down Expand Up @@ -102,7 +118,7 @@ void CSimulator::drawWindow() {
{
//int x = Event.button.x;
//int y = Event.button.y;
Coord mouse = GetMousePos();
Coord mouse = GetMousePosWorld();
int which = Event.button.button;
if (activeTool) {
activeTool->onMouseDown(mouse, which);
Expand All @@ -113,7 +129,7 @@ void CSimulator::drawWindow() {
{
//int x = Event.button.x;
//int y = Event.button.y;
Coord mouse = GetMousePos();
Coord mouse = GetMousePosWorld();
int which = Event.button.button;
if (activeTool) {
activeTool->onMouseUp(mouse, which);
Expand All @@ -138,7 +154,7 @@ void CSimulator::drawWindow() {
}
else if (Event.type == SDL_MOUSEWHEEL)
{
Coord mouse = GetMousePos();
Coord mouse = GetMousePosWorld();

Coord worldBeforeZoom = camera + (mouse / zoomFactor);

Expand Down Expand Up @@ -280,7 +296,7 @@ void CSimulator::destroyObject(CShape *s) {
sim->destroyObject(s);
}
class CShape *CSimulator::getShapeUnderCursor(bool bIncludeDeepText) {
Coord p = GetMousePos();
Coord p = GetMousePosWorld();
return sim->findShapeByBoundsPoint(p,bIncludeDeepText);
}
bool CSimulator::createSimulation(bool bDemo) {
Expand Down
2 changes: 1 addition & 1 deletion src/sim/Tool_Copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void Tool_Copy::onMouseDown(const Coord &pos, int button) {
}
void Tool_Copy::drawTool() {
sim->getCursorMgr()->setCursor(SDL_SYSTEM_CURSOR_ARROW);
Coord nowMousePos = GetMousePos();
Coord nowMousePos = GetMousePosWorld();
Coord delta = nowMousePos - prevMousePos;
if (copyingObject) {
copyingObject->translate(delta);
Expand Down
2 changes: 1 addition & 1 deletion src/sim/Tool_Info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void Tool_Info::drawTool() {
else {
sim->getCursorMgr()->setCursor(SDL_SYSTEM_CURSOR_ARROW);
}
Coord pos = GetMousePos();
Coord pos = GetMousePosWorld();
int h = pos.getY();
if (currentTarget) {
h = currentTarget->drawInformation2D(pos.getX(), h);
Expand Down
4 changes: 2 additions & 2 deletions src/sim/Tool_Move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void Tool_Move::onMouseDown(const Coord &pos, int button) {
if (button == SDL_BUTTON_LEFT) {
currentTarget = sim->getShapeUnderCursor();
if (currentTarget) {
prevPos = GetMousePos();
prevPos = GetMousePosWorld();
prevPos = roundToGrid(prevPos);
//sim->getCursorMgr()->setCursor(SDL_SYSTEM_CURSOR_SIZEALL);
}
Expand All @@ -53,7 +53,7 @@ void Tool_Move::drawTool() {
bMovingButtonHeld = sim->isMouseButtonHold(SDL_BUTTON_LEFT);
if (bMovingButtonHeld) {
if (currentTarget != 0) {
Coord nowPos = GetMousePos();
Coord nowPos = GetMousePosWorld();
nowPos = roundToGrid(nowPos);
Coord delta = nowPos - prevPos;
if (delta.isNonZero()) {
Expand Down
6 changes: 3 additions & 3 deletions src/sim/Tool_Use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

void Tool_Use::onMouseDown(const Coord &pos, int button) {
currentTarget = sim->getShapeUnderCursor();
prevPos = GetMousePos();
prevPos = GetMousePosWorld();

if (currentTarget != 0) {
Coord curPos = GetMousePos();
Coord curPos = GetMousePosWorld();
Coord delta = curPos - prevPos;
prevPos = curPos;
CControllerBase *cntrl = currentTarget->getController();
Expand Down Expand Up @@ -41,7 +41,7 @@ void Tool_Use::drawTool() {
}
}
if (currentTarget != 0) {
Coord curPos = GetMousePos();
Coord curPos = GetMousePosWorld();
Coord delta = curPos - prevPos;
prevPos = curPos;
CControllerBase *cntrl = currentTarget->getController();
Expand Down
4 changes: 2 additions & 2 deletions src/sim/Tool_Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void Tool_Wire::onMouseDown(const Coord &pos, int button) {
bSideness = !bSideness;
}
if (button == SDL_BUTTON_LEFT) {
Coord curPos = roundToGrid(GetMousePos());
Coord curPos = roundToGrid(GetMousePosWorld());
if (bActive) {
#if 1
sim->markAsModified();
Expand Down Expand Up @@ -62,7 +62,7 @@ void Tool_Wire::onMouseDown(const Coord &pos, int button) {
void Tool_Wire::drawTool() {
Coord m;
sim->getCursorMgr()->setCursor(SDL_SYSTEM_CURSOR_CROSSHAIR);
m = roundToGrid(GetMousePos());
m = roundToGrid(GetMousePosWorld());
if (bActive) {
if (0) {
glBegin(GL_LINES);
Expand Down
2 changes: 1 addition & 1 deletion src/sim/sim_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum {
int drawText(class CStyle *style, int x, int y, const char* fmt, ...);
#include "Coord.h"
Coord roundToGrid(Coord c);
Coord GetMousePos();
Coord GetMousePosWorld();
void FS_CreateDirectoriesForPath(const char *file_path);
char *FS_ReadTextFile(const char *fname);
bool FS_WriteTextFile(const char *data, const char *fname);
Expand Down
12 changes: 0 additions & 12 deletions src/sim/sim_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,6 @@ Coord roundToGrid(Coord c) {
return Coord(roundToGrid(c.getX()), roundToGrid(c.getY()));
}

Coord GetMousePos() {
Coord r;
int mx, my;
//SDL_GetGlobalMouseState(&mx, &my);
SDL_GetMouseState(&mx, &my);
// No longer needed after resize event was introduced
// BUGFIX FOR MENUBAR OFFSET
//my += WINDOWS_MOUSE_MENUBAR_OFFSET;
r.set(mx, my);
return r;
}



float CEdge::drawInformation2D(float x, float h) {
Expand Down

0 comments on commit 78119a2

Please sign in to comment.