implement ui
This commit is contained in:
148
src/ui/GameWorldView.h
Normal file
148
src/ui/GameWorldView.h
Normal file
@@ -0,0 +1,148 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include <QElapsedTimer>
|
||||
#include <QOpenGLWidget>
|
||||
#include <QPoint>
|
||||
#include <QRectF>
|
||||
#include <QTimer>
|
||||
#include <QVector2D>
|
||||
|
||||
#include "BlueprintDropEvent.h"
|
||||
#include "BuildingType.h"
|
||||
#include "EntityId.h"
|
||||
#include "FireEvent.h"
|
||||
#include "GameConfig.h"
|
||||
#include "Rotation.h"
|
||||
#include "Tick.h"
|
||||
#include "TickDriver.h"
|
||||
#include "VisualsConfig.h"
|
||||
|
||||
class Simulation;
|
||||
class QPainter;
|
||||
|
||||
struct QPointCompare
|
||||
{
|
||||
bool operator()(const QPoint& a, const QPoint& b) const
|
||||
{
|
||||
if (a.x() != b.x()) { return a.x() < b.x(); }
|
||||
return a.y() < b.y();
|
||||
}
|
||||
};
|
||||
|
||||
class GameWorldView : public QOpenGLWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
GameWorldView(Simulation* sim, const GameConfig* config,
|
||||
const VisualsConfig* visuals, QWidget* parent = nullptr);
|
||||
|
||||
signals:
|
||||
void selectionChanged(const std::vector<EntityId>& ids);
|
||||
void stateUpdated(Tick tick, int blocks, double speed);
|
||||
void gameOver();
|
||||
void builderModeExited();
|
||||
|
||||
public slots:
|
||||
void enterBuilderMode(BuildingType type);
|
||||
void exitBuilderMode();
|
||||
void setGameSpeed(double multiplier);
|
||||
|
||||
protected:
|
||||
void initializeGL() override;
|
||||
void paintGL() override;
|
||||
void keyPressEvent(QKeyEvent* event) override;
|
||||
void keyReleaseEvent(QKeyEvent* event) override;
|
||||
void mousePressEvent(QMouseEvent* event) override;
|
||||
void mouseMoveEvent(QMouseEvent* event) override;
|
||||
void mouseReleaseEvent(QMouseEvent* event) override;
|
||||
|
||||
private slots:
|
||||
void onFrame();
|
||||
|
||||
private:
|
||||
void drawTiles(QPainter& painter);
|
||||
void drawBuildings(QPainter& painter);
|
||||
void drawBeltItems(QPainter& painter);
|
||||
void drawScrap(QPainter& painter);
|
||||
void drawShips(QPainter& painter);
|
||||
void drawBeams(QPainter& painter);
|
||||
void drawOverlays(QPainter& painter);
|
||||
void drawScreenSpace(QPainter& painter);
|
||||
|
||||
float tilePx() const;
|
||||
float viewportWidthTiles() const;
|
||||
QPointF worldToWidget(QVector2D worldPos) const;
|
||||
QPointF tileToWidget(QPoint tile) const;
|
||||
QPoint widgetToTile(QPoint widgetPt) const;
|
||||
QRectF tileRect(QPoint tile) const;
|
||||
QRect viewportRect() const;
|
||||
|
||||
float asteroidLeftEdge() const;
|
||||
float enemyStationRightEdge() const;
|
||||
void clampScroll();
|
||||
|
||||
bool isValidPlacement(BuildingType type, QPoint anchor, Rotation rot) const;
|
||||
const BuildingDef* findBuildingDef(BuildingType type) const;
|
||||
EntityId buildingAtTile(QPoint tile) const;
|
||||
|
||||
std::optional<QVector2D> entityPosition(EntityId id) const;
|
||||
void stepSpeed(int delta);
|
||||
void placeAtTile(QPoint tile);
|
||||
|
||||
struct ActiveBeam
|
||||
{
|
||||
FireEvent event;
|
||||
qint64 emittedWallMs;
|
||||
};
|
||||
|
||||
struct ToastEntry
|
||||
{
|
||||
QString text;
|
||||
qint64 createdWallMs;
|
||||
};
|
||||
|
||||
static constexpr qint64 kBeamLifetimeMs = 300;
|
||||
static constexpr qint64 kToastLifetimeMs = 4000;
|
||||
static constexpr qint64 kToastFadeStartMs = 3500;
|
||||
static constexpr float kScrollSpeedTilesPerSec = 10.0f;
|
||||
|
||||
Simulation* m_sim;
|
||||
const GameConfig* m_config;
|
||||
const VisualsConfig* m_visuals;
|
||||
|
||||
TickDriver m_tickDriver;
|
||||
QElapsedTimer m_frameTimer;
|
||||
qint64 m_wallMs;
|
||||
double m_gameSpeedMultiplier;
|
||||
double m_prevNonZeroSpeed;
|
||||
float m_scrollXTiles;
|
||||
|
||||
QTimer* m_renderTimer;
|
||||
|
||||
std::vector<ActiveBeam> m_activeBeams;
|
||||
std::vector<ToastEntry> m_toasts;
|
||||
|
||||
std::optional<BuildingType> m_builderType;
|
||||
Rotation m_ghostRotation;
|
||||
QPoint m_ghostTile;
|
||||
bool m_ghostValid;
|
||||
std::set<QPoint, QPointCompare> m_beltDragTiles;
|
||||
bool m_dragging;
|
||||
|
||||
bool m_demolishMode;
|
||||
EntityId m_demolishHoverId;
|
||||
|
||||
std::vector<EntityId> m_selectedIds;
|
||||
bool m_boxSelecting;
|
||||
QPoint m_boxStartTile;
|
||||
QPoint m_boxCurrentTile;
|
||||
|
||||
bool m_scrollLeft;
|
||||
bool m_scrollRight;
|
||||
bool m_gameOverShown;
|
||||
};
|
||||
Reference in New Issue
Block a user