Skip to content

Commit

Permalink
Merge pull request solvcon#408 from Tucchhaa/reassign_camera_keys
Browse files Browse the repository at this point in the history
Camera: Add movement items to view menu
  • Loading branch information
yungyuc authored Aug 17, 2024
2 parents 7c1bc46 + e2bedb5 commit 194a313
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 136 deletions.
11 changes: 5 additions & 6 deletions cpp/modmesh/view/R3DWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ R3DWidget::R3DWidget(Qt3DExtras::Qt3DWindow * window, RScene * scene, QWidget *
{
m_view->setRootEntity(m_scene);

resetCamera(m_view->camera());
resetCamera();

if (Toggle::instance().fixed().get_show_axis())
{
Expand Down Expand Up @@ -99,14 +99,13 @@ void R3DWidget::resizeEvent(QResizeEvent * event)
m_container->resize(event->size());
}

void R3DWidget::resetCamera(Qt3DRender::QCamera * camera,
float positionX,
float positionY,
float positionZ)
void R3DWidget::resetCamera() const
{
Qt3DRender::QCamera * camera = m_view->camera();

// Set up the camera.
camera->lens()->setPerspectiveProjection(45.0f, 16.0f / 9.0f, 0.1f, 1000.0f);
camera->setPosition(QVector3D(positionX, positionY, positionZ));
camera->setPosition(QVector3D(0.0f, 0.0f, 10.0f));
camera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
camera->setUpVector(QVector3D(0.f, 1.f, 0.f));

Expand Down
5 changes: 1 addition & 4 deletions cpp/modmesh/view/R3DWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ class R3DWidget
Qt3DExtras::QAbstractCameraController * qtCameraController() { return m_scene->controller(); }
CameraController * cameraController() { return dynamic_cast<CameraController *>(m_scene->controller()); }

void resetCamera(Qt3DRender::QCamera * camera,
float positionX = 0.0f,
float positionY = 0.0f,
float positionZ = 10.0f);
void resetCamera() const;

QPixmap grabPixmap() const { return m_view->screen()->grabWindow(m_view->winId()); }

Expand Down
49 changes: 23 additions & 26 deletions cpp/modmesh/view/RCameraController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ RCameraInputListener::RCameraInputListener(
, m_right_mouse_button_action(new Qt3DInput::QAction)
, m_shift_button_action(new Qt3DInput::QAction)
, m_alt_button_action(new Qt3DInput::QAction)
, m_ctrl_button_action(new Qt3DInput::QAction)
, m_left_mouse_button_input(new Qt3DInput::QActionInput)
, m_middle_mouse_button_input(new Qt3DInput::QActionInput)
, m_right_mouse_button_input(new Qt3DInput::QActionInput)
, m_shift_button_input(new Qt3DInput::QActionInput)
, m_alt_button_input(new Qt3DInput::QActionInput)
, m_ctrl_button_input(new Qt3DInput::QActionInput)
, m_mouse_rx_input(new Qt3DInput::QAnalogAxisInput)
, m_mouse_ry_input(new Qt3DInput::QAnalogAxisInput)
, m_mouse_tz_x_input(new Qt3DInput::QAnalogAxisInput)
Expand All @@ -80,13 +82,14 @@ RCameraInputListener::RCameraInputListener(
this,
[this](const float dt)
{
Qt3DExtras::QAbstractCameraController::InputState state{};
CameraInputState state{};
const bool isCtrlPressed = m_ctrl_button_action->isActive();

state.rxAxisValue = m_rx_axis->value();
state.ryAxisValue = m_ry_axis->value();
state.txAxisValue = m_tx_axis->value();
state.tyAxisValue = m_ty_axis->value();
state.tzAxisValue = m_tz_axis->value();
state.tyAxisValue = isCtrlPressed ? 0.0f : m_ty_axis->value();
state.tzAxisValue = (isCtrlPressed ? m_ty_axis->value() : 0.0f) + m_tz_axis->value();

state.leftMouseButtonActive = m_left_mouse_button_action->isActive();
state.middleMouseButtonActive = m_middle_mouse_button_action->isActive();
Expand All @@ -109,6 +112,7 @@ void RCameraInputListener::init()
m_logical_device->addAction(m_right_mouse_button_action);
m_logical_device->addAction(m_alt_button_action);
m_logical_device->addAction(m_shift_button_action);
m_logical_device->addAction(m_ctrl_button_action);

m_logical_device->addAxis(m_rx_axis);
m_logical_device->addAxis(m_ry_axis);
Expand Down Expand Up @@ -178,55 +182,48 @@ void RCameraInputListener::initKeyboardListeners() const
m_alt_button_input->setSourceDevice(m_keyboard_device);
m_alt_button_action->addInput(m_alt_button_input);

// ctrl button - On Windows Ctrl key, on Macs Cmd key
m_ctrl_button_input->setButtons(QList<int>{Qt::Key_Control});
m_ctrl_button_input->setSourceDevice(m_keyboard_device);
m_ctrl_button_action->addInput(m_ctrl_button_input);

// keyboard positive x translation
m_keyboard_tx_pos_input->setButtons(QList<int>{Qt::Key_D, Qt::Key_Right});
m_keyboard_tx_pos_input->setScale(1.0f);
m_keyboard_tx_pos_input->setSourceDevice(m_keyboard_device);
m_tx_axis->addInput(m_keyboard_tx_pos_input);

// keyboard positive y translation
m_keyboard_ty_pos_input->setButtons(QList<int>{Qt::Key_E, Qt::Key_PageUp});
m_keyboard_ty_pos_input->setButtons(QList<int>{Qt::Key_W, Qt::Key_Up});
m_keyboard_ty_pos_input->setScale(1.0f);
m_keyboard_ty_pos_input->setSourceDevice(m_keyboard_device);
m_ty_axis->addInput(m_keyboard_ty_pos_input);

// keyboard positive z translation
m_keyboard_tz_pos_input->setButtons(QList<int>{Qt::Key_W, Qt::Key_Up});
m_keyboard_tz_pos_input->setScale(1.0f);
m_keyboard_tz_pos_input->setSourceDevice(m_keyboard_device);
m_tz_axis->addInput(m_keyboard_tz_pos_input);

// keyboard negative x translation
m_keyboard_tx_neg_input->setButtons(QList<int>{Qt::Key_A, Qt::Key_Left});
m_keyboard_tx_neg_input->setScale(-1.0f);
m_keyboard_tx_neg_input->setSourceDevice(m_keyboard_device);
m_tx_axis->addInput(m_keyboard_tx_neg_input);

// keyboard negative y translation
m_keyboard_ty_neg_input->setButtons(QList<int>{Qt::Key_Q, Qt::Key_PageDown});
m_keyboard_ty_neg_input->setButtons(QList<int>{Qt::Key_S, Qt::Key_Down});
m_keyboard_ty_neg_input->setScale(-1.0f);
m_keyboard_ty_neg_input->setSourceDevice(m_keyboard_device);
m_ty_axis->addInput(m_keyboard_ty_neg_input);

// keyboard negative z translation
m_keyboard_tz_neg_input->setButtons(QList<int>{Qt::Key_S, Qt::Key_Down});
m_keyboard_tz_neg_input->setScale(-1.0f);
m_keyboard_tz_neg_input->setSourceDevice(m_keyboard_device);
m_tz_axis->addInput(m_keyboard_tz_neg_input);
}

RFirstPersonCameraController::RFirstPersonCameraController(QNode * parent)
: QFirstPersonCameraController(parent)
{
auto callback = [this](const InputState & state, const float dt)
auto callback = [this](const CameraInputState & state, const float dt)
{
updateCameraPosition(state, dt);
};

m_listener = new RCameraInputListener(keyboardDevice(), mouseDevice(), callback, this);
}

void RFirstPersonCameraController::updateCameraPosition(const InputState & input, const float dt)
void RFirstPersonCameraController::updateCameraPosition(const CameraInputState & input, const float dt)
{
constexpr auto positiveY = QVector3D(0.f, 1.f, 0.f);

Expand All @@ -249,15 +246,15 @@ void RFirstPersonCameraController::updateCameraPosition(const InputState & input
ROrbitCameraController::ROrbitCameraController(QNode * parent)
: QOrbitCameraController(parent)
{
auto callback = [this](const InputState & state, const float dt)
auto callback = [this](const CameraInputState & state, const float dt)
{
updateCameraPosition(state, dt);
};

m_listener = new RCameraInputListener(keyboardDevice(), mouseDevice(), callback, this);
}

void ROrbitCameraController::updateCameraPosition(const InputState & input, const float dt)
void ROrbitCameraController::updateCameraPosition(const CameraInputState & input, const float dt)
{
if (camera() == nullptr)
return;
Expand Down Expand Up @@ -290,17 +287,17 @@ void ROrbitCameraController::updateCameraPosition(const InputState & input, cons
// keyboard Input
if (input.altKeyActive)
{
orbit(input.txAxisValue * dt, input.tzAxisValue * dt);
orbit(input.txAxisValue * dt, input.tyAxisValue * dt);
}
else if (input.shiftKeyActive)
{
zoom(input.tzAxisValue * linearSpeed() * dt);
zoom(input.tyAxisValue * linearSpeed() * dt);
}
else
{
const float x = clamp(input.txAxisValue + (input.leftMouseButtonActive ? input.rxAxisValue : 0));
const float y = clamp(input.tyAxisValue + (input.leftMouseButtonActive ? input.ryAxisValue : 0));
const auto translation = QVector3D(x, y, input.tzAxisValue) * linearSpeed() * dt;
const auto translation = QVector3D(
input.txAxisValue, input.tyAxisValue, input.tzAxisValue) *
linearSpeed() * dt;

camera()->translate(translation);
}
Expand Down
46 changes: 38 additions & 8 deletions cpp/modmesh/view/RCameraController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,34 @@
namespace modmesh
{

struct CameraInputState
{
float rxAxisValue = 0;
float ryAxisValue = 0;
float txAxisValue = 0;
float tyAxisValue = 0;
float tzAxisValue = 0;

bool leftMouseButtonActive = false;
bool middleMouseButtonActive = false;
bool rightMouseButtonActive = false;

bool altKeyActive = false;
bool shiftKeyActive = false;
}; /* end struct CameraInputState */

enum class CameraControllerType
{
FirstPerson,
Orbit
};

class RCameraInputListener : public Qt3DCore::QEntity
{
Q_OBJECT

public:
using callback_type = std::function<void(const Qt3DExtras::QAbstractCameraController::InputState &, float)>;
using callback_type = std::function<void(const CameraInputState &, float)>;

RCameraInputListener(
Qt3DInput::QKeyboardDevice * keyboardDevice,
Expand Down Expand Up @@ -84,11 +106,13 @@ class RCameraInputListener : public Qt3DCore::QEntity
Qt3DInput::QAction * m_right_mouse_button_action;
Qt3DInput::QAction * m_shift_button_action;
Qt3DInput::QAction * m_alt_button_action;
Qt3DInput::QAction * m_ctrl_button_action;
Qt3DInput::QActionInput * m_left_mouse_button_input;
Qt3DInput::QActionInput * m_middle_mouse_button_input;
Qt3DInput::QActionInput * m_right_mouse_button_input;
Qt3DInput::QActionInput * m_shift_button_input;
Qt3DInput::QActionInput * m_alt_button_input;
Qt3DInput::QActionInput * m_ctrl_button_input;
// mouse rotation input
Qt3DInput::QAnalogAxisInput * m_mouse_rx_input;
Qt3DInput::QAnalogAxisInput * m_mouse_ry_input;
Expand All @@ -102,21 +126,23 @@ class RCameraInputListener : public Qt3DCore::QEntity
Qt3DInput::QButtonAxisInput * m_keyboard_tx_neg_input;
Qt3DInput::QButtonAxisInput * m_keyboard_ty_neg_input;
Qt3DInput::QButtonAxisInput * m_keyboard_tz_neg_input;
};
}; /* end class RCameraInputListener */

class CameraController
{
public:
virtual ~CameraController() = default;

virtual void updateCameraPosition(const Qt3DExtras::QAbstractCameraController::InputState & state, float dt) = 0;
virtual void updateCameraPosition(const CameraInputState & state, float dt) = 0;

virtual Qt3DRender::QCamera * getCamera() = 0;

virtual float getLinearSpeed() = 0;

virtual float getLookSpeed() = 0;

virtual CameraControllerType getType() = 0;

QVector3D position() { return getCamera()->position(); }

QVector3D viewVector() { return getCamera()->viewVector(); }
Expand All @@ -133,7 +159,7 @@ class CameraController
{
return dynamic_cast<Qt3DExtras::QAbstractCameraController *>(this);
}
};
}; /* end class CameraController */

class RFirstPersonCameraController : public Qt3DExtras::QFirstPersonCameraController
, public CameraController
Expand All @@ -152,8 +178,10 @@ class RFirstPersonCameraController : public Qt3DExtras::QFirstPersonCameraContro

void moveCamera(const InputState & state, float dt) override {}

void updateCameraPosition(const InputState & input, float dt) override;
};
void updateCameraPosition(const CameraInputState & input, float dt) override;

CameraControllerType getType() override { return CameraControllerType::FirstPerson; }
}; /* end class RFirstPersonCameraController */

class ROrbitCameraController : public Qt3DExtras::QOrbitCameraController
, public CameraController
Expand All @@ -170,7 +198,7 @@ class ROrbitCameraController : public Qt3DExtras::QOrbitCameraController
private:
void moveCamera(const InputState & state, float dt) override {}

void updateCameraPosition(const InputState & input, float dt) override;
void updateCameraPosition(const CameraInputState & input, float dt) override;

void zoom(float zoomValue) const;

Expand All @@ -179,7 +207,9 @@ class ROrbitCameraController : public Qt3DExtras::QOrbitCameraController
static float clamp(float value);

static float zoomDistanceSquared(QVector3D firstPoint, QVector3D secondPoint);
};

CameraControllerType getType() override { return CameraControllerType::Orbit; }
}; /* end class ROrbitCameraController */

} /* end namespace modmesh */

Expand Down
Loading

0 comments on commit 194a313

Please sign in to comment.