1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-03 17:56:21 +03:00

0031056: Samples - update glfw sample to use AIS_ViewController

This commit is contained in:
kgv 2019-10-10 17:09:07 +03:00 committed by apn
parent e1d039d56d
commit 3818aacaa5
2 changed files with 52 additions and 76 deletions

View File

@ -35,13 +35,49 @@
#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)
{
}
@ -193,15 +229,7 @@ void GlfwOcctView::mainloop()
glfwWaitEvents();
if (!myView.IsNull())
{
if (myView->IsInvalidated())
{
myView->Redraw();
}
else if (myToRedraw)
{
myView->RedrawImmediate();
}
myToRedraw = false;
FlushViewEvents (myContext, myView, true);
}
}
}
@ -237,7 +265,6 @@ void GlfwOcctView::onResize (int theWidth, int theHeight)
myView->MustBeResized();
myView->Invalidate();
myView->Redraw();
//myToRedraw = true;
}
}
@ -247,13 +274,10 @@ void GlfwOcctView::onResize (int theWidth, int theHeight)
// ================================================================
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)));
}
}
// ================================================================
@ -265,27 +289,13 @@ void GlfwOcctView::onMouseButton (int theButton, int theAction, int theMods)
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);
}
}
@ -295,30 +305,9 @@ void GlfwOcctView::onMouseButton (int theButton, int theAction, int theMods)
// ================================================================
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);
}
}

View File

@ -25,20 +25,12 @@
#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();
@ -115,11 +107,6 @@ private:
Handle(V3d_View) myView;
Handle(AIS_InteractiveContext) myContext;
CurAction3d myCurAction3d;
Graphic3d_Vec2i myMouseMin;
Graphic3d_Vec2i myMouseMax;
bool myToRedraw;
};
#endif // _GlfwOcctView_Header