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

📄 hud_minimap.cpp

📁 hl2 source code. Do not use it illegal.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		break;

	case MINIMAP_CLAMP:
		{
			// Clamp the position to lie within the minimap
			xfraction = clamp(xfraction, 0, 1);
			yfraction = clamp(yfraction, 0, 1);
		}
		break;

	case MINIMAP_NOCLIP:
		{
			// See if it's off screen
			if ( xfraction < 0.0 || xfraction > 1.0 ||
				 yfraction < 0.0 || yfraction > 1.0 )
			{
				inside = false;
			}
		}
		break;
	}

	outx = xfraction * wide;
	outy = yfraction * tall;
	
	return inside;
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *mapname - 
//-----------------------------------------------------------------------------
void CMinimapPanel::LevelInit( const char *mapname )
{
	SetBackgroundMaterials( MapData().m_Minimap.m_szBackgroundMaterial );

	m_nZoomLevel = DEFAULT_ZOOM_LEVEL;

	HOOK_MESSAGE( MinimapPulse );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMinimapPanel::LevelShutdown( void )
{
}

//-----------------------------------------------------------------------------
// Sets the background material
//-----------------------------------------------------------------------------
void CMinimapPanel::SetBackgroundMaterials( const char *pMaterialName )
{
	int i;
	for ( i = 0; i < MAX_ACT_TEAMS; i++ )
	{
		delete m_pBackground[ i ];
		m_pBackground[ i ] = NULL;
	}

	if ( pMaterialName[ 0 ] )
	{
		for ( i = 0; i < MAX_ACT_TEAMS; i++ )
		{
			char teammaterial[ 512 ];

			Q_snprintf( teammaterial, sizeof( teammaterial ), "%s_team%i",
				pMaterialName, i + 1 );

			// If a _team# version exists, use that, otherwise, use the default
			if ( vgui::filesystem()->FileExists( VarArgs( "materials/%s.vmt", teammaterial ) ) )
			{
				 m_pBackground[ i ] = new BitmapImage( GetVPanel(), teammaterial );
			}
			else
			{
				 m_pBackground[ i ] = new BitmapImage( GetVPanel(), pMaterialName );
			}
		}
	}

	if ( m_pTextPanel )
	{
		m_pTextPanel->SetImage( NULL );
	}

	ShutdownOverlays();
	InitOverlays( pMaterialName );
}

//-----------------------------------------------------------------------------
// Called when the mouse is hit 
//-----------------------------------------------------------------------------
void CMinimapPanel::OnMousePressed(MouseCode code)
{
	if ((code == MOUSE_LEFT) && m_pClient)
	{
		// Convert mouse position to world position
		int x, y;
		vgui::input()->GetCursorPos( x, y );

		int w, h;
		GetSize( w, h );

		Vector worldPos;
		worldPos.x = (float) x / (float) w;
		worldPos.y = 1.0f - (float) y / (float) h;
		worldPos.z = 0;	// z isn't used

		Vector worldMins, worldMaxs;
		MapData().GetMapBounds( worldMins, worldMaxs );
		worldPos *= (worldMaxs - worldMins);
		worldPos += worldMins;

		m_pClient->MinimapClicked( worldPos );
	}
}

void CMinimapPanel::SetBackgroundViewport( float minx, float miny, float maxx, float maxy, bool includedetails )
{
	int i;
	int x, y, w, h;

	x = 0;
	y = 0;
	w = GetWide();
	h = GetTall();

	if ( minx < 0.0f || maxx > 1.0f ||
		 miny < 0.0f || maxy > 1.0f )
	{
		float x0 = 0.0f;
		float y0 = 0.0f;
		float x1 = 1.0f;
		float y1 = 1.0f;

		float xrange = maxx - minx;
		float yrange = maxy - miny;

		if ( minx < 0.0f )
		{
			x0 = -minx / xrange;
			//xrange += minx;
			maxx -= minx;
			minx = 0.0f;
		}
		if ( maxx > 1.0f )
		{
			x1 = 1.0f - ( maxx - 1.0f ) / xrange;
			maxx = 1.0f;
		}
		if ( miny < 0.0f )
		{
			y0 = -miny / yrange;
			//yrange += miny;
			maxy -= miny;
			miny = 0.0f;
		}
		if ( maxy > 1.0f )
		{
			y1 =  1.0f - ( maxy - 1.0f ) / yrange;
			maxy = 1.0f;
		}

		x = x0 * w;
		y = y0 * h;
		w = x1 * w;
		h = y1 * h;
	}

	for ( i = 0; i < MAX_ACT_TEAMS; i++ )
	{
		if ( m_pBackground[ i ] )
		{
			m_pBackground[ i ]->SetPos( x, y );
			m_pBackground[ i ]->SetRenderSize( w, h );
			m_pBackground[ i ]->SetViewport( true, minx, miny, maxx, maxy );
		}
	}

	if ( includedetails )
	{
		int t;
		for ( t = 0; t < MAX_ACT_TEAMS; t++ )
		{
			for ( i = 0; i < MAX_ACTS; i++ )
			{
				Overlays *p = &m_rgOverlays[ t ][ i ];
				if ( !p->m_bInUse )
					continue;

				if ( !p->m_pOverlay )
					continue;

				p->m_pOverlay->SetPos( x, y );
				p->m_pOverlay->SetRenderSize( w, h );
				p->m_pOverlay->SetViewport( true, minx, miny, maxx, maxy );
			}
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMinimapPanel::PaintActOverlays( int teamIndex, int alpha )
{
	Assert( teamIndex >= 0 && teamIndex < MAX_ACT_TEAMS );

	bool textshowing = false;

	int i = GetCurrentActNumber();
	if ( i != ACT_NONE_SPECIFIED )
	{
		i = clamp( i, 0, MAX_ACTS - 1 );
		Overlays *p = &m_rgOverlays[ teamIndex ][ i ];
		if ( p->m_bInUse )
		{
			int r, g, b, a;

			if ( p->m_pOverlay )
			{
				p->m_pOverlay->GetColor( r, g, b, a );
				Color clr( r, g, b, alpha );
				p->m_pOverlay->SetColor( clr );
				p->m_pOverlay->DoPaint( NULL, 0, (float)alpha/255.0f );
			}

			if ( p->m_pText && m_pTextPanel )
			{
				p->m_pText->GetColor( r, g, b, a );
				Color clr( r, g, b, alpha );
				p->m_pText->SetColor( clr );

				m_pTextPanel->SetImage( p->m_pText );

				clr = m_BackgroundColor;
				clr[3] = alpha;
				m_pBackgroundPanel->SetBgColor( clr );
 
				textshowing = true;
			}				
		}
	}
	
	if ( !textshowing )
	{
		m_pTextPanel->SetVisible( false );
		m_pTextPanel->SetImage( NULL );
		m_pBackgroundPanel->SetVisible( false );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMinimapPanel::OnThink()
{
	BaseClass::OnThink();

	C_BaseTFPlayer *local = C_BaseTFPlayer::GetLocalPlayer();
	if ( local )
	{
		if ( local->m_TFLocal.m_bForceMapOverview && m_bMinimapZoomed != local->m_TFLocal.m_bForceMapOverview )
		{
			SetMinimapZoom( local->m_TFLocal.m_bForceMapOverview );
		}
	}

	if ( !IsVisible() )
		return;

	if ( m_flZoomAmount != m_flPrevZoomAmount )
	{
		m_flPrevZoomAmount = m_flZoomAmount;
		ComputeMapOrigin( m_vecCurrentOrigin );	
		InvokeOnTickOnChildren( this );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMinimapPanel::Paint()
{
	if ( gHUD.IsHidden( HIDEHUD_MISCSTATUS ) )
	{
		// Remove the minimap zoom if the hud's hidden
		SetMinimapZoom( false );
		return;
	}

	C_BasePlayer *local = C_BasePlayer::GetLocalPlayer();
	int team = 0;
	if ( local )
	{
		team = local->GetTeamNumber();
	}

	int w, h;
	GetSize( w, h );

	int alpha;
	bool shouldDrawDetails = ShouldDrawZoomDetails( alpha );

	if ( m_pTextPanel && m_pBackgroundPanel )
	{
		m_pTextPanel->SetVisible( shouldDrawDetails );
		m_pBackgroundPanel->SetVisible( shouldDrawDetails );
	}

	// DEBUGGING:  Allow cvar override
	if ( current_team.GetInt() >= 0 )
	{
		team = current_team.GetInt();
	}

	// Can can be 0 through MAX_ACT_TEAMS
	team = clamp( team, 0, MAX_ACT_TEAMS );

	// Array index is 0 to MAX_ACT_TEAMS - 1 where a team of zero means no team and won't be indexed
	//  due to logic that checks team > 0 
	int teamIndex = clamp( team - 1, 0, MAX_ACT_TEAMS - 1 );

	if ( m_pBackground[ teamIndex ] )
	{
		if ( shouldDrawDetails )
		{
			Color clr = m_BackgroundColor;
			clr[3] *= alpha / 255.0f;
			surface()->DrawSetColor( clr );
			surface()->DrawFilledRect( 0, 0, w, h );
		}

		float offsetx, offsety;

		// Need to translate m_vecCurrentOrigin into minimap space
		InternalWorldToMinimap( 
			MINIMAP_NOCLIP, 
			m_vecCurrentOrigin,
			-m_vecMapCenter,
			1,
			offsetx, offsety );

		// Scale to 0.0f to 1.0f
		offsetx /= (float)w;
		offsety /= (float)h;

		float minx, maxx, miny, maxy;

		float xscale = 1.0f;
		float yscale = 1.0f;
		float startx = 0.0f;
		float starty = 0.0f;

		Assert( m_flAspectAdjustment > 0.0f );

		// Note, the scale sense is inverted here
		float invaspect = 1.0f / m_flAspectAdjustment;

		if ( m_flAspectAdjustment < 1.0f )
		{
			xscale = invaspect;
			startx = ( 1.0f - xscale ) * 0.5f;
		}
		else
		{
			yscale = m_flAspectAdjustment;
			starty = ( 1.0f - yscale ) * 0.5f;
		}

		float halfzoom = ( 1.0f / GetAdjustedZoom() ) * 0.5f;

		// zoom scale is already normalized, so just take half in one direction, half the other
		minx = startx + xscale * ( offsetx - halfzoom );
		miny = starty + yscale * ( offsety - halfzoom );
		maxx = startx + xscale * ( offsetx + halfzoom );
		maxy = starty + yscale * ( offsety + halfzoom );

		SetBackgroundViewport( minx, miny, maxx, maxy, shouldDrawDetails );

		m_pBackground[ teamIndex ]->DoPaint( NULL );

		if ( shouldDrawDetails && ( team > 0 ) )
		{
			PaintActOverlays( teamIndex, alpha );
		}
	}
	else
	{
		Color clr = m_BackgroundColor;
		clr[3] *= alpha * 0.9f / 255.0f;

		surface()->DrawSetColor( clr );
		surface()->DrawFilledRect( 0, 0, w, h );
	}

	// Dumb place to do this
	if ( !shouldDrawDetails && CurrentActIsAWaitingAct() )
	{
		char *cmsg = "Wait for game start...";
		int width, height;
		messagechars->GetStringLength( g_hFontTrebuchet24, &width, &height, cmsg );
		messagechars->DrawString( g_hFontTrebuchet24, XRES(16), ScreenHeight() - (height * 6), 255, 255, 245, 255, cmsg, IMessageChars::MESSAGESTRINGID_NONE );
	}

	Color border = m_BorderColor;
	border[3] = border[3] * ( alpha * 0.9f ) / 255.0f;

	//border = vgui::Color( 255, 0, 120, 255 );

	surface()->DrawSetColor( border );
	surface()->DrawOutlinedRect( 0, 0, w, h );
	surface()->DrawOutlinedRect( 1, 1, w-1, h-1 );
}

void CMinimapPanel::InvokeOnTickOnChildren( vgui::Panel *parent )
{
	if ( !parent )
		return;

	int c = parent->GetChildCount();
	int i;
	for ( i = 0; i < c; i++ )
	{
		vgui::Panel *child = parent->GetChild( i );
		child->OnTick();
		InvokeOnTickOnChildren( child );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CMinimapPanel::OnTick()
{
	// See if the act's changed. If it has, bring up the act overlays.
	if ( m_nCurrentAct != GetCurrentActNumber() )
	{
	//	g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "MinimapActChanged" );
	//	SetMinimapZoom( true );
		m_nCurrentAct = GetCurrentActNumber();
	}

	// Cache these only once per frame if in a valid game
	if ( C_BasePlayer::GetLocalPlayer() && minimap_visible.GetBool() )
	{
		SetVisible( true );
		ComputeMapOrigin( m_vecCurrentOrigin );	
	}
	else
	{
		SetVisible( false );
	}

	InvokeOnTickOnChildren( this );
}

⌨️ 快捷键说明

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