Skip to content

Commit

Permalink
Refactor sprite scaling in STKModifiedSpriteBank
Browse files Browse the repository at this point in the history
Make it proportional to font height so that it can be auto-resized
  • Loading branch information
CodingJellyfish committed Apr 23, 2024
1 parent 1319c98 commit db2cc69
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 84 deletions.
21 changes: 8 additions & 13 deletions src/guiengine/CGUISpriteBank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <cassert>
#include "font/font_drawer.hpp"
#include "graphics/2dutils.hpp"
#include "guiengine/engine.hpp"

namespace irr
{
Expand All @@ -27,7 +28,7 @@ STKModifiedSpriteBank::STKModifiedSpriteBank(IGUIEnvironment* env) :
#endif

m_scale = 1.0f;
m_height = 0;
m_fixed_scale = 0.0f;
m_target_icon_size = core::dimension2du(0, 0);
if (Environment)
{
Expand Down Expand Up @@ -332,28 +333,22 @@ void STKModifiedSpriteBank::draw2DSpriteBatch(const core::array<u32>& indices,
}
} // draw2DSpriteBatch

// ----------------------------------------------------------------------------
void STKModifiedSpriteBank::scaleToHeight(int height)
{
m_height = height;
}

// ----------------------------------------------------------------------------
s32 STKModifiedSpriteBank::getScaledWidth(s32 width) const
{
if (m_height == 0)
return (s32)((float)width * m_scale);
if (m_fixed_scale == 0.0f)
return (s32)(GUIEngine::getFontHeight() * (float)width * m_scale);
else
return m_height;
return (s32)(GUIEngine::getFontHeight() * m_fixed_scale);
}

// ----------------------------------------------------------------------------
s32 STKModifiedSpriteBank::getScaledHeight(s32 height) const
{
if (m_height == 0)
return (s32)((float)height * m_scale);
if (m_fixed_scale == 0.0f)
return (s32)(GUIEngine::getFontHeight() * (float)height * m_scale);
else
return m_height;
return (s32)(GUIEngine::getFontHeight() * m_fixed_scale);
}

// ----------------------------------------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions src/guiengine/CGUISpriteBank.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class STKModifiedSpriteBank : public IGUISpriteBank
m_scale = scale;
}

void scaleToHeight(int height);
void setFixedScale(float scale)
{ m_fixed_scale = scale; }

void setTargetIconSize(int width, int height)
{ m_target_icon_size = core::dimension2du(width, height); }
Expand All @@ -78,7 +79,7 @@ class STKModifiedSpriteBank : public IGUISpriteBank
unsigned int m_magic_number;

float m_scale;
int m_height;
float m_fixed_scale;

struct SDrawBatch
{
Expand Down
52 changes: 30 additions & 22 deletions src/guiengine/widgets/list_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ ListWidget::ListWidget() : Widget(WTYPE_LIST)
m_sortable = true;
m_header_created = 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_icons = icons;
Expand All @@ -63,34 +64,39 @@ void ListWidget::setIcons(STKModifiedSpriteBank* icons, int size)
if (m_use_icons)
{
list->setSpriteBank(m_icons);
m_icon_scale = scale;
updateIconScale();
}
else
{
list->setSpriteBank(NULL);
}

// determine needed height
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;
}
}
}

// -----------------------------------------------------------------------------
void ListWidget::updateIconScale()
{
CGUISTKListBox* list = getIrrlichtElement<CGUISTKListBox>();
assert(list != NULL);

if (item_height > 0)
// 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++)
{
list->setItemHeight( item_height );
const int h = rects[n].getHeight();
if (h > item_height) item_height = h;
}
}
else

if (item_height > 0)
{
list->setSpriteBank(NULL);
list->setItemHeight(item_height);
}

}

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -699,4 +705,6 @@ void ListWidget::resize()
if (m_element)
m_element->setRelativePosition(getListBoxSize());
updateHeader();
if (m_use_icons)
updateIconScale();
}
5 changes: 4 additions & 1 deletion src/guiengine/widgets/list_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ namespace GUIEngine

bool m_header_created;

float m_icon_scale;

void repairSortCol()
{
// Exclude scrollbar
Expand All @@ -109,6 +111,7 @@ namespace GUIEngine
}

void updateHeader();
void updateIconScale();
int getHeaderHeight() const;
irr::core::rect<s32> getListBoxSize() const;

Expand Down Expand Up @@ -140,7 +143,7 @@ namespace GUIEngine
* 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()
*/
void setIcons(irr::gui::STKModifiedSpriteBank* icons, int size=-1);
void setIcons(irr::gui::STKModifiedSpriteBank* icons, float scale = -1.0f);


// ---- contents management
Expand Down
7 changes: 3 additions & 4 deletions src/states_screens/addons_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,10 @@ void AddonsScreen::init()
getWidget<GUIEngine::ListWidget>("list_addons");

// 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
m_icon_bank->setScale((float)GUIEngine::getFontHeight() / 72.0f);
m_icon_bank->setTargetIconSize(128,128);
w_list->setIcons(m_icon_bank, (int)(m_icon_height));
m_icon_bank->setTargetIconSize(128, 128);
w_list->setIcons(m_icon_bank, 2.0f);

m_type = "kart";

Expand Down
2 changes: 0 additions & 2 deletions src/states_screens/addons_screen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class AddonsScreen : public GUIEngine::Screen,
* addons_loading is being displayed. */
int m_selected_index;

