summaryrefslogtreecommitdiffstats
path: root/ToolRotate.cpp
blob: 6cd7626fd16dcde3b2f25ab41e348a40e69166bc (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
#include "ToolRotate.h"

ToolRotate::ToolRotate(EditManager *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));
}

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

void ToolRotate::activate() {
  pressed = false;
  editManager->highlightHoveredObject();
  editManager->setSelectedObject(NULL);
}

bool ToolRotate::buttonPress(unsigned int button) {
  if(button != 1 || !editManager->getHoveredObject())
    return false;
  
  pressed = true;
  Vertex v = *editManager->getHoveredVertex() - editManager->getHoveredObject()->getCenter();
  angle = atan2(v.getY(), v.getX());
  
  editManager->setSelectedObject(editManager->getHoveredObject());
  
  editManager->redraw();
  sidebar.update();
  
  return true;
}

bool ToolRotate::buttonRelease(unsigned int button) {
  if(button != 1)
    return false;
  
  pressed = false;
  
  editManager->setSelectedObject(NULL);
  
  editManager->redraw();
  sidebar.update();
  
  return true;
}

bool ToolRotate::motion() {
  if(!pressed) {
    editManager->highlightHoveredObject();
    return true;
  }
  
  if(!editManager->getHoveredVertex())
    return false;
  
  Vertex v = *editManager->getHoveredVertex() - editManager->getSelectedObject()->getCenter();
  
  float a = atan2(v.getY(), v.getX());
  
  editManager->getSelectedObject()->rotate(a-angle);
  
  angle = a;
  
  return false;
}