Refactor sprite scaling in STKModifiedSpriteBank

Make it proportional to font height so that it can be auto-resized
This commit is contained in:
CodingJellyfish 2024-04-23 15:30:35 +08:00
parent 1319c9845b
commit db2cc69d89
18 changed files with 70 additions and 85 deletions

View File

@ -11,6 +11,7 @@
#include <cassert> #include <cassert>
#include "font/font_drawer.hpp" #include "font/font_drawer.hpp"
#include "graphics/2dutils.hpp" #include "graphics/2dutils.hpp"
#include "guiengine/engine.hpp"
namespace irr namespace irr
{ {
@ -27,7 +28,7 @@ STKModifiedSpriteBank::STKModifiedSpriteBank(IGUIEnvironment* env) :
#endif #endif
m_scale = 1.0f; m_scale = 1.0f;
m_height = 0; m_fixed_scale = 0.0f;
m_target_icon_size = core::dimension2du(0, 0); m_target_icon_size = core::dimension2du(0, 0);
if (Environment) if (Environment)
{ {
@ -332,28 +333,22 @@ void STKModifiedSpriteBank::draw2DSpriteBatch(const core::array<u32>& indices,
} }
} // draw2DSpriteBatch } // draw2DSpriteBatch
// ----------------------------------------------------------------------------
void STKModifiedSpriteBank::scaleToHeight(int height)
{
m_height = height;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
s32 STKModifiedSpriteBank::getScaledWidth(s32 width) const s32 STKModifiedSpriteBank::getScaledWidth(s32 width) const
{ {
if (m_height == 0) if (m_fixed_scale == 0.0f)
return (s32)((float)width * m_scale); return (s32)(GUIEngine::getFontHeight() * (float)width * m_scale);
else else
return m_height; return (s32)(GUIEngine::getFontHeight() * m_fixed_scale);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
s32 STKModifiedSpriteBank::getScaledHeight(s32 height) const s32 STKModifiedSpriteBank::getScaledHeight(s32 height) const
{ {
if (m_height == 0) if (m_fixed_scale == 0.0f)
return (s32)((float)height * m_scale); return (s32)(GUIEngine::getFontHeight() * (float)height * m_scale);
else else
return m_height; return (s32)(GUIEngine::getFontHeight() * m_fixed_scale);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -67,7 +67,8 @@ public:
m_scale = scale; m_scale = scale;
} }
void scaleToHeight(int height); void setFixedScale(float scale)
{ m_fixed_scale = scale; }
void setTargetIconSize(int width, int height) void setTargetIconSize(int width, int height)
{ m_target_icon_size = core::dimension2du(width, height); } { m_target_icon_size = core::dimension2du(width, height); }
@ -78,7 +79,7 @@ protected:
unsigned int m_magic_number; unsigned int m_magic_number;
float m_scale; float m_scale;
int m_height; float m_fixed_scale;
struct SDrawBatch struct SDrawBatch
{ {

View File

@ -48,11 +48,12 @@ ListWidget::ListWidget() : Widget(WTYPE_LIST)
m_sortable = true; m_sortable = true;
m_header_created = false; m_header_created = false;
m_choosing_header = false; m_choosing_header = false;
m_icon_scale = -1.0f;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void ListWidget::setIcons(STKModifiedSpriteBank* icons, int size) void ListWidget::setIcons(STKModifiedSpriteBank* icons, float scale)
{ {
m_use_icons = (icons != NULL); m_use_icons = (icons != NULL);
m_icons = icons; m_icons = icons;
@ -63,28 +64,8 @@ void ListWidget::setIcons(STKModifiedSpriteBank* icons, int size)
if (m_use_icons) if (m_use_icons)
{ {
list->setSpriteBank(m_icons); list->setSpriteBank(m_icons);
m_icon_scale = scale;
// determine needed height updateIconScale();
int item_height = 0;
if (size > 0)
{
item_height = size;
}
else
{
const core::array< core::rect<s32> >& rects = m_icons->getPositions();
const int count = rects.size();
for (int n=0; n<count; n++)
{
const int h = rects[n].getHeight();
if (h > item_height) item_height = h;
}
}
if (item_height > 0)
{
list->setItemHeight( item_height );
}
} }
else else
{ {
@ -93,6 +74,31 @@ void ListWidget::setIcons(STKModifiedSpriteBank* icons, int size)
} }
// -----------------------------------------------------------------------------
void ListWidget::updateIconScale()
{
CGUISTKListBox* list = getIrrlichtElement<CGUISTKListBox>();
assert(list != NULL);
// determine needed height
int item_height = (int)(m_icon_scale * GUIEngine::getFontHeight());
if (m_icon_scale <= 0.0f)
{
const core::array< core::rect<s32> >& rects = m_icons->getPositions();
const int count = rects.size();
for (int n = 0; n < count; n++)
{
const int h = rects[n].getHeight();
if (h > item_height) item_height = h;
}
}
if (item_height > 0)
{
list->setItemHeight(item_height);
}
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
int ListWidget::getHeaderHeight() const int ListWidget::getHeaderHeight() const
{ {
@ -699,4 +705,6 @@ void ListWidget::resize()
if (m_element) if (m_element)
m_element->setRelativePosition(getListBoxSize()); m_element->setRelativePosition(getListBoxSize());
updateHeader(); updateHeader();
if (m_use_icons)
updateIconScale();
} }

View File

@ -98,6 +98,8 @@ namespace GUIEngine
bool m_header_created; bool m_header_created;
float m_icon_scale;
void repairSortCol() void repairSortCol()
{ {
// Exclude scrollbar // Exclude scrollbar
@ -109,6 +111,7 @@ namespace GUIEngine
} }
void updateHeader(); void updateHeader();
void updateIconScale();
int getHeaderHeight() const; int getHeaderHeight() const;
irr::core::rect<s32> getListBoxSize() const; irr::core::rect<s32> getListBoxSize() const;
@ -140,7 +143,7 @@ namespace GUIEngine
* you're done with it (but do not delete it when the list widget is still active) * you're done with it (but do not delete it when the list widget is still active)
* \pre may only be called after the widget has been added to the screen with add() * \pre may only be called after the widget has been added to the screen with add()
*/ */
void setIcons(irr::gui::STKModifiedSpriteBank* icons, int size=-1); void setIcons(irr::gui::STKModifiedSpriteBank* icons, float scale = -1.0f);
// ---- contents management // ---- contents management

View File

@ -157,11 +157,10 @@ void AddonsScreen::init()
getWidget<GUIEngine::ListWidget>("list_addons"); getWidget<GUIEngine::ListWidget>("list_addons");
// This defines the row height ! // This defines the row height !
m_icon_height = GUIEngine::getFontHeight() * 2; m_icon_bank->setScale(1.0f / 72.0f);
// 128 is the height of the image file // 128 is the height of the image file
m_icon_bank->setScale((float)GUIEngine::getFontHeight() / 72.0f); m_icon_bank->setTargetIconSize(128, 128);
m_icon_bank->setTargetIconSize(128,128); w_list->setIcons(m_icon_bank, 2.0f);
w_list->setIcons(m_icon_bank, (int)(m_icon_height));
m_type = "kart"; m_type = "kart";

View File

@ -69,8 +69,6 @@ private:
* addons_loading is being displayed. */ * addons_loading is being displayed. */
int m_selected_index; int m_selected_index;
float m_icon_height;
bool m_reloading; bool m_reloading;
bool m_sort_desc; bool m_sort_desc;

View File

@ -83,8 +83,7 @@ GhostReplayInfoDialog::GhostReplayInfoDialog(unsigned int replay_id,
/* Used to display kart icons for the selected replay(s) */ /* Used to display kart icons for the selected replay(s) */
irr::gui::STKModifiedSpriteBank *icon_bank = GhostReplaySelection::getInstance()->getIconBank(); irr::gui::STKModifiedSpriteBank *icon_bank = GhostReplaySelection::getInstance()->getIconBank();
int icon_height = GUIEngine::getFontHeight() * 3 / 2; m_replay_info_widget->setIcons(icon_bank, 1.5f);
m_replay_info_widget->setIcons(icon_bank, (int)icon_height);
updateReplayDisplayedInfo(); updateReplayDisplayedInfo();

View File

@ -101,11 +101,10 @@ HighScoreInfoDialog::HighScoreInfoDialog(Highscores* highscore, bool is_linear,
/* Used to display kart icons for the entries */ /* Used to display kart icons for the entries */
irr::gui::STKModifiedSpriteBank *icon_bank = HighScoreSelection::getInstance()->getIconBank(); irr::gui::STKModifiedSpriteBank *icon_bank = HighScoreSelection::getInstance()->getIconBank();
int icon_height = GUIEngine::getFontHeight() * 3 / 2;
icon_bank->setScale(icon_height/128.0f); icon_bank->setScale(1.5f / 128.0f);
icon_bank->setTargetIconSize(128, 128); icon_bank->setTargetIconSize(128, 128);
m_high_score_list->setIcons(icon_bank, (int)icon_height); m_high_score_list->setIcons(icon_bank, 1.5f);
updateHighscoreEntries(); updateHighscoreEntries();

View File

@ -234,8 +234,8 @@ void EditGPScreen::loadList(const int selected)
m_list->clear(); m_list->clear();
m_icons.clear(); m_icons.clear();
m_icon_bank->clear(); m_icon_bank->clear();
m_icon_bank->scaleToHeight (GUIEngine::getFontHeight() * 3 / 2); m_icon_bank->setFixedScale(1.5f);
m_list->setIcons(m_icon_bank, GUIEngine::getFontHeight() * 3 / 2); m_list->setIcons(m_icon_bank, 1.5f);
for (unsigned int i = 0; i < m_gp->getNumberOfTracks(true); i++) for (unsigned int i = 0; i < m_gp->getNumberOfTracks(true); i++)
{ {

View File

@ -177,14 +177,11 @@ void GhostReplaySelection::init()
{ {
Screen::init(); Screen::init();
m_cur_difficulty = RaceManager::get()->getDifficulty(); m_cur_difficulty = RaceManager::get()->getDifficulty();
int icon_height = GUIEngine::getFontHeight();
int row_height = GUIEngine::getFontHeight() * 5 / 4;
// 128 is the height of the image file // 128 is the height of the image file
m_icon_bank->setScale(icon_height/128.0f); m_icon_bank->setScale(1.0f / 128.0f);
m_icon_bank->setTargetIconSize(128, 128); m_icon_bank->setTargetIconSize(128, 128);
m_replay_list_widget->setIcons(m_icon_bank, (int)row_height); m_replay_list_widget->setIcons(m_icon_bank, 1.25f);
refresh(/*reload replay files*/ false, /* update columns */ true); refresh(/*reload replay files*/ false, /* update columns */ true);
} // init } // init

View File

@ -291,11 +291,9 @@ void GPInfoScreen::init()
getWidget<LabelWidget>("name")->setText(m_gp.getName(), false); getWidget<LabelWidget>("name")->setText(m_gp.getName(), false);
m_gp.checkConsistency(); m_gp.checkConsistency();
int icon_height = GUIEngine::getFontHeight(); m_icon_bank->setScale(1.0f / 128.0f);
int row_height = GUIEngine::getFontHeight() * 1.2f; m_icon_bank->setTargetIconSize(128, 128);
m_icon_bank->setScale(icon_height/128.0f); m_highscore_list->setIcons(m_icon_bank, 1.2f);
m_icon_bank->setTargetIconSize(128,128);
m_highscore_list->setIcons(m_icon_bank,row_height);
RaceManager::get()->setNumKarts(RaceManager::get()->getNumLocalPlayers() + m_ai_kart_spinner->getValue()); RaceManager::get()->setNumKarts(RaceManager::get()->getNumLocalPlayers() + m_ai_kart_spinner->getValue());
// We don't save highscores for random gps so load highscores here // We don't save highscores for random gps so load highscores here
updateHighscores(); updateHighscores();

View File

@ -168,13 +168,10 @@ void HighScoreSelection::init()
{ {
Screen::init(); Screen::init();
int icon_height = GUIEngine::getFontHeight();
int row_height = GUIEngine::getFontHeight() * 5 / 4;
// 128 is the height of the image file // 128 is the height of the image file
m_icon_bank->setScale(icon_height/128.0f); m_icon_bank->setScale(1.0f / 128.0f);
m_icon_bank->setTargetIconSize(128, 128); m_icon_bank->setTargetIconSize(128, 128);
m_high_scores_list_widget->setIcons(m_icon_bank, (int)row_height); m_high_scores_list_widget->setIcons(m_icon_bank, 1.25f);
refresh(/*reload high score list*/ false, /* update columns */ true); refresh(/*reload high score list*/ false, /* update columns */ true);
} // init } // init

View File

@ -157,7 +157,7 @@ void NetworkingLobby::loadedFromFile()
m_icon_bank->addTextureAsSprite(m_spectate_texture); m_icon_bank->addTextureAsSprite(m_spectate_texture);
m_icon_bank->addTextureAsSprite(icon_6); m_icon_bank->addTextureAsSprite(icon_6);
m_icon_bank->setScale((float)GUIEngine::getFontHeight() / 96.0f); m_icon_bank->setScale(1.0f / 96.0f);
m_icon_bank->setTargetIconSize(128, 128); m_icon_bank->setTargetIconSize(128, 128);
} // loadedFromFile } // loadedFromFile

View File

@ -174,7 +174,7 @@ void ServerSelection::init()
m_searcher->clearListeners(); m_searcher->clearListeners();
m_searcher->addListener(this); m_searcher->addListener(this);
m_icon_bank->setScale((float)GUIEngine::getFontHeight() / 72.0f); m_icon_bank->setScale(1.0f / 72.0f);
m_icon_bank->setTargetIconSize(128, 128); m_icon_bank->setTargetIconSize(128, 128);
video::ITexture* icon1 = irr_driver->getTexture( video::ITexture* icon1 = irr_driver->getTexture(
@ -204,10 +204,8 @@ void ServerSelection::init()
assert(tex); assert(tex);
m_icon_bank->addTextureAsSprite(tex); m_icon_bank->addTextureAsSprite(tex);
} }
int row_height = GUIEngine::getFontHeight() * 2;
m_server_list_widget->setIcons(m_icon_bank, row_height); m_server_list_widget->setIcons(m_icon_bank, 2.0f);
m_sort_desc = false; m_sort_desc = false;
refresh(); refresh();
m_ipv6_only_without_nat64 = false; m_ipv6_only_without_nat64 = false;

View File

@ -451,10 +451,9 @@ void TracksScreen::init()
m_track_icons->addTextureAsSprite(tex); m_track_icons->addTextureAsSprite(tex);
} }
int icon_height = getHeight() / 13; m_track_icons->setScale(1.0f / 128.0f);
m_track_icons->setScale(icon_height / 256.0f);
m_track_icons->setTargetIconSize(256, 256); m_track_icons->setTargetIconSize(256, 256);
m_vote_list->setIcons(m_track_icons, (int)icon_height); m_vote_list->setIcons(m_track_icons);
const PeerVote* vote = cl->getVote(STKHost::get()->getMyHostId()); const PeerVote* vote = cl->getVote(STKHost::get()->getMyHostId());
if (vote) if (vote)

View File

@ -18,7 +18,6 @@
#include "states_screens/options/options_screen_device.hpp" #include "states_screens/options/options_screen_device.hpp"
#include "config/user_config.hpp" #include "config/user_config.hpp"
#include "guiengine/CGUISpriteBank.hpp"
#include "guiengine/message_queue.hpp" #include "guiengine/message_queue.hpp"
#include "guiengine/scalable_font.hpp" #include "guiengine/scalable_font.hpp"
#include "guiengine/screen.hpp" #include "guiengine/screen.hpp"

View File

@ -74,9 +74,7 @@ void OptionsScreenInput::loadedFromFile()
m_icon_bank->addTextureAsSprite(icon4); m_icon_bank->addTextureAsSprite(icon4);
m_icon_bank->addTextureAsSprite(icon5); m_icon_bank->addTextureAsSprite(icon5);
// scale icons depending on font height m_icon_bank->setScale(1.0f / 72.0f);
const float scale = GUIEngine::getFontHeight() / 72.0f;
m_icon_bank->setScale(scale);
m_icon_bank->setTargetIconSize(128, 128); m_icon_bank->setTargetIconSize(128, 128);
m_gamepad_count = 0; m_gamepad_count = 0;
} // loadFromFile } // loadFromFile

View File

@ -345,12 +345,9 @@ void TrackInfoScreen::init()
if (has_highscores) if (has_highscores)
{ {
int icon_height = GUIEngine::getFontHeight(); m_icon_bank->setScale(1.0f / 128.0f);
int row_height = GUIEngine::getFontHeight() * 1.2f;
m_icon_bank->setScale(icon_height/128.0f);
m_icon_bank->setTargetIconSize(128, 128); m_icon_bank->setTargetIconSize(128, 128);
m_highscore_entries->setIcons(m_icon_bank, (int)row_height); m_highscore_entries->setIcons(m_icon_bank, 1.2f);
m_highscore_entries->setVisible(has_highscores); m_highscore_entries->setVisible(has_highscores);
updateHighScores(); updateHighScores();