// CPolygonButton.cpp // Copyright (c) 2006 Turtle Rock Studios, Inc. #include "cbase.h" #include "polygonbutton.h" #include "keyvalues.h" // memdbgon must be the last include file in a .cpp file!!! #include "tier0/memdbgon.h" //-------------------------------------------------------------------------------------------------------- CPolygonButton::CPolygonButton( vgui::Panel *parent, const char *panelName ) : vgui::Button( parent, panelName, L"" ) { m_unscaledHotspotPoints.RemoveAll(); m_unscaledVisibleHotspotPoints.RemoveAll(); m_hotspotPoints = NULL; m_visibleHotspotPoints = NULL; m_numHotspotPoints = 0; m_numVisibleHotspotPoints = 0; m_nWhiteMaterial = vgui::surface()->CreateNewTextureID(); vgui::surface()->DrawSetTextureFile( m_nWhiteMaterial, "vgui/white" , true, false ); } //-------------------------------------------------------------------------------------------------------- void CPolygonButton::ApplySettings( KeyValues *data ) { BaseClass::ApplySettings( data ); // Re-read hotspot data from disk UpdateHotspots( data ); } //-------------------------------------------------------------------------------------------------------- void CPolygonButton::UpdateHotspots( KeyValues *data ) { // clear out our old hotspot if ( m_hotspotPoints ) { delete[] m_hotspotPoints; m_hotspotPoints = NULL; m_numHotspotPoints = 0; } if ( m_visibleHotspotPoints ) { delete[] m_visibleHotspotPoints; m_visibleHotspotPoints = NULL; m_numVisibleHotspotPoints = 0; } m_unscaledHotspotPoints.RemoveAll(); m_unscaledVisibleHotspotPoints.RemoveAll(); // read in a new one KeyValues *points = data->FindKey( "Hotspot", false ); if ( points ) { for ( KeyValues *value = points->GetFirstValue(); value; value = value->GetNextValue() ) { const char *str = value->GetString(); float x, y; if ( 2 == sscanf( str, "%f %f", &x, &y ) ) { m_unscaledHotspotPoints.AddToTail( Vector2D( x, y ) ); } } } points = data->FindKey( "VisibleHotspot", false ); if ( !points ) { points = data->FindKey( "Hotspot", false ); } if ( points ) { for ( KeyValues *value = points->GetFirstValue(); value; value = value->GetNextValue() ) { const char *str = value->GetString(); float x, y; if ( 2 == sscanf( str, "%f %f", &x, &y ) ) { m_unscaledVisibleHotspotPoints.AddToTail( Vector2D( x, y ) ); } } } } //-------------------------------------------------------------------------------------------------------- /** * Clip out cursor positions inside our overall rectangle that are outside our hotspot. */ vgui::VPANEL CPolygonButton::IsWithinTraverse( int x, int y, bool traversePopups ) { if ( m_numHotspotPoints < 3 ) { return NULL; } vgui::VPANEL within = BaseClass::IsWithinTraverse( x, y, traversePopups ); if ( within == GetVPanel() ) { int wide, tall; GetSize( wide, tall ); ScreenToLocal( x, y ); bool inside = true; for ( int i=0; iDrawSetColor( c ); vgui::surface()->DrawSetTexture( m_nWhiteMaterial ); vgui::surface()->DrawTexturedPolygon( m_numVisibleHotspotPoints, m_visibleHotspotPoints ); } //-------------------------------------------------------------------------------------------------------- /** * Paints the polygonal border */ void CPolygonButton::PaintBorder( void ) { Color c = GetButtonFgColor(); vgui::surface()->DrawSetColor( c ); vgui::surface()->DrawSetTexture( m_nWhiteMaterial ); vgui::surface()->DrawTexturedPolyLine( m_visibleHotspotPoints, m_numVisibleHotspotPoints ); } //-------------------------------------------------------------------------------------------------------- void CPolygonButton::ApplySchemeSettings( vgui::IScheme *scheme ) { BaseClass::ApplySchemeSettings( scheme ); InvalidateLayout(); // so we can reposition the text } //--------------------------------------------------------------------------------------------------------