zoomedit:
* Use signals to inform RenderArea of view changes
This commit is contained in:
parent
1d6f52984d
commit
36d892d1f0
10 changed files with 198 additions and 145 deletions
|
@ -30,22 +30,51 @@
|
|||
namespace ZoomEdit {
|
||||
namespace View {
|
||||
|
||||
void MapView::zoom(Gui::RenderArea*, int zoom, float, float) {
|
||||
if(!mainArea)
|
||||
void MapView::setMainView(TopView *mainView0) {
|
||||
mainView = mainView0;
|
||||
|
||||
if(mainViewUpdate)
|
||||
mainViewUpdate.disconnect();
|
||||
|
||||
if(mainView)
|
||||
mainViewUpdate = mainView->signalUpdate().connect(signalUpdate());
|
||||
}
|
||||
|
||||
void MapView::init() {
|
||||
glClearColor(0, 0, 0, 0);
|
||||
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
glEnable(GL_LINE_SMOOTH);
|
||||
glEnable(GL_POINT_SMOOTH);
|
||||
}
|
||||
|
||||
void MapView::resize(float width, float height) {
|
||||
if(width == viewWidth && height == viewHeight)
|
||||
return;
|
||||
|
||||
TopView *mainView = dynamic_cast<TopView*>(mainArea->getView());
|
||||
viewWidth = width; viewHeight = height;
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
if(width != 0 && height != 0)
|
||||
glScalef(2.0f/width, -2.0f/height, 1);
|
||||
|
||||
signalUpdate().emit();
|
||||
}
|
||||
|
||||
void MapView::zoom(int zoom, float, float) {
|
||||
if(!mainView)
|
||||
return;
|
||||
|
||||
mainView->zoom(mainArea, zoom, 0, 0);
|
||||
mainView->zoom(zoom, 0, 0);
|
||||
}
|
||||
|
||||
void MapView::move(Gui::RenderArea*, float x, float y, unsigned int state) {
|
||||
if(!mainArea)
|
||||
return;
|
||||
|
||||
TopView *mainView = dynamic_cast<TopView*>(mainArea->getView());
|
||||
void MapView::move(float x, float y, unsigned int state) {
|
||||
if(!mainView)
|
||||
return;
|
||||
|
||||
|
@ -54,28 +83,20 @@ void MapView::move(Gui::RenderArea*, float x, float y, unsigned int state) {
|
|||
|
||||
mainView->setXCenter(mainView->getXCenter() - x/scale);
|
||||
mainView->setYCenter(mainView->getYCenter() - y/scale);
|
||||
|
||||
mainArea->queue_draw();
|
||||
}
|
||||
|
||||
void MapView::click(Gui::RenderArea *renderArea, float x, float y) {
|
||||
if(!mainArea)
|
||||
return;
|
||||
|
||||
TopView *mainView = dynamic_cast<TopView*>(mainArea->getView());
|
||||
void MapView::click(float x, float y) {
|
||||
if(!mainView)
|
||||
return;
|
||||
|
||||
mainView->setXCenter(xCenter + (x - renderArea->get_width()/2)/scale);
|
||||
mainView->setYCenter(yCenter + (y - renderArea->get_height()/2)/scale);
|
||||
|
||||
mainArea->queue_draw();
|
||||
mainView->setXCenter(xCenter + (x - viewWidth/2)/scale);
|
||||
mainView->setYCenter(yCenter + (y - viewHeight/2)/scale);
|
||||
}
|
||||
|
||||
void MapView::render(Gui::RenderArea *renderArea) {
|
||||
void MapView::render() {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
if(!level)
|
||||
if(!mainView || !mainView->getLevel())
|
||||
return;
|
||||
|
||||
float minX, maxX, minY, maxY;
|
||||
|
@ -87,7 +108,7 @@ void MapView::render(Gui::RenderArea *renderArea) {
|
|||
if(width == 0 || height == 0)
|
||||
return;
|
||||
|
||||
scale = std::min(renderArea->get_width()/width, renderArea->get_height()/height)*0.75f;
|
||||
scale = std::min(viewWidth/width, viewHeight/height)*0.75f;
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPushMatrix();
|
||||
|
@ -95,44 +116,38 @@ void MapView::render(Gui::RenderArea *renderArea) {
|
|||
glScalef(scale, scale, 1);
|
||||
glTranslatef(-xCenter, -yCenter, 0);
|
||||
|
||||
const std::list<Data::Room*> &rooms = level->getRooms();
|
||||
const std::list<Data::Room*> &rooms = mainView->getLevel()->getRooms();
|
||||
|
||||
for(std::list<Data::Room*>::const_iterator room = rooms.begin(); room != rooms.end(); ++room)
|
||||
TopView::renderRoom(*room);
|
||||
|
||||
if(mainArea) {
|
||||
TopView *mainView = dynamic_cast<TopView*>(mainArea->getView());
|
||||
|
||||
if(mainView) {
|
||||
float mainXCenter = mainView->getXCenter(), mainYCenter = mainView->getYCenter();
|
||||
float mainScale = mainView->getScale();
|
||||
|
||||
float mainWidth = mainArea->get_width()/mainScale, mainHeight = mainArea->get_height()/mainScale;
|
||||
|
||||
glColor4f(0.7f, 0.7f, 0.7f, 0.3f);
|
||||
|
||||
glBegin(GL_POLYGON);
|
||||
|
||||
glVertex2f(mainXCenter-mainWidth/2, mainYCenter-mainHeight/2);
|
||||
glVertex2f(mainXCenter+mainWidth/2, mainYCenter-mainHeight/2);
|
||||
glVertex2f(mainXCenter+mainWidth/2, mainYCenter+mainHeight/2);
|
||||
glVertex2f(mainXCenter-mainWidth/2, mainYCenter+mainHeight/2);
|
||||
|
||||
glEnd();
|
||||
|
||||
glColor4f(0.7f, 0.7f, 0.7f, 0.7f);
|
||||
glLineWidth(1.0f);
|
||||
|
||||
glBegin(GL_LINE_LOOP);
|
||||
|
||||
glVertex2f(mainXCenter-mainWidth/2, mainYCenter-mainHeight/2);
|
||||
glVertex2f(mainXCenter+mainWidth/2, mainYCenter-mainHeight/2);
|
||||
glVertex2f(mainXCenter+mainWidth/2, mainYCenter+mainHeight/2);
|
||||
glVertex2f(mainXCenter-mainWidth/2, mainYCenter+mainHeight/2);
|
||||
|
||||
glEnd();
|
||||
}
|
||||
}
|
||||
float mainXCenter = mainView->getXCenter(), mainYCenter = mainView->getYCenter();
|
||||
float mainScale = mainView->getScale();
|
||||
|
||||
float mainWidth = mainView->getWidth()/mainScale, mainHeight = mainView->getHeight()/mainScale;
|
||||
|
||||
glColor4f(0.7f, 0.7f, 0.7f, 0.3f);
|
||||
|
||||
glBegin(GL_POLYGON);
|
||||
|
||||
glVertex2f(mainXCenter-mainWidth/2, mainYCenter-mainHeight/2);
|
||||
glVertex2f(mainXCenter+mainWidth/2, mainYCenter-mainHeight/2);
|
||||
glVertex2f(mainXCenter+mainWidth/2, mainYCenter+mainHeight/2);
|
||||
glVertex2f(mainXCenter-mainWidth/2, mainYCenter+mainHeight/2);
|
||||
|
||||
glEnd();
|
||||
|
||||
glColor4f(0.7f, 0.7f, 0.7f, 0.7f);
|
||||
glLineWidth(1.0f);
|
||||
|
||||
glBegin(GL_LINE_LOOP);
|
||||
|
||||
glVertex2f(mainXCenter-mainWidth/2, mainYCenter-mainHeight/2);
|
||||
glVertex2f(mainXCenter+mainWidth/2, mainYCenter-mainHeight/2);
|
||||
glVertex2f(mainXCenter+mainWidth/2, mainYCenter+mainHeight/2);
|
||||
glVertex2f(mainXCenter-mainWidth/2, mainYCenter+mainHeight/2);
|
||||
|
||||
glEnd();
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPopMatrix();
|
||||
|
@ -148,10 +163,10 @@ void MapView::getBounds(float *minX, float *maxX, float *minY, float *maxY) {
|
|||
if(maxY)
|
||||
*maxY = 0;
|
||||
|
||||
if(!level)
|
||||
if(!mainView || !mainView->getLevel())
|
||||
return;
|
||||
|
||||
const std::list<Data::Room*> &rooms = level->getRooms();
|
||||
const std::list<Data::Room*> &rooms = mainView->getLevel()->getRooms();
|
||||
|
||||
for(std::list<Data::Room*>::const_iterator room = rooms.begin(); room != rooms.end(); ++room) {
|
||||
const std::list<Data::Triangle*> &triangles = (*room)->getFloorTriangles();
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#define ZOOMEDIT_VIEW_MAPVIEW_H_
|
||||
|
||||
#include "View.h"
|
||||
#include <sigc++/connection.h>
|
||||
|
||||
namespace ZoomEdit {
|
||||
|
||||
|
@ -34,29 +35,33 @@ class TopView;
|
|||
|
||||
class MapView : public View {
|
||||
private:
|
||||
Data::Level *level;
|
||||
sigc::connection mainViewUpdate;
|
||||
|
||||
Gui::RenderArea *mainArea;
|
||||
float viewWidth, viewHeight;
|
||||
|
||||
TopView *mainView;
|
||||
|
||||
float scale, xCenter, yCenter;
|
||||
|
||||
void getBounds(float *minX, float *maxX, float *minY, float *maxY);
|
||||
|
||||
public:
|
||||
MapView(Gui::RenderArea *mainArea0 = 0, Data::Level *level0 = 0)
|
||||
: level(level0), mainArea(mainArea0), scale(1), xCenter(0), yCenter(0) {}
|
||||
MapView(TopView *mainView0 = 0)
|
||||
: mainView(0), scale(1), xCenter(0), yCenter(0) {
|
||||
setMainView(mainView0);
|
||||
}
|
||||
|
||||
Data::Level* getLevel() {return level;}
|
||||
void setLevel(Data::Level *level0) {level = level0;}
|
||||
TopView* getMainView() {return mainView;}
|
||||
void setMainView(TopView *mainView0);
|
||||
|
||||
Gui::RenderArea* getMainArea() {return mainArea;}
|
||||
void setMainArea(Gui::RenderArea *mainArea0) {mainArea = mainArea0;}
|
||||
virtual void init();
|
||||
virtual void resize(float width, float height);
|
||||
|
||||
virtual void zoom(Gui::RenderArea*, int zoom, float, float);
|
||||
virtual void move(Gui::RenderArea*, float x, float y, unsigned int state);
|
||||
virtual void click(Gui::RenderArea *renderArea, float x, float y);
|
||||
virtual void render();
|
||||
|
||||
virtual void render(Gui::RenderArea *renderArea);
|
||||
virtual void zoom(int zoom, float, float);
|
||||
virtual void move(float x, float y, unsigned int state);
|
||||
virtual void click(float x, float y);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -58,12 +58,12 @@ bool TopView::Edge::operator<(const Edge &e) const {
|
|||
return false;
|
||||
}
|
||||
|
||||
void TopView::drawGrid(Gui::RenderArea *renderArea) {
|
||||
void TopView::drawGrid() {
|
||||
float depth = 1.25f + 0.04f*zoomLevel;
|
||||
float depth2 = std::floor(depth);
|
||||
float step = std::pow(0.1f, depth2);
|
||||
float width = renderArea->get_width()/scale;
|
||||
float height = renderArea->get_height()/scale;
|
||||
float width = viewWidth/scale;
|
||||
float height = viewHeight/scale;
|
||||
|
||||
float x1 = xCenter - width/2;
|
||||
float x2 = xCenter + width/2;
|
||||
|
@ -140,7 +140,34 @@ void TopView::renderRoom(Data::Room *room) {
|
|||
glEnd();
|
||||
}
|
||||
|
||||
void TopView::zoom(Gui::RenderArea *renderArea, int zoom, float x, float y) {
|
||||
void TopView::init() {
|
||||
glClearColor(0, 0, 0, 0);
|
||||
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
glEnable(GL_LINE_SMOOTH);
|
||||
glEnable(GL_POINT_SMOOTH);
|
||||
}
|
||||
|
||||
void TopView::resize(float width, float height) {
|
||||
if(width == viewWidth && height == viewHeight)
|
||||
return;
|
||||
|
||||
viewWidth = width; viewHeight = height;
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
if(width != 0 && height != 0)
|
||||
glScalef(2.0f/width, -2.0f/height, 1);
|
||||
|
||||
signalUpdate().emit();
|
||||
}
|
||||
|
||||
void TopView::zoom(int zoom, float x, float y) {
|
||||
const float oldScale = scale;
|
||||
|
||||
zoomLevel = std::max(std::min(zoomLevel + zoom, 50), -100);
|
||||
|
@ -149,29 +176,29 @@ void TopView::zoom(Gui::RenderArea *renderArea, int zoom, float x, float y) {
|
|||
xCenter += x*(1/oldScale - 1/scale);
|
||||
yCenter += y*(1/oldScale - 1/scale);
|
||||
|
||||
renderArea->queue_draw();
|
||||
signalUpdate().emit();
|
||||
}
|
||||
|
||||
void TopView::move(Gui::RenderArea *renderArea, float x, float y, unsigned int state) {
|
||||
void TopView::move(float x, float y, unsigned int state) {
|
||||
if(!(state & GDK_BUTTON3_MASK))
|
||||
return;
|
||||
|
||||
xCenter += x/scale;
|
||||
yCenter += y/scale;
|
||||
|
||||
renderArea->queue_draw();
|
||||
signalUpdate().emit();
|
||||
}
|
||||
|
||||
void TopView::render(Gui::RenderArea *renderArea) {
|
||||
void TopView::render() {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
|
||||
glScalef(scale, scale, 1);
|
||||
glTranslatef(-xCenter, -yCenter, 0);
|
||||
|
||||
drawGrid(renderArea);
|
||||
drawGrid();
|
||||
|
||||
if(!level)
|
||||
return;
|
||||
|
@ -180,9 +207,6 @@ void TopView::render(Gui::RenderArea *renderArea) {
|
|||
|
||||
for(std::list<Data::Room*>::const_iterator room = rooms.begin(); room != rooms.end(); ++room)
|
||||
renderRoom(*room);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -45,30 +45,38 @@ class TopView : public View {
|
|||
|
||||
Data::Level *level;
|
||||
|
||||
float viewWidth, viewHeight;
|
||||
|
||||
float xCenter, yCenter;
|
||||
|
||||
int zoomLevel;
|
||||
float scale;
|
||||
|
||||
void drawGrid(Gui::RenderArea *renderArea);
|
||||
void drawGrid();
|
||||
|
||||
public:
|
||||
TopView(Data::Level *level0 = 0) : level(level0), xCenter(0), yCenter(0), zoomLevel(0), scale(100) {}
|
||||
TopView(Data::Level *level0 = 0) : level(level0), viewWidth(0), viewHeight(0), xCenter(0), yCenter(0), zoomLevel(0), scale(100) {}
|
||||
|
||||
float getWidth() const {return viewWidth;}
|
||||
float getHeight() const {return viewHeight;}
|
||||
|
||||
float getXCenter() const {return xCenter;}
|
||||
void setXCenter(float xCenter0) {xCenter = xCenter0;}
|
||||
void setXCenter(float xCenter0) {xCenter = xCenter0; signalUpdate().emit();}
|
||||
float getYCenter() const {return yCenter;}
|
||||
void setYCenter(float yCenter0) {yCenter = yCenter0;}
|
||||
void setYCenter(float yCenter0) {yCenter = yCenter0; signalUpdate().emit();}
|
||||
|
||||
float getScale() const {return scale;}
|
||||
|
||||
Data::Level* getLevel() {return level;}
|
||||
void setLevel(Data::Level *level0) {level = level0;}
|
||||
void setLevel(Data::Level *level0) {level = level0; signalUpdate().emit();}
|
||||
|
||||
virtual void zoom(Gui::RenderArea *renderArea, int zoom, float x, float y);
|
||||
virtual void move(Gui::RenderArea *renderArea, float x, float y, unsigned int state);
|
||||
virtual void init();
|
||||
virtual void resize(float width, float height);
|
||||
|
||||
virtual void render(Gui::RenderArea *renderArea);
|
||||
virtual void render();
|
||||
|
||||
virtual void zoom(int zoom, float x, float y);
|
||||
virtual void move(float x, float y, unsigned int state);
|
||||
|
||||
static void renderRoom(Data::Room *room);
|
||||
};
|
||||
|
|
|
@ -20,23 +20,28 @@
|
|||
#ifndef ZOOMEDIT_VIEW_VIEW_H_
|
||||
#define ZOOMEDIT_VIEW_VIEW_H_
|
||||
|
||||
#include <sigc++/signal.h>
|
||||
|
||||
namespace ZoomEdit {
|
||||
|
||||
namespace Gui {
|
||||
class RenderArea;
|
||||
}
|
||||
|
||||
namespace View {
|
||||
|
||||
class View {
|
||||
private:
|
||||
sigc::signal<void> update;
|
||||
|
||||
public:
|
||||
virtual ~View() {}
|
||||
|
||||
virtual void render(Gui::RenderArea *renderArea) = 0;
|
||||
virtual void init() = 0;
|
||||
virtual void resize(float width, float height) = 0;
|
||||
|
||||
virtual void zoom(Gui::RenderArea*, int, float, float) {}
|
||||
virtual void move(Gui::RenderArea*, float, float, unsigned int) {}
|
||||
virtual void click(Gui::RenderArea*, float, float) {}
|
||||
virtual void render() = 0;
|
||||
|
||||
virtual void zoom(int, float, float) {}
|
||||
virtual void move(float, float, unsigned int) {}
|
||||
virtual void click(float, float) {}
|
||||
|
||||
sigc::signal<void> signalUpdate() const {return update;}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue