2023-06-12 00:30:13 +02:00
|
|
|
//========= Copyright (c) 1996-2002, Valve LLC, All rights reserved. ============
|
2016-06-04 15:24:23 +02:00
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
//
|
|
|
|
// $NoKeywords: $
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include "vgui_grid.h"
|
|
|
|
|
|
|
|
using namespace vgui;
|
|
|
|
|
|
|
|
#define AssertCheck(expr, msg) \
|
|
|
|
if(!(expr))\
|
|
|
|
{\
|
|
|
|
assert(!msg);\
|
|
|
|
return 0;\
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------ //
|
|
|
|
// CGrid::CGridEntry.
|
|
|
|
// ------------------------------------------------------------------------------ //
|
|
|
|
CGrid::CGridEntry::CGridEntry()
|
|
|
|
{
|
|
|
|
m_pPanel = NULL;
|
|
|
|
m_bUnderline = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGrid::CGridEntry::~CGridEntry()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------ //
|
|
|
|
// CGrid.
|
|
|
|
// ------------------------------------------------------------------------------ //
|
|
|
|
CGrid::CGrid()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
CGrid::~CGrid()
|
|
|
|
{
|
|
|
|
Term();
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
bool CGrid::SetDimensions( int xCols, int yRows )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
Term();
|
|
|
|
|
|
|
|
m_GridEntries = new CGridEntry[xCols * yRows];
|
2021-10-27 03:35:11 +02:00
|
|
|
m_Widths = new int[xCols * 2 + yRows * 2];
|
2016-06-04 15:24:23 +02:00
|
|
|
m_Heights = m_Widths + xCols;
|
|
|
|
m_ColOffsets = m_Heights + yRows;
|
|
|
|
m_RowOffsets = m_ColOffsets + xCols;
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
if( !m_GridEntries || !m_Widths )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
Term();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
memset( m_Widths, 0, sizeof(int) * ( xCols * 2 + yRows * 2 ) );
|
2016-06-04 15:24:23 +02:00
|
|
|
|
|
|
|
m_xCols = xCols;
|
|
|
|
m_yRows = yRows;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGrid::Term()
|
|
|
|
{
|
|
|
|
delete [] m_GridEntries;
|
|
|
|
delete [] m_Widths;
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
Panel *CGrid::GetEntry( int x, int y )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
return GridEntry( x, y )->m_pPanel;
|
2016-06-04 15:24:23 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
bool CGrid::SetEntry( int x, int y, Panel *pPanel )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
CGridEntry *pEntry = GridEntry( x, y );
|
|
|
|
if( !pEntry )
|
2016-06-04 15:24:23 +02:00
|
|
|
return false;
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
if( pEntry->m_pPanel )
|
|
|
|
pEntry->m_pPanel->setParent( NULL );
|
2016-06-04 15:24:23 +02:00
|
|
|
|
|
|
|
pEntry->m_pPanel = pPanel;
|
2021-10-27 03:35:11 +02:00
|
|
|
if( pPanel )
|
|
|
|
pPanel->setParent( this );
|
2016-06-04 15:24:23 +02:00
|
|
|
|
|
|
|
m_bDirty = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CGrid::GetXSpacing()
|
|
|
|
{
|
|
|
|
return m_xSpacing;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CGrid::GetYSpacing()
|
|
|
|
{
|
|
|
|
return m_ySpacing;
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
void CGrid::SetSpacing( int xSpacing, int ySpacing )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
if( xSpacing != m_xSpacing )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
m_xSpacing = xSpacing;
|
2021-10-27 03:35:11 +02:00
|
|
|
CalcColOffsets( 0 );
|
2016-06-04 15:24:23 +02:00
|
|
|
m_bDirty = true;
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
if( ySpacing != m_ySpacing )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
m_ySpacing = ySpacing;
|
2021-10-27 03:35:11 +02:00
|
|
|
CalcRowOffsets( 0 );
|
2016-06-04 15:24:23 +02:00
|
|
|
m_bDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CGrid::SetColumnWidth(int iColumn, int width)
|
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
AssertCheck( iColumn >= 0 && iColumn < m_xCols, "CGrid::SetColumnWidth : invalid location specified" );
|
2016-06-04 15:24:23 +02:00
|
|
|
m_Widths[iColumn] = width;
|
2021-10-27 03:35:11 +02:00
|
|
|
CalcColOffsets( iColumn + 1 );
|
2016-06-04 15:24:23 +02:00
|
|
|
m_bDirty = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
bool CGrid::SetRowHeight( int iRow, int height )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
AssertCheck( iRow >= 0 && iRow < m_yRows, "CGrid::SetColumnWidth : invalid location specified" );
|
2016-06-04 15:24:23 +02:00
|
|
|
m_Heights[iRow] = height;
|
2021-10-27 03:35:11 +02:00
|
|
|
CalcRowOffsets( iRow + 1 );
|
2016-06-04 15:24:23 +02:00
|
|
|
m_bDirty = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
int CGrid::GetColumnWidth( int iColumn )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
AssertCheck( iColumn >= 0 && iColumn < m_xCols, "CGrid::GetColumnWidth: invalid location specified" );
|
2016-06-04 15:24:23 +02:00
|
|
|
return m_Widths[iColumn];
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
int CGrid::GetRowHeight( int iRow )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
AssertCheck( iRow >= 0 && iRow < m_yRows, "CGrid::GetRowHeight: invalid location specified" );
|
2016-06-04 15:24:23 +02:00
|
|
|
return m_Heights[iRow];
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
int CGrid::CalcFitColumnWidth( int iColumn )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
AssertCheck( iColumn >= 0 && iColumn < m_xCols, "CGrid::CalcFitColumnWidth: invalid location specified" );
|
2016-06-04 15:24:23 +02:00
|
|
|
|
|
|
|
int maxSize = 0;
|
2021-10-27 03:35:11 +02:00
|
|
|
for( int i = 0; i < m_yRows; i++ )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
Panel *pPanel = GridEntry( iColumn, i )->m_pPanel;
|
|
|
|
if( !pPanel )
|
2016-06-04 15:24:23 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int w, h;
|
2021-10-27 03:35:11 +02:00
|
|
|
pPanel->getSize( w, h );
|
|
|
|
if( w > maxSize )
|
2016-06-04 15:24:23 +02:00
|
|
|
maxSize = w;
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxSize;
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
int CGrid::CalcFitRowHeight( int iRow )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
AssertCheck( iRow >= 0 && iRow < m_yRows, "CGrid::CalcFitRowHeight: invalid location specified" );
|
2016-06-04 15:24:23 +02:00
|
|
|
|
|
|
|
int maxSize = 0;
|
2021-10-27 03:35:11 +02:00
|
|
|
for( int i = 0; i < m_xCols; i++ )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
Panel *pPanel = GridEntry( i, iRow )->m_pPanel;
|
|
|
|
if( !pPanel )
|
2016-06-04 15:24:23 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int w, h;
|
2021-10-27 03:35:11 +02:00
|
|
|
pPanel->getSize( w, h );
|
|
|
|
if( h > maxSize )
|
2016-06-04 15:24:23 +02:00
|
|
|
maxSize = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGrid::AutoSetRowHeights()
|
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
for( int i = 0; i < m_yRows; i++ )
|
|
|
|
SetRowHeight( i, CalcFitRowHeight( i ) );
|
2016-06-04 15:24:23 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
bool CGrid::GetEntryBox( int col, int row, int &x, int &y, int &w, int &h )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
AssertCheck( col >= 0 && col < m_xCols && row >= 0 && row < m_yRows, "CGrid::GetEntryBox: invalid location specified" );
|
2016-06-04 15:24:23 +02:00
|
|
|
|
|
|
|
x = m_ColOffsets[col];
|
|
|
|
w = m_Widths[col];
|
|
|
|
|
|
|
|
y = m_RowOffsets[row];
|
|
|
|
h = m_Heights[row];
|
2021-10-27 03:35:11 +02:00
|
|
|
return true;
|
2016-06-04 15:24:23 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
bool CGrid::CopyColumnWidths( CGrid *pOther )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
if( !pOther || pOther->m_xCols != m_xCols )
|
2016-06-04 15:24:23 +02:00
|
|
|
return false;
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
for( int i = 0; i < m_xCols; i++ )
|
2016-06-04 15:24:23 +02:00
|
|
|
m_Widths[i] = pOther->m_Widths[i];
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
CalcColOffsets( 0 );
|
2016-06-04 15:24:23 +02:00
|
|
|
m_bDirty = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGrid::RepositionContents()
|
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
for( int x = 0; x < m_xCols; x++ )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
for( int y=0; y < m_yRows; y++ )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
Panel *pPanel = GridEntry( x, y )->m_pPanel;
|
|
|
|
if( !pPanel )
|
2016-06-04 15:24:23 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
pPanel->setBounds(
|
|
|
|
m_ColOffsets[x],
|
|
|
|
m_RowOffsets[y],
|
|
|
|
m_Widths[x],
|
|
|
|
m_Heights[y]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CGrid::CalcDrawHeight()
|
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
if( m_yRows > 0 )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
return m_RowOffsets[m_yRows - 1] + m_Heights[m_yRows - 1] + m_ySpacing;
|
2016-06-04 15:24:23 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGrid::paint()
|
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
if( m_bDirty )
|
2016-06-04 15:24:23 +02:00
|
|
|
RepositionContents();
|
|
|
|
|
|
|
|
Panel::paint();
|
|
|
|
|
|
|
|
// walk the grid looking for underlined rows
|
|
|
|
int x = 0, y = 0;
|
2021-10-27 03:35:11 +02:00
|
|
|
for( int row = 0; row < m_yRows; row++ )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
CGridEntry *cell = GridEntry( 0, row );
|
2016-06-04 15:24:23 +02:00
|
|
|
|
|
|
|
y += cell->m_pPanel->getTall() + m_ySpacing;
|
2021-10-27 03:35:11 +02:00
|
|
|
if( cell->m_bUnderline )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
drawSetColor( cell->m_UnderlineColor[0], cell->m_UnderlineColor[1], cell->m_UnderlineColor[2], cell->m_UnderlineColor[3] );
|
|
|
|
drawFilledRect( 0, y - (cell->m_iUnderlineOffset + 1 ), getWide(), y - cell->m_iUnderlineOffset );
|
2016-06-04 15:24:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGrid::paintBackground()
|
|
|
|
{
|
|
|
|
Panel::paintBackground();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: sets underline color for a particular row
|
|
|
|
//-----------------------------------------------------------------------------
|
2021-10-27 03:35:11 +02:00
|
|
|
void CGrid::SetRowUnderline( int row, bool enabled, int offset, int r, int g, int b, int a )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
CGridEntry *cell = GridEntry( 0, row );
|
2016-06-04 15:24:23 +02:00
|
|
|
cell->m_bUnderline = enabled;
|
2021-10-27 03:35:11 +02:00
|
|
|
if( enabled )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
cell->m_iUnderlineOffset = offset;
|
|
|
|
cell->m_UnderlineColor[0] = r;
|
|
|
|
cell->m_UnderlineColor[1] = g;
|
|
|
|
cell->m_UnderlineColor[2] = b;
|
|
|
|
cell->m_UnderlineColor[3] = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGrid::Clear()
|
|
|
|
{
|
|
|
|
m_xCols = m_yRows = 0;
|
|
|
|
m_Widths = NULL;
|
|
|
|
m_GridEntries = NULL;
|
|
|
|
m_xSpacing = m_ySpacing = 0;
|
|
|
|
m_bDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGrid::CGridEntry* CGrid::GridEntry(int x, int y)
|
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
AssertCheck( x >= 0 && x < m_xCols && y >= 0 && y < m_yRows, "CGrid::GridEntry: invalid location specified" );
|
|
|
|
return &m_GridEntries[y * m_xCols + x];
|
2016-06-04 15:24:23 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
void CGrid::CalcColOffsets( int iStart )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
int cur = m_xSpacing;
|
2021-10-27 03:35:11 +02:00
|
|
|
if( iStart != 0 )
|
|
|
|
cur += m_ColOffsets[iStart - 1] + m_Widths[iStart - 1];
|
2016-06-04 15:24:23 +02:00
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
for( int i = iStart; i < m_xCols; i++ )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
m_ColOffsets[i] = cur;
|
|
|
|
cur += m_Widths[i] + m_xSpacing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
void CGrid::CalcRowOffsets( int iStart )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
int cur = m_ySpacing;
|
2021-10-27 03:35:11 +02:00
|
|
|
if( iStart != 0 )
|
|
|
|
cur += m_RowOffsets[iStart - 1];
|
2016-06-04 15:24:23 +02:00
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
for( int i = iStart; i < m_yRows; i++ )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
m_RowOffsets[i] = cur;
|
|
|
|
cur += m_Heights[i] + m_ySpacing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
bool CGrid::getCellAtPoint( int worldX, int worldY, int &row, int &col )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
row = -1; col = -1;
|
2021-10-27 03:35:11 +02:00
|
|
|
|
|
|
|
for( int x = 0; x < m_xCols; x++ )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
for( int y = 0; y < m_yRows; y++ )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
2021-10-27 03:35:11 +02:00
|
|
|
Panel *pPanel = GridEntry( x, y )->m_pPanel;
|
|
|
|
if( !pPanel )
|
2016-06-04 15:24:23 +02:00
|
|
|
continue;
|
|
|
|
|
2021-10-27 03:35:11 +02:00
|
|
|
if( pPanel->isWithin( worldX, worldY ) )
|
2016-06-04 15:24:23 +02:00
|
|
|
{
|
|
|
|
col = x;
|
|
|
|
row = y;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|