Made planets selectable
This commit is contained in:
parent
5c2d5f5454
commit
2a4df119ac
5 changed files with 482 additions and 8 deletions
|
@ -3,17 +3,43 @@
|
|||
Planet::Planet(float xPos, float yPos, float radius, QColor color) :
|
||||
position{ xPos, yPos }, radius(radius)
|
||||
{
|
||||
circle = QBrush(color);
|
||||
Initialize(color);
|
||||
}
|
||||
|
||||
Planet::Planet(QPointF pos, float radius, QColor color) :
|
||||
position(pos), radius(radius)
|
||||
{
|
||||
circle = QBrush(color);
|
||||
Initialize(color);
|
||||
}
|
||||
|
||||
void Planet::Select(bool select)
|
||||
{
|
||||
if (select)
|
||||
outline.setColor(selectionColor);
|
||||
else
|
||||
outline.setColor(Qt::transparent);
|
||||
}
|
||||
|
||||
void Planet::Draw(QPainter* painter)
|
||||
{
|
||||
painter->setBrush(circle);
|
||||
painter->setPen(outline);
|
||||
painter->drawEllipse(position, radius, radius);
|
||||
}
|
||||
|
||||
bool Planet::IsInside(QPointF point) const
|
||||
{
|
||||
QPointF distance = point - position;
|
||||
return (distance.x() * distance.x() + distance.y() * distance.y() < radius * radius);
|
||||
}
|
||||
|
||||
void Planet::Initialize(QColor color)
|
||||
{
|
||||
circle = QBrush(color);
|
||||
outline = QPen(Qt::transparent);
|
||||
outline.setWidthF(4.f);
|
||||
|
||||
int h, s, v, a;
|
||||
color.getHsv(&h, &s, &v, &a);
|
||||
selectionColor = QColor::fromHsv((h + 180) % 360, s, v, a);
|
||||
}
|
||||
|
|
|
@ -11,12 +11,21 @@ public:
|
|||
Planet(QPointF pos, float radius, QColor color);
|
||||
|
||||
void Resize(float newRadius) { radius = newRadius; }
|
||||
void Select(bool select);
|
||||
|
||||
void Draw(QPainter* painter);
|
||||
|
||||
bool IsInside(QPointF point) const;
|
||||
|
||||
private:
|
||||
void Initialize(QColor color);
|
||||
|
||||
private:
|
||||
QPointF position;
|
||||
float radius;
|
||||
|
||||
QColor selectionColor;
|
||||
|
||||
QBrush circle;
|
||||
QPen outline;
|
||||
};
|
|
@ -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;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include <QOpenGLWidget>
|
||||
|
||||
|
@ -18,15 +19,24 @@ public:
|
|||
private slots:
|
||||
void Render();
|
||||
|
||||
private:
|
||||
void lmb_MakePlanet(QMouseEvent* event);
|
||||
void lmb_SelectPlanet(QMouseEvent* event);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent* event) override;
|
||||
|
||||
private:
|
||||
bool mouseDown = false;
|
||||
|
||||
std::function<void(QMouseEvent*)> lmbAction;
|
||||
|
||||
QPointF mouseClickPos;
|
||||
|
||||
QBrush background;
|
||||
QTimer* renderTimer;
|
||||
|
||||
std::vector<Planet*> planets;
|
||||
Planet* selected = nullptr;
|
||||
Planet* hovered = nullptr;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue