diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-04-02 09:46:24 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-04-02 10:21:45 +0200 |
commit | 0f8e03229b5aaa1727c612a55180945c7608ad2d (patch) | |
tree | bd4a3b77af16cc0a0ecc904ff34b7e4cba8da161 /src/gui | |
parent | 71b39b1d1674406703cbdfd5f3297af72e92bfe9 (diff) | |
download | mana-0f8e03229b5aaa1727c612a55180945c7608ad2d.tar.gz mana-0f8e03229b5aaa1727c612a55180945c7608ad2d.tar.bz2 mana-0f8e03229b5aaa1727c612a55180945c7608ad2d.tar.xz mana-0f8e03229b5aaa1727c612a55180945c7608ad2d.zip |
General code cleanups
* Removed some unused includes and forward declarations.
* Use std::unique_ptr to automate cleanup.
* Use TextRenderer::renderText in BrowserBox to avoid code duplication.
* Removed unused STATE_NORMAL from StateFlags.
* Small layout fix in ServerDialog.
* Reduced rewrapping delay in BrowserBox to 33ms and disabled it
entirely when there are no more than 1000 lines to rewrap. The
rewrapping is usually fast enough.
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/charselectdialog.cpp | 2 | ||||
-rw-r--r-- | src/gui/chatwindow.cpp | 28 | ||||
-rw-r--r-- | src/gui/chatwindow.h | 16 | ||||
-rw-r--r-- | src/gui/customserverdialog.cpp | 47 | ||||
-rw-r--r-- | src/gui/customserverdialog.h | 20 | ||||
-rw-r--r-- | src/gui/inventorywindow.cpp | 2 | ||||
-rw-r--r-- | src/gui/outfitwindow.cpp | 26 | ||||
-rw-r--r-- | src/gui/outfitwindow.h | 4 | ||||
-rw-r--r-- | src/gui/quitdialog.cpp | 10 | ||||
-rw-r--r-- | src/gui/recorder.cpp | 15 | ||||
-rw-r--r-- | src/gui/register.cpp | 2 | ||||
-rw-r--r-- | src/gui/serverdialog.cpp | 14 | ||||
-rw-r--r-- | src/gui/serverdialog.h | 2 | ||||
-rw-r--r-- | src/gui/widgets/browserbox.cpp | 45 | ||||
-rw-r--r-- | src/gui/widgets/layout.h | 9 |
15 files changed, 76 insertions, 166 deletions
diff --git a/src/gui/charselectdialog.cpp b/src/gui/charselectdialog.cpp index 7792813c..eb649c58 100644 --- a/src/gui/charselectdialog.cpp +++ b/src/gui/charselectdialog.cpp @@ -150,7 +150,7 @@ CharSelectDialog::CharSelectDialog(LoginData *loginData): for (int i = 0; i < (int)mLoginData->characterSlots; i++) { mCharacterEntries.push_back(new CharacterDisplay(this)); - place(i % SLOTS_PER_ROW, (int)i / SLOTS_PER_ROW, mCharacterEntries[i]); + place(i % SLOTS_PER_ROW, i / SLOTS_PER_ROW, mCharacterEntries[i]); } reflowLayout(); diff --git a/src/gui/chatwindow.cpp b/src/gui/chatwindow.cpp index 453baea2..8a34c961 100644 --- a/src/gui/chatwindow.cpp +++ b/src/gui/chatwindow.cpp @@ -76,18 +76,17 @@ class ChatAutoComplete : public AutoCompleteLister { void getAutoCompleteList(std::vector<std::string> &list) const override { - auto *tab = static_cast<ChatTab*>(chatWindow->mChatTabs - ->getSelectedTab()); - - return tab->getAutoCompleteList(list); + auto tab = static_cast<ChatTab *>(chatWindow->mChatTabs->getSelectedTab()); + tab->getAutoCompleteList(list); } }; ChatWindow::ChatWindow(): Window(_("Chat")), - mHistory(new TextHistory()), + mItemLinkHandler(new ItemLinkHandler(this)), + mChatInput(new ChatInput), mAutoComplete(new ChatAutoComplete), - mTmpVisible(false) + mChatTabs(new TabbedArea) { listen(Event::ChatChannel); listen(Event::NoticesChannel); @@ -106,21 +105,16 @@ ChatWindow::ChatWindow(): setMinWidth(150); setMinHeight(90); - mItemLinkHandler = new ItemLinkHandler(this); - - mChatInput = new ChatInput; mChatInput->setActionEventId("chatinput"); mChatInput->addActionListener(this); - mChatTabs = new TabbedArea; - getLayout().setPadding(3); place(0, 0, mChatTabs, 3, 3); place(0, 3, mChatInput, 3); loadWindowState(); - mChatInput->setHistory(mHistory); + mChatInput->setHistory(&mHistory); mChatInput->setAutoComplete(mAutoComplete); mRecorder = new Recorder(this); @@ -131,7 +125,6 @@ ChatWindow::~ChatWindow() delete mRecorder; removeAllWhispers(); delete mItemLinkHandler; - delete mHistory; delete mAutoComplete; } @@ -146,15 +139,10 @@ ChatTab *ChatWindow::getFocused() const return static_cast<ChatTab*>(mChatTabs->getSelectedTab()); } -void ChatWindow::clearTab(ChatTab *tab) -{ - if (tab) - tab->clearText(); -} - void ChatWindow::clearTab() { - clearTab(getFocused()); + if (auto tab = getFocused()) + tab->clearText(); } void ChatWindow::prevTab() diff --git a/src/gui/chatwindow.h b/src/gui/chatwindow.h index f47b7140..6cac6f8e 100644 --- a/src/gui/chatwindow.h +++ b/src/gui/chatwindow.h @@ -31,21 +31,14 @@ #include <guichan/widget.hpp> #include <guichan/widgetlistener.hpp> -#include <list> #include <string> #include <map> -#include <vector> -class BrowserBox; class ChatTab; -class Channel; class ChatInput; class Recorder; -class ScrollArea; class TabbedArea; class ItemLinkHandler; -class Tab; -class WhisperTab; #define DEFAULT_CHAT_WINDOW_SCROLL 7 // 1 means `1/8th of the window size'. @@ -98,11 +91,6 @@ class ChatWindow : public Window, ChatTab *getFocused() const; /** - * Clear the given tab. - */ - void clearTab(ChatTab *tab); - - /** * Clear the current tab. */ void clearTab(); @@ -201,11 +189,11 @@ class ChatWindow : public Window, /** Input box for typing chat messages. */ ChatInput *mChatInput; - TextHistory *mHistory; + TextHistory mHistory; AutoCompleteLister *mAutoComplete; private: - bool mTmpVisible; + bool mTmpVisible = false; /** Tabbed area for holding each channel. */ TabbedArea *mChatTabs; diff --git a/src/gui/customserverdialog.cpp b/src/gui/customserverdialog.cpp index f58e51ad..5ed3a445 100644 --- a/src/gui/customserverdialog.cpp +++ b/src/gui/customserverdialog.cpp @@ -37,11 +37,10 @@ std::string TypeListModel::getElementAt(int elementIndex) if (elementIndex == 0) return "TmwAthena"; #ifdef MANASERV_SUPPORT - else if (elementIndex == 1) + if (elementIndex == 1) return "ManaServ"; #endif - else - return "Unknown"; + return "Unknown"; } CustomServerDialog::CustomServerDialog(ServerDialog *parent, int index): @@ -62,8 +61,8 @@ CustomServerDialog::CustomServerDialog(ServerDialog *parent, int index): mPortField = new TextField(std::string()); #ifdef MANASERV_SUPPORT - mTypeListModel = new TypeListModel(); - mTypeField = new DropDown(mTypeListModel); + mTypeListModel = std::make_unique<TypeListModel>(); + mTypeField = new DropDown(mTypeListModel.get()); mTypeField->setSelected(0); // TmwAthena by default for now. #endif @@ -91,32 +90,11 @@ CustomServerDialog::CustomServerDialog(ServerDialog *parent, int index): place(4, 5, mOkButton); place(3, 5, mCancelButton); - // Do this manually instead of calling reflowLayout so we can enforce a - // minimum width. - int width = 0, height = 0; - getLayout().reflow(width, height); - if (width < 300) - { - width = 300; - getLayout().reflow(width, height); - } - if (height < 120) - { - height = 120; - getLayout().reflow(width, height); - } - - setContentSize(width, height); - - setMinWidth(getWidth()); - setMinHeight(getHeight()); - setDefaultSize(getWidth(), getHeight(), ImageRect::CENTER); + reflowLayout(); + setLocationRelativeTo(getParentWindow()); - setResizable(false); addKeyListener(this); - loadWindowState(); - // Add the entry's info when in modify mode. if (index > -1) { @@ -131,23 +109,12 @@ CustomServerDialog::CustomServerDialog(ServerDialog *parent, int index): #endif } - setLocationRelativeTo(getParentWindow()); setVisible(true); mNameField->requestFocus(); } -CustomServerDialog::~CustomServerDialog() -{ -#ifdef MANASERV_SUPPORT - delete mTypeListModel; -#endif -} - -void CustomServerDialog::logic() -{ - Window::logic(); -} +CustomServerDialog::~CustomServerDialog() = default; void CustomServerDialog::action(const gcn::ActionEvent &event) { diff --git a/src/gui/customserverdialog.h b/src/gui/customserverdialog.h index c17849d5..e523260c 100644 --- a/src/gui/customserverdialog.h +++ b/src/gui/customserverdialog.h @@ -20,19 +20,18 @@ #pragma once -class Button; -class Label; -class TextField; -class DropDown; -class ServerDialog; -class TypeListModel; - #include "gui/widgets/window.h" #include <guichan/actionlistener.hpp> #include <guichan/keylistener.hpp> #include <guichan/listmodel.hpp> +#include <memory> + +class Button; +class DropDown; +class ServerDialog; +class TextField; /** * Server Type List Model @@ -64,7 +63,6 @@ class CustomServerDialog : public Window, { public: CustomServerDialog(ServerDialog *parent, int index = -1); - ~CustomServerDialog() override; /** @@ -74,18 +72,16 @@ class CustomServerDialog : public Window, void keyPressed(gcn::KeyEvent &keyEvent) override; - void logic() override; - private: TextField *mServerAddressField; TextField *mPortField; - TextField *mNameField; + TextField *mNameField; TextField *mDescriptionField; Button *mOkButton; Button *mCancelButton; #ifdef MANASERV_SUPPORT DropDown *mTypeField; - TypeListModel *mTypeListModel; + std::unique_ptr<TypeListModel> mTypeListModel; #endif ServerDialog *mServerDialog; // The index of the entry to modify, -1 when only adding a new entry. diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp index 73a2522a..ab2e9c86 100644 --- a/src/gui/inventorywindow.cpp +++ b/src/gui/inventorywindow.cpp @@ -76,7 +76,7 @@ InventoryWindow::InventoryWindow(Inventory *inventory): mItems = new ItemContainer(mInventory); mItems->addSelectionListener(this); - gcn::ScrollArea *invenScroll = new ScrollArea(mItems); + auto invenScroll = new ScrollArea(mItems); invenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); mSlotsLabel = new Label(_("Slots:")); diff --git a/src/gui/outfitwindow.cpp b/src/gui/outfitwindow.cpp index b5033d70..f914c0cc 100644 --- a/src/gui/outfitwindow.cpp +++ b/src/gui/outfitwindow.cpp @@ -78,8 +78,8 @@ OutfitWindow::~OutfitWindow() void OutfitWindow::load() { - for (int o = 0; o < OUTFITS_COUNT; o++) - memset(mOutfits[o].items, -1, sizeof(mOutfits[o].items)); + for (auto &mOutfit : mOutfits) + memset(mOutfit.items, -1, sizeof(mOutfit.items)); for (auto &outfit : config.outfits) { @@ -89,10 +89,8 @@ void OutfitWindow::load() std::string buf; std::stringstream ss(outfit.items); - for (size_t i = 0; (ss >> buf) && i < OUTFIT_ITEM_COUNT; i++) - { + for (int i = 0; (ss >> buf) && i < OUTFIT_ITEM_COUNT; i++) mOutfits[outfit.index].items[i] = atoi(buf.c_str()); - } mOutfits[outfit.index].unequip = outfit.unequip; } @@ -105,16 +103,15 @@ void OutfitWindow::save() std::string outfitStr; for (int o = 0; o < OUTFITS_COUNT; o++) { - auto &items = mOutfits[o].items; bool emptyOutfit = true; - for (int i = 0; i < OUTFIT_ITEM_COUNT; i++) + for (int item : mOutfits[o].items) { if (!outfitStr.empty()) outfitStr += " "; - outfitStr += items[i] ? toString(items[i]) : toString(-1); - emptyOutfit &= items[i] <= 0; + outfitStr += item ? toString(item) : toString(-1); + emptyOutfit &= item <= 0; } if (!emptyOutfit) @@ -159,10 +156,9 @@ void OutfitWindow::wearOutfit(int outfit) if (mOutfits[outfit].unequip) unequipNotInOutfit(outfit); - Item *item; - for (int i = 0; i < OUTFIT_ITEM_COUNT; i++) + for (int i : mOutfits[outfit].items) { - item = PlayerInfo::getInventory()->findItem(mOutfits[outfit].items[i]); + Item *item = PlayerInfo::getInventory()->findItem(i); if (item && !item->isEquipped() && item->getQuantity()) { if (item->isEquippable()) @@ -174,9 +170,7 @@ void OutfitWindow::wearOutfit(int outfit) void OutfitWindow::copyOutfit(int outfit) { for (int i = 0; i < OUTFIT_ITEM_COUNT; i++) - { mOutfits[mCurrentOutfit].items[i] = mOutfits[outfit].items[i]; - } } void OutfitWindow::draw(gcn::Graphics *graphics) @@ -328,9 +322,9 @@ void OutfitWindow::unequipNotInOutfit(int outfit) if (inventory->getItem(i) && inventory->getItem(i)->isEquipped()) { bool found = false; - for (int f = 0; f < OUTFIT_ITEM_COUNT; f++) + for (int item : mOutfits[outfit].items) { - if (inventory->getItem(i)->getId() == mOutfits[outfit].items[f]) + if (inventory->getItem(i)->getId() == item) { found = true; break; diff --git a/src/gui/outfitwindow.h b/src/gui/outfitwindow.h index 661d9b1a..10de5321 100644 --- a/src/gui/outfitwindow.h +++ b/src/gui/outfitwindow.h @@ -25,8 +25,8 @@ #include <guichan/actionlistener.hpp> -#define OUTFITS_COUNT 15 -#define OUTFIT_ITEM_COUNT 9 +constexpr int OUTFITS_COUNT = 15; +constexpr int OUTFIT_ITEM_COUNT = 9; class Button; class CheckBox; diff --git a/src/gui/quitdialog.cpp b/src/gui/quitdialog.cpp index 4afbd419..51831c4c 100644 --- a/src/gui/quitdialog.cpp +++ b/src/gui/quitdialog.cpp @@ -71,7 +71,8 @@ QuitDialog::QuitDialog(QuitDialog** pointerToMe): placeOption(place, mSwitchAccountServer); // Only added if we are connected to a gameserver - if (state == STATE_GAME) placeOption(place, mSwitchCharacter); + if (state == STATE_GAME) + placeOption(place, mSwitchCharacter); } mOptions[0]->setSelected(true); @@ -90,7 +91,9 @@ QuitDialog::QuitDialog(QuitDialog** pointerToMe): QuitDialog::~QuitDialog() { - if (mMyPointer) *mMyPointer = nullptr; + if (mMyPointer) + *mMyPointer = nullptr; + // Optional widgets, so delete them by hand. delete mForceQuit; delete mLogoutQuit; @@ -166,7 +169,8 @@ void QuitDialog::keyPressed(gcn::KeyEvent &keyEvent) mOptions[0]->setSelected(true); return; } - else if (it == mOptions.begin() && dir < 0) + + if (it == mOptions.begin() && dir < 0) it = mOptions.end(); it += dir; diff --git a/src/gui/recorder.cpp b/src/gui/recorder.cpp index 8dd0f8ed..894e3631 100644 --- a/src/gui/recorder.cpp +++ b/src/gui/recorder.cpp @@ -32,15 +32,16 @@ #include "utils/gettext.h" #include "utils/stringutils.h" -Recorder::Recorder(ChatWindow *chat, const std::string &title, - const std::string &buttonTxt) : - Window(title) +Recorder::Recorder(ChatWindow *chat, + const std::string &title, + const std::string &buttonTxt) + : Window(title) + , mChat(chat) { setWindowName("Recorder"); const int offsetX = 2 * getPadding() + 10; const int offsetY = getTitleBarHeight() + getPadding() + 10; - mChat = chat; auto *button = new Button(buttonTxt, "activate", this); // 123 is the default chat window height. If you change this in Chat, please @@ -56,16 +57,12 @@ Recorder::Recorder(ChatWindow *chat, const std::string &title, loadWindowState(); } -Recorder::~Recorder() -{ -} +Recorder::~Recorder() = default; void Recorder::record(const std::string &msg) { if (mStream.is_open()) - { mStream << msg << std::endl; - } } void Recorder::setRecordingFile(const std::string &msg) diff --git a/src/gui/register.cpp b/src/gui/register.cpp index d4ebb59c..685e4898 100644 --- a/src/gui/register.cpp +++ b/src/gui/register.cpp @@ -22,14 +22,12 @@ #include "gui/register.h" #include "client.h" -#include "configuration.h" #include "log.h" #include "gui/logindialog.h" #include "gui/okdialog.h" #include "gui/widgets/button.h" -#include "gui/widgets/checkbox.h" #include "gui/widgets/label.h" #include "gui/widgets/layout.h" #include "gui/widgets/passwordfield.h" diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index 6bf5a9d5..fa4087ce 100644 --- a/src/gui/serverdialog.cpp +++ b/src/gui/serverdialog.cpp @@ -36,7 +36,6 @@ #include "gui/widgets/layout.h" #include "gui/widgets/listbox.h" #include "gui/widgets/scrollarea.h" -#include "gui/widgets/textfield.h" #include "resources/theme.h" @@ -156,8 +155,8 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): loadCustomServers(); - mServersListModel = new ServersListModel(&mServers, this); - mServersList = new ServersListBox(mServersListModel); + mServersListModel = std::make_unique<ServersListModel>(&mServers, this); + mServersList = new ServersListBox(mServersListModel.get()); auto *usedScroll = new ScrollArea(mServersList); usedScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); @@ -176,8 +175,8 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): usedScroll->setVerticalScrollAmount(0); place(0, 0, usedScroll, 6, 5).setPadding(3); - place(0, 5, mDescription, 5); - place(0, 6, mDownloadText, 5); + place(0, 5, mDescription, 6); + place(0, 6, mDownloadText, 6); place(0, 7, mManualEntryButton); place(1, 7, mModifyButton); place(2, 7, mDeleteButton); @@ -217,10 +216,7 @@ ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): downloadServerList(); } -ServerDialog::~ServerDialog() -{ - delete mServersListModel; -} +ServerDialog::~ServerDialog() = default; void ServerDialog::action(const gcn::ActionEvent &event) { diff --git a/src/gui/serverdialog.h b/src/gui/serverdialog.h index e0d74006..cc2725be 100644 --- a/src/gui/serverdialog.h +++ b/src/gui/serverdialog.h @@ -136,7 +136,7 @@ class ServerDialog : public Window, Button *mDeleteButton; ListBox *mServersList; - ServersListModel *mServersListModel; + std::unique_ptr<ServersListModel> mServersListModel; const std::string &mDir; diff --git a/src/gui/widgets/browserbox.cpp b/src/gui/widgets/browserbox.cpp index fd777133..ef40d8c4 100644 --- a/src/gui/widgets/browserbox.cpp +++ b/src/gui/widgets/browserbox.cpp @@ -22,11 +22,13 @@ #include "gui/widgets/browserbox.h" +#include "keyboardconfig.h" +#include "textrenderer.h" + #include "gui/gui.h" #include "gui/truetypefont.h" #include "gui/widgets/linkhandler.h" -#include "keyboardconfig.h" #include "resources/itemdb.h" #include "resources/iteminfo.h" #include "resources/theme.h" @@ -279,34 +281,15 @@ void BrowserBox::draw(gcn::Graphics *graphics) if (part.y > yEnd) return; - auto font = part.font; - - // Handle text shadows - if (mShadows) - { - graphics->setColor(Theme::getThemeColor(Theme::SHADOW, - part.color.a / 2)); - - if (mOutline) - font->drawString(graphics, part.text, part.x + 2, part.y + 2); - else - font->drawString(graphics, part.text, part.x + 1, part.y + 1); - } - - if (mOutline) - { - // Text outline - graphics->setColor(Theme::getThemeColor(Theme::OUTLINE, - part.color.a / 4)); - font->drawString(graphics, part.text, part.x + 1, part.y); - font->drawString(graphics, part.text, part.x - 1, part.y); - font->drawString(graphics, part.text, part.x, part.y + 1); - font->drawString(graphics, part.text, part.x, part.y - 1); - } - - // the main text - graphics->setColor(part.color); - font->drawString(graphics, part.text, part.x, part.y); + TextRenderer::renderText(graphics, + part.text, + part.x, + part.y, + Graphics::LEFT, + part.color, + part.font, + mOutline, + mShadows); } } } @@ -322,7 +305,7 @@ void BrowserBox::relayoutText() layoutTextRow(row, context); mLastLayoutWidth = getWidth(); - mLayoutTimer.set(100); + mLayoutTimer.set(33); setHeight(context.y); } @@ -552,7 +535,7 @@ void BrowserBox::updateHoveredLink(int x, int y) void BrowserBox::maybeRelayoutText() { // Reduce relayouting frequency when there is a lot of text - if (mTextRows.size() > 100) + if (mTextRows.size() > 1000) if (!mLayoutTimer.passed()) return; diff --git a/src/gui/widgets/layout.h b/src/gui/widgets/layout.h index c7652334..42f08758 100644 --- a/src/gui/widgets/layout.h +++ b/src/gui/widgets/layout.h @@ -167,9 +167,12 @@ class LayoutCell }; LayoutCell() = default; - ~LayoutCell(); + // Copy not allowed, as the cell may own an array. + LayoutCell(LayoutCell const &) = delete; + LayoutCell &operator=(LayoutCell const &) = delete; + /** * Sets the padding around the cell content. */ @@ -231,10 +234,6 @@ class LayoutCell void computeSizes(); private: - // Copy not allowed, as the cell may own an array. - LayoutCell(LayoutCell const &); - LayoutCell &operator=(LayoutCell const &); - union { gcn::Widget *mWidget; |