Files
c-evo-evo/game/localplayer/Term.MainScreen.h
T

379 lines
15 KiB
C++

// This file is part of c-evo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#pragma once
// TMainScreen equivalent - PARTIAL PORT. Ported so far: the window layer and
// paint pipeline (Term.MainScreen.Window.cpp), PanelPaint (…Panel.cpp),
// InitTurn/NextUnit/EndTurn/ZoomToCity (…Turn.cpp), most of Client()
// (…Client.cpp) and the 17 lazy dialog accessors with the small cross-dialog
// methods in Term.MainScreen.cpp. Still missing: the menus, mouse/keyboard
// input, MoveUnit/ShowMove and sound. This header is kept SEPARATE from the
// widely-included Term.h so consumers that only need the `Term::mainScreen`
// pointer don't have to transitively include all the dialog headers below.
#include "Battle.h"
#include "Cevo/CevoArea.h"
#include "Cevo/CevoButtonB.h"
#include "Cevo/CevoButtonC.h"
#include "Cevo/CevoEOTButton.h"
#include "Cevo/CevoPVScrollBar.h"
#include "CityScreen.h"
#include "CityType.h"
#include "Diagram.h"
#include "Draft.h"
#include "Enhance.h"
#include "Help.h"
#include "IsoEngine.h"
#include "Log.h"
#include "MiniMap.h"
#include "MessgEx.h"
#include "NatStat.h"
#include "Nego.h"
#include "Rates.h"
#include "Select.h"
#include "TechTree.h"
#include "Term.h"
#include "UnitStat.h"
#include "Wonders.h"
#include <QPoint>
#include <QRegion>
#include <QTimer>
#include <array>
#include <memory>
namespace Cevo {
namespace Term {
class MainScreen : public DrawDlg {
Q_OBJECT
public:
explicit MainScreen(QWidget *parent = nullptr);
~MainScreen() override;
Wonders::WondersDlg &wondersDlg();
TechTree::TechTreeDlg &techTreeDlg();
Enhance::EnhanceDlg &enhanceDlg();
Nego::NegoDlg &negoDlg();
CityType::CityTypeDlg &cityTypeDlg();
Diagram::DiaDlg &diaDlg();
CityScreen::CityDlg &cityDlg();
Rates::RatesDlg &ratesDlg();
Battle::BattleDlg &battleDlg();
NatStat::NatStatDlg &natStatDlg();
UnitStat::UnitStatDlg &unitStatDlg();
Draft::DraftDlg &draftDlg();
Select::ModalSelectDlg &modalSelectDlg();
Select::ListDlg &listDlg();
MessgEx::MessgExDlg &messgExDlg();
Help::HelpDlg &helpDlg();
Cevo::LogDlg &logDlg();
// Positions a just-created persistent dialog relative to the (not yet
// ported) main screen's primary monitor - self-contained, doesn't
// need the real map/panel to exist.
void arrangeDialog(BufferedDrawDlg *form);
void arrangeDialogs();
int dipCall(int command);
int offerCall(TOffer &offer);
// TODO: needs PanelPaint/the real main map (not ported yet) - only
// the already-portable side (ListDlg::ecoChange()/NatStatDlg::
// ecoChange()/CityDlg::smartUpdateContent()) runs for now.
void updateViews(bool updateCityScreen = false);
bool contactRefused(int p, const QString &item);
// Repaints the map, minimap and panel from scratch.
void repaintAll();
void setAIName(int p, const QString &name);
void rememberPeaceViolation();
// Term.pas's GetTribeList(): fills unusedTribeFiles from the Tribes directory.
void getTribeList();
// Picks (at random among the best) the unused tribe whose colour differs most from the tribes in play.
int chooseUnusedTribe();
void showEnemyShipChange(const TShowShipChange &showShipChange);
// Term.pas's InitModule()/DoneModule(): one-time asset/table setup
// (advance icons, StdUnits graphics, smallImp).
void initModule();
void doneModule();
// Term.pas's TMainScreen.Client(): the in-process client entry point the
// server calls for the human player. See Term.MainScreen.Client.cpp for
// which commands are served so far.
void client(int command, int newPlayer, void *data);
// Term.pas's EndNego(): leaves the negotiation screen.
void endNego();
// Term.pas's ZoomToCity()/CityClosed(): open the city screen for a city
// and refocus once it is closed.
bool zoomToCity(int loc, bool nextUnitOnClose = false, int showEvent = 0);
void cityClosed(int activateUix, bool stepFocus = false, bool selectFocus = false);
// ---- Map view (Term.MainScreen.Window.cpp) ----
// Scrolls the base map position by (dx, dy) tiles and repaints.
void scroll(int dx, int dy);
// Puts location `loc` at pixel `mapPos` of the map view.
void setMapPos(int loc, QPoint mapPos);
void centre(int loc);
int locationOfScreenPixel(int x, int y) const;
int getCenterLoc() const;
void setTroopLoc(int loc);
void paintLoc(int loc, int radius = 0);
void paintLocBeforeMove(int fromLoc);
void paintLocTemp(int loc, PaintLocTempStyle style = PaintLocTempStyle::Normal);
void paintDestination();
void paintAll();
void paintAllMaps();
void panelPaint();
void setUnFocus(int uix);
void focusOnLoc(int loc, int options = 0);
// Moves the focused unit to an adjacent tile (Term.pas's MoveUnit).
int moveUnit(int dx, int dy, int options = 0);
// Path finder: moves the focused unit to loc, starts a multi-turn goto if too far.
void moveToLoc(int loc, bool checkSuicide);
// Animates a unit moving from step0 to step1 of nStep.
void moveOnScreen(const TShowMove &showMove, int step0, int step1, int nStep, bool restore = true);
void processEventsWithDisabledKeyboard();
// Client() branches that animate what happens on the map.
void clientShow(int command, int newPlayer, void *data);
// ---- Menus, settings and input (Term.MainScreen.Menus.cpp / Input.cpp) ----
void loadSettings();
void saveSettings();
void updateMusic();
void setDebugMap(int p);
void setViewpoint(int p);
void focusNextUnit(int dir = 1);
int doJob(int job);
void setFullScreen(bool active);
protected:
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override;
private:
enum class MenuCommand {
Help, TechTree, Jump, Run, Names, LocCodes, Log, EnhanceDef, CityTypes, RandomMap, FillMap, Website, Resign,
Disband, Utilize, City, Pillage, Home, Load, Unload, SelectTransport, GoOn, Cancel, Recover, Wait, NoOrders, Stay,
Centre, PrevUnit, NextUnit, UnitStat, CityStat, ScienceStat, EUnitStat, Diagram, Wonders, Ships, Nations, Empire,
Revolution, Trans, Afforest, Clear, Fort, AirBase, Canal, Mine, Farm, Irrigation, RailRoad, Road, Pollution,
Enhance, TileSmall, TileNormal, TileBig, SoundOn, SoundOnAlt, SoundOff, MusicOn, MusicOff
};
enum class PopupKind { Game, Unit, Stat, Terrain, Edit };
struct Menus;
void createMenus();
void initPopup(PopupKind kind);
void showPopup(PopupKind kind, const QPoint &globalPos);
void menuCommand(MenuCommand command);
// Triggers a menu item if it is visible and enabled (MenuClick_Check of FormKeyDown).
void menuClickChecked(PopupKind kind, MenuCommand command);
void mapMouseDown(Qt::MouseButton button, Qt::KeyboardModifiers modifiers, QPoint pos);
void mapMouseMove(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, QPoint pos);
void panelMouseDown(Qt::MouseButton button, Qt::KeyboardModifiers modifiers, QPoint pos);
void panelMouseMove(Qt::MouseButtons buttons, QPoint pos);
void panelMouseUp();
bool isPanelPixel(int x, int y) const;
void mapButtonClicked(ButtonC *button);
void mapOptionButtonChanged(ButtonC *button, int option);
void panelButtonClicked(ButtonB *button);
void disbandOrUtilize(bool utilize);
void setTroopLocAndPanel(int loc);
void wireControls();
QMenu *createUnitMenu();
void triggerMenu(MenuCommand command);
bool menuItemUsable(MenuCommand command) const;
void setTileSize(IsoEngine::TileSize tileSize, int loc, QPoint mapPos);
void setTileSizeCenter(IsoEngine::TileSize tileSize);
void checkTerrainBtnVisible();
void checkMovieSpeedBtnState();
void setMapOptions();
protected:
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void showEvent(QShowEvent *event) override;
void closeEvent(QCloseEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
private:
void createControls();
void resizeControls();
void arrangeMidPanel();
void mainOffscreenPaint();
void miniMapPaint();
void copyMiniToPanel();
void processRect(int x0, int y0, int nx, int ny, int options);
void timerTick();
void invalidateRect(int left, int top, int right, int bottom);
void invalidateMiniMap();
void initTurn(int newPlayer);
void nextUnit(int nearLoc, bool autoTurn);
bool endTurn(bool wasSkipped = false);
void paintTopBar();
void paintPanelBackground();
void paintPanelBrushes();
void paintPanelSelectedUnit();
void paintPanelTroops();
void paintPanelTerrain();
void paintPanelButtons();
void paintZoomedTile(Dpi::Bitmap &dst, int x, int y, int loc);
// Immediately repaints what invalidateRect() collected (Pascal's Update).
void updateNow();
// Map view state (Term.pas's private fields of the same names).
int m_xw = 0; // base map x
int m_yw = 0; // base map y
int m_xwd = 0;
int m_ywd = 0;
int m_xwMini = 0;
int m_ywMini = 0;
int m_xMidPanel = 0;
int m_xRightPanel = 0;
int m_xTroop = 0;
int m_xTerrain = 0;
int m_xMini = 0;
int m_yMini = 0;
int m_ywmax = 0;
int m_ywcenter = 0;
int m_troopLoc = -1;
int m_trCnt = 0;
int m_trRow = 0;
int m_trPitch = 66;
int m_mapWidth = 0;
int m_mapOffset = 0;
int m_mapHeight = 0;
int m_blinkTime = 0;
int m_markCityLoc = -1;
int m_brushLoc = -1;
bool m_edited = false;
std::uint32_t m_brushType = fGrass;
std::array<int, 64> m_trix{}; // what each visible panel slot shows (unit index or brush)
bool m_mapValid = false;
bool m_smallScreen = false;
bool m_turnComplete = false;
bool m_tracking = false;
bool m_skipped = false;
bool m_goOnPhase = false;
bool m_haveStrategyAdvice = false;
int m_pTurn = -1;
bool m_repaintOnResize = false;
bool m_locCodesChecked = false;
QRegion m_dirty;
std::array<std::unique_ptr<Dpi::Bitmap>, nPl> m_aiLogo;
int m_lastResizeWidth = 0;
int m_lastResizeHeight = 0;
Dpi::Bitmap m_offscreen; // the map view, window-sized
Dpi::Bitmap m_panel;
Dpi::Bitmap m_topBar;
// Temporarily painted tile (unit blink), drawn over the map layer.
Dpi::Bitmap m_blinkTile;
QPoint m_blinkTilePos;
bool m_blinkTileValid = false;
// The map rectangle a moving unit travels through (see moveOnScreen).
Dpi::Bitmap m_moveTile;
QPoint m_moveTilePos;
bool m_moveTileValid = false;
bool m_keyboardDisabled = false;
struct CurrentMoveInfo {
int afterMovePaintRadius = 0;
int afterAttackExpeller = -1;
bool doShow = false;
bool isAlly = false;
} m_currentMoveInfo;
std::unique_ptr<IsoEngine::IsoMap> m_mainMap;
std::unique_ptr<IsoEngine::IsoMap> m_noMap;
std::unique_ptr<IsoEngine::IsoMap> m_noMapPanel;
std::unique_ptr<MiniMap> m_miniMap;
std::unique_ptr<Menus> m_menus;
int m_editLoc = -1;
int m_mouseX = 0;
int m_mouseY = 0;
int m_dragStartLoc = -1;
QRect m_restoredBounds;
bool m_formFullScreen = false;
std::unique_ptr<PVScrollBar> m_verticalScrollBar;
ButtonB *m_unitBtn = nullptr;
ButtonB *m_unitInfoBtn = nullptr;
ButtonB *m_terrainBtn = nullptr;
std::array<ButtonB *, 4> m_movieSpeedBtn{};
ButtonC *m_mapBtn0 = nullptr;
ButtonC *m_mapBtn1 = nullptr;
ButtonC *m_mapBtn4 = nullptr;
ButtonC *m_mapBtn5 = nullptr;
ButtonC *m_mapBtn6 = nullptr;
EOTButton *m_eot = nullptr;
Area *m_menuArea = nullptr;
Area *m_treasuryArea = nullptr;
Area *m_researchArea = nullptr;
Area *m_managementArea = nullptr;
QTimer *m_timer = nullptr;
// Term.pas's implementation-level state (Jump/pLogo/GameOK/Idle/...).
std::array<int, nPl> m_jump{};
int m_pLogo = -1;
bool m_gameOK = true;
bool m_idle = false;
bool m_startRunning = false;
bool m_firstMovieTurn = true;
bool m_closable = false;
int m_movieSpeed = 1;
std::unique_ptr<Wonders::WondersDlg> m_wondersDlg;
std::unique_ptr<TechTree::TechTreeDlg> m_techTreeDlg;
std::unique_ptr<Enhance::EnhanceDlg> m_enhanceDlg;
std::unique_ptr<Nego::NegoDlg> m_negoDlg;
std::unique_ptr<CityType::CityTypeDlg> m_cityTypeDlg;
std::unique_ptr<Diagram::DiaDlg> m_diaDlg;
std::unique_ptr<CityScreen::CityDlg> m_cityDlg;
std::unique_ptr<Rates::RatesDlg> m_ratesDlg;
std::unique_ptr<Battle::BattleDlg> m_battleDlg;
std::unique_ptr<NatStat::NatStatDlg> m_natStatDlg;
std::unique_ptr<UnitStat::UnitStatDlg> m_unitStatDlg;
std::unique_ptr<Draft::DraftDlg> m_draftDlg;
std::unique_ptr<Select::ModalSelectDlg> m_modalSelectDlg;
std::unique_ptr<Select::ListDlg> m_listDlg;
std::unique_ptr<MessgEx::MessgExDlg> m_messgExDlg;
std::unique_ptr<Help::HelpDlg> m_helpDlg;
std::unique_ptr<Cevo::LogDlg> m_logDlg;
};
enum class FormAction { Close, Enable, Disable, Update, SmartUpdateContent };
// Term.pas's ApplyToVisibleForms(): applies the action to every visible
// BufferedDrawDlg window.
void applyToVisibleForms(FormAction formAction);
// Term.pas's ChooseResearch(): lets the player pick the next research
// (including the draft of a new military unit class / far advance). Returns
// false if the player cancelled.
bool chooseResearch();
} // namespace Term
} // namespace Cevo