-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill_operation.cpp
100 lines (82 loc) · 2.92 KB
/
fill_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
#include "libtcod.hpp"
#include "gui/gui.hpp"
#include "app.h"
#include "brush.h"
#include "fill_operation.h"
FillOperation::FillOperation() {
}
FillOperation::~FillOperation() {
}
void FillOperation::start() {
oldMouse = app->mouse;
justCleared = false;
app->gui->useSymbolToggleButton->setVisible(true);
app->gui->useForegroundToggleButton->setVisible(true);
app->gui->useBackgroundToggleButton->setVisible(true);
app->gui->useSolidToggleButton->setVisible(true);
}
void FillOperation::update() {
TCOD_mouse_t mouse = app->mouse;
int mouseX = app->canvasMouseX;
int mouseY = app->canvasMouseY;
if((mouse.lbutton && !oldMouse.lbutton) ||
(mouse.rbutton && !oldMouse.rbutton)) {
active = true;
Brush *fillBrush;
// If pressing the left mouse button use brush1 otherwise brush2
mouse.lbutton == true ? fillBrush = &app->brush1 : fillBrush = &app->brush2;
Brush currentCellBrush;
currentCellBrush.symbol = app->canvasCon->getChar(mouseX, mouseY);
currentCellBrush.fore = app->canvasCon->getCharForeground(mouseX, mouseY);
currentCellBrush.back = app->canvasCon->getCharBackground(mouseX, mouseY);
if(app->solidCon->getCharBackground(mouseX, mouseY) == TCODColor(0, 0, 255))
currentCellBrush.solid = true;
else
currentCellBrush.solid = false;
if(!(currentCellBrush.symbol == fillBrush->symbol &&
currentCellBrush.fore == fillBrush->fore &&
currentCellBrush.back == fillBrush->back &&
currentCellBrush.solid == fillBrush->solid))
{
// First make a backup for undo
app->addUndo();
// Do a fill on the overlay then blit the overlay onto the canvas
app->applyCanvasToOverlay();
doFill(mouseX, mouseY, ¤tCellBrush, fillBrush);
app->applyOverlayToCanvas();
app->clearOverlay();
}
} else {
// if no button was pressed or we just cleared
active = false;
// Draw a preview of brush1 on the overlay
app->clearOverlay();
app->applyBrushToOverlayCell(mouseX, mouseY, &app->brush1);
}
oldMouse = mouse;
}
void FillOperation::end() {
}
void FillOperation::doFill(int x, int y, Brush *oldBrush, Brush *newBrush) {
if(x < app->canvasWidth && x >= 0 && y < app->canvasHeight && y >= 0) {
if(cellUsesBrush(x, y, oldBrush)) {
app->applyBrushToOverlayCell(x, y, newBrush);
doFill(x - 1, y, oldBrush, newBrush);
doFill(x + 1, y, oldBrush, newBrush);
doFill(x, y - 1, oldBrush, newBrush);
doFill(x, y + 1, oldBrush, newBrush);
}
}
}
bool FillOperation::cellUsesBrush(int x, int y, Brush* brush) {
if(app->overlayCon->getCharBackground(x, y) == brush->back &&
app->overlayCon->getCharForeground(x, y) == brush->fore &&
app->overlayCon->getChar(x, y) == brush->symbol) {
TCODColor col = app->solidOverlayCon->getCharBackground(x, y);
if((brush->solid && app->solidOverlayCon->getCharBackground(x, y) == TCODColor(0, 0, 255)) ||
(!brush->solid && app->solidOverlayCon->getCharBackground(x, y) == TCODColor(255, 255, 255))) {
return true;
}
}
return false;
}