Refactor Dialog class containing fonts:

- the previous code has a _font pointer in derived classes, and a _font reference in the base class; this causes name collisions
- the Dialog class now takes a font reference, which is used throughout the hierarchy
- this simplifies certain code (setTitle vs. initTitle) and also eliminates clang warnings
This commit is contained in:
Stephen Anthony 2018-02-06 19:26:49 -03:30
parent 3837921073
commit e01321fbea
6 changed files with 42 additions and 76 deletions

View File

@ -27,13 +27,10 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CommandDialog::CommandDialog(OSystem& osystem, DialogContainer& parent) CommandDialog::CommandDialog(OSystem& osystem, DialogContainer& parent)
: Dialog(osystem, parent) : Dialog(osystem, parent, osystem.frameBuffer().font(), "Commands")
{ {
const GUI::Font& font = instance().frameBuffer().font(); const int buttonWidth = _font.getStringWidth("Right Diff B") + 20,
initTitle(font, "Commands"); buttonHeight = _font.getLineHeight() + 6,
const int buttonWidth = font.getStringWidth("Right Diff B") + 20,
buttonHeight = font.getLineHeight() + 6,
rowHeight = buttonHeight + 8; rowHeight = buttonHeight + 8;
// Set real dimensions // Set real dimensions
@ -46,7 +43,7 @@ CommandDialog::CommandDialog(OSystem& osystem, DialogContainer& parent)
auto ADD_CD_BUTTON = [&](const string& label, int cmd) auto ADD_CD_BUTTON = [&](const string& label, int cmd)
{ {
ButtonWidget* bw = new ButtonWidget(this, font, xoffset, yoffset, ButtonWidget* bw = new ButtonWidget(this, _font, xoffset, yoffset,
buttonWidth, buttonHeight, label, cmd); buttonWidth, buttonHeight, label, cmd);
xoffset += buttonWidth + 8; xoffset += buttonWidth + 8;
return bw; return bw;

View File

@ -28,7 +28,7 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ContextMenu::ContextMenu(GuiObject* boss, const GUI::Font& font, ContextMenu::ContextMenu(GuiObject* boss, const GUI::Font& font,
const VariantList& items, int cmd, int width) const VariantList& items, int cmd, int width)
: Dialog(boss->instance(), boss->parent()), : Dialog(boss->instance(), boss->parent(), font),
CommandSender(boss), CommandSender(boss),
_rowHeight(font.getLineHeight()), _rowHeight(font.getLineHeight()),
_firstEntry(0), _firstEntry(0),
@ -39,7 +39,6 @@ ContextMenu::ContextMenu(GuiObject* boss, const GUI::Font& font,
_isScrolling(false), _isScrolling(false),
_scrollUpColor(kColor), _scrollUpColor(kColor),
_scrollDnColor(kColor), _scrollDnColor(kColor),
_font(font),
_cmd(cmd), _cmd(cmd),
_xorig(0), _xorig(0),
_yorig(0), _yorig(0),

View File

@ -120,7 +120,6 @@ class ContextMenu : public Dialog, public CommandSender
bool _isScrolling; bool _isScrolling;
uInt32 _scrollUpColor, _scrollDnColor; uInt32 _scrollUpColor, _scrollDnColor;
const GUI::Font& _font;
int _cmd; int _cmd;
uInt32 _xorig, _yorig; uInt32 _xorig, _yorig;

View File

@ -44,10 +44,10 @@
* ... * ...
*/ */
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Dialog::Dialog(OSystem& instance, DialogContainer& parent, const GUI::Font& font,
Dialog::Dialog(OSystem& instance, DialogContainer& parent, const GUI::Font& font, const string& title, const string& title, int x, int y, int w, int h)
int x, int y, int w, int h)
: GuiObject(instance, parent, *this, x, y, w, h), : GuiObject(instance, parent, *this, x, y, w, h),
_font(font),
_mouseWidget(nullptr), _mouseWidget(nullptr),
_focusedWidget(nullptr), _focusedWidget(nullptr),
_dragWidget(nullptr), _dragWidget(nullptr),
@ -57,31 +57,17 @@ Dialog::Dialog(OSystem& instance, DialogContainer& parent, const GUI::Font& font
_processCancel(false), _processCancel(false),
_title(title), _title(title),
_th(0), _th(0),
_font(&font),
_surface(nullptr), _surface(nullptr),
_tabID(0), _tabID(0),
_flags(WIDGET_ENABLED | WIDGET_BORDER | WIDGET_CLEARBG) _flags(WIDGET_ENABLED | WIDGET_BORDER | WIDGET_CLEARBG)
{ {
initTitle(font, title); setTitle(title);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Dialog::Dialog(OSystem& instance, DialogContainer& parent, Dialog::Dialog(OSystem& instance, DialogContainer& parent,
int x, int y, int w, int h) int x, int y, int w, int h)
: GuiObject(instance, parent, *this, x, y, w, h), : Dialog(instance, parent, instance.frameBuffer().font(), "", x, y, w, h)
_mouseWidget(nullptr),
_focusedWidget(nullptr),
_dragWidget(nullptr),
_okWidget(nullptr),
_cancelWidget(nullptr),
_visible(false),
_processCancel(false),
_title(""),
_th(0),
_font(nullptr),
_fh(0),
_surface(nullptr),
_tabID(0),
_flags(WIDGET_ENABLED | WIDGET_BORDER | WIDGET_CLEARBG)
{ {
} }
@ -133,27 +119,16 @@ void Dialog::close(bool refresh)
parent().removeDialog(); parent().removeDialog();
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Dialog::initTitle(const GUI::Font& font, const string& title)
{
_font = &font;
_fh = font.getLineHeight();
setTitle(title);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Dialog::setTitle(const string& title) void Dialog::setTitle(const string& title)
{ {
if(_font != nullptr)
{
_title = title; _title = title;
_h -= _th; _h -= _th;
if(title.empty()) if(title.empty())
_th = 0; _th = 0;
else else
_th = _fh + 4; _th = _font.getLineHeight() + 4;
_h += _th; _h += _th;
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -336,7 +311,7 @@ void Dialog::drawDialog()
if(_th) if(_th)
{ {
s.fillRect(_x, _y, _w, _th, onTop ? kColorTitleBar : kColorTitleBarLo); s.fillRect(_x, _y, _w, _th, onTop ? kColorTitleBar : kColorTitleBarLo);
s.drawString(*_font, _title, _x + 10, _y + 2 + 1, _font->getStringWidth(_title), s.drawString(_font, _title, _x + 10, _y + 2 + 1, _font.getStringWidth(_title),
onTop ? kColorTitleText : kColorTitleTextLo); onTop ? kColorTitleText : kColorTitleTextLo);
} }
} }

View File

@ -46,8 +46,8 @@ class Dialog : public GuiObject
public: public:
Dialog(OSystem& instance, DialogContainer& parent, Dialog(OSystem& instance, DialogContainer& parent,
int x = 0, int y = 0, int w = 0, int h = 0); int x = 0, int y = 0, int w = 0, int h = 0);
Dialog(OSystem& instance, DialogContainer& parent, const GUI::Font& font, const string& title, Dialog(OSystem& instance, DialogContainer& parent, const GUI::Font& font,
int x = 0, int y = 0, int w = 0, int h = 0); const string& title = "", int x = 0, int y = 0, int w = 0, int h = 0);
virtual ~Dialog(); virtual ~Dialog();
@ -129,8 +129,6 @@ class Dialog : public GuiObject
void processCancelWithoutWidget(bool state) { _processCancel = state; } void processCancelWithoutWidget(bool state) { _processCancel = state; }
void initTitle(const GUI::Font& font, const string& title);
private: private:
void buildCurrentFocusList(int tabID = -1); void buildCurrentFocusList(int tabID = -1);
bool handleNavEvent(Event::Type e); bool handleNavEvent(Event::Type e);
@ -138,18 +136,19 @@ class Dialog : public GuiObject
bool cycleTab(int direction); bool cycleTab(int direction);
protected: protected:
const GUI::Font& _font;
Widget* _mouseWidget; Widget* _mouseWidget;
Widget* _focusedWidget; Widget* _focusedWidget;
Widget* _dragWidget; Widget* _dragWidget;
Widget* _defaultWidget; Widget* _defaultWidget;
Widget* _okWidget; Widget* _okWidget;
Widget* _cancelWidget; Widget* _cancelWidget;
bool _visible; bool _visible;
bool _processCancel; bool _processCancel;
string _title; string _title;
int _th; int _th;
const GUI::Font* _font;
int _fh;
Common::FixedStack<shared_ptr<FBSurface>> mySurfaceStack; Common::FixedStack<shared_ptr<FBSurface>> mySurfaceStack;

View File

@ -48,16 +48,13 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
OptionsDialog::OptionsDialog(OSystem& osystem, DialogContainer& parent, OptionsDialog::OptionsDialog(OSystem& osystem, DialogContainer& parent,
GuiObject* boss, int max_w, int max_h, stellaMode mode) GuiObject* boss, int max_w, int max_h, stellaMode mode)
: Dialog(osystem, parent), : Dialog(osystem, parent, osystem.frameBuffer().font(), "Options"),
myMode(mode), myMode(mode),
_boss(boss) _boss(boss)
{ {
const GUI::Font& font = instance().frameBuffer().font(); const int buttonWidth = _font.getStringWidth("Game Properties" + ELLIPSIS) + 20,
initTitle(font, "Options"); buttonHeight = _font.getLineHeight() + 6,
rowHeight = _font.getLineHeight() + 10;
const int buttonWidth = font.getStringWidth("Game Properties" + ELLIPSIS) + 20,
buttonHeight = font.getLineHeight() + 6,
rowHeight = font.getLineHeight() + 10;
const int VBORDER = 10 + _th; const int VBORDER = 10 + _th;
_w = 2 * buttonWidth + 30; _w = 2 * buttonWidth + 30;
@ -69,7 +66,7 @@ OptionsDialog::OptionsDialog(OSystem& osystem, DialogContainer& parent,
auto ADD_OD_BUTTON = [&](const string& label, int cmd) auto ADD_OD_BUTTON = [&](const string& label, int cmd)
{ {
ButtonWidget* bw = new ButtonWidget(this, font, xoffset, yoffset, ButtonWidget* bw = new ButtonWidget(this, _font, xoffset, yoffset,
buttonWidth, buttonHeight, label, cmd); buttonWidth, buttonHeight, label, cmd);
yoffset += rowHeight; yoffset += rowHeight;
return bw; return bw;
@ -128,21 +125,21 @@ OptionsDialog::OptionsDialog(OSystem& osystem, DialogContainer& parent,
addCancelWidget(b); addCancelWidget(b);
// Now create all the dialogs attached to each menu button // Now create all the dialogs attached to each menu button
myVideoDialog = make_unique<VideoDialog>(osystem, parent, font, max_w, max_h); myVideoDialog = make_unique<VideoDialog>(osystem, parent, _font, max_w, max_h);
myAudioDialog = make_unique<AudioDialog>(osystem, parent, font); myAudioDialog = make_unique<AudioDialog>(osystem, parent, _font);
myInputDialog = make_unique<InputDialog>(osystem, parent, font, max_w, max_h); myInputDialog = make_unique<InputDialog>(osystem, parent, _font, max_w, max_h);
myUIDialog = make_unique<UIDialog>(osystem, parent, font); myUIDialog = make_unique<UIDialog>(osystem, parent, _font);
mySnapshotDialog = make_unique<SnapshotDialog>(osystem, parent, font, max_w, max_h); mySnapshotDialog = make_unique<SnapshotDialog>(osystem, parent, _font, max_w, max_h);
myConfigPathDialog = make_unique<ConfigPathDialog>(osystem, parent, font, boss, max_w, max_h); myConfigPathDialog = make_unique<ConfigPathDialog>(osystem, parent, _font, boss, max_w, max_h);
myRomAuditDialog = make_unique<RomAuditDialog>(osystem, parent, font, max_w, max_h); myRomAuditDialog = make_unique<RomAuditDialog>(osystem, parent, _font, max_w, max_h);
myGameInfoDialog = make_unique<GameInfoDialog>(osystem, parent, font, this); myGameInfoDialog = make_unique<GameInfoDialog>(osystem, parent, _font, this);
#ifdef CHEATCODE_SUPPORT #ifdef CHEATCODE_SUPPORT
myCheatCodeDialog = make_unique<CheatCodeDialog>(osystem, parent, font); myCheatCodeDialog = make_unique<CheatCodeDialog>(osystem, parent, _font);
#endif #endif
myLoggerDialog = make_unique<LoggerDialog>(osystem, parent, font, max_w, max_h); myLoggerDialog = make_unique<LoggerDialog>(osystem, parent, _font, max_w, max_h);
myDeveloperDialog = make_unique<DeveloperDialog>(osystem, parent, font, max_w, max_h); myDeveloperDialog = make_unique<DeveloperDialog>(osystem, parent, _font, max_w, max_h);
myHelpDialog = make_unique<HelpDialog>(osystem, parent, font); myHelpDialog = make_unique<HelpDialog>(osystem, parent, _font);
myAboutDialog = make_unique<AboutDialog>(osystem, parent, font); myAboutDialog = make_unique<AboutDialog>(osystem, parent, _font);
addToFocusList(wid); addToFocusList(wid);