Skip to content

Commit

Permalink
Merge pull request #16 from fanbsun/master
Browse files Browse the repository at this point in the history
update jwidget.py
  • Loading branch information
AndySomogyi authored Apr 5, 2021
2 parents bc7a3db + 17b00e0 commit 55c52c8
Showing 1 changed file with 142 additions and 5 deletions.
147 changes: 142 additions & 5 deletions src/jwidget.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,149 @@
import mechanica as m
from ipywidgets import widgets
import numpy as np
import threading
import time
from ipyevents import Event

flag = False
downflag = False
shiftflag = False

def init(*args, **kwargs):
print("hello from mechanica.jwidget.init()")
global flag, downflag, shiftflag

w = widgets.Image(value=m.system.image_data(), width=600)
d = Event(source=w, watched_events=['mousedown', 'mouseup', 'mousemove', 'keyup','keydown', 'wheel'])
no_drag = Event(source=w, watched_events=['dragstart'], prevent_default_action = True)
d.on_dom_event(listen_mouse)
run = widgets.ToggleButton(
value = False,
description='Run',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='run the simulation',
icon = 'play'
)
pause = widgets.ToggleButton(
value = False,
description='Pause',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='pause the simulation',
icon = 'pause'
)

reset = widgets.ToggleButton(
value = False,
description='Reset',
disabled=False,
button_style='', # 'success', 'info', 'warning', 'danger' or ''
tooltip='reset the simulation',
icon = 'stop'
)

def onToggleRun(b):
global flag
if run.value:
run.button_style = 'success'
pause.value = False
pause.button_style = ''
reset.value = False
reset.button_style = ''
flag = True
else:
run.button_style=''
flag = False

def onTogglePause(b):
global flag
if pause.value:
pause.button_style = 'success'
run.value = False
run.button_style = ''
reset.value = False
reset.button_style = ''
flag = False
else:
pause.button_style=''
flag = True

def onToggleReset(b):
global flag
if reset.value:
reset.button_style = 'success'
pause.value = False
pause.button_style = ''
run.value = False
run.button_style = ''
flag = False
m.Universe.reset()
else:
reset.button_style=''
#w = create_simulation()

buttons = widgets.HBox([run, pause, reset])
run.observe(onToggleRun,'value')
pause.observe(onTogglePause,'value')
reset.observe(onToggleReset,'value')

box = widgets.VBox([w, buttons])
#display(box)

def background_threading():
global flag
while True:
m.Simulator.context_make_current()
if flag:
m.step()
w.value = m.system.image_data()
m.Simulator.context_release()
time.sleep(0.01)


t = threading.Thread(target=background_threading)
t.start()







def run(*args, **kwargs):
print("hello from mechanica.jwidget.run()")
global flag

flag = True

# return true to tell Mechanica to not run a simulation loop,
# jwidget runs it's one loop.
return True


def listen_mouse(event):
global downflag, shiftflag
if event['type'] == "mousedown":
m.system.camera_init_mouse([event['dataX'], event['dataY']])
downflag = True
if event['type'] == "mouseup":
downflag = False
if event['type'] == "mousemove":
if downflag and not shiftflag:
m.system.camera_rotate_mouse([event['dataX'], event['dataY']])
if downflag and shiftflag:
m.system.camera_translate_mouse([event['dataX'], event['dataY']])

if event['shiftKey'] == True:
shiftflag = True
if event['shiftKey'] == False:
shiftflag = False
if event['type'] == "wheel":
m.system.camera_zoom_by(event['deltaY'])
if event['type'] == "keydown" and event['code'] == "KeyR":
m.system.camera_reset()




# return true to tell Mechanica to not run a simulation loop,
# jwidget runs it's one loop.
return True


0 comments on commit 55c52c8

Please sign in to comment.