Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pointer lock API / example for X11 #107

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

swesterfeld
Copy link
Contributor

As discussed in issue #104 it would be great if pugl supported grabbing/locking the mouse cursor. Here is my attempt to support this in pugl. I've only implemented the X11 version. The API has been inspired by the Qt API. They don't really have an extra "lock/grab the pointer" functionality. Instead, you can build your own based on two primitives:

  • hide the mouse cursor
  • set the mouse cursor position

So this branch adds these two primitives and then a pugl pointer lock example which has a simple control that will "lock" the pointer while pressed, supporting normal and fine adjustment (which would hit the border of the screen without pointer lock).

From an API point of view the difference between Qt and my suggestion is that in the Qt API you set the cursor position with global coordinates, whereas here this is using local coordinates relative to the view. I don't know if there are good reasons to use global coordinates in pugl.

For comparision, here is how you can implement a pointer lock using Qt:

#include <QApplication>
#include <QWidget>
#include <QCursor>
#include <QMouseEvent>

class CustomWidget : public QWidget
{
public:
  CustomWidget(QWidget *parent = nullptr) : QWidget(parent)
  {
  }

protected:
  QPointF orig_pos;
  QPointF acc_pos;
  bool dragging = false;

  void
  mousePressEvent (QMouseEvent *event) override
  {
    if (event->button() == Qt::LeftButton)
      {
        setCursor(Qt::BlankCursor);
        orig_pos = mapToGlobal (event->pos());
        acc_pos = QPointF (0, 0);
        dragging = true;
      }
  }

  void
  mouseMoveEvent (QMouseEvent *event) override
  {
    if (dragging)
      {
        auto global_pos = mapToGlobal (event->pos());
        if (global_pos != orig_pos)
          {
            auto delta_pos = global_pos - orig_pos;
            acc_pos += delta_pos;
            printf ("%f %f\n", acc_pos.x(), acc_pos.y());
            QCursor::setPos (orig_pos.x(), orig_pos.y());
          }
      }
  }

  void
  mouseReleaseEvent (QMouseEvent *event) override
  {
    if (event->button() == Qt::LeftButton)
      {
        QCursor::setPos (orig_pos.x(), orig_pos.y());
        setCursor (Qt::ArrowCursor);
        dragging = false;
      }
  }
};

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);

  CustomWidget widget;
  widget.setGeometry (100, 100, 400, 300);
  widget.show();

  return app.exec();
}

@drobilla
Copy link
Collaborator

Thanks. Sorry for the massive delay, I'll try to find some time to implement this on the other platforms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants