示例#1
0
def sdl2_event_pump(events):
    # Feed events into the loop
    for event in sdl2.ext.get_events():
        if event.type == sdl2.SDL_QUIT:
            events.append(WindowEvent(WindowEvent.QUIT))
        elif event.type == sdl2.SDL_KEYDOWN:
            events.append(
                WindowEvent(
                    KEY_DOWN.get(event.key.keysym.sym, WindowEvent.PASS)))
        elif event.type == sdl2.SDL_KEYUP:
            events.append(
                WindowEvent(KEY_UP.get(event.key.keysym.sym,
                                       WindowEvent.PASS)))
        elif event.type == sdl2.SDL_WINDOWEVENT:
            if event.window.windowID == 1:
                if event.window.event == sdl2.SDL_WINDOWEVENT_FOCUS_LOST:
                    events.append(WindowEvent(WindowEvent.WINDOW_UNFOCUS))
                elif event.window.event == sdl2.SDL_WINDOWEVENT_FOCUS_GAINED:
                    events.append(WindowEvent(WindowEvent.WINDOW_FOCUS))
        elif event.type == sdl2.SDL_MOUSEMOTION or event.type == sdl2.SDL_MOUSEBUTTONUP:
            mouse_button = -1
            if event.type == sdl2.SDL_MOUSEBUTTONUP:
                if event.button.button == sdl2.SDL_BUTTON_LEFT:
                    mouse_button = 0
                elif event.button.button == sdl2.SDL_BUTTON_RIGHT:
                    mouse_button = 1

            events.append(
                WindowEventMouse(WindowEvent._INTERNAL_MOUSE,
                                 window_id=event.motion.windowID,
                                 mouse_x=event.motion.x,
                                 mouse_y=event.motion.y,
                                 mouse_button=mouse_button))
    return events
示例#2
0
    def on_key_press(self, key, key_modifiers):
        actions = []
        print("KEY PRESSED: {}".format(key))

        # switch statement to create WindowEvents (button commands for PyBoy)
        if (key == arcade.key.UP):
            actions = [WindowEvent.PRESS_ARROW_UP, WindowEvent.RELEASE_ARROW_UP]
        elif (key == arcade.key.DOWN):
            actions = [WindowEvent.PRESS_ARROW_DOWN, WindowEvent.RELEASE_ARROW_DOWN]
        elif (key == arcade.key.LEFT):
            actions = [WindowEvent.PRESS_ARROW_LEFT, WindowEvent.RELEASE_ARROW_LEFT]
        elif (key == arcade.key.RIGHT):
            actions = [WindowEvent.PRESS_ARROW_RIGHT, WindowEvent.RELEASE_ARROW_RIGHT]
        elif (key == arcade.key.Z):
            actions = [WindowEvent.PRESS_BUTTON_B, WindowEvent.RELEASE_BUTTON_B]
        elif (key == arcade.key.X):
            actions = [WindowEvent.PRESS_BUTTON_A, WindowEvent.RELEASE_BUTTON_A]
        elif (key == arcade.key.ENTER):
            actions = [WindowEvent.PRESS_BUTTON_SELECT, WindowEvent.RELEASE_BUTTON_SELECT]
        elif (key == arcade.key.RSHIFT):
            actions = [WindowEvent.PRESS_BUTTON_START, WindowEvent.RELEASE_BUTTON_START]

        # if actions list isn't empty, send commands to pyboy and tick the pyboy window to process each command
        if actions:
            for action in actions:
                self.pyboy.send_input(WindowEvent(action))
                self.pyboy.tick()
示例#3
0
文件: pyboy.py 项目: chrsbell/PyBoy
    def send_input(self, event):
        """
        Send a single input to control the emulator. This is both Game Boy buttons and emulator controls.

        See `pyboy.WindowEvent` for which events to send.

        Args:
            event (pyboy.WindowEvent): The event to send
        """
        self.events.append(WindowEvent(event))
