Skip to content

Commit 8929742

Browse files
committed
example:
removed zero by division in example simulator: toggle Fullscreen with T key now only works in fullscreen mode added documentation for some functions gave some functions better name
1 parent e84bf80 commit 8929742

2 files changed

Lines changed: 42 additions & 19 deletions

File tree

ev3dev2simulator/Simulator.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import arcade
1212
from pymunk import Space
1313

14+
# HACK: https://gamedev.stackexchange.com/questions/16024/secondary-monitor-freezes-game-window
15+
#from pyglet.gl import *
16+
1417
# HACK: need to change dir to Simulator script's directory because resources are loaded relative from this directory
1518
script_dir = os.path.dirname(os.path.realpath(__file__))
1619
os.chdir(script_dir)
@@ -41,7 +44,7 @@ def __init__(self, robot_state: RobotState, robot_pos: Tuple[int, int, int], sho
4144
self.check_for_unique_instance()
4245

4346
self.robot_state = robot_state
44-
self.init_screen(use_second_screen_to_show_simulator)
47+
self.set_screen_to_display_simulator_at_startup(use_second_screen_to_show_simulator)
4548

4649
self.scaling_multiplier = get_config().get_scale()
4750
self.large_sim_type = get_config().is_large_sim_type()
@@ -99,20 +102,30 @@ def __init__(self, robot_state: RobotState, robot_pos: Tuple[int, int, int], sho
99102
self.setup()
100103

101104
if show_fullscreen == True:
102-
self.toggleFullScreen()
105+
self.toggleFullScreenOnCurrentScreen()
103106

104107
if show_maximized == True:
105108
self.maximize()
106109

107110
self.check_for_activation()
108111

109-
def init_screen(self,use_second_screen_to_show_simulator):
112+
def set_screen_to_display_simulator_at_startup(self,use_second_screen_to_show_simulator):
113+
""" Set screen to use to display the simulator at startup. For windows this works only in fullscreen mode.
114+
115+
By default set current screen to show simulator, but if use_second_screen_to_show_simulator==True
116+
then change screen to other screen.
117+
118+
On MacOS this works for both fullscreen and none-fullscreen mode.
119+
On Windows this only works for fullscreen mode. For none-fullscreen always the first screen is used.
120+
"""
121+
110122
# get current_screen_index
111123
current_screen_index=0
112124
if use_second_screen_to_show_simulator == True:
113125
current_screen_index=1
114126
display = pyglet.canvas.get_display()
115127
screens= display.get_screens()
128+
for screen in screens: print(screen)
116129
num_screens=len(screens)
117130
if num_screens== 1:
118131
current_screen_index=0
@@ -147,6 +160,9 @@ def get_default_screen():
147160

148161

149162
def check_for_unique_instance(self):
163+
""" Detect whether an other instance is already running. If so then trigger the
164+
activation for the other instance and terminate this instance.
165+
"""
150166

151167
tmpdir=tempfile.gettempdir()
152168
self.pidfile = os.path.join(tmpdir,"ev3dev2simulator.pid")
@@ -168,6 +184,12 @@ def check_for_unique_instance(self):
168184
sys.exit()
169185

170186
def check_for_activation(self):
187+
""" checks each interval whether the simulator windows must be activated (bring to front)
188+
189+
note: activation can happen when one tries to start another instance of the simulator,
190+
and that instance detects an instance is already running. It then triggers the
191+
activation for the other instance and terminates itself.
192+
"""
171193
from pyglet import clock
172194

173195
def callback(dt):
@@ -278,18 +300,15 @@ def on_key_press(self, key, modifiers):
278300
if key == arcade.key.Q:
279301
self.on_close()
280302

281-
# Toggle fullscreen between screens
303+
# Toggle fullscreen between screens (only works at fullscreen mode)
282304
if key == arcade.key.T:
283-
# User hits T. Switch screen used for fullscreen.
284-
285-
# to switch screen when in fullscreen we first have to back to normal window, and do fullscreen again
305+
# User hits T. When at fullscreen, then switch screen used for fullscreen.
286306
if self.fullscreen:
287-
self.set_fullscreen(False)
288-
289-
# switch which screen is used for fullscreen ; Toggle between first and second screen (other screens are ignored)
290-
self.toggleScreenUsedForFullscreen()
291-
292-
self.setFullScreen()
307+
# to switch screen when in fullscreen we first have to back to normal window, and do fullscreen again
308+
self.set_fullscreen(False)
309+
# switch which screen is used for fullscreen ; Toggle between first and second screen (other screens are ignored)
310+
self.toggleScreenUsedForFullscreen()
311+
self.setFullScreen()
293312

294313
# Maximize window
295314
# note: is toggle on macos, but not on windows
@@ -302,11 +321,11 @@ def on_key_press(self, key, modifiers):
302321
# src: http://arcade.academy/examples/full_screen_example.html
303322
if key == arcade.key.F:
304323
self.updateCurrentScreen()
305-
self.toggleFullScreen()
324+
self.toggleFullScreenOnCurrentScreen()
306325

307326

308327
#toggle screen for fullscreen
309-
# BUG: doesn't work on macos => see explaination in init_screen() method
328+
# BUG: doesn't work on macos => see explaination in set_screen_to_display_simulator_at_startup() method
310329
def toggleScreenUsedForFullscreen(self):
311330
display = pyglet.canvas.get_display()
312331
screens= display.get_screens()
@@ -358,7 +377,7 @@ def updateCurrentScreen(self):
358377
self._screen=screens[self.current_screen_index]
359378

360379

361-
def toggleFullScreen(self):
380+
def toggleFullScreenOnCurrentScreen(self):
362381
# User hits 'f' Flip between full and not full screen.
363382
self.set_fullscreen(not self.fullscreen)
364383

@@ -370,7 +389,7 @@ def toggleFullScreen(self):
370389
# HACK for macos: without this hack fullscreen on the second screen is shifted downwards in the y direction
371390
# By also calling the maximize function te position the fullscreen in second screen is corrected!)
372391
import platform
373-
if platform.system().lower() == "darwin":
392+
if self.fullscreen and platform.system().lower() == "darwin":
374393
self.maximize()
375394

376395
def setFullScreen(self):
@@ -411,6 +430,12 @@ def on_draw(self):
411430

412431
arcade.start_render()
413432

433+
# HACK: https://gamedev.stackexchange.com/questions/16024/secondary-monitor-freezes-game-window
434+
# glEnable(GL_BLEND)
435+
# glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
436+
# glEnable(GL_LINE_SMOOTH)
437+
# glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE)
438+
414439
self.obstacle_elements.draw()
415440
self.robot_elements.draw()
416441

examples/DriveRectangle.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
log("turn right")
4040
tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_BACKWARD, TURN_TIME)
4141

42-
x=1/0
43-
4442
log("drive forward")
4543
tankDrive.on_for_seconds(SPEED_FORWARD, SPEED_FORWARD, 3)
4644

0 commit comments

Comments
 (0)