Made planets selectable
This commit is contained in:
parent
5c2d5f5454
commit
2a4df119ac
5 changed files with 482 additions and 8 deletions
|
@ -8,9 +8,14 @@
|
|||
#include <QTimer>
|
||||
#include <QtMath>
|
||||
|
||||
#define BIND_LMB(f) (lmbAction = std::bind(&Screen::f, this, std::placeholders::_1))
|
||||
|
||||
Screen::Screen(QWidget* parent) :
|
||||
QOpenGLWidget(parent)
|
||||
{
|
||||
setMouseTracking(true);
|
||||
BIND_LMB(lmb_MakePlanet);
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
background = QBrush(Qt::black);
|
||||
|
@ -25,23 +30,39 @@ Screen::Screen(QWidget* parent) :
|
|||
|
||||
void Screen::mouseMoveEvent(QMouseEvent* event)
|
||||
{
|
||||
if (mouseDown)
|
||||
if (mouseDown && hovered == nullptr)
|
||||
{
|
||||
Planet* planet = planets.back();
|
||||
QPointF distance = event->localPos() - mouseClickPos;
|
||||
planet->Resize(qSqrt(distance.x()*distance.x() + distance.y() *distance.y()));
|
||||
}
|
||||
else
|
||||
{
|
||||
hovered = nullptr;
|
||||
for (unsigned i = planets.size(); i-- > 0; )
|
||||
{
|
||||
if (planets[i]->IsInside(event->localPos()))
|
||||
{
|
||||
setCursor(Qt::OpenHandCursor);
|
||||
BIND_LMB(lmb_SelectPlanet);
|
||||
hovered = planets[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (hovered == nullptr)
|
||||
{
|
||||
setCursor(Qt::ArrowCursor);
|
||||
BIND_LMB(lmb_MakePlanet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Screen::mousePressEvent(QMouseEvent* event)
|
||||
{
|
||||
if (event->button() == Qt::MouseButton::LeftButton)
|
||||
{
|
||||
planets.push_back(new Planet(event->localPos(), 0,
|
||||
QColor(rand() % 256, rand() % 256, rand() % 256)
|
||||
));
|
||||
mouseClickPos = event->localPos();
|
||||
mouseDown = true;
|
||||
lmbAction(event);
|
||||
}
|
||||
|
||||
if (event->button() == Qt::MouseButton::RightButton)
|
||||
|
@ -65,6 +86,28 @@ void Screen::Render()
|
|||
update();
|
||||
}
|
||||
|
||||
void Screen::lmb_MakePlanet(QMouseEvent* event)
|
||||
{
|
||||
planets.push_back(new Planet(event->localPos(), 0,
|
||||
QColor(rand() % 256, rand() % 256, rand() % 256)
|
||||
));
|
||||
mouseClickPos = event->localPos();
|
||||
mouseDown = true;
|
||||
}
|
||||
|
||||
void Screen::lmb_SelectPlanet(QMouseEvent* event)
|
||||
{
|
||||
if (hovered != nullptr)
|
||||
{
|
||||
if (selected != nullptr)
|
||||
selected->Select(false);
|
||||
|
||||
hovered->Select(true);
|
||||
}
|
||||
|
||||
selected = hovered;
|
||||
}
|
||||
|
||||
void Screen::paintEvent(QPaintEvent* event)
|
||||
{
|
||||
QPainter painter;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue