/
rotor_window.cpp
399 lines (323 loc) · 11.5 KB
/
rotor_window.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/***************************************************************************
* Copyright 2015 Martin Grap
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
/*! \file rotor_window.cpp
* \brief Implementation of GUI classes that know how to draw rotor windows for the Enigma, KL7 and several other machines.
*/
#include<rotor_window.h>
#include<sg39.h>
void rotor_window_base::set_machine(rotor_machine *m, string &identifier)
{
the_machine = m;
rotor_identifier = identifier;
update();
}
gunichar rotor_window_base::get_ring_pos()
{
ustring ring_pos = the_machine->visualize_rotor_pos(rotor_identifier);
gunichar result;
result = Glib::Unicode::toupper(ring_pos[0]);
return result;
}
void rotor_window_base::update()
{
if (the_machine != NULL)
{
wheel_pos = get_ring_pos();
}
}
void rotor_window_base::on_mouse_button_down(Cairo::RefPtr<Cairo::Context> cr, int x_pos, int y_pos)
{
if (test(x_pos, y_pos))
{
if (y_pos >= y)
{
// Clicked below the middle -> advance one position
the_machine->get_stepping_gear()->advance_rotor(rotor_identifier);
}
else
{
// Clicked above the middle -> step one position back
the_machine->get_stepping_gear()->step_rotor_back(rotor_identifier);
}
update(cr);
}
}
void rotor_window_base::update(Cairo::RefPtr<Cairo::Context> cr)
{
gunichar help;
if (the_machine != NULL)
{
help = get_ring_pos();
if (wheel_pos != help)
{
wheel_pos = help;
draw(cr);
}
}
}
/* ------------------------------------------------------------------ */
enigma_rotor_window::enigma_rotor_window(int pos_x, int pos_y, bool orient)
: rotor_window_base(pos_x, pos_y)
{
screw_orientation = orient;
upper = new screw(0, 0, screw_orientation);
lower = new screw(0, 0, !screw_orientation);
set_defaults();
}
void enigma_rotor_window::set_defaults()
{
wheel_pos = 'A';
is_greek = false;
the_machine = NULL;
is_numeric = false;
has_ellipse = true;
window_size = 40;
calc_limits();
set_bkg_col(LIGHT_GREY);
set_dash_col(DARK_GREY);
set_rotor_bkg_col(WHITE);
set_dash_bkg_col(BACKGROUND_GREY);
}
void enigma_rotor_window::calc_limits()
{
// Derive drawing parameters from window_size
ellipse_width = ((double)window_size) + 5.0;
ellipse_height = ((double)window_size) * 3.0;
rotor_rim_width = (double)(window_size / 3);
// Make all digits after decimal point disappear. Prevents drawing bug in Ubuntu 14.04 LTS.
padded_size = (int)(window_size - (rotor_rim_width / 2));
screw_size = (double)(window_size / 6);
font_size_char = (int)(((double)window_size) * 0.6);
font_size_numeric = (int)(((double)window_size) * 0.55);
char_width_numeric = (font_size_numeric / 2) + 1;
upper->set_radius(screw_size);
lower->set_radius(screw_size);
upper->set_elem_pos(x, y - window_size);
lower->set_elem_pos(x, y + window_size);
}
bool enigma_rotor_window::test(int pos_x, int pos_y)
{
bool result = false;
if (is_active)
{
// Return true if (pos_x, pos_y) is within the rotor rim/handle
result = ((pos_x >= (x + padded_size)) && (pos_x <= (x + padded_size + rotor_rim_width + 2))) && ((pos_y >= (y - (2 * window_size))) && (pos_y <= (y + (2 * window_size))));
}
return result;
}
void enigma_rotor_window::draw_wheel_pos(Cairo::RefPtr<Cairo::Context> cr, gunichar new_pos)
{
wheel_pos = new_pos;
const char *trans_2 = "00000000011111111112222222";
const char *trans_1 = "12345678901234567890123456";
int win_size = ((int)(padded_size)) - 1;
cr->save();
// Draw background of rotor window as a rectangle filled with the rotor background colour
cr->set_source_rgb(rotor_r, rotor_g, rotor_b);
cr->rectangle(x - win_size / 2, y - win_size / 2, win_size, win_size); // Set path
cr->fill_preserve(); // Fill everyting inside the path and preserve the path
// Draw black border around the path, i.e. the rectangle that represents the rotor window
cr->set_line_width(1.0);
cr->set_source_rgb(BLACK);
cr->stroke();
cr->restore();
cr->save();
// Set colour in which to draw the rotor position
if (!is_greek)
cr->set_source_rgb(BLACK); // Default is black
else
cr->set_source_rgb(RED); // The greek wheel on M4 has red markings
if (!is_numeric)
{
// rotor position is displayed as character A-Z
print_char(cr, x, y, new_pos, font_size_char);
}
else
{
// rotor position is displayed as a number 01, 02, ..., 26
print_char(cr, x - char_width_numeric / 2, y, trans_2[new_pos - 'A'], font_size_numeric);
print_char(cr, x + char_width_numeric / 2, y, trans_1[new_pos - 'A'], font_size_numeric);
}
cr->restore();
}
void enigma_rotor_window::set_dash_col(double r, double g, double b)
{
dash_r = r;
dash_g = g;
dash_b = b;
}
void enigma_rotor_window::set_dash_bkg_col(double r, double g, double b)
{
dash_bkg_r = r;
dash_bkg_g = g;
dash_bkg_b = b;
}
void enigma_rotor_window::set_bkg_col(double r, double g, double b)
{
bkg_r = r;
bkg_g = g;
bkg_b = b;
}
void enigma_rotor_window::set_rotor_bkg_col(double r, double g, double b)
{
rotor_r = r;
rotor_g = g;
rotor_b = b;
}
void enigma_rotor_window::set_elem_pos(int new_x, int new_y)
{
element::set_elem_pos(new_x, new_y);
upper->set_elem_pos(x, y - window_size);
lower->set_elem_pos(x, y + window_size);
}
void enigma_rotor_window::draw(Cairo::RefPtr<Cairo::Context> cr)
{
vector<double> dashes;
// Pattern used to draw a dashed line (15 pixels of line followed by 15 "empty" pixels)
dashes.push_back(15.0);
dashes.push_back(15.0);
if (has_ellipse)
{
cr->save();
// Draw background ellipse
cr->set_source_rgb(bkg_r, bkg_g, bkg_b);
draw_ellipse(cr, x, y, ellipse_width, ellipse_height);
cr->fill();
// Draw black border of background ellipse
cr->set_source_rgb(BLACK);
cr->set_line_width(1.2);
draw_ellipse(cr, x, y, ellipse_width, ellipse_height);
cr->stroke();
cr->restore();
}
cr->save();
// Draw a line of width rotor_rim_width in the dash background colour
cr->set_line_width(rotor_rim_width);
cr->set_source_rgb(dash_bkg_r, dash_bkg_g, dash_bkg_b);
cr->move_to(x + window_size, y - (2 * window_size));
cr->line_to(x + window_size, y + (2 * window_size));
cr->stroke();
// Draw a dashed line in the dash colour inside the previously drawn line
// This creates the impression of "notches" on the handle/rim
cr->set_source_rgb(dash_r, dash_g, dash_b);
cr->set_dash(dashes, ((wheel_pos - 'A') & 1) * 15); // modifying the offset creates illusion of movement
cr->move_to(x + window_size, y - (2 * window_size));
cr->line_to(x + window_size, y + (2 * window_size));
cr->stroke();
// Draw border around handle/rim
cr->set_line_width(2.0);
cr->unset_dash();
cr->set_source_rgb(DARK_GREY);
cr->rectangle(x + padded_size, y - (2 * window_size), rotor_rim_width, (4 * window_size));
cr->stroke();
cr->restore();
draw_wheel_pos(cr, wheel_pos);
if (has_ellipse)
{
// Draw screws
upper->draw(cr);
lower->draw(cr);
}
}
/* ------------------------------------------------------------------ */
thin_rotor::thin_rotor(int pos_x, int pos_y, int tr_width, int tr_height)
: rotor_window_base(pos_x, pos_y)
{
width = tr_width;
height = tr_height;
set_col_base(LIGHT_GREY);
}
void thin_rotor::set_col_base(double r, double g, double b)
{
red = r;
green = g;
blue = b;
}
void thin_rotor::draw(Cairo::RefPtr<Cairo::Context> cr)
{
cr->save();
// Draw rotor window rectangle
cr->set_source_rgb(red, green, blue);
cr->set_line_width(1.0);
cr->rectangle(x - width / 2, y - height / 2, width, height);
cr->fill();
cr->stroke();
cr->restore();
cr->save();
// Draw rotor position
cr->set_source_rgb(BLACK);
print_char(cr, x, y, wheel_pos, width - 2);
cr->restore();
}
bool thin_rotor::test(int pos_x, int pos_y)
{
// Return true if (pos_x, pos_y) is within the rotor window rectangle
return (pos_x >= (x - width / 2)) && (pos_y >= y - height / 2) && (pos_x <= (x + width / 2)) && (pos_y <= y + height / 2);
}
/* ------------------------------------------------------------------ */
gunichar thin_kl7_rotor::get_ring_pos()
{
ustring ring_pos = the_machine->visualize_rotor_pos(rotor_identifier);
gunichar result = ring_pos[0];
if ((result >= '0') && (result <= '9'))
{
result = ' ';
}
result = Glib::Unicode::toupper(result);
return result;
}
/* ------------------------------------------------------------------ */
thin_action_rotor::thin_action_rotor(int pos_x, int pos_y, sigc::slot<void, string&, Cairo::RefPtr<Cairo::Context> > action_func, int tr_width, int tr_height)
: thin_rotor(pos_x, pos_y, tr_width, tr_height)
{
action = action_func;
}
void thin_action_rotor::draw(Cairo::RefPtr<Cairo::Context> cr)
{
int help_y = y + height / 2;
thin_rotor::draw(cr);
cr->save();
// Draw action rectangle
cr->set_source_rgb(DARK_GREY);
cr->rectangle(x - width / 2, help_y, width, (height / 6));
cr->fill();
cr->stroke();
// Draw border in background colour around action rectangle to make it appear smaller
cr->set_line_width(3.0);
cr->set_source_rgb(BACKGROUND_GREY);
cr->move_to(x - width / 2, help_y + 1);
cr->line_to(x + width / 2, help_y + 1);
cr->stroke();
cr->restore();
}
bool thin_action_rotor::test(int pos_x, int pos_y)
{
// Return true if (pos_x, pos_y) is either within the rotor window or the action rectangle
return thin_rotor::test(pos_x, pos_y) || ((pos_x >= (x - width / 2)) && (pos_y >= y + height / 2) && (pos_x <= (x + width / 2)) && (pos_y <= y + (height / 2) + (height / 6)));
}
void thin_action_rotor::on_mouse_button_down(Cairo::RefPtr<Cairo::Context> cr, int x_pos, int y_pos)
{
int help_y = y + (height / 2) + 1; // y-position of the top of the action rectangle
if (test(x_pos, y_pos) && (y_pos > help_y))
{
// Click appeared in action rectangle
action(rotor_identifier, cr);
}
else
{
thin_rotor::on_mouse_button_down(cr, x_pos, y_pos);
}
}