From 5d40050580920776f8f2d880bfe0cb056993448d Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Fri, 23 Sep 2022 14:22:44 -0400 Subject: [PATCH] Set lattice on open --- src/jam | 9 +++++---- src/painter.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/jam b/src/jam index 782edcb..72c340f 100755 --- a/src/jam +++ b/src/jam @@ -91,10 +91,6 @@ class Jam_app(App): # command prompt self.command_prompt=Command_prompt(self) - # load lattice - if self.lattice=="square": - self.painter.lattice=Square_lattice() - layout.add_widget(self.painter) layout.add_widget(self.status_bar) layout.add_widget(self.command_prompt) @@ -106,9 +102,14 @@ class Jam_app(App): # set readonly mode self.readonly=not os.access(self.openfile,os.W_OK) + # load lattice + if self.lattice=="square": + self.painter.set_lattice(Square_lattice()) + return layout + # disable red circles on right click Config.set('input', 'mouse', 'mouse,disable_multitouch') # do not exit on escape diff --git a/src/painter.py b/src/painter.py index 45445d2..9055b9a 100644 --- a/src/painter.py +++ b/src/painter.py @@ -51,12 +51,29 @@ class Painter(Widget): self.keyboard = Window.request_keyboard(None,self,"text") self.keyboard.bind(on_key_down=self.on_key_down,on_key_up=self.on_key_up,on_textinput=self.on_textinput) + # redraw on resize + self.bind(size=lambda obj,value: self.draw()) + def reset(self): self.particles=[] self.undermouse=None self.draw() + # set lattice + def set_lattice(self,lattice): + self.lattice=lattice + + # draw + self.draw() + + # snap all existing particles to grid + for particle in self.particles: + delta=self.lattice.nearest_delta(particle.squares[0].pos) + if not self.check_interaction_any(particle,delta): + particle.move(delta) + + # convert logical coordinates (normalized and centered) to the ones that are plotted def pos_tocoord_x(self,x): return self.width/2+x*Square_element.size