<commit_before>#include "Dialer.h"
#include "Helpers.h"

using namespace reza::ui;
using namespace cinder;
using namespace std;

template<typename T>
DialerT<T>::DialerT( std::string name, T value, T min, T max, Format format ) : ControlWithLabel(), mUseRef(false), mValueRef(new T(value)), mMin(min), mMax(max), mFormat(format), mCallbackFn(nullptr)
{
    setName( name );
    setValue( value );
}

template<typename T>
DialerT<T>::DialerT( std::string name, T *value, T min, T max, Format format ) : ControlWithLabel(), mUseRef(true), mValueRef(value), mMin(min), mMax(max), mFormat(format), mCallbackFn(nullptr)
{
    setName( name );
    setValue( *value );
}

template<typename T>
DialerT<T>::~DialerT()
{
    if( !mUseRef )
    {
        delete mValueRef;
    }
}

template<typename T>
void DialerT<T>::setup()
{
    string maxLengthString = "";
    string minString = toString( mMin, mFormat.mPrecision );
    string maxString = toString( mMax, mFormat.mPrecision );

    if( minString.length() > maxString.length() ) {
        maxLengthString = minString;
    } else {
        maxLengthString = "+"+maxString;
    }
    mSetWidth = maxLengthString.length() - 1;
    mNumOfPrecisionZones = maxLengthString.length();

    if( !mFormat.mSign ) {
//        mSetWidth--;
        mNumOfPrecisionZones--;
        maxLengthString = maxLengthString.substr( 1 );
    }
    
    size_t dotIndex = maxLengthString.find(".");
    if( dotIndex == string::npos )
    {
        mDotIndex = -1;
        mFormat.mPrecision = 0;
    }
    else
    {
        mDotIndex = mNumOfPrecisionZones - dotIndex;
    }
    
    if( !mLabelRef )
    {
        mLabelRef = Label::create( mName + "_LABEL", maxLengthString, mFormat.mFontSize );
        addSubView( mLabelRef );
    }
    mLabelWidth = mLabelRef->getStringWidth( maxLengthString );
    mDigitWidth = mLabelWidth / (float) mNumOfPrecisionZones;
    mLabelRef->setOrigin( vec2( mPadding.mLeft, mPadding.mTop ) );
    vec2 size = mLabelRef->getSize();
    setSize( vec2( size.x + mPadding.mLeft + mPadding.mRight, size.y + mPadding.mTop + mPadding.mBottom ) );
    updateLabel();
    View::setup();
}

template<typename T>
void DialerT<T>::update()
{
    if( mUseRef )
    {
        T scaledValue = lmap<double>( mValue, 0.0, 1.0, mMin, mMax );
        if( (*mValueRef) != scaledValue )
        {
            setValue( *mValueRef );
        }
    }
    View::update();
}

template<typename T>
void DialerT<T>::trigger( bool recursive )
{
    if( mCallbackFn )
    {
        mCallbackFn( getValue() );
    }
    Control::trigger( recursive );
}

template<typename T>
JsonTree DialerT<T>::save()
{
    JsonTree tree = View::save();
    tree.addChild( JsonTree( "VALUE",  getValue() ) );
    return tree;
}

template<typename T>
void DialerT<T>::load( const ci::JsonTree &data )
{
    if( data.hasChild( "VALUE" ) )
    {
        setValue( data.getValueForKey<T>("VALUE") );
        trigger();
    }
    View::load( data );
}

template<typename T>
void DialerT<T>::setValue( T value )
{
    mValue = lmap<double>( value, mMin, mMax, 0.0, 1.0 );
    updateValueRef();
    updateLabel();
    setNeedsDisplay();
}

template<typename T>
T DialerT<T>::getValue()
{
    return *mValueRef;
}

template<typename T>
double DialerT<T>::getNormalizedValue()
{
    return max( min( mValue, 1.0 ), 0.0 );
}
template<typename T>
void DialerT<T>::updateValueRef()
{
    *mValueRef = lmap<double>( mValue, 0.0, 1.0, mMin, mMax );
}

template<typename T>
void DialerT<T>::updateLabel()
{
    T value = getValue();
    string label = "";
    if( mFormat.mSign ) { label += ( value > 0 ? "+" : "-" ); }
    
    label += toString( fabs(value), mFormat.mPrecision, mSetWidth, '0' );

    if( mFormat.mLabel ) { label += " " + mName; }
    
    setLabel( label );
}

template<typename T>
DialerT<T>* DialerT<T>::setCallback( const std::function<void(T)> &callback )
{
    mCallbackFn = callback;
    return this;
}

template<typename T>
void DialerT<T>::setMax( T max, bool keepValueTheSame )
{
    setMinAndMax( max, mMin, keepValueTheSame );
}

template<typename T>
T DialerT<T>::getMax()
{
    return mMax;
}

template<typename T>
void DialerT<T>::setMin( T min, bool keepValueTheSame )
{
    setMinAndMax( mMax, min, keepValueTheSame );
}

template<typename T>
T DialerT<T>::getMin()
{
    return mMin;
}

template<typename T>
vec2 DialerT<T>::getMaxAndMin()
{
    return vec2( mMax, mMin );
}

