Fixed absolute vs relative paths for images, fixing lots of warnings printed on the terminal

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@4702 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2010-02-13 01:44:07 +00:00
parent 512326776e
commit 6d4078d3c7
8 changed files with 93 additions and 40 deletions

View File

@ -116,8 +116,9 @@ gives text (a label) to the widget where supported. Ribbon-grids give a special
to this parameter, see ribbon-grid docs above.
PROP_ICON "icon"
give an icon to the widget. Property contents is the path to the file, relative
relative to the /data directory of STK.
give an icon to the widget. Property contents is the path to the file, by default relative
relative to the /data directory of STK (several methods of IconButtonWidget and DynamicRibbon
can enable you to use absolute paths if you wish).
PROP_TEXT_ALIGN "text_align"
used exclusively by label components. Value can be "right" or "center" (left used if not specified).

View File

@ -286,7 +286,8 @@ void DynamicRibbonWidget::setSubElements()
}
// -----------------------------------------------------------------------------
void DynamicRibbonWidget::addItem( const irr::core::stringw& user_name, const std::string& code_name,
const std::string& image_file, const unsigned int badges )
const std::string& image_file, const unsigned int badges,
IconButtonWidget::IconPathType image_path_type)
{
ItemDescription desc;
desc.m_user_name = user_name;
@ -294,6 +295,7 @@ void DynamicRibbonWidget::addItem( const irr::core::stringw& user_name, const st
desc.m_sshot_file = image_file;
desc.m_badges = badges;
desc.m_animated = false;
desc.m_image_path_type = image_path_type;
m_items.push_back(desc);
}
@ -302,7 +304,7 @@ void DynamicRibbonWidget::addItem( const irr::core::stringw& user_name, const st
void DynamicRibbonWidget::addAnimatedItem( const irr::core::stringw& user_name, const std::string& code_name,
const std::vector<std::string>& image_files, const float time_per_frame,
const unsigned int badges )
const unsigned int badges, IconButtonWidget::IconPathType image_path_type )
{
ItemDescription desc;
desc.m_user_name = user_name;
@ -312,6 +314,7 @@ void DynamicRibbonWidget::addAnimatedItem( const irr::core::stringw& user_name,
desc.m_animated = true;
desc.m_curr_time = 0.0f;
desc.m_time_per_frame = time_per_frame;
desc.m_image_path_type = image_path_type;
m_items.push_back(desc);
@ -679,7 +682,7 @@ void DynamicRibbonWidget::updateItemDisplay()
std::string item_icon = (m_items[icon_id].m_animated ?
m_items[icon_id].m_all_images[0] :
m_items[icon_id].m_sshot_file);
icon->setImage( item_icon.c_str() );
icon->setImage( item_icon.c_str(), m_items[icon_id].m_image_path_type );
icon->m_properties[PROP_ID] = m_items[icon_id].m_code_name;
icon->setLabel(m_items[icon_id].m_user_name);

View File

@ -49,6 +49,7 @@ namespace GUIEngine
irr::core::stringw m_user_name;
std::string m_code_name;
std::string m_sshot_file;
IconButtonWidget::IconPathType m_image_path_type;
bool m_animated;
/** used instead of 'm_sshot_file' if m_animated is true */
@ -167,27 +168,31 @@ namespace GUIEngine
/** Dynamically add an item to the ribbon's list of items (will not be visible until you
* call 'updateItemDisplay' or 'add').
*
* \param user_name The name that will shown to the user (may be translated)
* \param code_name The non-translated internal name used to uniquely identify this item.
* \param image_file A path to a texture that will the icon of this item (path relative to data dir, just like PROP_ICON)
* \param badge Whether to add badges to this item (bitmask, see possible values in widget.hpp)
* \param user_name The name that will shown to the user (may be translated)
* \param code_name The non-translated internal name used to uniquely identify this item.
* \param image_file A path to a texture that will the icon of this item (path relative to data dir, just like PROP_ICON)
* \param badge Whether to add badges to this item (bitmask, see possible values in widget.hpp)
* \param image_path_type How to interpret the path to the image (absolute or relative)
*/
void addItem( const irr::core::stringw& user_name, const std::string& code_name,
const std::string& image_file, const unsigned int badge=0 );
const std::string& image_file, const unsigned int badge=0,
IconButtonWidget::IconPathType image_path_type=IconButtonWidget::ICON_PATH_TYPE_RELATIVE);
/** Dynamically add an animated item to the ribbon's list of items (will not be visible until you
* call 'updateItemDisplay' or 'add'). Animated means it has many images that will be shown in
* a slideshown fashion.
*
* \param user_name The name that will shown to the user (may be translated)
* \param code_name The non-translated internal name used to uniquely identify this item.
* \param image_files A path to a texture that will the icon of this item (path relative to data dir, just like PROP_ICON)
* \param user_name The name that will shown to the user (may be translated)
* \param code_name The non-translated internal name used to uniquely identify this item.
* \param image_files A path to a texture that will the icon of this item (path relative to data dir, just like PROP_ICON)
* \param time_per_frame Time (in seconds) to spend at each image.
* \param badge Whether to add badges to this item (bitmask, see possible values in widget.hpp)
* \param badge Whether to add badges to this item (bitmask, see possible values in widget.hpp)
* \param image_path_type How to interpret the path to the image (absolute or relative)
*/
void addAnimatedItem( const irr::core::stringw& user_name, const std::string& code_name,
const std::vector<std::string>& image_files, const float time_per_frame,
const unsigned int badge=0 );
const unsigned int badge=0,
IconButtonWidget::IconPathType image_path_type=IconButtonWidget::ICON_PATH_TYPE_RELATIVE);
/** Clears all items added through 'addItem'. You can then add new items with 'addItem' and call
'updateItemDisplay' to update the display. */

View File

@ -24,7 +24,8 @@ using namespace irr::core;
using namespace irr::gui;
// -----------------------------------------------------------------------------
IconButtonWidget::IconButtonWidget(ScaleMode scale_mode, const bool tab_stop, const bool focusable)
IconButtonWidget::IconButtonWidget(ScaleMode scale_mode, const bool tab_stop,
const bool focusable, IconPathType pathType)
{
m_label = NULL;
m_texture = NULL;
@ -34,12 +35,22 @@ IconButtonWidget::IconButtonWidget(ScaleMode scale_mode, const bool tab_stop, co
m_tab_stop = tab_stop;
m_focusable = focusable;
m_scale_mode = scale_mode;
m_icon_path_type = pathType;
}
// -----------------------------------------------------------------------------
void IconButtonWidget::add()
{
// ---- Icon
m_texture = irr_driver->getTexture((file_manager->getDataDir() + "/" +m_properties[PROP_ICON]).c_str());
if (m_icon_path_type == ICON_PATH_TYPE_ABSOLUTE)
{
m_texture = irr_driver->getTexture(m_properties[PROP_ICON].c_str());
}
else if (m_icon_path_type == ICON_PATH_TYPE_RELATIVE)
{
m_texture = irr_driver->getTexture((file_manager->getDataDir() + "/" +m_properties[PROP_ICON]).c_str());
}
assert(m_texture != NULL);
m_texture_w = m_texture->getSize().Width;
m_texture_h = m_texture->getSize().Height;
@ -106,21 +117,28 @@ void IconButtonWidget::add()
/** \precondition At the moment, the new texture must have the same aspct ratio as the previous one since the object will not
* be modified to fit a different aspect ratio
*/
void IconButtonWidget::setImage(const char* path_to_texture)
void IconButtonWidget::setImage(const char* path_to_texture, IconPathType pathType)
{
m_properties[PROP_ICON] = path_to_texture;
m_texture = irr_driver->getTexture((file_manager->getDataDir() + "/" + m_properties[PROP_ICON]).c_str());
if (m_texture == NULL)
if (pathType != ICON_PATH_TYPE_NO_CHANGE)
{
m_icon_path_type = pathType;
}
m_properties[PROP_ICON] = path_to_texture;
if (m_icon_path_type == ICON_PATH_TYPE_ABSOLUTE)
{
// texture not found, try with absolute path
m_texture = irr_driver->getTexture(m_properties[PROP_ICON].c_str());
}
if(!m_texture)
else if (m_icon_path_type == ICON_PATH_TYPE_RELATIVE)
{
fprintf(stderr, "Texture '%s' not found - aborting.\n",
m_properties[PROP_ICON].c_str());
exit(-1);
m_texture = irr_driver->getTexture((file_manager->getDataDir() + "/" + m_properties[PROP_ICON]).c_str());
}
if (!m_texture)
{
fprintf(stderr, "Texture '%s' not found!\n", m_properties[PROP_ICON].c_str());
m_texture = irr_driver->getTexture((file_manager->getDataDir() + "/gui/main_help.png").c_str());
}
m_texture_w = m_texture->getSize().Width;

View File

@ -38,8 +38,20 @@ namespace GUIEngine
SCALE_MODE_KEEP_TEXTURE_ASPECT_RATIO,
SCALE_MODE_KEEP_CUSTOM_ASPECT_RATIO
};
enum IconPathType
{
ICON_PATH_TYPE_ABSOLUTE,
/** relative to the data dir */
ICON_PATH_TYPE_RELATIVE,
/** not a valid value per se, but can be passed as argument to leave
* the path type as it currently is */
ICON_PATH_TYPE_NO_CHANGE
};
protected:
IconPathType m_icon_path_type;
friend class Skin;
irr::gui::IGUIStaticText* m_label;
@ -54,7 +66,8 @@ namespace GUIEngine
/** Whether to make the widget included in keyboard navigation order when adding */
bool m_tab_stop;
IconButtonWidget(ScaleMode scale_mode=SCALE_MODE_KEEP_TEXTURE_ASPECT_RATIO, const bool tab_stop=true, const bool focusable=true);
IconButtonWidget(ScaleMode scale_mode=SCALE_MODE_KEEP_TEXTURE_ASPECT_RATIO, const bool tab_stop=true,
const bool focusable=true, IconPathType pathType=ICON_PATH_TYPE_RELATIVE);
virtual ~IconButtonWidget() {}
/** Callback called when this widget needs to be added (see base class Widget) */
@ -68,7 +81,7 @@ namespace GUIEngine
void setLabel(irr::core::stringw new_label);
/** Change the texture used for this icon. The path is relative to the data directory, just like PROP_ICON. */
void setImage(const char* path_to_texture);
void setImage(const char* path_to_texture, IconPathType pathType=ICON_PATH_TYPE_NO_CHANGE);
/** Change the texture used for this icon. */
void setImage(irr::video::ITexture* texture);

View File

@ -19,6 +19,7 @@
#include "guiengine/engine.hpp"
#include "guiengine/screen.hpp"
#include "guiengine/widget.hpp"
#include "io/file_manager.hpp"
#include "race/grand_prix_manager.hpp"
#include "race/race_manager.hpp"
#include "states_screens/dialogs/gp_info_dialog.hpp"
@ -107,8 +108,10 @@ GPInfoDialog::GPInfoDialog(const std::string& gpIdent, const float w, const floa
}
// ---- Track screenshot
m_screenshot_widget = new IconButtonWidget(IconButtonWidget::SCALE_MODE_KEEP_CUSTOM_ASPECT_RATIO,
false, false);
false /* tab stop */, false /* focusable */,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE /* Track gives us absolute paths */);
// images are saved squared, but must be stretched to 4:3
m_screenshot_widget->setCustomAspectRatio(4.0f / 3.0f);
@ -119,12 +122,11 @@ GPInfoDialog::GPInfoDialog(const std::string& gpIdent, const float w, const floa
Track* track = track_manager->getTrack(tracks[0]);
// temporary icon, will replace it just after adding (hack to support absolute paths [FIXME])
m_screenshot_widget->m_properties[PROP_ICON] = "gui/main_help.png";
m_screenshot_widget->m_properties[PROP_ICON] = (track != NULL ?
track->getScreenshotFile().c_str() :
file_manager->getDataDir() + "gui/main_help.png");
m_screenshot_widget->setParent(m_irrlicht_window);
m_screenshot_widget->add();
m_screenshot_widget->setImage(track != NULL ? track->getScreenshotFile().c_str() : "gui/main_help.png");
m_children.push_back(m_screenshot_widget);
@ -228,7 +230,9 @@ void GPInfoDialog::onUpdate(float dt)
}
Track* track = track_manager->getTrack(tracks[frameAfter]);
m_screenshot_widget->setImage(track != NULL ? track->getScreenshotFile().c_str() : "gui/main_help.png");
m_screenshot_widget->setImage((track != NULL ? track->getScreenshotFile().c_str() :
(file_manager->getDataDir()+"gui/main_help.png").c_str()),
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);
}
// ------------------------------------------------------------------------------------------------------

View File

@ -169,11 +169,13 @@ void TracksScreen::init()
if (unlock_manager->isLocked(gp->getId()))
{
gps_widget->addAnimatedItem( _("Locked : solve active challenges to gain access to more!"),
"locked", sshot_files, 1.5f, TROPHY_BADGE );
"locked", sshot_files, 1.5f, TROPHY_BADGE,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);
}
else
{
gps_widget->addAnimatedItem( gp->getName(), gp->getId(), sshot_files, 1.5f, TROPHY_BADGE );
gps_widget->addAnimatedItem( gp->getName(), gp->getId(), sshot_files, 1.5f, TROPHY_BADGE,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE );
}
}
gps_widget->updateItemDisplay();
@ -190,14 +192,18 @@ void TracksScreen::init()
if (unlock_manager->isLocked(curr->getIdent()))
{
tracks_widget->addItem( _("Locked : solve active challenges to gain access to more!"), "locked", curr->getScreenshotFile(), true );
tracks_widget->addItem( _("Locked : solve active challenges to gain access to more!"),
"locked", curr->getScreenshotFile(), true,
IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE);
}
else
{
tracks_widget->addItem( curr->getName(), curr->getIdent(), curr->getScreenshotFile(), false );
tracks_widget->addItem( curr->getName(), curr->getIdent(), curr->getScreenshotFile(),
false, IconButtonWidget::ICON_PATH_TYPE_ABSOLUTE );
}
}
tracks_widget->addItem(_("Random Track"), "random_track", "/gui/track_random.png");
tracks_widget->addItem(_("Random Track"), "random_track", "/gui/track_random.png",
IconButtonWidget::ICON_PATH_TYPE_RELATIVE);
tracks_widget->updateItemDisplay();
}

View File

@ -215,7 +215,10 @@ public:
const std::string& getDescription () const {return m_description; }
const std::string& getDesigner () const {return m_designer; }
/** Returns an absolute path to the screenshot file of this track */
const std::string& getScreenshotFile () const {return m_screenshot; }
const std::string& getItemStyle () const {return m_item_style; }
bool hasFinalCamera () const {return m_has_final_camera; }
const Vec3& getCameraPosition () const {return m_camera_final_position;}