From 6248f70bb73b5b3943b298614c7d324f1876d5a9 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 25 Sep 2020 13:55:35 +0200 Subject: [PATCH] Can now edit planets --- src/CMakeLists.txt | 2 +- src/MainWindow.cpp | 67 +++++++++++++++-- src/MainWindow.hpp | 21 +++++- src/Planet.hpp | 4 +- src/PlanetConfig.cpp | 58 +++++++++++++++ src/PlanetConfig.hpp | 22 ++++++ src/Screen.cpp | 17 ++++- src/Screen.hpp | 2 + src/main.cpp | 4 +- ui/MainWindow.ui | 48 ++++++++++-- ui/PlanetConfig.ui | 170 +++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 395 insertions(+), 20 deletions(-) create mode 100644 src/PlanetConfig.cpp create mode 100644 src/PlanetConfig.hpp create mode 100644 ui/PlanetConfig.ui diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b4abb30..6e323c7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index e3327d9..d752ec2 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -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); -} \ No newline at end of file +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; +} diff --git a/src/MainWindow.hpp b/src/MainWindow.hpp index c7e476f..84fd373 100644 --- a/src/MainWindow.hpp +++ b/src/MainWindow.hpp @@ -3,13 +3,32 @@ #include #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; }; diff --git a/src/Planet.hpp b/src/Planet.hpp index 4dbfa98..b8a5047 100644 --- a/src/Planet.hpp +++ b/src/Planet.hpp @@ -20,10 +20,12 @@ public: private: void Initialize(QColor color); -private: +public: QPointF position; float radius; +private: + QColor selectionColor; QBrush circle; diff --git a/src/PlanetConfig.cpp b/src/PlanetConfig.cpp new file mode 100644 index 0000000..af8afef --- /dev/null +++ b/src/PlanetConfig.cpp @@ -0,0 +1,58 @@ +#include "PlanetConfig.hpp" + +#include +#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::of(&QDoubleSpinBox::valueChanged), + instance, [instance](double d) { instance->OnRadiusChanged(d); }); + + connect(ui.xPos, QOverload::of(&QDoubleSpinBox::valueChanged), + instance, [instance](double d) { instance->OnXChanged(d); }); + + connect(ui.yPos, QOverload::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); +} diff --git a/src/PlanetConfig.hpp b/src/PlanetConfig.hpp new file mode 100644 index 0000000..021f8eb --- /dev/null +++ b/src/PlanetConfig.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include + +#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; +}; \ No newline at end of file diff --git a/src/Screen.cpp b/src/Screen.cpp index 3720e99..829b381 100644 --- a/src/Screen.cpp +++ b/src/Screen.cpp @@ -8,6 +8,8 @@ #include #include +#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) diff --git a/src/Screen.hpp b/src/Screen.hpp index 65890b5..2729bea 100644 --- a/src/Screen.hpp +++ b/src/Screen.hpp @@ -7,6 +7,8 @@ #include "Planet.hpp" +class MainWindow; + class Screen : public QOpenGLWidget { public: diff --git a/src/main.cpp b/src/main.cpp index 2492cc0..a83c5b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); } \ No newline at end of file diff --git a/ui/MainWindow.ui b/ui/MainWindow.ui index 0a4df98..a7a4a78 100644 --- a/ui/MainWindow.ui +++ b/ui/MainWindow.ui @@ -6,10 +6,16 @@ 0 0 - 726 - 643 + 1000 + 800 + + + 1000 + 800 + + MainWindow @@ -27,16 +33,44 @@ 0 - - - - QLayout::SetDefaultConstraint + + 0 + + + + + + + + + 0 + 0 + - + + + 200 + 16777215 + + + + + + Screen + QOpenGLWidget +
Screen.hpp
+
+ + PlanetConfig + QWidget +
PlanetConfig.hpp
+ 1 +
+
diff --git a/ui/PlanetConfig.ui b/ui/PlanetConfig.ui new file mode 100644 index 0000000..3e613eb --- /dev/null +++ b/ui/PlanetConfig.ui @@ -0,0 +1,170 @@ + + + Form + + + + 0 + 0 + 436 + 567 + + + + Form + + + false + + + background-color: rgb(222, 222, 222); + + + + + + + + + 0 + 0 + + + + + Arial + 12 + 75 + true + + + + TextLabel + + + Qt::AutoText + + + Qt::AlignHCenter|Qt::AlignTop + + + true + + + + + + + + + + Courier New + 12 + + + + Radius + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 1000.000000000000000 + + + 0.100000000000000 + + + + + + + + + + + + Courier New + 12 + + + + Colour + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + + + + + Courier New + 12 + + + + X + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + Courier New + 12 + + + + Y + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 1000.000000000000000 + + + 0.100000000000000 + + + + + + + 10000.000000000000000 + + + 0.100000000000000 + + + + + + + + + + + +