From c649cf883480071c9b93909c4ea4ee5ec98155b2 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 6 Jun 2014 14:49:47 +0300 Subject: Remove some duplicate code. --- src/actionmanager.cpp | 19 ++++++----- src/eventsmanager.cpp | 17 +++------- src/gui/popups/popupmenu.cpp | 51 ++++++++++++------------------ src/gui/popups/popupmenu.h | 2 ++ src/gui/widgets/dropshortcutcontainer.cpp | 22 ------------- src/gui/widgets/emoteshortcutcontainer.cpp | 28 ++-------------- src/gui/widgets/itemshortcutcontainer.cpp | 24 -------------- src/gui/widgets/shortcutcontainer.cpp | 30 ++++++++++++++++++ src/gui/widgets/spellshortcutcontainer.cpp | 23 -------------- src/itemsoundmanager.cpp | 18 ++++------- src/itemsoundmanager.h | 4 +++ src/render/sdl2graphics.cpp | 26 +++++++-------- src/render/sdl2softwaregraphics.cpp | 35 +++++++------------- src/sdlshared.h | 1 + 14 files changed, 106 insertions(+), 194 deletions(-) diff --git a/src/actionmanager.cpp b/src/actionmanager.cpp index 10b51cb41..5c0bf85fd 100644 --- a/src/actionmanager.cpp +++ b/src/actionmanager.cpp @@ -663,14 +663,19 @@ impHandler0(pickup) return false; } +static void doSit() +{ + if (inputManager.isActionActive(InputAction::EMOTE)) + player_node->updateSit(); + else + player_node->toggleSit(); +} + impHandler0(sit) { if (player_node) { - if (inputManager.isActionActive(InputAction::EMOTE)) - player_node->updateSit(); - else - player_node->toggleSit(); + doSit(); return true; } return false; @@ -1147,10 +1152,8 @@ impHandler0(stopSit) return false; if (!player_node->getTarget()) { - if (inputManager.isActionActive(InputAction::EMOTE)) - player_node->updateSit(); - else - player_node->toggleSit(); + doSit(); + return true; } return true; } diff --git a/src/eventsmanager.cpp b/src/eventsmanager.cpp index 73f7ef203..a87b1fd1d 100644 --- a/src/eventsmanager.cpp +++ b/src/eventsmanager.cpp @@ -23,11 +23,9 @@ #include "configuration.h" #include "client.h" #include "game.h" -#ifdef USE_SDL2 -#include "render/graphics.h" -#endif #include "logger.h" #include "mumblemanager.h" +#include "sdlshared.h" #include "settings.h" #include "gui/windowmanager.h" @@ -36,6 +34,10 @@ #include "input/inputmanager.h" +#ifdef USE_SDL2 +#include "render/graphics.h" +#endif + #include "utils/process.h" #include "debug.h" @@ -115,13 +117,8 @@ bool EventsManager::handleEvents() const else { SDL_Event event; - // Handle SDL events -#ifdef USE_SDL2 while (SDL_WaitEventTimeout(&event, 0)) -#else - while (SDL_PollEvent(&event)) -#endif { if (!handleCommonEvents(event)) { @@ -168,11 +165,7 @@ void EventsManager::handleGameEvents() const // Events SDL_Event event; -#ifdef USE_SDL2 while (SDL_WaitEventTimeout(&event, 0)) -#else - while (SDL_PollEvent(&event)) -#endif { if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) game->updateHistory(event); diff --git a/src/gui/popups/popupmenu.cpp b/src/gui/popups/popupmenu.cpp index 00cae3ee3..5d0f32f19 100644 --- a/src/gui/popups/popupmenu.cpp +++ b/src/gui/popups/popupmenu.cpp @@ -2521,6 +2521,25 @@ void PopupMenu::showPopup(int x, int y) requestMoveToTop(); } +void PopupMenu::addNormalRelations() +{ + // TRANSLATORS: popup menu item + // TRANSLATORS: add player to disregarded list + mBrowserBox->addRow("disregard", _("Disregard")); + // TRANSLATORS: popup menu item + // TRANSLATORS: add player to ignore list + mBrowserBox->addRow("ignore", _("Ignore")); + // TRANSLATORS: popup menu item + // TRANSLATORS: add player to black list + mBrowserBox->addRow("blacklist", _("Black list")); + // TRANSLATORS: popup menu item + // TRANSLATORS: add player to enemy list + mBrowserBox->addRow("enemy", _("Set as enemy")); + // TRANSLATORS: popup menu item + // TRANSLATORS: add player to erased list + mBrowserBox->addRow("erase", _("Erase")); +} + void PopupMenu::addPlayerRelation(const std::string &name) { switch (player_relations.getRelation(name)) @@ -2529,39 +2548,11 @@ void PopupMenu::addPlayerRelation(const std::string &name) // TRANSLATORS: popup menu item // TRANSLATORS: add player to friends list mBrowserBox->addRow("friend", _("Be friend")); - // TRANSLATORS: popup menu item - // TRANSLATORS: add player to disregarded list - mBrowserBox->addRow("disregard", _("Disregard")); - // TRANSLATORS: popup menu item - // TRANSLATORS: add player to ignore list - mBrowserBox->addRow("ignore", _("Ignore")); - // TRANSLATORS: popup menu item - // TRANSLATORS: add player to black list - mBrowserBox->addRow("blacklist", _("Black list")); - // TRANSLATORS: popup menu item - // TRANSLATORS: add player to enemy list - mBrowserBox->addRow("enemy", _("Set as enemy")); - // TRANSLATORS: popup menu item - // TRANSLATORS: add player to erased list - mBrowserBox->addRow("erase", _("Erase")); + addNormalRelations(); break; case PlayerRelation::FRIEND: - // TRANSLATORS: popup menu item - // TRANSLATORS: add player to disregarded list - mBrowserBox->addRow("disregard", _("Disregard")); - // TRANSLATORS: popup menu item - // TRANSLATORS: add player to ignore list - mBrowserBox->addRow("ignore", _("Ignore")); - // TRANSLATORS: popup menu item - // TRANSLATORS: add player to black list - mBrowserBox->addRow("blacklist", _("Black list")); - // TRANSLATORS: popup menu item - // TRANSLATORS: add player to enemy list - mBrowserBox->addRow("enemy", _("Set as enemy")); - // TRANSLATORS: popup menu item - // TRANSLATORS: add player to erased list - mBrowserBox->addRow("erase", _("Erase")); + addNormalRelations(); break; case PlayerRelation::BLACKLISTED: diff --git a/src/gui/popups/popupmenu.h b/src/gui/popups/popupmenu.h index 72c16ce50..69ca3c17b 100644 --- a/src/gui/popups/popupmenu.h +++ b/src/gui/popups/popupmenu.h @@ -170,6 +170,8 @@ class PopupMenu final : public Popup, public LinkHandler void addGmCommands(); + void addNormalRelations(); + void showGMPopup(); BrowserBox *mBrowserBox; diff --git a/src/gui/widgets/dropshortcutcontainer.cpp b/src/gui/widgets/dropshortcutcontainer.cpp index 087f4b372..f1434aa23 100644 --- a/src/gui/widgets/dropshortcutcontainer.cpp +++ b/src/gui/widgets/dropshortcutcontainer.cpp @@ -54,36 +54,14 @@ DropShortcutContainer::DropShortcutContainer(Widget2 *const widget) : { mItemPopup->postInit(); - addMouseListener(this); - addWidgetListener(this); - - mBackgroundImg = Theme::getImageFromThemeXml( - "item_shortcut_background.xml", "background.xml"); if (dropShortcut) mMaxItems = dropShortcut->getItemCount(); else mMaxItems = 0; - - if (mBackgroundImg) - { - mBackgroundImg->setAlpha(settings.guiAlpha); - mBoxHeight = mBackgroundImg->getHeight(); - mBoxWidth = mBackgroundImg->getWidth(); - } - else - { - mBoxHeight = 1; - mBoxWidth = 1; - } } DropShortcutContainer::~DropShortcutContainer() { - if (mBackgroundImg) - { - mBackgroundImg->decRef(); - mBackgroundImg = nullptr; - } delete2(mItemPopup); } diff --git a/src/gui/widgets/emoteshortcutcontainer.cpp b/src/gui/widgets/emoteshortcutcontainer.cpp index 85e07e2b7..0d802034e 100644 --- a/src/gui/widgets/emoteshortcutcontainer.cpp +++ b/src/gui/widgets/emoteshortcutcontainer.cpp @@ -39,6 +39,7 @@ #include "resources/db/emotedb.h" #include "utils/stringutils.h" +#include "utils/delete2.h" #include "debug.h" @@ -53,13 +54,6 @@ EmoteShortcutContainer::EmoteShortcutContainer(Widget2 *const widget) : { mEmotePopup->postInit(); - addMouseListener(this); - addWidgetListener(this); - - mForegroundColor2 = getThemeColor(Theme::TEXT_OUTLINE); - mBackgroundImg = Theme::getImageFromThemeXml( - "item_shortcut_background.xml", "background.xml"); - if (mBackgroundImg) mBackgroundImg->setAlpha(settings.guiAlpha); @@ -72,29 +66,11 @@ EmoteShortcutContainer::EmoteShortcutContainer(Widget2 *const widget) : } mMaxItems = MAX_ITEMS; - - if (mBackgroundImg) - { - mBoxHeight = mBackgroundImg->getHeight(); - mBoxWidth = mBackgroundImg->getWidth(); - } - else - { - mBoxHeight = 1; - mBoxWidth = 1; - } - mForegroundColor = getThemeColor(Theme::TEXT); } EmoteShortcutContainer::~EmoteShortcutContainer() { - delete mEmotePopup; - - if (mBackgroundImg) - { - mBackgroundImg->decRef(); - mBackgroundImg = nullptr; - } + delete2(mEmotePopup); } void EmoteShortcutContainer::setWidget2(const Widget2 *const widget) diff --git a/src/gui/widgets/itemshortcutcontainer.cpp b/src/gui/widgets/itemshortcutcontainer.cpp index 670807ed5..51a1cef13 100644 --- a/src/gui/widgets/itemshortcutcontainer.cpp +++ b/src/gui/widgets/itemshortcutcontainer.cpp @@ -68,38 +68,14 @@ ItemShortcutContainer::ItemShortcutContainer(Widget2 *const widget, mItemPopup->postInit(); mSpellPopup->postInit(); - addMouseListener(this); - addWidgetListener(this); - - mForegroundColor2 = getThemeColor(Theme::TEXT_OUTLINE); - mBackgroundImg = Theme::getImageFromThemeXml( - "item_shortcut_background.xml", "background.xml"); if (itemShortcut[mNumber]) mMaxItems = itemShortcut[mNumber]->getItemCount(); else mMaxItems = 0; - - if (mBackgroundImg) - { - mBackgroundImg->setAlpha(settings.guiAlpha); - mBoxHeight = mBackgroundImg->getHeight(); - mBoxWidth = mBackgroundImg->getWidth(); - } - else - { - mBoxHeight = 1; - mBoxWidth = 1; - } - mForegroundColor = getThemeColor(Theme::TEXT); } ItemShortcutContainer::~ItemShortcutContainer() { - if (mBackgroundImg) - { - mBackgroundImg->decRef(); - mBackgroundImg = nullptr; - } delete2(mItemPopup); delete2(mSpellPopup); } diff --git a/src/gui/widgets/shortcutcontainer.cpp b/src/gui/widgets/shortcutcontainer.cpp index e789c863d..313e37262 100644 --- a/src/gui/widgets/shortcutcontainer.cpp +++ b/src/gui/widgets/shortcutcontainer.cpp @@ -23,9 +23,12 @@ #include "gui/widgets/shortcutcontainer.h" #include "graphicsvertexes.h" +#include "settings.h" #include "gui/gui.h" +#include "resources/image.h" + #include "utils/delete2.h" #include "debug.h" @@ -45,10 +48,37 @@ ShortcutContainer::ShortcutContainer(Widget2 *const widget) : mVertexes(new ImageCollection) { mAllowLogic = false; + + addMouseListener(this); + addWidgetListener(this); + + mForegroundColor = getThemeColor(Theme::TEXT); + mForegroundColor2 = getThemeColor(Theme::TEXT_OUTLINE); + + mBackgroundImg = Theme::getImageFromThemeXml( + "item_shortcut_background.xml", "background.xml"); + + if (mBackgroundImg) + { + mBackgroundImg->setAlpha(settings.guiAlpha); + mBoxHeight = mBackgroundImg->getHeight(); + mBoxWidth = mBackgroundImg->getWidth(); + } + else + { + mBoxHeight = 1; + mBoxWidth = 1; + } } ShortcutContainer::~ShortcutContainer() { + if (mBackgroundImg) + { + mBackgroundImg->decRef(); + mBackgroundImg = nullptr; + } + if (gui) gui->removeDragged(this); diff --git a/src/gui/widgets/spellshortcutcontainer.cpp b/src/gui/widgets/spellshortcutcontainer.cpp index 014fa4354..08e22ee98 100644 --- a/src/gui/widgets/spellshortcutcontainer.cpp +++ b/src/gui/widgets/spellshortcutcontainer.cpp @@ -50,37 +50,14 @@ SpellShortcutContainer::SpellShortcutContainer(Widget2 *const widget, { mSpellPopup->postInit(); - addMouseListener(this); - addWidgetListener(this); - - mForegroundColor2 = getThemeColor(Theme::TEXT_OUTLINE); - - mBackgroundImg = Theme::getImageFromThemeXml( - "item_shortcut_background.xml", "background.xml"); if (spellShortcut) mMaxItems = spellShortcut->getSpellsCount(); else mMaxItems = 0; - - if (mBackgroundImg) - { - mBackgroundImg->setAlpha(settings.guiAlpha); - mBoxHeight = mBackgroundImg->getHeight(); - mBoxWidth = mBackgroundImg->getWidth(); - } - else - { - mBoxHeight = 1; - mBoxWidth = 1; - } - mForegroundColor = getThemeColor(Theme::TEXT); } SpellShortcutContainer::~SpellShortcutContainer() { - if (mBackgroundImg) - mBackgroundImg->decRef(); - mBackgroundImg = nullptr; delete2(mSpellPopup); } diff --git a/src/itemsoundmanager.cpp b/src/itemsoundmanager.cpp index 5206273a7..71fc015bf 100644 --- a/src/itemsoundmanager.cpp +++ b/src/itemsoundmanager.cpp @@ -41,16 +41,7 @@ void ItemSoundManager::playSfx(const Item *const item, void ItemSoundManager::playSfx(const int itemId, const ItemSoundEvent::Type sound) { - const ItemInfo &info = ItemDB::get(itemId); - std::string sfx = info.getSound(sound).sound; - if (sfx.empty()) - { - // fallback to player race sound if no item sound. - const int id = -100 - player_node->getSubType(); - const ItemInfo &info2 = ItemDB::get(id); - sfx = info2.getSound(sound).sound; - } - soundManager.playGuiSfx(sfx); + playSfx(ItemDB::get(itemId), sound); } void ItemSoundManager::playSfx(const FloorItem *const item, @@ -58,7 +49,12 @@ void ItemSoundManager::playSfx(const FloorItem *const item, { if (!item) return; - const ItemInfo &info = ItemDB::get(item->getItemId()); + playSfx(ItemDB::get(item->getItemId()), sound); +} + +void ItemSoundManager::playSfx(const ItemInfo &info, + const ItemSoundEvent::Type sound) +{ std::string sfx = info.getSound(sound).sound; if (sfx.empty()) { diff --git a/src/itemsoundmanager.h b/src/itemsoundmanager.h index 28037ca07..0383f5615 100644 --- a/src/itemsoundmanager.h +++ b/src/itemsoundmanager.h @@ -27,6 +27,7 @@ class FloorItem; class Item; +class ItemInfo; class ItemSoundManager final { @@ -41,6 +42,9 @@ class ItemSoundManager final static void playSfx(const FloorItem *const item, const ItemSoundEvent::Type sound); + + static void playSfx(const ItemInfo &info, + const ItemSoundEvent::Type sound); }; #endif // ITEMSOUNDMANAGER_H diff --git a/src/render/sdl2graphics.cpp b/src/render/sdl2graphics.cpp index 9c93c6e38..1acca0a6c 100644 --- a/src/render/sdl2graphics.cpp +++ b/src/render/sdl2graphics.cpp @@ -118,6 +118,15 @@ static int FakeSDL_RenderCopy(SDL_Renderer *const renderer, static_cast(mColor.b), \ static_cast(mColor.a)) +#define defRectFromArea(rect, area) \ + const SDL_Rect rect = \ + { \ + static_cast(area.x), \ + static_cast(area.y), \ + static_cast(area.width), \ + static_cast(area.height) \ + } + SDLGraphics::SDLGraphics() : Graphics(), mRendererFlags(SDL_RENDERER_SOFTWARE), @@ -662,13 +671,7 @@ bool SDLGraphics::pushClipArea(const Rect &area) const bool result = Graphics::pushClipArea(area); const ClipRect &carea = mClipStack.top(); - const SDL_Rect rect = - { - static_cast(carea.x), - static_cast(carea.y), - static_cast(carea.width), - static_cast(carea.height) - }; + defRectFromArea(rect, carea); SDL_RenderSetClipRect(mRenderer, &rect); return result; } @@ -681,14 +684,7 @@ void SDLGraphics::popClipArea() return; const ClipRect &carea = mClipStack.top(); - const SDL_Rect rect = - { - static_cast(carea.x), - static_cast(carea.y), - static_cast(carea.width), - static_cast(carea.height) - }; - + defRectFromArea(rect, carea); SDL_RenderSetClipRect(mRenderer, &rect); } diff --git a/src/render/sdl2softwaregraphics.cpp b/src/render/sdl2softwaregraphics.cpp index 8a5729027..49e450507 100644 --- a/src/render/sdl2softwaregraphics.cpp +++ b/src/render/sdl2softwaregraphics.cpp @@ -48,6 +48,15 @@ static unsigned int *cG = nullptr; static unsigned int *cB = nullptr; #endif +#define defRectFromArea(rect, area) \ + const SDL_Rect rect = \ + { \ + static_cast(area.x), \ + static_cast(area.y), \ + static_cast(area.width), \ + static_cast(area.height) \ + } + SDL2SoftwareGraphics::SDL2SoftwareGraphics() : Graphics(), mRendererFlags(SDL_RENDERER_SOFTWARE), @@ -1085,14 +1094,7 @@ void SDL2SoftwareGraphics::fillRectangle(const Rect &rectangle) } else { - SDL_Rect rect = - { - static_cast(area.x), - static_cast(area.y), - static_cast(area.width), - static_cast(area.height) - }; - + defRectFromArea(rect, area); const uint32_t color = SDL_MapRGBA(mSurface->format, static_cast(mColor.r), static_cast(mColor.g), @@ -1117,13 +1119,7 @@ bool SDL2SoftwareGraphics::pushClipArea(const Rect &area) const bool result = Graphics::pushClipArea(area); const ClipRect &carea = mClipStack.top(); - const SDL_Rect rect = - { - static_cast(carea.x), - static_cast(carea.y), - static_cast(carea.width), - static_cast(carea.height) - }; + defRectFromArea(rect, carea); SDL_SetClipRect(mSurface, &rect); return result; } @@ -1136,14 +1132,7 @@ void SDL2SoftwareGraphics::popClipArea() return; const ClipRect &carea = mClipStack.top(); - const SDL_Rect rect = - { - static_cast(carea.x), - static_cast(carea.y), - static_cast(carea.width), - static_cast(carea.height) - }; - + defRectFromArea(rect, carea); SDL_SetClipRect(mSurface, &rect); } diff --git a/src/sdlshared.h b/src/sdlshared.h index a011045bc..295cb29e7 100644 --- a/src/sdlshared.h +++ b/src/sdlshared.h @@ -59,6 +59,7 @@ #define SDL_Window SDL_Surface #define SDL_JoystickNameForIndex SDL_JoystickName #define SurfaceImageHelper SDLImageHelper +#define SDL_WaitEventTimeout(event, time) SDL_PollEvent(event) #endif // USE_SDL2 -- cgit v1.2.3-60-g2f50