Use exceptions for error handling

0.8
Bruno Van de Velde 2014-04-09 22:27:18 +02:00
parent 23bf4f3110
commit 7db1147c38
67 changed files with 1099 additions and 2506 deletions

0
examples/xubuntu_bg_aluminium.jpg Executable file → Normal file
View File

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View File

@ -91,10 +91,10 @@ namespace tgui
/// \param frameDuration The amount of time that the frame will be displayed on the screen.
/// When the duration is 0 (default) then the animation will be blocked at that frame.
///
/// \return True when the frame was loaded.
/// \throw Exception when the image couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool addFrame(const std::string& filename, sf::Time frameDuration = sf::Time());
void addFrame(const std::string& filename, sf::Time frameDuration = sf::Time());
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -268,14 +268,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -77,6 +77,12 @@ namespace tgui
{
public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Destructor
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual ~WidgetBorders() {}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Changes the size of the borders.
///

View File

@ -91,10 +91,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a Button section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "Button" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -248,14 +250,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -91,10 +91,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a ChatBox section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "ChatBox" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -383,12 +385,12 @@ namespace tgui
/// \param scrollbarConfigFileFilename Filename of the config file.
/// The config file must contain a Scrollbar section with the needed information.
///
/// \return
/// - true when the scrollbar was successfully loaded
/// - false when the loading of the scrollbar failed
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "Scrollbar" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool setScrollbar(const std::string& scrollbarConfigFileFilename);
void setScrollbar(const std::string& scrollbarConfigFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -454,14 +456,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -62,10 +62,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a Checkbox section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "Checkbox" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool load(const std::string& configFileFilename);
virtual void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -106,10 +106,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a ChildWindow section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "ChildWindow" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool load(const std::string& configFileFilename);
virtual void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -471,14 +473,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -124,14 +124,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -89,10 +89,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a ComboBox section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "ComboBox" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -556,14 +558,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -44,62 +44,45 @@ namespace tgui
public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Destructor. Closes the file if still open.
// Cpen and read the properties inside a section of the given file.
// An exception will be thrown when the file could not be opened or when the section could not be read.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
~ConfigFile();
ConfigFile(const std::string& filename, const std::string& section);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Open a given file. If a file is already open then it will be closed first.
//
// return: true when the file was opened
// false when it couldn't be opened
// Returns the list of properties and their values.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool open(const std::string& filename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Reads a specific section from the file and returns the corresponding properties and values.
//
// return: true when a section was found
// false when there was no section with the given name
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool read(std::string section, std::vector<std::string>& properties, std::vector<std::string>& values);
const std::vector<std::pair<std::string, std::string>>& getProperties() const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Reads a bool from a string value.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool readBool(const std::string& value, bool defaultValue) const;
bool readBool(std::vector<std::pair<std::string, std::string>>::const_iterator it) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Reads a color from a string value.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Color readColor(const std::string& value) const;
sf::Color readColor(std::vector<std::pair<std::string, std::string>>::const_iterator it) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Loads a texture based on the contents of the string value.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void readTexture(std::vector<std::pair<std::string, std::string>>::const_iterator it, const std::string& rootPath, Texture& texture) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Reads an int rectangle from a string value.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool readIntRect(std::string value, sf::IntRect& rect) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Loads a texture based on the contents of the string value.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool readTexture(const std::string& value, const std::string& rootPath, Texture& texture) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Close the file that was opened by the open function.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void close();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Remove the whitespace from the line, starting from the character c.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -121,7 +104,9 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:
std::ifstream m_file;
std::string m_filename;
std::string m_section;
std::vector<std::pair<std::string, std::string>> m_properties;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -409,7 +409,7 @@ namespace tgui
/// - false when one of the widgets couldn't be loaded
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool loadWidgetsFromFile(const std::string& filename);
void loadWidgetsFromFile(const std::string& filename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -422,7 +422,7 @@ namespace tgui
/// - false when the file couldn't be created
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool saveWidgetsToFile(const std::string& filename);
void saveWidgetsToFile(const std::string& filename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -35,15 +35,12 @@
#include <TGUI/Config.hpp>
#include <TGUI/Borders.hpp>
#include <TGUI/Exception.hpp>
#include <TGUI/TextureManager.hpp>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef TGUI_OUTPUT
#define TGUI_OUTPUT(x) sf::err() << x << std::endl;
#endif
#define TGUI_MINIMUM(x, y) (x < y) ? x : y
#define TGUI_MAXIMUM(x, y) (x > y) ? x : y

View File

@ -107,10 +107,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a EditBox section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "EditBox" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -474,14 +476,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,53 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus's Graphical User Interface
// Copyright (C) 2012-2014 Bruno Van de Velde (vdv_b@tgui.eu)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef TGUI_EXCEPTION_HPP
#define TGUI_EXCEPTION_HPP
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <string>
#include <stdexcept>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class TGUI_API Exception : public std::runtime_error {
public:
explicit Exception(const std::string& argument) :
std::runtime_error(argument)
{
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif // TGUI_EXCEPTION_HPP

View File

@ -442,14 +442,12 @@ namespace tgui
///
/// \param filename Filename of the widget file that is to be loaded
///
/// \return
/// - true on success
/// - false when the file could not be opened
/// - false when the file contains a mistake
/// - false when one of the widgets couldn't be loaded
/// \throw Exception when the file could not be opened
/// \throw Exception when the file contains a mistake
/// \throw Exception when one of the widgets, described in the file, couldn't be loaded
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool loadWidgetsFromFile(const std::string& filename);
void loadWidgetsFromFile(const std::string& filename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -457,12 +455,10 @@ namespace tgui
///
/// \param filename Filename of the widget file that is to be created
///
/// \return
/// - true on success
/// - false when the file couldn't be created
/// \throw Exception when the file couldn't be created
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool saveWidgetsToFile(const std::string& filename);
void saveWidgetsToFile(const std::string& filename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -89,10 +89,12 @@ namespace tgui
///
/// @param configFileFilename Filename of the config file.
///
/// The config file must contain a Knob section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "Knob" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -310,14 +312,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -68,10 +68,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a Label section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "Label" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -261,14 +263,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -90,10 +90,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a ListBox section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "ListBox" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -564,14 +566,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -88,10 +88,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a LoadingBar section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "Loadingbar" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -290,14 +292,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -61,10 +61,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a MenuBar section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "MenuBar" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -359,14 +361,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -90,10 +90,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a MessageBox section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "MessageBox" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -193,14 +195,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -183,14 +183,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -94,7 +94,7 @@ namespace tgui
/// - false when the image couldn't be loaded (probably not found)
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& filename);
void load(const std::string& filename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -184,14 +184,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -88,10 +88,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a RadioButton section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "RadioButton" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
virtual void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -302,14 +304,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -88,10 +88,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a Scrollbar section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "Scrollbar" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -314,14 +316,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -26,11 +26,11 @@
#ifndef TGUI_SHARED_WIDGET_PTR_INL
#define TGUI_SHARED_WIDGET_PTR_INL
#include <cassert>
#include <TGUI/Container.hpp>
#include <TGUI/Gui.hpp>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace tgui

View File

@ -88,10 +88,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a Slider section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "Slider" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -278,14 +280,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -88,10 +88,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a Slider2d section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "Slider2d" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -270,14 +272,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -88,10 +88,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a SpinButton section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "SpinButton" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -240,14 +242,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -183,14 +183,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -88,10 +88,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a Tab section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "Tab" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -416,14 +418,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -90,10 +90,12 @@ namespace tgui
///
/// \param configFileFilename Filename of the config file.
///
/// The config file must contain a TextBox section with the needed information.
/// \throw Exception when the config file couldn't be opened.
/// \throw Exception when the config file didn't contain a "TextBox" section with the needed information.
/// \throw Exception when one of the images, described in the config file, couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool load(const std::string& configFileFilename);
void load(const std::string& configFileFilename);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -506,14 +508,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -66,8 +66,10 @@ namespace tgui
///
/// The second time you call this function with the same filename, the previously loaded image will be reused.
///
/// \throw Exception when image couldn't be loaded.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool getTexture(Texture& texture,
void getTexture(Texture& texture,
const std::string& filename,
const sf::IntRect& partRect = sf::IntRect(0, 0, 0, 0),
const sf::IntRect& middlePart = sf::IntRect(0, 0, 0, 0),

View File

@ -164,17 +164,6 @@ namespace tgui
bool isDisabled() const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Returns true when the widget is loaded successfully.
///
/// \return Is the widget loaded?
///
/// When a widget is not loaded correctly then it will not be drawn.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool isLoaded() const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \brief Focus the widget.
///
@ -373,14 +362,14 @@ namespace tgui
// This function is a (slow) way to set properties on the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool setProperty(std::string property, const std::string& value);
virtual void setProperty(std::string property, const std::string& value);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// \internal
// This function is a (slow) way to get properties of the widget, no matter what type it is.
// When the requested property doesn't exist in the widget then the functions will return false.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
virtual bool getProperty(std::string property, std::string& value) const;
virtual void getProperty(std::string property, std::string& value) const;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -426,9 +415,6 @@ namespace tgui
// Is the widget visible? When it is invisible it will not receive events and it won't be drawn.
bool m_visible;
// This bool will be true from the moment that the load function is completed successfully.
bool m_loaded;
// This will store the different phases that the widget can have
// e.g. if there isn't a mouse down image then a button should not try to change its image on mouse down
unsigned char m_widgetPhase;

View File

@ -63,7 +63,7 @@ namespace tgui
AnimatedPicture::~AnimatedPicture()
{
// Remove the textures (if we are the only one using it)
for (unsigned int i=0; i< m_textures.size(); ++i)
for (unsigned int i = 0; i < m_textures.size(); ++i)
TGUI_TextureManager.removeTexture(m_textures[i]);
// Clear the vectors
@ -82,7 +82,7 @@ namespace tgui
this->ClickableWidget::operator=(right);
// If there already were frames then remove them now
for (unsigned int i=0; i< m_textures.size(); ++i)
for (unsigned int i = 0; i < m_textures.size(); ++i)
TGUI_TextureManager.removeTexture(m_textures[i]);
std::swap(m_textures, temp.m_textures);
@ -104,38 +104,25 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool AnimatedPicture::addFrame(const std::string& filename, sf::Time frameDuration)
void AnimatedPicture::addFrame(const std::string& filename, sf::Time frameDuration)
{
// Check if the filename is empty
if (filename.empty())
return false;
Texture tempTexture;
// Try to load the texture from the file
if (TGUI_TextureManager.getTexture(tempTexture, getResourcePath() + filename))
Texture tempTexture;
TGUI_TextureManager.getTexture(tempTexture, getResourcePath() + filename);
// If this is the first frame then set it as the current displayed frame
if (m_textures.empty())
{
// If this is the first frame then set it as the current displayed frame
if (m_textures.empty())
{
m_currentFrame = 0;
m_currentFrame = 0;
// Remember the size of this image
m_size = sf::Vector2f(tempTexture.getSize());
}
// Add the texture
tempTexture.setColor(sf::Color(255, 255, 255, m_opacity));
m_textures.push_back(tempTexture);
// Store the frame duration
m_frameDuration.push_back(frameDuration);
// Return true to indicate that nothing went wrong
return m_loaded = true;
// Remember the size of this image
m_size = sf::Vector2f(tempTexture.getSize());
}
else // The texture was not loaded
return m_loaded = false;
// Add the texture and store its duration
tempTexture.setColor(sf::Color(255, 255, 255, m_opacity));
m_textures.push_back(tempTexture);
m_frameDuration.push_back(frameDuration);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -228,10 +215,7 @@ namespace tgui
if (!m_frameDuration.empty())
return m_frameDuration[m_currentFrame];
else
{
TGUI_OUTPUT("TGUI warning: Can't get duration of current frame: no frames available.");
return sf::Time();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -258,10 +242,6 @@ namespace tgui
if (m_currentFrame >= static_cast<int>(frame))
--m_currentFrame;
// Check if you are removing the last frame
if (m_textures.size() == 1)
m_loaded = false;
return true;
}
@ -270,7 +250,7 @@ namespace tgui
void AnimatedPicture::removeAllFrames()
{
// Remove the textures (if we are the only one using it)
for (unsigned int i=0; i< m_textures.size(); ++i)
for (unsigned int i = 0; i< m_textures.size(); ++i)
TGUI_TextureManager.removeTexture(m_textures[i]);
// Clear the vectors
@ -279,7 +259,6 @@ namespace tgui
// Reset the animation
stop();
m_loaded = false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -315,7 +294,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool AnimatedPicture::setProperty(std::string property, const std::string& value)
void AnimatedPicture::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -326,7 +305,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
m_playing = false;
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Playing' property.");
throw Exception("Failed to parse 'Playing' property.");
}
else if (property == "looping")
{
@ -335,7 +314,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
m_looping = false;
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Looping' property.");
throw Exception("Failed to parse 'Looping' property.");
}
else if (property == "callback")
{
@ -351,15 +330,12 @@ namespace tgui
}
}
else // The property didn't match
return ClickableWidget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool AnimatedPicture::getProperty(std::string property, std::string& value) const
void AnimatedPicture::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -385,10 +361,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return ClickableWidget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -451,7 +424,7 @@ namespace tgui
void AnimatedPicture::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if (m_loaded)
if (!m_textures.empty())
{
states.transform *= getTransform();
states.transform.scale(m_size.x / m_textures[m_currentFrame].getSize().x, m_size.y / m_textures[m_currentFrame].getSize().y);

View File

@ -27,7 +27,6 @@
#include <TGUI/Button.hpp>
#include <cmath>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -100,13 +99,10 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Button::load(const std::string& configFileFilename)
void Button::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// If the button was loaded before then remove the old textures first
if (m_textureNormal.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureNormal);
if (m_textureHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureHover);
@ -114,24 +110,7 @@ namespace tgui
if (m_textureFocused.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureFocused);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("Button", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "Button");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -140,74 +119,33 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "separatehoverimage")
{
m_separateHoverImage = configFile.readBool(value, false);
}
else if (property == "textcolor")
{
m_text.setColor(configFile.readColor(value));
}
else if (property == "normalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for NormalImage in section Button in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "hoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for HoverImage in section Button in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "downimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureDown))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for DownImage in section Button in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "focusedimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureFocused))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for FocusedImage in section Button in " + m_loadedConfigFile + ".");
return false;
}
}
if (it->first == "separatehoverimage")
m_separateHoverImage = configFile.readBool(it);
else if (it->first == "textcolor")
m_text.setColor(configFile.readColor(it));
else if (it->first == "normalimage")
configFile.readTexture(it, configFileFolder, m_textureNormal);
else if (it->first == "hoverimage")
configFile.readTexture(it, configFileFolder, m_textureHover);
else if (it->first == "downimage")
configFile.readTexture(it, configFileFolder, m_textureDown);
else if (it->first == "focusedimage")
configFile.readTexture(it, configFileFolder, m_textureFocused);
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section Button in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section Button in " + m_loadedConfigFile + ".");
}
// Make sure the required texture was loaded
if (m_textureNormal.getData() != nullptr)
{
m_loaded = true;
setSize(m_textureNormal.getImageSize().x, m_textureNormal.getImageSize().y);
}
else
{
TGUI_OUTPUT("TGUI error: NormalImage wasn't loaded. Is the Button section in " + m_loadedConfigFile + " complete?");
return false;
}
if (m_textureNormal.getData() == nullptr)
throw Exception("NormalImage wasn't loaded. Is the Button section in " + m_loadedConfigFile + " complete?");
// Check if optional textures were loaded
setSize(m_textureNormal.getImageSize().x, m_textureNormal.getImageSize().y);
// The widget can only be focused when there is an image available for this phase
if (m_textureFocused.getData() != nullptr)
{
m_allowFocus = true;
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -237,10 +175,6 @@ namespace tgui
void Button::setSize(float width, float height)
{
// Don't do anything when the button wasn't loaded correctly
if (m_loaded == false)
return;
m_textureDown.setSize(width, height);
m_textureHover.setSize(width, height);
m_textureNormal.setSize(width, height);
@ -265,10 +199,6 @@ namespace tgui
void Button::setText(const sf::String& text)
{
// Don't do anything when the button wasn't loaded correctly
if (m_loaded == false)
return;
// Set the new text
m_text.setString(text);
m_callback.text = text;
@ -402,7 +332,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Button::setProperty(std::string property, const std::string& value)
void Button::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -438,15 +368,12 @@ namespace tgui
}
}
else // The property didn't match
return ClickableWidget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Button::getProperty(std::string property, std::string& value) const
void Button::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -478,10 +405,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return ClickableWidget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -510,9 +434,6 @@ namespace tgui
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if (!m_loaded)
return;
if (m_separateHoverImage)
{
if (m_mouseDown && m_mouseHover && m_textureDown.getData())

View File

@ -33,7 +33,6 @@ namespace tgui
Canvas::Canvas()
{
m_loaded = true;
m_callback.widgetType = Type_Canvas;
setSize(100, 100);

View File

@ -53,9 +53,6 @@ namespace tgui
m_panel = new Panel();
m_panel->setSize(360, 200);
m_panel->setBackgroundColor(sf::Color::White);
// Load the chat box with default values
m_loaded = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -130,13 +127,10 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ChatBox::load(const std::string& configFileFilename)
void ChatBox::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// If there already was a scrollbar then delete it now
if (m_scroll != nullptr)
{
@ -145,24 +139,7 @@ namespace tgui
}
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("ChatBox", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "ChatBox");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -171,57 +148,50 @@ namespace tgui
configFileFolder = configFileFilename.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "backgroundcolor")
if (it->first == "backgroundcolor")
{
setBackgroundColor(configFile.readColor(value));
setBackgroundColor(configFile.readColor(it));
}
else if (property == "bordercolor")
else if (it->first == "bordercolor")
{
setBorderColor(configFile.readColor(value));
setBorderColor(configFile.readColor(it));
}
else if (property == "borders")
else if (it->first == "borders")
{
Borders borders;
if (extractBorders(value, borders))
if (extractBorders(it->second, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
throw Exception("Failed to parse the 'Borders' property in section ChatBox in " + m_loadedConfigFile);
}
else if (property == "scrollbar")
else if (it->first == "scrollbar")
{
if ((value.length() < 3) || (value[0] != '"') || (value[value.length()-1] != '"'))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for Scrollbar in section ChatBox in " + m_loadedConfigFile + ".");
return false;
}
if ((it->second.length() < 3) || (it->second[0] != '"') || (it->second[it->second.length()-1] != '"'))
throw Exception("Failed to parse value for Scrollbar property in section ChatBox in " + m_loadedConfigFile + ".");
// load the scrollbar and check if it failed
m_scroll = new Scrollbar();
if (m_scroll->load(configFileFolder + value.substr(1, value.length()-2)) == false)
try {
m_scroll->load(configFileFolder + it->second.substr(1, it->second.length()-2));
}
catch (Exception& e)
{
// The scrollbar couldn't be loaded so it must be deleted
delete m_scroll;
m_scroll = nullptr;
throw e;
}
return false;
}
else // The scrollbar was loaded successfully
{
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setLowValue(static_cast<unsigned int>(m_panel->getSize().y));
m_scroll->setSize(m_scroll->getSize().x, m_panel->getSize().y);
m_scroll->setMaximum(static_cast<unsigned int>(m_fullTextHeight));
}
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setLowValue(static_cast<unsigned int>(m_panel->getSize().y));
m_scroll->setSize(m_scroll->getSize().x, m_panel->getSize().y);
m_scroll->setMaximum(static_cast<unsigned int>(m_fullTextHeight));
}
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section ChatBox in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section ChatBox in " + m_loadedConfigFile + ".");
}
return m_loaded = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -235,10 +205,6 @@ namespace tgui
void ChatBox::setSize(float width, float height)
{
// Don't do anything when the text box wasn't loaded correctly
if (m_loaded == false)
return;
// A negative size is not allowed for this widget
if (width < 0) width = -width;
if (height < 0) height = -height;
@ -554,36 +520,29 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ChatBox::setScrollbar(const std::string& scrollbarConfigFileFilename)
void ChatBox::setScrollbar(const std::string& scrollbarConfigFileFilename)
{
// Do nothing when the string is empty
if (scrollbarConfigFileFilename.empty() == true)
return false;
// If the scrollbar was already created then delete it first
if (m_scroll != nullptr)
delete m_scroll;
// load the scrollbar and check if it failed
m_scroll = new Scrollbar();
if(m_scroll->load(scrollbarConfigFileFilename) == false)
try {
m_scroll->load(scrollbarConfigFileFilename);
}
catch (Exception& e)
{
// The scrollbar couldn't be loaded so it must be deleted
delete m_scroll;
m_scroll = nullptr;
return false;
throw e;
}
else // The scrollbar was loaded successfully
{
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setSize(m_scroll->getSize().x, m_panel->getSize().y);
m_scroll->setLowValue(static_cast<unsigned int>(m_panel->getSize().y));
m_scroll->setMaximum(static_cast<unsigned int>(m_fullTextHeight));
return true;
}
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setSize(m_scroll->getSize().x, m_panel->getSize().y);
m_scroll->setLowValue(static_cast<unsigned int>(m_panel->getSize().y));
m_scroll->setMaximum(static_cast<unsigned int>(m_fullTextHeight));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -644,10 +603,6 @@ namespace tgui
void ChatBox::leftMousePressed(float x, float y)
{
// Don't do anything when the text box wasn't loaded correctly
if (m_loaded == false)
return;
// Set the mouse down flag to true
m_mouseDown = true;
@ -824,7 +779,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ChatBox::setProperty(std::string property, const std::string& value)
void ChatBox::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -842,7 +797,7 @@ namespace tgui
if (extractBorders(value, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Borders' property.");
throw Exception("Failed to parse 'Borders' property.");
}
else if (property == "backgroundcolor")
{
@ -872,32 +827,29 @@ namespace tgui
std::string::size_type closeBracketPos = line.rfind(')');
if ((openBracketPos == std::string::npos) || (closeBracketPos == std::string::npos) || (openBracketPos >= closeBracketPos))
return false;
throw Exception("Could not decode Lines property: failed to match brackets.");
sf::Color color = extractColor(line.substr(openBracketPos, closeBracketPos - openBracketPos + 1));
std::string::size_type commaPos = line.rfind(',', openBracketPos);
if (commaPos == std::string::npos)
return false;
throw Exception("Could not decode Lines property: failed to find comma.");
line.erase(commaPos);
addLine(line, color);
}
else
return false;
throw Exception("Could not decode Lines property: failed to match brackets.");
}
}
else // The property didn't match
return Widget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ChatBox::getProperty(std::string property, std::string& value) const
void ChatBox::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -922,10 +874,7 @@ namespace tgui
encodeList(lines, value);
}
else // The property didn't match
return Widget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -48,13 +48,10 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Checkbox::load(const std::string& configFileFilename)
void Checkbox::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// If the checkbox was loaded before then remove the old textures
if (m_textureUnchecked.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureUnchecked);
if (m_textureChecked.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureChecked);
@ -62,24 +59,7 @@ namespace tgui
if (m_textureFocused.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureFocused);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("Checkbox", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "Checkbox");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -88,71 +68,31 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "textcolor")
{
m_text.setColor(configFile.readColor(value));
}
else if (property == "checkedimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureChecked))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for CheckedImage in section Checkbox in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "uncheckedimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureUnchecked))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for UncheckedImage in section Checkbox in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "hoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for HoverImage in section Checkbox in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "focusedimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureFocused))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for FocusedImage in section Checkbox in " + m_loadedConfigFile + ".");
return false;
}
}
if (it->first == "textcolor")
m_text.setColor(configFile.readColor(it));
else if (it->first == "checkedimage")
configFile.readTexture(it, configFileFolder, m_textureChecked);
else if (it->first == "uncheckedimage")
configFile.readTexture(it, configFileFolder, m_textureUnchecked);
else if (it->first == "hoverimage")
configFile.readTexture(it, configFileFolder, m_textureHover);
else if (it->first == "focusedimage")
configFile.readTexture(it, configFileFolder, m_textureFocused);
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section Checkbox in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section Checkbox in " + m_loadedConfigFile + ".");
}
// Make sure the required texture was loaded
if ((m_textureChecked.getData() != nullptr) && (m_textureUnchecked.getData() != nullptr))
{
m_loaded = true;
setSize(m_textureUnchecked.getImageSize().x, m_textureUnchecked.getImageSize().y);
}
else
{
TGUI_OUTPUT("TGUI error: Not all needed images were loaded for the checkbox. Is the Checkbox section in " + m_loadedConfigFile + " complete?");
return false;
}
if ((m_textureChecked.getData() == nullptr) || (m_textureUnchecked.getData() == nullptr))
throw Exception("Not all needed images were loaded for the checkbox. Is the Checkbox section in " + m_loadedConfigFile + " complete?");
// Check if optional textures were loaded
setSize(m_textureUnchecked.getImageSize().x, m_textureUnchecked.getImageSize().y);
// The widget can only be focused when there is an image available for this phase
if (m_textureFocused.getData() != nullptr)
{
m_allowFocus = true;
}
// When there is no error we will return true
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -30,7 +30,6 @@
#include <TGUI/ChildWindow.hpp>
#include <cmath>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -139,38 +138,18 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ChildWindow::load(const std::string& configFileFilename)
void ChildWindow::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// Until the loading succeeds, the child window will be marked as unloaded
m_loaded = false;
// Remove the textures when they were loaded before
if (m_textureTitleBar.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureTitleBar);
if (m_textureTitleBar.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureTitleBar);
if (m_closeButton->m_textureNormal.getData() != nullptr) TGUI_TextureManager.removeTexture(m_closeButton->m_textureNormal);
if (m_closeButton->m_textureHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_closeButton->m_textureHover);
if (m_closeButton->m_textureDown.getData() != nullptr) TGUI_TextureManager.removeTexture(m_closeButton->m_textureDown);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("ChildWindow", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "ChildWindow");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -179,104 +158,54 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "backgroundcolor")
{
setBackgroundColor(configFile.readColor(value));
}
else if (property == "titlecolor")
{
setTitleColor(configFile.readColor(value));
}
else if (property == "bordercolor")
{
setBorderColor(configFile.readColor(value));
}
else if (property == "titlebarimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureTitleBar))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for TitlebarImage in section ChildWindow in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "closebuttonseparatehoverimage")
{
m_closeButton->m_separateHoverImage = configFile.readBool(value, false);
}
else if (property == "closebuttonnormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_closeButton->m_textureNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for CloseButtonNormalImage in section Button in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "closebuttonhoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_closeButton->m_textureHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for CloseButtonHoverImage in section Button in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "closebuttondownimage")
{
if (!configFile.readTexture(value, configFileFolder, m_closeButton->m_textureDown))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for CloseButtonDownImage in section Button in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "borders")
if (it->first == "backgroundcolor")
setBackgroundColor(configFile.readColor(it));
else if (it->first == "titlecolor")
setTitleColor(configFile.readColor(it));
else if (it->first == "bordercolor")
setBorderColor(configFile.readColor(it));
else if (it->first == "titlebarimage")
configFile.readTexture(it, configFileFolder, m_textureTitleBar);
else if (it->first == "closebuttonseparatehoverimage")
m_closeButton->m_separateHoverImage = configFile.readBool(it);
else if (it->first == "closebuttonnormalimage")
configFile.readTexture(it, configFileFolder, m_closeButton->m_textureNormal);
else if (it->first == "closebuttonhoverimage")
configFile.readTexture(it, configFileFolder, m_closeButton->m_textureHover);
else if (it->first == "closebuttondownimage")
configFile.readTexture(it, configFileFolder, m_closeButton->m_textureDown);
else if (it->first == "borders")
{
Borders borders;
if (extractBorders(value, borders))
if (extractBorders(it->second, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
throw Exception("Failed to parse the 'Borders' property in section ChildWindow in " + m_loadedConfigFile);
}
else if (property == "distancetoside")
{
setDistanceToSide(tgui::stoul(value));
}
else if (it->first == "distancetoside")
setDistanceToSide(tgui::stoul(it->second));
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section ChildWindow in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section ChildWindow in " + m_loadedConfigFile + ".");
}
// Initialize the close button if it was loaded
if (m_closeButton->m_textureNormal.getData() != nullptr)
{
m_closeButton->m_loaded = true;
m_closeButton->setSize(m_closeButton->m_textureNormal.getImageSize().x, m_closeButton->m_textureNormal.getImageSize().y);
}
else // Close button wan't loaded
{
TGUI_OUTPUT("TGUI error: Missing a CloseButtonNormalImage property in section ChildWindow in " + m_loadedConfigFile + ".");
return false;
}
// Make sure the close button was loaded
if (m_closeButton->m_textureNormal.getData() == nullptr)
throw Exception("Missing a CloseButtonNormalImage property in section ChildWindow in " + m_loadedConfigFile + ".");
m_closeButton->setSize(m_closeButton->m_textureNormal.getImageSize().x, m_closeButton->m_textureNormal.getImageSize().y);
// Make sure the required texture was loaded
if (m_textureTitleBar.getData() != nullptr)
{
m_titleBarHeight = m_textureTitleBar.getImageSize().y;
if (m_textureTitleBar.getData() == nullptr)
throw Exception("Not all needed images were loaded for the child window. Is the ChildWindow section in " + m_loadedConfigFile + " complete?");
m_loaded = true;
setSize(m_textureTitleBar.getImageSize().x, m_textureTitleBar.getImageSize().x * 3.0f / 4.0f);
}
else
{
TGUI_OUTPUT("TGUI error: Not all needed images were loaded for the child window. Is the ChildWindow section in " + m_loadedConfigFile + " complete?");
return false;
}
m_titleBarHeight = m_textureTitleBar.getImageSize().y;
setSize(m_textureTitleBar.getImageSize().x, m_textureTitleBar.getImageSize().x * 3.0f / 4.0f);
// Set the size of the title text
m_titleText.setCharacterSize(m_titleBarHeight * 8 / 10);
// When there is no error we will return true
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -435,10 +364,6 @@ namespace tgui
void ChildWindow::setTitleBarHeight(unsigned int height)
{
// Don't continue when the child window has not been loaded yet
if (m_loaded == false)
return;
// Remember the new title bar height
m_titleBarHeight = height;
@ -593,13 +518,10 @@ namespace tgui
TGUI_TextureManager.removeTexture(m_iconTexture);
// Load the icon image
if (TGUI_TextureManager.getTexture(m_iconTexture, filename))
{
m_iconTexture.setSize(m_titleBarHeight / m_textureTitleBar.getImageSize().y * m_iconTexture.getImageSize().x,
m_titleBarHeight / m_textureTitleBar.getImageSize().y * m_iconTexture.getImageSize().y);
}
else // Loading failed
TGUI_OUTPUT("Failed to load \"" + filename + "\" as icon for the ChildWindow");
TGUI_TextureManager.getTexture(m_iconTexture, filename);
m_iconTexture.setSize(m_titleBarHeight / m_textureTitleBar.getImageSize().y * m_iconTexture.getImageSize().x,
m_titleBarHeight / m_textureTitleBar.getImageSize().y * m_iconTexture.getImageSize().y);
// Reposition the images and text
setPosition(getPosition());
@ -641,10 +563,6 @@ namespace tgui
bool ChildWindow::mouseOnWidget(float x, float y)
{
// Don't continue when the child window has not been loaded yet
if (m_loaded == false)
return false;
// Check if the mouse is on top of the title bar
if (getTransform().transformRect(sf::FloatRect(0, 0, m_size.x + m_borders.left + m_borders.right, static_cast<float>(m_titleBarHeight))).contains(x, y))
{
@ -858,7 +776,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ChildWindow::setProperty(std::string property, const std::string& value)
void ChildWindow::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -892,7 +810,7 @@ namespace tgui
if (extractBorders(value, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Borders' property.");
throw Exception("Failed to parse 'Borders' property.");
}
else if (property == "distancetoside")
{
@ -907,7 +825,7 @@ namespace tgui
else if ((value == "right") || (value == "Right"))
setTitleAlignment(TitleAlignmentRight);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'TitleAlignment' property.");
throw Exception("Failed to parse 'TitleAlignment' property.");
}
else if (property == "callback")
{
@ -927,15 +845,12 @@ namespace tgui
}
}
else // The property didn't match
return Container::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Container::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ChildWindow::getProperty(std::string property, std::string& value) const
void ChildWindow::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -986,10 +901,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return Container::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Container::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -1023,10 +935,6 @@ namespace tgui
void ChildWindow::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Don't draw when the child window wasn't created
if (m_loaded == false)
return;
// Draw the title bar
target.draw(m_textureTitleBar, states);

View File

@ -154,7 +154,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ClickableWidget::setProperty(std::string property, const std::string& value)
void ClickableWidget::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -176,15 +176,12 @@ namespace tgui
}
}
else // The property didn't match
return Widget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ClickableWidget::getProperty(std::string property, std::string& value) const
void ClickableWidget::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -210,10 +207,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return Widget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -33,7 +33,6 @@
#include <TGUI/ComboBox.hpp>
#include <cmath>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -119,13 +118,10 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ComboBox::load(const std::string& configFileFilename)
void ComboBox::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// Remove all textures if they were loaded before
if (m_textureArrowUpNormal.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureArrowUpNormal);
if (m_textureArrowUpHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureArrowUpHover);
@ -133,24 +129,7 @@ namespace tgui
if (m_textureArrowDownHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureArrowDownHover);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("ComboBox", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "ComboBox");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -159,99 +138,73 @@ namespace tgui
configFileFolder = configFileFilename.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "separatehoverimage")
if (it->first == "separatehoverimage")
{
m_separateHoverImage = configFile.readBool(value, false);
m_separateHoverImage = configFile.readBool(it);
}
else if (property == "backgroundcolor")
else if (it->first == "backgroundcolor")
{
setBackgroundColor(configFile.readColor(value));
setBackgroundColor(configFile.readColor(it));
}
else if (property == "textcolor")
else if (it->first == "textcolor")
{
setTextColor(configFile.readColor(value));
setTextColor(configFile.readColor(it));
}
else if (property == "selectedbackgroundcolor")
else if (it->first == "selectedbackgroundcolor")
{
setSelectedBackgroundColor(configFile.readColor(value));
setSelectedBackgroundColor(configFile.readColor(it));
}
else if (property == "selectedtextcolor")
else if (it->first == "selectedtextcolor")
{
setSelectedTextColor(configFile.readColor(value));
setSelectedTextColor(configFile.readColor(it));
}
else if (property == "bordercolor")
else if (it->first == "bordercolor")
{
setBorderColor(configFile.readColor(value));
setBorderColor(configFile.readColor(it));
}
else if (property == "borders")
else if (it->first == "borders")
{
Borders borders;
if (extractBorders(value, borders))
if (extractBorders(it->second, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
throw Exception("Failed to parse the 'Borders' property in section ComboBox in " + m_loadedConfigFile);
}
else if (property == "arrowupnormalimage")
else if (it->first == "arrowupnormalimage")
{
if (!configFile.readTexture(value, getResourcePath() + configFileFolder, m_textureArrowUpNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowUpNormalImage in section ComboBox in " + m_loadedConfigFile + ".");
return false;
}
configFile.readTexture(it, getResourcePath() + configFileFolder, m_textureArrowUpNormal);
}
else if (property == "arrowuphoverimage")
else if (it->first == "arrowuphoverimage")
{
if (!configFile.readTexture(value, getResourcePath() + configFileFolder, m_textureArrowUpHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowUpHoverImage in section ComboBox in " + m_loadedConfigFile + ".");
return false;
}
configFile.readTexture(it, getResourcePath() + configFileFolder, m_textureArrowUpHover);
}
else if (property == "arrowdownnormalimage")
else if (it->first == "arrowdownnormalimage")
{
if (!configFile.readTexture(value, getResourcePath() + configFileFolder, m_textureArrowDownNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowDownNormalImage in section ComboBox in " + m_loadedConfigFile + ".");
return false;
}
configFile.readTexture(it, getResourcePath() + configFileFolder, m_textureArrowDownNormal);
}
else if (property == "arrowdownhoverimage")
else if (it->first == "arrowdownhoverimage")
{
if (!configFile.readTexture(value, getResourcePath() + configFileFolder, m_textureArrowDownHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowDownHoverImage in section ComboBox in " + m_loadedConfigFile + ".");
return false;
}
configFile.readTexture(it, getResourcePath() + configFileFolder, m_textureArrowDownHover);
}
else if (property == "scrollbar")
else if (it->first == "scrollbar")
{
if ((value.length() < 3) || (value[0] != '"') || (value[value.length()-1] != '"'))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for Scrollbar in section ComboBox in " + m_loadedConfigFile + ".");
return false;
}
if ((it->second.length() < 3) || (it->second[0] != '"') || (it->second[it->second.length()-1] != '"'))
throw Exception("Failed to parse value for Scrollbar in section ComboBox in " + m_loadedConfigFile + ".");
if (!m_listBox->setScrollbar(configFileFolder + value.substr(1, value.length()-2)))
return false;
m_listBox->setScrollbar(configFileFolder + it->second.substr(1, it->second.length()-2));
}
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section ComboBox in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section ComboBox in " + m_loadedConfigFile + ".");
}
// Make sure the required textures were loaded
if ((m_textureArrowUpNormal.getData() == nullptr) || (m_textureArrowDownNormal.getData() == nullptr))
{
TGUI_OUTPUT("TGUI error: Not all needed images were loaded for the combo box. Is the ComboBox section in " + m_loadedConfigFile + " complete?");
return false;
}
throw Exception("Not all needed images were loaded for the combo box. Is the ComboBox section in " + m_loadedConfigFile + " complete?");
// Remove all items (in case this is the second time that the load function was called)
m_listBox->removeAllItems();
return m_loaded = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -265,10 +218,6 @@ namespace tgui
void ComboBox::setSize(float width, float height)
{
// Don't set the scale when loading failed
if (m_loaded == false)
return;
// A negative size is not allowed for this widget
if (width < 0) width = -width;
if (height < 0) height = -height;
@ -287,10 +236,7 @@ namespace tgui
sf::Vector2f ComboBox::getSize() const
{
if (m_loaded)
return sf::Vector2f(m_listBox->getSize().x, static_cast<float>(m_listBox->getItemHeight()));
else
return sf::Vector2f(0, 0);
return sf::Vector2f(m_listBox->getSize().x, static_cast<float>(m_listBox->getItemHeight()));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -422,10 +368,6 @@ namespace tgui
m_borders.bottom = bottomBorder;
m_listBox->setBorders(m_borders.left, m_borders.bottom, m_borders.right, m_borders.bottom);
// Don't set the width and height when loading failed
if (m_loaded == false)
return;
// There is a minimum width
if (m_listBox->getSize().x < 50 + m_textureArrowDownNormal.getSize().x)
m_listBox->setSize(50.0f + m_textureArrowDownNormal.getSize().x, m_listBox->getSize().y);
@ -435,10 +377,6 @@ namespace tgui
int ComboBox::addItem(const sf::String& item)
{
// An item can only be added when the combo box was loaded correctly
if (m_loaded == false)
return false;
// Make room to add another item, until there are enough items
if ((m_nrOfItemsToDisplay == 0) || (m_nrOfItemsToDisplay > m_listBox->getItems().size()))
m_listBox->setSize(m_listBox->getSize().x, static_cast<float>(m_listBox->getItemHeight() * (m_listBox->getItems().size() + 1)));
@ -570,10 +508,6 @@ namespace tgui
bool ComboBox::mouseOnWidget(float x, float y)
{
// Don't do anything when the combo box wasn't loaded correctly
if (m_loaded == false)
return false;
// Get the current position
sf::Vector2f position = getPosition();
@ -660,7 +594,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ComboBox::setProperty(std::string property, const std::string& value)
void ComboBox::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -698,7 +632,7 @@ namespace tgui
if (extractBorders(value, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Borders' property.");
throw Exception("Failed to parse 'Borders' property.");
}
else if (property == "maximumitems")
{
@ -732,15 +666,12 @@ namespace tgui
}
}
else // The property didn't match
return Widget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ComboBox::getProperty(std::string property, std::string& value) const
void ComboBox::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -786,10 +717,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return Widget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -898,10 +826,6 @@ namespace tgui
void ComboBox::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Don't draw anything when the combo box was not loaded correctly
if (m_loaded == false)
return;
// Calculate the scale factor of the view
float scaleViewX = target.getSize().x / target.getView().getSize().x;
float scaleViewY = target.getSize().y / target.getView().getSize().y;

View File

@ -35,45 +35,25 @@ namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ConfigFile::~ConfigFile()
ConfigFile::ConfigFile(const std::string& filename, const std::string& section) :
m_filename(filename),
m_section (section)
{
// If a file is still open then close it
if (m_file.is_open())
m_file.close();
}
std::ifstream file(filename.c_str());
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (!file.is_open())
throw Exception("Failed to open config file '" + filename + "'.");
bool ConfigFile::open(const std::string& filename)
{
// If a file is already open then close it
if (m_file.is_open())
m_file.close();
// Open the file
m_file.open(filename.c_str(), std::ifstream::in);
// Check if the file was opened
if (m_file.is_open())
return true;
else
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ConfigFile::read(std::string section, std::vector<std::string>& properties, std::vector<std::string>& values)
{
bool error = false;
bool sectionFound = false;
unsigned int lineNumber = 0;
std::string lowercaseSection = toLower(section);
// Stop reading when we reach the end of the file
while (!m_file.eof())
while (!file.eof())
{
// Get the next line
std::string line;
std::getline(m_file, line);
std::getline(file, line);
lineNumber++;
std::string::const_iterator c = line.begin();
@ -84,14 +64,10 @@ namespace tgui
{
// If we already found our section then this would be the next section
if (sectionFound)
return !error;
// Convert the section names to lowercase in order to compare them
section = toLower(section);
sectionName = toLower(sectionName);
break;
// If this is the section we were looking for then start reading the properties
if ((section + ":") == sectionName)
if ((lowercaseSection + ":") == toLower(sectionName))
sectionFound = true;
}
else // This isn't a section
@ -100,73 +76,177 @@ namespace tgui
if (!sectionFound)
continue;
std::string property;
std::string value;
if (!removeWhitespace(line, c))
continue; // empty line
// Read the property in lowercase
property = readWord(line, c);
property = toLower(property);
std::string property = toLower(readWord(line, c));
if (!removeWhitespace(line, c))
{
TGUI_OUTPUT("TGUI error: Failed to parse line " + tgui::to_string(lineNumber) + ".");
error = true;
}
throw Exception("Failed to parse line " + tgui::to_string(lineNumber) + " in section " + section + " in file " + filename + ".");
// There has to be an assignment character
if (*c == '=')
++c;
else
{
TGUI_OUTPUT("TGUI error: Failed to parse line " + tgui::to_string(lineNumber) + ".");
error = true;
}
throw Exception("Failed to parse line " + tgui::to_string(lineNumber) + " in section " + section + " in file " + filename + ".");
if (!removeWhitespace(line, c))
{
TGUI_OUTPUT("TGUI error: Failed to parse line " + tgui::to_string(lineNumber) + ".");
error = true;
}
throw Exception("Failed to parse line " + tgui::to_string(lineNumber) + " in section " + section + " in file " + filename + ".");
int pos = c - line.begin();
value = line.substr(pos, line.length() - pos);
std::string value = line.substr(pos, line.length() - pos);
properties.push_back(property);
values.push_back(value);
m_properties.push_back(std::make_pair(property, value));
}
}
// Output an error when the section wasn't found
// Throw an exception when the section wasn't found
if (!sectionFound)
{
TGUI_OUTPUT("TGUI error: Section '" + section + "' was not found in the config file.");
error = true;
}
// The end of the file was reached
return !error;
throw Exception("Section '" + section + "' was not found in " + filename + ".");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ConfigFile::readBool(const std::string& value, bool defaultValue) const
const std::vector<std::pair<std::string, std::string>>& ConfigFile::getProperties() const
{
if ((value == "true") || (value == "True") || (value == "TRUE") || (value == "1"))
return m_properties;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ConfigFile::readBool(std::vector<std::pair<std::string, std::string>>::const_iterator it) const
{
if ((it->second == "true") || (it->second == "True") || (it->second == "TRUE") || (it->second == "1"))
return true;
else if ((value == "false") || (value == "False") || (value == "FALSE") || (value == "0"))
else if ((it->second == "false") || (it->second == "False") || (it->second == "FALSE") || (it->second == "0"))
return false;
else
return defaultValue;
throw Exception("Failed to parse property " + it->first + " in section " + m_section + " in file " + m_filename + ".");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::Color ConfigFile::readColor(const std::string& value) const
sf::Color ConfigFile::readColor(std::vector<std::pair<std::string, std::string>>::const_iterator it) const
{
return extractColor(value);
try
{
return extractColor(it->second);
}
catch (const Exception& e)
{
throw Exception("Failed to read the color value from property " + it->first + " in section " + m_section + " in file " + m_filename);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ConfigFile::readTexture(std::vector<std::pair<std::string, std::string>>::const_iterator it, const std::string& rootPath, Texture& texture) const
{
std::string::const_iterator c = it->second.begin();
// Remove all whitespaces (string should still contains something)
if (!removeWhitespace(it->second, c))
throw Exception("Failed to parse property " + it->first + " in section " + m_section + " in file " + m_filename + ". Value is empty.");
// There has to be a quote
if (*c == '"')
++c;
else
{
throw Exception("Failed to parse property " + it->first + " in section " + m_section + " in file " + m_filename
+ ". Expected an opening quote for the filename.");
}
std::string filename;
char prev = '\0';
// Look for the end quote
bool filenameFound = false;
while (c != it->second.end())
{
if ((*c != '"') || (prev == '\\'))
{
prev = *c;
filename.push_back(*c);
++c;
}
else
{
++c;
filenameFound = true;
break;
}
}
if (!filenameFound)
{
throw Exception("Failed to parse property " + it->first + " in section " + m_section + " in file " + m_filename
+ ". Failed to find the closing quote of the filename.");
}
// There may be optional parameters
sf::IntRect partRect;
sf::IntRect middleRect;
bool repeat = false;
while (removeWhitespace(it->second, c))
{
std::string word;
auto openingBracketPos = it->second.find('(', c - it->second.begin());
if (openingBracketPos != std::string::npos)
word = it->second.substr(c - it->second.begin(), openingBracketPos - (c - it->second.begin()));
else
word = it->second.substr(c - it->second.begin(), it->second.length() - (c - it->second.begin()));
if ((word == "Stretch") || (word == "stretch"))
{
repeat = false;
std::advance(c, 7);
}
else if ((word == "Repeat") || (word == "repeat"))
{
repeat = true;
std::advance(c, 6);
}
else
{
sf::IntRect* rect = nullptr;
if ((word == "Part") || (word == "part"))
{
rect = &partRect;
std::advance(c, 4);
}
else if ((word == "Middle") || (word == "middle"))
{
rect = &middleRect;
std::advance(c, 6);
}
else
{
throw Exception("Failed to parse property " + it->first + " in section " + m_section + " in file " + m_filename
+ ". Unexpected word '" + word + "' in front of opening bracket.");
}
auto closeBracketPos = it->second.find(')', c - it->second.begin());
if (closeBracketPos != std::string::npos)
{
if (!readIntRect(it->second.substr(c - it->second.begin(), closeBracketPos - (c - it->second.begin()) + 1), *rect))
throw Exception("Failed to parse " + word + " rectangle for property " + it->first + " in section " + m_section + " in file " + m_filename + ".");
}
else
{
throw Exception("Failed to parse property " + it->first + " in section " + m_section + " in file " + m_filename
+ ". Failed to find closing bracket for " + word + " rectangle.");
}
std::advance(c, closeBracketPos - (c - it->second.begin()) + 1);
}
}
// Load the texture
TGUI_TextureManager.getTexture(texture, rootPath + filename, partRect, middleRect, repeat);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -222,111 +302,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ConfigFile::readTexture(const std::string& value, const std::string& rootPath, Texture& texture) const
{
std::string::const_iterator c = value.begin();
// Remove all whitespaces (string should still contains something)
if (!removeWhitespace(value, c))
return false;
// There has to be a quote
if (*c == '"')
++c;
else
return false;
std::string filename;
char prev = '\0';
// Look for the end quote
bool filenameFound = false;
while (c != value.end())
{
if ((*c != '"') || (prev == '\\'))
{
prev = *c;
filename.push_back(*c);
++c;
}
else
{
++c;
filenameFound = true;
break;
}
}
if (!filenameFound)
return false;
// There may be optional parameters
sf::IntRect partRect;
sf::IntRect middleRect;
bool repeat = false;
while (removeWhitespace(value, c))
{
std::string word;
auto openingBracketPos = value.find('(', c - value.begin());
if (openingBracketPos != std::string::npos)
word = value.substr(c - value.begin(), openingBracketPos - (c - value.begin()));
else
word = value.substr(c - value.begin(), value.length() - (c - value.begin()));
if ((word == "Stretch") || (word == "stretch"))
{
repeat = false;
std::advance(c, 7);
}
else if ((word == "Repeat") || (word == "repeat"))
{
repeat = true;
std::advance(c, 6);
}
else
{
sf::IntRect* rect = nullptr;
if ((word == "Part") || (word == "part"))
{
rect = &partRect;
std::advance(c, 4);
}
else if ((word == "Middle") || (word == "middle"))
{
rect = &middleRect;
std::advance(c, 6);
}
if (rect == nullptr)
return false;
auto closeBracketPos = value.find(')', c - value.begin());
if (closeBracketPos != std::string::npos)
readIntRect(value.substr(c - value.begin(), closeBracketPos - (c - value.begin()) + 1), *rect);
else
return false;
std::advance(c, closeBracketPos - (c - value.begin()) + 1);
}
}
// Load the texture
return TGUI_TextureManager.getTexture(texture, rootPath + filename, partRect, middleRect, repeat);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ConfigFile::close()
{
// Close the file (if it is open)
if (m_file.is_open())
m_file.close();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ConfigFile::removeWhitespace(const std::string& line, std::string::const_iterator& c) const
{
while (c != line.end())

View File

@ -525,7 +525,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Container::loadWidgetsFromFile(const std::string& filename)
void Container::loadWidgetsFromFile(const std::string& filename)
{
#define COMPARE_WIDGET(length, name, widgetName) \
if (line.substr(0, length).compare(name) == 0) \
@ -540,18 +540,20 @@ namespace tgui
std::list<Widget*> widgetPtr;
// Open the file
std::ifstream m_file(getResourcePath() + filename);
std::ifstream file(getResourcePath() + filename);
// Check if the file was not opened
if (m_file.is_open() == false)
return false;
if (file.is_open() == false)
throw Exception("Failed to open " + filename + " to load the widgets from it.");
std::string line;
unsigned int lineNumber = 0;
// Stop reading when we reach the end of the file or when something went wrong
bool failed = false;
while (!m_file.eof() && !failed)
while (!file.eof())
{
std::string line;
std::getline(m_file, line);
std::getline(file, line);
lineNumber++;
if (!line.empty())
{
@ -624,10 +626,7 @@ namespace tgui
// Find the next quote
quotePos2 = line.find('"', backslashPos + 1);
if (quotePos2 == std::string::npos)
{
failed = true;
break;
}
throw Exception("Failed to find the closing quote on line " + tgui::to_string(lineNumber) + " in file " + filename + ".");
}
// Find the next backslash
@ -636,7 +635,7 @@ namespace tgui
// There may never be more than two quotes
if (line.find('"', quotePos2 + 1) != std::string::npos)
failed = true;
throw Exception("Too many quote characters on line " + tgui::to_string(lineNumber) + " in file " + filename + ".");
// Convert the part behind the quote to lowercase
line = line.substr(0, quotePos2 + 1) + toLower(line.substr(quotePos2 + 1));
@ -646,12 +645,12 @@ namespace tgui
line.erase(quotePos2 - 1, 1);
}
else // The second quote is missing
failed = true;
throw Exception("Failed to find the closing quote on line " + tgui::to_string(lineNumber) + " in file " + filename + ".");
}
}
// Only continue when the line hasn't become empty and nothing went wrong so far
if (!line.empty() && !failed)
if (!line.empty())
{
// Check if this is the first line
if (progress.empty())
@ -664,10 +663,7 @@ namespace tgui
continue;
}
else // The first line is wrong
{
failed = true;
break;
}
throw Exception("Expected a 'window' section in " + filename + ".");
}
// Check for opening and closing brackets
@ -680,19 +676,13 @@ namespace tgui
continue;
}
else
{
failed = true;
break;
}
throw Exception("Failed to find the expected '{' on line " + tgui::to_string(lineNumber) + " in file " + filename + ".");
}
else
else if (line.compare("}") == 0)
{
if (line.compare("}") == 0)
{
widgetPtr.pop_back();
progress.pop();
continue;
}
widgetPtr.pop_back();
progress.pop();
continue;
}
// The line doesn't contain a '}', so check if it contains another widget
@ -727,30 +717,19 @@ namespace tgui
{
std::string::size_type equalSignPosition = line.find('=');
if (equalSignPosition == std::string::npos)
{
failed = true;
break;
}
throw Exception("Failed to find the expected '=' on line " + tgui::to_string(lineNumber) + " in file " + filename + ".");
if (!widgetPtr.back()->setProperty(line.substr(0, equalSignPosition), line.substr(equalSignPosition + 1)))
failed = true;
widgetPtr.back()->setProperty(line.substr(0, equalSignPosition), line.substr(equalSignPosition + 1));
}
}
}
m_file.close();
if (failed)
return false;
return true;
#undef COMPARE_WIDGET
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Container::saveWidgetsToFile(const std::string& filename)
void Container::saveWidgetsToFile(const std::string& filename)
{
std::string tabs;
@ -758,8 +737,8 @@ namespace tgui
std::ofstream m_file(filename);
// Check if the file was not opened
if (m_file.is_open() == false)
return false;
if (!m_file.is_open())
throw Exception("Failed to create " + filename + " to save the widgets to.");
m_file << tabs << "Window:" << std::endl;
m_file << tabs << "{" << std::endl;
@ -804,13 +783,19 @@ namespace tgui
tabs += "\t";
std::string value;
if ((*widgetIt)->getProperty("Filename", value))
try
{
(*widgetIt)->getProperty("Filename", value);
m_file << tabs << "Filename = \"" << value << "\"" << std::endl;
}
else if ((*widgetIt)->getProperty("ConfigFile", value))
catch (const Exception&)
{
m_file << tabs << "ConfigFile = \"" << value << "\"" << std::endl;
try
{
(*widgetIt)->getProperty("ConfigFile", value);
m_file << tabs << "ConfigFile = \"" << value << "\"" << std::endl;
}
catch (const Exception&) {}
}
auto properties = (*widgetIt)->getPropertyList();
@ -845,8 +830,6 @@ namespace tgui
tabs.erase(tabs.length()-1);
m_file << tabs << "}" << std::endl;
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -30,7 +30,6 @@
#include <TGUI/Clipboard.hpp>
#include <cmath>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -156,37 +155,17 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool EditBox::load(const std::string& configFileFilename)
void EditBox::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// Remove the textures when they were loaded before
if (m_textureNormal.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureNormal);
if (m_textureHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureHover);
if (m_textureFocused.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureFocused);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("EditBox", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "EditBox");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -195,88 +174,66 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "separatehoverimage")
if (it->first == "separatehoverimage")
{
m_separateHoverImage = configFile.readBool(value, false);
m_separateHoverImage = configFile.readBool(it);
}
else if (property == "textcolor")
else if (it->first == "textcolor")
{
sf::Color color = extractColor(value);
sf::Color color = extractColor(it->second);
m_textBeforeSelection.setColor(color);
m_textAfterSelection.setColor(color);
}
else if (property == "selectedtextcolor")
else if (it->first == "selectedtextcolor")
{
m_textSelection.setColor(extractColor(value));
m_textSelection.setColor(extractColor(it->second));
}
else if (property == "selectedtextbackgroundcolor")
else if (it->first == "selectedtextbackgroundcolor")
{
m_selectedTextBackground.setFillColor(extractColor(value));
m_selectedTextBackground.setFillColor(extractColor(it->second));
}
else if (property == "caretcolor")
else if (it->first == "caretcolor")
{
m_caret.setFillColor(extractColor(value));
m_caret.setFillColor(extractColor(it->second));
}
else if (property == "caretwidth")
else if (it->first == "caretwidth")
{
m_caret.setSize(sf::Vector2f(tgui::stof(value), m_caret.getSize().y));
m_caret.setSize(sf::Vector2f(tgui::stof(it->second), m_caret.getSize().y));
}
else if (property == "borders")
else if (it->first == "borders")
{
Borders borders;
if (extractBorders(value, borders))
if (extractBorders(it->second, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
throw Exception("Failed to parse the 'Borders' property in section EditBox in " + m_loadedConfigFile);
}
else if (property == "normalimage")
else if (it->first == "normalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for NormalImage in section EditBox in " + m_loadedConfigFile + ".");
return false;
}
configFile.readTexture(it, configFileFolder, m_textureNormal);
}
else if (property == "hoverimage")
else if (it->first == "hoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for HoverImage in section EditBox in " + m_loadedConfigFile + ".");
return false;
}
configFile.readTexture(it, configFileFolder, m_textureHover);
}
else if (property == "focusedimage")
else if (it->first == "focusedimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureFocused))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for FocusedImage in section EditBox in " + m_loadedConfigFile + ".");
return false;
}
configFile.readTexture(it, configFileFolder, m_textureFocused);
}
else
TGUI_OUTPUT("TGUI error: Unrecognized property '" + property + "' in section EditBox in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section EditBox in " + m_loadedConfigFile + ".");
}
// Make sure the required texture was loaded
if (m_textureNormal.getData() != nullptr)
{
m_loaded = true;
setSize(m_textureNormal.getImageSize().x, m_textureNormal.getImageSize().y);
}
else
{
TGUI_OUTPUT("TGUI error: NormalImage wasn't loaded. Is the EditBox section in " + m_loadedConfigFile + " complete?");
return false;
}
if (m_textureNormal.getData() == nullptr)
throw Exception("NormalImage wasn't loaded. Is the EditBox section in " + m_loadedConfigFile + " complete?");
setSize(m_textureNormal.getImageSize().x, m_textureNormal.getImageSize().y);
// Auto-size the text
setTextSize(0);
// When there is no error we will return true
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -303,10 +260,6 @@ namespace tgui
void EditBox::setSize(float width, float height)
{
// Don't do anything when the edit box wasn't loaded correctly
if (m_loaded == false)
return;
// Recalculate the text size when auto scaling
if (m_textSize == 0)
setText(m_text);
@ -333,10 +286,6 @@ namespace tgui
void EditBox::setText(const sf::String& text)
{
// Don't do anything when the edit box wasn't loaded correctly
if (m_loaded == false)
return;
// Check if the text is auto sized
if (m_textSize == 0)
{
@ -477,10 +426,6 @@ namespace tgui
void EditBox::setPasswordCharacter(char passwordChar)
{
// Don't do anything when the edit box wasn't loaded correctly
if (m_loaded == false)
return;
// Change the password character
m_passwordChar = passwordChar;
@ -944,10 +889,6 @@ namespace tgui
void EditBox::keyPressed(const sf::Event::KeyEvent& event)
{
// Don't do anything when the edit box wasn't loaded correctly
if (m_loaded == false)
return;
// Check if one of the correct keys was pressed
if (event.code == sf::Keyboard::Left)
{
@ -1162,10 +1103,6 @@ namespace tgui
void EditBox::textEntered(sf::Uint32 key)
{
// Don't do anything when the edit box wasn't loaded correctly
if (m_loaded == false)
return;
// If only numbers are supported then make sure the input is valid
if (m_numbersOnly)
{
@ -1264,7 +1201,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool EditBox::setProperty(std::string property, const std::string& value)
void EditBox::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -1287,7 +1224,7 @@ namespace tgui
if (value.length() == 1)
setPasswordCharacter(value[0]);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'PasswordCharacter' propery.");
throw Exception("Failed to parse 'PasswordCharacter' propery.");
}
else
setPasswordCharacter('\0');
@ -1302,7 +1239,7 @@ namespace tgui
if (extractBorders(value, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Borders' property.");
throw Exception("Failed to parse 'Borders' property.");
}
else if (property == "textcolor")
{
@ -1327,7 +1264,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
limitTextWidth(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'LimitTextWidth' property.");
throw Exception("Failed to parse 'LimitTextWidth' property.");
}
else if (property == "caretwidth")
{
@ -1340,7 +1277,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
setNumbersOnly(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'NumbersOnly' property.");
throw Exception("Failed to parse 'NumbersOnly' property.");
}
else if (property == "callback")
{
@ -1358,15 +1295,12 @@ namespace tgui
}
}
else // The property didn't match
return ClickableWidget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool EditBox::getProperty(std::string property, std::string& value) const
void EditBox::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -1424,10 +1358,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return ClickableWidget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -1701,10 +1632,6 @@ namespace tgui
void EditBox::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Don't draw anything when the edit box was not loaded correctly
if (m_loaded == false)
return;
if (m_separateHoverImage)
{
if (m_mouseHover && m_textureHover.getData())

View File

@ -35,9 +35,6 @@ namespace tgui
Grid::Grid()
{
m_callback.widgetType = Type_Grid;
// There is nothing to load
m_loaded = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -515,10 +512,6 @@ namespace tgui
bool Grid::mouseOnWidget(float x, float y)
{
// Don't continue when the child window has not been loaded yet
if (m_loaded == false)
return false;
// Get the current position
sf::Vector2f position = getPosition();

View File

@ -354,16 +354,16 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Gui::loadWidgetsFromFile(const std::string& filename)
void Gui::loadWidgetsFromFile(const std::string& filename)
{
return m_container.loadWidgetsFromFile(filename);
m_container.loadWidgetsFromFile(filename);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Gui::saveWidgetsToFile(const std::string& filename)
void Gui::saveWidgetsToFile(const std::string& filename)
{
return m_container.saveWidgetsToFile(filename);
m_container.saveWidgetsToFile(filename);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -108,36 +108,16 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Knob::load(const std::string& configFileFilename)
void Knob::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// If the knob was loaded before then remove the old textures first
if (m_backgroundTexture.getData() != nullptr) TGUI_TextureManager.removeTexture(m_backgroundTexture);
if (m_foregroundTexture.getData() != nullptr) TGUI_TextureManager.removeTexture(m_foregroundTexture);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("Knob", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "Knob");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -146,50 +126,25 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "backgroundimage")
{
if (!configFile.readTexture(value, configFileFolder, m_backgroundTexture))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for BackgroundImage in section Knob in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "foregroundimage")
{
if (!configFile.readTexture(value, configFileFolder, m_foregroundTexture))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ForegroundImage in section Knob in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "imagerotation")
{
m_imageRotation = tgui::stof(value);
}
if (it->first == "backgroundimage")
configFile.readTexture(it, configFileFolder, m_backgroundTexture);
else if (it->first == "foregroundimage")
configFile.readTexture(it, configFileFolder, m_foregroundTexture);
else if (it->first == "imagerotation")
m_imageRotation = tgui::stof(it->second);
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section Knob in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section Knob in " + m_loadedConfigFile + ".");
}
// Make sure the required textures was loaded
if ((m_backgroundTexture.getData() != nullptr) && (m_foregroundTexture.getData() != nullptr))
{
m_foregroundTexture.setRotation(m_startRotation - m_imageRotation);
if ((m_backgroundTexture.getData() == nullptr) || (m_foregroundTexture.getData() == nullptr))
throw Exception("Not all needed images were loaded for the knob. Is the Knob section in " + m_loadedConfigFile + " complete?");
m_loaded = true;
setSize(m_backgroundTexture.getImageSize().x, m_backgroundTexture.getImageSize().y);
}
else
{
TGUI_OUTPUT("TGUI error: Not all needed images were loaded for the knob. Is the Knob section in " + m_loadedConfigFile + " complete?");
return false;
}
m_foregroundTexture.setRotation(m_startRotation - m_imageRotation);
return true;
setSize(m_backgroundTexture.getImageSize().x, m_backgroundTexture.getImageSize().y);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -214,10 +169,6 @@ namespace tgui
void Knob::setSize(float width, float height)
{
// Don't do anything when the knob wasn't loaded correctly
if (m_loaded == false)
return;
m_backgroundTexture.setSize(width, height);
m_foregroundTexture.setSize(m_foregroundTexture.getImageSize().x / m_backgroundTexture.getImageSize().x * width,
m_foregroundTexture.getImageSize().y / m_backgroundTexture.getImageSize().y * height);
@ -431,10 +382,6 @@ namespace tgui
void Knob::mouseMoved(float x, float y)
{
// Don't do anything when the knob wasn't loaded correctly
if (m_loaded == false)
return;
if (m_mouseHover == false)
mouseEnteredWidget();
@ -566,7 +513,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Knob::setProperty(std::string property, const std::string& value)
void Knob::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -601,7 +548,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
setClockwiseTurning(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'ClockwiseTurning' property.");
throw Exception("Failed to parse 'ClockwiseTurning' property.");
}
else if (property == "callback")
{
@ -617,15 +564,12 @@ namespace tgui
}
}
else // The property didn't match
return Widget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Knob::getProperty(std::string property, std::string& value) const
void Knob::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -661,10 +605,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return Widget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -29,7 +29,6 @@
#include <TGUI/Label.hpp>
#include <cmath>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -41,7 +40,6 @@ namespace tgui
m_autoSize(true)
{
m_callback.widgetType = Type_Label;
m_loaded = true;
m_background.setFillColor(sf::Color::Transparent);
}
@ -61,49 +59,21 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Label::load(const std::string& configFileFilename)
void Label::load(const std::string& configFileFilename)
{
// Don't continue when the config file was empty
if (configFileFilename.empty())
return true;
m_loadedConfigFile = getResourcePath() + configFileFilename;
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("Label", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "Label");
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "textcolor")
{
setTextColor(extractColor(value));
}
if (it->first == "textcolor")
setTextColor(extractColor(it->second));
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section Label in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section Label in " + m_loadedConfigFile + ".");
}
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -247,7 +217,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Label::setProperty(std::string property, const std::string& value)
void Label::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -280,18 +250,15 @@ namespace tgui
else if ((value == "false") || (value == "False"))
setAutoSize(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'AutoSize' property.");
throw Exception("Failed to parse 'AutoSize' property.");
}
else // The property didn't match
return ClickableWidget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Label::getProperty(std::string property, std::string& value) const
void Label::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -308,10 +275,7 @@ namespace tgui
else if (property == "autosize")
value = m_autoSize ? "true" : "false";
else // The property didn't match
return ClickableWidget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -31,7 +31,6 @@
#include <cmath>
#include <algorithm>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -50,7 +49,6 @@ namespace tgui
{
m_callback.widgetType = Type_ListBox;
m_draggableWidget = true;
m_loaded = true;
changeColors();
}
@ -134,7 +132,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ListBox::load(const std::string& configFileFilename)
void ListBox::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
@ -146,24 +144,7 @@ namespace tgui
}
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("ListBox", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "ListBox");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -172,69 +153,65 @@ namespace tgui
configFileFolder = configFileFilename.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "backgroundcolor")
if (it->first == "backgroundcolor")
{
setBackgroundColor(extractColor(value));
setBackgroundColor(extractColor(it->second));
}
else if (property == "textcolor")
else if (it->first == "textcolor")
{
setTextColor(extractColor(value));
setTextColor(extractColor(it->second));
}
else if (property == "selectedbackgroundcolor")
else if (it->first == "selectedbackgroundcolor")
{
setSelectedBackgroundColor(extractColor(value));
setSelectedBackgroundColor(extractColor(it->second));
}
else if (property == "selectedtextcolor")
else if (it->first == "selectedtextcolor")
{
setSelectedTextColor(extractColor(value));
setSelectedTextColor(extractColor(it->second));
}
else if (property == "bordercolor")
else if (it->first == "bordercolor")
{
setBorderColor(extractColor(value));
setBorderColor(extractColor(it->second));
}
else if (property == "borders")
else if (it->first == "borders")
{
Borders borders;
if (extractBorders(value, borders))
if (extractBorders(it->second, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
throw Exception("Failed to parse the 'Borders' property in section ListBox in " + m_loadedConfigFile);
}
else if (property == "scrollbar")
else if (it->first == "scrollbar")
{
if ((value.length() < 3) || (value[0] != '"') || (value[value.length()-1] != '"'))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for Scrollbar in section ChatBox in " + m_loadedConfigFile + ".");
return false;
}
if ((it->second.length() < 3) || (it->second[0] != '"') || (it->second[it->second.length()-1] != '"'))
throw Exception("Failed to parse value for Scrollbar in section ListBox in " + m_loadedConfigFile + ".");
// load the scrollbar and check if it failed
m_scroll = new Scrollbar();
if (m_scroll->load(configFileFolder + value.substr(1, value.length()-2)) == false)
try
{
// load the scrollbar
m_scroll = new Scrollbar();
m_scroll->load(configFileFolder + it->second.substr(1, it->second.length()-2));
}
catch (const Exception& e)
{
// The scrollbar couldn't be loaded so it must be deleted
delete m_scroll;
m_scroll = nullptr;
return false;
}
else // The scrollbar was loaded successfully
{
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setSize(m_scroll->getSize().x, static_cast<float>(m_size.y));
m_scroll->setLowValue(m_size.y);
m_scroll->setMaximum(m_items.size() * m_itemHeight);
throw Exception("Failed to create the internal scrollbar in ListBox. " + std::string(e.what()));
}
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setSize(m_scroll->getSize().x, static_cast<float>(m_size.y));
m_scroll->setLowValue(m_size.y);
m_scroll->setMaximum(m_items.size() * m_itemHeight);
}
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section ListBox in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section ListBox in " + m_loadedConfigFile + ".");
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -423,7 +400,7 @@ namespace tgui
bool ListBox::setSelectedItem(const sf::String& itemName)
{
// Loop through all items
for (unsigned int i=0; i<m_items.size(); ++i)
for (unsigned int i = 0; i < m_items.size(); ++i)
{
// Check if a match was found
if (m_items[i] == itemName)
@ -434,8 +411,6 @@ namespace tgui
}
}
TGUI_OUTPUT("TGUI warning: Failed to select the item in the list box. The name didn't match any item.");
// No match was found
m_selectedItem = -1;
return false;
@ -452,9 +427,8 @@ namespace tgui
}
// If the index is too high then deselect the items
if (index > int(m_items.size()-1))
if (index > static_cast<int>(m_items.size())-1)
{
TGUI_OUTPUT("TGUI warning: Failed to select the item in the list box. The index was too high.");
m_selectedItem = -1;
return false;
}
@ -477,10 +451,7 @@ namespace tgui
{
// The index can't be too high
if (index > m_items.size()-1)
{
TGUI_OUTPUT("TGUI warning: Failed to remove the item from the list box. The index was too high.");
return false;
}
// Remove the item
m_items.erase(m_items.begin() + index);
@ -524,7 +495,6 @@ namespace tgui
}
}
TGUI_OUTPUT("TGUI warning: Failed to remove the item from the list box. The name didn't match any item.");
return false;
}
@ -549,10 +519,7 @@ namespace tgui
{
// The index can't be too high
if (index > m_items.size()-1)
{
TGUI_OUTPUT("TGUI warning: The index of the item was too high. Returning an empty string.");
return "";
}
// Return the item
return m_items[index];
@ -571,7 +538,6 @@ namespace tgui
}
// No match was found
TGUI_OUTPUT("TGUI warning: The name didn't match any item. Returning -1 as item index.");
return -1;
}
@ -614,9 +580,13 @@ namespace tgui
if (m_scroll != nullptr)
delete m_scroll;
// load the scrollbar and check if it failed
m_scroll = new Scrollbar();
if(m_scroll->load(scrollbarConfigFileFilename) == false)
try
{
// load the scrollbar
m_scroll = new Scrollbar();
m_scroll->load(scrollbarConfigFileFilename);
}
catch (const Exception& e)
{
// The scrollbar couldn't be loaded so it must be deleted
delete m_scroll;
@ -624,16 +594,14 @@ namespace tgui
return false;
}
else // The scrollbar was loaded successfully
{
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setSize(m_scroll->getSize().x, static_cast<float>(m_size.y));
m_scroll->setLowValue(m_size.y);
m_scroll->setMaximum(m_items.size() * m_itemHeight);
return true;
}
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setSize(m_scroll->getSize().x, static_cast<float>(m_size.y));
m_scroll->setLowValue(m_size.y);
m_scroll->setMaximum(m_items.size() * m_itemHeight);
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -981,7 +949,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ListBox::setProperty(std::string property, const std::string& value)
void ListBox::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -1023,7 +991,7 @@ namespace tgui
if (extractBorders(value, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Borders' property.");
throw Exception("Failed to parse 'Borders' property.");
}
else if (property == "items")
{
@ -1053,15 +1021,12 @@ namespace tgui
}
}
else // The property didn't match
return Widget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ListBox::getProperty(std::string property, std::string& value) const
void ListBox::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -1107,10 +1072,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return Widget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -27,7 +27,6 @@
#include <TGUI/LoadingBar.hpp>
#include <cmath>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -102,36 +101,16 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool LoadingBar::load(const std::string& configFileFilename)
void LoadingBar::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// Remove all textures if they were loaded before
if (m_textureBack.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureBack);
if (m_textureFront.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureFront);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("LoadingBar", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "LoadingBar");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -140,56 +119,28 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "backimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureBack))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for BackImage in section LoadingBar in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "frontimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureFront))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for FrontImage in section LoadingBar in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "textcolor")
{
setTextColor(extractColor(value));
}
else if (property == "textsize")
{
setTextSize(tgui::stoi(value));
}
if (it->first == "backimage")
configFile.readTexture(it, configFileFolder, m_textureBack);
else if (it->first == "frontimage")
configFile.readTexture(it, configFileFolder, m_textureFront);
else if (it->first == "textcolor")
setTextColor(extractColor(it->second));
else if (it->first == "textsize")
setTextSize(tgui::stoi(it->second));
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section LoadingBar in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section LoadingBar in " + m_loadedConfigFile + ".");
}
// Make sure the required textures were loaded
if ((m_textureBack.getData() != nullptr) && (m_textureFront.getData() != nullptr))
{
m_loaded = true;
setSize(m_textureBack.getImageSize().x, m_textureBack.getImageSize().y);
}
else
{
TGUI_OUTPUT("TGUI error: Not all needed images were loaded for the loading bar. Is the LoadingBar section in " + m_loadedConfigFile + " complete?");
return false;
}
if ((m_textureBack.getData() == nullptr) || (m_textureFront.getData() == nullptr))
throw Exception("Not all needed images were loaded for the loading bar. Is the LoadingBar section in " + m_loadedConfigFile + " complete?");
setSize(m_textureBack.getImageSize().x, m_textureBack.getImageSize().y);
// Calculate the size of the front image (the size of the part that will be drawn)
recalculateSize();
// Loading has succeeded
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -203,10 +154,6 @@ namespace tgui
void LoadingBar::setSize(float width, float height)
{
// Don't do anything when the loading bar wasn't loaded correctly
if (m_loaded == false)
return;
// A negative size is not allowed for this widget
if (width < 0) width = -width;
if (height < 0) height = -height;
@ -345,10 +292,6 @@ namespace tgui
void LoadingBar::setText(const sf::String& text)
{
// Don't do anything when the loading bar wasn't loaded correctly
if (m_loaded == false)
return;
// Set the new text
m_text.setString(text);
@ -436,7 +379,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool LoadingBar::setProperty(std::string property, const std::string& value)
void LoadingBar::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -484,15 +427,12 @@ namespace tgui
}
}
else // The property didn't match
return ClickableWidget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool LoadingBar::getProperty(std::string property, std::string& value) const
void LoadingBar::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -530,10 +470,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return ClickableWidget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -555,10 +492,6 @@ namespace tgui
void LoadingBar::recalculateSize()
{
// Don't calculate anything when the loading bar wasn't loaded correctly
if (m_loaded == false)
return;
// Set the size of the front image
m_textureFront.setTextureRect(sf::FloatRect(0, 0, m_textureFront.getSize().x * ((m_value - m_minimum) / static_cast<float>(m_maximum - m_minimum)), m_textureFront.getSize().y));
}
@ -577,10 +510,6 @@ namespace tgui
void LoadingBar::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Don't draw when the loading bar wasn't loaded correctly
if (m_loaded == false)
return;
// Apply the transformation
states.transform *= getTransform();

View File

@ -29,7 +29,6 @@
#include <TGUI/MenuBar.hpp>
#include <cmath>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -48,8 +47,6 @@ namespace tgui
setSize(0, 20);
changeColors();
m_loaded = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -61,61 +58,29 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool MenuBar::load(const std::string& configFileFilename)
void MenuBar::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("MenuBar", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "MenuBar");
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "backgroundcolor")
{
setBackgroundColor(extractColor(value));
}
else if (property == "textcolor")
{
setTextColor(extractColor(value));
}
else if (property == "selectedbackgroundcolor")
{
setSelectedBackgroundColor(extractColor(value));
}
else if (property == "selectedtextcolor")
{
setSelectedTextColor(extractColor(value));
}
else if (property == "distancetoside")
{
setDistanceToSide(tgui::stoul(value));
}
if (it->first == "backgroundcolor")
setBackgroundColor(extractColor(it->second));
else if (it->first == "textcolor")
setTextColor(extractColor(it->second));
else if (it->first == "selectedbackgroundcolor")
setSelectedBackgroundColor(extractColor(it->second));
else if (it->first == "selectedtextcolor")
setSelectedTextColor(extractColor(it->second));
else if (it->first == "distancetoside")
setDistanceToSide(tgui::stoul(it->second));
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section MenuBar in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section MenuBar in " + m_loadedConfigFile + ".");
}
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -426,37 +391,34 @@ namespace tgui
bool MenuBar::mouseOnWidget(float x, float y)
{
if (m_loaded)
// Check if the mouse is on top of the menu bar
if (getTransform().transformRect(sf::FloatRect(0, 0, m_size.x, m_size.y)).contains(x, y))
return true;
else
{
// Check if the mouse is on top of the menu bar
if (getTransform().transformRect(sf::FloatRect(0, 0, m_size.x, m_size.y)).contains(x, y))
return true;
else
// Check if there is a menu open
if (m_visibleMenu != -1)
{
// Check if there is a menu open
if (m_visibleMenu != -1)
// Search the left position of the open menu
float left = 0;
for (int i = 0; i < m_visibleMenu; ++i)
left += m_menus[i].text.getLocalBounds().width + (2 * m_distanceToSide);
// Find out what the width of the menu should be
float width = 0;
for (unsigned int j = 0; j < m_menus[m_visibleMenu].menuItems.size(); ++j)
{
// Search the left position of the open menu
float left = 0;
for (int i = 0; i < m_visibleMenu; ++i)
left += m_menus[i].text.getLocalBounds().width + (2 * m_distanceToSide);
// Find out what the width of the menu should be
float width = 0;
for (unsigned int j = 0; j < m_menus[m_visibleMenu].menuItems.size(); ++j)
{
if (width < m_menus[m_visibleMenu].menuItems[j].getLocalBounds().width + (3 * m_distanceToSide))
width = m_menus[m_visibleMenu].menuItems[j].getLocalBounds().width + (3 * m_distanceToSide);
}
// There is a minimum width
if (width < m_minimumSubMenuWidth)
width = static_cast<float>(m_minimumSubMenuWidth);
// Check if the mouse is on top of the open menu
if (getTransform().transformRect(sf::FloatRect(left, m_size.y, width, m_size.y * m_menus[m_visibleMenu].menuItems.size())).contains(x, y))
return true;
if (width < m_menus[m_visibleMenu].menuItems[j].getLocalBounds().width + (3 * m_distanceToSide))
width = m_menus[m_visibleMenu].menuItems[j].getLocalBounds().width + (3 * m_distanceToSide);
}
// There is a minimum width
if (width < m_minimumSubMenuWidth)
width = static_cast<float>(m_minimumSubMenuWidth);
// Check if the mouse is on top of the open menu
if (getTransform().transformRect(sf::FloatRect(left, m_size.y, width, m_size.y * m_menus[m_visibleMenu].menuItems.size())).contains(x, y))
return true;
}
}
@ -636,7 +598,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool MenuBar::setProperty(std::string property, const std::string& value)
void MenuBar::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -712,15 +674,12 @@ namespace tgui
}
}
else // The property didn't match
return Widget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool MenuBar::getProperty(std::string property, std::string& value) const
void MenuBar::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -780,10 +739,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return Widget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -95,32 +95,12 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool MessageBox::load(const std::string& configFileFilename)
void MessageBox::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("MessageBox", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "MessageBox");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -132,88 +112,60 @@ namespace tgui
bool buttonPropertyFound = false;
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "textcolor")
if (it->first == "textcolor")
{
m_label->setTextColor(configFile.readColor(value));
m_label->setTextColor(configFile.readColor(it));
}
else if (property == "childwindow")
else if (it->first == "childwindow")
{
if ((value.length() < 3) || (value[0] != '"') || (value[value.length()-1] != '"'))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ChildWindow in section MessageBox in " + m_loadedConfigFile + ".");
return false;
}
if ((it->second.length() < 3) || (it->second[0] != '"') || (it->second[it->second.length()-1] != '"'))
throw Exception("Failed to parse value for ChildWindow in section MessageBox in " + m_loadedConfigFile + ".");
if (!ChildWindow::load(configFileFolder + value.substr(1, value.length()-2)))
try
{
TGUI_OUTPUT("TGUI error: Failed to load the internal ChildWindow for MessageBox.");
}
else
ChildWindow::load(configFileFolder + it->second.substr(1, it->second.length()-2));
childWindowPropertyFound = true;
}
else if (property == "button")
{
if ((value.length() < 3) || (value[0] != '"') || (value[value.length()-1] != '"'))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for Button in section MessageBox in " + m_loadedConfigFile + ".");
return false;
}
catch (const Exception& e)
{
throw Exception("Failed to load the internal ChildWindow for MessageBox. " + std::string(e.what()));
}
}
else if (it->first == "button")
{
if ((it->second.length() < 3) || (it->second[0] != '"') || (it->second[it->second.length()-1] != '"'))
throw Exception("Failed to parse value for Button in section MessageBox in " + m_loadedConfigFile + ".");
m_buttonConfigFileFilename = configFileFolder + value.substr(1, value.length()-2);
m_buttonConfigFileFilename = configFileFolder + it->second.substr(1, it->second.length()-2);
buttonPropertyFound = true;
}
else
{
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section MessageBox in " + m_loadedConfigFile + ".");
}
throw Exception("Unrecognized property '" + it->first + "' in section MessageBox in " + m_loadedConfigFile + ".");
}
if (!childWindowPropertyFound)
{
TGUI_OUTPUT("TGUI error: Missing a ChildWindow property in section MessageBox in " + m_loadedConfigFile + ".");
return false;
}
if (!buttonPropertyFound)
{
TGUI_OUTPUT("TGUI error: Missing a Button property in section MessageBox in " + m_loadedConfigFile + ".");
return false;
}
throw Exception("Missing a ChildWindow property in section MessageBox in " + m_loadedConfigFile + ".");
return m_loaded = true;
if (!buttonPropertyFound)
throw Exception("Missing a Button property in section MessageBox in " + m_loadedConfigFile + ".");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MessageBox::setText(const sf::String& text)
{
if (m_loaded)
{
m_label->setText(text);
m_label->setText(text);
rearrange();
}
else
{
TGUI_OUTPUT("TGUI error: Failed to set the text. MessageBox was not loaded completely.");
}
rearrange();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sf::String MessageBox::getText() const
{
if (m_loaded)
return m_label->getText();
else
{
TGUI_OUTPUT("TGUI error: Failed to set the text. MessageBox was not loaded completely.");
return "";
}
return m_label->getText();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -250,15 +202,12 @@ namespace tgui
{
m_textSize = size;
if (m_loaded)
{
m_label->setTextSize(size);
m_label->setTextSize(size);
for (unsigned int i = 0; i < m_buttons.size(); ++i)
m_buttons[i]->setTextSize(m_textSize);
for (unsigned int i = 0; i < m_buttons.size(); ++i)
m_buttons[i]->setTextSize(m_textSize);
rearrange();
}
rearrange();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -272,27 +221,20 @@ namespace tgui
void MessageBox::addButton(const sf::String& caption)
{
if (m_loaded)
{
Button::Ptr button(*this);
button->load(m_buttonConfigFileFilename);
button->setTextSize(m_textSize);
button->setText(caption);
button->bindCallbackEx(&MessageBox::ButtonClickedCallbackFunction, this, Button::LeftMouseClicked | Button::SpaceKeyPressed | Button::ReturnKeyPressed);
Button::Ptr button(*this);
button->load(m_buttonConfigFileFilename);
button->setTextSize(m_textSize);
button->setText(caption);
button->bindCallbackEx(&MessageBox::ButtonClickedCallbackFunction, this, Button::LeftMouseClicked | Button::SpaceKeyPressed | Button::ReturnKeyPressed);
m_buttons.push_back(button);
m_buttons.push_back(button);
rearrange();
}
else // MessageBox wasn't loaded
{
TGUI_OUTPUT("TGUI error: Could not add a button. MessageBox was not loaded completely.");
}
rearrange();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool MessageBox::setProperty(std::string property, const std::string& value)
void MessageBox::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -322,7 +264,7 @@ namespace tgui
}
else if (property == "callback")
{
Widget::setProperty(property, value);
ChildWindow::setProperty(property, value);
std::vector<sf::String> callbacks;
decodeList(value, callbacks);
@ -334,15 +276,12 @@ namespace tgui
}
}
else // The property didn't match
return ChildWindow::setProperty(property, value);
// You pass here when one of the properties matched
return true;
ChildWindow::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool MessageBox::getProperty(std::string property, std::string& value) const
void MessageBox::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -363,7 +302,7 @@ namespace tgui
else if (property == "callback")
{
std::string tempValue;
Widget::getProperty(property, tempValue);
ChildWindow::getProperty(property, tempValue);
std::vector<sf::String> callbacks;
@ -378,10 +317,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return ChildWindow::getProperty(property, value);
// You pass here when one of the properties matched
return true;
ChildWindow::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -400,9 +336,6 @@ namespace tgui
void MessageBox::rearrange()
{
if (!m_loaded)
return;
// Calculate the button size
float buttonWidth = 5.0f * m_textSize;
float buttonHeight = m_textSize * 10.0f / 8.0f;

View File

@ -40,7 +40,6 @@ namespace tgui
m_texture (nullptr)
{
m_callback.widgetType = Type_Panel;
m_loaded = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -164,10 +163,6 @@ namespace tgui
bool Panel::mouseOnWidget(float x, float y)
{
// Don't continue when the panel has not been loaded yet
if (m_loaded == false)
return false;
// Check if the mouse is inside the panel
if (getTransform().transformRect(sf::FloatRect(0, 0, m_size.x, m_size.y)).contains(x, y))
return true;
@ -239,7 +234,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Panel::setProperty(std::string property, const std::string& value)
void Panel::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -265,15 +260,12 @@ namespace tgui
}
}
else // The property didn't match
return Container::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Container::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Panel::getProperty(std::string property, std::string& value) const
void Panel::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -301,10 +293,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return Container::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Container::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -320,10 +309,6 @@ namespace tgui
void Panel::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Don't draw when the texture wasn't created
if (m_loaded == false)
return;
// Calculate the scale factor of the view
float scaleViewX = target.getSize().x / target.getView().getSize().x;
float scaleViewY = target.getSize().y / target.getView().getSize().y;

View File

@ -81,17 +81,8 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Picture::load(const std::string& filename)
void Picture::load(const std::string& filename)
{
// When everything is loaded successfully, this will become true.
m_loaded = false;
m_size.x = 0;
m_size.y = 0;
// Make sure that the filename isn't empty
if (filename.empty())
return false;
m_loadedFilename = getResourcePath() + filename;
// If we have already loaded a texture then first delete it
@ -99,17 +90,10 @@ namespace tgui
TGUI_TextureManager.removeTexture(m_texture);
// Try to load the texture from the file
if (TGUI_TextureManager.getTexture(m_texture, m_loadedFilename))
{
m_loaded = true;
TGUI_TextureManager.getTexture(m_texture, m_loadedFilename);
// Remember the size of the texture
setSize(m_texture.getImageSize().x, m_texture.getImageSize().y);
return true;
}
else // The texture was not loaded
return false;
// Remember the size of the texture
setSize(m_texture.getImageSize().x, m_texture.getImageSize().y);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -135,33 +119,25 @@ namespace tgui
m_size.x = width;
m_size.y = height;
if (m_loaded)
m_texture.setSize(width, height);
else
TGUI_OUTPUT("TGUI warning: Picture::setSize called while Picture wasn't loaded yet.");
m_texture.setSize(width, height);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Picture::setSmooth(bool smooth)
{
if (m_loaded)
if (m_texture.getData())
m_texture.getData()->texture.setSmooth(smooth);
else
TGUI_OUTPUT("TGUI warning: Picture::setSmooth called while Picture wasn't loaded yet.");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Picture::isSmooth() const
{
if (m_loaded)
if (m_texture.getData())
return m_texture.getData()->texture.isSmooth();
else
{
TGUI_OUTPUT("TGUI warning: Picture::isSmooth called while Picture wasn't loaded yet.");
return false;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -177,10 +153,6 @@ namespace tgui
bool Picture::mouseOnWidget(float x, float y)
{
// Don't do anything when the image wasn't loaded
if (m_loaded == false)
return false;
// Check if the mouse is on top of the picture
if (getTransform().transformRect(sf::FloatRect(0, 0, m_size.x, m_size.y)).contains(x, y))
{
@ -202,7 +174,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Picture::setProperty(std::string property, const std::string& value)
void Picture::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -217,18 +189,15 @@ namespace tgui
else if ((value == "false") || (value == "False"))
setSmooth(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Smooth' property.");
throw Exception("Failed to parse 'Smooth' property.");
}
else // The property didn't match
return ClickableWidget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Picture::getProperty(std::string property, std::string& value) const
void Picture::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -237,10 +206,7 @@ namespace tgui
else if (property == "smooth")
value = isSmooth() ? "true" : "false";
else // The property didn't match
return ClickableWidget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -27,7 +27,6 @@
#include <TGUI/RadioButton.hpp>
#include <cmath>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -103,13 +102,10 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool RadioButton::load(const std::string& configFileFilename)
void RadioButton::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// If the radio button was loaded before then remove the old textures
if (m_textureUnchecked.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureUnchecked);
if (m_textureChecked.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureChecked);
@ -117,24 +113,7 @@ namespace tgui
if (m_textureFocused.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureFocused);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("RadioButton", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "RadioButton");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -143,70 +122,31 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "textcolor")
{
m_text.setColor(configFile.readColor(value));
}
else if (property == "checkedimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureChecked))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for CheckedImage in section RadioButton in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "uncheckedimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureUnchecked))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for UncheckedImage in section RadioButton in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "hoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for HoverImage in section RadioButton in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "focusedimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureFocused))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for FocusedImage in section RadioButton in " + m_loadedConfigFile + ".");
return false;
}
}
if (it->first == "textcolor")
m_text.setColor(configFile.readColor(it));
else if (it->first == "checkedimage")
configFile.readTexture(it, configFileFolder, m_textureChecked);
else if (it->first == "uncheckedimage")
configFile.readTexture(it, configFileFolder, m_textureUnchecked);
else if (it->first == "hoverimage")
configFile.readTexture(it, configFileFolder, m_textureHover);
else if (it->first == "focusedimage")
configFile.readTexture(it, configFileFolder, m_textureFocused);
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section RadioButton in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section RadioButton in " + m_loadedConfigFile + ".");
}
// Make sure the required texture was loaded
if ((m_textureChecked.getData() != nullptr) && (m_textureUnchecked.getData() != nullptr))
{
m_loaded = true;
setSize(m_textureUnchecked.getImageSize().x, m_textureUnchecked.getImageSize().y);
}
else
{
TGUI_OUTPUT("TGUI error: Not all needed images were loaded for the radio button. Is the RadioButton section in " + m_loadedConfigFile + " complete?");
return false;
}
if ((m_textureChecked.getData() == nullptr) || (m_textureUnchecked.getData() == nullptr))
throw Exception("Not all needed images were loaded for the radio button. Is the RadioButton section in " + m_loadedConfigFile + " complete?");
// Check if optional textures were loaded
setSize(m_textureUnchecked.getImageSize().x, m_textureUnchecked.getImageSize().y);
// The widget can only be focused when there is an image available for this phase
if (m_textureFocused.getData() != nullptr)
{
m_allowFocus = true;
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -237,10 +177,6 @@ namespace tgui
void RadioButton::setSize(float width, float height)
{
// Don't do anything when the radio button wasn't loaded correctly
if (m_loaded == false)
return;
// A negative size is not allowed for this widget
if (width < 0) width = -width;
if (height < 0) height = -height;
@ -326,10 +262,6 @@ namespace tgui
void RadioButton::setText(const sf::String& text)
{
// Don't do anything when the radio button wasn't loaded correctly
if (m_loaded == false)
return;
// Set the new text
m_text.setString(text);
@ -422,10 +354,6 @@ namespace tgui
bool RadioButton::mouseOnWidget(float x, float y)
{
// Don't do anything when the radio button wasn't loaded correctly
if (m_loaded == false)
return false;
// Check if the mouse is on top of the image
if (getTransform().transformRect(sf::FloatRect(0, 0, m_textureUnchecked.getSize().x, m_textureUnchecked.getSize().y)).contains(x, y))
return true;
@ -528,7 +456,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool RadioButton::setProperty(std::string property, const std::string& value)
void RadioButton::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -543,7 +471,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
uncheck();
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Checked' property.");
throw Exception("Failed to parse 'Checked' property.");
}
else if (property == "text")
{
@ -564,7 +492,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
allowTextClick(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'AllowTextClick' property.");
throw Exception("Failed to parse 'AllowTextClick' property.");
}
else if (property == "callback")
{
@ -586,15 +514,12 @@ namespace tgui
}
}
else // The property didn't match
return ClickableWidget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool RadioButton::getProperty(std::string property, std::string& value) const
void RadioButton::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -638,10 +563,7 @@ namespace tgui
value += "," + tempValue;
}
else
return ClickableWidget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -132,13 +132,10 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Scrollbar::load(const std::string& configFileFilename)
void Scrollbar::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// Remove all textures if they were loaded before
if (m_textureTrackNormal.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureTrackNormal);
if (m_textureTrackHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureTrackHover);
@ -150,24 +147,7 @@ namespace tgui
if (m_textureArrowDownHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureArrowDownHover);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("Scrollbar", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "Scrollbar");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -176,103 +156,42 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "separatehoverimage")
if (it->first == "separatehoverimage")
m_separateHoverImage = configFile.readBool(it);
else if (it->first == "verticalimage")
{
m_separateHoverImage = configFile.readBool(value, false);
}
else if (property == "verticalimage")
{
m_verticalImage = configFile.readBool(value, false);
m_verticalImage = configFile.readBool(it);
m_verticalScroll = m_verticalImage;
}
else if (property == "tracknormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureTrackNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for TrackNormalImage in section Scrollbar in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "trackhoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureTrackHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for TrackHoverImage in section Scrollbar in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "thumbnormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureThumbNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ThumbNormalImage in section Scrollbar in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "thumbhoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureThumbHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ThumbHoverImage in section Scrollbar in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "arrowupnormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureArrowUpNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowUpNormalImage in section Scrollbar in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "arrowuphoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureArrowUpHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowUpHoverImage in section Scrollbar in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "arrowdownnormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureArrowDownNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowDownNormalImage in section Scrollbar in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "arrowdownhoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureArrowDownHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowDownHoverImage in section Scrollbar in " + m_loadedConfigFile + ".");
return false;
}
}
else if (it->first == "tracknormalimage")
configFile.readTexture(it, configFileFolder, m_textureTrackNormal);
else if (it->first == "trackhoverimage")
configFile.readTexture(it, configFileFolder, m_textureTrackHover);
else if (it->first == "thumbnormalimage")
configFile.readTexture(it, configFileFolder, m_textureThumbNormal);
else if (it->first == "thumbhoverimage")
configFile.readTexture(it, configFileFolder, m_textureThumbHover);
else if (it->first == "arrowupnormalimage")
configFile.readTexture(it, configFileFolder, m_textureArrowUpNormal);
else if (it->first == "arrowuphoverimage")
configFile.readTexture(it, configFileFolder, m_textureArrowUpHover);
else if (it->first == "arrowdownnormalimage")
configFile.readTexture(it, configFileFolder, m_textureArrowDownNormal);
else if (it->first == "arrowdownhoverimage")
configFile.readTexture(it, configFileFolder, m_textureArrowDownHover);
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section Scrollbar in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section Scrollbar in " + m_loadedConfigFile + ".");
}
// Make sure the required textures were loaded
if ((m_textureTrackNormal.getData() != nullptr) && (m_textureThumbNormal.getData() != nullptr)
&& (m_textureArrowUpNormal.getData() != nullptr) && (m_textureArrowDownNormal.getData() != nullptr))
{
m_loaded = true;
setSize(m_textureTrackNormal.getImageSize().x, m_textureTrackNormal.getImageSize().y);
setVerticalScroll(m_verticalScroll);
}
else
{
TGUI_OUTPUT("TGUI error: Not all needed images were loaded for the scrollbar. Is the Scrollbar section in " + m_loadedConfigFile + " complete?");
return false;
}
if ((m_textureTrackNormal.getData() == nullptr) || (m_textureThumbNormal.getData() == nullptr)
|| (m_textureArrowUpNormal.getData() == nullptr) || (m_textureArrowDownNormal.getData() == nullptr))
throw Exception("Not all needed images were loaded for the scrollbar. Is the Scrollbar section in " + m_loadedConfigFile + " complete?");
return true;
setSize(m_textureTrackNormal.getImageSize().x, m_textureTrackNormal.getImageSize().y);
setVerticalScroll(m_verticalScroll);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -319,10 +238,6 @@ namespace tgui
void Scrollbar::setSize(float width, float height)
{
// Don't do anything when the scrollbar wasn't loaded correctly
if (m_loaded == false)
return;
if (m_verticalImage == m_verticalScroll)
m_textureTrackNormal.setSize(width, height);
else
@ -583,10 +498,6 @@ namespace tgui
bool Scrollbar::mouseOnWidget(float x, float y)
{
// Don't do anything when the scrollbar wasn't loaded correctly
if (m_loaded == false)
return false;
// Don't make any calculations when no scrollbar is needed
if ((m_maximum <= m_lowValue) && (m_autoHide == true))
return false;
@ -740,10 +651,6 @@ namespace tgui
void Scrollbar::mouseMoved(float x, float y)
{
// If the scrollbar wasn't loaded then do nothing
if (m_loaded == false)
return;
if (m_mouseHover == false)
mouseEnteredWidget();
@ -900,7 +807,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Scrollbar::setProperty(std::string property, const std::string& value)
void Scrollbar::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -927,7 +834,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
setVerticalScroll(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'VerticalScroll' property.");
throw Exception("Failed to parse 'VerticalScroll' property.");
}
else if (property == "callback")
{
@ -949,18 +856,15 @@ namespace tgui
else if ((value == "false") || (value == "False"))
setAutoHide(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'AutoHide' property.");
throw Exception("Failed to parse 'AutoHide' property.");
}
else // The property didn't match
return Widget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Scrollbar::getProperty(std::string property, std::string& value) const
void Scrollbar::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -994,10 +898,7 @@ namespace tgui
else if (property == "autohide")
value = m_autoHide ? "true" : "false";
else // The property didn't match
return Widget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -1028,10 +929,6 @@ namespace tgui
void Scrollbar::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// If the scrollbar wasn't loaded then don't draw it
if (m_loaded == false)
return;
// Don't draw the scrollbar when it isn't needed
if ((m_autoHide == true) && (m_maximum <= m_lowValue))
return;

View File

@ -112,13 +112,10 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Slider::load(const std::string& configFileFilename)
void Slider::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// Remove all textures if they were loaded before
if (m_textureTrackNormal.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureTrackNormal);
if (m_textureTrackHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureTrackHover);
@ -126,24 +123,7 @@ namespace tgui
if (m_textureThumbHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureThumbHover);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("Slider", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "Slider");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -152,69 +132,32 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "separatehoverimage")
if (it->first == "separatehoverimage")
m_separateHoverImage = configFile.readBool(it);
else if (it->first == "verticalimage")
{
m_separateHoverImage = configFile.readBool(value, false);
}
else if (property == "verticalimage")
{
m_verticalImage = configFile.readBool(value, false);
m_verticalImage = configFile.readBool(it);
m_verticalScroll = m_verticalImage;
}
else if (property == "tracknormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureTrackNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for TrackNormalImage in section Slider in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "trackhoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureTrackHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for TrackHoverImage in section Slider in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "thumbnormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureThumbNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ThumbNormalImage in section Slider in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "thumbhoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureThumbHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ThumbHoverImage in section Slider in " + m_loadedConfigFile + ".");
return false;
}
}
else if (it->first == "tracknormalimage")
configFile.readTexture(it, configFileFolder, m_textureTrackNormal);
else if (it->first == "trackhoverimage")
configFile.readTexture(it, configFileFolder, m_textureTrackHover);
else if (it->first == "thumbnormalimage")
configFile.readTexture(it, configFileFolder, m_textureThumbNormal);
else if (it->first == "thumbhoverimage")
configFile.readTexture(it, configFileFolder, m_textureThumbHover);
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section Slider in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section Slider in " + m_loadedConfigFile + ".");
}
// Make sure the required textures were loaded
if ((m_textureTrackNormal.getData() != nullptr) && (m_textureThumbNormal.getData() != nullptr))
{
m_loaded = true;
setSize(m_textureTrackNormal.getImageSize().x, m_textureTrackNormal.getImageSize().y);
}
else
{
TGUI_OUTPUT("TGUI error: Not all needed images were loaded for the slider. Is the Slider section in " + m_loadedConfigFile + " complete?");
return false;
}
if ((m_textureTrackNormal.getData() == nullptr) && (m_textureThumbNormal.getData() == nullptr))
throw Exception("Not all needed images were loaded for the slider. Is the Slider section in " + m_loadedConfigFile + " complete?");
return true;
setSize(m_textureTrackNormal.getImageSize().x, m_textureTrackNormal.getImageSize().y);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -251,10 +194,6 @@ namespace tgui
void Slider::setSize(float width, float height)
{
// Don't do anything when the slider wasn't loaded correctly
if (m_loaded == false)
return;
if (m_verticalImage == m_verticalScroll)
{
m_textureTrackNormal.setSize(width, height);
@ -460,10 +399,6 @@ namespace tgui
bool Slider::mouseOnWidget(float x, float y)
{
// Don't do anything when the slider wasn't loaded correctly
if (m_loaded == false)
return false;
// Check if the mouse is on top of the thumb
if (sf::FloatRect(m_textureThumbNormal.getPosition().x, m_textureThumbNormal.getPosition().y, getThumbSize().x, getThumbSize().y).contains(x, y))
{
@ -508,10 +443,6 @@ namespace tgui
void Slider::mouseMoved(float x, float y)
{
// Don't do anything when the slider wasn't loaded correctly
if (m_loaded == false)
return;
if (m_mouseHover == false)
mouseEnteredWidget();
@ -576,7 +507,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Slider::setProperty(std::string property, const std::string& value)
void Slider::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -603,7 +534,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
setVerticalScroll(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'VerticalScroll' property.");
throw Exception("Failed to parse 'VerticalScroll' property.");
}
else if (property == "callback")
{
@ -619,15 +550,12 @@ namespace tgui
}
}
else // The property didn't match
return Widget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Slider::getProperty(std::string property, std::string& value) const
void Slider::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -659,10 +587,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return Widget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -692,10 +617,6 @@ namespace tgui
void Slider::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Don't draw when the slider wasn't loaded correctly
if (m_loaded == false)
return;
if (m_separateHoverImage)
{
if (m_mouseHover)

View File

@ -110,13 +110,10 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Slider2d::load(const std::string& configFileFilename)
void Slider2d::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// Remove all textures if they were loaded before
if (m_textureTrackNormal.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureTrackNormal);
if (m_textureTrackHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureTrackHover);
@ -124,24 +121,7 @@ namespace tgui
if (m_textureThumbHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureThumbHover);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("Slider2d", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "Slider2d");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -150,64 +130,28 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "separatehoverimage")
{
m_separateHoverImage = configFile.readBool(value, false);
}
else if (property == "tracknormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureTrackNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for TrackNormalImage in section Slider2d in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "trackhoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureTrackHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for TrackHoverImage in section Slider2d in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "thumbnormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureThumbNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ThumbNormalImage in section Slider2d in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "thumbhoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureThumbHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ThumbHoverImage in section Slider2d in " + m_loadedConfigFile + ".");
return false;
}
}
if (it->first == "separatehoverimage")
m_separateHoverImage = configFile.readBool(it);
else if (it->first == "tracknormalimage")
configFile.readTexture(it, configFileFolder, m_textureTrackNormal);
else if (it->first == "trackhoverimage")
configFile.readTexture(it, configFileFolder, m_textureTrackHover);
else if (it->first == "thumbnormalimage")
configFile.readTexture(it, configFileFolder, m_textureThumbNormal);
else if (it->first == "thumbhoverimage")
configFile.readTexture(it, configFileFolder, m_textureThumbHover);
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section Slider2d in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section Slider2d in " + m_loadedConfigFile + ".");
}
// Make sure the required textures were loaded
if ((m_textureTrackNormal.getData() != nullptr) && (m_textureThumbNormal.getData() != nullptr))
{
// Set the size of the slider
m_size = sf::Vector2f(m_textureTrackNormal.getSize());
}
else
{
TGUI_OUTPUT("TGUI error: Not all needed images were loaded for the slider. Is the Slider2d section in " + m_loadedConfigFile + " complete?");
return false;
}
if ((m_textureTrackNormal.getData() == nullptr) || (m_textureThumbNormal.getData() == nullptr))
throw Exception("Not all needed images were loaded for the slider. Is the Slider2d section in " + m_loadedConfigFile + " complete?");
return m_loaded = true;
// Set the size of the slider
m_size = sf::Vector2f(m_textureTrackNormal.getSize());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -221,10 +165,6 @@ namespace tgui
void Slider2d::setSize(float width, float height)
{
// Don't do anything when the slider wasn't loaded correctly
if (m_loaded == false)
return;
// A negative size is not allowed for this widget
if (width < 0) width = -width;
if (height < 0) height = -height;
@ -388,10 +328,6 @@ namespace tgui
void Slider2d::mouseMoved(float x, float y)
{
// Don't do anything when the slider wasn't loaded correctly
if (m_loaded == false)
return;
if (m_mouseHover == false)
mouseEnteredWidget();
@ -463,7 +399,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Slider2d::setProperty(std::string property, const std::string& value)
void Slider2d::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -484,13 +420,13 @@ namespace tgui
tgui::stof(value.substr(commaPos+1, value.length()-commaPos-2))));
}
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Minimum' property.");
throw Exception("Failed to parse 'Minimum' property.");
}
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Minimum' property.");
throw Exception("Failed to parse 'Minimum' property.");
}
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Minimum' property.");
throw Exception("Failed to parse 'Minimum' property.");
}
else if (property == "maximum")
{
@ -505,13 +441,13 @@ namespace tgui
tgui::stof(value.substr(commaPos+1, value.length()-commaPos-2))));
}
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Maximum' property.");
throw Exception("Failed to parse 'Maximum' property.");
}
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Maximum' property.");
throw Exception("Failed to parse 'Maximum' property.");
}
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Maximum' property.");
throw Exception("Failed to parse 'Maximum' property.");
}
else if (property == "value")
{
@ -526,13 +462,13 @@ namespace tgui
tgui::stof(value.substr(commaPos+1, value.length()-commaPos-2))));
}
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Value' property.");
throw Exception("Failed to parse 'Value' property.");
}
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Value' property.");
throw Exception("Failed to parse 'Value' property.");
}
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Value' property.");
throw Exception("Failed to parse 'Value' property.");
}
else if (property == "fixedthumbsize")
{
@ -541,7 +477,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
setFixedThumbSize(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'FixedThumbSize' property.");
throw Exception("Failed to parse 'FixedThumbSize' property.");
}
else if (property == "enablethumbcenter")
{
@ -550,7 +486,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
enableThumbCenter(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'EnableThumbCenter' property.");
throw Exception("Failed to parse 'EnableThumbCenter' property.");
}
else if (property == "callback")
{
@ -568,15 +504,12 @@ namespace tgui
}
}
else // The property didn't match
return ClickableWidget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Slider2d::getProperty(std::string property, std::string& value) const
void Slider2d::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -612,10 +545,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return ClickableWidget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -636,10 +566,6 @@ namespace tgui
void Slider2d::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Don't draw when the slider wasn't loaded correctly
if (m_loaded == false)
return;
// Calculate the scale factor of the view
float scaleViewX = target.getSize().x / target.getView().getSize().x;
float scaleViewY = target.getSize().y / target.getView().getSize().y;

View File

@ -109,13 +109,10 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool SpinButton::load(const std::string& configFileFilename)
void SpinButton::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// If the button was loaded before then remove the old textures first
if (m_textureArrowUpNormal.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureArrowUpNormal);
if (m_textureArrowUpHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureArrowUpHover);
@ -123,24 +120,7 @@ namespace tgui
if (m_textureArrowDownHover.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureArrowDownHover);
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("SpinButton", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "SpinButton");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -149,64 +129,28 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "separatehoverimage")
{
m_separateHoverImage = configFile.readBool(value, false);
}
else if (property == "arrowupnormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureArrowUpNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowUpNormalImage in section SpinButton in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "arrowuphoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureArrowUpHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowUpHoverImage in section SpinButton in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "arrowdownnormalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureArrowDownNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowDownNormalImage in section SpinButton in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "arrowdownhoverimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureArrowDownHover))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for ArrowDownHoverImage in section SpinButton in " + m_loadedConfigFile + ".");
return false;
}
}
if (it->first == "separatehoverimage")
m_separateHoverImage = configFile.readBool(it);
else if (it->first == "arrowupnormalimage")
configFile.readTexture(it, configFileFolder, m_textureArrowUpNormal);
else if (it->first == "arrowuphoverimage")
configFile.readTexture(it, configFileFolder, m_textureArrowUpHover);
else if (it->first == "arrowdownnormalimage")
configFile.readTexture(it, configFileFolder, m_textureArrowDownNormal);
else if (it->first == "arrowdownhoverimage")
configFile.readTexture(it, configFileFolder, m_textureArrowDownHover);
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section SpinButton in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section SpinButton in " + m_loadedConfigFile + ".");
}
// Make sure the required textures were loaded
if ((m_textureArrowUpNormal.getData() != nullptr) && (m_textureArrowDownNormal.getData() != nullptr))
{
m_size.x = static_cast<float>(m_textureArrowUpNormal.getSize().x);
m_size.y = static_cast<float>(m_textureArrowUpNormal.getSize().y + m_textureArrowDownNormal.getSize().y);
}
else
{
TGUI_OUTPUT("TGUI error: Not all needed images were loaded for the spin button. Is the SpinButton section in " + m_loadedConfigFile + " complete?");
return false;
}
if ((m_textureArrowUpNormal.getData() == nullptr) || (m_textureArrowDownNormal.getData() == nullptr))
throw Exception("Not all needed images were loaded for the spin button. Is the SpinButton section in " + m_loadedConfigFile + " complete?");
return m_loaded = true;
m_size.x = m_textureArrowUpNormal.getSize().x;
m_size.y = m_textureArrowUpNormal.getSize().y + m_textureArrowDownNormal.getSize().y;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -220,10 +164,6 @@ namespace tgui
void SpinButton::setSize(float width, float height)
{
// Don't do anything when the spin button wasn't loaded correctly
if (m_loaded == false)
return;
// Store the new size
m_size.x = width;
m_size.y = height;
@ -435,7 +375,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool SpinButton::setProperty(std::string property, const std::string& value)
void SpinButton::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -462,7 +402,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
setVerticalScroll(false);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'VerticalScroll' property.");
throw Exception("Failed to parse 'VerticalScroll' property.");
}
else if (property == "callback")
{
@ -478,15 +418,12 @@ namespace tgui
}
}
else // The property didn't match
return ClickableWidget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool SpinButton::getProperty(std::string property, std::string& value) const
void SpinButton::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -518,10 +455,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return ClickableWidget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
ClickableWidget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -541,10 +475,6 @@ namespace tgui
void SpinButton::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Don't draw when the spin button wasn't loaded correctly
if (m_loaded == false)
return;
// Adjust the transformation
states.transform *= getTransform();

