Skip to content

Commit

Permalink
Merge pull request #123 from kkimurak/enumerize
Browse files Browse the repository at this point in the history
Add enums for statements (camera mode, camera motion mode, 3D cursor mode)
  • Loading branch information
mahi97 authored Sep 2, 2020
2 parents 0568391 + 13971db commit 59058ec
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
22 changes: 20 additions & 2 deletions include/glwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ class GLWidget : public QGLWidget {

Q_OBJECT
public:
enum class CursorMode {
STEADY = 0,
PLACE_ROBOT = 1,
PLACE_BALL = 2,
};
enum class CameraMode {
BIRDS_EYE_FROM_TOUCH_LINE = 0,
CURRENT_ROBOT_VIEW = 1,
TOP_VIEW = 2,
BIRDS_EYE_FROM_OPPOSITE_TOUCH_LINE = 3,
BIRDS_EYE_FROM_BLUE = 4,
BIRDS_EYE_FROM_YELLOW = 5,
MAX_ACTIVE_MODE_FOR_CHANGEMODE=BIRDS_EYE_FROM_YELLOW,
// non-avaliable modes when "toggle camera mode" called
LOCK_TO_ROBOT = -1,
LOCK_TO_BALL = -2,
};
GLWidget(QWidget *parent,ConfigWidget* _cfg);
~GLWidget();
dReal getFPS();
Expand All @@ -51,7 +68,7 @@ class GLWidget : public QGLWidget {
QAction* moveRobotHereAct;
QAction* changeCamModeAct;
QMenu *cameraMenu;
int Current_robot,Current_team,cammode;
int Current_robot,Current_team;
int lockedIndex;
bool ctrl,alt,kickingball,altTrigger;
bool chiping;
Expand Down Expand Up @@ -92,7 +109,8 @@ public slots:
void keyReleaseEvent(QKeyEvent* event);
void closeEvent(QCloseEvent *event);
private:
int state;
CursorMode state;
CameraMode cammode;
int moving_robot_id,clicked_robot;
int frames;
bool first_time;
Expand Down
7 changes: 6 additions & 1 deletion include/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ Copyright (C) 2011, Parsian Robotic Center (eew.aut.ac.ir/~parsian/grsim)
#include <QGLWidget>
#include <QString>

enum CameraMotionMode {
ROTATE_VIEW_POINT = 1,
MOVE_POSITION_FREELY = 2,
MOVE_POSITION_LR = 4,
};

class CGraphics
{
Expand Down Expand Up @@ -61,7 +66,7 @@ class CGraphics
dReal renderDepth();
void setRenderDepth(dReal depth);
void setViewpoint (dReal x,dReal y,dReal z,dReal h,dReal p,dReal r);
void cameraMotion (int mode, int deltax, int deltay);
void cameraMotion (CameraMotionMode mode, int deltax, int deltay);
void lookAt(dReal x,dReal y,dReal z);
void getCameraForward(dReal& x,dReal& y,dReal& z);
void getCameraRight(dReal& x,dReal& y,dReal& z);
Expand Down
57 changes: 28 additions & 29 deletions src/glwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ GLWidget::GLWidget(QWidget *parent, ConfigWidget* _cfg)
: QGLWidget(parent)
{
frames = 0;
state = 0;
state = CursorMode::STEADY;
first_time = true;
cfg = _cfg;

Expand All @@ -44,7 +44,7 @@ GLWidget::GLWidget(QWidget *parent, ConfigWidget* _cfg)
ssl = new SSLWorld(this,cfg,forms[2],forms[2]);
Current_robot = 0;
Current_team = 0;
cammode = 0;
cammode = CameraMode::BIRDS_EYE_FROM_TOUCH_LINE;
setMouseTracking(true);

blueRobotsMenu = new QMenu("&Blue Robots");
Expand Down Expand Up @@ -127,15 +127,15 @@ void GLWidget::moveRobot()
{
ssl->show3DCursor = true;
ssl->cursor_radius = cfg->robotSettings.RobotRadius;
state = 1;
state = CursorMode::PLACE_ROBOT;
moving_robot_id = clicked_robot;
}

void GLWidget::unselectRobot()
{
ssl->show3DCursor = false;
ssl->cursor_radius = cfg->robotSettings.RobotRadius;
state = 0;
state = CursorMode::STEADY;
moving_robot_id= ssl->robotIndex(Current_robot,Current_team);
}

Expand Down Expand Up @@ -173,15 +173,15 @@ void GLWidget::moveCurrentRobot()
{
ssl->show3DCursor = true;
ssl->cursor_radius = cfg->robotSettings.RobotRadius;
state = 1;
state = CursorMode::PLACE_ROBOT;
moving_robot_id = ssl->robotIndex(Current_robot,Current_team);
}

void GLWidget::moveBall()
{
ssl->show3DCursor = true;
ssl->cursor_radius = cfg->BallRadius();
state = 2;
state = CursorMode::PLACE_BALL;
}

void GLWidget::mousePressEvent(QMouseEvent *event)
Expand All @@ -190,22 +190,22 @@ void GLWidget::mousePressEvent(QMouseEvent *event)
lastPos = event->pos();
if (event->buttons() & Qt::LeftButton)
{
if (state==1)
if (state==CursorMode::PLACE_ROBOT)
{
if (moving_robot_id!=-1)
{
ssl->robots[moving_robot_id]->setXY(ssl->cursor_x,ssl->cursor_y);
state = 0;
state = CursorMode::STEADY;
ssl->show3DCursor = false;
}
}
else if (state==2)
else if (state==CursorMode::PLACE_BALL)
{
ssl->ball->setBodyPosition(ssl->cursor_x,ssl->cursor_y,cfg->BallRadius()*1.1*20.0);
dBodySetAngularVel(ssl->ball->body,0,0,0);
dBodySetLinearVel(ssl->ball->body,0,0,0);
ssl->show3DCursor = false;
state = 0;
state = CursorMode::STEADY;
}
else {
if (ssl->selected>=0){
Expand Down Expand Up @@ -303,13 +303,13 @@ void GLWidget::mouseMoveEvent(QMouseEvent *event)
int dy = -(event->y() - lastPos.y());
if (event->buttons() & Qt::LeftButton) {
if (ctrl)
ssl->g->cameraMotion(4,dx,dy);
ssl->g->cameraMotion(CameraMotionMode::MOVE_POSITION_FREELY,dx,dy);
else
ssl->g->cameraMotion(1,dx,dy);
ssl->g->cameraMotion(CameraMotionMode::ROTATE_VIEW_POINT,dx,dy);
}
else if (event->buttons() & Qt::MidButton)
{
ssl->g->cameraMotion(2,dx,dy);
ssl->g->cameraMotion(CameraMotionMode::MOVE_POSITION_LR,dx,dy);
}
lastPos = event->pos();
update3DCursor(event->x(),event->y());
Expand Down Expand Up @@ -360,20 +360,20 @@ void GLWidget::step()
void GLWidget::paintGL()
{
if (!ssl->g->isGraphicsEnabled()) return;
if (cammode==1)
if (cammode==CameraMode::CURRENT_ROBOT_VIEW)
{
dReal x,y,z;
int R = ssl->robotIndex(Current_robot,Current_team);
ssl->robots[R]->getXY(x,y);z = 0.3;
ssl->g->setViewpoint(x,y,z,ssl->robots[R]->getDir(),-25,0);
}
if (cammode==-1)
if (cammode==CameraMode::LOCK_TO_ROBOT)
{
dReal x,y,z;
ssl->robots[lockedIndex]->getXY(x,y);z = 0.1;
ssl->g->lookAt(x,y,z);
}
if (cammode==-2)
else if(cammode==CameraMode::LOCK_TO_BALL)
{
dReal x,y,z;
ssl->ball->getBodyPosition(x,y,z);
Expand All @@ -400,22 +400,21 @@ void GLWidget::paintGL()
void GLWidget::changeCameraMode()
{
static dReal xyz[3],hpr[3];
if (cammode<0) cammode=0;
else cammode ++;
cammode %= 6;
if (cammode==0)
if(static_cast<int>(cammode)<0) cammode=CameraMode::BIRDS_EYE_FROM_TOUCH_LINE;
cammode = static_cast<CameraMode>(static_cast<int>(cammode) + 1);
cammode = static_cast<CameraMode>(static_cast<int>(cammode)%(static_cast<int>(CameraMode::MAX_ACTIVE_MODE_FOR_CHANGEMODE)+1));

if (cammode==CameraMode::BIRDS_EYE_FROM_TOUCH_LINE)
ssl->g->setViewpoint(0,-(cfg->Field_Width()+cfg->Field_Margin()*2.0f)/2.0f,3,90,-45,0);
else if (cammode==1)
{
else if (cammode==CameraMode::CURRENT_ROBOT_VIEW)
ssl->g->getViewpoint(xyz,hpr);
}
else if (cammode==2)
else if (cammode==CameraMode::TOP_VIEW)
ssl->g->setViewpoint(0,0,5,0,-90,0);
else if (cammode==3)
else if (cammode==CameraMode::BIRDS_EYE_FROM_OPPOSITE_TOUCH_LINE)
ssl->g->setViewpoint(0, (cfg->Field_Width()+cfg->Field_Margin()*2.0f)/2.0f,3,270,-45,0);
else if (cammode==4)
else if (cammode==CameraMode::BIRDS_EYE_FROM_BLUE)
ssl->g->setViewpoint(-(cfg->Field_Length()+cfg->Field_Margin()*2.0f)/2.0f,0,3,0,-45,0);
else if (cammode==5)
else if (cammode==CameraMode::BIRDS_EYE_FROM_YELLOW)
ssl->g->setViewpoint((cfg->Field_Length()+cfg->Field_Margin()*2.0f)/2.0f,0,3,180,-45,0);
}

Expand Down Expand Up @@ -547,13 +546,13 @@ void GLWidget::moveBallHere()

void GLWidget::lockCameraToRobot()
{
cammode = -1;
cammode = CameraMode::LOCK_TO_ROBOT;
lockedIndex = ssl->robotIndex(Current_robot,Current_team);//clicked_robot;
}

void GLWidget::lockCameraToBall()
{
cammode = -2;
cammode = CameraMode::LOCK_TO_BALL;
}

void GLWidget::moveRobotHere()
Expand Down
8 changes: 4 additions & 4 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,22 @@ void CGraphics::wrapCameraAngles()
}
}

void CGraphics::cameraMotion (int mode, int deltax, int deltay)
void CGraphics::cameraMotion (CameraMotionMode mode, int deltax, int deltay)
{
if (graphicDisabled) return;
dReal side = 0.01f * dReal(deltax);
dReal fwd = (mode==4) ? (0.01f * dReal(deltay)) : 0.0f;
dReal fwd = (mode==CameraMotionMode::ROTATE_VIEW_POINT) ? (0.01f * dReal(deltay)) : 0.0f;
dReal s = (dReal) sin (view_hpr[0]*M_PI/180.0f);
dReal c = (dReal) cos (view_hpr[0]*M_PI/180.0f);

if (mode==1) {
if (mode==CameraMotionMode::ROTATE_VIEW_POINT) {
view_hpr[0] += dReal (deltax) * 0.5f;
view_hpr[1] += dReal (deltay) * 0.5f;
}
else {
view_xyz[0] += -s*side + c*fwd;
view_xyz[1] += c*side + s*fwd;
if (mode==2 || mode==5) view_xyz[2] += 0.01f * dReal(deltay);
if (mode==CameraMotionMode::MOVE_POSITION_FREELY) view_xyz[2] += 0.01f * dReal(deltay);
}
wrapCameraAngles();
}
Expand Down

0 comments on commit 59058ec

Please sign in to comment.