我目前正在为pyglet开发一个小图形库。该模块用于制作完全由线组成的矢量图形。
在它的开发中,我遇到了一个问题,点击拖动(并移动一个点)也会导致on_mouse_press事件触发,该事件在最后一个活动链接和你试图拖动的点之间创建一个新链接。 / p>
我似乎无法想出任何方法来解决这个问题,使得创建点之间的链接感觉迟钝,我已经创建链接on_mouse_release,以便我可以确定鼠标是否在链接点之前已被拖动。
是否有其他人对如何在不显示迟滞的情况下实现这一点有任何明智的想法。
编辑:使用pyglet与python
澄清即时通讯答案 0 :(得分: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()
简短说明:
我知道这是一个老问题,但它是在“未答复”的情况下,所以希望能够解决它并从列表中删除。
答案 1 :(得分: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.
}
您需要调整它以使用您的代码,但它应该是一个良好的开端。