254 lines
6.1 KiB
C++
254 lines
6.1 KiB
C++
/*
|
|
* TopView.cpp
|
|
*
|
|
* Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "TopView.h"
|
|
#include <Data/Level.h>
|
|
#include <Data/Room.h>
|
|
#include <Data/Triangle.h>
|
|
#include <Gui/RenderArea.h>
|
|
#include <Math/Triangle2D.h>
|
|
#include <GL/gl.h>
|
|
#include <cmath>
|
|
#include <set>
|
|
|
|
namespace ZoomEdit {
|
|
namespace View {
|
|
|
|
bool TopView::Edge::operator<(const Edge &e) const {
|
|
if(v1->getX() < e.v1->getX())
|
|
return true;
|
|
if(v1->getX() > e.v1->getX())
|
|
return false;
|
|
if(v1->getY() < e.v1->getY())
|
|
return true;
|
|
if(v1->getY() > e.v1->getY())
|
|
return false;
|
|
if(v1->getZ() < e.v1->getZ())
|
|
return true;
|
|
if(v1->getZ() > e.v1->getZ())
|
|
return false;
|
|
if(v2->getX() < e.v2->getX())
|
|
return true;
|
|
if(v2->getX() > e.v2->getX())
|
|
return false;
|
|
if(v2->getY() < e.v2->getY())
|
|
return true;
|
|
if(v2->getY() > e.v2->getY())
|
|
return false;
|
|
if(v2->getZ() < e.v2->getZ())
|
|
return true;
|
|
if(v2->getZ() > e.v2->getZ())
|
|
return false;
|
|
|
|
return false;
|
|
}
|
|
|
|
void TopView::drawGrid() {
|
|
float depth = 1.25f + 0.04f*zoomLevel;
|
|
float depth2 = std::floor(depth);
|
|
float step = std::pow(0.1f, depth2);
|
|
float width = viewWidth/scale;
|
|
float height = viewHeight/scale;
|
|
|
|
float x1 = xCenter - width/2;
|
|
float x2 = xCenter + width/2;
|
|
float y1 = yCenter - height/2;
|
|
float y2 = yCenter + height/2;
|
|
|
|
glLineWidth(1.0f);
|
|
|
|
glBegin(GL_LINES);
|
|
|
|
for(int i = 0; 0.4f*(depth-depth2+i-1) < 0.5f; ++i) {
|
|
float f = std::min(0.4f*(depth-depth2+i), 0.5f);
|
|
glColor3f(f, f, f);
|
|
|
|
for(f = x1 - std::fmod(x1, step); f <= x2; f+=step) {
|
|
glVertex2f(f, y1);
|
|
glVertex2f(f, y2);
|
|
}
|
|
|
|
for(f = y1 - std::fmod(y1, step); f <= y2; f+=step) {
|
|
glVertex2f(x1, f);
|
|
glVertex2f(x2, f);
|
|
}
|
|
|
|
step *= 10;
|
|
}
|
|
|
|
glEnd();
|
|
}
|
|
|
|
void TopView::renderRoom(Data::Room *room, bool selected) {
|
|
const std::list<Data::Triangle*> &floor = room->getFloorTriangles();
|
|
std::multiset<Edge> edges;
|
|
|
|
if(selected)
|
|
glColor4f(0.0f, 0.7f, 1.0f, 0.2f);
|
|
else
|
|
glColor4f(0.0f, 0.7f, 1.0f, 0.3f);
|
|
|
|
glBegin(GL_TRIANGLES);
|
|
|
|
for(std::list<Data::Triangle*>::const_iterator t = floor.begin(); t != floor.end(); ++t) {
|
|
for(int i = 0; i < 3; ++i) {
|
|
const Data::Vertex &v = (*t)->getVertex(i);
|
|
const Data::Vertex &v2 = (*t)->getVertex((i+1)%3);
|
|
|
|
glVertex2f(v.getX(), v.getZ());
|
|
|
|
edges.insert(Edge(&v, &v2));
|
|
}
|
|
}
|
|
|
|
glEnd();
|
|
|
|
for(std::multiset<Edge>::iterator next = edges.begin(); next != edges.end();) {
|
|
std::multiset<Edge>::iterator edge = next++;
|
|
std::multiset<Edge>::iterator edge2 = edges.find(Edge(edge->v2, edge->v1));
|
|
|
|
if(edge2 != edges.end()) {
|
|
edges.erase(edge2);
|
|
next = edge;
|
|
++next;
|
|
edges.erase(edge);
|
|
}
|
|
}
|
|
|
|
if(selected) {
|
|
glColor4f(1.0f, 1.0f, 1.0f, 0.9f);
|
|
glLineWidth(2.0f);
|
|
}
|
|
else {
|
|
glColor4f(0.0f, 0.7f, 1.0f, 0.7f);
|
|
glLineWidth(1.0f);
|
|
}
|
|
|
|
glBegin(GL_LINES);
|
|
|
|
for(std::multiset<Edge>::iterator edge = edges.begin(); edge != edges.end(); ++edge) {
|
|
glVertex2f(edge->v1->getX(), edge->v1->getZ());
|
|
glVertex2f(edge->v2->getX(), edge->v2->getZ());
|
|
}
|
|
|
|
glEnd();
|
|
}
|
|
|
|
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);
|
|
scale = 100*std::pow(1.1f, zoomLevel);
|
|
|
|
xCenter += x*(1/oldScale - 1/scale);
|
|
yCenter += y*(1/oldScale - 1/scale);
|
|
|
|
signalUpdate().emit();
|
|
}
|
|
|
|
void TopView::move(float x, float y, unsigned int state) {
|
|
if(!(state & GDK_BUTTON3_MASK))
|
|
return;
|
|
|
|
xCenter += x/scale;
|
|
yCenter += y/scale;
|
|
|
|
signalUpdate().emit();
|
|
}
|
|
|
|
void TopView::render() {
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
glLoadIdentity();
|
|
|
|
glScalef(scale, scale, 1);
|
|
glTranslatef(-xCenter, -yCenter, 0);
|
|
|
|
drawGrid();
|
|
|
|
if(!level)
|
|
return;
|
|
|
|
const std::list<Data::Room*> &rooms = level->getRooms();
|
|
|
|
for(std::list<Data::Room*>::const_iterator room = rooms.begin(); room != rooms.end(); ++room)
|
|
renderRoom(*room, *room == selectedRoom);
|
|
}
|
|
|
|
void TopView::click(float x, float y, unsigned int button) {
|
|
if(button != 1)
|
|
return;
|
|
|
|
Math::Vertex2D v(xCenter + x/scale, yCenter + y/scale);
|
|
|
|
selectedRoom = 0;
|
|
|
|
const std::list<Data::Room*>& rooms = level->getRooms();
|
|
|
|
for(std::list<Data::Room*>::const_iterator room = rooms.begin(); room != rooms.end(); ++room) {
|
|
const std::list<Data::Triangle*>& triangles = (*room)->getFloorTriangles();
|
|
|
|
for(std::list<Data::Triangle*>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
|
|
Math::Vertex2D v1((*t)->getVertex(0).getX(), (*t)->getVertex(0).getZ());
|
|
Math::Vertex2D v2((*t)->getVertex(1).getX(), (*t)->getVertex(1).getZ());
|
|
Math::Vertex2D v3((*t)->getVertex(2).getX(), (*t)->getVertex(2).getZ());
|
|
|
|
if(Math::Triangle2D(v1, v2, v3).contains(v)) {
|
|
selectedRoom = *room;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(selectedRoom)
|
|
break;
|
|
}
|
|
|
|
signalUpdate().emit();
|
|
}
|
|
|
|
}
|
|
}
|