Files
c-evo-evo/game/localplayer/Term.MainScreen.Input.cpp
2026-11-09 11:30:09 +01:00

755 lines
35 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.
// TMainScreen's mouse and keyboard handling (Term.pas): MapBoxMouseDown,
// PanelBoxMouse*, FormMouse*, FormKeyDown/FormKeyUp and the buttons.
#include "Term.MainScreen.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "Global.h"
#include "KeyBindings.h"
#include "Messg.h"
#include "Sound.h"
#include <QApplication>
#include <QElapsedTimer>
#include <QKeyEvent>
#include <QMenu>
#include <QMouseEvent>
namespace Cevo {
namespace Term {
namespace {
QKeySequence keyOf(const QKeyEvent *event)
{
return QKeySequence(event->modifiers() | event->key());
}
} // namespace
bool MainScreen::isPanelPixel(int x, int y) const
{
return y >= TopBarHeight + m_mapHeight || (y >= height() - PanelHeight && (x < m_xMidPanel || x >= m_xRightPanel));
}
void MainScreen::mousePressEvent(QMouseEvent *event)
{
if (!m_idle)
return;
const QPoint pos = event->position().toPoint();
const int x = pos.x();
const int y = pos.y();
if (x < 40 && y < 40) {
if (gameMode != cMovie)
showPopup(PopupKind::Game, mapToGlobal(QPoint(0, TopBarHeight - 1)));
} else if (isPanelPixel(x, y)) {
panelMouseDown(event->button(), event->modifiers(), QPoint(x, y - (height() - PanelHeight)));
} else if (y >= TopBarHeight && x >= m_mapOffset && x < m_mapOffset + m_mapWidth) {
mapMouseDown(event->button(), event->modifiers(), QPoint(x - m_mapOffset, y - TopBarHeight));
}
}
void MainScreen::mouseMoveEvent(QMouseEvent *event)
{
if (!m_idle)
return;
const QPoint pos = event->position().toPoint();
if (isPanelPixel(pos.x(), pos.y()))
panelMouseMove(event->buttons(), QPoint(pos.x(), pos.y() - (height() - PanelHeight)));
else if (pos.y() >= TopBarHeight && pos.x() >= m_mapOffset && pos.x() < m_mapOffset + m_mapWidth)
mapMouseMove(event->buttons(), event->modifiers(), QPoint(pos.x() - m_mapOffset, pos.y() - TopBarHeight));
}
void MainScreen::mouseReleaseEvent(QMouseEvent *)
{
if (m_idle)
panelMouseUp();
}
void MainScreen::panelMouseMove(Qt::MouseButtons buttons, QPoint pos)
{
const int lx = ClientTools::g.lx;
const int ly = ClientTools::g.ly;
const int xxt = m_mainMap->xxt;
const int yyt = m_mainMap->yyt;
if (!(m_tracking && (buttons & Qt::LeftButton))) {
m_tracking = false;
return;
}
const int x = pos.x();
const int y = pos.y();
if (x >= m_xMini + 2 && y >= m_yMini + 2 && x < m_xMini + 2 + 2 * lx && y < m_yMini + 2 + ly) {
const int xCentre = (m_xwMini + (x - m_xMini - 2) / 2 + lx / 2 + m_mapWidth / (xxt * 4)) % lx;
const int yCentre = y - m_yMini - 2;
m_xw = (xCentre - m_mapWidth / (xxt * 4) + lx) % lx;
if (m_ywmax <= 0) {
m_yw = m_ywcenter;
} else {
m_yw = (yCentre - m_mapHeight / (yyt * 2) + 1) & ~1;
if (m_yw < 0)
m_yw = 0;
else if (m_yw > m_ywmax)
m_yw = m_ywmax;
}
// the mini map with the frame around the view being dragged
bitBltBitmap(m_panel, m_xMini + 2, m_yMini + 2, lx * 2, ly, m_miniMap->bitmap, 0, 0);
Dpi::Canvas &canvas = m_panel.canvas();
if (m_ywmax <= 0)
frame(canvas, x - m_mapWidth / (xxt * 2), m_yMini + 2, x + m_mapWidth / (xxt * 2) - 1, m_yMini + 2 + ly - 1,
MainTexture->colorMark, MainTexture->colorMark);
else
frame(canvas, x - m_mapWidth / (xxt * 2), m_yMini + 2 + m_yw, x + m_mapWidth / (xxt * 2) - 1,
m_yMini + 2 + m_yw + m_mapHeight / yyt - 2, MainTexture->colorMark, MainTexture->colorMark);
canvas.end();
mainOffscreenPaint();
invalidateMiniMap();
updateNow();
}
}
void MainScreen::panelMouseUp()
{
if (m_tracking) {
m_tracking = false;
m_xwMini = m_xw;
m_ywMini = m_yw;
miniMapPaint();
panelPaint();
}
}
void MainScreen::mapMouseMove(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers, QPoint pos)
{
m_mouseX = pos.x();
m_mouseY = pos.y();
if (clientMode == cEditMap && (buttons & Qt::LeftButton) && !m_tracking) {
const int mouseLoc = locationOfScreenPixel(pos.x(), pos.y());
if (mouseLoc != m_brushLoc)
mapMouseDown(Qt::LeftButton, modifiers, pos);
}
}
void MainScreen::panelMouseDown(Qt::MouseButton button, Qt::KeyboardModifiers modifiers, QPoint pos)
{
using ClientTools::me;
using ClientTools::myMap;
using ClientTools::myRO;
using ClientTools::myUn;
const int lx = ClientTools::g.lx;
const int ly = ClientTools::g.ly;
const int x = pos.x();
const int y = pos.y();
if (gameMode == cMovie || button != Qt::LeftButton)
return;
const bool shift = modifiers & Qt::ShiftModifier;
if (x >= m_xMini + 2 && y >= m_yMini + 2 && x < m_xMini + 2 + 2 * lx && y < m_yMini + 2 + ly) {
if (shift) {
const int xMouse = (m_xwMini + (x - (m_xMini + 2) + m_mapWidth / (m_mainMap->xxt * 2) + lx) / 2) % lx;
const int mouseLoc = xMouse + lx * (y - (m_yMini + 2));
if ((myMap[mouseLoc] & fTerrain) != static_cast<std::uint32_t>(fUNKNOWN)) {
const int p1 = (*myRO->Territory)[mouseLoc];
if (p1 == me || (p1 >= 0 && myRO->Treaty[p1] >= trNone))
natStatDlg().showNewContent(WindowMode::Persistent, p1);
}
} else {
if (m_cityDlg && m_cityDlg->isVisible())
m_cityDlg->close();
if (m_unitStatDlg && m_unitStatDlg->isVisible())
m_unitStatDlg->close();
m_tracking = true;
panelMouseMove(Qt::LeftButton, pos);
}
} else if (clientMode != cEditMap && x >= width() - XPalace && y >= YPalace && x < width() - XPalace + xSizeBig
&& y < YPalace + ySizeBig) {
showPopup(PopupKind::Stat,
mapToGlobal(QPoint(width() - XPalace + 6, height() - PanelHeight + YPalace + ySizeBig + 3)));
} else if (x >= m_xTroop + 1 && y >= YTroop + 1 && x < m_xTroop + m_trRow * m_trPitch && y <= YTroop + 55) {
const int i = (x - m_xTroop - 1) / m_trPitch;
const int selected = m_trix[static_cast<std::size_t>(i)];
if (selected < 0)
return;
if (clientMode == cEditMap) {
m_brushType = static_cast<std::uint32_t>(selected);
panelPaint();
} else if (m_troopLoc >= 0) {
if ((myMap[m_troopLoc] & fOwned) != 0) {
if (shift) {
unitStatDlg().showNewContentOwnModel(WindowMode::Persistent, myUn[selected].mix);
} else if (!supervising && clientMode < scContact && x - m_xTroop - 1 - i * m_trPitch >= 60 - 20
&& y >= YTroop + 35
&& (myUn[selected].Job > jNone
|| (myUn[selected].Status & (IsoEngine::usStay | IsoEngine::usRecover | IsoEngine::usGoto)) != 0)) {
// wake up
myUn[selected].Status = (myUn[selected].Status
& (0xFFFF - IsoEngine::usStay - IsoEngine::usRecover - IsoEngine::usGoto
- IsoEngine::usEnhance))
| usWaiting;
if (myUn[selected].Job > jNone)
ClientTools::server(sStartJob + (jNone << 4), me, selected, nullptr);
if (IsoEngine::unFocus < 0 && !(m_cityDlg && m_cityDlg->isVisible())) {
setUnFocus(selected);
setTroopLoc(myUn[selected].Loc);
focusOnLoc(m_troopLoc, flRepaintPanel);
} else {
if (m_cityDlg && m_cityDlg->isVisible() && m_cityDlg->restoreUnFocus < 0)
m_cityDlg->restoreUnFocus = selected;
panelPaint();
}
} else if (clientMode < scContact) {
if (supervising) {
unitStatDlg().showNewContentOwnUnit(WindowMode::Persistent, selected);
} else if (m_cityDlg && m_cityDlg->isVisible()) {
m_cityDlg->closeAction = CityScreen::CityCloseAction::None;
m_cityDlg->close();
ClientTools::sumCities(taxSum, scienceSum);
setUnFocus(selected);
} else {
IsoEngine::destinationMarkOn = false;
paintDestination();
IsoEngine::unFocus = selected;
unStartLoc = m_troopLoc;
m_blinkTime = 0;
IsoEngine::blinkOn = false;
paintLoc(m_troopLoc);
}
if (IsoEngine::unFocus >= 0) {
m_unitInfoBtn->setVisible(true);
m_unitBtn->setVisible(true);
m_turnComplete = false;
m_eot->setButtonIndex(eotGray);
}
checkTerrainBtnVisible();
panelPaint();
}
} else if (ClientTools::server(sGetUnits, me, m_troopLoc, &m_trCnt) >= rExecuted) {
if (shift)
unitStatDlg().showNewContentEnemyModel(WindowMode::Persistent,
(*myRO->EnemyUn)[myRO->nEnemyUn + selected].emix); // model info
else
unitStatDlg().showNewContentEnemyUnit(WindowMode::Persistent, myRO->nEnemyUn + selected); // unit info
}
}
}
}
void MainScreen::mapMouseDown(Qt::MouseButton button, Qt::KeyboardModifiers modifiers, QPoint pos)
{
using ClientTools::me;
using ClientTools::myMap;
using ClientTools::myModel;
using ClientTools::myRO;
using ClientTools::myUn;
using ClientTools::server;
const int lx = ClientTools::g.lx;
const int ly = ClientTools::g.ly;
const bool shift = modifiers & Qt::ShiftModifier;
const bool ctrl = modifiers & Qt::ControlModifier;
if (gameMode == cMovie)
return;
if (m_cityDlg && m_cityDlg->isVisible())
m_cityDlg->close();
if (m_unitStatDlg && m_unitStatDlg->isVisible())
m_unitStatDlg->close();
const int mouseLoc = locationOfScreenPixel(pos.x(), pos.y());
if (mouseLoc < 0 || mouseLoc >= lx * ly)
return;
if (button == Qt::LeftButton && !shift) {
bool doCenter = true;
if (clientMode == cEditMap) {
doCenter = false;
TEditTileData editTileData;
editTileData.Loc = mouseLoc;
const std::uint32_t tile = myMap[mouseLoc];
const std::uint32_t brush = m_brushType;
std::uint32_t newTile;
if (ctrl) { // toggle special resource
const std::uint32_t special = (((tile >> 5) & 3) + 1);
switch (tile & fTerrain) {
case fOcean: newTile = tile; break;
case fGrass:
case fArctic: newTile = (tile & ~static_cast<std::uint32_t>(fSpecial)) | ((special % 2) << 5); break;
default: newTile = (tile & ~static_cast<std::uint32_t>(fSpecial)) | ((special % 3) << 5); break;
}
} else if (brush <= static_cast<std::uint32_t>(fTerrain)) {
newTile = (tile & ~static_cast<std::uint32_t>(fTerrain)) | fSpecial | brush;
} else if ((brush & fDeadLands) != 0) {
if ((tile & (fDeadLands | fModern)) == (brush & (fDeadLands | fModern)))
newTile = tile & ~static_cast<std::uint32_t>(fDeadLands | fModern);
else
newTile = (tile & ~static_cast<std::uint32_t>(fDeadLands | fModern)) | brush;
} else if ((brush & fTerImp) != 0) {
if ((tile & fTerImp) == brush)
newTile = tile & ~static_cast<std::uint32_t>(fTerImp);
else
newTile = (tile & ~static_cast<std::uint32_t>(fTerImp)) | brush;
} else if ((brush & (fPrefStartPos | fStartPos)) != 0) {
if ((tile & (fPrefStartPos | fStartPos)) == (brush & (fPrefStartPos | fStartPos)))
newTile = tile & ~static_cast<std::uint32_t>(fPrefStartPos | fStartPos);
else
newTile = (tile & ~static_cast<std::uint32_t>(fPrefStartPos | fStartPos)) | brush;
} else {
newTile = tile ^ brush;
}
editTileData.NewTile = static_cast<std::int32_t>(newTile);
server(sEditTile, me, 0, &editTileData);
m_edited = true;
m_brushLoc = mouseLoc;
paintLoc(mouseLoc, 2);
miniMapPaint();
copyMiniToPanel();
m_panel.canvas().end();
invalidateMiniMap();
} else if ((myMap[mouseLoc] & fCity) != 0) { // city clicked
if ((myMap[mouseLoc] & (fOwned | fSpiedOut)) != 0) {
zoomToCity(mouseLoc);
doCenter = false;
} else {
unitStatDlg().showNewContentEnemyCity(WindowMode::Persistent, mouseLoc);
doCenter = false;
}
} else if ((myMap[mouseLoc] & fUnit) != 0) { // unit clicked
if ((myMap[mouseLoc] & fOwned) != 0) {
doCenter = false;
if (!supervising && clientMode < scContact) { // not in negotiation mode
int uix;
if (IsoEngine::unFocus >= 0 && myUn[IsoEngine::unFocus].Loc == mouseLoc) { // rotate
uix = (IsoEngine::unFocus + 1) % myRO->nUn;
int i = myRO->nUn - 1;
while (i > 0) {
if (myUn[uix].Loc == mouseLoc && myUn[uix].Job == jNone
&& (myUn[uix].Status & (IsoEngine::usStay | IsoEngine::usRecover | IsoEngine::usEnhance | usWaiting))
== usWaiting)
break;
--i;
uix = (uix + 1) % myRO->nUn;
}
if (i == 0)
uix = IsoEngine::unFocus;
} else {
server(sGetDefender, me, mouseLoc, &uix);
}
if (uix != IsoEngine::unFocus)
setUnFocus(uix);
m_turnComplete = false;
m_eot->setButtonIndex(eotGray);
}
setTroopLoc(mouseLoc);
panelPaint();
} else if ((myMap[mouseLoc] & fSpiedOut) != 0 && !ctrl) {
doCenter = false;
setTroopLoc(mouseLoc);
panelPaint();
} else {
doCenter = false;
unitStatDlg().showNewContentEnemyLoc(WindowMode::Persistent, mouseLoc);
}
}
if (doCenter) {
centre(mouseLoc);
paintAllMaps();
}
} else if (clientMode != cEditMap && button == Qt::RightButton && !shift) {
if (supervising) {
m_editLoc = mouseLoc;
server(sGetModels, me, 0, nullptr);
QMenu *createUnit = createUnitMenu();
int createLoc = mouseLoc;
for (int p1 = 0; p1 < nPl; ++p1) {
if (((1 << p1) & myRO->Alive) == 0)
continue;
QMenu *playerMenu = createUnit->addMenu(Tribes::tribe[static_cast<std::size_t>(p1)]->tPhrase(QStringLiteral("SHORTNAME")));
for (int emix = myRO->nEnemyModel - 1; emix >= 0; --emix) {
const TModelInfo &model = (*myRO->EnemyModel)[emix];
if (model.Owner == p1
&& server(sCreateUnit - sExecute + (p1 << 4), me, model.mix, &createLoc) >= rExecuted) {
if (!Tribes::tribe[static_cast<std::size_t>(p1)]->modelPictureAt(model.mix).hGr)
initEnemyModel(emix);
QAction *action = playerMenu->addAction(
Tribes::tribe[static_cast<std::size_t>(p1)]->modelNameAt(model.mix));
const int mix = model.mix;
connect(action, &QAction::triggered, this, [this, p1, mix] {
int loc = m_editLoc;
if (ClientTools::server(sCreateUnit + (p1 << 4), ClientTools::me, mix, &loc) >= rExecuted)
paintLoc(m_editLoc);
});
}
}
playerMenu->menuAction()->setVisible(!playerMenu->isEmpty());
}
showPopup(PopupKind::Edit, mapToGlobal(QPoint(pos.x() + m_mapOffset, pos.y() + TopBarHeight)));
} else if (IsoEngine::unFocus >= 0 && myUn[IsoEngine::unFocus].Loc != mouseLoc) {
TUn &un = myUn[IsoEngine::unFocus];
const int dx = ((mouseLoc % lx * 2 + ((mouseLoc / lx) & 1)) - (un.Loc % lx * 2 + ((un.Loc / lx) & 1)) + 3 * lx)
% (2 * lx)
- lx;
const int dy = mouseLoc / lx - un.Loc / lx;
TMoveAdviceData moveAdviceData;
if (std::abs(dx) + std::abs(dy) < 3) {
IsoEngine::destinationMarkOn = false;
paintDestination();
un.Status = (un.Status & (0xFFFF - IsoEngine::usStay - IsoEngine::usRecover - IsoEngine::usGoto - IsoEngine::usEnhance))
| usWaiting;
moveUnit(dx, dy, muAutoNext); // simple move
} else if (ClientTools::getMoveAdvice(IsoEngine::unFocus, mouseLoc, moveAdviceData) >= rExecuted) {
if ((myMap[mouseLoc] & (fUnit | fOwned)) == static_cast<std::uint32_t>(fUnit)) {
// check for suicide mission before movement
Battle::BattleDlg &battle = battleDlg();
battle.forecast.pAtt = me;
battle.forecast.mixAtt = un.mix;
battle.forecast.HealthAtt = un.Health;
battle.forecast.ExpAtt = un.Exp;
battle.forecast.FlagsAtt = static_cast<std::int32_t>(un.Flags);
battle.forecast.Movement = un.Movement;
if (server(sGetBattleForecastEx, me, mouseLoc, &battle.forecast) >= rExecuted
&& battle.forecast.EndHealthAtt <= 0) {
battle.uix = IsoEngine::unFocus;
battle.toLoc = mouseLoc;
battle.isSuicideQuery = true;
if (battle.showModal() != Dpi::ModalResult::Ok)
return;
}
}
IsoEngine::destinationMarkOn = false;
paintDestination();
un.Status = (un.Status & ~(IsoEngine::usStay | IsoEngine::usRecover | IsoEngine::usEnhance)) | usWaiting;
moveToLoc(mouseLoc, false); // goto
}
}
} else if (button == Qt::MiddleButton && IsoEngine::unFocus >= 0
&& (myModel[myUn[IsoEngine::unFocus].mix].Kind == mkSettler
|| myModel[myUn[IsoEngine::unFocus].mix].Kind == mkSlaves)) {
IsoEngine::destinationMarkOn = false;
paintDestination();
myUn[IsoEngine::unFocus].Status = (myUn[IsoEngine::unFocus].Status
& (0xFFFF - IsoEngine::usStay - IsoEngine::usRecover - IsoEngine::usGoto))
| IsoEngine::usEnhance;
const int uix = IsoEngine::unFocus;
if (mouseLoc != myUn[uix].Loc)
moveToLoc(mouseLoc, true); // goto
if (IsoEngine::unFocus == uix && myUn[uix].Loc == mouseLoc)
triggerMenu(MenuCommand::Enhance);
} else if (button == Qt::LeftButton && shift && (myMap[mouseLoc] & fTerrain) != static_cast<std::uint32_t>(fUNKNOWN)) {
helpOnTerrain(mouseLoc, WindowMode::Persistent);
} else if (clientMode <= cContinue && button == Qt::RightButton && shift && IsoEngine::unFocus >= 0
&& (myMap[mouseLoc] & (fUnit | fOwned)) == static_cast<std::uint32_t>(fUnit)) { // battle forecast
TUn &un = myUn[IsoEngine::unFocus];
Battle::BattleDlg &battle = battleDlg();
battle.forecast.pAtt = me;
battle.forecast.mixAtt = un.mix;
battle.forecast.HealthAtt = un.Health;
battle.forecast.ExpAtt = un.Exp;
battle.forecast.FlagsAtt = static_cast<std::int32_t>(un.Flags);
battle.forecast.Movement = un.Movement;
if (server(sGetBattleForecastEx, me, mouseLoc, &battle.forecast) >= rExecuted) {
battle.uix = IsoEngine::unFocus;
battle.toLoc = mouseLoc;
const QPoint global = mapToGlobal(QPoint(pos.x() + m_mapOffset, pos.y() + TopBarHeight));
const QRect desktop = Dpi::Screen::primaryMonitor().boundsRect();
int left = global.x() - battle.width() / 2;
left = std::clamp(left, desktop.left(), std::max(desktop.left(), desktop.right() - battle.width()));
int top = global.y() - battle.height() / 2;
top = std::clamp(top, desktop.top(), std::max(desktop.top(), desktop.bottom() - battle.height()));
battle.move(left, top);
battle.isSuicideQuery = false;
battle.show();
}
}
Q_UNUSED(m_dragStartLoc);
}
void MainScreen::mapButtonClicked(ButtonC *button)
{
const int bit = button->property("tag").toInt() >> 8;
mapOptionChecked ^= 1u << bit;
setMapOptions();
button->setButtonIndex(static_cast<int>((mapOptionChecked >> bit) & 1) + 2);
if (button == m_mapBtn0) { // update mini map only
miniMapPaint();
panelPaint();
} else { // update main map
m_mapValid = false;
paintAllMaps();
}
}
void MainScreen::mapOptionButtonChanged(ButtonC *button, int option)
{
if (button->down()) {
mapOptionChecked |= 1u << option;
button->setToolTip(QString());
} else {
mapOptionChecked &= ~(1u << option);
button->setToolTip(phrases.lookup(QStringLiteral("CONTROLS"), (button->property("tag").toInt() & 0xFF) - 1));
}
setMapOptions();
m_mapValid = false;
paintAllMaps();
}
void MainScreen::panelButtonClicked(ButtonB *button)
{
if (IsoEngine::unFocus < 0)
return;
showPopup(button == m_unitBtn ? PopupKind::Unit : PopupKind::Terrain, mapToGlobal(button->pos()));
}
void MainScreen::wireControls()
{
m_unitBtn->onClick = [this] { panelButtonClicked(m_unitBtn); };
m_terrainBtn->onClick = [this] { panelButtonClicked(m_terrainBtn); };
m_unitInfoBtn->onClick = [this] {
if (IsoEngine::unFocus >= 0)
unitStatDlg().showNewContentOwnModel(WindowMode::Persistent, ClientTools::myUn[IsoEngine::unFocus].mix);
};
for (ButtonC *button : { m_mapBtn0, m_mapBtn1, m_mapBtn5 })
button->onClick = [this, button] { mapButtonClicked(button); };
m_mapBtn4->onDownChanged = [this] { mapOptionButtonChanged(m_mapBtn4, ClientTools::moGreatWall); };
m_mapBtn6->onDownChanged = [this] { mapOptionButtonChanged(m_mapBtn6, ClientTools::moBareTerrain); };
for (ButtonB *button : m_movieSpeedBtn) {
button->onClick = [this, button] {
m_movieSpeed = button->property("tag").toInt() >> 8;
checkMovieSpeedBtnState();
};
}
m_eot->onClick = [this] {
if (gameMode == cMovie) {
messgExDlg().cancelMovie();
ClientTools::server(sBreak, ClientTools::me, 0, nullptr);
} else if (clientMode < 0) {
m_skipped = true;
} else if (clientMode >= scContact) {
negoDlg().showNewContent(WindowMode::Persistent);
} else if (m_jump[static_cast<std::size_t>(m_pTurn)] > 0) {
m_jump[static_cast<std::size_t>(m_pTurn)] = 0;
m_startRunning = false;
} else {
endTurn();
}
};
}
void MainScreen::keyReleaseEvent(QKeyEvent *event)
{
if (m_keyboardDisabled)
return;
if (m_idle && event->key() == Qt::Key_Menu) // the menu key calls the game menu
showPopup(PopupKind::Game, mapToGlobal(QPoint(0, TopBarHeight - 1)));
}
void MainScreen::keyPressEvent(QKeyEvent *event)
{
using namespace KeyBindings;
using ClientTools::myUn;
if (m_keyboardDisabled)
return;
const QKeySequence key = keyOf(event);
if (bFullScreen->test(key))
setFullScreen(!fullScreen);
if (gameMode == cMovie) {
if (bScienceStat->test(key))
menuClickChecked(PopupKind::Stat, MenuCommand::ScienceStat);
else if (bDiagram->test(key))
menuClickChecked(PopupKind::Stat, MenuCommand::Diagram);
else if (bWonders->test(key))
menuClickChecked(PopupKind::Stat, MenuCommand::Wonders);
else if (bShips->test(key))
menuClickChecked(PopupKind::Stat, MenuCommand::Ships);
return;
}
if (!m_idle)
return;
// map move
const auto moveMap = [this](int x, int y) {
const int speed = ClientTools::hasSaveOption(optionChecked, ClientTools::soScrollFast) ? 2 : 1;
if (x != 0 || y != 0)
scroll(x * speed, y * speed);
};
if (bMapMoveLeftDown->test(key)) moveMap(-1, 1);
else if (bMapMoveDown->test(key)) moveMap(0, 2);
else if (bMapMoveRightDown->test(key)) moveMap(1, 1);
else if (bMapMoveLeft->test(key)) moveMap(-2, 0);
else if (bMapMoveRight->test(key)) moveMap(2, 0);
else if (bMapMoveLeftUp->test(key)) moveMap(-1, -1);
else if (bMapMoveUp->test(key)) moveMap(0, -2);
else if (bMapMoveRightUp->test(key)) moveMap(1, -1);
if (clientMode == cEditMap) {
if (bResign->test(key)) triggerMenu(MenuCommand::Resign);
else if (bRandomMap->test(key)) triggerMenu(MenuCommand::RandomMap);
else if (bFillMap->test(key)) triggerMenu(MenuCommand::FillMap);
else if (bHelp->test(key)) triggerMenu(MenuCommand::Help);
return;
}
if (bEndTurn->test(key)) endTurn();
else if (bHelp->test(key)) triggerMenu(MenuCommand::Help);
else if (bUnitStat->test(key)) menuClickChecked(PopupKind::Stat, MenuCommand::UnitStat);
else if (bCityStat->test(key)) menuClickChecked(PopupKind::Stat, MenuCommand::CityStat);
else if (bScienceStat->test(key)) menuClickChecked(PopupKind::Stat, MenuCommand::ScienceStat);
else if (bEUnitStat->test(key)) menuClickChecked(PopupKind::Stat, MenuCommand::EUnitStat);
else if (bDiagram->test(key)) menuClickChecked(PopupKind::Stat, MenuCommand::Diagram);
else if (bWonders->test(key)) menuClickChecked(PopupKind::Stat, MenuCommand::Wonders);
else if (bShips->test(key)) menuClickChecked(PopupKind::Stat, MenuCommand::Ships);
else if (bNations->test(key)) menuClickChecked(PopupKind::Stat, MenuCommand::Nations);
else if (bEmpire->test(key)) menuClickChecked(PopupKind::Stat, MenuCommand::Empire);
else if (bSetDebugMap0->test(key)) setDebugMap(-1);
else if (bSetDebugMap1->test(key)) setDebugMap(1);
else if (bSetDebugMap2->test(key)) setDebugMap(2);
else if (bSetDebugMap3->test(key)) setDebugMap(3);
else if (bSetDebugMap4->test(key)) setDebugMap(4);
else if (bSetDebugMap5->test(key)) setDebugMap(5);
else if (bSetDebugMap6->test(key)) setDebugMap(6);
else if (bSetDebugMap7->test(key)) setDebugMap(7);
else if (bSetDebugMap8->test(key)) setDebugMap(8);
else if (bSetDebugMap9->test(key)) setDebugMap(9);
else if (bJump->test(key)) triggerMenu(MenuCommand::Jump);
else if (bLocCodes->test(key)) triggerMenu(MenuCommand::LocCodes);
else if (bLogDlg->test(key)) {
if (logDlg().isVisible())
logDlg().close();
else
logDlg().show();
} else if (bNames->test(key)) triggerMenu(MenuCommand::Names);
else if (bResign->test(key)) menuClickChecked(PopupKind::Game, MenuCommand::Resign);
else if (bRun->test(key)) triggerMenu(MenuCommand::Run);
else if (bTestMapRepaint->test(key)) { // test map repaint time
QElapsedTimer timer;
timer.start();
m_mapValid = false;
mainOffscreenPaint();
simpleMessage(QStringLiteral("Map repaint time: %1 ms").arg(timer.nsecsElapsed() / 1e6, 0, 'f', 3));
} else if (bSetViewpoint0->test(key)) setViewpoint(0);
else if (bSetViewpoint1->test(key)) setViewpoint(1);
else if (bSetViewpoint2->test(key)) setViewpoint(2);
else if (bSetViewpoint3->test(key)) setViewpoint(3);
else if (bSetViewpoint4->test(key)) setViewpoint(4);
else if (bSetViewpoint5->test(key)) setViewpoint(5);
else if (bSetViewpoint6->test(key)) setViewpoint(6);
else if (bSetViewpoint7->test(key)) setViewpoint(7);
else if (bSetViewpoint8->test(key)) setViewpoint(8);
else if (bSetViewpoint9->test(key)) setViewpoint(9);
else if (bMapBtn0->test(key)) mapButtonClicked(m_mapBtn0);
else if (bMapBtn1->test(key)) mapButtonClicked(m_mapBtn1);
else if (bMapBtn4->test(key)) mapButtonClicked(m_mapBtn4);
else if (bMapBtn5->test(key)) mapButtonClicked(m_mapBtn5);
else if (bMapBtn6->test(key)) mapButtonClicked(m_mapBtn6);
else if (bTechTree->test(key)) triggerMenu(MenuCommand::TechTree);
else if (bWait->test(key)) triggerMenu(MenuCommand::Wait);
if (IsoEngine::unFocus >= 0) {
const auto doMoveUnit = [this](int x, int y) {
IsoEngine::destinationMarkOn = false;
paintDestination();
TUn &un = ClientTools::myUn[IsoEngine::unFocus];
un.Status = (un.Status & (0xFFFF - IsoEngine::usStay - IsoEngine::usRecover - IsoEngine::usGoto - IsoEngine::usEnhance))
| usWaiting;
moveUnit(x, y, muAutoNext);
};
using Cmd = MenuCommand;
const auto test = [](int job) {
return ClientTools::jobTest(IsoEngine::unFocus, job, 1ull << eTreaty);
};
if (bDisbandUnit->test(key)) triggerMenu(Cmd::Disband);
else if (bFortify->test(key)) menuClickChecked(PopupKind::Terrain, Cmd::Fort);
else if (bCenterUnit->test(key)) triggerMenu(Cmd::Centre);
else if (bStay->test(key)) triggerMenu(Cmd::Stay);
else if (bNoOrders->test(key)) triggerMenu(Cmd::NoOrders);
else if (bPrevUnit->test(key)) triggerMenu(Cmd::PrevUnit);
else if (bNextUnit->test(key)) triggerMenu(Cmd::NextUnit);
else if (bCancel->test(key)) menuClickChecked(PopupKind::Unit, Cmd::Cancel);
else if (bPillage->test(key)) menuClickChecked(PopupKind::Unit, Cmd::Pillage);
else if (bSelectTransport->test(key)) menuClickChecked(PopupKind::Unit, Cmd::SelectTransport);
else if (bAirBase->test(key)) menuClickChecked(PopupKind::Terrain, Cmd::AirBase);
else if (bBuildCity->test(key)) menuClickChecked(PopupKind::Unit, Cmd::City);
else if (bEnhance->test(key)) {
initPopup(PopupKind::Terrain);
if (menuItemUsable(Cmd::Enhance))
triggerMenu(Cmd::Enhance);
else
triggerMenu(Cmd::EnhanceDef);
} else if (bGoOn->test(key)) menuClickChecked(PopupKind::Unit, Cmd::GoOn);
else if (bHome->test(key)) menuClickChecked(PopupKind::Unit, Cmd::Home);
else if (bFarmClearIrrigation->test(key)) {
if (test(jFarm)) triggerMenu(Cmd::Farm);
else if (test(jClear)) triggerMenu(Cmd::Clear);
else menuClickChecked(PopupKind::Terrain, Cmd::Irrigation);
} else if (bLoad->test(key)) menuClickChecked(PopupKind::Unit, Cmd::Load);
else if (bAfforestMine->test(key)) {
if (test(jAfforest)) triggerMenu(Cmd::Afforest);
else menuClickChecked(PopupKind::Terrain, Cmd::Mine);
} else if (bCanal->test(key)) menuClickChecked(PopupKind::Terrain, Cmd::Canal);
else if (bTrans->test(key)) menuClickChecked(PopupKind::Terrain, Cmd::Trans);
else if (bPollution->test(key)) menuClickChecked(PopupKind::Terrain, Cmd::Pollution);
else if (bRailRoad->test(key)) {
if (test(jRR)) triggerMenu(Cmd::RailRoad);
else menuClickChecked(PopupKind::Terrain, Cmd::Road);
} else if (bUnload->test(key)) menuClickChecked(PopupKind::Unit, Cmd::Unload);
else if (bRecover->test(key)) menuClickChecked(PopupKind::Unit, Cmd::Recover);
else if (bUtilize->test(key)) menuClickChecked(PopupKind::Unit, Cmd::Utilize);
// unit move
else if (bUnitMoveLeftDown->test(key)) doMoveUnit(-1, 1);
else if (bUnitMoveDown->test(key)) doMoveUnit(0, 2);
else if (bUnitMoveRightDown->test(key)) doMoveUnit(1, 1);
else if (bUnitMoveLeft->test(key)) doMoveUnit(-2, 0);
else if (bUnitMoveRight->test(key)) doMoveUnit(2, 0);
else if (bUnitMoveLeftUp->test(key)) doMoveUnit(-1, -1);
else if (bUnitMoveUp->test(key)) doMoveUnit(0, -2);
else if (bUnitMoveRightUp->test(key)) doMoveUnit(1, -1);
}
Q_UNUSED(myUn);
}
void MainScreen::setFullScreen(bool active)
{
fullScreen = active;
if (active) {
if (!m_formFullScreen && !isFullScreen())
m_restoredBounds = geometry();
m_formFullScreen = true;
showFullScreen();
} else {
m_formFullScreen = false;
showNormal();
if (m_restoredBounds.isValid())
setGeometry(m_restoredBounds);
}
}
} // namespace Term
} // namespace Cevo