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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
import sys
import wx
import time
from itertools import izip
from .svg import color_block_to_point_lists
class EmbroiderySimulator(wx.Frame):
def __init__(self, *args, **kwargs):
stitch_plan = kwargs.pop('stitch_plan', None)
self.x_position = kwargs.pop('x_position', None)
self.on_close_hook = kwargs.pop('on_close', None)
self.frame_period = kwargs.pop('frame_period', 80)
self.stitches_per_frame = kwargs.pop('stitches_per_frame', 1)
self.target_duration = kwargs.pop('target_duration', None)
self.margin = 30
screen_rect = self.get_current_screen_rect()
self.max_width = kwargs.pop('max_width', screen_rect[2])
self.max_height = kwargs.pop('max_height', screen_rect[3])
self.scale = 1
self.min_width = 600
if self.max_width < self.min_width:
self.max_width = self.min_width
self.load(stitch_plan)
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self, wx.ID_ANY)
self.panel.SetBackgroundStyle(wx.BG_STYLE_PAINT)
self.SetBackgroundColour('white')
self.slider_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.set_slider()
self.button_sizer = wx.StdDialogButtonSizer()
self.button_label = (
[_("<<"),
_('Speed | Play reverse (arrow left)'),
self.animation_reverse],
[_(">>"),
_('Speed | Play forward (arrow right)'),
self.animation_forward],
[_("Pause"), _("Pause (P)"), self.animation_pause],
[_("Restart"), _("Restart (R)"), self.animation_restart],
[_("Quit"), _("Close (Q)"), self.animation_quit])
self.buttons = []
for i in range(0, len(self.button_label)):
self.buttons.append(wx.Button(self, -1, self.button_label[i][0]))
self.button_sizer.Add(self.buttons[i], 1, wx.EXPAND)
self.buttons[i].SetToolTip(self.button_label[i][1])
self.buttons[i].Bind(wx.EVT_BUTTON, self.button_label[i][2])
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.panel, 1, wx.EXPAND)
self.sizer.Add(self.slider_sizer, 0, wx.EXPAND)
self.sizer.Add(self.button_sizer, 0, wx.EXPAND)
self.SetSizer(self.sizer)
self.calculate_dimensions()
if self.target_duration:
self.adjust_speed(self.target_duration)
self.buffer = wx.Bitmap(
self.width * self.scale + self.margin * 2,
self.height * self.scale + self.margin * 2)
self.dc = wx.BufferedDC()
self.dc.SelectObject(self.buffer)
self.canvas = wx.GraphicsContext.Create(self.dc)
self.clear()
self.current_frame = 0
self.animation_direction = 1
self.speed_info = self.calculate_speed_level()
self.set_stitch_counter(0)
shortcut_keys = [
(wx.ACCEL_NORMAL, ord('+'), self.animation_speed_up),
(wx.ACCEL_NORMAL, ord('='), self.animation_speed_up),
(wx.ACCEL_SHIFT, ord('='), self.animation_speed_up),
(wx.ACCEL_NORMAL, wx.WXK_ADD, self.animation_speed_up),
(wx.ACCEL_NORMAL, wx.WXK_NUMPAD_ADD, self.animation_speed_up),
(wx.ACCEL_NORMAL, wx.WXK_NUMPAD_UP, self.animation_speed_up),
(wx.ACCEL_NORMAL, wx.WXK_UP, self.animation_speed_up),
(wx.ACCEL_NORMAL, ord('-'), self.animation_slow_down),
(wx.ACCEL_NORMAL, ord('_'), self.animation_slow_down),
(wx.ACCEL_NORMAL, wx.WXK_SUBTRACT, self.animation_slow_down),
(wx.ACCEL_NORMAL, wx.WXK_NUMPAD_SUBTRACT, self.animation_slow_down),
(wx.ACCEL_NORMAL, wx.WXK_NUMPAD_DOWN, self.animation_slow_down),
(wx.ACCEL_NORMAL, wx.WXK_DOWN, self.animation_slow_down),
(wx.ACCEL_NORMAL, wx.WXK_RIGHT, self.animation_forward),
(wx.ACCEL_NORMAL, wx.WXK_LEFT, self.animation_reverse),
(wx.ACCEL_NORMAL, wx.WXK_NUMPAD_RIGHT, self.animation_forward),
(wx.ACCEL_NORMAL, wx.WXK_NUMPAD_LEFT, self.animation_reverse),
(wx.ACCEL_NORMAL, ord('r'), self.animation_restart),
(wx.ACCEL_NORMAL, ord('p'), self.animation_pause),
(wx.ACCEL_NORMAL, ord('q'), self.animation_quit)]
accel_entries = []
for shortcut_key in shortcut_keys:
eventId = wx.NewId()
accel_entries.append((shortcut_key[0], shortcut_key[1], eventId))
self.Bind(wx.EVT_MENU, shortcut_key[2], id=eventId)
accel_table = wx.AcceleratorTable(accel_entries)
self.SetAcceleratorTable(accel_table)
self.Bind(wx.EVT_SIZE, self.on_size)
self.Bind(wx.EVT_CLOSE, self.on_close)
self.Bind(wx.EVT_SLIDER, self.on_slider)
self.panel.Bind(wx.EVT_PAINT, self.on_paint)
self.panel.SetFocus()
self.timer = None
self.last_pos = None
def get_current_screen_rect(self):
current_screen = wx.Display.GetFromPoint(wx.GetMousePosition())
display = wx.Display(current_screen)
screen_rect = display.GetClientArea()
return screen_rect
def load(self, stitch_plan=None):
if stitch_plan:
self.mirror = False
self.stitch_plan_to_lines(stitch_plan)
self.move_to_top_left()
return
def reset_speed(self):
if self.target_duration:
self.adjust_speed(self, self.target_duration)
else:
self.stitches_per_frame = 1
self.frame_period = 80
self.set_speed_info(self.calculate_speed_level())
def adjust_speed(self, duration):
self.frame_period = 1000 * float(duration) / len(self.lines)
self.stitches_per_frame = 1
while self.frame_period < 1.0:
self.frame_period *= 2
self.stitches_per_frame *= 2
def animation_forward(self, event):
if self.current_frame == 1:
self.animation_direction = 1
self.reset_speed()
self.timer.StartOnce(self.frame_period)
elif self.animation_direction == -1 and self.frame_period > 1280:
self.animation_direction = 1
self.set_speed_info(self.calculate_speed_level())
elif self.animation_direction == 1:
self.animation_speed_up('speed_up')
else:
self.animation_slow_down('slow_down')
def animation_reverse(self, event):
if self.current_frame == len(self.lines):
self.animation_direction = -1
self.reset_speed()
self.timer.StartOnce(self.frame_period)
elif self.animation_direction == 1 and self.frame_period > 1280:
self.animation_direction = -1
self.set_speed_info(self.calculate_speed_level())
elif self.animation_direction == -1:
self.animation_speed_up('speed_up')
else:
self.animation_slow_down('slow_down')
def animation_speed_up(self, event):
if self.stitches_per_frame <= 1280:
if self.frame_period == 1:
self.stitches_per_frame *= 2
else:
self.frame_period = self.frame_period / 2
self.animation_update_timer()
def animation_slow_down(self, event):
if self.frame_period <= 1280:
if self.stitches_per_frame == 1:
self.frame_period *= 2
else:
self.stitches_per_frame /= 2
self.animation_update_timer()
def animation_restart(self, event):
self.current_frame = 1
self.stop()
self.clear()
self.go()
def animation_pause(self, event):
if self.timer.IsRunning():
self.timer.Stop()
else:
self.timer.StartOnce(self.frame_period)
def animation_quit(self, event):
self.Close()
def animation_update_timer(self):
self.frame_period = max(1, self.frame_period)
self.stitches_per_frame = max(self.stitches_per_frame, 1)
self.speed_info = self.calculate_speed_level()
self.set_stitch_counter(self.current_frame)
if self.timer.IsRunning():
self.timer.Stop()
self.timer.StartOnce(self.frame_period)
def calculate_speed_level(self):
count = 0
speed = self.frame_period
speed_plus = 1
animation_direction = '>>' if self.animation_direction == 1 else '<<'
while speed <= 2560:
speed = speed * 2
count += 1
while speed_plus < self.stitches_per_frame:
speed_plus = speed_plus * 2
count += 1
return animation_direction + ' x' + str(count)
def set_stitch_counter(self, current_frame):
self.dc.SetTextForeground('red')
stitch_counter_text = _("Stitch # ") + \
str(current_frame) + ' / ' + str(len(self.lines))
self.dc.DrawText(stitch_counter_text, 30, 5)
self.set_speed_info(self.speed_info)
def set_speed_info(self, speed_info):
self.speed_info = speed_info
self.dc.SetTextForeground('blue')
self.dc.DrawText(_("Speed ") + str(speed_info), 210, 5)
def on_slider(self, event):
self.panel.SetFocus()
self.draw_one_frame()
obj = event.GetEventObject()
self.current_frame = obj.GetValue()
self.animation_update_timer()
def set_slider(self):
self.stitch_slider = wx.Slider(
self, value=1, minValue=1, maxValue=len(
self.lines), style=wx.SL_HORIZONTAL | wx.SL_LABELS)
self.slider_sizer.Add(self.stitch_slider, 1, wx.EXPAND)
def set_stitch_slider(self, val):
self.stitch_slider.SetValue(val)
def _strip_quotes(self, string):
if string.startswith('"') and string.endswith('"'):
string = string[1:-1]
return string
def color_to_pen(self, color):
return wx.Pen(color.visible_on_white.rgb)
def stitch_plan_to_lines(self, stitch_plan):
self.pens = []
self.lines = []
for color_block in stitch_plan:
pen = self.color_to_pen(color_block.color)
for i, point_list in enumerate(
color_block_to_point_lists(color_block)):
if i == 0:
# add the first stitch
first_x, first_y = point_list[0]
self.lines.append((first_x, first_y, first_x, first_y))
self.pens.append(pen)
# if there's only one point, there's nothing to do, so skip
if len(point_list) < 2:
continue
for start, end in izip(point_list[:-1], point_list[1:]):
line = (start[0], start[1], end[0], end[1])
self.lines.append(line)
self.pens.append(pen)
def move_to_top_left(self):
"""remove any unnecessary whitespace around the design"""
min_x = sys.maxsize
min_y = sys.maxsize
for x1, y1, x2, y2 in self.lines:
min_x = min(min_x, x2)
min_y = min(min_y, y2)
new_lines = []
for line in self.lines:
(start, end, start1, end1) = line
new_lines.append(
(start - min_x,
end - min_y,
start1 - min_x,
end1 - min_y))
self.lines = new_lines
def calculate_dimensions(self):
# 0.01 avoids a division by zero below for designs with no width or
# height (e.g. a straight vertical or horizontal line)
width = 0.01
height = 0.01
for x1, y1, x2, y2 in self.lines:
width = max(width, x2)
height = max(height, y2)
self.width = width
self.height = height
button_width, button_height = self.buttons[0].GetSize()
slider_width, slider_height = self.stitch_slider.GetSize()
self.controls_height = button_height + slider_height
self.scale = min(
float(
self.max_width -
self.margin *
2) /
width,
float(
self.max_height -
self.margin *
2 -
self.controls_height) /
height)
# make room for decorations and the margin
self.scale *= 0.95
for i, point in enumerate(self.lines):
x1, x2, y1, y2 = point
x1 = x1 * self.scale + self.margin
y1 = y1 * self.scale + self.margin
x2 = x2 * self.scale + self.margin
y2 = y2 * self.scale + self.margin
self.lines[i] = (x1, x2, y1, y2)
def go(self):
self.clear()
self.current_frame = 0
if not self.timer:
self.timer = wx.PyTimer(self.iterate_frames)
self.timer.StartOnce(self.frame_period)
def on_close(self, event):
self.stop()
if self.on_close_hook:
self.on_close_hook()
# If we keep a reference here, wx crashes when the process exits.
self.canvas = None
self.Destroy()
def stop(self):
if self.timer:
self.timer.Stop()
def clear(self):
self.dc.SetBackground(wx.Brush('white'))
self.dc.Clear()
self.last_pos = None
self.Refresh()
def on_size(self, e):
# ensure that the whole canvas is visible
window_width, window_height = self.GetSize()
client_width, client_height = self.GetClientSize()
decorations_width = window_width - client_width
decorations_height = window_height - client_height
setsize_window_width = self.width * self.scale + \
decorations_width + self.margin * 2
setsize_window_height = self.height * self.scale + \
decorations_height + self.controls_height + self.margin * 2
# set minimum width (force space for control buttons)
if setsize_window_width < self.min_width:
setsize_window_width = self.min_width
self.SetSize((setsize_window_width, setsize_window_height))
# center the simulation on screen if not called by params
# else center vertically
if self.x_position is None:
self.Centre()
else:
display_rect = self.get_current_screen_rect()
self.SetPosition(
(self.x_position,
display_rect[3] /
2 -
setsize_window_height /
2))
e.Skip()
def on_paint(self, e):
dc = wx.AutoBufferedPaintDC(self.panel)
dc.Blit(
0,
0,
self.buffer.GetWidth(),
self.buffer.GetHeight(),
self.dc,
0,
0)
self.last_pos_x, self.last_pos_y, self.last_pos_x1, self.last_pos_y1 = self.lines[0]
if hasattr(self, 'visible_lines'):
if len(self.visible_lines) > 0:
self.last_pos_x1, self.last_pos_y1, self.last_pos_x, self.last_pos_y = self.visible_lines[-1]
dc.DrawLine(
self.last_pos_x - 10,
self.last_pos_y,
self.last_pos_x + 10,
self.last_pos_y)
dc.DrawLine(
self.last_pos_x,
self.last_pos_y - 10,
self.last_pos_x,
self.last_pos_y + 10)
def iterate_frames(self):
self.current_frame += self.stitches_per_frame * self.animation_direction
if self.current_frame <= len(self.lines) and self.current_frame >= 1:
# calculate time_to_next_frame
start = time.time()
self.draw_one_frame()
duration = time.time() - start
duration_ms = int(duration * 1000)
time_to_next_frame = self.frame_period - duration_ms
time_to_next_frame = max(1, time_to_next_frame)
self.timer.StartOnce(time_to_next_frame)
elif self.current_frame > len(self.lines):
self.current_frame = len(self.lines)
self.draw_one_frame()
elif self.current_frame < 1:
self.current_frame = 1
self.draw_one_frame()
else:
self.timer.Stop()
self.set_stitch_counter(self.current_frame)
self.set_stitch_slider(self.current_frame)
def draw_one_frame(self):
self.clear()
self.visible_lines = self.lines[:self.current_frame]
self.dc.DrawLineList(self.visible_lines,
self.pens[:self.current_frame])
|