Skip to content

Commit

Permalink
Merge pull request #2302 from akortunov/guifixes
Browse files Browse the repository at this point in the history
Scalable UI textures
  • Loading branch information
psi29a committed Apr 12, 2019
2 parents 9b716a2 + 31ddb0a commit 785a667
Show file tree
Hide file tree
Showing 23 changed files with 478 additions and 346 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -75,6 +75,7 @@
Feature #4887: Add openmw command option to set initial random seed
Feature #4890: Make Distant Terrain configurable
Feature #4962: Add casting animations for magic items
Feature #4968: Scalable UI widget skins
Task #4686: Upgrade media decoder to a more current FFmpeg API
Task #4695: Optimize Distant Terrain memory consumption

Expand Down
2 changes: 1 addition & 1 deletion apps/openmw/CMakeLists.txt
Expand Up @@ -32,7 +32,7 @@ add_openmw_dir (mwinput
add_openmw_dir (mwgui
layout textinput widgets race class birth review windowmanagerimp console dialogue
windowbase statswindow messagebox journalwindow charactercreation
mapwindow windowpinnablebase tooltips scrollwindow bookwindow
mapwindow windowpinnablebase tooltips scrollwindow bookwindow resourceskin
formatting inventorywindow container hud countdialog tradewindow settingswindow
confirmationdialog alchemywindow referenceinterface spellwindow mainmenu quickkeysmenu
itemselection spellbuyingwindow loadingscreen levelupdialog waitdialog spellcreationdialog
Expand Down
21 changes: 6 additions & 15 deletions apps/openmw/mwgui/bookwindow.cpp
Expand Up @@ -20,7 +20,7 @@ namespace MWGui
{

BookWindow::BookWindow ()
: WindowBase("openmw_book.layout")
: BookWindowBase("openmw_book.layout")
, mCurrentPage(0)
, mTakeButtonShow(true)
, mTakeButtonAllowed(true)
Expand All @@ -43,10 +43,10 @@ namespace MWGui
getWidget(mLeftPage, "LeftPage");
getWidget(mRightPage, "RightPage");

adjustButton(mCloseButton);
adjustButton(mTakeButton);
adjustButton(mNextPageButton);
adjustButton(mPrevPageButton);
adjustButton("CloseButton");
adjustButton("TakeButton");
adjustButton("PrevPageBTN");
float scale = adjustButton("NextPageBTN");

mLeftPage->setNeedMouseFocus(true);
mLeftPage->eventMouseWheel += MyGUI::newDelegate(this, &BookWindow::onMouseWheel);
Expand All @@ -62,7 +62,7 @@ namespace MWGui
{
// english button has a 7 pixel wide strip of garbage on its right edge
mNextPageButton->setSize(64-7, mNextPageButton->getSize().height);
mNextPageButton->setImageCoord(MyGUI::IntCoord(0,0,64-7,mNextPageButton->getSize().height));
mNextPageButton->setImageCoord(MyGUI::IntCoord(0,0,(64-7)*scale,mNextPageButton->getSize().height*scale));
}

center();
Expand Down Expand Up @@ -187,15 +187,6 @@ namespace MWGui
}
}

void BookWindow::adjustButton (Gui::ImageButton* button)
{
MyGUI::IntSize diff = button->getSize() - button->getRequestedSize();
button->setSize(button->getRequestedSize());

if (button->getAlign().isRight())
button->setPosition(button->getPosition() + MyGUI::IntPoint(diff.width,0));
}

void BookWindow::nextPage()
{
if ((mCurrentPage+1)*2 < mPages.size())
Expand Down
3 changes: 1 addition & 2 deletions apps/openmw/mwgui/bookwindow.hpp
Expand Up @@ -9,7 +9,7 @@

namespace MWGui
{
class BookWindow : public WindowBase
class BookWindow : public BookWindowBase
{
public:
BookWindow();
Expand All @@ -34,7 +34,6 @@ namespace MWGui

void updatePages();
void clearPages();
void adjustButton(Gui::ImageButton* button);

private:
typedef std::pair<int, int> Page;
Expand Down
29 changes: 25 additions & 4 deletions apps/openmw/mwgui/itemwidget.cpp
Expand Up @@ -2,6 +2,7 @@

#include <MyGUI_FactoryManager.h>
#include <MyGUI_ImageBox.h>
#include <MyGUI_RenderManager.h>
#include <MyGUI_TextBox.h>

// correctIconPath
Expand Down Expand Up @@ -30,6 +31,7 @@ namespace

namespace MWGui
{
std::map<std::string, float> ItemWidget::mScales;

ItemWidget::ItemWidget()
: mItem(nullptr)
Expand Down Expand Up @@ -146,10 +148,29 @@ namespace MWGui
if (backgroundTex != "")
backgroundTex += ".dds";

if (state == Barter && !isMagic)
setFrame(backgroundTex, MyGUI::IntCoord(2,2,42,42));
else
setFrame(backgroundTex, MyGUI::IntCoord(0,0,42,42));
if (!backgroundTex.empty())
{
float scale = 1.f;
auto found = mScales.find(backgroundTex);
if (found == mScales.end())
{
// By default, background icons are supposed to use the 42x42 part of 64x64 image.
// If the image has a different size, we should use a different chunk size
// Cache result to do not retrieve background texture every frame.
MyGUI::ITexture* texture = MyGUI::RenderManager::getInstance().getTexture(backgroundTex);
if (texture)
scale = texture->getHeight() / 64.f;

mScales[backgroundTex] = scale;
}
else
scale = found->second;

if (state == Barter && !isMagic)
setFrame(backgroundTex, MyGUI::IntCoord(2*scale,2*scale,44*scale,44*scale));
else
setFrame(backgroundTex, MyGUI::IntCoord(0,0,44*scale,44*scale));
}

setIcon(ptr);
}
Expand Down
2 changes: 2 additions & 0 deletions apps/openmw/mwgui/itemwidget.hpp
Expand Up @@ -50,6 +50,8 @@ namespace MWGui

std::string mCurrentIcon;
std::string mCurrentFrame;

static std::map<std::string, float> mScales;
};

class SpellWidget : public ItemWidget
Expand Down
17 changes: 3 additions & 14 deletions apps/openmw/mwgui/journalwindow.cpp
Expand Up @@ -153,7 +153,7 @@ namespace
}

adjustButton(PrevPageBTN);
adjustButton(NextPageBTN);
float nextButtonScale = adjustButton(NextPageBTN);
adjustButton(CloseBTN);
adjustButton(CancelBTN);
adjustButton(JournalBTN);
Expand All @@ -168,7 +168,7 @@ namespace
{
// english button has a 7 pixel wide strip of garbage on its right edge
nextButton->setSize(64-7, nextButton->getSize().height);
nextButton->setImageCoord(MyGUI::IntCoord(0,0,64-7,nextButton->getSize().height));
nextButton->setImageCoord(MyGUI::IntCoord(0,0,(64-7)*nextButtonScale,nextButton->getSize().height*nextButtonScale));
}

if (!questList)
Expand Down Expand Up @@ -226,17 +226,6 @@ namespace
mTopicsMode = false;
}

void adjustButton (char const * name)
{
Gui::ImageButton* button = getWidget<Gui::ImageButton>(name);

MyGUI::IntSize diff = button->getSize() - button->getRequestedSize();
button->setSize(button->getRequestedSize());

if (button->getAlign().isRight())
button->setPosition(button->getPosition() + MyGUI::IntPoint(diff.width,0));
}

void onOpen()
{
if (!MWBase::Environment::get().getWindowManager ()->getJournalAllowed ())
Expand Down Expand Up @@ -665,7 +654,7 @@ MWGui::JournalWindow * MWGui::JournalWindow::create (JournalViewModel::Ptr Model
}

MWGui::JournalWindow::JournalWindow()
:WindowBase("openmw_journal.layout")
: BookWindowBase("openmw_journal.layout")
{

}
2 changes: 1 addition & 1 deletion apps/openmw/mwgui/journalwindow.hpp
Expand Up @@ -13,7 +13,7 @@ namespace MWGui
{
struct JournalViewModel;

struct JournalWindow : public WindowBase
struct JournalWindow : public BookWindowBase
{
JournalWindow();

Expand Down
11 changes: 7 additions & 4 deletions apps/openmw/mwgui/mainmenu.cpp
Expand Up @@ -285,15 +285,18 @@ namespace MWGui
Gui::ImageButton* button = mButtons[buttonId];
button->setVisible(true);

// By default, assume that all menu buttons textures should have 64 height.
// If they have a different resolution, scale them.
MyGUI::IntSize requested = button->getRequestedSize();
float scale = requested.height / 64.f;

button->setImageCoord(MyGUI::IntCoord(0, 0, requested.width, requested.height));
// Trim off some of the excessive padding
// TODO: perhaps do this within ImageButton?
int height = requested.height-16;
button->setImageTile(MyGUI::IntSize(requested.width, height));
button->setCoord((maxwidth-requested.width) / 2, curH, requested.width, height);
curH += height;
int height = requested.height;
button->setImageTile(MyGUI::IntSize(requested.width, requested.height-16*scale));
button->setCoord((maxwidth-requested.width/scale) / 2, curH, requested.width/scale, height/scale-16);
curH += height/scale-16;
}

if (state == MWBase::StateManager::State_NoGame)
Expand Down
83 changes: 83 additions & 0 deletions apps/openmw/mwgui/resourceskin.cpp
@@ -0,0 +1,83 @@
#include "resourceskin.hpp"

#include <MyGUI_RenderManager.h>

#include <components/misc/stringops.hpp>

namespace MWGui
{
void resizeSkin(MyGUI::xml::ElementPtr _node)
{
_node->setAttribute("type", "ResourceSkin");
const std::string size = _node->findAttribute("size");
if (!size.empty())
return;

const std::string textureName = _node->findAttribute("texture");
if (textureName.empty())
return;

MyGUI::ITexture* texture = MyGUI::RenderManager::getInstance().getTexture(textureName);
if (!texture)
return;

MyGUI::IntCoord coord(0, 0, texture->getWidth(), texture->getHeight());
MyGUI::xml::ElementEnumerator basis = _node->getElementEnumerator();
const std::string textureSize = std::to_string(coord.width) + " " + std::to_string(coord.height);
_node->addAttribute("size", textureSize);
while (basis.next())
{
if (basis->getName() != "BasisSkin")
continue;

const std::string basisSkinType = basis->findAttribute("type");
if (Misc::StringUtils::ciEqual(basisSkinType, "SimpleText"))
continue;

const std::string offset = basis->findAttribute("offset");
if (!offset.empty())
continue;

basis->addAttribute("offset", coord);

MyGUI::xml::ElementEnumerator state = basis->getElementEnumerator();
while (state.next())
{
if (state->getName() == "State")
{
const std::string stateOffset = state->findAttribute("offset");
if (!stateOffset.empty())
continue;

state->addAttribute("offset", coord);
if (Misc::StringUtils::ciEqual(basisSkinType, "TileRect"))
{
MyGUI::xml::ElementEnumerator property = state->getElementEnumerator();
bool hasTileSize = false;
while (property.next("Property"))
{
const std::string key = property->findAttribute("key");
if (key != "TileSize")
continue;

hasTileSize = true;
}

if (!hasTileSize)
{
MyGUI::xml::ElementPtr tileSizeProperty = state->createChild("Property");
tileSizeProperty->addAttribute("key", "TileSize");
tileSizeProperty->addAttribute("value", textureSize);
}
}
}
}
}
}

void AutoSizedResourceSkin::deserialization(MyGUI::xml::ElementPtr _node, MyGUI::Version _version)
{
resizeSkin(_node);
Base::deserialization(_node, _version);
}
}
18 changes: 18 additions & 0 deletions apps/openmw/mwgui/resourceskin.hpp
@@ -0,0 +1,18 @@
#ifndef MWGUI_RESOURCESKIN_H
#define MWGUI_RESOURCESKIN_H

#include <MyGUI_ResourceSkin.h>

namespace MWGui
{
class AutoSizedResourceSkin : public MyGUI::ResourceSkin
{
MYGUI_RTTI_DERIVED( AutoSizedResourceSkin )

public:
virtual void deserialization(MyGUI::xml::ElementPtr _node, MyGUI::Version _version);
};

}

#endif
18 changes: 3 additions & 15 deletions apps/openmw/mwgui/scrollwindow.cpp
Expand Up @@ -16,23 +16,11 @@

#include "formatting.hpp"

namespace
{
void adjustButton (Gui::ImageButton* button)
{
MyGUI::IntSize diff = button->getSize() - button->getRequestedSize();
button->setSize(button->getRequestedSize());

if (button->getAlign().isRight())
button->setPosition(button->getPosition() + MyGUI::IntPoint(diff.width,0));
}
}

namespace MWGui
{

ScrollWindow::ScrollWindow ()
: WindowBase("openmw_scroll.layout")
: BookWindowBase("openmw_scroll.layout")
, mTakeButtonShow(true)
, mTakeButtonAllowed(true)
{
Expand All @@ -44,8 +32,8 @@ namespace MWGui
getWidget(mTakeButton, "TakeButton");
mTakeButton->eventMouseButtonClick += MyGUI::newDelegate(this, &ScrollWindow::onTakeButtonClicked);

adjustButton(mCloseButton);
adjustButton(mTakeButton);
adjustButton("CloseButton");
adjustButton("TakeButton");

mCloseButton->eventKeyButtonPressed += MyGUI::newDelegate(this, &ScrollWindow::onKeyButtonPressed);
mTakeButton->eventKeyButtonPressed += MyGUI::newDelegate(this, &ScrollWindow::onKeyButtonPressed);
Expand Down
2 changes: 1 addition & 1 deletion apps/openmw/mwgui/scrollwindow.hpp
Expand Up @@ -12,7 +12,7 @@ namespace Gui

namespace MWGui
{
class ScrollWindow : public WindowBase
class ScrollWindow : public BookWindowBase
{
public:
ScrollWindow ();
Expand Down

0 comments on commit 785a667

Please sign in to comment.