Skip to content

Commit

Permalink
small code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 13, 2023
1 parent 2ee3832 commit 1c98dba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
32 changes: 10 additions & 22 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "mainwidget.h"

Expand All @@ -17,18 +15,23 @@ MainWidget::~MainWidget()
doneCurrent();
}

void MainWidget::mousePressEvent(QMouseEvent *e)

void MainWidget::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
{
switch (e->button())
{
case Qt::LeftButton:
m_left_mouse_button_down = true;
m_left_mouse_button_down = flag;
break;

case Qt::MiddleButton:
m_middle_mouse_button_down = true;
m_middle_mouse_button_down = flag;
break;
}
}
void MainWidget::mousePressEvent(QMouseEvent *e)
{
set_mouse_button_pressed_flag(e, true);
m_last_mouse_pos = QVector2D(e->position());
}
void MainWidget::mouseMoveEvent(QMouseEvent* e)
Expand All @@ -41,10 +44,9 @@ void MainWidget::mouseMoveEvent(QMouseEvent* e)
const float rotation_scale_factor = 0.1f;
const float theta_around_x = rotation_scale_factor * diff.y();
const float theta_around_y = rotation_scale_factor * diff.x();

m_camera.rotate(theta_around_x, theta_around_y);
}
else
else if(m_middle_mouse_button_down)
{
const float zoom_scale_factor = 0.01f;
const auto distance = zoom_scale_factor * diff.y();
Expand All @@ -53,23 +55,9 @@ void MainWidget::mouseMoveEvent(QMouseEvent* e)

m_last_mouse_pos = current_mouse_pos;
}
void MainWidget::wheelEvent(QWheelEvent* e)
{
auto distance = e->angleDelta();
m_camera.move_forward(distance.x());
}
void MainWidget::mouseReleaseEvent(QMouseEvent *e)
{
switch (e->button())
{
case Qt::LeftButton:
m_left_mouse_button_down = false;
break;

case Qt::MiddleButton:
m_middle_mouse_button_down = false;
break;
}
set_mouse_button_pressed_flag(e, false);
}
void MainWidget::timerEvent(QTimerEvent *)
{
Expand Down
35 changes: 18 additions & 17 deletions Arrangement_on_surface_2/demo/earth/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,32 @@

class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
{
Q_OBJECT
Q_OBJECT

public:
using QOpenGLWidget::QOpenGLWidget;
~MainWidget();
using QOpenGLWidget::QOpenGLWidget;
~MainWidget();

protected:
void mousePressEvent(QMouseEvent *e) override;
void mouseMoveEvent(QMouseEvent* e) override;
void wheelEvent(QWheelEvent* event) override;
void mouseReleaseEvent(QMouseEvent *e) override;
void timerEvent(QTimerEvent *e) override;
void set_mouse_button_pressed_flag(QMouseEvent* e, bool flag);
void mousePressEvent(QMouseEvent *e) override;
void mouseMoveEvent(QMouseEvent* e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
void timerEvent(QTimerEvent *e) override;

void initializeGL() override;
void resizeGL(int w, int h) override;
void paintGL() override;

void initializeGL() override;
void resizeGL(int w, int h) override;
void paintGL() override;

void add_shader(GLuint the_program,
const char* shader_code,
GLenum shader_type);

void add_shader(GLuint the_program,
const char* shader_code,
GLenum shader_type);

void init_camera();
void init_geometry();
void init_shader_program();
void init_camera();
void init_geometry();
void init_shader_program();

private:
std::unique_ptr<Sphere> m_sphere;
Expand Down

0 comments on commit 1c98dba

Please sign in to comment.