2011-03-07 7 views
2

현재 Im은 pyglet을위한 작은 그래픽 라이브러리에서 작업 중입니다. 이 모듈은 벡터 그래픽을 모두 선으로 구성하는 데 사용됩니다.마우스 다운과 마우스 홀드의 구분

나는 그 지점에서 드래그 (그리고 점 이동)를 클릭하면 마지막 활성 링크와 드래그하려는 점 사이에 새로운 링크를 생성하는 on_mouse_press 이벤트가 발생하는 문제가 발생했습니다.

나는이 점을 해결하기 위해 dosent가 포인트 사이의 링크를 만드는 것을 생각할 것 같지 않은데, 링크를 만들 때 on_mouse_release 링크를 대신 만들었 기 때문에 포인트를 연결하기 전에 마우스를 드래그했는지 확인할 수 있습니다.

다른 사람들이 뒤늦게 나타나지 않고 어떻게 작동하는지 알 수있는 아이디어가 있습니까?

편집 : MouseDown 다른 이벤트가 무시되어야 함을 나타 내기 위해 부울 변수를 설정 파이썬

답변

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

0

와 pyglet를 사용하여 메신저를 명확히한다.

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

첫 번째로 pyglet을 python과 함께 사용하고 있지만, 어쨌든 당신이 얻고있는 것을 얻을 수 있습니다. 내 문제는 내가 드래그 이벤트를 감지 할 수 없다는 말은 아니지만, 드래그 이벤트가 시작될 때 on_mouse_press가 여전히 – Hugoagogo

+0

이라고 불리우는 것으로 알고 있지만 솔루션이 매우 다른 구문 등으로 비슷하다고 생각합니다. 파이썬에 익숙하지 않아서 도움이 될지 모르겠습니다! –

+0

Nah 나는 그것을 견딜 수 있지만 다른 문제를 해결하고있다 – Hugoagogo

관련 문제