View File

@ -112,10 +112,6 @@ namespace tgui
void SpriteSheet::setCells(unsigned int rows, unsigned int columns)
{
// Make sure that the picture was already loaded
if (m_loaded == false)
return;
// You can't have 0 rows
if (rows == 0)
rows = 1;
@ -174,10 +170,6 @@ namespace tgui
void SpriteSheet::setVisibleCell(unsigned int row, unsigned int column)
{
// Make sure that the picture was already loaded
if (m_loaded == false)
return;
// You can't make a row visible that doesn't exist
if (row > m_rows)
row = m_rows;
@ -214,7 +206,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool SpriteSheet::setProperty(std::string property, const std::string& value)
void SpriteSheet::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -227,15 +219,12 @@ namespace tgui
setColumns(tgui::stoi(value));
}
else // The property didn't match
return Picture::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Picture::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool SpriteSheet::getProperty(std::string property, std::string& value) const
void SpriteSheet::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -244,10 +233,7 @@ namespace tgui
else if (property == "columns")
value = tgui::to_string(getColumns());
else // The property didn't match
return Picture::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Picture::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -155,8 +155,8 @@ namespace tgui
}
}
// If you pass here then something is wrong about the line, the color will be black
return sf::Color::Black;
// If you pass here then something is wrong about the line
throw Exception("Failed to parse color from string.");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -359,8 +359,7 @@ namespace tgui
if (next == encodedString.cend())
{
TGUI_OUTPUT("TGUI warning: Escape character at the end of the string. Ignoring character.");
continue;
throw Exception("Escape character at the end of the string.");
}
else if ((*next == '\\') || (*next == '\"'))
{
@ -379,8 +378,7 @@ namespace tgui
}
else
{
TGUI_OUTPUT(std::string("TGUI warning: Escape character in front of '") + *next + "'. Ignoring escape character.");
continue;
throw Exception(std::string("Escape character in front of '") + *next);
}
}
else // No escape character, just a normal character to be added to the string
@ -461,8 +459,7 @@ namespace tgui
if (next == encodedString.cend())
{
TGUI_OUTPUT("TGUI warning: Escape character at the end of the string. Ignoring character.");
continue;
throw Exception("Escape character at the end of the string.");
}
else if ((*next == '\\') || (*next == '\"') || (*next == ','))
{
@ -481,7 +478,7 @@ namespace tgui
}
else
{
TGUI_OUTPUT(std::string("TGUI warning: Escape character in front of '") + *next + "'. Ignoring escape character.");
throw Exception(std::string("Escape character in front of '") + *next);
continue;
}
}

