2011-03-07 119 views
2

我目前正在爲pyglet製作一個小圖形庫。該模塊用於製作完全由線組成的矢量圖形。區分鼠標按下和鼠標按下

在它的開發中,我遇到了一個問題,點擊拖動(並移動一個點)也會引發on_mouse_press事件,這會在最後一個活動鏈接和您要拖動的點之間創建一個新鏈接。

我似乎想不出任何方法來解決這個問題,dosent使點之間創建鏈接感覺laggy,我已經創建鏈接on_mouse_release,而不是讓我可以確定是否鼠標已連接點之前被拖動。

有沒有其他人有什麼好的想法,我可以如何讓這個工作沒有出現laggy。

編輯:澄清使用pyglet IM與蟒蛇

回答

3
#!/usr/bin/python 
import pyglet 
from time import time, sleep 

class Window(pyglet.window.Window): 
    def __init__(self, refreshrate): 
     super(Window, self).__init__(vsync = False) 
     self.frames = 0 
     self.framerate = pyglet.text.Label(text='Unknown', font_name='Verdana', font_size=8, x=10, y=10, color=(255,255,255,255)) 
     self.last = time() 
     self.alive = 1 
     self.refreshrate = refreshrate 
     self.click = None 
     self.drag = False 

    def on_draw(self): 
     self.render() 

    def on_mouse_press(self, x, y, button, modifiers): 
     self.click = x,y 

    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): 
     if self.click: 
      self.drag = True 
      print 'Drag offset:',(dx,dy) 

    def on_mouse_release(self, x, y, button, modifiers): 
     if not self.drag and self.click: 
      print 'You clicked here', self.click, 'Relese point:',(x,y) 
     else: 
      print 'You draged from', self.click, 'to:',(x,y) 
     self.click = None 
     self.drag = False 

    def render(self): 
     self.clear() 
     if time() - self.last >= 1: 
      self.framerate.text = str(self.frames) 
      self.frames = 0 
      self.last = time() 
     else: 
      self.frames += 1 
     self.framerate.draw() 
     self.flip() 

    def on_close(self): 
     self.alive = 0 

    def run(self): 
     while self.alive: 
      self.render() 
      # ----> Note: <---- 
      # Without self.dispatc_events() the screen will freeze 
      # due to the fact that i don't call pyglet.app.run(), 
      # because i like to have the control when and what locks 
      # the application, since pyglet.app.run() is a locking call. 
      event = self.dispatch_events() 
      sleep(1.0/self.refreshrate) 

win = Window(23) # set the fps 
win.run() 

簡短說明:

  • 跟蹤當前鼠標狀態的窗口內
  • 跟蹤鼠標事件
  • 的起始點在鼠標釋放,做你想做的事啥子或更新拖動內的任何內容。

我知道這是一個古老的問題,但它是在「未答覆」之下,所以希望得到它解決和列表。

+0

還沒有驗證,但這看起來是一個很好的解決方案 – Hugoagogo 2013-02-20 11:14:21

0

上的MouseDown設置一個布爾變量來表示其他事件應該被忽略。

private bool IgnoreEvents { set; get; } 

... 

protected void Control_MouseDown(object sender, EventArgs e) 
{ 
    //Dragging or holding. 
    this.IgnoreEvents = true; 
} 

protected void Control_MouseUp(object sender, EventArgs e) 
{ 
    //Finished dragging or holding. 
    this.IgnoreEvents = false; 
} 

protected void OtherControl_SomeOtherEvent(object sender, EventArgs e) 
{ 
    if (this.IgnoreEvents) 
     return; 

    //Otherwise to normal stuff here. 
} 

你需要調整它以適應你的代碼,但它應該是一個好的開始。

+0

首先即時使用Python的pyglet,但我有種得到你在無論如何得到。我的問題是,我無法檢測到拖動事件,但是,在拖動事件開始時,on_mouse_press仍然被稱爲 – Hugoagogo 2011-03-07 04:31:34

+0

我明白,但認爲解決方案可能類似(語法非常不同)等。我不是特別熟悉Python,所以我不確定我能幫上忙。 – 2011-03-07 04:34:04

+0

Nah我可以承受它,但它正在解決一個不同的問題 – Hugoagogo 2011-03-08 05:09:43