summaryrefslogtreecommitdiffstats
path: root/ToolAddRect.cpp
blob: a749c69b108e8ac047bec7d5360c6b7ed7e90e58 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "ToolAddRect.h"
#include <GL/gl.h>


Room ToolAddRect::createRoom() {
  const Vertex *v2 = editManager->getHoveredVertex();
  
  Room room;
  room.push_back(v1);
  room.push_back(Vertex(v1.getX(), v2->getY()));
  room.push_back(*v2);
  room.push_back(Vertex(v2->getX(), v1.getY()));
  
  return room;
}

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

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

void ToolAddRect::activate() {
  editManager->setHighlightedObject(NULL);
  editManager->setSelectedObject(NULL);
}

void ToolAddRect::render(const Level &level, const Rectangle &rect, float scale) {
  if(!pressed || !editManager->getHoveredVertex())
    return;
  
  Room room = createRoom();
  
  if(editManager->polygonOk(room))
    glColor4f(0.0f, 0.7f, 1.0f, 0.2f);
  else
    glColor4f(1.0f, 0.3f, 0.3f, 0.2f);
  
  fillPolygon(room);
  
  glLineWidth(2.0f);
  glColor4f(0.0f, 0.7f, 1.0f, 0.7f);
  drawPolygon(room);
}

bool ToolAddRect::buttonPress(unsigned int button) {
  if(button != 1)
    return false;
  
  if(!editManager->getHoveredVertex())
    return false;
  
  if(!editManager->vertexOk(*editManager->getHoveredVertex()))
    return false;
  
  pressed = true;
  v1 = *editManager->getHoveredVertex();
  
  editManager->redraw();
  sidebar.update();
  
  return true;
}

bool ToolAddRect::buttonRelease(unsigned int button) {
  if(button != 1 || !pressed)
    return false;
  
  pressed = false;
  
  if(editManager->getHoveredVertex()) {
    Room room = createRoom();
    
    if(editManager->polygonOk(room)) {
      editManager->addRoom(room);
      
      editManager->redraw();
      sidebar.update();
      
      return true;
    }
  }
  
  editManager->redraw();
  sidebar.update();
  
  return false;
}