View File

@ -29,7 +29,6 @@
#include <TGUI/Tab.hpp>
#include <cmath>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -135,13 +134,10 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Tab::load(const std::string& configFileFilename)
void Tab::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
// When everything is loaded successfully, this will become true.
m_loaded = false;
// If the button was loaded before then remove the old textures first
if (m_textureNormal.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureNormal);
if (m_textureSelected.getData() != nullptr) TGUI_TextureManager.removeTexture(m_textureSelected);
@ -165,24 +161,7 @@ namespace tgui
}
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("Tab", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "Tab");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -191,45 +170,22 @@ namespace tgui
configFileFolder = m_loadedConfigFile.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "separateselectedimage")
{
m_separateSelectedImage = configFile.readBool(value, false);
}
else if (property == "textcolor")
{
m_textColor = configFile.readColor(value);
}
else if (property == "selectedtextcolor")
{
m_selectedTextColor = configFile.readColor(value);
}
else if (property == "distancetoside")
{
setDistanceToSide(tgui::stoul(value));
}
else if (property == "normalimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureNormal))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for NormalImage in section Tab in " + m_loadedConfigFile + ".");
return false;
}
}
else if (property == "selectedimage")
{
if (!configFile.readTexture(value, configFileFolder, m_textureSelected))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for SelectedImage in section Tab in " + m_loadedConfigFile + ".");
return false;
}
}
if (it->first == "separateselectedimage")
m_separateSelectedImage = configFile.readBool(it);
else if (it->first == "textcolor")
m_textColor = configFile.readColor(it);
else if (it->first == "selectedtextcolor")
m_selectedTextColor = configFile.readColor(it);
else if (it->first == "distancetoside")
setDistanceToSide(tgui::stoul(it->second));
else if (it->first == "normalimage")
configFile.readTexture(it, configFileFolder, m_textureNormal);
else if (it->first == "selectedimage")
configFile.readTexture(it, configFileFolder, m_textureSelected);
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section Tab in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section Tab in " + m_loadedConfigFile + ".");
}
// Clear the vectors
@ -237,16 +193,11 @@ namespace tgui
// Make sure the required texture was loaded
if (m_textureNormal.getData() == nullptr)
{
TGUI_OUTPUT("TGUI error: NormalImage wasn't loaded. Is the Tab section in " + m_loadedConfigFile + " complete?");
return false;
}
throw Exception("NormalImage wasn't loaded. Is the Tab section in " + m_loadedConfigFile + " complete?");
// Recalculate the text size when auto sizing
if (m_textSize == 0)
setTextSize(0);
return m_loaded = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -283,10 +234,6 @@ namespace tgui
sf::Vector2f Tab::getSize() const
{
// Make sure the tab has been loaded
if (m_loaded == false)
return sf::Vector2f(0, 0);
// Add the width of all the tabs together
float width = 0;
for (auto it = m_texturesNormal.begin(); it != m_texturesNormal.end(); ++it)
@ -349,8 +296,6 @@ namespace tgui
return;
}
}
TGUI_OUTPUT("TGUI warning: Failed to select the tab. The name didn't match any tab.");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -359,10 +304,7 @@ namespace tgui
{
// If the index is too big then do nothing
if (index > m_tabNames.size() - 1)
{
TGUI_OUTPUT("TGUI warning: Failed to select the tab. The index was too high.");
return;
}
// Select the tab
m_selectedTab = index;
@ -410,8 +352,6 @@ namespace tgui
return;
}
}
TGUI_OUTPUT("TGUI warning: Failed to remove the tab. The name didn't match any tab.");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -420,10 +360,7 @@ namespace tgui
{
// The index can't be too high
if (index > m_tabNames.size() - 1)
{
TGUI_OUTPUT("TGUI warning: Failed to remove the tab. The index was too high.");
return;
}
// Remove the tab
m_tabNames.erase(m_tabNames.begin() + index);
@ -642,12 +579,8 @@ namespace tgui
bool Tab::mouseOnWidget(float x, float y)
{
// Check if the mouse is on top of the tab
if (m_loaded)
{
if (getTransform().transformRect(sf::FloatRect(0, 0, getSize().x, getSize().y)).contains(x, y))
return true;
}
if (getTransform().transformRect(sf::FloatRect(0, 0, getSize().x, getSize().y)).contains(x, y))
return true;
if (m_mouseHover)
mouseLeftWidget();
@ -694,7 +627,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Tab::setProperty(std::string property, const std::string& value)
void Tab::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -750,15 +683,12 @@ namespace tgui
}
}
else // The property didn't match
return Widget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Tab::getProperty(std::string property, std::string& value) const
void Tab::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -796,10 +726,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return Widget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -857,9 +784,6 @@ namespace tgui
void Tab::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if (m_loaded == false)
return;
GLint scissor[4];
bool clippingRequired = false;
sf::FloatRect realRect;

