2013-08-27 15:48:13 +02:00
|
|
|
#ifndef COMPONENTS_LOADINGLISTENER_H
|
|
|
|
#define COMPONENTS_LOADINGLISTENER_H
|
|
|
|
|
2022-08-06 16:55:09 +02:00
|
|
|
#include <memory>
|
2015-12-15 21:03:56 +01:00
|
|
|
#include <string>
|
|
|
|
|
2013-08-27 15:48:13 +02:00
|
|
|
namespace Loading
|
|
|
|
{
|
|
|
|
class Listener
|
|
|
|
{
|
|
|
|
public:
|
2015-12-15 21:03:56 +01:00
|
|
|
/// Set a text label to show on the loading screen.
|
2015-12-15 21:14:25 +01:00
|
|
|
/// @param label The label
|
|
|
|
/// @param important Is the label considered important to show?
|
|
|
|
/// @note "non-important" labels may not show on screen if the loading process went so fast
|
|
|
|
/// that the implementation decided not to show a loading screen at all. "important" labels
|
|
|
|
/// will show in a separate message-box if the loading screen was not shown.
|
2021-05-26 23:19:22 +02:00
|
|
|
virtual void setLabel(const std::string& label, bool important = false) {}
|
2013-08-27 15:48:13 +02:00
|
|
|
|
2015-12-15 21:03:56 +01:00
|
|
|
/// Start a loading sequence. Must call loadingOff() when done.
|
|
|
|
/// @note To get the loading screen to actually update, you must call setProgress / increaseProgress
|
|
|
|
/// periodically.
|
|
|
|
/// @note It is best to use the ScopedLoad object instead of using loadingOn()/loadingOff() directly,
|
|
|
|
/// so that the loading is exception safe.
|
2024-06-06 11:23:05 +03:00
|
|
|
virtual void loadingOn() {}
|
2015-11-13 23:12:07 +01:00
|
|
|
virtual void loadingOff() {}
|
2013-08-27 15:48:13 +02:00
|
|
|
|
2015-12-15 21:03:56 +01:00
|
|
|
/// Set the total range of progress (e.g. the number of objects to load).
|
2015-11-13 23:12:07 +01:00
|
|
|
virtual void setProgressRange(size_t range) {}
|
2015-12-15 21:03:56 +01:00
|
|
|
/// Set current progress. Valid range is [0, progressRange)
|
2015-11-13 23:12:07 +01:00
|
|
|
virtual void setProgress(size_t value) {}
|
2015-12-15 21:03:56 +01:00
|
|
|
/// Increase current progress, default by 1.
|
2015-11-13 23:12:07 +01:00
|
|
|
virtual void increaseProgress(size_t increase = 1) {}
|
2020-03-26 12:07:32 +04:00
|
|
|
|
|
|
|
virtual ~Listener() = default;
|
2013-08-27 15:48:13 +02:00
|
|
|
};
|
|
|
|
|
2022-08-06 16:55:09 +02:00
|
|
|
struct LoadingOff
|
|
|
|
{
|
|
|
|
void operator()(Listener* listener) const
|
|
|
|
{
|
|
|
|
if (listener != nullptr)
|
|
|
|
listener->loadingOff();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-15 21:03:56 +01:00
|
|
|
/// @brief Used for stopping a loading sequence when the object goes out of scope
|
2013-08-27 15:48:13 +02:00
|
|
|
struct ScopedLoad
|
|
|
|
{
|
2022-08-06 16:55:09 +02:00
|
|
|
std::unique_ptr<Listener, LoadingOff> mListener;
|
|
|
|
|
|
|
|
explicit ScopedLoad(Listener* listener)
|
|
|
|
: mListener(listener)
|
|
|
|
{
|
|
|
|
if (mListener != nullptr)
|
|
|
|
mListener->loadingOn();
|
|
|
|
}
|
2013-08-27 15:48:13 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|