Skip to content

Commit

Permalink
Implement possible fix for #28
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuston-0 committed Jul 16, 2021
1 parent dc8795c commit bd0fa2c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/opengl/GLWin.hh
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,9 @@ Shape* pick(int x, int y, Shape*); // click on (x,y), get Shape behind
void (GLWin::*callback)());
uint32_t registerCallback(uint32_t input, const char name[], Security s,
std::function<void()> action);
template <typename T>
template <typename T, typename U>
uint32_t registerCallback(uint32_t input, const char name[], Security s,
void (T::*callback)(), T* ptr) {
void (T::*callback)(), U* ptr) {
auto cb_funcptr = std::bind(callback, ptr);
return registerCallback(input, name, s, cb_funcptr);
}
Expand Down
15 changes: 8 additions & 7 deletions src/opengl/MapView2D.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Style;
class MapView2D : public Shape {
private:
const Style* style;
float centerX, centerY, scaleX, scaleY;
float centerX, centerY, shiftX, shiftY, scaleX, scaleY;
glm::mat4 originalTransform;
glm::mat4 transform;
// pointer to map loader object has advantage of opaqueness in header file
Expand All @@ -22,6 +22,7 @@ class MapView2D : public Shape {
void setProjection() {
transform = glm::ortho(centerX - scaleX, centerX + scaleX, centerY - scaleY,
centerY + scaleY);
getWin()->setDirty();
}
void translate(float percentX, float percentY) {
centerX += percentX * scaleX;
Expand All @@ -31,15 +32,15 @@ class MapView2D : public Shape {
MapView2D(Canvas* parent, const Style* s, BlockMapLoader* bml = nullptr)
: Shape(parent), style(s), bml(bml), transform(1.0f) {
const BlockMapLoader::BoundRect& bounds = bml->getBlockMapHeader()->bounds;
float centerX = (bounds.xMin + bounds.xMax) * 0.5;
float centerY = (bounds.yMin + bounds.yMax) * 0.5;
centerX = (bounds.xMin + bounds.xMax) * 0.5;
centerY = (bounds.yMin + bounds.yMax) * 0.5;

double ySize = parent->getHeight();
double xSize = parent->getWidth();
double shiftX = -bounds.xMin * xSize / (bounds.xMax - bounds.xMin);
double shiftY = ySize + (bounds.yMin * ySize / (bounds.yMax - bounds.yMin));
double scaleX = xSize / (bounds.xMax - bounds.xMin);
double scaleY = -ySize / (bounds.yMax - bounds.yMin);
shiftX = -bounds.xMin * xSize / (bounds.xMax - bounds.xMin);
shiftY = ySize + (bounds.yMin * ySize / (bounds.yMax - bounds.yMin));
scaleX = xSize / (bounds.xMax - bounds.xMin);
scaleY = -ySize / (bounds.yMax - bounds.yMin);

std::cout << "shift: " << shiftX << " " << shiftY << "\n";
std::cout << "scale: " << scaleX << " " << scaleY << "\n";
Expand Down
2 changes: 2 additions & 0 deletions src/opengl/Shape.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "opengl/Shape.hh"
#include "opengl/Canvas.hh"

Shape::~Shape() {}
GLWin* Shape::getWin() const { return parentCanvas->getWin(); }
8 changes: 8 additions & 0 deletions src/opengl/Shape.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace std {

class Inputs;
class Canvas;
class GLWin;
class Shape {
protected:
uint32_t vao, vbo, sbo, lbo, pbo, cbo;
Expand All @@ -21,6 +22,13 @@ class Shape {
public:
Shape(Canvas* parent) : parentCanvas(parent) {}
virtual ~Shape();

/**
* @brief Get a pointer to the GLWin object
*
* @return GLWin* Window containing the Shape
*/
GLWin* getWin() const;
#if 0
void setParentCanvas(Canvas *c) {
parentCanvas = c;
Expand Down
2 changes: 2 additions & 0 deletions test/SolarSystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class SolarSystem : public GLWin {
bindEvent(Inputs::LARROW, &SolarSystem::panLeft, this);
bindEvent(Inputs::UPARROW, &SolarSystem::panForward, this);
bindEvent(Inputs::DOWNARROW, &SolarSystem::panBack, this);
bindEvent(Inputs::INSERT, &SolarSystem::speedTime, this);
bindEvent(Inputs::DEL, &SolarSystem::slowTime, this);
}
void update() {
Canvas* c = currentTab()->getMainCanvas();
Expand Down

0 comments on commit bd0fa2c

Please sign in to comment.