def set_fullscreen(self, value): if value: self.fullscreen_toggle.text = _("YES") else: self.fullscreen_toggle.text = _("NO") if gg.fullscreen != value: gg.set_fullscreen(value) dialog.Dialog.top.needs_resize = True
def handle(self, event): """Sends an event through all the applicable handlers, returning constants.NO_RESULT if the event goes unhandled or is handled without requesting the dialog to exit. Otherwise, returns the value provided by the handler.""" # Get the applicable handlers. The handlers lists are all sorted. # If more than one handler type is applicable, we use [:] to make a # copy of the first type's list, then insort_all to insert the elements # of the other lists in proper sorted order. handlers = [] if event.type == pygame.MOUSEMOTION: # Compress multiple MOUSEMOTION events into one. # Note that the pos will be wrong, so pygame.mouse.get_pos() must # be used instead. time.sleep(1. / g.FPS) pygame.event.clear(pygame.MOUSEMOTION) # Generic mouse motion handlers. handlers = self.handlers.get(constants.MOUSEMOTION, [])[:] # Drag handlers. if event.buttons[0]: insort_all(handlers, self.handlers.get(constants.DRAG, [])) elif event.type == pygame.USEREVENT: # Clear excess timer ticks. pygame.event.clear(pygame.USEREVENT) # Timer tick handlers. handlers = self.handlers.get(constants.TICK, []) # Generate repeated keys. if self.key_down: self.repeat_counter += 1 if self.repeat_counter >= 5: self.repeat_counter = 0 self.handle(self.key_down) elif event.type in (pygame.KEYDOWN, pygame.KEYUP): # Generic key event handlers. handlers = self.handlers.get(constants.KEY, [])[:] # TODO: Dynamize global key handlers. # TODO: Allows customization of global key handlers. # Important: Global key handlers should always be a combination # of two keys or F# keys. if event.key == pygame.K_RETURN and pygame.key.get_mods( ) & pygame.KMOD_ALT: if event.type == pygame.KEYDOWN: g.set_fullscreen(not g.fullscreen) Dialog.top.needs_resize = True elif event.key == pygame.K_F5: if event.type == pygame.KEYDOWN: import code.graphics.theme as theme if theme.current: import code.data as data theme_id = theme.current.id data.load_themes() theme.set_theme(theme_id, force_reload=True) elif event.type == pygame.KEYDOWN: # Generic keydown handlers. insort_all(handlers, self.handlers.get(constants.KEYDOWN, [])) if event.unicode: # Unicode-based keydown handlers for this particular key. insort_all(handlers, self.key_handlers.get(event.unicode, [])) # Keycode-based handlers for this particular key. insort_all(handlers, self.key_handlers.get(event.key, [])) # Begin repeating keys. if self.key_down is not event: self.key_down = event self.repeat_counter = -10 self.start_timer(force=True) else: # event.type == pygame.KEYUP: # Stop repeating keys. self.key_down = None self.reset_timer() # Generic keyup handlers. insort_all(handlers, self.handlers.get(constants.KEYUP, [])) # Keycode-based handlers for this particular key. insort_all(handlers, self.key_handlers.get(event.key, [])) # OLPC XO-1 ebook mode. if g.ebook_mode and event.key in KEYPAD: handlers = [(0, handle_ebook)] elif event.type == pygame.MOUSEBUTTONUP: # Handle mouse scrolls by imitating PageUp/Dn if event.button in (4, 5): if event.button == 4: key = pygame.K_PAGEUP else: key = pygame.K_PAGEDOWN fake_key(key) return constants.NO_RESULT # Mouse click handlers. handlers = [] + self.handlers.get(constants.CLICK, []) when = time.time() where = event.pos what = event.button old_when, old_where, old_what = self.last_click self.last_click = when, where, what if what == old_what and when - old_when < .5: # Taxicab distance. dist = (abs(where[0] - old_where[0]) + abs(where[1] - old_where[1])) if dist < 10: # Add double-click handlers, but keep the click handlers. insort_all(handlers, self.handlers.get(constants.DOUBLECLICK, [])) elif event.type == pygame.QUIT: raise SystemExit return self.call_handlers(handlers, event)
except Exception as reason: sys.stderr.write("Cannot load preferences file %s! (%s)\n" % (save_loc, reason)) sys.exit(1) if prefs.has_section("Preferences"): try: desired_language = prefs.get("Preferences", "lang") if desired_language in i18n.available_languages(): i18n.set_language(desired_language) else: raise ValueError except Exception: sys.stderr.write("Invalid or missing 'lang' in preferences.\n") try: gg.set_fullscreen(prefs.getboolean("Preferences", "fullscreen")) except Exception: sys.stderr.write("Invalid or missing 'fullscreen' setting in preferences.\n") try: mixer.nosound = prefs.getboolean("Preferences", "nosound") except Exception: sys.stderr.write("Invalid or missing 'nosound' setting in preferences.\n") try: desired_set_grab = prefs.getboolean("Preferences", "grab") except Exception: sys.stderr.write("Invalid or missing 'grab' setting in preferences.\n") try: g.daynight = prefs.getboolean("Preferences", "daynight")