示例#1
0
void SliderT<T>::input( ci::app::MouseEvent &event )
{
    vec2 hp = getHitPercent( event.getPos() );
    hp.x = min( max( hp.x, 0.0f ), 1.0f );

    float value = lmap<double>( hp.x, 0.0, 1.0, mMin, mMax );
    if( mFormat.mSticky || event.isMetaDown() )
    {
        value = ceil( value / mFormat.mStickyValue ) * mFormat.mStickyValue;
    }
    
    setValue( value );
}
示例#2
0
void MultiSlider::input( ci::app::MouseEvent &event )
{
	vec2 pt = event.getPos();
	vec2 hp = getHitPercent( event.getPos() );
	hp.x = std::min( std::max( hp.x, 0.0f ), 1.0f );

	if( event.isMetaDown() ) {
		setValue( hp.x );
		return;
	}

	if( mHitKey == "" ) {
		mHitKey = getKey( pt );
		mHoverKey = "";
	}

	if( mHitKey != "" ) {
		setValue( mHitKey, lmap<double>( hp.x, 0.0, 1.0, mDataMap[mHitKey]->mMin, mDataMap[mHitKey]->mMax ) );
	}
}
示例#3
0
void BSplineEditor::input( const ci::app::MouseEvent &event )
{
	vec2 hp = getHitPercent( event.getPos() );
	hp.x = std::min( std::max( hp.x, 0.0f ), 1.0f );
	hp.y = std::min( std::max( hp.y, 0.0f ), 1.0f );
	hp = vec2( lmap<float>( hp.x, 0.0, 1.0, mFormat.mMin.x, mFormat.mMax.x ),
		lmap<float>( hp.y, 0.0, 1.0, mFormat.mMin.y, mFormat.mMax.y ) );

	if( event.isMetaDown() || mFormat.mSticky ) {
		if( mFormat.mGrid ) {
			float w = mHitRect.getWidth();
			float h = mHitRect.getHeight();
			float sv = h > w ? h : w;
			sv = mFormat.mGridSize / sv;
			hp.x = ceil( hp.x / sv ) * sv;
			hp.y = ceil( hp.y / sv ) * sv;
		}
		else {
			hp.x = ceil( hp.x / mFormat.mStickyValue ) * mFormat.mStickyValue;
			hp.y = ceil( hp.y / mFormat.mStickyValue ) * mFormat.mStickyValue;
		}
	}

	if( getState() == State::NORMAL || getState() == State::OVER ) {
		mHitIndex = -1;
	}
	else if( mHitIndex == -1 ) {
		float distance = 100000.0;
		int index = -1;
		std::map<float, int> distMap;
		int totalCtrlPts = mControlPoints.size();
		bool addToEnd = true;

		for( int i = 0; i < totalCtrlPts; i++ ) {
			float len = length( hp - mControlPoints[i] );
			distMap[len] = i;
			if( len < distance ) {
				distance = len;
				index = i;
			}
		}

		if( distance < ( length( mFormat.mMax - mFormat.mMin ) * mFormat.mThreshold ) ) {
			mHitIndex = index;
			if( ( event.isRight() || event.isMetaDown() ) ) {
				mControlPoints.erase( mControlPoints.begin() + mHitIndex );
				updateSplineRef();
				mHitIndex = -1;
			}
			addToEnd = false;
		}
		else {
			float thres = 0.1f * length( mFormat.mMax - mFormat.mMin );
			float tpp = 1.0f / float( totalCtrlPts - 1.0f );
			float bestDistance = 1000000;
			float bestTime = -1;
			int bestIndex = -1;
			if( mValid ) {
				for( auto &it : distMap ) {
					int id = it.second;

					int bid = ( id > 0 ) ? ( id - 1 ) : id;
					int eid = ( ( id + 1 ) == totalCtrlPts ) ? id : ( id + 1 );
					if( bid == eid ) {
						continue;
					}
					float startTime = bid * tpp;
					float endTime = eid * tpp;

					for( float t = startTime; t <= endTime; t += 0.01f ) {
						vec2 pt = mSplineRef->getPosition( t );
						float d = length( hp - pt );
						if( d < bestDistance ) {
							bestDistance = d;
							bestTime = t;
							bestIndex = id;
						}
					}

					if( bestDistance < thres ) {
						int insertIndex = bid + 1;
						if( bestTime > ( id * tpp ) ) {
							insertIndex = id + 1;
						}
						mControlPoints.insert( mControlPoints.begin() + insertIndex, hp );
						updateSplineRef();
						addToEnd = false;
						break;
					}
				}
			}
		}

		if( addToEnd ) {
			mHitIndex = mControlPoints.size();
			mControlPoints.push_back( hp );
			updateSplineRef();
		}
	}
	else {
		mControlPoints[mHitIndex] = hp;
		updateSplineRef();
	}
}