Files
c-evo-evo/game/localplayer/Term.MainScreen.Window.cpp
T

781 lines
28 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 window layer (Term.pas): controls, layout, the map view paint
// pipeline, scrolling and the helpers that locate tiles on screen.
//
// Deviations from the original, all caused by the move from direct GDI/LCL
// canvas painting to Qt's retained paintEvent:
// - the map is always repainted completely when the view moves (the original
// scrolled the previous picture with ScrollDC and only painted the exposed
// strips);
// - the unit "blink" tile of PaintLocTemp is kept in m_blinkTile and drawn on
// top of the map layer by paintEvent instead of being blitted onto the
// screen;
// - the shared global Offscreen/OffscreenUser handover is gone, the window
// owns its map buffer.
#include "Term.MainScreen.h"
#include "Cevo/CevoGraphicSet.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "Global.h"
#include <QApplication>
#include <QCloseEvent>
#include <QCursor>
#include <QPainter>
#include <QRect>
#include <QWheelEvent>
#include <algorithm>
namespace Cevo {
namespace Term {
namespace {
constexpr int ScrollBarWidth = 16;
constexpr int BrushTypeCount = 26;
QString controlsHint(int tag)
{
return phrases.lookup(QStringLiteral("CONTROLS"), (tag & 0xFF) - 1);
}
} // namespace
void MainScreen::createControls()
{
const auto makeButtonB = [this](int tag, int index, bool permanent) {
auto *button = new ButtonB(this);
button->setProperty("tag", tag);
button->setButtonIndex(index);
button->setPermanent(permanent);
button->setVisible(false);
if ((tag & 0xFF) != 0)
button->setToolTip(controlsHint(tag));
return button;
};
const auto makeButtonC = [this](int tag, int index) {
auto *button = new ButtonC(this);
button->setProperty("tag", tag);
button->setVisible(false);
if ((tag & 0xFF) != 0)
button->setToolTip(controlsHint(tag));
if (index != 1)
index = static_cast<int>((mapOptionChecked >> (tag >> 8)) & 1) + 2;
button->setButtonIndex(index);
return button;
};
const auto makeArea = [this](const QString &hint) {
auto *area = new Area(this);
if (!phrases2FallenBackToEnglish)
area->setToolTip(hint);
return area;
};
m_unitBtn = makeButtonB(14, 2, false);
m_unitInfoBtn = makeButtonB(15, 7, false);
m_terrainBtn = makeButtonB(28, 3, false);
for (int i = 0; i < 4; ++i)
m_movieSpeedBtn[static_cast<std::size_t>(i)] = makeButtonB((i + 1) << 8, 24 + i, true);
m_mapBtn0 = makeButtonC(51, 2);
m_mapBtn1 = makeButtonC(291, 2);
m_mapBtn4 = makeButtonC(1028, 1);
m_mapBtn5 = makeButtonC(1328, 2);
m_mapBtn6 = makeButtonC(1541, 1);
m_eot = new EOTButton(this);
m_eot->setVisible(false);
m_menuArea = makeArea(phrases2[QStringLiteral("BTN_MENU")]);
m_menuArea->setGeometry(2, 1, 36, 36);
m_treasuryArea = makeArea(phrases2[QStringLiteral("TIP_TREASURY")]);
m_treasuryArea->resize(164, 36);
m_researchArea = makeArea(phrases[QStringLiteral("SCIENCE")]);
m_researchArea->resize(240, 36);
m_managementArea = makeArea(phrases2[QStringLiteral("BTN_MANAGE")]);
m_managementArea->resize(56, 40);
initButtons();
if (Templates)
m_eot->setTemplate(&Templates->data);
m_timer = new QTimer(this);
m_timer->setInterval(50);
connect(m_timer, &QTimer::timeout, this, &MainScreen::timerTick);
}
void MainScreen::resizeControls()
{
const int w = width();
const int h = height();
const int lx = ClientTools::g.lx;
const int ly = ClientTools::g.ly;
const int xxt = m_mainMap->xxt;
const int yyt = m_mainMap->yyt;
createOffscreen(m_offscreen, w, h);
m_mainMap->setOutput(&m_offscreen);
m_offscreen.canvas().font().assign(UniFont[static_cast<std::size_t>(FontType::Small)]);
m_smallScreen = w < 1024;
const int maxMapWidth = (lx * 2 - 3) * xxt; // avoids the same tile being visible left and right
if (w <= maxMapWidth) {
m_mapWidth = w;
m_mapOffset = 0;
} else {
m_mapWidth = maxMapWidth;
m_mapOffset = (w - m_mapWidth) / 2;
}
m_mapHeight = h - TopBarHeight - PanelHeight + Overlap;
m_panel.setSize(w, PanelHeight);
m_topBar.setSize(w, TopBarHeight);
const int miniFrame = (LxmaxXxx - ly) / 2;
m_xMidPanel = (lx + miniFrame) * 2 + 1;
m_xRightPanel = w - LeftPanelWidth - 10;
m_trPitch = clientMode == cEditMap ? 2 * xxt : 66;
m_xMini = miniFrame - 5;
m_yMini = (PanelHeight - 26 - LxmaxXxx) / 2 + miniFrame;
m_ywmax = (ly - m_mapHeight / yyt + 1) & ~1;
m_ywcenter = -((m_mapHeight - yyt * (ly - 1)) / (4 * yyt)) * 2;
// only for ywmax <= 0
if (m_ywmax <= 0)
m_yw = m_ywcenter;
else if (m_yw < 0)
m_yw = 0;
else if (m_yw > m_ywmax)
m_yw = m_ywmax;
m_unitInfoBtn->move(m_xMidPanel + 7 + 99, h - 29);
m_unitBtn->move(m_xMidPanel + 7 + 99 + 31, h - 29);
m_terrainBtn->move(m_xMidPanel + 7 + 99 + 62, h - 29);
m_movieSpeedBtn[0]->move(w / 2 - 62, h - 91);
m_movieSpeedBtn[1]->move(w / 2 - 62 + 29, h - 91);
m_movieSpeedBtn[2]->move(w / 2 - 62 + 2 * 29, h - 91);
m_movieSpeedBtn[3]->move(w / 2 - 62 + 3 * 29 + 12, h - 91);
m_eot->move(w - 62, h - 64);
if (m_verticalScrollBar) {
m_verticalScrollBar->setBorderSpacing(h - YTroop - 24, w - m_xRightPanel + 8, 8);
auto *scrollBar = m_verticalScrollBar->scrollBar();
scrollBar->setGeometry(m_xRightPanel + 10 - 14 - ScrollBarWidth, h - MidPanelHeight + 8, ScrollBarWidth,
h - m_verticalScrollBar->marginTop() - m_verticalScrollBar->marginBottom());
}
m_mapBtn0->move(m_xMini + lx - 44, h - 15);
m_mapBtn1->move(m_xMini + lx - 28, h - 15);
m_mapBtn5->move(m_xMini + lx - 12, h - 15);
m_mapBtn4->move(m_xMini + lx + 20, h - 15);
m_mapBtn6->move(m_xMini + lx + 36, h - 15);
m_treasuryArea->move(w / 2 - 172, 1);
m_researchArea->move(w / 2, 1);
m_managementArea->move(w - XPalace, TopBarHeight + m_mapHeight - Overlap + YPalace);
arrangeMidPanel();
if (m_repaintOnResize)
repaintAll();
setTroopLoc(m_troopLoc);
}
void MainScreen::arrangeMidPanel()
{
if (clientMode == cEditMap) {
m_xTroop = m_xMidPanel + 15;
} else {
const int xxt = m_mainMap->xxt;
if (supervising)
m_xTerrain = m_xMidPanel + 2 * xxt + 14;
else if (width() < 1280)
m_xTerrain = width() / 2 + (1280 - width()) / 3;
else
m_xTerrain = width() / 2;
m_xTroop = m_xTerrain + 2 * xxt + 12;
// not perfect but we assume almost no one is still playing on a 800x600 screen
if (m_smallScreen && !supervising)
m_xTroop = m_xRightPanel + 10 - 3 * 66 - ScrollBarWidth - 19 - 4;
}
m_trRow = (m_xRightPanel + 10 - m_xTroop - ScrollBarWidth - 19) / m_trPitch;
}
void MainScreen::resizeEvent(QResizeEvent *event)
{
DrawDlg::resizeEvent(event);
if (ClientTools::g.lx <= 0 || ClientTools::g.ly <= 0)
return;
if (m_lastResizeWidth == width() && m_lastResizeHeight == height())
return;
m_lastResizeWidth = width();
m_lastResizeHeight = height();
resizeControls();
}
void MainScreen::showEvent(QShowEvent *event)
{
DrawDlg::showEvent(event);
m_lastResizeWidth = -1;
if (ClientTools::g.lx > 0 && ClientTools::g.ly > 0) {
m_lastResizeWidth = width();
m_lastResizeHeight = height();
resizeControls(); // places the mini map correctly according to its size
}
m_timer->start();
}
void MainScreen::closeEvent(QCloseEvent *event)
{
DrawDlg::closeEvent(event);
if (!event->isAccepted())
return;
m_timer->stop();
}
// ---- painting ----
void MainScreen::invalidateRect(int left, int top, int right, int bottom)
{
const QRect rect(left, top, right - left, bottom - top);
m_dirty += rect;
update(rect);
}
void MainScreen::updateNow()
{
if (m_dirty.isEmpty())
return;
const QRegion region = m_dirty;
m_dirty = QRegion();
repaint(region);
}
void MainScreen::paintEvent(QPaintEvent *)
{
m_dirty = QRegion();
if (ClientTools::g.lx <= 0 || m_mapWidth <= 0)
return;
mainOffscreenPaint();
QPainter painter(this);
const int cw = width();
const int ch = height();
const int mapBottom = TopBarHeight + m_mapHeight - Overlap;
const auto blit = [&painter](int x, int y, const Dpi::Bitmap &src, int xSrc, int ySrc, int w, int h) {
if (w > 0 && h > 0)
painter.drawImage(QPoint(x, y), src.image(), QRect(xSrc, ySrc, w, h));
};
if (m_mapOffset > 0 || m_mapOffset + m_mapWidth < cw) { // pillarbox: make left and right border black
const QColor color = ClientTools::me < 0 ? QColor(Qt::black) : colorFromDelphi(EmptySpaceColor);
if (m_xMidPanel > m_mapOffset) {
painter.fillRect(QRect(0, TopBarHeight, m_mapOffset, m_mapHeight - Overlap), color);
} else {
painter.fillRect(QRect(0, TopBarHeight, m_xMidPanel, m_mapHeight - Overlap), color);
painter.fillRect(QRect(m_xMidPanel, TopBarHeight, m_mapOffset - m_xMidPanel, m_mapHeight), color);
}
if (m_xRightPanel < m_mapOffset + m_mapWidth) {
painter.fillRect(QRect(m_mapOffset + m_mapWidth, TopBarHeight,
cw - (m_mapOffset + m_mapWidth), m_mapHeight - Overlap),
color);
} else {
painter.fillRect(QRect(m_mapOffset + m_mapWidth, TopBarHeight,
m_xRightPanel - (m_mapOffset + m_mapWidth), m_mapHeight),
color);
painter.fillRect(QRect(m_xRightPanel, TopBarHeight, cw - m_xRightPanel, m_mapHeight - Overlap), color);
}
}
painter.save();
painter.setClipRect(QRect(m_mapOffset, TopBarHeight, m_mapWidth, m_mapHeight), Qt::IntersectClip);
blit(m_mapOffset, TopBarHeight, m_offscreen, 0, 0, m_mapWidth, m_mapHeight);
if (m_blinkTileValid)
painter.drawImage(m_blinkTilePos + QPoint(m_mapOffset, TopBarHeight), m_blinkTile.image());
painter.restore();
blit(0, 0, m_topBar, 0, 0, cw, TopBarHeight);
blit(0, mapBottom, m_panel, 0, 0, m_xMidPanel, Overlap);
blit(m_xRightPanel, mapBottom, m_panel, m_xRightPanel, 0, m_panel.width() - m_xRightPanel, Overlap);
blit(0, TopBarHeight + m_mapHeight, m_panel, 0, Overlap, m_panel.width(), PanelHeight - Overlap);
if (m_pLogo >= 0 && ClientTools::g.RO[m_pLogo] == nullptr && m_aiLogo[static_cast<std::size_t>(m_pLogo)])
painter.drawImage(QPoint(m_xRightPanel + 10 - (16 + 64), ch - PanelHeight),
m_aiLogo[static_cast<std::size_t>(m_pLogo)]->image());
}
void MainScreen::mainOffscreenPaint()
{
if (ClientTools::me < 0) {
Dpi::Canvas &canvas = m_offscreen.canvas();
canvas.brush().setColor(Qt::black);
canvas.fillRect(QRect(0, 0, m_mapWidth, m_mapHeight));
canvas.brush().setStyle(Qt::NoBrush);
return;
}
m_mainMap->setPaintBounds(0, 0, m_mapWidth, m_mapHeight);
if (m_mapValid && m_xwd == m_xw && m_ywd == m_yw)
return; // map window not moved
processRect(m_xw, m_yw, m_mapWidth / m_mainMap->xxt, m_mapHeight / m_mainMap->yyt, prPaint | prInvalidate);
invalidateRect(m_xMidPanel, TopBarHeight + m_mapHeight - Overlap, m_xRightPanel, TopBarHeight + m_mapHeight);
m_xwd = m_xw;
m_ywd = m_yw;
m_mapValid = true;
}
void MainScreen::miniMapPaint()
{
m_miniMap->paint(ClientTools::myMap, m_mapWidth, clientMode, m_mainMap->xxt, m_xwMini);
}
void MainScreen::paintAll()
{
mainOffscreenPaint();
m_xwMini = m_xw;
m_ywMini = m_yw;
miniMapPaint();
panelPaint();
}
void MainScreen::repaintAll()
{
if (!m_mainMap || ClientTools::g.lx <= 0 || m_mapWidth <= 0)
return;
invalidateRect(0, TopBarHeight, width(), TopBarHeight + m_mapHeight);
m_mapValid = false;
paintAll();
}
void MainScreen::invalidateMiniMap()
{
const int lx = ClientTools::g.lx;
const int ly = ClientTools::g.ly;
invalidateRect(m_xMini + 2, TopBarHeight + m_mapHeight - Overlap + m_yMini + 2, m_xMini + 2 + lx * 2,
TopBarHeight + m_mapHeight - Overlap + m_yMini + 2 + ly);
}
void MainScreen::paintAllMaps()
{
mainOffscreenPaint();
m_xwMini = m_xw;
m_ywMini = m_yw;
miniMapPaint();
copyMiniToPanel();
invalidateMiniMap();
}
void MainScreen::copyMiniToPanel()
{
const int lx = ClientTools::g.lx;
const int ly = ClientTools::g.ly;
const int xxt = m_mainMap->xxt;
const int yyt = m_mainMap->yyt;
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_markCityLoc >= 0) {
// TODO: the city mark sprite (InitCityMark/CityMark2) is not ported yet.
} else if (m_ywmax <= 0) {
frame(canvas, m_xMini + 2 + lx - m_mapWidth / (xxt * 2), m_yMini + 2, m_xMini + 1 + lx + m_mapWidth / (xxt * 2),
m_yMini + 2 + ly - 1, MainTexture->colorMark, MainTexture->colorMark);
} else {
frame(canvas, m_xMini + 2 + lx - m_mapWidth / (xxt * 2), m_yMini + 2 + m_yw,
m_xMini + 1 + lx + m_mapWidth / (xxt * 2), m_yMini + m_yw + m_mapHeight / yyt, MainTexture->colorMark,
MainTexture->colorMark);
}
}
void MainScreen::processRect(int x0, int y0, int nx, int ny, int options)
{
const int lx = ClientTools::g.lx;
const int xxt = m_mainMap->xxt;
const int yyt = m_mainMap->yyt;
int xl = nx * xxt + xxt;
int yl = ny * yyt + yyt * 2;
int xs = (x0 - m_xw) * (xxt * 2) + (y0 & 1) * xxt - lx * (xxt * 2);
// |xs+xl/2-MapWidth/2| -> min
while (std::abs(2 * (xs + lx * (xxt * 2)) + xl - m_mapWidth) < std::abs(2 * xs + xl - m_mapWidth))
xs += lx * (xxt * 2);
const int ys = (y0 - m_yw) * yyt - yyt;
if (xs + xl > m_mapWidth)
xl = m_mapWidth - xs;
if (ys + yl > m_mapHeight)
yl = m_mapHeight - ys;
if (xl <= 0 || yl <= 0)
return;
if (options & prPaint) {
if (options & prAutoBounds)
m_mainMap->setPaintBounds(xs, ys, xs + xl, ys + yl);
m_mainMap->paint(xs, ys, x0 + lx * y0, nx, ny, -1, -1);
m_blinkTileValid = false;
}
if (options & prInvalidate)
invalidateRect(m_mapOffset + xs, TopBarHeight + ys, m_mapOffset + xs + xl, TopBarHeight + ys + yl);
}
void MainScreen::paintLoc(int loc, int radius)
{
if (!m_mapValid)
return;
const int lx = ClientTools::g.lx;
const int yLoc = (loc + lx * 1024) / lx - 1024;
const int x0 = (loc + ((yLoc & 1) - 2 * radius + lx * 1024) / 2) % lx;
processRect(x0, yLoc - 2 * radius, 4 * radius + 1, 4 * radius + 1, prPaint | prAutoBounds | prInvalidate);
updateNow();
}
void MainScreen::paintLocBeforeMove(int fromLoc)
{
if (!m_mapValid)
return;
const int lx = ClientTools::g.lx;
const int yLoc = (fromLoc + lx * 1024) / lx - 1024;
const int x0 = (fromLoc + ((yLoc & 1) + lx * 1024) / 2) % lx;
processRect(x0, yLoc, 1, 1, prPaint | prAutoBounds);
}
void MainScreen::paintLocTemp(int loc, PaintLocTempStyle style)
{
if (!m_mapValid)
return;
const int lx = ClientTools::g.lx;
const int xxt = m_noMap->xxt;
const int yyt = m_noMap->yyt;
if (m_blinkTile.width() != 2 * xxt || m_blinkTile.height() != 3 * yyt) {
m_blinkTile.setSize(2 * xxt, 3 * yyt);
m_blinkTile.canvas().font().assign(UniFont[static_cast<std::size_t>(FontType::Small)]);
}
const int y0 = loc / lx;
const int x0 = loc % lx;
int xMap = (x0 - m_xw) * (xxt * 2) + (y0 & 1) * xxt - lx * (xxt * 2);
// |xMap+xxt-MapWidth/2| -> min
while (std::abs(2 * (xMap + lx * (xxt * 2)) + 2 * xxt - m_mapWidth) < std::abs(2 * xMap + 2 * xxt - m_mapWidth))
xMap += lx * (xxt * 2);
const int yMap = (y0 - m_yw) * yyt - yyt;
m_noMap->setOutput(&m_blinkTile);
m_noMap->setPaintBounds(0, 0, 2 * xxt, 3 * yyt);
m_noMap->paint(0, 0, loc, 1, 1, -1, -1, style == PaintLocTempStyle::Blink);
m_blinkTile.canvas().end();
m_blinkTilePos = QPoint(xMap, yMap);
m_blinkTileValid = true;
invalidateRect(m_mapOffset + xMap, TopBarHeight + yMap, m_mapOffset + xMap + 2 * xxt,
TopBarHeight + yMap + 3 * yyt);
updateNow();
}
void MainScreen::paintDestination()
{
if (IsoEngine::unFocus >= 0 && (ClientTools::myUn[IsoEngine::unFocus].Status & IsoEngine::usGoto)) {
const int destination = ClientTools::myUn[IsoEngine::unFocus].Status >> 16;
if (destination != 0x7FFF && destination != ClientTools::myUn[IsoEngine::unFocus].Loc)
paintLocTemp(destination, PaintLocTempStyle::Blink);
}
}
// ---- scrolling and coordinates ----
void MainScreen::scroll(int dx, int dy)
{
const int lx = ClientTools::g.lx;
m_xw = (m_xw + lx + dx) % lx;
if (m_ywmax > 0) {
m_yw += 2 * dy;
if (m_yw < 0)
m_yw = 0;
else if (m_yw > m_ywmax)
m_yw = m_ywmax;
}
paintAllMaps();
updateNow();
}
void MainScreen::setMapPos(int loc, QPoint mapPos)
{
const int lx = ClientTools::g.lx;
const int xxt = m_mainMap->xxt;
const int yyt = m_mainMap->yyt;
m_xw = (loc % lx) - (mapPos.x() - ((xxt * 2) * ((loc / lx) & 1)) / 2) / (xxt * 2);
m_xw = (m_xw + lx) % lx;
if (m_ywmax <= 0) {
m_yw = m_ywcenter;
} else {
m_yw = (loc / lx - (mapPos.y() * 2) / (yyt * 2) + 1) & ~1;
if (m_yw < 0)
m_yw = 0;
else if (m_yw > m_ywmax)
m_yw = m_ywmax;
}
}
void MainScreen::centre(int loc)
{
setMapPos(loc, QPoint(m_mapWidth / 2, m_mapHeight / 2));
}
int MainScreen::locationOfScreenPixel(int x, int y) const
{
const int lx = ClientTools::g.lx;
const int xxt = m_mainMap->xxt;
const int yyt = m_mainMap->yyt;
const int qx = (x * (yyt * 2) + y * (xxt * 2) + xxt * yyt * 2) / (xxt * yyt * 4) - 1;
const int qy = (y * (xxt * 2) - x * (yyt * 2) - xxt * yyt * 2 + 4000 * xxt * yyt) / (xxt * yyt * 4) - 999;
return (m_xw + (qx - qy + 2048) / 2 - 1024 + lx) % lx + lx * (m_yw + qx + qy);
}
int MainScreen::getCenterLoc() const
{
const int lx = ClientTools::g.lx;
return (m_xw + m_mapWidth / (m_mainMap->xxt * 4)) % lx + (m_yw + m_mapHeight / (m_mainMap->yyt * 2)) * lx;
}
void MainScreen::focusOnLoc(int loc, int options)
{
const int lx = ClientTools::g.lx;
const int xxt = m_mainMap->xxt;
const int yyt = m_mainMap->yyt;
const int dx = lx + 1 - (m_xw - loc + lx * 1024 + 1) % lx;
const bool outside = dx >= (m_mapWidth + 1) / (xxt * 2) - 2
|| (m_ywmax > 0
&& ((m_yw > 0 && loc / lx <= m_yw + 1)
|| (m_yw < m_ywmax && loc / lx >= m_yw + (m_mapHeight - 1) / yyt - 2)));
bool changed = true;
if (outside) {
centre(loc);
paintAllMaps();
} else if (!m_mapValid) {
paintAllMaps();
} else {
changed = false;
}
if (options & flRepaintPanel)
panelPaint();
if (changed && (options & flImmUpdate))
updateNow();
}
void MainScreen::setTroopLoc(int loc)
{
m_troopLoc = loc;
m_trRow = (m_xRightPanel + 10 - m_xTroop - ScrollBarWidth - 19) / m_trPitch;
m_trCnt = 0;
int trixFocus = -1;
if (!ClientTools::myRO)
return;
if (clientMode == cEditMap) {
m_trCnt = BrushTypeCount;
} else if (loc >= 0 && (ClientTools::myMap[loc] & fUnit) != 0) {
if (ClientTools::myMap[loc] & fOwned) { // count own units here
int uixDefender = -1;
ClientTools::server(sGetDefender, ClientTools::me, m_troopLoc, &uixDefender);
for (int prio = 1; prio >= 0; --prio) {
for (int uix = 0; uix < ClientTools::myRO->nUn; ++uix) {
if (((uix == uixDefender) == (prio != 0)) && ClientTools::myUn[uix].Loc == loc) {
if (uix == IsoEngine::unFocus)
trixFocus = m_trCnt;
++m_trCnt;
}
}
}
} else { // count enemy units here
ClientTools::server(sGetUnits, ClientTools::me, loc, &m_trCnt);
}
}
if (!m_verticalScrollBar)
return;
if (m_trCnt == 0) {
m_verticalScrollBar->init(0, 1);
} else {
const int trRow = std::max(1, m_trRow);
m_verticalScrollBar->init((m_trCnt + trRow - 1) / trRow, 1);
if (m_verticalScrollBar->max() >= m_verticalScrollBar->pageSize() && trixFocus >= 0)
m_verticalScrollBar->setPosition(trixFocus / trRow);
}
}
void MainScreen::setUnFocus(int uix)
{
Q_ASSERT(!(uix >= 0 && supervising));
if (uix != IsoEngine::unFocus) {
IsoEngine::destinationMarkOn = false;
paintDestination();
if (uix >= 0)
unStartLoc = ClientTools::myUn[uix].Loc;
IsoEngine::blinkOn = false;
m_blinkTime = -1;
if (IsoEngine::unFocus >= 0) {
const int loc0 = ClientTools::myUn[IsoEngine::unFocus].Loc;
if (uix < 0 || loc0 != ClientTools::myUn[uix].Loc) {
IsoEngine::unFocus = -1;
paintLoc(loc0);
}
}
IsoEngine::unFocus = uix;
}
m_unitInfoBtn->setVisible(IsoEngine::unFocus >= 0);
m_unitBtn->setVisible(IsoEngine::unFocus >= 0);
checkTerrainBtnVisible();
}
void MainScreen::checkTerrainBtnVisible()
{
if (IsoEngine::unFocus >= 0) {
const TUn &un = ClientTools::myUn[IsoEngine::unFocus];
const TModel &model = ClientTools::myModel[un.mix];
const std::uint32_t tile = ClientTools::myMap[un.Loc];
m_terrainBtn->setVisible((tile & fCity) == 0 && un.Master < 0
&& (model.Kind == mkSettler
|| (model.Kind == mkSlaves
&& ClientTools::myRO->Wonder[woPyramids].EffectiveOwner >= 0)));
} else {
m_terrainBtn->setVisible(false);
}
}
void MainScreen::checkMovieSpeedBtnState()
{
const bool movie = gameMode == cMovie;
for (int i = 0; i < 4; ++i) {
ButtonB *button = m_movieSpeedBtn[static_cast<std::size_t>(i)];
if (movie)
button->setDown(m_movieSpeed == i + 1);
button->setVisible(movie);
}
}
void MainScreen::setMapOptions()
{
m_miniMap->mapOptions = mapOptionChecked;
IsoEngine::mapOptions = mapOptionChecked;
if (clientMode == cEditMap)
IsoEngine::mapOptions |= 1u << ClientTools::moEditMode;
if (m_locCodesChecked)
IsoEngine::mapOptions |= 1u << ClientTools::moLocCodes;
}
void MainScreen::setTileSize(IsoEngine::TileSize tileSize, int loc, QPoint mapPos)
{
m_mainMap->setTileSize(tileSize);
m_noMap->setTileSize(tileSize);
m_lastResizeWidth = -1; // force repaint on resize
resizeControls();
setMapPos(loc, mapPos);
paintAllMaps();
applyToVisibleForms(FormAction::SmartUpdateContent);
}
void MainScreen::setTileSizeCenter(IsoEngine::TileSize tileSize)
{
setTileSize(tileSize, getCenterLoc(), QPoint(m_mapWidth / 2, m_mapHeight / 2));
}
void MainScreen::wheelEvent(QWheelEvent *event)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
const QPoint pos = event->position().toPoint();
#else
const QPoint pos = event->pos();
#endif
const int delta = event->angleDelta().y();
if (delta == 0 || ClientTools::g.lx <= 0 || m_mapWidth <= 0)
return;
if (pos.y() > height() - MidPanelHeight && pos.y() < height()) {
if (m_verticalScrollBar && m_verticalScrollBar->scrollBar()->isVisible()) {
const int position = std::clamp(m_verticalScrollBar->position() + (delta > 0 ? -1 : 1), 0,
std::max(0, m_verticalScrollBar->max() - 1));
m_verticalScrollBar->setPosition(position);
}
} else {
const IsoEngine::TileSize current = m_mainMap->tileSize();
const int loc = locationOfScreenPixel(pos.x() - m_mapOffset, pos.y() - TopBarHeight);
const QPoint mapPos(pos.x() - m_mapOffset, pos.y() - TopBarHeight);
if (delta > 0 && current != IsoEngine::TileSize::Big)
setTileSize(static_cast<IsoEngine::TileSize>(static_cast<int>(current) + 1), loc, mapPos);
else if (delta < 0 && current != IsoEngine::TileSize::Small)
setTileSize(static_cast<IsoEngine::TileSize>(static_cast<int>(current) - 1), loc, mapPos);
}
event->accept();
}
// ---- timer ----
void MainScreen::timerTick()
{
if (!m_idle || ClientTools::me < 0 || gameMode == cMovie)
return;
QWidget *active = QApplication::activeWindow();
auto *activeDlg = qobject_cast<BufferedDrawDlg *>(active);
const bool dialogOpen = QApplication::activeModalWidget() != nullptr
|| (activeDlg && activeDlg->windowMode() != WindowMode::Persistent);
if (dialogOpen) {
m_blinkTime = BlinkOnTime + BlinkOffTime - 1;
if (!IsoEngine::blinkOn) {
IsoEngine::blinkOn = true;
if (IsoEngine::unFocus >= 0)
paintLocTemp(ClientTools::myUn[IsoEngine::unFocus].Loc);
else if (m_turnComplete && !supervising)
m_eot->setButtonIndexFast(eotBlinkOn);
}
return;
}
constexpr int ScrollWidth = 5;
if (active && !ClientTools::hasSaveOption(optionChecked, ClientTools::soScrollOff)) {
const int scrollSpeed = ClientTools::hasSaveOption(optionChecked, ClientTools::soScrollFast) ? 2 : 1;
int dx = 0;
int dy = 0;
const QPoint mousePos = mapFromGlobal(QCursor::pos());
if (mousePos.y() < height() - PanelHeight) {
if (mousePos.x() >= 0 && mousePos.x() <= ScrollWidth)
dx = -scrollSpeed; // scroll left
else if (mousePos.x() <= width() && mousePos.x() >= width() - ScrollWidth - 1)
dx = scrollSpeed; // scroll right
}
if (mousePos.y() >= 0 && mousePos.y() <= ScrollWidth)
dy = -scrollSpeed; // scroll up
else if (mousePos.y() <= height() && mousePos.y() >= height() - ScrollWidth - 1
&& mousePos.x() >= m_terrainBtn->x() + m_terrainBtn->width()
&& mousePos.x() < m_xRightPanel + 10 - 8)
dy = scrollSpeed; // scroll down
if (dx != 0 || dy != 0) {
if (auto *activeForm = qobject_cast<Dpi::Form *>(active); activeForm && activeForm != this
&& activeForm->onDeactivate)
activeForm->onDeactivate();
scroll(dx, dy);
}
}
m_blinkTime = (m_blinkTime + 1) % (BlinkOnTime + BlinkOffTime);
IsoEngine::blinkOn = m_blinkTime >= BlinkOffTime;
IsoEngine::destinationMarkOn = true;
if (IsoEngine::unFocus >= 0) {
if (m_blinkTime == 0 || m_blinkTime == BlinkOffTime) {
paintLocTemp(ClientTools::myUn[IsoEngine::unFocus].Loc, PaintLocTempStyle::Blink);
paintDestination();
}
} else if (m_turnComplete && !supervising) {
if (m_blinkTime == 0)
m_eot->setButtonIndexFast(eotBlinkOff);
else if (m_blinkTime == BlinkOffTime)
m_eot->setButtonIndexFast(eotBlinkOn);
}
}
} // namespace Term
} // namespace Cevo