⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sceneramptool.cpp

📁 hl2 source code. Do not use it illegal.
💻 CPP
📖 第 1 页 / 共 4 页
字号:
int SceneRampTool::GetPixelForTimeValue( float time, bool *clipped /*=NULL*/ )
{
	if ( clipped )
	{
		*clipped = false;
	}

	float st, ed;
	GetStartAndEndTime( st, ed );

	float frac = ( time - st ) / ( ed - st );
	if ( frac < 0.0 || frac > 1.0 )
	{
		if ( clipped )
		{
			*clipped = true;
		}
	}

	int pixel = ( int )( frac * w2() );
	return pixel;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : mx - 
//			clip - 
// Output : float
//-----------------------------------------------------------------------------
float SceneRampTool::GetTimeValueForMouse( int mx, bool clip /*=false*/)
{
	float st, ed;
	GetStartAndEndTime( st, ed );

	if ( clip )
	{
		if ( mx < 0 )
		{
			return st;
		}
		if ( mx > w2() )
		{
			return ed;
		}
	}

	float frac = (float)( mx )  / (float)( w2() );
	return st + frac * ( ed - st );
}


//-----------------------------------------------------------------------------
// Purpose: 
// Output : float
//-----------------------------------------------------------------------------
float SceneRampTool::GetTimeZoomScale( void )
{
	return ( float )m_nTimeZoom / 100.0f;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : scale - 
//-----------------------------------------------------------------------------
void SceneRampTool::SetTimeZoomScale( int scale )
{
	m_nTimeZoom = scale;
}

void SceneRampTool::OnChangeScale( void )
{
	// Zoom time in  / out
	CInputParams params;
	memset( &params, 0, sizeof( params ) );

	strcpy( params.m_szDialogTitle, "Change Zoom" );
	strcpy( params.m_szPrompt, "New scale (e.g., 2.5x):" );

	Q_snprintf( params.m_szInputText, sizeof( params.m_szInputText ), "%.2f", (float)m_nTimeZoom / 100.0f );

	if ( !InputProperties( &params ) )
		return;

	m_nTimeZoom = clamp( (int)( 100.0f * atof( params.m_szInputText ) ), 1, MAX_TIME_ZOOM );

	m_nLastHPixelsNeeded = -1;
	m_nLeftOffset= 0;
	InvalidateLayout();
	Con_Printf( "Zoom factor %i %%\n", m_nTimeZoom );
}

void SceneRampTool::DrawSceneEnd( CChoreoWidgetDrawHelper& drawHelper )
{
	CChoreoScene *s = GetSafeScene();
	if ( !s )
		return;

	float duration = s->FindStopTime();
	if ( !duration )
		return;

	int leftx = GetPixelForTimeValue( duration );
	if ( leftx >= w2() )
		return;

	RECT rcSample;
	GetSampleTrayRect( rcSample );

	drawHelper.DrawColoredLine(
		COLOR_CHOREO_ENDTIME, PS_SOLID, 1,
		leftx, rcSample.top, leftx, rcSample.bottom );
}

void SceneRampTool::GetSampleTrayRect( RECT& rc )
{
	rc.left = 0;
	rc.right = w2();
	rc.top = GetCaptionHeight() + 65;

	rc.bottom = h2() - m_nScrollbarHeight-2;
}

void SceneRampTool::DrawSamplesSimple( CChoreoWidgetDrawHelper& drawHelper, CChoreoScene *scene, bool clearbackground, COLORREF sampleColor, RECT &rcSamples )
{
	if ( clearbackground )
	{
		drawHelper.DrawFilledRect( RGB( 230, 230, 215 ), rcSamples );
	}

	if ( !scene )
		return;

	float starttime = 0.0f;
	float endtime = scene->FindStopTime();

	COLORREF lineColor = sampleColor;
	COLORREF dotColor = RGB( 0, 0, 255 );
	COLORREF dotColorSelected = RGB( 240, 80, 20 );

	float fx = 0.0f;

	int width = rcSamples.right  - rcSamples.left;
	if ( width <= 0.0f )
		return;

	int height = rcSamples.bottom - rcSamples.top;
	int bottom = rcSamples.bottom;

	float timestepperpixel = endtime / (float)width;

	float stoptime = endtime;
	
	float prev_value = scene->GetSceneRampIntensity( starttime );
	int prev_x = rcSamples.left;
	float prev_t = 0.0f;

	for ( float x = rcSamples.left; x < rcSamples.right; x+=3 )
	{
		float t = (float)( x - rcSamples.left ) * timestepperpixel;

		float value =  scene->GetSceneRampIntensity( starttime + t );

		// Draw segment
		drawHelper.DrawColoredLine( lineColor, PS_SOLID, 1,
			prev_x, bottom - prev_value * height,
			x, bottom - value * height );

		prev_x = x;
		prev_t = t;
		prev_value = value;
	}
}

void SceneRampTool::DrawSamples( CChoreoWidgetDrawHelper& drawHelper, RECT &rcSamples )
{
	drawHelper.DrawFilledRect( RGB( 230, 230, 215 ), rcSamples );

	CChoreoScene *s = GetSafeScene();
	if ( !s )
		return;

	int rampCount = s->GetSceneRampCount();
	if ( !rampCount )
		return;

	float starttime;
	float endtime;

	GetStartAndEndTime( starttime, endtime );

	COLORREF lineColor = RGB( 0, 0, 255 );
	COLORREF dotColor = RGB( 0, 0, 255 );
	COLORREF dotColorSelected = RGB( 240, 80, 20 );

	float fx = 0.0f;

	int height = rcSamples.bottom - rcSamples.top;
	int bottom = rcSamples.bottom;

	float timestepperpixel = 1.0f / GetPixelsPerSecond();

	float stoptime = min( endtime, s->FindStopTime() );
	
	float prev_t = starttime;
	float prev_value = s->GetSceneRampIntensity( prev_t );

	for ( float t = starttime-timestepperpixel; t <= stoptime; t += timestepperpixel )
	{
		float value =  s->GetSceneRampIntensity( t );

		int prevx, x;

		bool clipped1, clipped2;
		x = GetPixelForTimeValue( t, &clipped1 );
		prevx = GetPixelForTimeValue( prev_t, &clipped2 );

		if ( !clipped1 && !clipped2 )
		{
			// Draw segment
			drawHelper.DrawColoredLine( lineColor, PS_SOLID, 1,
				prevx, bottom - prev_value * height,
				x, bottom - value * height );
		}

		prev_t = t;
		prev_value = value;

	}

	for ( int sample = 0; sample < rampCount; sample++ )
	{
		CExpressionSample *start = s->GetSceneRamp( sample );

		/*
		int pixel = (int)( ( start->time / event_time ) * width + 0.5f);
		int x = m_rcBounds.left + pixel;
		float roundedfrac = (float)pixel / (float)width;
		*/
		float value = start->value;
		bool clipped = false;
		int x = GetPixelForTimeValue( start->time, &clipped );
		if ( clipped )
			continue;
		int y = bottom - value * height;

		int dotsize = 4;
		int dotSizeSelected = 5;

		COLORREF clr = dotColor;
		COLORREF clrSelected = dotColorSelected;

		drawHelper.DrawCircle( 
			start->selected ? clrSelected : clr, 
			x, y, 
			start->selected ? dotSizeSelected : dotsize,
			true );
	}
}

CExpressionSample *SceneRampTool::GetSampleUnderMouse( mxEvent *event )
{
	CChoreoScene *s = GetSafeScene();
	if ( !s )
		return NULL;

	RECT rcSamples;
	GetSampleTrayRect( rcSamples );

	POINT pt;
	pt.x = event->x;
	pt.y = event->y;

	if ( !PtInRect( &rcSamples, pt ) )
		return NULL;

	float timeperpixel = 1.0f / g_pSceneRampTool->GetPixelsPerSecond();
	float closest_dist = 999999.f;
	CExpressionSample *bestsample = NULL;

	// Search for closes
	float t = GetTimeValueForMouse( event->x, false );

	for ( int i = 0; i < s->GetSceneRampCount(); i++ )
	{
		CExpressionSample *sample = s->GetSceneRamp( i );
		Assert( sample );

		float dist = fabs( sample->time - t );
		if ( dist < closest_dist )
		{
			bestsample = sample;
			closest_dist = dist;
		}

	}

	// Not close to any of them!!!
	if ( closest_dist > ( 5.0f * timeperpixel ) )
		return NULL;

	return bestsample;
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void SceneRampTool::SelectPoints( void )
{
	RECT rcSelection;
	
	rcSelection.left = m_nStartX < m_nLastX ? m_nStartX : m_nLastX;
	rcSelection.right = m_nStartX < m_nLastX ? m_nLastX : m_nStartX;

	rcSelection.top = m_nStartY < m_nLastY ? m_nStartY : m_nLastY;
	rcSelection.bottom = m_nStartY < m_nLastY ? m_nLastY : m_nStartY;

	InflateRect( &rcSelection, 3, 3 );

	RECT rcSamples;
	GetSampleTrayRect( rcSamples );

	int width = w2();
	int height = rcSamples.bottom - rcSamples.top;

	CChoreoScene *s = GetSafeScene();
	if ( !s )
		return;

	float duration = s->FindStopTime();
	if ( !duration )
		return;

	float fleft = (float)GetTimeValueForMouse( rcSelection.left );
	float fright = (float)GetTimeValueForMouse( rcSelection.right );

	//fleft *= duration;
	//fright *= duration;

	float ftop = (float)( rcSelection.top - rcSamples.top ) / (float)height;
	float fbottom = (float)( rcSelection.bottom - rcSamples.top ) / (float)height;

	fleft = clamp( fleft, 0.0f, duration );
	fright = clamp( fright, 0.0f, duration );
	ftop = clamp( ftop, 0.0f, 1.0f );
	fbottom = clamp( fbottom, 0.0f, 1.0f );

	float timestepperpixel = 1.0f / GetPixelsPerSecond();
	float yfracstepperpixel = 1.0f / (float)height;

	float epsx = 2*timestepperpixel;
	float epsy = 2*yfracstepperpixel;

	for ( int i = 0; i < s->GetSceneRampCount(); i++ )
	{
		CExpressionSample *sample = s->GetSceneRamp( i );
		
		if ( sample->time + epsx < fleft )
			continue;

		if ( sample->time - epsx > fright )
			continue;

		//if ( (1.0f - sample->value ) + epsy < ftop )
		//	continue;

		//if ( (1.0f - sample->value ) - epsy > fbottom )
		//	continue;

		sample->selected = true;
	}

	redraw();
}

//-----------------------------------------------------------------------------
// Purpose: 
// Output : int
//-----------------------------------------------------------------------------
int SceneRampTool::CountSelectedSamples( void )
{
	int c = 0;
	CChoreoScene *s = GetSafeScene();
	if ( s )
	{
		int count = s->GetSceneRampCount();

		for ( int i = 0; i < count; i++ )
		{
			CExpressionSample *sample = s->GetSceneRamp( i );
			if ( !sample->selected )
				continue;

			c++;
		}
	}
	return c;
}

void SceneRampTool::MoveSelectedSamples( float dfdx, float dfdy )
{
	int selecteditems = CountSelectedSamples();
	if ( !selecteditems )
		return;

	CChoreoScene *s = GetSafeScene();
	if ( !s )
		return;

	int c = s->GetSceneRampCount();

	float duration = s->FindStopTime();
	//dfdx *= duration;

	for ( int i = 0; i < c; i++ )
	{
		CExpressionSample *sample = s->GetSceneRamp( i );
		if ( !sample || !sample->selected )
			continue;

		sample->time += dfdx;
		sample->time = clamp( sample->time, 0.0f, duration );

		sample->value -= dfdy;
		sample->value = clamp( sample->value, 0.0f, 1.0f );
	}
			
	s->ResortSceneRamp();
	redraw();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void SceneRampTool::DeselectAll( void )
{
	int i;

	int selecteditems = CountSelectedSamples();
	if ( !selecteditems )
		return;

	CChoreoScene *s = GetSafeScene();
	Assert( s );
	if ( !s )
		return;

	for ( i = s->GetSceneRampCount() - 1; i >= 0 ; i-- )
	{
		CExpressionSample *sample = s->GetSceneRamp( i );
		sample->selected = false;
	}
	
	redraw();
}

void SceneRampTool::SelectAll( void )
{
	int i;

	CChoreoScene *s = GetSafeScene();
	Assert( s );
	if ( !s )
		return;

	for ( i = s->GetSceneRampCount() - 1; i >= 0 ; i-- )
	{
		CExpressionSample *sample = s->GetSceneRamp( i );
		sample->selected = true;
	}
	
	redraw();
}

void SceneRampTool::Delete( void )
{
	int i;

	CChoreoScene *s = GetSafeScene();
	if ( !s )
		return;

	int selecteditems = CountSelectedSamples();
	if ( !selecteditems )
		return;

	g_pChoreoView->SetDirty( true );
	g_pChoreoView->PushUndo( "Delete scene ramp points" );

	for ( i = s->GetSceneRampCount() - 1; i >= 0 ; i-- )
	{
		CExpressionSample *sample = s->GetSceneRamp( i );
		if ( !sample->selected )
			continue;

		s->DeleteSceneRamp( i );
	}

	g_pChoreoView->PushRedo( "Delete scene ramp points" );

	redraw();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -