-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas.hpp
131 lines (112 loc) · 3.84 KB
/
Canvas.hpp
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
#pragma once
#include "Widget.hpp"
#include "MainWindow.hpp"
#include "ToolPalette.hpp"
#include "color.hpp"
#include <SFML/Graphics.hpp>
#include "tools.hpp"
namespace gGUI {
class Canvas : public Widget, public booba::Image {
using color = uint32_t;
protected:
color *pixels = nullptr;
bool modified = true;
bool usedArr = false;
sf::Image image;
sf::Texture texture;
public:
Signal MouseAct;
Canvas(size_t in_x, size_t in_y, size_t in_w, size_t in_h, MainWindow *p, g::color c = g::color::white)
: Widget(in_x, in_y, in_w, in_h, p, "NONE")
{
assert(parent);
p->setCanvas(this);
pixels = new color[w * h];
assert(pixels != nullptr);
image.create(w, h, sf::Color(c.toInt()));
MouseAct.connect(dynamic_cast<MainWindow*>(parent)->getToolPalette()->CanvasMAct);
}
Canvas(size_t in_x, size_t in_y, size_t in_w, size_t in_h, Widget *p, g::color c = g::color::white)
: Widget(in_x, in_y, in_w, in_h, p, "NONE")
{
assert(parent);
pixels = new color[w * h];
assert(pixels != nullptr);
image.create(w, h, sf::Color(c.toInt()));
}
~Canvas()
{
assert(pixels != nullptr);
delete[] pixels;
}
virtual uint32_t getH() override
{
return h;
}
virtual uint32_t getW() override
{
return w;
}
virtual void emitSignals(Event ev)
{
ev.widgetID = reinterpret_cast<uint64_t>(this);
if (ev.type == Event::MouseMove || ev.type == Event::MousePress || ev.type == Event::MouseRelease) {
MouseAct.call(ev);
}
/*
auto p = dynamic_cast<MainWindow*>(parent)->getToolPalette();
p->emitSignals(ev);
*/
}
virtual color getPixel(int32_t in_x, int32_t in_y) override //FIXME change to size_t when standart allowes
{
assert(0 <= in_x and in_x < w and 0 <= in_y and in_y < h);
return pixels[in_y * w + in_x];
}
virtual void putPixel(uint32_t in_x, uint32_t in_y, color c) override
{
assert(in_x < w and in_y < h);
if (pixels[in_y * w + in_x] == c)
return;
pixels[in_y * w + in_x] = c;
image.setPixel(in_x, in_y, sf::Color(c));
modified = true;
}
virtual color& operator()(uint32_t in_x, uint32_t in_y)
{
assert(in_x < w and in_y < h);
modified = true;
usedArr = true;
return pixels[in_y * w + in_x];
}
virtual const color& operator()(uint32_t in_x, uint32_t in_y) const
{
assert(in_x < w and in_y < h);
return pixels[in_y * w + in_x];
}
virtual void draw(sf::RenderWindow &window, size_t p_x, size_t p_y)
{
if (not isShown)
return;
if (modified) {
assert(pixels != nullptr);
if (usedArr)
image.create(w, h, (uint8_t*)pixels);
texture.loadFromImage(image);
sprite.setTexture(texture);
modified = false;
usedArr = false;
}
Widget::draw(window, p_x, p_y);
}
virtual Widget *belongs(size_t pos_x, size_t pos_y, size_t parent_x = 0, size_t parent_y = 0) const override
{
if (not isShown)
return nullptr;
if ((pos_x <= x + parent_x) || (pos_y <= y + parent_y)
|| (w <= pos_x - x - parent_x) || (h <= pos_y - y - parent_y))
return nullptr;
return (Widget*)this;
}
};
}