03.交互--鼠標,鍵盤

"""交互
一個兩層的場景。一個顯示當前被按下的鍵(一次,一個或多個),另外一個顯示鼠標位置的文本,而後單擊以移動文本。
"""
import cocos
import pyglet
from cocos.director import director


class KeyDisplay(cocos.layer.Layer):
    # 容許圖層接收Director.window事件
    is_event_handler = True

    def __init__(self):
        super(KeyDisplay, self).__init__()
        self.text = cocos.text.Label('', x=100, y=280)
        # 記錄按鍵set集合
        # self.key_pressed = []
        self.key_pressed = set()
        self.update_text()
        self.add(self.text)

    def update_text(self):
        """更新text"""
        # 轉爲符號字符串
        key_names = [pyglet.window.key.symbol_string(k) for k in self.key_pressed]
        text = 'Keys:' + ','.join(key_names)
        self.text.element.text = text

    # 事件處理器, 向層添加事件處理器就是一個添加名爲 on_<event name(事件名)> 的方法
    def on_key_press(self, key, modifiers):
        """按下一個鍵
       'key'是一個常量,指示按下哪一個鍵,來自 pyglet.window.key
       'modifiers(修飾符)'是一個按位或幾個常量,指示哪些修飾符在按下時處於活動狀態(ctrl,shift,capslock等)
       """
        # self.key_pressed.append(key)
        self.key_pressed.add(key)
        self.update_text()

    def on_key_release(self, key, modifiers):
        """釋放一個鍵
         'key'是一個常量,指示按下哪一個鍵,來自 pyglet.window.key
         'modifiers(修飾符)'是一個按位或幾個常量,指示哪些修飾符在按下時處於活動狀態(ctrl,shift,capslock等)
         """
        self.key_pressed.remove(key)
        self.update_text()


class MouseDisplay(cocos.layer.Layer):
    """
    其餘:
    on_mouse_release : 當按鍵鬆開
    on_mouse_scroll : 當滾輪轉動
    on_mouse_leave : 當鼠標指針移除窗口
    on_mouse_enter : 當鼠標指針移入窗口
    """
    is_event_handler = True

    def __init__(self):
        super(MouseDisplay, self).__init__()

        self.posx = 100
        self.posy = 240
        self.text = cocos.text.Label('No mouse events', font_size=18, x=self.posx, y=self.posy)
        self.add(self.text)

    def update_text(self, x, y):
        text = 'Mouse @ %d,%d' % (x, y)
        self.text.element.text = text
        self.text.element.x = self.posx
        self.text.element.y = self.posy

    def on_mouse_motion(self, x, y, dx, dy):
        """當鼠標移動到應用程序窗口上而且沒有按鼠標按鍵時
        (x,y)是鼠標的物理座標 ,鼠標事件處理器從pyglet接收其物理座標
        (dx,dy)是鼠標至上次調用後通過的的距離向量
        """
        self.update_text(x, y)

    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        """此函數在鼠標在應用程序窗口上移動並按下鼠標按鍵時被調用
          (x,y)是鼠標的物理座標
          (dx,dy)是鼠標至上次調用後通過的的距離向量
          'buttons' 是一個按位或pyglet.window.mouse常量-->LEFT,MIDDLE,RIGHT
          'modifiers' 是一個按位或pyglet.window.key修飾符常量(值如 'SHIFT', 'OPTION', 'ALT')
        """
        self.update_text(x, y)

    def on_mouse_press(self, x, y, buttons, modifiers):
        """按下鼠標按鍵不只更新鼠標位置,還改變標籤的位置
        (x,y)是鼠標的物理座標
        'buttons' 是一個按位或pyglet.window.mouse常量-->LEFT,MIDDLE,RIGHT
        'modifiers' 是一個按位或pyglet.window.key修飾符常量(值如 'SHIFT', 'OPTION', 'ALT')
        """
        self.posx, self.posy = director.get_virtual_coordinates(x, y)
        self.update_text(x, y)


if __name__ == '__main__':
    cocos.director.director.init(resizable=True)  # 可調整大小
    keyDisplay_layer = KeyDisplay()
    mouseDisplay_layer = MouseDisplay()
    # 接收層列表
    main_scene = cocos.scene.Scene(keyDisplay_layer, mouseDisplay_layer)
    # 運行場景
    cocos.director.director.run(main_scene)
相關文章
相關標籤/搜索