Can now edit planets
This commit is contained in:
parent
2a4df119ac
commit
6248f70bb7
|
@ -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();
|
||||
}
|
|
@ -6,10 +6,16 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>726</width>
|
||||
<height>643</height>
|
||||
<width>1000</width>
|
||||
<height>800</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>1000</width>
|
||||
<height>800</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MainWindow</string>
|
||||
</property>
|
||||
|
@ -27,16 +33,44 @@
|
|||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<layout class="QGridLayout" name="layout">
|
||||
<property name="sizeConstraint">
|
||||
<enum>QLayout::SetDefaultConstraint</enum>
|
||||
<property name="spacing">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item row="1" column="0">
|
||||
<widget class="Screen" name="screen"/>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="PlanetConfig" name="config" native="true">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
</layout>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>200</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>Screen</class>
|
||||
<extends>QOpenGLWidget</extends>
|
||||
<header>Screen.hpp</header>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>PlanetConfig</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>PlanetConfig.hpp</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
|
170
ui/PlanetConfig.ui
Normal file
170
ui/PlanetConfig.ui
Normal file
|
@ -0,0 +1,170 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>Form</class>
|
||||
<widget class="QWidget" name="Form">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>436</width>
|
||||
<height>567</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<property name="autoFillBackground">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
<string notr="true">background-color: rgb(222, 222, 222);</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<item row="0" column="0">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="name">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Arial</family>
|
||||
<pointsize>12</pointsize>
|
||||
<weight>75</weight>
|
||||
<bold>true</bold>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>TextLabel</string>
|
||||
</property>
|
||||
<property name="textFormat">
|
||||
<enum>Qt::AutoText</enum>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignHCenter|Qt::AlignTop</set>
|
||||
</property>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Courier New</family>
|
||||
<pointsize>12</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Radius</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDoubleSpinBox" name="radius">
|
||||
<property name="maximum">
|
||||
<double>1000.000000000000000</double>
|
||||
</property>
|
||||
<property name="singleStep">
|
||||
<double>0.100000000000000</double>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Courier New</family>
|
||||
<pointsize>12</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Colour</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QWidget" name="color" native="true"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label_3">
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Courier New</family>
|
||||
<pointsize>12</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>X</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="label_4">
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Courier New</family>
|
||||
<pointsize>12</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Y</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QDoubleSpinBox" name="xPos">
|
||||
<property name="maximum">
|
||||
<double>1000.000000000000000</double>
|
||||
</property>
|
||||
<property name="singleStep">
|
||||
<double>0.100000000000000</double>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QDoubleSpinBox" name="yPos">
|
||||
<property name="maximum">
|
||||
<double>10000.000000000000000</double>
|
||||
</property>
|
||||
<property name="singleStep">
|
||||
<double>0.100000000000000</double>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
Loading…
Reference in a new issue