-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFillTool.cpp
More file actions
35 lines (26 loc) · 1.04 KB
/
FillTool.cpp
File metadata and controls
35 lines (26 loc) · 1.04 KB
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
#include "FillTool.h"
void FillTool::onMousePressed(const sf::Vector2f& pos, sf::Mouse::Button btn) {
if (btn != sf::Mouse::Left) return;
fillAt(pos);
}
void FillTool::fillAt(const sf::Vector2f& pos) {
sf::Image img = m_canvas->copyToImage();
unsigned w = img.getSize().x;
unsigned h = img.getSize().y;
if (pos.x < 0 || pos.y < 0 || pos.x >= (float)w || pos.y >= (float)h) return;
sf::Color target = img.getPixel((unsigned)pos.x, (unsigned)pos.y);
if (target == m_color) return; // óæå íóæíûé öâåò
std::queue<sf::Vector2u> q;
q.push({ (unsigned)pos.x, (unsigned)pos.y });
while (!q.empty()) {
auto p = q.front(); q.pop();
if (p.x >= w || p.y >= h) continue;
if (img.getPixel(p.x, p.y) != target) continue;
img.setPixel(p.x, p.y, m_color);
if (p.x > 0) q.push({ p.x - 1, p.y });
if (p.x + 1 < w) q.push({ p.x + 1, p.y });
if (p.y > 0) q.push({ p.x, p.y - 1 });
if (p.y + 1 < h) q.push({ p.x, p.y + 1 });
}
m_canvas->updateFromImage(img);
}