float m_icon_height;

bool m_reloading;

bool m_sort_desc;
Expand Down
3 changes: 1 addition & 2 deletions src/states_screens/dialogs/ghost_replay_info_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ GhostReplayInfoDialog::GhostReplayInfoDialog(unsigned int replay_id,

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

updateReplayDisplayedInfo();

Expand Down
5 changes: 2 additions & 3 deletions src/states_screens/dialogs/high_score_info_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ HighScoreInfoDialog::HighScoreInfoDialog(Highscores* highscore, bool is_linear,

/* Used to display kart icons for the entries */
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);
m_high_score_list->setIcons(icon_bank, (int)icon_height);
m_high_score_list->setIcons(icon_bank, 1.5f);

updateHighscoreEntries();

Expand Down
4 changes: 2 additions & 2 deletions src/states_screens/edit_gp_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ void EditGPScreen::loadList(const int selected)
m_list->clear();
m_icons.clear();
m_icon_bank->clear();
m_icon_bank->scaleToHeight (GUIEngine::getFontHeight() * 3 / 2);
m_list->setIcons(m_icon_bank, GUIEngine::getFontHeight() * 3 / 2);
m_icon_bank->setFixedScale(1.5f);
m_list->setIcons(m_icon_bank, 1.5f);

for (unsigned int i = 0; i < m_gp->getNumberOfTracks(true); i++)
{
Expand Down
9 changes: 3 additions & 6 deletions src/states_screens/ghost_replay_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,11 @@ void GhostReplaySelection::init()
{
Screen::init();
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
m_icon_bank->setScale(icon_height/128.0f);
m_icon_bank->setScale(1.0f / 128.0f);
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);
} // init
Expand Down
8 changes: 3 additions & 5 deletions src/states_screens/gp_info_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,9 @@ void GPInfoScreen::init()
getWidget<LabelWidget>("name")->setText(m_gp.getName(), false);
m_gp.checkConsistency();

int icon_height = GUIEngine::getFontHeight();
int row_height = GUIEngine::getFontHeight() * 1.2f;
m_icon_bank->setScale(icon_height/128.0f);
m_icon_bank->setTargetIconSize(128,128);
m_highscore_list->setIcons(m_icon_bank,row_height);
m_icon_bank->setScale(1.0f / 128.0f);
m_icon_bank->setTargetIconSize(128, 128);
m_highscore_list->setIcons(m_icon_bank, 1.2f);
RaceManager::get()->setNumKarts(RaceManager::get()->getNumLocalPlayers() + m_ai_kart_spinner->getValue());
// We don't save highscores for random gps so load highscores here
updateHighscores();
Expand Down
7 changes: 2 additions & 5 deletions src/states_screens/high_score_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,10 @@ void HighScoreSelection::init()
{
Screen::init();

int icon_height = GUIEngine::getFontHeight();
int row_height = GUIEngine::getFontHeight() * 5 / 4;

// 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_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);
} // init
Expand Down
2 changes: 1 addition & 1 deletion src/states_screens/online/networking_lobby.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void NetworkingLobby::loadedFromFile()
m_icon_bank->addTextureAsSprite(m_spectate_texture);
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);
} // loadedFromFile

Expand Down
6 changes: 2 additions & 4 deletions src/states_screens/online/server_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void ServerSelection::init()
m_searcher->clearListeners();
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);

video::ITexture* icon1 = irr_driver->getTexture(
Expand Down Expand Up @@ -204,10 +204,8 @@ void ServerSelection::init()
assert(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;
refresh();
m_ipv6_only_without_nat64 = false;
Expand Down
5 changes: 2 additions & 3 deletions src/states_screens/online/tracks_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,9 @@ void TracksScreen::init()
m_track_icons->addTextureAsSprite(tex);
}

int icon_height = getHeight() / 13;
m_track_icons->setScale(icon_height / 256.0f);
m_track_icons->setScale(1.0f / 128.0f);
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());
if (vote)
Expand Down
1 change: 0 additions & 1 deletion src/states_screens/options/options_screen_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "states_screens/options/options_screen_device.hpp"

#include "config/user_config.hpp"
#include "guiengine/CGUISpriteBank.hpp"
#include "guiengine/message_queue.hpp"
#include "guiengine/scalable_font.hpp"
#include "guiengine/screen.hpp"
Expand Down
4 changes: 1 addition & 3 deletions src/states_screens/options/options_screen_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ void OptionsScreenInput::loadedFromFile()
m_icon_bank->addTextureAsSprite(icon4);
m_icon_bank->addTextureAsSprite(icon5);

// scale icons depending on font height
const float scale = GUIEngine::getFontHeight() / 72.0f;
m_icon_bank->setScale(scale);
m_icon_bank->setScale(1.0f / 72.0f);
m_icon_bank->setTargetIconSize(128, 128);
m_gamepad_count = 0;
} // loadFromFile
Expand Down
7 changes: 2 additions & 5 deletions src/states_screens/track_info_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,9 @@ void TrackInfoScreen::init()

if (has_highscores)
{
int icon_height = GUIEngine::getFontHeight();
int row_height = GUIEngine::getFontHeight() * 1.2f;

m_icon_bank->setScale(icon_height/128.0f);
m_icon_bank->setScale(1.0f / 128.0f);
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);

updateHighScores();
Expand Down

0 comments on commit db2cc69

Please sign in to comment.