Skip to content

File evabPercent.h

File List > src > evabPercent.h

Go to the documentation of this file

#pragma once

#include "evabInputInt.h"
#include "evabInputFloat.h"

namespace evab
{
    template <typename TElement, unsigned char kStep = 0>
    class Percent : public TElement
    {
        static_assert(kStep <= 50, "kStep must be <= 50");

        unsigned char mStep;

    public:
        template <typename... Args>
        Percent(Args &&...args)
            : TElement(args...)
        {
            if (kStep > 0)
                mStep = kStep;
            else if (TElement::Count() > 1)
                mStep = max(1, 100 / (TElement::Count() - 1));
            else
                mStep = 1;
        }

        void SetPercent(unsigned char aPercent)
        {
            aPercent = constrain(aPercent, 0, 100);
            unsigned char index = aPercent / mStep;
            if (index >= TElement::Count())
                index = TElement::Count() - 1;
            TElement::Select(index);
        }

        unsigned char GetPercent() const
        {
            signed short selected = TElement::Selected();
            if (selected < 0)
                return 0;
            return selected * mStep;
        }

        unsigned char GetStep() const { return mStep; }
    };

}