-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resources.cpp
299 lines (287 loc) · 13.2 KB
/
Resources.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "Resources.h"
#include "Item.h"
#include <wx/graphics.h>
static_assert(sizeof(int) == sizeof(size_t) / 2); //Required for hashing
namespace {
const wxPoint2DDouble resistorPoints[] {
{0, 0.5},
{0.140625, 0.5},
{0.1875, 0.59375},
{0.28125, 0.40625},
{0.375, 0.59375},
{0.453125, 0.40625},
{0.53125, 0.59375},
{0.625, 0.40625},
{0.703125, 0.59375},
{0.796875, 0.40625},
{0.84375, 0.5},
{1, 0.5}
};
wxBitmap initBitmap(int size) {
wxImage image{size, size};
image.InitAlpha();
memset(image.GetAlpha(), 0, size * size); //set to fully transparent
return {image};
}
double rScale(double d, double scale) { //radial scale (scaling point from center)
return 0.5 + (d - 0.5) * scale;
}
}
namespace std { //used by std::unordered_map
template<>
struct hash<std::pair<int,bool>> {
size_t operator() (std::pair<int,bool> key) const {
return key.first | (static_cast<size_t>(key.second) << (sizeof(int) * 8 + 1));
}
};
template<>
struct hash<std::pair<int,int>> {
size_t operator() (std::pair<int,int> key) const {
return (static_cast<size_t>(key.first) << (sizeof(int) * 8)) | key.second;
}
};
template<>
struct hash<std::tuple<int,bool,bool>> {
size_t operator() (std::tuple<int,bool,bool> key) const {
return std::get<0>(key) | (static_cast<size_t>(std::get<1>(key)) << (sizeof(int) * 8 + 1)) | (static_cast<size_t>(std::get<2>(key)) << (sizeof(int) * 8 + 2));
}
};
}
wxBitmap resources::getBinBitmap(int size) { //doing it this way instead of image.Scale to get antialiasing
static wxImage binImage{"res/bin.png", wxBITMAP_TYPE_PNG};
wxBitmap binFullBitmap{binImage};
wxBitmap bitmap{initBitmap(size)};
wxMemoryDC dc{bitmap};
wxGraphicsContext* context = wxGraphicsContext::Create(dc);
context->DrawBitmap(binFullBitmap, 0, 0, size, size);
delete context;
dc.SelectObject(wxNullBitmap);
return bitmap;
}
wxBitmap resources::getResistorBitmap(int size, bool rotated) {
static std::unordered_map<std::pair<int,bool>, wxBitmap> cache{};
std::pair<int,bool> key{size, rotated};
auto iter = cache.find(key);
if(iter != cache.end()) {
return iter->second;
}
wxBitmap bitmap{initBitmap(size)};
wxMemoryDC dc{bitmap};
wxGraphicsContext* context = wxGraphicsContext::Create(dc);
wxPen pen = wxPen{wxPenInfo(*wxBLACK, std::ceil(size * 22.0 / 1024))};
context->SetPen(pen);
constexpr size_t numPoints = sizeof(resistorPoints) / sizeof(resistorPoints[0]);
wxPoint2DDouble points[numPoints];
for(int i = 0; i < numPoints; i ++) {
if(rotated) {
points[i] = {resistorPoints[i].m_y * size, resistorPoints[i].m_x * size};
} else {
points[i] = resistorPoints[i] * size;
}
}
context->StrokeLines(numPoints, points);
delete context;
dc.SelectObject(wxNullBitmap);
cache[key] = bitmap;
return bitmap;
}
wxIconBundle resources::getResistorIconBundle() {
return wxIconBundle("res/resistor-multires.ico", wxBITMAP_TYPE_ICO);
}
//Only called once, so no need to cache
wxBitmap resources::getWireBitmap(int size) {
wxBitmap bitmap{initBitmap(size)};
wxMemoryDC dc{bitmap};
wxGraphicsContext* context = wxGraphicsContext::Create(dc);
wxPen pen = wxPen{wxPenInfo(*wxBLACK, std::ceil(size * 22.0 / 1024))};
context->SetPen(pen);
context->StrokeLine(0, size / 2.0, size, size / 2.0);
delete context;
dc.SelectObject(wxNullBitmap);
return bitmap;
}
wxBitmap resources::getVoltSourceBitmap(int size, int shape, bool toolbar) {
static std::unordered_map<std::pair<int,int>, wxBitmap> cache{};
std::pair<int, int> key{size, shape};
if(!toolbar) {
auto iter = cache.find(key);
if (iter != cache.end()) {
return iter->second;
}
}
double scale = toolbar ? 1 : 0.4 / 0.7;
wxBitmap bitmap{initBitmap(size)};
wxMemoryDC dc{bitmap};
wxGraphicsContext* context = wxGraphicsContext::Create(dc);
wxPen pen = wxPen{wxPenInfo(*wxBLACK, std::ceil(size * 22.0 / 1024))};
context->SetPen(pen);
if(shape & Item::DEPENDENT) { //Draw border
const wxPoint2DDouble points[] = {
{0.5 * size, rScale(0.15, scale) * size},
{rScale(0.85, scale) * size, 0.5 * size},
{0.5 * size, rScale(0.85, scale) * size},
{rScale(0.15, scale) * size, 0.5 * size},
{0.5 * size, rScale(0.15, scale) * size}};
context->StrokeLines(5, points);
}
else {
context->DrawEllipse(rScale(0.15, scale) * size, rScale(0.15, scale) * size, 0.7 * scale * size, 0.7 * scale * size);
}
if((shape & Item::UP) || (shape & Item::DOWN)) { //Draw wire connections
context->StrokeLine(0.5 * size, 0, 0.5 * size, rScale(0.15, scale) * size);
context->StrokeLine(0.5 * size, rScale(0.85, scale) * size, 0.5 * size, size);
} else {
context->StrokeLine(0, 0.5 * size, rScale(0.15, scale) * size, 0.5 * size);
context->StrokeLine(rScale(0.85, scale) * size, 0.5 * size, size, 0.5 * size);
}
if(shape & Item::UP) { //Draw + and -
context->StrokeLine(0.5 * size, rScale(0.3, scale) * size, 0.5 * size, 0.5 * size);
context->StrokeLine(rScale(0.4, scale) * size, rScale(0.4, scale) * size, rScale(0.6, scale) * size, rScale(0.4, scale) * size);
context->StrokeLine(rScale(0.4, scale) * size, rScale(0.67, scale) * size, rScale(0.6, scale) * size, rScale(0.67, scale) * size);
} else if(shape & Item::RIGHT) {
context->StrokeLine(rScale(0.7, scale) * size, 0.5 * size, 0.5 * size, 0.5 * size);
context->StrokeLine(rScale(0.6, scale) * size, rScale(0.4, scale) * size, rScale(0.6, scale) * size, rScale(0.6, scale) * size);
context->StrokeLine(rScale(0.33, scale) * size, rScale(0.4, scale) * size, rScale(0.33, scale) * size, rScale(0.6, scale) * size);
} else if(shape & Item::DOWN) {
context->StrokeLine(0.5 * size, rScale(0.7, scale) * size, 0.5 * size, 0.5 * size);
context->StrokeLine(rScale(0.4, scale) * size, rScale(0.6, scale) * size, rScale(0.6, scale) * size, rScale(0.6, scale) * size);
context->StrokeLine(rScale(0.4, scale) * size, rScale(0.32, scale) * size, rScale(0.6, scale) * size, rScale(0.32, scale) * size);
} else {
context->StrokeLine(rScale(0.3, scale) * size, 0.5 * size, 0.5 * size, 0.5 * size);
context->StrokeLine(rScale(0.4, scale) * size, rScale(0.4, scale) * size, rScale(0.4, scale) * size, rScale(0.6, scale) * size);
context->StrokeLine(rScale(0.67, scale) * size, rScale(0.4, scale) * size, rScale(0.67, scale) * size, rScale(0.6, scale) * size);
}
delete context;
dc.SelectObject(wxNullBitmap);
if(!toolbar) cache[key] = bitmap;
return bitmap;
}
wxBitmap resources::getAmpSourceBitmap(int size, int shape, bool toolbar) {
static std::unordered_map<std::pair<int,int>,wxBitmap> cache{};
std::pair<int,int> key{size, shape};
if(!toolbar) {
auto iter = cache.find(key);
if (iter != cache.end()) {
return iter->second;
}
}
wxBitmap bitmap{initBitmap(size)};
wxMemoryDC dc{bitmap};
wxGraphicsContext* context = wxGraphicsContext::Create(dc);
wxPen pen = wxPen{wxPenInfo(*wxBLACK, std::ceil(size * 22.0 / 1024))};
context->SetPen(pen);
double scale = toolbar ? 1 : 0.4 / 0.7;
if(shape & Item::DEPENDENT) { //Draw border
const wxPoint2DDouble points[] = {
{0.5 * size, rScale(0.15, scale) * size},
{rScale(0.85, scale) * size, 0.5 * size},
{0.5 * size, rScale(0.85, scale) * size},
{rScale(0.15, scale) * size, 0.5 * size},
{0.5 * size, rScale(0.15, scale) * size}};
context->StrokeLines(5, points);
}
else {
context->DrawEllipse(rScale(0.15, scale) * size, rScale(0.15, scale) * size, 0.7 * scale * size, 0.7 * scale * size);
}
if((shape & Item::UP) || (shape & Item::DOWN)) { //Draw wire connections and main part of arrow
context->StrokeLine(0.5 * size, 0, 0.5 * size, rScale(0.15, scale) * size);
context->StrokeLine(0.5 * size, rScale(0.85, scale) * size, 0.5 * size, size);
context->StrokeLine(0.5 * size, rScale(0.7, scale) * size, 0.5 * size, rScale(0.3, scale) * size);
} else {
context->StrokeLine(0, 0.5 * size, rScale(0.15, scale) * size, 0.5 * size);
context->StrokeLine(rScale(0.85, scale) * size, 0.5 * size, size, 0.5 * size);
context->StrokeLine(rScale(0.3, scale) * size, 0.5 * size, rScale(0.7, scale) * size, 0.5 * size);
}
if(shape & Item::UP) { //Draw current arrow
const wxPoint2DDouble points[] = {{rScale(0.47, scale) * size, rScale(0.4, scale) * size},
{0.5 * size, rScale(0.3, scale) * size},
{rScale(0.53, scale) * size, rScale(0.4, scale) * size}};
context->StrokeLines(3, points);
} else if(shape & Item::RIGHT) {
const wxPoint2DDouble points[] = {{rScale(0.6, scale) * size, rScale(0.47, scale) * size},
{rScale(0.7, scale) * size, 0.5 * size},
{rScale(0.6, scale) * size, rScale(0.53, scale) * size}};
context->StrokeLines(3, points);
} else if(shape & Item::DOWN) {
const wxPoint2DDouble points[] = {{rScale(0.47, scale) * size, rScale(0.6, scale) * size},
{0.5 * size, rScale(0.7, scale) * size},
{rScale(0.53, scale) * size, rScale(0.6, scale) * size}};
context->StrokeLines(3, points);
} else {
const wxPoint2DDouble points[] = {{rScale(0.4, scale) * size, rScale(0.47, scale) * size},
{rScale(0.3, scale) * size, 0.5 * size},
{rScale(0.4, scale) * size, rScale(0.53, scale) * size}};
context->StrokeLines(3, points);
}
delete context;
dc.SelectObject(wxNullBitmap);
if(!toolbar) cache[key] = bitmap;
return bitmap;
}
wxBitmap resources::getCapacitorBitmap(int size, bool rotated) {
static std::unordered_map<std::pair<int,bool>, wxBitmap> cache{};
std::pair<int,bool> key{size, rotated};
auto iter = cache.find(key);
if(iter != cache.end()) {
return iter->second;
}
wxBitmap bitmap{initBitmap(size)};
wxMemoryDC dc{bitmap};
wxGraphicsContext* context = wxGraphicsContext::Create(dc);
wxPen pen = wxPen{wxPenInfo(*wxBLACK, std::ceil(size * 22.0 / 1024))};
context->SetPen(pen);
if(rotated) {
context->StrokeLine(0.5 * size, 0, 0.5 * size, 0.42 * size);
context->StrokeLine(0.5 * size, 0.58 * size, 0.5 * size, size);
context->StrokeLine(0.3 * size, 0.42 * size, 0.7 * size, 0.42 * size);
context->StrokeLine(0.3 * size, 0.58 * size, 0.7 * size, 0.58 * size);
} else {
context->StrokeLine(0, 0.5 * size, 0.42 * size, 0.5 * size);
context->StrokeLine(0.58 * size, 0.5 * size, size, 0.5 * size);
context->StrokeLine(0.42 * size, 0.3 * size, 0.42 * size, 0.7 * size);
context->StrokeLine(0.58 * size, 0.3 * size, 0.58 * size, 0.7 * size);
}
delete context;
dc.SelectObject(wxNullBitmap);
cache[key] = bitmap;
return bitmap;
}
wxBitmap resources::getSwitchBitmap(int size, bool rotated, bool closed) {
static std::unordered_map<std::tuple<int,bool,bool>, wxBitmap> cache{};
std::tuple<int,bool,bool> key {size, rotated, closed};
auto iter = cache.find(key);
if(iter != cache.end()) {
return iter->second;
}
wxBitmap bitmap{initBitmap(size)};
wxMemoryDC dc{bitmap};
wxGraphicsContext* context = wxGraphicsContext::Create(dc);
int border = std::ceil(size * 22.0 / 1024);
wxPen pen = wxPen{wxPenInfo(*wxBLACK, border)};
context->SetPen(pen);
if(rotated) {
context->DrawEllipse(0.45 * size, border, 0.1 * size, 0.1 * size);
context->DrawEllipse(0.45 * size, 0.9 * size - border, 0.1 * size, 0.1 * size);
context->StrokeLine(0.5 * size, border, 0.5 * size, -1);
context->StrokeLine(0.5 * size, size - border, 0.5 * size, size + 1);
if(closed) {
context->StrokeLine(0.5 * size, 0.1 * size + border, 0.5 * size, 0.9 * size - border);
} else {
context->StrokeLine(0.5 * size, 0.1 * size + border, 0.2 * size, 0.8 * size - border);
}
} else {
context->DrawEllipse(border, 0.45 * size, 0.1 * size, 0.1 * size);
context->DrawEllipse(0.9 * size - border, 0.45 * size, 0.1 * size, 0.1 * size);
context->StrokeLine(border, 0.5 * size, -1, 0.5 * size);
context->StrokeLine(size - border, 0.5 * size, size + 1, 0.5 * size);
if(closed) {
context->StrokeLine(0.1 * size + border, 0.5 * size, 0.9 * size - border, 0.5 * size);
} else {
context->StrokeLine(0.1 * size + border, 0.5 * size, 0.8 * size - border, 0.2 * size);
}
}
delete context;
dc.SelectObject(wxNullBitmap);
cache[key] = bitmap;
return bitmap;
}