Skip to content

Progress Bar

Hapax edited this page Jan 27, 2016 · 6 revisions

Introduction

A class to draw a progress bar.

Usage

Declaration

sw::ProgressBar progressBar;
creates a progress bar with the default size

sw::ProgressBar progressBar(sf::Vector2f size);
creates a progress bar with the specified size (sf::Vector2f)

Drawing

This class inherits from sf::Drawable so it is drawn in the same way as all SFML drawables:
window.draw(progressBar);
where window is an sf::RenderWindow.

Transformations

This class inherits from sf::Transformable so it has all the usual SFML transformations available.

Manipulation

Progress

"Progress" is value that is represented by this object

  • setRatio(float)
    sets the progress in the range from 0 to 1

  • setPercentage(float)
    sets the progress in the range from 0 to 100

  • setFromValueInRange(T value, T range)
    sets the progress as value in the range from 0 to range. all parameters must be the same type

  • setFromValueInRange(T value, T minimum, T maximum)
    sets the progress as value in the range from minimum to maximum. all parameters must be the same type

Visual Representation

  • setSize(sf::Vector2f)
    sets the size of the progress bar (this does not include any external frame/outline)

  • setFrameThickness(float)
    sets the thickness of the frame. A positive value causes the frame to extend outwards from the bar's size (is taken into account when retrieving bounds but not size"). A negative value causes the frame to extend inwards from the bar's outer edge. Note that this means that it will be hidden behind the progression bar but not in the remaining area. Zero causes the frame to be invisible

  • setFrameColor(sf::Color)
    sets the sf::Color of the frame

  • setBackgroundColor(sf::Color)
    sets the sf::Color of the background - the rectangle that is covered by a completed progress bar, which is always displayed - behind the progress indicator rectangle

  • setColor(sf::Color)
    sets the sf::Color of the progress indicator rectangle

  • setShowBar(bool)
    enables or disables showing the actual bar representing the progress amount

  • setShowBackgroundAndFrame(bool)
    enables or disables showing the background and frame

Texturing

  • setTexture(texture[, resetRect])
    sets the bar's texture to the sf::Texture passed in texture. resetRect causes the texture rectangle to be set to the texture's size. If both parameters are omitted, the texture is cleared/nullified

  • setBackgroundTexture(texture[, resetRect])
    sets the background's texture to the sf::Texture passed in texture. resetRect causes the texture rectangle to be set to the texture's size. If both parameters are omitted, the texture is cleared/nullified

  • setTextureRect(textureRectangle)
    sets the bar's texture rectangle to the sf::IntRect passed in textureRectangle. Note that this is the rectangle used when the progress is "complete" (i.e. at 100%). When the progress is lower, only a portion of this rectangle will be shown

  • setBackgroundTextureRect(textureRectangle)
    sets the background's texture rectangle to the sf::IntRect passed in textureRectangle

Information

  • getRatio()
    returns the current progression (float) in the range from 0 to 1

  • getPercentage()
    returns the current progression (float) in the range from 0 to 100

  • getSize()
    returns a [sf::Vector] of the size of the progress bar (not including any external frame)

  • getFrameThickness()
    returns the thickness of the frame (positive, negative and zero values are all valid)

  • getFrameColor()
    returns the sf::Color of the frame

  • getBackgroundColor()
    returns the sf::Color of the background

  • getColor()
    returns the sf::Color of the progression indicator rectangle

  • getShowBar()
    returns a bool representing if the actual bar representing the progress is to be shown

  • getShowBackgroundAndFrame()
    returns a bool representing if the background and frame are to be shown

  • getTexture()
    returns a const reference to the sf::Texture that is used by the bar

  • getBackgroundTexture()
    returns a const reference to the sf::Texture that is used by the background

  • getLocalBounds()
    returns the local bounding rectangle (sf::FloatRect). This includes any external frame, which means that left and top may be non-zero (the top-left of the progression indicator rectangle is at zero locally)

  • getGlobalBounds()
    returns the global bounding rectangle (sf::FloatRect). This includes any external frame after any transformations

  • getAnchorProgressTop()
    returns the anchor point (sf::Vector2f) of the top of the current progress, after all transformations

  • getAnchorProgressCenter()
    returns the anchor point (sf::Vector2f) of the centre of the current progress, after all transformations

  • getAnchorProgressBottom()
    returns the anchor point (sf::Vector2f) of the bottom of the current progress, after all transformations

Simple Example

#include <SFML/Graphics.hpp>
#include <SelbaWard/ProgressBar.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(100, 30), "Progress Bar simple example", sf::Style::Default);
    sw::ProgressBar progressBar;
    progressBar.setShowBackgroundAndFrame(true);
    progressBar.setPosition((sf::Vector2f(window.getSize()) - progressBar.getSize()) / 2.f);
    bool isIncreasing{ true };
    sf::Clock clock;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        const float frame{ clock.restart().asSeconds() * 0.3f };
        const float target{ isIncreasing ? progressBar.getRatio() + frame : progressBar.getRatio() - frame };
        if (target < 0.f)
            isIncreasing = true;
        else if (target > 1.f)
            isIncreasing = false;
        progressBar.setRatio(target);
        window.clear(sf::Color::Blue);
        window.draw(progressBar);
        window.display();
    }
    return EXIT_SUCCESS;
}

The code above displays (it animates to full and empty):
Simple Example

(Progress Bar v1.1)