#include <GLFW/glfw3.h>
+namespace
+{
+ //! Convert GLFW mouse button into Aspect_VKeyMouse.
+ static Aspect_VKeyMouse mouseButtonFromGlfw (int theButton)
+ {
+ switch (theButton)
+ {
+ case GLFW_MOUSE_BUTTON_LEFT: return Aspect_VKeyMouse_LeftButton;
+ case GLFW_MOUSE_BUTTON_RIGHT: return Aspect_VKeyMouse_RightButton;
+ case GLFW_MOUSE_BUTTON_MIDDLE: return Aspect_VKeyMouse_MiddleButton;
+ }
+ return Aspect_VKeyMouse_NONE;
+ }
+
+ //! Convert GLFW key modifiers into Aspect_VKeyFlags.
+ static Aspect_VKeyFlags keyFlagsFromGlfw (int theFlags)
+ {
+ Aspect_VKeyFlags aFlags = Aspect_VKeyFlags_NONE;
+ if ((theFlags & GLFW_MOD_SHIFT) != 0)
+ {
+ aFlags |= Aspect_VKeyFlags_SHIFT;
+ }
+ if ((theFlags & GLFW_MOD_CONTROL) != 0)
+ {
+ aFlags |= Aspect_VKeyFlags_CTRL;
+ }
+ if ((theFlags & GLFW_MOD_ALT) != 0)
+ {
+ aFlags |= Aspect_VKeyFlags_ALT;
+ }
+ if ((theFlags & GLFW_MOD_SUPER) != 0)
+ {
+ aFlags |= Aspect_VKeyFlags_META;
+ }
+ return aFlags;
+ }
+}
+
// ================================================================
// Function : GlfwOcctView
// Purpose :
// ================================================================
GlfwOcctView::GlfwOcctView()
-: myCurAction3d (CurAction3d_Nothing),
- myToRedraw (true)
{
}
glfwWaitEvents();
if (!myView.IsNull())
{
- if (myView->IsInvalidated())
- {
- myView->Redraw();
- }
- else if (myToRedraw)
- {
- myView->RedrawImmediate();
- }
- myToRedraw = false;
+ FlushViewEvents (myContext, myView, true);
}
}
}
myView->MustBeResized();
myView->Invalidate();
myView->Redraw();
- //myToRedraw = true;
}
}
// ================================================================
void GlfwOcctView::onMouseScroll (double theOffsetX, double theOffsetY)
{
- if (myView.IsNull()) { return; }
-
- const Graphic3d_Vec2i aPos = myOcctWindow->CursorPosition();
- myView->StartZoomAtPoint (aPos.x(), aPos.y());
- myView->ZoomAtPoint (0, 0, int(theOffsetY * 4.0), int(theOffsetY * 4.0));
- myView->Invalidate();
- myToRedraw = true;
+ if (!myView.IsNull())
+ {
+ UpdateZoom (Aspect_ScrollDelta (myOcctWindow->CursorPosition(), int(theOffsetY * 8.0)));
+ }
}
// ================================================================
if (myView.IsNull()) { return; }
const Graphic3d_Vec2i aPos = myOcctWindow->CursorPosition();
- if (theAction != GLFW_PRESS)
+ if (theAction == GLFW_PRESS)
{
- myCurAction3d = CurAction3d_Nothing;
- return;
+ PressMouseButton (aPos, mouseButtonFromGlfw (theButton), keyFlagsFromGlfw (theMods), false);
}
-
- myMouseMin = aPos;
- myMouseMax = aPos;
- switch (theButton)
+ else
{
- case GLFW_MOUSE_BUTTON_RIGHT:
- {
- myCurAction3d = CurAction3d_DynamicRoation;
- myView->StartRotation (aPos.x(), aPos.y());
- break;
- }
- case GLFW_MOUSE_BUTTON_MIDDLE:
- {
- myCurAction3d = CurAction3d_DynamicPanning;
- break;
- }
+ ReleaseMouseButton (aPos, mouseButtonFromGlfw (theButton), keyFlagsFromGlfw (theMods), false);
}
}
// ================================================================
void GlfwOcctView::onMouseMove (int thePosX, int thePosY)
{
- if (myView.IsNull()) { return; }
-
- switch (myCurAction3d)
+ const Graphic3d_Vec2i aNewPos (thePosX, thePosY);
+ if (!myView.IsNull())
{
- case CurAction3d_DynamicRoation:
- {
- myView->Rotation (thePosX, thePosY);
- myView->Invalidate();
- myToRedraw = true;
- break;
- }
- case CurAction3d_DynamicPanning:
- {
- myView->Pan (thePosX - myMouseMax.x(), -(thePosY - myMouseMax.y()));
- myView->Invalidate();
- myToRedraw = true;
- myMouseMax.SetValues (thePosX, thePosY);
- break;
- }
- default:
- {
- myContext->MoveTo (thePosX, thePosY, myView, false);
- myToRedraw = true;
- break;
- }
+ UpdateMousePosition (aNewPos, PressedMouseButtons(), LastMouseFlags(), false);
}
}
#include "GlfwOcctWindow.h"
#include <AIS_InteractiveContext.hxx>
+#include <AIS_ViewController.hxx>
#include <V3d_View.hxx>
//! Sample class creating 3D Viewer within GLFW window.
-class GlfwOcctView
+class GlfwOcctView : protected AIS_ViewController
{
-public:
- enum CurAction3d
- {
- CurAction3d_Nothing,
- CurAction3d_DynamicZooming,
- CurAction3d_DynamicPanning,
- CurAction3d_DynamicRoation
- };
-
public:
//! Default constructor.
GlfwOcctView();
Handle(V3d_View) myView;
Handle(AIS_InteractiveContext) myContext;
- CurAction3d myCurAction3d;
- Graphic3d_Vec2i myMouseMin;
- Graphic3d_Vec2i myMouseMax;
- bool myToRedraw;
-
};
#endif // _GlfwOcctView_Header