From 9569e469a481076bdbc330377bf8445eb32fbaa6 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 25 May 2015 18:07:58 +0300 Subject: Add safeDraw method into shortcutcontainer childs. --- src/gui/widgets/emoteshortcutcontainer.cpp | 52 +++++++++++ src/gui/widgets/emoteshortcutcontainer.h | 2 + src/gui/widgets/itemshortcutcontainer.cpp | 137 +++++++++++++++++++++++++++++ src/gui/widgets/itemshortcutcontainer.h | 2 + src/gui/widgets/shortcutcontainer.cpp | 37 ++++---- src/gui/widgets/shortcutcontainer.h | 2 + src/gui/widgets/spellshortcutcontainer.cpp | 60 +++++++++++++ src/gui/widgets/spellshortcutcontainer.h | 2 + src/gui/widgets/virtshortcutcontainer.cpp | 74 ++++++++++++++++ src/gui/widgets/virtshortcutcontainer.h | 2 + 10 files changed, 352 insertions(+), 18 deletions(-) (limited to 'src/gui') diff --git a/src/gui/widgets/emoteshortcutcontainer.cpp b/src/gui/widgets/emoteshortcutcontainer.cpp index 0896188d9..97f012e89 100644 --- a/src/gui/widgets/emoteshortcutcontainer.cpp +++ b/src/gui/widgets/emoteshortcutcontainer.cpp @@ -127,6 +127,58 @@ void EmoteShortcutContainer::draw(Graphics *graphics) BLOCK_END("EmoteShortcutContainer::draw") } +void EmoteShortcutContainer::safeDraw(Graphics *graphics) +{ + if (!emoteShortcut) + return; + + BLOCK_START("EmoteShortcutContainer::draw") + if (settings.guiAlpha != mAlpha) + { + if (mBackgroundImg) + mBackgroundImg->setAlpha(mAlpha); + mAlpha = settings.guiAlpha; + } + + Font *const font = getFont(); + safeDrawBackground(graphics); + + unsigned sz = static_cast(mEmoteImg.size()); + if (sz > mMaxItems) + sz = mMaxItems; + for (unsigned i = 0; i < sz; i++) + { + const EmoteSprite *const emoteImg = mEmoteImg[i]; + if (emoteImg) + { + const AnimatedSprite *const sprite = emoteImg->sprite; + if (sprite) + { + sprite->draw(graphics, + (i % mGridWidth) * mBoxWidth + 2, + (i / mGridWidth) * mBoxHeight + 10); + } + } + } + for (unsigned i = 0; i < mMaxItems; i++) + { + const int emoteX = (i % mGridWidth) * mBoxWidth; + const int emoteY = (i / mGridWidth) * mBoxHeight; + + // Draw emote keyboard shortcut. + const std::string key = inputManager.getKeyValueString( + InputAction::EMOTE_1 + i); + + font->drawString(graphics, + mForegroundColor, + mForegroundColor2, + key, + emoteX + 2, emoteY + 2); + } + + BLOCK_END("EmoteShortcutContainer::draw") +} + void EmoteShortcutContainer::mouseDragged(MouseEvent &event A_UNUSED) { } diff --git a/src/gui/widgets/emoteshortcutcontainer.h b/src/gui/widgets/emoteshortcutcontainer.h index 8c58b793d..c1ba76b2b 100644 --- a/src/gui/widgets/emoteshortcutcontainer.h +++ b/src/gui/widgets/emoteshortcutcontainer.h @@ -51,6 +51,8 @@ class EmoteShortcutContainer final : public ShortcutContainer */ void draw(Graphics *graphics) override final; + void safeDraw(Graphics *graphics) override final; + /** * Handles mouse when dragged. */ diff --git a/src/gui/widgets/itemshortcutcontainer.cpp b/src/gui/widgets/itemshortcutcontainer.cpp index cc835903d..a0c807963 100644 --- a/src/gui/widgets/itemshortcutcontainer.cpp +++ b/src/gui/widgets/itemshortcutcontainer.cpp @@ -216,6 +216,143 @@ void ItemShortcutContainer::draw(Graphics *graphics) BLOCK_END("ItemShortcutContainer::draw") } +void ItemShortcutContainer::safeDraw(Graphics *graphics) +{ + BLOCK_START("ItemShortcutContainer::draw") + const ItemShortcut *const selShortcut = itemShortcut[mNumber]; + if (!selShortcut) + { + BLOCK_END("ItemShortcutContainer::draw") + return; + } + + if (settings.guiAlpha != mAlpha) + { + if (mBackgroundImg) + mBackgroundImg->setAlpha(mAlpha); + mAlpha = settings.guiAlpha; + } + + Font *const font = getFont(); + safeDrawBackground(graphics); + + const Inventory *const inv = PlayerInfo::getInventory(); + if (!inv) + { + BLOCK_END("ItemShortcutContainer::draw") + return; + } + + // +++ for future usage need reorder drawing images before text or back + for (unsigned i = 0; i < mMaxItems; i++) + { + const int itemX = (i % mGridWidth) * mBoxWidth; + const int itemY = (i / mGridWidth) * mBoxHeight; + + // Draw item keyboard shortcut. + const std::string key = inputManager.getKeyValueString( + InputAction::SHORTCUT_1 + i); + font->drawString(graphics, + mForegroundColor, + mForegroundColor, + key, + itemX + 2, itemY + 2); + + const int itemId = selShortcut->getItem(i); + const unsigned char itemColor = selShortcut->getItemColor(i); + + if (itemId < 0) + continue; + + // this is item + if (itemId < SPELL_MIN_ID) + { + const Item *const item = inv->findItem(itemId, itemColor); + if (item) + { + // Draw item icon. + Image *const image = item->getImage(); + if (image) + { + std::string caption; + if (item->getQuantity() > 1) + caption = toString(item->getQuantity()); + else if (item->isEquipped() == Equipped_true) + caption = "Eq."; + + image->setAlpha(1.0F); + graphics->drawImage(image, itemX, itemY); + if (item->isEquipped() == Equipped_true) + { + font->drawString(graphics, + mEquipedColor, + mEquipedColor2, + caption, + itemX + (mBoxWidth - font->getWidth(caption)) / 2, + itemY + mBoxHeight - 14); + } + else + { + font->drawString(graphics, + mUnEquipedColor, + mUnEquipedColor2, + caption, + itemX + (mBoxWidth - font->getWidth(caption)) / 2, + itemY + mBoxHeight - 14); + } + } + } + } + else if (itemId < SKILL_MIN_ID && spellManager) + { // this is magic shortcut + const TextCommand *const spell = spellManager + ->getSpellByItem(itemId); + if (spell) + { + if (!spell->isEmpty()) + { + Image *const image = spell->getImage(); + + if (image) + { + image->setAlpha(1.0F); + graphics->drawImage(image, itemX, itemY); + } + } + + font->drawString(graphics, + mForegroundColor, + mForegroundColor, + spell->getSymbol(), + itemX + 2, itemY + mBoxHeight / 2); + } + } + else if (skillDialog) + { + const SkillInfo *const skill = skillDialog->getSkill( + itemId - SKILL_MIN_ID); + if (skill) + { + Image *const image = skill->data->icon; + + if (image) + { + image->setAlpha(1.0F); + graphics->drawImage(image, itemX, itemY); + } + + font->drawString(graphics, + mForegroundColor, + mForegroundColor, + skill->data->shortName, + itemX + 2, + itemY + mBoxHeight / 2); + } + } + } + BLOCK_END("ItemShortcutContainer::draw") +} + void ItemShortcutContainer::mouseDragged(MouseEvent &event) { ItemShortcut *const selShortcut = itemShortcut[mNumber]; diff --git a/src/gui/widgets/itemshortcutcontainer.h b/src/gui/widgets/itemshortcutcontainer.h index d62b18914..a211941c2 100644 --- a/src/gui/widgets/itemshortcutcontainer.h +++ b/src/gui/widgets/itemshortcutcontainer.h @@ -51,6 +51,8 @@ class ItemShortcutContainer final : public ShortcutContainer */ void draw(Graphics *graphics) override final; + void safeDraw(Graphics *graphics) override final; + /** * Handles mouse when dragged. */ diff --git a/src/gui/widgets/shortcutcontainer.cpp b/src/gui/widgets/shortcutcontainer.cpp index 1ac9a9d69..e38b4c294 100644 --- a/src/gui/widgets/shortcutcontainer.cpp +++ b/src/gui/widgets/shortcutcontainer.cpp @@ -124,29 +124,30 @@ void ShortcutContainer::drawBackground(Graphics *g) { if (mBackgroundImg) { - if (isBatchDrawRenders(openGLMode)) - { - if (mRedraw) - { - mRedraw = false; - mVertexes->clear(); - for (unsigned i = 0; i < mMaxItems; i ++) - { - g->calcTileCollection(mVertexes, mBackgroundImg, - (i % mGridWidth) * mBoxWidth, - (i / mGridWidth) * mBoxHeight); - } - g->finalize(mVertexes); - } - g->drawTileCollection(mVertexes); - } - else + if (mRedraw) { + mRedraw = false; + mVertexes->clear(); for (unsigned i = 0; i < mMaxItems; i ++) { - g->drawImage(mBackgroundImg, (i % mGridWidth) * mBoxWidth, + g->calcTileCollection(mVertexes, mBackgroundImg, + (i % mGridWidth) * mBoxWidth, (i / mGridWidth) * mBoxHeight); } + g->finalize(mVertexes); + } + g->drawTileCollection(mVertexes); + } +} + +void ShortcutContainer::safeDrawBackground(Graphics *g) +{ + if (mBackgroundImg) + { + for (unsigned i = 0; i < mMaxItems; i ++) + { + g->drawImage(mBackgroundImg, (i % mGridWidth) * mBoxWidth, + (i / mGridWidth) * mBoxHeight); } } } diff --git a/src/gui/widgets/shortcutcontainer.h b/src/gui/widgets/shortcutcontainer.h index 8b8f52ff6..a8dad30c9 100644 --- a/src/gui/widgets/shortcutcontainer.h +++ b/src/gui/widgets/shortcutcontainer.h @@ -88,6 +88,8 @@ class ShortcutContainer notfinal : public Widget, void drawBackground(Graphics *g); + void safeDrawBackground(Graphics *g); + protected: /** * Constructor. Initializes the shortcut container. diff --git a/src/gui/widgets/spellshortcutcontainer.cpp b/src/gui/widgets/spellshortcutcontainer.cpp index 672858412..5a0de0995 100644 --- a/src/gui/widgets/spellshortcutcontainer.cpp +++ b/src/gui/widgets/spellshortcutcontainer.cpp @@ -121,6 +121,66 @@ void SpellShortcutContainer::draw(Graphics *graphics) BLOCK_END("SpellShortcutContainer::draw") } +void SpellShortcutContainer::safeDraw(Graphics *graphics) +{ + if (!spellShortcut) + return; + + BLOCK_START("SpellShortcutContainer::draw") + if (settings.guiAlpha != mAlpha) + { + mAlpha = settings.guiAlpha; + if (mBackgroundImg) + mBackgroundImg->setAlpha(mAlpha); + } + + Font *const font = getFont(); + + const int selectedId = spellShortcut->getSelectedItem(); + graphics->setColor(mForegroundColor); + safeDrawBackground(graphics); + + // +++ in future need reorder images and string drawing. + for (unsigned i = 0; i < mMaxItems; i++) + { + const int itemX = (i % mGridWidth) * mBoxWidth; + const int itemY = (i / mGridWidth) * mBoxHeight; + + const int itemId = getItemByIndex(i); + if (selectedId >= 0 && itemId == selectedId) + { + graphics->drawRectangle(Rect(itemX + 1, itemY + 1, + mBoxWidth - 1, mBoxHeight - 1)); + } + + if (!spellManager) + continue; + + const TextCommand *const spell = spellManager->getSpell(itemId); + if (spell) + { + if (!spell->isEmpty()) + { + Image *const image = spell->getImage(); + + if (image) + { + image->setAlpha(1.0F); + graphics->drawImage(image, itemX, itemY); + } + } + + font->drawString(graphics, + mForegroundColor, + mForegroundColor2, + spell->getSymbol(), + itemX + 2, itemY + mBoxHeight / 2); + } + } + + BLOCK_END("SpellShortcutContainer::draw") +} + void SpellShortcutContainer::mouseDragged(MouseEvent &event) { if (event.getButton() == MouseButton::LEFT) diff --git a/src/gui/widgets/spellshortcutcontainer.h b/src/gui/widgets/spellshortcutcontainer.h index b72a46c05..eb2dbec49 100644 --- a/src/gui/widgets/spellshortcutcontainer.h +++ b/src/gui/widgets/spellshortcutcontainer.h @@ -51,6 +51,8 @@ class SpellShortcutContainer final : public ShortcutContainer */ void draw(Graphics *graphics) override final; + void safeDraw(Graphics *graphics) override final; + /** * Handles mouse when dragged. */ diff --git a/src/gui/widgets/virtshortcutcontainer.cpp b/src/gui/widgets/virtshortcutcontainer.cpp index c4a85a0ed..138d3f5a6 100644 --- a/src/gui/widgets/virtshortcutcontainer.cpp +++ b/src/gui/widgets/virtshortcutcontainer.cpp @@ -144,6 +144,80 @@ void VirtShortcutContainer::draw(Graphics *graphics) BLOCK_END("VirtShortcutContainer::draw") } +void VirtShortcutContainer::safeDraw(Graphics *graphics) +{ + if (!mShortcut) + return; + + BLOCK_START("VirtShortcutContainer::safeDraw") + if (settings.guiAlpha != mAlpha) + { + mAlpha = settings.guiAlpha; + if (mBackgroundImg) + mBackgroundImg->setAlpha(mAlpha); + } + + safeDrawBackground(graphics); + + const Inventory *const inv = PlayerInfo::getInventory(); + if (!inv) + { + BLOCK_END("VirtShortcutContainer::safeDraw") + return; + } + + Font *const font = getFont(); + + for (unsigned i = 0; i < mMaxItems; i++) + { + const int itemX = (i % mGridWidth) * mBoxWidth; + const int itemY = (i / mGridWidth) * mBoxHeight; + + if (mShortcut->getItem(i) < 0) + continue; + + const Item *const item = inv->findItem(mShortcut->getItem(i), + mShortcut->getItemColor(i)); + + if (item) + { + // Draw item icon. + Image *const image = item->getImage(); + + if (image) + { + std::string caption; + if (item->getQuantity() > 1) + caption = toString(item->getQuantity()); + else if (item->isEquipped() == Equipped_true) + caption = "Eq."; + + image->setAlpha(1.0F); + graphics->drawImage(image, itemX, itemY); + if (item->isEquipped() == Equipped_true) + { + font->drawString(graphics, + mEquipedColor, + mEquipedColor2, + caption, + itemX + (mBoxWidth - font->getWidth(caption)) / 2, + itemY + mBoxHeight - 14); + } + else + { + font->drawString(graphics, + mUnEquipedColor, + mUnEquipedColor2, + caption, + itemX + (mBoxWidth - font->getWidth(caption)) / 2, + itemY + mBoxHeight - 14); + } + } + } + } + BLOCK_END("VirtShortcutContainer::safeDraw") +} + void VirtShortcutContainer::mouseDragged(MouseEvent &event) { if (!mShortcut) diff --git a/src/gui/widgets/virtshortcutcontainer.h b/src/gui/widgets/virtshortcutcontainer.h index b2fd0dfe5..96aea1012 100644 --- a/src/gui/widgets/virtshortcutcontainer.h +++ b/src/gui/widgets/virtshortcutcontainer.h @@ -53,6 +53,8 @@ class VirtShortcutContainer final : public ShortcutContainer */ void draw(Graphics *graphics) override final; + void safeDraw(Graphics *graphics) override final; + /** * Handles mouse when dragged. */ -- cgit v1.2.3-70-g09d2