Can now edit planets
This commit is contained in:
parent
2a4df119ac
commit
6248f70bb7
11 changed files with 395 additions and 20 deletions
|
@ -7,7 +7,7 @@ cmake_minimum_required (VERSION 3.8)
|
|||
add_executable (GraviSim
|
||||
"main.cpp"
|
||||
"MainWindow.cpp"
|
||||
"Screen.hpp" "Screen.cpp" "Planet.hpp" "Planet.cpp")
|
||||
"Screen.hpp" "Screen.cpp" "Planet.hpp" "Planet.cpp" "PlanetConfig.cpp")
|
||||
|
||||
target_link_libraries(GraviSim
|
||||
Qt5::Widgets
|
||||
|
|
|
@ -2,11 +2,68 @@
|
|||
|
||||
#include "Screen.hpp"
|
||||
|
||||
MainWindow::MainWindow(QWidget* parent) :
|
||||
QMainWindow(parent)
|
||||
MainWindow* MainWindow::instance = nullptr;
|
||||
|
||||
MainWindow* MainWindow::Instance()
|
||||
{
|
||||
if (instance == nullptr)
|
||||
{
|
||||
instance = new MainWindow();
|
||||
instance->Setup();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
MainWindow::MainWindow() :
|
||||
QMainWindow(Q_NULLPTR), activePlanet(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
void MainWindow::Setup()
|
||||
{
|
||||
ui.setupUi(this);
|
||||
ClosePlanetDialog();
|
||||
}
|
||||
|
||||
Screen* screen = new Screen(this);
|
||||
ui.layout->addWidget(screen, 0, 0);
|
||||
}
|
||||
void MainWindow::OpenPlanetDialog(Planet* planet)
|
||||
{
|
||||
activePlanet = nullptr;
|
||||
|
||||
ui.config->Enable();
|
||||
ui.config->SetTitle("Planets don't have names yet");
|
||||
ui.config->SetRadius(planet->radius);
|
||||
ui.config->SetX(planet->position.rx());
|
||||
ui.config->SetY(planet->position.ry());
|
||||
|
||||
activePlanet = planet;
|
||||
}
|
||||
|
||||
void MainWindow::ClosePlanetDialog()
|
||||
{
|
||||
activePlanet = nullptr;
|
||||
|
||||
ui.config->SetTitle("No planet selected");
|
||||
ui.config->Disable();
|
||||
ui.config->SetRadius(0.f);
|
||||
ui.config->SetX(0.f);
|
||||
ui.config->SetY(0.f);
|
||||
}
|
||||
|
||||
void MainWindow::OnRadiusChanged(double radius)
|
||||
{
|
||||
if(activePlanet != nullptr)
|
||||
activePlanet->radius = radius;
|
||||
}
|
||||
|
||||
void MainWindow::OnXChanged(double x)
|
||||
{
|
||||
if (activePlanet != nullptr)
|
||||
activePlanet->position.rx() = x;
|
||||
}
|
||||
|
||||
void MainWindow::OnYChanged(double y)
|
||||
{
|
||||
if (activePlanet != nullptr)
|
||||
activePlanet->position.ry() = y;
|
||||
}
|
||||
|
|
|
@ -3,13 +3,32 @@
|
|||
#include <QMainWindow>
|
||||
#include "ui_MainWindow.h"
|
||||
|
||||
#include "Planet.hpp"
|
||||
#include "PlanetConfig.hpp"
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow(QWidget* parent = Q_NULLPTR);
|
||||
static MainWindow* Instance();
|
||||
|
||||
public:
|
||||
void OpenPlanetDialog(Planet* planet);
|
||||
void ClosePlanetDialog();
|
||||
|
||||
public slots:
|
||||
void OnRadiusChanged(double radius);
|
||||
void OnXChanged(double x);
|
||||
void OnYChanged(double y);
|
||||
|
||||
private:
|
||||
MainWindow();
|
||||
void Setup();
|
||||
|
||||
private:
|
||||
Ui::MainWindow ui;
|
||||
|
||||
Planet* activePlanet;
|
||||
static MainWindow* instance;
|
||||
};
|
||||
|
|
|
@ -20,10 +20,12 @@ public:
|
|||
private:
|
||||
void Initialize(QColor color);
|
||||
|
||||
private:
|
||||
public:
|
||||
QPointF position;
|
||||
float radius;
|
||||
|
||||
private:
|
||||
|
||||
QColor selectionColor;
|
||||
|
||||
QBrush circle;
|
||||
|
|
58
src/PlanetConfig.cpp
Normal file
58
src/PlanetConfig.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
#include "PlanetConfig.hpp"
|
||||
|
||||
#include <qcolordialog.h>
|
||||
#include "MainWindow.hpp"
|
||||
|
||||
PlanetConfig::PlanetConfig(QWidget* parent) :
|
||||
QWidget(parent)
|
||||
{
|
||||
ui.setupUi(this);
|
||||
|
||||
ui.color = new QColorDialog(this);
|
||||
|
||||
MainWindow* instance = MainWindow::Instance();
|
||||
|
||||
connect(ui.radius, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
|
||||
instance, [instance](double d) { instance->OnRadiusChanged(d); });
|
||||
|
||||
connect(ui.xPos, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
|
||||
instance, [instance](double d) { instance->OnXChanged(d); });
|
||||
|
||||
connect(ui.yPos, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
|
||||
instance, [instance](double d) { instance->OnYChanged(d); });
|
||||
|
||||
}
|
||||
|
||||
void PlanetConfig::Disable()
|
||||
{
|
||||
ui.radius->setDisabled(true);
|
||||
ui.xPos->setDisabled(true);
|
||||
ui.yPos->setDisabled(true);
|
||||
}
|
||||
|
||||
void PlanetConfig::Enable()
|
||||
{
|
||||
ui.radius->setDisabled(false);
|
||||
ui.xPos->setDisabled(false);
|
||||
ui.yPos->setDisabled(false);
|
||||
}
|
||||
|
||||
void PlanetConfig::SetTitle(const QString& title)
|
||||
{
|
||||
ui.name->setText(title);
|
||||
}
|
||||
|
||||
void PlanetConfig::SetRadius(double radius)
|
||||
{
|
||||
ui.radius->setValue(radius);
|
||||
}
|
||||
|
||||
void PlanetConfig::SetX(double x)
|
||||
{
|
||||
ui.xPos->setValue(x);
|
||||
}
|
||||
|
||||
void PlanetConfig::SetY(double y)
|
||||
{
|
||||
ui.yPos->setValue(y);
|
||||
}
|
22
src/PlanetConfig.hpp
Normal file
22
src/PlanetConfig.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#include "ui_PlanetConfig.h"
|
||||
|
||||
class PlanetConfig : public QWidget
|
||||
{
|
||||
public:
|
||||
PlanetConfig(QWidget* parent);
|
||||
|
||||
void Disable();
|
||||
void Enable();
|
||||
|
||||
void SetTitle(const QString& title);
|
||||
void SetRadius(double radius);
|
||||
void SetX(double x);
|
||||
void SetY(double y);
|
||||
|
||||
private:
|
||||
Ui::Form ui;
|
||||
};
|
|
@ -8,6 +8,8 @@
|
|||
#include <QTimer>
|
||||
#include <QtMath>
|
||||
|
||||
#include "MainWindow.hpp"
|
||||
|
||||
#define BIND_LMB(f) (lmbAction = std::bind(&Screen::f, this, std::placeholders::_1))
|
||||
|
||||
Screen::Screen(QWidget* parent) :
|
||||
|
@ -53,7 +55,8 @@ void Screen::mouseMoveEvent(QMouseEvent* event)
|
|||
if (hovered == nullptr)
|
||||
{
|
||||
setCursor(Qt::ArrowCursor);
|
||||
BIND_LMB(lmb_MakePlanet);
|
||||
if(selected == nullptr)
|
||||
BIND_LMB(lmb_MakePlanet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -103,9 +106,17 @@ void Screen::lmb_SelectPlanet(QMouseEvent* event)
|
|||
selected->Select(false);
|
||||
|
||||
hovered->Select(true);
|
||||
selected = hovered;
|
||||
MainWindow::Instance()->OpenPlanetDialog(selected);
|
||||
}
|
||||
else
|
||||
{
|
||||
BIND_LMB(lmb_MakePlanet);
|
||||
selected->Select(false);
|
||||
selected = nullptr;
|
||||
MainWindow::Instance()->ClosePlanetDialog();
|
||||
return;
|
||||
}
|
||||
|
||||
selected = hovered;
|
||||
}
|
||||
|
||||
void Screen::paintEvent(QPaintEvent* event)
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
#include "Planet.hpp"
|
||||
|
||||
class MainWindow;
|
||||
|
||||
class Screen : public QOpenGLWidget
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -5,8 +5,8 @@ int main(int argc, char** argv)
|
|||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
MainWindow window;
|
||||
window.show();
|
||||
MainWindow* window = MainWindow::Instance();
|
||||
window->show();
|
||||
|
||||
return app.exec();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue