-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrect_operation.cpp
171 lines (140 loc) · 4.64 KB
/
rect_operation.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "libtcod.hpp"
#include "gui/gui.hpp"
#include "app.h"
#include "brush.h"
#include "misc.h"
#include "rect_operation.h"
RectOperation::RectOperation() {
}
RectOperation::~RectOperation() {
}
void RectOperation::start() {
justCleared = false;
app->gui->outlineToggleButton->setVisible(true);
app->gui->filledToggleButton->setVisible(true);
app->gui->useSymbolToggleButton->setVisible(true);
app->gui->useForegroundToggleButton->setVisible(true);
app->gui->useBackgroundToggleButton->setVisible(true);
app->gui->useSolidToggleButton->setVisible(true);
}
void RectOperation::update() {
TCOD_mouse_t mouse = app->mouse;
int mouseX = app->canvasMouseX;
int mouseY = app->canvasMouseY;
// If the user just let go of the mouse buttons
if(mouse.lbutton == false && mouse.rbutton == false && active == true) {
// First make a backup for undo
app->addUndo();
// Then blit the overlay onto the canvas
app->applyOverlayToCanvas();
}
// if the user press the left and right mouse button at the same time then
// erase the current stroke
if(mouse.lbutton == true && mouse.rbutton == true && active == true) {
app->clearOverlay();
justCleared = true; // justCleared is needed so that we don't get a dot at
// the end. Just try and comment out that rect to see what happens. :)
}
if(mouse.lbutton == false && mouse.rbutton == false) {
justCleared = false;
}
// If the user just started drawing the rect
if((mouse.lbutton == true || mouse.rbutton == true) && active == false) {
rectStartX = mouseX;
rectStartY = mouseY;
}
app->clearOverlay();
if((mouse.lbutton || mouse.rbutton) && justCleared == false) {
active = true;
Brush *outlineBrush;
Brush *fillBrush;
// If pressing the left mouse button use brush1 otherwise brush2
if(mouse.lbutton == true) {
outlineBrush = &app->brush1;
fillBrush = &app->brush2;
} else {
outlineBrush = &app->brush2;
fillBrush = &app->brush1;
}
int rectEndX, rectEndY;
if(TCODConsole::isKeyPressed(TCODK_SHIFT)) {
setSquareEndPoint(&rectEndX, &rectEndY);
} else {
rectEndX = mouseX;
rectEndY = mouseY;
}
bool drawOutline = app->gui->outlineToggleButton->isPressed();
bool drawFill = app->gui->filledToggleButton->isPressed();
// If just drawing a fill then use the outline brush as the fill brush
if(drawFill && !drawOutline) {
fillBrush = outlineBrush;
}
if(drawFill) {
// Draw a filled rectangle
// Note: we use min and max so that the for loop always executes
for(int x = min(rectStartX, rectEndX); x <= max(rectStartX, rectEndX); x++) {
for(int y = min(rectStartY, rectEndY); y <= max(rectStartY, rectEndY); y++) {
// Draw on the overlay using the fill brush
app->applyBrushToOverlayCell(x, y, fillBrush);
}
}
}
if(drawOutline) {
// Draw the outline of a rect
int x = rectStartX;
int y = rectStartY;
// Draw the top side of rect
TCODLine::init(x, y, rectEndX, y);
do {
// Draw on the overlay using the outlineBrush
app->applyBrushToOverlayCell(x, y, outlineBrush);
} while(!TCODLine::step(&x, &y));
// Start a rect from starting mouse cell to new mouse cell
TCODLine::init(x, y, rectEndX, rectEndY);
do {
// Draw on the overlay using the outlineBrush
app->applyBrushToOverlayCell(x, y, outlineBrush);
} while(!TCODLine::step(&x, &y));
// Start a rect from starting mouse cell to new mouse cell
TCODLine::init(x, y, rectStartX, y);
do {
// Draw on the overlay using the outlineBrush
app->applyBrushToOverlayCell(x, y, outlineBrush);
} while(!TCODLine::step(&x, &y));
// Start a rect from starting mouse cell to new mouse cell
TCODLine::init(x, y, x, rectStartY);
do {
// Draw on the overlay using the outlineBrush
app->applyBrushToOverlayCell(x, y, outlineBrush);
} while(!TCODLine::step(&x, &y));
}
} else {
// if no button was pressed or we just cleared
active = false;
// Clear the overlay and draw a preview of the current outlineBrush on it
app->applyBrushToOverlayCell(mouseX, mouseY, &app->brush1);
}
}
void RectOperation::end() {
}
// Sets the value of the rect end point when the we want a square
void RectOperation::setSquareEndPoint(int* rectEndX, int* rectEndY) {
int mouseX = app->canvasMouseX;
int mouseY = app->canvasMouseY;
float dx = abs(rectStartX - mouseX);
float dy = abs(rectStartY - mouseY);
// Make a square with the end position being as large as possible
if(dx > dy) {
*rectEndX = mouseX;
if(mouseY > rectStartY)
*rectEndY = rectStartY + dx;
else
*rectEndY = rectStartY - dx;
} else {
*rectEndY = mouseY;
if(mouseX > rectStartX)
*rectEndX = rectStartX + dy;
else
*rectEndX = rectStartX - dy;
}
}