Skip to content

Commit

Permalink
simulator: zoom p1, test
Browse files Browse the repository at this point in the history
  • Loading branch information
openshwprojects committed Sep 18, 2024
1 parent 04e51e2 commit ba3690a
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/sim/Simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ void CSimulator::setTool(Tool_Base *tb) {
activeTool->setSimulator(this);
activeTool->onBegin();
}

float cameraX = 0.0f;
float cameraY = 0.0f;
float zoomFactor = 1.0f;
void CSimulator::drawWindow() {
char buffer[256];
const char *projectPathDisp = projectPath.c_str();
Expand Down Expand Up @@ -88,6 +90,12 @@ void CSimulator::drawWindow() {
}
}
else {
switch (Event.key.keysym.sym) {
case SDLK_LEFT: cameraX -= 10.0f; break;
case SDLK_RIGHT: cameraX += 10.0f; break;
case SDLK_UP: cameraY -= 10.0f; break;
case SDLK_DOWN: cameraY += 10.0f; break;
}
onKeyDown(Event.key.keysym.sym);
}
}
Expand Down Expand Up @@ -129,6 +137,26 @@ void CSimulator::drawWindow() {
}
}
}
else if (Event.type == SDL_MOUSEWHEEL)
{
Coord mouse = GetMousePos();

float worldXBeforeZoom = cameraX + (mouse.getX() / zoomFactor);
float worldYBeforeZoom = cameraY + (mouse.getY() / zoomFactor);

if (Event.wheel.y > 0) {
zoomFactor *= 1.1f;
}
else if (Event.wheel.y < 0) {
zoomFactor /= 1.1f;
}

float worldXAfterZoom = cameraX + (mouse.getX() / zoomFactor);
float worldYAfterZoom = cameraY + (mouse.getY() / zoomFactor);

cameraX += (worldXBeforeZoom - worldXAfterZoom);
cameraY += (worldYBeforeZoom - worldYAfterZoom);
}
else if (Event.type == SDL_QUIT)
{
Running = 0;
Expand Down Expand Up @@ -156,7 +184,8 @@ void CSimulator::drawWindow() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, WinWidth, WinHeight, 0.0f, 0.0f, 1.0f);
glOrtho(0, WinWidth, WinHeight, 0, 0.0f, 1.0f);


int h = 40;
h = drawText(NULL, 10, h, "OpenBeken Simulator");
Expand All @@ -173,6 +202,10 @@ void CSimulator::drawWindow() {
glColor3f(1.0f, 0.0f, 0.0f);
drawText(&g_style_text_red, 260, 40, "WARNING: The following sketch may not be a correct circuit schematic. Connections in this simulator are simplified.");

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(cameraX, cameraX + WinWidth / zoomFactor, cameraY + WinHeight / zoomFactor, cameraY, 0.0f, 1.0f);

glColor3f(0.7f, 0.7f, 0.7f);
glLineWidth(0.25f);
glBegin(GL_LINES);
Expand Down

0 comments on commit ba3690a

Please sign in to comment.