summaryrefslogtreecommitdiffstats
path: root/ToolAddPolygon.cpp
blob: 0ac8feabe78cf68ce4fcdd77254478b25387533e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "ToolAddPolygon.h"
#include <GL/gl.h>


ToolAddPolygon::ToolAddPolygon(EditManager *editManager) : Renderer(editManager), sidebar(editManager) {
  this->editManager = editManager;
  
  image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_BUTTON);
  g_object_ref_sink(G_OBJECT(image));
}

ToolAddPolygon::~ToolAddPolygon() {
  g_object_unref(G_OBJECT(image));
}

void ToolAddPolygon::activate() {
  newRoom = Room();
  
  editManager->setHighlightedObject(NULL);
  editManager->setSelectedObject(NULL);
}

void ToolAddPolygon::deactivate() {
  editManager->addRoom(newRoom);
}

void ToolAddPolygon::render(const Level &level, const Rectangle &rect, float scale) {
  if(editManager->polygonOk(newRoom))
    glColor4f(0.0f, 0.7f, 1.0f, 0.2f);
  else
    glColor4f(1.0f, 0.3f, 0.3f, 0.2f);
  
  fillPolygon(newRoom);
  
  glLineWidth(2.0f);
  glColor4f(0.0f, 0.7f, 1.0f, 0.7f);
  drawPolygon(newRoom, false);
  
  if(!newRoom.empty() && editManager->getHoveredVertex()) {
    if(!editManager->vertexOk(*editManager->getHoveredVertex(), &newRoom))
      glColor4f(1.0f, 0.3f, 0.3f, 0.7f);
    
    glBegin(GL_LINES);
    
    glVertex2f(newRoom.back().getX(), newRoom.back().getY());
    glVertex2f(editManager->getHoveredVertex()->getX(), editManager->getHoveredVertex()->getY());
    
    glEnd();
  }
}

bool ToolAddPolygon::buttonPress(unsigned int button) {
  if(button != 1)
    return false;
  
  if(!editManager->getHoveredVertex())
    return false;
  
  if(!editManager->vertexOk(*editManager->getHoveredVertex(), &newRoom))
    return false;
  
  newRoom.push_back(*editManager->getHoveredVertex());
  
  editManager->redraw();
  sidebar.update();
  
  return true;
}