Im currently working on a little graphics library for pyglet. The module is used to开发者_如何学运维 make vector graphics composed entirely of lines.
In its develoment i have have run into a problem where, clicking to drag (and move a point) also causes fires an on_mouse_press event that creates a new link between the last active link and the point you are trying to drag.
I cant seem to think of any way to fix this that dosent make creating links between points feel laggy, i have creating links on_mouse_release instead so that i could determine if the mouse had been draged before linking the point.
Does anyone else have any bright ideas on how i can get this to work without appearing laggy.
EDIT: to clarify im using pyglet with python
#!/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()
Short description:
- Keep track of the current mouse state within the window
- Keep track of startpoint of mouse event
- At mouse release, do waht you want to do or update whatever it is within the drag.
I know it's an old question, but it's under the "unanswered" so hoping to get it resolved and out of the list.
On MouseDown set a boolean variable to indicate that other events should be ignored.
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.
}
You'll need to tweak this to work with your code of course but it should be a good start.
精彩评论