host the modals in the window instead of in windows of their own
Every dialog was an OS window with a dim widget behind it in the game window. That is why a click beside one could not reach it: Qt drops mouse events for a window a modal blocks, so the dim -- a child of the blocked window -- never saw the press. ModalLayer is that dim grown up: still a child of the main window covering its rect, but it now hosts the open modal, places it, and runs its event loop, so it is the widget the clicks beside a modal land on. It keeps a stack, paints one dim however many modals are open, and a hold keeps it up while one modal hands over to the next. ModalDimOverlay and ModalDimScope are gone; ModalLayerHold replaces the scope at the sites that still open a system message box. ModalDialog is what a dialog inherits in place of the window it lost: the panel background, the drawn header with its optional close button, and the dismissal gestures. isDismissible() governs Q and, next, the click outside -- one predicate because both gestures reach the same dialogs. Its default refuses, so a dialog opts in. requestDismiss() is what a gesture asks for, and ShipLayoutDialog overrides it with the one-step ladder its Q handler used to spell out, which is now reached by both. DialogDismiss.h folded into the base. The four dialogs the player meets keep their contents unchanged and lose their title bars: the recipe/schematic selection, blueprint selection, ship layout, and schematic choice dialogs. Placement moved with them -- placeOnSelectionPanel became getSelectionPanelAnchor, and the layer centers on that rectangle in window coordinates, with no global mapping and no frame height to guess at. Two things followed from dropping OS modality: the focus guard that hands focus back to the game world now also asks the layer whether a modal is open, and closeEvent refuses to close the window while one is, since its nested loop runs over widgets the window owns. The escape menu, the game-over and win screens, and the two name dialogs are still system dialogs; they are dimmed by a layer hold until they are converted next. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ne3mejABZoLWKLh8fgpM3x
This commit is contained in:
97
src/ui/ModalDialog.cpp
Normal file
97
src/ui/ModalDialog.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "ModalDialog.h"
|
||||
|
||||
#include <QBoxLayout>
|
||||
#include <QChar>
|
||||
#include <QFont>
|
||||
#include <QHBoxLayout>
|
||||
#include <QKeyEvent>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QString>
|
||||
|
||||
namespace
|
||||
{
|
||||
// The header's own metrics, matching the row the blueprint selection dialog drew
|
||||
// before this class existed (REQ-UI-BLUEPRINT-DIALOG).
|
||||
const int kSpacingPx = 8;
|
||||
const int kSmallButtonSizePx = 22;
|
||||
const QChar kCrossGlyph(0x00D7); // U+00D7 MULTIPLICATION SIGN
|
||||
|
||||
// Q dismisses an open dialog, beside the Escape that QDialog already handles
|
||||
// (REQ-UI-DIALOG-DISMISS). Ctrl must not be held, matching how the game world's
|
||||
// table separates a chord from the bare key (resolveKeyAction in
|
||||
// lib/core/ControlAction.cpp). This deliberately stays out of that table: the table
|
||||
// answers what an input does in the player's current situation, and a dialog has no
|
||||
// situation -- it holds focus and takes the key whatever the world beneath it is
|
||||
// doing.
|
||||
bool isDismissKey(const QKeyEvent& event)
|
||||
{
|
||||
return event.key() == Qt::Key_Q
|
||||
&& (event.modifiers() & Qt::ControlModifier) == 0;
|
||||
}
|
||||
}
|
||||
|
||||
ModalDialog::ModalDialog(QWidget* parent)
|
||||
: QDialog(parent)
|
||||
{
|
||||
// An ordinary child widget rather than a window: the layer places it and the dim
|
||||
// behind it is the same widget's paint (REQ-UI-MODAL-CHROME). Square corners rather
|
||||
// than rounded ones -- rounding needs a translucent background, which is unreliable
|
||||
// on Windows. The border matches the build bar and the selection panel.
|
||||
setWindowFlags(Qt::Widget);
|
||||
setAttribute(Qt::WA_StyledBackground, true);
|
||||
// A type selector, so it reaches every subclass but none of the child widgets
|
||||
// inside them, which draw themselves.
|
||||
setStyleSheet(QStringLiteral(
|
||||
"ModalDialog { background-color: palette(window);"
|
||||
" border: 1px solid palette(mid); }"));
|
||||
}
|
||||
|
||||
bool ModalDialog::isDismissible() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void ModalDialog::requestDismiss()
|
||||
{
|
||||
reject();
|
||||
}
|
||||
|
||||
QHBoxLayout* ModalDialog::addHeader(QBoxLayout* mainLayout, const QString& title,
|
||||
bool withCloseButton)
|
||||
{
|
||||
QHBoxLayout* headerLayout = new QHBoxLayout();
|
||||
headerLayout->setSpacing(kSpacingPx);
|
||||
|
||||
QLabel* titleLabel = new QLabel(title, this);
|
||||
QFont headerFont = titleLabel->font();
|
||||
headerFont.setBold(true);
|
||||
titleLabel->setFont(headerFont);
|
||||
headerLayout->addWidget(titleLabel);
|
||||
|
||||
// The stretch is added before the close button so a subclass inserting after the
|
||||
// title lands left of it, where the blueprint dialog's hotkey badge belongs.
|
||||
headerLayout->addStretch();
|
||||
|
||||
if (withCloseButton)
|
||||
{
|
||||
QPushButton* closeButton = new QPushButton(QString(kCrossGlyph), this);
|
||||
closeButton->setFixedSize(kSmallButtonSizePx, kSmallButtonSizePx);
|
||||
closeButton->setToolTip(tr("Close"));
|
||||
connect(closeButton, &QPushButton::clicked, this, &QDialog::reject);
|
||||
headerLayout->addWidget(closeButton);
|
||||
}
|
||||
|
||||
mainLayout->insertLayout(0, headerLayout);
|
||||
return headerLayout;
|
||||
}
|
||||
|
||||
void ModalDialog::keyPressEvent(QKeyEvent* event)
|
||||
{
|
||||
if (isDismissible() && isDismissKey(*event))
|
||||
{
|
||||
requestDismiss();
|
||||
return;
|
||||
}
|
||||
QDialog::keyPressEvent(event);
|
||||
}
|
||||
Reference in New Issue
Block a user