kopia lustrzana https://github.com/inkstitch/inkstitch
Merge pull request #10 from lexelby/live-simulate
live stitching preview in the Params extensionpull/11/head
commit
e204aadab0
14
embroider.py
14
embroider.py
|
@ -121,7 +121,7 @@ class EmbroideryElement(object):
|
|||
|
||||
if param.endswith('_mm'):
|
||||
# print >> dbg, "get_float_param", param, value, "*", self.options.pixels_per_mm
|
||||
value = value * self.options.pixels_per_mm
|
||||
value = value * getattr(self.options, "pixels_per_mm", 10)
|
||||
|
||||
return value
|
||||
|
||||
|
@ -133,7 +133,7 @@ class EmbroideryElement(object):
|
|||
return default
|
||||
|
||||
if param.endswith('_mm'):
|
||||
value = int(value * self.options.pixels_per_mm)
|
||||
value = int(value * getattr(self.options, "pixels_per_mm", 10))
|
||||
|
||||
return value
|
||||
|
||||
|
@ -207,7 +207,7 @@ class EmbroideryElement(object):
|
|||
|
||||
path = deepcopy(path)
|
||||
|
||||
cspsubdiv(path, self.options.flat)
|
||||
cspsubdiv(path, getattr(self.options, "flat", 0.1))
|
||||
|
||||
flattened = []
|
||||
|
||||
|
@ -500,7 +500,7 @@ class Fill(EmbroideryElement):
|
|||
|
||||
# only stitch the first point if it's a reasonable distance away from the
|
||||
# last stitch
|
||||
if not patch.stitches or (beg - patch.stitches[-1]).length() > 0.5 * self.options.pixels_per_mm:
|
||||
if not patch.stitches or (beg - patch.stitches[-1]).length() > 0.5 * getattr(self.options, "pixels_per_mm", 10):
|
||||
patch.add_stitch(beg)
|
||||
|
||||
first_stitch = self.adjust_stagger(beg, angle, row_spacing, max_stitch_length)
|
||||
|
@ -515,7 +515,7 @@ class Fill(EmbroideryElement):
|
|||
patch.add_stitch(beg + offset * row_direction)
|
||||
offset += max_stitch_length
|
||||
|
||||
if (end - patch.stitches[-1]).length() > 0.1 * self.options.pixels_per_mm:
|
||||
if (end - patch.stitches[-1]).length() > 0.1 * getattr(self.options, "pixels_per_mm", 10):
|
||||
patch.add_stitch(end)
|
||||
|
||||
|
||||
|
@ -1000,7 +1000,7 @@ class AutoFill(Fill):
|
|||
patch.add_stitch(PyEmb.Point(*outline.interpolate(pos).coords[0]))
|
||||
|
||||
end = PyEmb.Point(*end)
|
||||
if (end - patch.stitches[-1]).length() > 0.1 * self.options.pixels_per_mm:
|
||||
if (end - patch.stitches[-1]).length() > 0.1 * getattr(self.options, "pixels_per_mm", 10):
|
||||
patch.add_stitch(end)
|
||||
|
||||
print >> dbg, "end connect_points"
|
||||
|
@ -1941,5 +1941,3 @@ if __name__ == '__main__':
|
|||
print >> dbg, traceback.format_exc()
|
||||
|
||||
dbg.flush()
|
||||
|
||||
dbg.close()
|
||||
|
|
|
@ -5,6 +5,9 @@ import os
|
|||
import sys
|
||||
import json
|
||||
import traceback
|
||||
import time
|
||||
from threading import Thread, Event
|
||||
from copy import copy
|
||||
from cStringIO import StringIO
|
||||
import wx
|
||||
from wx.lib.scrolledpanel import ScrolledPanel
|
||||
|
@ -13,7 +16,7 @@ import inkex
|
|||
from embroider import Param, EmbroideryElement, Fill, AutoFill, Stroke, SatinColumn, descendants
|
||||
from functools import partial
|
||||
from itertools import groupby
|
||||
|
||||
from embroider_simulate import EmbroiderySimulator
|
||||
|
||||
def presets_path():
|
||||
try:
|
||||
|
@ -128,8 +131,14 @@ class ParamsTab(ScrolledPanel):
|
|||
def set_parent_tab(self, tab):
|
||||
self.parent_tab = tab
|
||||
|
||||
def is_dependent_tab(self):
|
||||
return self.parent_tab is not None
|
||||
|
||||
def enabled(self):
|
||||
return self.toggle_checkbox.IsChecked()
|
||||
|
||||
def update_toggle_state(self, event=None, notify_pair=True):
|
||||
enable = self.toggle_checkbox.IsChecked()
|
||||
enable = self.enabled()
|
||||
# print self.name, "update_toggle_state", enable
|
||||
for child in self.settings_grid.GetChildren():
|
||||
widget = child.GetWindow()
|
||||
|
@ -137,7 +146,7 @@ class ParamsTab(ScrolledPanel):
|
|||
child.GetWindow().Enable(enable)
|
||||
|
||||
if notify_pair and self.paired_tab:
|
||||
self.paired_tab.pair_changed(self.toggle_checkbox.IsChecked())
|
||||
self.paired_tab.pair_changed(enable)
|
||||
|
||||
for tab in self.dependent_tabs:
|
||||
tab.dependent_enable(enable)
|
||||
|
@ -149,7 +158,7 @@ class ParamsTab(ScrolledPanel):
|
|||
# print self.name, "pair_changed", value
|
||||
new_value = not value
|
||||
|
||||
if self.toggle_checkbox.IsChecked() != new_value:
|
||||
if self.enabled() != new_value:
|
||||
self.set_toggle_state(not value)
|
||||
self.toggle_checkbox.changed = True
|
||||
self.update_toggle_state(notify_pair=False)
|
||||
|
@ -170,7 +179,7 @@ class ParamsTab(ScrolledPanel):
|
|||
values = {}
|
||||
|
||||
if self.toggle:
|
||||
checked = self.toggle_checkbox.IsChecked()
|
||||
checked = self.enabled()
|
||||
if self.toggle_checkbox in self.changed_inputs and not self.toggle.inverse:
|
||||
values[self.toggle.name] = checked
|
||||
|
||||
|
@ -192,10 +201,16 @@ class ParamsTab(ScrolledPanel):
|
|||
for name, value in values.iteritems():
|
||||
node.set_param(name, value)
|
||||
|
||||
def on_change(self, callable):
|
||||
self.on_change_hook = callable
|
||||
|
||||
def changed(self, event):
|
||||
self.changed_inputs.add(event.GetEventObject())
|
||||
event.Skip()
|
||||
|
||||
if self.on_change_hook:
|
||||
self.on_change_hook(self)
|
||||
|
||||
def load_preset(self, preset):
|
||||
preset_data = preset.get(self.name, {})
|
||||
|
||||
|
@ -305,12 +320,22 @@ class SettingsFrame(wx.Frame):
|
|||
def __init__(self, *args, **kwargs):
|
||||
# begin wxGlade: MyFrame.__init__
|
||||
self.tabs_factory = kwargs.pop('tabs_factory', [])
|
||||
wx.Frame.__init__(self, None, wx.ID_ANY,
|
||||
self.cancel_hook = kwargs.pop('on_cancel', None)
|
||||
wx.Frame.__init__(self, None, wx.ID_ANY,
|
||||
"Embroidery Params"
|
||||
)
|
||||
self.notebook = wx.Notebook(self, wx.ID_ANY)
|
||||
self.tabs = self.tabs_factory(self.notebook)
|
||||
|
||||
for tab in self.tabs:
|
||||
tab.on_change(self.update_simulator)
|
||||
|
||||
self.simulate_window = None
|
||||
self.simulate_thread = None
|
||||
self.simulate_refresh_needed = Event()
|
||||
|
||||
wx.CallLater(1000, self.update_simulator)
|
||||
|
||||
self.presets_box = wx.StaticBox(self, wx.ID_ANY, label="Presets")
|
||||
|
||||
self.preset_chooser = wx.ComboBox(self, wx.ID_ANY)
|
||||
|
@ -329,7 +354,8 @@ class SettingsFrame(wx.Frame):
|
|||
self.delete_preset_button.Bind(wx.EVT_BUTTON, self.delete_preset)
|
||||
|
||||
self.cancel_button = wx.Button(self, wx.ID_ANY, "Cancel")
|
||||
self.cancel_button.Bind(wx.EVT_BUTTON, self.close)
|
||||
self.cancel_button.Bind(wx.EVT_BUTTON, self.cancel)
|
||||
self.Bind(wx.EVT_CLOSE, self.cancel)
|
||||
|
||||
self.use_last_button = wx.Button(self, wx.ID_ANY, "Use Last Settings")
|
||||
self.use_last_button.Bind(wx.EVT_BUTTON, self.use_last)
|
||||
|
@ -341,6 +367,94 @@ class SettingsFrame(wx.Frame):
|
|||
self.__do_layout()
|
||||
# end wxGlade
|
||||
|
||||
def update_simulator(self, tab=None):
|
||||
if self.simulate_window:
|
||||
self.simulate_window.stop()
|
||||
self.simulate_window.clear()
|
||||
|
||||
if not self.simulate_thread or not self.simulate_thread.is_alive():
|
||||
self.simulate_thread = Thread(target=self.simulate_worker)
|
||||
self.simulate_thread.daemon = True
|
||||
self.simulate_thread.start()
|
||||
|
||||
self.simulate_refresh_needed.set()
|
||||
|
||||
def simulate_worker(self):
|
||||
while True:
|
||||
self.simulate_refresh_needed.wait()
|
||||
self.simulate_refresh_needed.clear()
|
||||
self.update_patches()
|
||||
|
||||
def update_patches(self):
|
||||
patches = self.generate_patches()
|
||||
|
||||
if patches and not self.simulate_refresh_needed.is_set():
|
||||
wx.CallAfter(self.refresh_simulator, patches)
|
||||
|
||||
def refresh_simulator(self, patches):
|
||||
if self.simulate_window:
|
||||
self.simulate_window.stop()
|
||||
self.simulate_window.load(patches=patches)
|
||||
else:
|
||||
my_rect = self.GetRect()
|
||||
simulator_pos = my_rect.GetTopRight()
|
||||
simulator_pos.x += 5
|
||||
|
||||
try:
|
||||
self.simulate_window = EmbroiderySimulator(None, -1, "Embroidery Simulator", simulator_pos, size=(300, 300), patches=patches, on_close=self.simulate_window_closed, target_duration=5)
|
||||
except:
|
||||
error = traceback.format_exc()
|
||||
|
||||
try:
|
||||
# a window may have been created, so we need to destroy it
|
||||
# or the app will never exit
|
||||
wx.Window.FindWindowByName("Embroidery Simulator").Destroy()
|
||||
except:
|
||||
pass
|
||||
|
||||
info_dialog(self, error, "Internal Error")
|
||||
|
||||
self.simulate_window.Show()
|
||||
wx.CallLater(10, self.Raise)
|
||||
|
||||
wx.CallAfter(self.simulate_window.go)
|
||||
|
||||
def simulate_window_closed(self):
|
||||
self.simulate_window = None
|
||||
|
||||
def generate_patches(self):
|
||||
patches = []
|
||||
nodes = []
|
||||
|
||||
for tab in self.tabs:
|
||||
tab.apply()
|
||||
|
||||
if tab.enabled() and not tab.is_dependent_tab():
|
||||
nodes.extend(tab.nodes)
|
||||
|
||||
# sort nodes into the proper stacking order
|
||||
nodes.sort(key=lambda node: node.order)
|
||||
|
||||
try:
|
||||
for node in nodes:
|
||||
if self.simulate_refresh_needed.is_set():
|
||||
# cancel; params were updated and we need to start over
|
||||
return []
|
||||
|
||||
# Making a copy of the embroidery element is an easy
|
||||
# way to drop the cache in the @cache decorators used
|
||||
# for many params in embroider.py.
|
||||
|
||||
patches.extend(copy(node).to_patches(None))
|
||||
except SystemExit:
|
||||
raise
|
||||
except:
|
||||
# Ignore errors. This can be things like incorrect paths for
|
||||
# satins or division by zero caused by incorrect param values.
|
||||
pass
|
||||
|
||||
return patches
|
||||
|
||||
def update_preset_list(self):
|
||||
preset_names = load_presets().keys()
|
||||
preset_names = [preset for preset in preset_names if preset != "__LAST__"]
|
||||
|
@ -397,10 +511,10 @@ class SettingsFrame(wx.Frame):
|
|||
|
||||
|
||||
def _load_preset(self, preset_name):
|
||||
preset = self.check_and_load_preset(preset_name)
|
||||
preset = self.check_and_load_preset(preset_name)
|
||||
if not preset:
|
||||
return
|
||||
|
||||
|
||||
for tab in self.tabs:
|
||||
tab.load_preset(preset)
|
||||
|
||||
|
@ -420,29 +534,41 @@ class SettingsFrame(wx.Frame):
|
|||
if not preset_name:
|
||||
return
|
||||
|
||||
preset = self.check_and_load_preset(preset_name)
|
||||
preset = self.check_and_load_preset(preset_name)
|
||||
if not preset:
|
||||
return
|
||||
|
||||
delete_preset(preset_name)
|
||||
delete_preset(preset_name)
|
||||
self.update_preset_list()
|
||||
self.preset_chooser.SetValue("")
|
||||
|
||||
event.Skip()
|
||||
|
||||
def apply(self, event):
|
||||
def _apply(self):
|
||||
for tab in self.tabs:
|
||||
tab.apply()
|
||||
|
||||
def apply(self, event):
|
||||
self._apply()
|
||||
save_preset("__LAST__", self.get_preset_data())
|
||||
self.Close()
|
||||
self.close()
|
||||
|
||||
def use_last(self, event):
|
||||
self._load_preset("__LAST__")
|
||||
self.apply(event)
|
||||
|
||||
def close(self, event):
|
||||
self.Close()
|
||||
def close(self):
|
||||
if self.simulate_window:
|
||||
self.simulate_window.stop()
|
||||
self.simulate_window.Close()
|
||||
|
||||
self.Destroy()
|
||||
|
||||
def cancel(self, event):
|
||||
if self.cancel_hook:
|
||||
self.cancel_hook()
|
||||
|
||||
self.close()
|
||||
|
||||
def __set_properties(self):
|
||||
# begin wxGlade: MyFrame.__set_properties
|
||||
|
@ -476,6 +602,10 @@ class SettingsFrame(wx.Frame):
|
|||
# end wxGlade
|
||||
|
||||
class EmbroiderParams(inkex.Effect):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.cancelled = False
|
||||
inkex.Effect.__init__(self, *args, **kwargs)
|
||||
|
||||
def get_nodes(self):
|
||||
if self.selected:
|
||||
nodes = []
|
||||
|
@ -507,9 +637,11 @@ class EmbroiderParams(inkex.Effect):
|
|||
nodes = self.get_nodes()
|
||||
nodes_by_class = defaultdict(list)
|
||||
|
||||
for node in self.get_nodes():
|
||||
for z, node in enumerate(self.get_nodes()):
|
||||
for cls in self.embroidery_classes(node):
|
||||
nodes_by_class[cls].append(node)
|
||||
element = cls(node)
|
||||
element.order = z
|
||||
nodes_by_class[cls].append(element)
|
||||
|
||||
return sorted(nodes_by_class.items(), key=lambda (cls, nodes): cls.__name__)
|
||||
|
||||
|
@ -521,7 +653,7 @@ class EmbroiderParams(inkex.Effect):
|
|||
else:
|
||||
getter = 'get_param'
|
||||
|
||||
values = filter(lambda item: item is not None,
|
||||
values = filter(lambda item: item is not None,
|
||||
(getattr(node, getter)(param.name, param.default) for node in nodes))
|
||||
|
||||
return values
|
||||
|
@ -535,7 +667,6 @@ class EmbroiderParams(inkex.Effect):
|
|||
def create_tabs(self, parent):
|
||||
tabs = []
|
||||
for cls, nodes in self.get_nodes_by_class():
|
||||
nodes = [cls(node) for node in nodes]
|
||||
params = cls.get_params()
|
||||
|
||||
for param in params:
|
||||
|
@ -587,12 +718,20 @@ class EmbroiderParams(inkex.Effect):
|
|||
return tabs
|
||||
|
||||
|
||||
def cancel(self):
|
||||
self.cancelled = True
|
||||
|
||||
def effect(self):
|
||||
app = wx.App()
|
||||
frame = SettingsFrame(tabs_factory=self.create_tabs)
|
||||
frame = SettingsFrame(tabs_factory=self.create_tabs, on_cancel=self.cancel)
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
if self.cancelled:
|
||||
# This prevents the superclass from outputting the SVG, because we
|
||||
# may have modified the DOM.
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def save_stderr():
|
||||
# GTK likes to spam stderr, which inkscape will show in a dialog.
|
||||
|
@ -606,7 +745,7 @@ def save_stderr():
|
|||
def restore_stderr():
|
||||
os.dup2(sys.stderr_dup, 2)
|
||||
sys.stderr_backup.write(sys.stderr.getvalue())
|
||||
sys.sys.stderr = stderr_backup
|
||||
sys.stderr = stderr_backup
|
||||
|
||||
|
||||
# end of class MyFrame
|
||||
|
@ -616,6 +755,8 @@ if __name__ == "__main__":
|
|||
try:
|
||||
e = EmbroiderParams()
|
||||
e.affect()
|
||||
except SystemExit:
|
||||
raise
|
||||
except:
|
||||
traceback.print_exc()
|
||||
|
||||
|
|
|
@ -3,21 +3,31 @@ import os
|
|||
import numpy
|
||||
import wx
|
||||
import inkex
|
||||
import simplestyle
|
||||
import colorsys
|
||||
|
||||
from embroider import patches_to_stitches, stitches_to_polylines
|
||||
|
||||
class EmbroiderySimulator(wx.Frame):
|
||||
def __init__(self, *args, **kwargs):
|
||||
stitch_file = kwargs.pop('stitch_file')
|
||||
stitch_file = kwargs.pop('stitch_file', None)
|
||||
patches = kwargs.pop('patches', 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)
|
||||
|
||||
wx.Frame.__init__(self, *args, **kwargs)
|
||||
|
||||
self.panel = wx.Panel(self, wx.ID_ANY)
|
||||
self.panel.SetFocus()
|
||||
|
||||
self.stitches = self._parse_stitches(stitch_file)
|
||||
self.width, self.height = self.get_dimensions()
|
||||
self.load(stitch_file, patches)
|
||||
|
||||
self.buffer = wx.EmptyBitmap(self.width, self.height)
|
||||
if self.target_duration:
|
||||
self.adjust_speed(self.target_duration)
|
||||
|
||||
self.buffer = wx.Bitmap(self.width, self.height)
|
||||
self.dc = wx.MemoryDC()
|
||||
self.dc.SelectObject(self.buffer)
|
||||
self.canvas = wx.GraphicsContext.Create(self.dc)
|
||||
|
@ -28,8 +38,33 @@ class EmbroiderySimulator(wx.Frame):
|
|||
self.panel.Bind(wx.EVT_PAINT, self.on_paint)
|
||||
self.panel.Bind(wx.EVT_KEY_DOWN, self.on_key_down)
|
||||
|
||||
self.timer = None
|
||||
|
||||
self.last_pos = None
|
||||
|
||||
self.Bind(wx.EVT_CLOSE, self.on_close)
|
||||
|
||||
def load(self, stitch_file=None, patches=None):
|
||||
if stitch_file:
|
||||
self.mirror = True
|
||||
self.segments = self._parse_stitch_file(stitch_file)
|
||||
elif patches:
|
||||
self.mirror = False
|
||||
self.segments = self._patches_to_segments(patches)
|
||||
else:
|
||||
return
|
||||
|
||||
self.trim_margins()
|
||||
self.width, self.height = self.get_dimensions()
|
||||
|
||||
def adjust_speed(self, duration):
|
||||
self.frame_period = 1000 * float(duration) / len(self.segments)
|
||||
self.stitches_per_frame = 1
|
||||
|
||||
while self.frame_period < 1.0:
|
||||
self.frame_period *= 2
|
||||
self.stitches_per_frame *= 2
|
||||
|
||||
def on_key_down(self, event):
|
||||
keycode = event.GetKeyCode()
|
||||
|
||||
|
@ -50,6 +85,10 @@ class EmbroiderySimulator(wx.Frame):
|
|||
self.timer.Stop()
|
||||
else:
|
||||
self.timer.Start(self.frame_period)
|
||||
elif keycode == ord("R"):
|
||||
self.stop()
|
||||
self.clear()
|
||||
self.go()
|
||||
|
||||
self.frame_period = max(1, self.frame_period)
|
||||
self.stitches_per_frame = max(self.stitches_per_frame, 1)
|
||||
|
@ -64,15 +103,55 @@ class EmbroiderySimulator(wx.Frame):
|
|||
|
||||
return string
|
||||
|
||||
def _parse_stitches(self, stitch_file_path):
|
||||
def color_to_pen(self, color):
|
||||
# python colorsys module uses floats from 0 to 1.0
|
||||
color = [value / 255.0 for value in color]
|
||||
|
||||
hls = list(colorsys.rgb_to_hls(*color))
|
||||
|
||||
# Our background is white. If the color is too close to white, then
|
||||
# it won't be visible. Capping lightness should make colors visible
|
||||
# without changing them too much.
|
||||
hls[1] = min(hls[1], 0.85)
|
||||
|
||||
color = colorsys.hls_to_rgb(*hls)
|
||||
|
||||
# convert back to values in the range of 0-255
|
||||
color = [value * 255 for value in color]
|
||||
|
||||
return wx.Pen(color)
|
||||
|
||||
def _patches_to_segments(self, patches):
|
||||
stitches = patches_to_stitches(patches)
|
||||
|
||||
segments = []
|
||||
|
||||
last_pos = None
|
||||
last_color = None
|
||||
pen = None
|
||||
|
||||
for stitch in stitches:
|
||||
pos = (stitch.x, stitch.y)
|
||||
|
||||
if stitch.color == last_color:
|
||||
segments.append(((last_pos, pos), pen))
|
||||
else:
|
||||
pen = self.color_to_pen(simplestyle.parseColor(stitch.color))
|
||||
|
||||
last_pos = pos
|
||||
last_color = stitch.color
|
||||
|
||||
return segments
|
||||
|
||||
def _parse_stitch_file(self, stitch_file_path):
|
||||
# "$","1","229","229","229","(null)","(null)"
|
||||
# "*","JUMP","1.595898","48.731899"
|
||||
# "*","STITCH","1.595898","48.731899"
|
||||
|
||||
stitches = []
|
||||
segments = []
|
||||
|
||||
pos = (0, 0)
|
||||
color = wx.Brush('black')
|
||||
pen = wx.Brush('black')
|
||||
cut = True
|
||||
|
||||
with open(stitch_file_path) as stitch_file:
|
||||
|
@ -84,7 +163,7 @@ class EmbroiderySimulator(wx.Frame):
|
|||
|
||||
if symbol == "$":
|
||||
red, green, blue = fields[2:5]
|
||||
color = wx.Pen((int(red), int(green), int(blue)))
|
||||
color = color_to_pen((int(red), int(green), int(blue)))
|
||||
elif symbol == "*":
|
||||
if command == "COLOR":
|
||||
# change color
|
||||
|
@ -97,33 +176,84 @@ class EmbroiderySimulator(wx.Frame):
|
|||
new_pos = (int(float(x) * 10), int(float(y) * 10))
|
||||
|
||||
if not cut:
|
||||
stitches.append(((pos, new_pos), color))
|
||||
segments.append(((pos, new_pos), pen))
|
||||
|
||||
cut = False
|
||||
pos = new_pos
|
||||
|
||||
return stitches
|
||||
return segments
|
||||
|
||||
def all_coordinates(self):
|
||||
for segment in self.segments:
|
||||
start, end = segment[0]
|
||||
|
||||
yield start
|
||||
yield end
|
||||
|
||||
def trim_margins(self):
|
||||
"""remove any unnecessary whitespace around the design"""
|
||||
|
||||
min_x = sys.maxint
|
||||
min_y = sys.maxint
|
||||
|
||||
for x, y in self.all_coordinates():
|
||||
min_x = min(min_x, x)
|
||||
min_y = min(min_y, y)
|
||||
|
||||
new_segments = []
|
||||
|
||||
for segment in self.segments:
|
||||
(start, end), color = segment
|
||||
|
||||
new_segment = (
|
||||
(
|
||||
(start[0] - min_x, start[1] - min_y),
|
||||
(end[0] - min_x, end[1] - min_y),
|
||||
),
|
||||
color
|
||||
)
|
||||
|
||||
new_segments.append(new_segment)
|
||||
|
||||
self.segments = new_segments
|
||||
|
||||
def get_dimensions(self):
|
||||
width = 0
|
||||
height = 0
|
||||
|
||||
for stitch in self.stitches:
|
||||
(start_x, start_y), (end_x, end_y) = stitch[0]
|
||||
|
||||
width = max(width, start_x, end_x)
|
||||
height = max(height, start_y, end_y)
|
||||
for x, y in self.all_coordinates():
|
||||
width = max(width, x)
|
||||
height = max(height, y)
|
||||
|
||||
return width, height
|
||||
|
||||
def go(self):
|
||||
self.clear()
|
||||
|
||||
self.current_stitch = 0
|
||||
self.timer = wx.PyTimer(self.draw_one_stitch)
|
||||
|
||||
if not self.timer:
|
||||
self.timer = wx.PyTimer(self.draw_one_frame)
|
||||
|
||||
self.timer.Start(self.frame_period)
|
||||
|
||||
def on_close(self, event):
|
||||
self.stop()
|
||||
|
||||
if self.on_close_hook:
|
||||
self.on_close_hook()
|
||||
|
||||
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
|
||||
|
@ -149,12 +279,14 @@ class EmbroiderySimulator(wx.Frame):
|
|||
dc = wx.ClientDC(self)
|
||||
dc.DrawBitmap(self.buffer, 0, 0)
|
||||
|
||||
def draw_one_stitch(self):
|
||||
def draw_one_frame(self):
|
||||
for i in xrange(self.stitches_per_frame):
|
||||
try:
|
||||
((x1, y1), (x2, y2)), color = self.stitches[self.current_stitch]
|
||||
y1 = self.height - y1
|
||||
y2 = self.height - y2
|
||||
((x1, y1), (x2, y2)), color = self.segments[self.current_stitch]
|
||||
|
||||
if self.mirror:
|
||||
y1 = self.height - y1
|
||||
y2 = self.height - y2
|
||||
|
||||
self.canvas.SetPen(color)
|
||||
self.canvas.DrawLines(((x1, y1), (x2, y2)))
|
||||
|
|
Ładowanie…
Reference in New Issue