区分鼠标按下和鼠标按住

发布于 2024-10-20 11:59:38 字数 311 浏览 6 评论 0原文

我目前正在为 pyglet 开发一个小型图形库。该模块用于制作完全由线条组成的矢量图形。

在其开发过程中,我遇到了一个问题,单击拖动(并移动一个点)也会导致触发 on_mouse_press 事件,该事件在最后一个活动链接和您尝试拖动的点之间创建一个新链接。

我似乎想不出任何方法来解决这个问题,不会让在点之间创建链接感觉很滞后,我在 on_mouse_release 上创建了链接,这样我就可以确定在链接点之前鼠标是否已被拖动。

有没有其他人对我如何让它在不显得迟缓的情况下工作有任何好主意。

编辑:澄清我使用 pyglet 和 python

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一人独醉 2024-10-27 11:59:38
#!/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()

简短描述:

  • 跟踪窗口内当前的鼠标状态
  • 跟踪鼠标事件的起点
  • 在鼠标释放时,执行您想要执行的操作或更新拖动中的任何内容。

我知道这是一个老问题,但它处于“未回答”状态,因此希望能够解决它并从列表中删除。

#!/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.

过气美图社 2024-10-27 11:59:38

在 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.
}

当然,您需要调整它才能与您的代码一起使用,但这应该是一个好的开始。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文