diff --git a/CHANGES.rst b/CHANGES.rst index 9fe1c859..8c1dea4a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -22,6 +22,7 @@ Release History 0.4.4 (unreleased) ================== +- Improvement (Experimental): Added simulator hooks - Improvement: Added audible spike sounds - Improvement (Experimental): Nodes have access to the GUI keyboard state - Improvement: support for nengo-bio Connections diff --git a/nengo_gui/components/sim_control.py b/nengo_gui/components/sim_control.py index b05a80c1..d9478990 100644 --- a/nengo_gui/components/sim_control.py +++ b/nengo_gui/components/sim_control.py @@ -190,11 +190,16 @@ def javascript(self): def message(self, msg): if msg == 'pause': self.paused = True + if 'on_pause' in self.page.locals: + self.page.locals['on_pause'](self.page.sim) elif msg == 'config': self.send_config_options = True elif msg == 'continue': if self.page.sim is None: self.page.rebuild = True + else: + if 'on_continue' in self.page.locals: + self.page.locals['on_continue'](self.page.sim) self.paused = False elif msg == 'reset': self.paused = True diff --git a/nengo_gui/guibackend.py b/nengo_gui/guibackend.py index 4d054215..e2581653 100644 --- a/nengo_gui/guibackend.py +++ b/nengo_gui/guibackend.py @@ -460,6 +460,7 @@ def create_page(self, filename, reset_cfg=False): def remove_page(self, page): self._last_access = time.time() + page.close() self.pages.remove(page) if (not self._shutting_down and self.settings.auto_shutdown > 0 and len(self.pages) <= 0): diff --git a/nengo_gui/page.py b/nengo_gui/page.py index 863ca411..7ddf340c 100644 --- a/nengo_gui/page.py +++ b/nengo_gui/page.py @@ -498,6 +498,8 @@ def build(self): if self.sim is not None: if self.settings.backend in Page.singleton_sims: Page.singleton_sims[self.settings.backend] = self + if 'on_start' in self.locals: + self.locals['on_start'](self.sim) # remove the temporary components added for visualization for c in self.components: @@ -520,6 +522,8 @@ def runner(self): self.sim.run_steps(self.sim.max_steps) else: self.sim.step() + if 'on_step' in self.locals: + self.locals['on_step'](self.sim) except Exception as err: if self.finished: return @@ -527,8 +531,16 @@ def runner(self): self.error = dict(trace=traceback.format_exc(), line=line) self.sim = None while self.sims_to_close: - self.sims_to_close.pop().close() + s = self.sims_to_close.pop() + if 'on_close' in self.locals: + self.locals['on_close'](s) + s.close() if self.rebuild: self.build() self.sim = None + + def close(self): + if self.sim is not None: + if 'on_close' in self.locals: + self.locals['on_close'](self.sim)