def test_get_set_mod_state(self): initial = keyboard.get_mod_state() for state in(keycode.KMOD_NUM | keycode.KMOD_CAPS | keycode.KMOD_MODE, keycode.KMOD_NUM | keycode.KMOD_CAPS, keycode.KMOD_CAPS): keyboard.set_mod_state(state) self.assertEqual(keyboard.get_mod_state(), state) state = keycode.KMOD_NUM keyboard.set_mod_state(state) self.assertEqual(keyboard.get_mod_state(), state) keyboard.set_mod_state(initial) self.assertEqual(keyboard.get_mod_state(), initial)
def test_pygame2_sdl_keyboard_set_mod_state(self): # __doc__ (as of 2009-05-13) for pygame2.sdl.keyboard.set_mod_state: # set_mod_state (mod) -> None # # Sets the current modifier key state. # # Sets the current modifier key state. mod has to be a bitwise OR'd # combination of the KMOD_* flags as they are specified in the # constants. self.assertEqual (keyboard.get_mod_state (), 0) kstate = constants.KMOD_LALT|constants.KMOD_NUM keyboard.set_mod_state (kstate) self.assertEqual (keyboard.get_mod_state (), kstate) keyboard.set_mod_state (constants.KMOD_CAPS) self.assertEqual (keyboard.get_mod_state (), constants.KMOD_CAPS) keyboard.set_mod_state (kstate) self.assertEqual (keyboard.get_mod_state (), kstate)
def test_pygame2_sdl_keyboard_get_mod_state(self): # __doc__ (as of 2009-05-13) for pygame2.sdl.keyboard.get_mod_state: # get_mod_state () -> state # # Returns the current state of the modifier keys (CTRL, ALT, etc.). # # Returns a single integer representing a bitmask of all the # modifier keys being held. Using bitwise operators you can test # if specific shift keys are pressed, the state of the capslock # button, and more. The bitmask will consist of the various # KMOD_* flags as specified in the constants. self.assertEqual (keyboard.get_mod_state (), 0) kstate = constants.KMOD_LALT|constants.KMOD_NUM keyboard.set_mod_state (kstate) self.assertEqual (keyboard.get_mod_state (), kstate) keyboard.set_mod_state (constants.KMOD_CAPS) self.assertEqual (keyboard.get_mod_state (), constants.KMOD_CAPS) keyboard.set_mod_state (kstate) self.assertEqual (keyboard.get_mod_state (), kstate)
def loop(self, choke): frame_no = 0 last_loop_ts = last_animflip_ts = sdltimer.get_ticks() - 1 render_choke = 1000/choke if choke > 0 else 0 # ala G_FPS_CAP anim_period = 1000/self.anim_fps paused = False finished = False panning = False scrolldict = { SDLK_LEFT: ( 1, 0), SDLK_RIGHT: ( -1, 0), SDLK_UP: ( 0, 1), SDLK_DOWN: ( 0, -1), SDLK_HOME: ( 1, 1), SDLK_PAGEUP: ( -1, 1), SDLK_END: ( 1, -1), SDLK_PAGEDOWN: ( -1, -1), } def had_input(): if not self.had_input: self.had_input = True self.hp_cheat.active = False while not finished: loop_start = sdltimer.get_ticks() self.last_loop_time = loop_start - last_loop_ts last_loop_ts = loop_start if not paused: if loop_start - last_animflip_ts > anim_period: frame_no += 1 last_animflip_ts = loop_start if frame_no > self.gamedata.maxframes-1: frame_no = 0 self.render(frame_no) while not finished: # hang around in case fps is user-limited while True: # eat events ev = sdlevents.poll_event(True) kmodstate = sdlkeyboard.get_mod_state() if ev is None: break elif ev.type == SDL_KEYDOWN: kcode = ev.key.keysym.sym if kcode == SDLK_SPACE: paused = not paused elif kcode == SDLK_F1: self.hp_cheat.active = not self.hp_cheat.active elif kcode == SDLK_F2: self.dump_fbos() elif kcode == SDLK_ESCAPE: if self.had_input: finished = True break else: self.hp_cheat.active = False elif kcode == SDLK_KP_MULTIPLY: self.show_hidden = False if self.show_hidden else True elif kcode in (SDLK_KP_DIVIDE, SDLK_BACKQUOTE): self.hp_debug.active = False if self.hp_debug.active else True elif kcode == SDLK_PERIOD and ev.mod & KMOD_SHIFT: self.zpan(-1) elif kcode == SDLK_COMMA and ev.mod & KMOD_SHIFT: self.zpan(1) elif kcode in scrolldict: boost = 10 if kmodstate & KMOD_SHIFT else 1 self.pan(Coord2(scrolldict[kcode][0] * boost * self.psz.x, scrolldict[kcode][1] * boost * self.psz.y)) elif kcode == SDLK_BACKSPACE: self.render_origin = self.gamedata.window elif kcode == SDLK_KP_PLUS: if self.anim_fps > 1: self.anim_fps += 1 elif self.anim_fps > 0.5: self.anim_fps = 1 else: self.anim_fps *= 2 anim_period = 1000.0 / self.anim_fps elif kcode == SDLK_KP_MINUS: if self.anim_fps > 1: self.anim_fps -= 1 else: self.anim_fps /= 2 anim_period = 1000.0 / self.anim_fps elif kcode == SDLK_MINUS: if self.zeddown > 1: self.zeddown -= 1 elif kcode == SDLK_EQUALS: if self.zeddown < self.gamedata.dim.z - 1: self.zeddown += 1 had_input() elif ev.type == SDL_QUIT: finished = True break elif ev.type == SDL_WINDOWEVENT: if ev.window.event == sdlvideo.SDL_WINDOWEVENT_RESIZED: self.reshape(Size2(ev.window.data1, ev.window.data2)) elif ev.type == SDL_MOUSEBUTTONDOWN: had_input() if ev.button.button == SDL_BUTTON_RIGHT: # RMB panning = True elif ev.button.button == SDL_BUTTON_LEFT: paused = not paused elif ev.type == SDL_MOUSEBUTTONUP: if ev.button.button == SDL_BUTTON_RIGHT: panning = False elif ev.type == SDL_MOUSEMOTION: if panning: self.pan(Coord2(ev.motion.xrel, ev.motion.yrel)) elif ev.type == SDL_MOUSEWHEEL: had_input() amount = -ev.wheel.y mpos = Coord2._make(sdlmouse.get_mouse_state()[1:]) if kmodstate & KMOD_CTRL: if amount > 0: self.zoom("zoom_in", mpos) else: self.zoom("zoom_out", mpos) elif kmodstate & KMOD_SHIFT: self.zpan(10 * amount) else: self.zpan(1 * amount) elapsed_time = sdltimer.get_ticks() - last_loop_ts if elapsed_time > render_choke: break sdltimer.delay(10)