View File

@ -30,8 +30,6 @@
#include <TGUI/TextBox.hpp>
#include <TGUI/Clipboard.hpp>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace tgui
@ -65,9 +63,6 @@ namespace tgui
m_draggableWidget = true;
changeColors();
// Load the text box with default values
m_loaded = true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -181,7 +176,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TextBox::load(const std::string& configFileFilename)
void TextBox::load(const std::string& configFileFilename)
{
m_loadedConfigFile = getResourcePath() + configFileFilename;
@ -193,24 +188,7 @@ namespace tgui
}
// Open the config file
ConfigFile configFile;
if (!configFile.open(m_loadedConfigFile))
{
TGUI_OUTPUT("TGUI error: Failed to open " + m_loadedConfigFile + ".");
return false;
}
// Read the properties and their values (as strings)
std::vector<std::string> properties;
std::vector<std::string> values;
if (!configFile.read("TextBox", properties, values))
{
TGUI_OUTPUT("TGUI error: Failed to parse " + m_loadedConfigFile + ".");
return false;
}
// Close the config file
configFile.close();
ConfigFile configFile(m_loadedConfigFile, "TextBox");
// Find the folder that contains the config file
std::string configFileFolder = "";
@ -219,72 +197,68 @@ namespace tgui
configFileFolder = configFileFilename.substr(0, slashPos+1);
// Handle the read properties
for (unsigned int i = 0; i < properties.size(); ++i)
for (auto it = configFile.getProperties().cbegin(); it != configFile.getProperties().cend(); ++it)
{
std::string property = properties[i];
std::string value = values[i];
if (property == "backgroundcolor")
if (it->first == "backgroundcolor")
{
setBackgroundColor(configFile.readColor(value));
setBackgroundColor(configFile.readColor(it));
}
else if (property == "textcolor")
else if (it->first == "textcolor")
{
setTextColor(configFile.readColor(value));
setTextColor(configFile.readColor(it));
}
else if (property == "selectedtextbackgroundcolor")
else if (it->first == "selectedtextbackgroundcolor")
{
setSelectedTextBackgroundColor(configFile.readColor(value));
setSelectedTextBackgroundColor(configFile.readColor(it));
}
else if (property == "selectedtextcolor")
else if (it->first == "selectedtextcolor")
{
setSelectedTextColor(configFile.readColor(value));
setSelectedTextColor(configFile.readColor(it));
}
else if (property == "caretcolor")
else if (it->first == "caretcolor")
{
setCaretColor(configFile.readColor(value));
setCaretColor(configFile.readColor(it));
}
else if (property == "bordercolor")
else if (it->first == "bordercolor")
{
setBorderColor(configFile.readColor(value));
setBorderColor(configFile.readColor(it));
}
else if (property == "borders")
else if (it->first == "borders")
{
Borders borders;
if (extractBorders(value, borders))
if (extractBorders(it->second, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
throw Exception("Failed to parse the 'Borders' property in section TextBox in " + m_loadedConfigFile);
}
else if (property == "scrollbar")
else if (it->first == "scrollbar")
{
if ((value.length() < 3) || (value[0] != '"') || (value[value.length()-1] != '"'))
{
TGUI_OUTPUT("TGUI error: Failed to parse value for Scrollbar in section ChatBox in " + m_loadedConfigFile + ".");
return false;
}
if ((it->second.length() < 3) || (it->second[0] != '"') || (it->second[it->second.length()-1] != '"'))
throw Exception("Failed to parse value for Scrollbar in section TextBox in " + m_loadedConfigFile + ".");
// load the scrollbar and check if it failed
m_scroll = new Scrollbar();
if (m_scroll->load(configFileFolder + value.substr(1, value.length()-2)) == false)
try
{
// load the scrollbar
m_scroll = new Scrollbar();
m_scroll->load(configFileFolder + it->second.substr(1, it->second.length()-2));
}
catch (const Exception& e)
{
// The scrollbar couldn't be loaded so it must be deleted
delete m_scroll;
m_scroll = nullptr;
return false;
}
else // The scrollbar was loaded successfully
{
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setLowValue(m_size.y);
m_scroll->setSize(m_scroll->getSize().x, static_cast<float>(m_size.y));
throw Exception("Failed to create the internal scrollbar in TextBox. " + std::string(e.what()));
}
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setLowValue(m_size.y);
m_scroll->setSize(m_scroll->getSize().x, static_cast<float>(m_size.y));
}
else
TGUI_OUTPUT("TGUI warning: Unrecognized property '" + property + "' in section ChatBox in " + m_loadedConfigFile + ".");
throw Exception("Unrecognized property '" + it->first + "' in section TextBox in " + m_loadedConfigFile + ".");
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -298,10 +272,6 @@ namespace tgui
void TextBox::setSize(float width, float height)
{
// Don't do anything when the text box wasn't loaded correctly
if (m_loaded == false)
return;
// Don't continue when line height is 0
if (m_lineHeight == 0)
return;
@ -380,10 +350,6 @@ namespace tgui
void TextBox::setText(const sf::String& text)
{
// Don't do anything when the text box wasn't loaded correctly
if (m_loaded == false)
return;
// Store the text
m_text = text;
@ -395,10 +361,6 @@ namespace tgui
void TextBox::addText(const sf::String& text)
{
// Don't do anything when the text box wasn't loaded correctly
if (m_loaded == false)
return;
// Add the text
m_text += text;
@ -718,9 +680,13 @@ namespace tgui
if (m_scroll != nullptr)
delete m_scroll;
// load the scrollbar and check if it failed
m_scroll = new Scrollbar();
if(m_scroll->load(scrollbarConfigFileFilename) == false)
try
{
// load the scrollbar
m_scroll = new Scrollbar();
m_scroll->load(scrollbarConfigFileFilename);
}
catch (const Exception& e)
{
// The scrollbar couldn't be loaded so it must be deleted
delete m_scroll;
@ -728,16 +694,14 @@ namespace tgui
return false;
}
else // The scrollbar was loaded successfully
{
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setSize(m_scroll->getSize().x, static_cast<float>(m_size.y));
m_scroll->setLowValue(m_size.y);
m_scroll->setMaximum(m_lines * m_lineHeight);
return true;
}
// Initialize the scrollbar
m_scroll->setVerticalScroll(true);
m_scroll->setSize(m_scroll->getSize().x, static_cast<float>(m_size.y));
m_scroll->setLowValue(m_size.y);
m_scroll->setMaximum(m_lines * m_lineHeight);
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -786,10 +750,6 @@ namespace tgui
bool TextBox::mouseOnWidget(float x, float y)
{
// Don't do anything when the text box wasn't loaded correctly
if (m_loaded == false)
return false;
// Get the current position
sf::Vector2f position = getPosition();
@ -823,10 +783,6 @@ namespace tgui
void TextBox::leftMousePressed(float x, float y)
{
// Don't do anything when the text box wasn't loaded correctly
if (m_loaded == false)
return;
// Set the mouse down flag
m_mouseDown = true;
@ -953,10 +909,6 @@ namespace tgui
void TextBox::leftMouseReleased(float x, float y)
{
// Don't do anything when the text box wasn't loaded correctly
if (m_loaded == false)
return;
// If there is a scrollbar then pass it the event
if (m_scroll != nullptr)
{
@ -1015,10 +967,6 @@ namespace tgui
void TextBox::mouseMoved(float x, float y)
{
// Don't do anything when the text box wasn't loaded correctly
if (m_loaded == false)
return;
if (m_mouseHover == false)
mouseEnteredWidget();
@ -1096,10 +1044,6 @@ namespace tgui
void TextBox::keyPressed(const sf::Event::KeyEvent& event)
{
// Don't do anything when the edit box wasn't loaded correctly
if (m_loaded == false)
return;
// Check if one of the correct keys was pressed
if (event.code == sf::Keyboard::Left)
{
@ -1516,10 +1460,6 @@ namespace tgui
void TextBox::textEntered(sf::Uint32 key)
{
// Don't do anything when the text box wasn't loaded correctly
if (m_loaded == false)
return;
if (m_readOnly)
return;
@ -1647,7 +1587,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TextBox::setProperty(std::string property, const std::string& value)
void TextBox::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -1675,7 +1615,7 @@ namespace tgui
if (extractBorders(value, borders))
setBorders(borders.left, borders.top, borders.right, borders.bottom);
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Borders' property.");
throw Exception("Failed to parse 'Borders' property.");
}
else if (property == "backgroundcolor")
{
@ -1719,15 +1659,12 @@ namespace tgui
}
}
else // The property didn't match
return Widget::setProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::setProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TextBox::getProperty(std::string property, std::string& value) const
void TextBox::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -1775,10 +1712,7 @@ namespace tgui
value += "," + tempValue;
}
else // The property didn't match
return Widget::getProperty(property, value);
// You pass here when one of the properties matched
return true;
Widget::getProperty(property, value);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -2065,10 +1999,6 @@ namespace tgui
void TextBox::updateDisplayedText()
{
// Don't continue when the text box wasn't loaded correctly
if (m_loaded == false)
return;
// Don't continue when line height is 0
if (m_lineHeight == 0)
return;
@ -2379,10 +2309,6 @@ namespace tgui
void TextBox::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// Don't draw anything when the text box wasn't loaded correctly
if (m_loaded == false)
return;
// Calculate the scale factor of the view
float scaleViewX = target.getSize().x / target.getView().getSize().x;
float scaleViewY = target.getSize().y / target.getView().getSize().y;

View File

@ -26,7 +26,6 @@
#include <SFML/OpenGL.hpp>
#include <TGUI/Texture.hpp>
#include <cassert>
#include <cmath>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -256,8 +255,6 @@ namespace tgui
void Texture::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
assert(getData() != nullptr);
states.transform.translate(getOrigin());
states.transform *= getTransform();

View File

@ -32,7 +32,7 @@ namespace tgui
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TextureManager::getTexture(Texture& texture, const std::string& filename, const sf::IntRect& partRect, const sf::IntRect& middleRect, bool repeated)
void TextureManager::getTexture(Texture& texture, const std::string& filename, const sf::IntRect& partRect, const sf::IntRect& middleRect, bool repeated)
{
// Look if we already had this image
auto imageIt = m_imageMap.find(filename);
@ -53,7 +53,7 @@ namespace tgui
else
texture.setTexture(*it, sf::IntRect(0, 0, it->texture.getSize().x, it->texture.getSize().y));
return true;
return;
}
}
}
@ -95,13 +95,13 @@ namespace tgui
else
texture.setTexture(imageIt->second.data.back(), sf::IntRect(0, 0, data.texture.getSize().x, data.texture.getSize().y));
return true;
return;
}
}
// The image couldn't be loaded
m_imageMap.erase(imageIt);
return false;
throw Exception("Failed to load image " + filename + ".");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -131,7 +131,6 @@ namespace tgui
}
}
TGUI_OUTPUT("TGUI warning: Can't copy texture that wasn't loaded by TextureManager.");
return false;
}
@ -164,9 +163,6 @@ namespace tgui
}
}
}
TGUI_OUTPUT("TGUI warning: Can't remove texture that wasn't loaded by TextureManager.");
return;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -27,6 +27,8 @@
#include <TGUI/SharedWidgetPtr.inl>
#include <TGUI/Container.hpp>
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace tgui
@ -36,7 +38,6 @@ namespace tgui
Widget::Widget() :
m_enabled (true),
m_visible (true),
m_loaded (false),
m_widgetPhase (0),
m_parent (nullptr),
m_opacity (255),
@ -61,7 +62,6 @@ namespace tgui
CallbackManager (copy),
m_enabled (copy.m_enabled),
m_visible (copy.m_visible),
m_loaded (copy.m_loaded),
m_widgetPhase (copy.m_widgetPhase),
m_parent (copy.m_parent),
m_opacity (copy.m_opacity),
@ -95,7 +95,6 @@ namespace tgui
m_enabled = right.m_enabled;
m_visible = right.m_visible;
m_loaded = right.m_loaded;
m_widgetPhase = right.m_widgetPhase;
m_parent = right.m_parent;
m_opacity = right.m_opacity;
@ -176,13 +175,6 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Widget::isLoaded() const
{
return m_loaded;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Widget::focus()
{
if (m_parent)
@ -389,7 +381,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Widget::setProperty(std::string property, const std::string& value)
void Widget::setProperty(std::string property, const std::string& value)
{
property = toLower(property);
@ -416,7 +408,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
m_visible = false;
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Visible' property.");
throw Exception("Failed to parse 'Visible' property.");
}
else if (property == "enabled")
{
@ -425,7 +417,7 @@ namespace tgui
else if ((value == "false") || (value == "False"))
m_enabled = false;
else
TGUI_OUTPUT("TGUI error: Failed to parse 'Enabled' property.");
throw Exception("Failed to parse 'Enabled' property.");
}
else if (property == "transparency")
{
@ -453,15 +445,12 @@ namespace tgui
}
}
else // The property didn't match
return false;
// You pass here when one of the properties matched
return true;
throw Exception("Failed to set unknown property '" + property + "'.");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Widget::getProperty(std::string property, std::string& value) const
void Widget::getProperty(std::string property, std::string& value) const
{
property = toLower(property);
@ -497,10 +486,7 @@ namespace tgui
encodeList(callbacks, value);
}
else // The property didn't match
return false;
// You pass here when one of the properties matched
return true;
throw Exception("Failed to get unknown property '" + property + "'.");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////