示例#4
0
def sdl2_event_pump(events):
    global _sdlcontroller
    # Feed events into the loop
    for event in sdl2.ext.get_events():
        if event.type == sdl2.SDL_QUIT:
            events.append(WindowEvent(WindowEvent.QUIT))
        elif event.type == sdl2.SDL_KEYDOWN:
            events.append(
                WindowEvent(
                    KEY_DOWN.get(event.key.keysym.sym, WindowEvent.PASS)))
        elif event.type == sdl2.SDL_KEYUP:
            events.append(
                WindowEvent(KEY_UP.get(event.key.keysym.sym,
                                       WindowEvent.PASS)))
        elif event.type == sdl2.SDL_WINDOWEVENT:
            if event.window.windowID == 1:
                if event.window.event == sdl2.SDL_WINDOWEVENT_FOCUS_LOST:
                    events.append(WindowEvent(WindowEvent.WINDOW_UNFOCUS))
                elif event.window.event == sdl2.SDL_WINDOWEVENT_FOCUS_GAINED:
                    events.append(WindowEvent(WindowEvent.WINDOW_FOCUS))
        elif event.type == sdl2.SDL_MOUSEWHEEL:
            events.append(
                WindowEventMouse(WindowEvent._INTERNAL_MOUSE,
                                 window_id=event.motion.windowID,
                                 mouse_scroll_x=event.wheel.x,
                                 mouse_scroll_y=event.wheel.y))
        elif event.type == sdl2.SDL_MOUSEMOTION or event.type == sdl2.SDL_MOUSEBUTTONUP:
            mouse_button = -1
            if event.type == sdl2.SDL_MOUSEBUTTONUP:
                if event.button.button == sdl2.SDL_BUTTON_LEFT:
                    mouse_button = 0
                elif event.button.button == sdl2.SDL_BUTTON_RIGHT:
                    mouse_button = 1

            events.append(
                WindowEventMouse(WindowEvent._INTERNAL_MOUSE,
                                 window_id=event.motion.windowID,
                                 mouse_x=event.motion.x,
                                 mouse_y=event.motion.y,
                                 mouse_button=mouse_button))
        elif event.type == sdl2.SDL_CONTROLLERDEVICEADDED:
            _sdlcontroller = sdl2.SDL_GameControllerOpen(event.cdevice.which)
        elif event.type == sdl2.SDL_CONTROLLERDEVICEREMOVED:
            sdl2.SDL_GameControllerClose(_sdlcontroller)
        elif event.type == sdl2.SDL_CONTROLLERBUTTONDOWN:
            events.append(
                WindowEvent(
                    CONTROLLER_DOWN.get(event.cbutton.button,
                                        WindowEvent.PASS)))
        elif event.type == sdl2.SDL_CONTROLLERBUTTONUP:
            events.append(
                WindowEvent(
                    CONTROLLER_UP.get(event.cbutton.button, WindowEvent.PASS)))
    return events
示例#5
0
 def _glkeyboardspecial(self, c, x, y, up):
     if up:
         if c == GLUT_KEY_UP:
             self.events.append(WindowEvent(WindowEvent.RELEASE_ARROW_UP))
         if c == GLUT_KEY_DOWN:
             self.events.append(WindowEvent(WindowEvent.RELEASE_ARROW_DOWN))
         if c == GLUT_KEY_LEFT:
             self.events.append(WindowEvent(WindowEvent.RELEASE_ARROW_LEFT))
         if c == GLUT_KEY_RIGHT:
             self.events.append(WindowEvent(
                 WindowEvent.RELEASE_ARROW_RIGHT))
     else:
         if c == GLUT_KEY_UP:
             self.events.append(WindowEvent(WindowEvent.PRESS_ARROW_UP))
         if c == GLUT_KEY_DOWN:
             self.events.append(WindowEvent(WindowEvent.PRESS_ARROW_DOWN))
         if c == GLUT_KEY_LEFT:
             self.events.append(WindowEvent(WindowEvent.PRESS_ARROW_LEFT))
         if c == GLUT_KEY_RIGHT:
             self.events.append(WindowEvent(WindowEvent.PRESS_ARROW_RIGHT))
示例#6
0
 def _glkeyboard(self, c, x, y, up):
     if up:
         if c == "a":
             self.events.append(WindowEvent(WindowEvent.RELEASE_BUTTON_A))
         elif c == "s":
             self.events.append(WindowEvent(WindowEvent.RELEASE_BUTTON_B))
         elif c == "z":
             self.events.append(WindowEvent(WindowEvent.STATE_SAVE))
         elif c == "x":
             self.events.append(WindowEvent(WindowEvent.STATE_LOAD))
         elif c == " ":
             self.events.append(WindowEvent(WindowEvent.RELEASE_SPEED_UP))
         elif c == chr(8):
             self.events.append(
                 WindowEvent(WindowEvent.RELEASE_BUTTON_SELECT))
         elif c == chr(13):
             self.events.append(
                 WindowEvent(WindowEvent.RELEASE_BUTTON_START))
         elif c == "o":
             self.events.append(WindowEvent(WindowEvent.SCREENSHOT_RECORD))
     else:
         if c == "a":
             self.events.append(WindowEvent(WindowEvent.PRESS_BUTTON_A))
         elif c == "s":
             self.events.append(WindowEvent(WindowEvent.PRESS_BUTTON_B))
         elif c == chr(27):
             self.events.append(WindowEvent(WindowEvent.QUIT))
         elif c == " ":
             self.events.append(WindowEvent(WindowEvent.PRESS_SPEED_UP))
         elif c == "i":
             self.events.append(
                 WindowEvent(WindowEvent.SCREEN_RECORDING_TOGGLE))
         elif c == chr(8):
             self.events.append(WindowEvent(
                 WindowEvent.PRESS_BUTTON_SELECT))
         elif c == chr(13):
             self.events.append(WindowEvent(WindowEvent.PRESS_BUTTON_START))