template<typename T>
void DialerT<T>::setMinAndMax( T min, T max, bool keepValueTheSame )
{<commit_msg>fixed setMin & setMax function in dialer<commit_after>#include "Dialer.h"
#include "Helpers.h"

using namespace reza::ui;
using namespace cinder;
using namespace std;

template<typename T>
DialerT<T>::DialerT( std::string name, T value, T min, T max, Format format ) : ControlWithLabel(), mUseRef(false), mValueRef(new T(value)), mMin(min), mMax(max), mFormat(format), mCallbackFn(nullptr)
{
    setName( name );
    setValue( value );
}

template<typename T>
DialerT<T>::DialerT( std::string name, T *value, T min, T max, Format format ) : ControlWithLabel(), mUseRef(true), mValueRef(value), mMin(min), mMax(max), mFormat(format), mCallbackFn(nullptr)
{
    setName( name );
    setValue( *value );
}

template<typename T>
DialerT<T>::~DialerT()
{
    if( !mUseRef )
    {
        delete mValueRef;
    }
}

template<typename T>
void DialerT<T>::setup()
{
    string maxLengthString = "";
    string minString = toString( mMin, mFormat.mPrecision );
    string maxString = toString( mMax, mFormat.mPrecision );

    if( minString.length() > maxString.length() ) {
        maxLengthString = minString;
    } else {
        maxLengthString = "+"+maxString;
    }
    mSetWidth = maxLengthString.length() - 1;
    mNumOfPrecisionZones = maxLengthString.length();

    if( !mFormat.mSign ) {
//        mSetWidth--;
        mNumOfPrecisionZones--;
        maxLengthString = maxLengthString.substr( 1 );
    }
    
    size_t dotIndex = maxLengthString.find(".");
    if( dotIndex == string::npos )
    {
        mDotIndex = -1;
        mFormat.mPrecision = 0;
    }
    else
    {
        mDotIndex = mNumOfPrecisionZones - dotIndex;
    }
    
    if( !mLabelRef )
    {
        mLabelRef = Label::create( mName + "_LABEL", maxLengthString, mFormat.mFontSize );
        addSubView( mLabelRef );
    }
    mLabelWidth = mLabelRef->getStringWidth( maxLengthString );
    mDigitWidth = mLabelWidth / (float) mNumOfPrecisionZones;
    mLabelRef->setOrigin( vec2( mPadding.mLeft, mPadding.mTop ) );
    vec2 size = mLabelRef->getSize();
    setSize( vec2( size.x + mPadding.mLeft + mPadding.mRight, size.y + mPadding.mTop + mPadding.mBottom ) );
    updateLabel();
    View::setup();
}

template<typename T>
void DialerT<T>::update()
{
    if( mUseRef )
    {
        T scaledValue = lmap<double>( mValue, 0.0, 1.0, mMin, mMax );
        if( (*mValueRef) != scaledValue )
        {
            setValue( *mValueRef );
        }
    }
    View::update();
}

template<typename T>
void DialerT<T>::trigger( bool recursive )
{
    if( mCallbackFn )
    {
        mCallbackFn( getValue() );
    }
    Control::trigger( recursive );
}

template<typename T>
JsonTree DialerT<T>::save()
{
    JsonTree tree = View::save();
    tree.addChild( JsonTree( "VALUE",  getValue() ) );
    return tree;
}

template<typename T>
void DialerT<T>::load( const ci::JsonTree &data )
{
    if( data.hasChild( "VALUE" ) )
    {
        setValue( data.getValueForKey<T>("VALUE") );
        trigger();
    }
    View::load( data );
}

template<typename T>
void DialerT<T>::setValue( T value )
{
    mValue = lmap<double>( value, mMin, mMax, 0.0, 1.0 );
    updateValueRef();
    updateLabel();
    setNeedsDisplay();
}

template<typename T>
T DialerT<T>::getValue()
{
    return *mValueRef;
}

template<typename T>
double DialerT<T>::getNormalizedValue()
{
    return max( min( mValue, 1.0 ), 0.0 );
}
template<typename T>
void DialerT<T>::updateValueRef()
{
    *mValueRef = lmap<double>( mValue, 0.0, 1.0, mMin, mMax );
}

template<typename T>
void DialerT<T>::updateLabel()
{
    T value = getValue();
    string label = "";
    if( mFormat.mSign ) { label += ( value > 0 ? "+" : "-" ); }
    
    label += toString( fabs(value), mFormat.mPrecision, mSetWidth, '0' );

    if( mFormat.mLabel ) { label += " " + mName; }
    
    setLabel( label );
}

template<typename T>
DialerT<T>* DialerT<T>::setCallback( const std::function<void(T)> &callback )
{
    mCallbackFn = callback;
    return this;
}

template<typename T>
void DialerT<T>::setMax( T max, bool keepValueTheSame )
{
    setMinAndMax( mMin, max, keepValueTheSame );
}

template<typename T>
T DialerT<T>::getMax()
{
    return mMax;
}

template<typename T>
void DialerT<T>::setMin( T min, bool keepValueTheSame )
{
    setMinAndMax( min, mMax, keepValueTheSame );
}

template<typename T>
T DialerT<T>::getMin()
{
    return mMin;
}

template<typename T>
vec2 DialerT<T>::getMaxAndMin()
{
    return vec2( mMax, mMin );
}

template<typename T>
void DialerT<T>::setMinAndMax( T min, T max, bool keepValueTheSame )
{
{'fraction_non_alphanumeric': 0.1513078026707869, 'fraction_numerical': 0.005959607107383291, 'mean_word_length': 3.342117872544322, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2679778', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3271, 'n_words': 699}