lv2 / pugl

A minimal portable API for embeddable GUIs
https://gitlab.com/lv2/pugl/
ISC License
174 stars 34 forks source link

Pointer lock API / example for X11 #107

Open swesterfeld opened 11 months ago

swesterfeld commented 11 months ago

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:

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 commented 2 months ago

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