Fixed the last known bug in the widget manager layout, plus cleanups

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1932 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
cosmosninja
2008-05-18 05:43:29 +00:00
parent 7679494058
commit b86445ebf0
2 changed files with 7 additions and 547 deletions

View File

@@ -122,36 +122,9 @@ bool WidgetManager::addWgt
}
//-----------------------------------------------------------------------------
bool WidgetManager::switchOrder()
void WidgetManager::switchOrder()
{
#if 0
const int LAST_ELEM = (int)m_elems.size() - 1;
if( LAST_ELEM > -1 )
{
if( m_elems[ LAST_ELEM ].type == ET_SWITCH )
{
const int LAST_WGT = (int)m_widgets.size() - 1;
if( LAST_WGT > -1 )
{
std::cerr << "WARNING: tried to switch the order twice " <<
"in a row after widget with token " <<
m_widgets[LAST_WGT].token << ".\n";
}
else
{
std::cerr << "WARNING: tried to switch the order twice " <<
"in a row before the first widget.\n";
}
return false;
}
}
#endif
m_elems.push_back( WidgetElement( ET_SWITCH, 0 ));
return true;
}
//-----------------------------------------------------------------------------
@@ -414,14 +387,6 @@ bool WidgetManager::layout(const WidgetArea POSITION)
break;
}
/*TMP: CHECKS
*4 wgts of different sizes
*switch; 4 wgts of different sizes
*4 wgts
*
*
*
*/
/* We have to give each widget it's pixel position and create their rect.
* The position given to the widgets is their bottom left corner; since
* OpenGL the bottom left corner of the screen for (0,0), it's easier
@@ -468,284 +433,6 @@ bool WidgetManager::layout(const WidgetArea POSITION)
break;
}
}
#if 0
int cursor_x = x;
int cursor_y = y;
WidgetID* curr_wgt = 0;
const int NUM_ELEMS = (int)m_elems.size();
int i;
for( i = pos; i < NUM_ELEMS; ++i )
{
switch( m_elems[ i ].type )
{
case ET_WGT:
curr_wgt = &m_widgets[ m_elems[ i ].pos ];
curr_wgt->widget->m_x = cursor_x;
//We have to give createRect() the bottom left corner
curr_wgt->widget->m_y = cursor_y - curr_wgt->widget->m_height;
if( !(curr_wgt->widget->createRect()) ) return false;
cursor_x += curr_wgt->widget->m_width;
break;
case ET_BREAK:
x += ( calcLineWidth( pos ) - calcLineWidth( i + 1 )) / 2;
std::cerr << pos << " " << calcLineWidth ( pos ) << " " << i << " " << calcLineWidth(i+1) << std::endl;
y = cursor_y - calcLineHeight( pos );
pos = i;
return true;
case ET_SWITCH:
++i;
layoutColumn( cursor_x, cursor_y, i );
break;
}
}
pos = i;
return true;
#endif
#if 0
//The container is the line/row or column that contains a widget,
//including the starting position.
//A new value is pushed with each orientation switch, and popped with
//each break that closes a switch.
std::stack<int> container_x;
std::stack<int> container_y;
std::stack<int> container_pos;
int cursor_x = m_x + ( WGTS_WIDTH - calcLineWidth( 0 )) / 2;
int cursor_y = m_y;
container_x.push( cursor_x );
container_y.push( cursor_y );
container_pos.push( 0 );
WidgetID* curr_wgt = 0;
bool horizontal = true;
bool order_was_switched = false;
const int NUM_ELEMS = (int)m_elems.size();
for( int i = 0; i < NUM_ELEMS; ++i )
{
std::cerr << "I " << m_elems[i].type << " " << i << std::endl;
std::cerr << "x " << cursor_x << std::endl;
std::cerr << "y " << cursor_y << std::endl;
switch( m_elems[i].type )
{
case ET_WGT:
curr_wgt = &m_widgets[ m_elems[ i ].pos ];
curr_wgt->widget->m_x = cursor_x;
//We have to give createRect() the bottom left corner, not the
//top left corner
curr_wgt->widget->m_y = cursor_y - curr_wgt->widget->m_height;
if( !(curr_wgt->widget->createRect()) ) return false;
if( horizontal )
{
cursor_x += curr_wgt->widget->m_width;
}
else if( i + 1 < NUM_ELEMS )
{
switch( m_elems[ i + 1 ].type )
{
case ET_WGT:
cursor_x += ( curr_wgt->widget->m_width -
m_widgets[ m_elems[ i + 1 ].pos ].widget->
m_width ) / 2;
break;
case ET_SWITCH:
cursor_x += ( curr_wgt->widget->m_width -
calcLineWidth( i + 2 ) ) / 2;
break;
case ET_BREAK:
break;
}
cursor_y -= curr_wgt->widget->m_height;
}
break;
case ET_BREAK:
assert( curr_wgt != 0 );
if( horizontal )
{
cursor_y -= calcLineHeight( container_pos.top() + 1 );
}
if( order_was_switched )
{
container_x.pop();
container_y.pop();
container_pos.pop();
}
if( horizontal )
{
// cursor_x.top() -= ( calcLineWidth( cursor_pos.top() ) +
// calcLineWidth( i + 1 )) / 2;
// cursor_x.top() += ( calcLineWidth( cursor_pos.top() ) -
// calcLineWidth( i + 1 )) / 2;
cursor_x = container_x.top() +
( calcLineWidth( container_pos.top() ) -
calcLineWidth( i + 1 )) / 2;
}
else
{
cursor_x = container_x.top() +
calcLineWidth( container_pos.top() ) -
calcColumnWidth( i + 1 );
cursor_y = container_y.top();
}
if( order_was_switched )
{
horizontal = !horizontal;
if( container_x.size() < 2 ) order_was_switched = false;
}
break;
case ET_SWITCH:
horizontal = !horizontal;
container_x.push( cursor_x );
container_y.push( cursor_y );
container_pos.push( i );
if( horizontal )
{
//TODO: Check when the first line is smaller than the rest
// cursor_x = calcColumnWidth( i ) - calcLineWidth( i + 1 );
}
else if( i + 1 < NUM_ELEMS )
{
switch( m_elems[ i + 1 ].type )
{
case ET_WGT:
/*cursor_x += ( calcLineWidth( i ) -
m_widgets[ m_elems[ i + 1 ].pos ].widget->
m_width ) / 2;*/
break;
case ET_SWITCH:
cursor_x += ( calcLineWidth( i ) -
calcLineWidth( i + 2 ) ) / 2;
break;
case ET_BREAK:
break;
}
/* {
cursor_x.top() += calcLineWidth( i ) - calcColumnWidth( i + 1 );*/
}
order_was_switched = true;
break;
}
#endif
#if 0
//Position the first widget, these coordinates are for the top left
//corner of the first widget.
int widget_x = m_x + ( WGTS_WIDTH - calcLineWidth( 0 )) / 2;
int widget_y = m_y;
const int NUM_ELEMS = (int)m_elems.size();
int line_pos = 0;
int column_pos = -1;
int curr_wgt = 0;
for( int i = 0; i < NUM_ELEMS; ++i )
{
switch( m_elems[i].type )
{
case ET_WGT:
curr_wgt = m_elems[i].pos;
if( column_pos != -1 )
{
//If we are inside a column, we need to recalculate the X
//position so the widget is centered around the column
const int CENTERED_POS = ( calcColumnWidth( column_pos ) -
m_widgets[curr_wgt].widget->m_width) / 2;
widget_x += CENTERED_POS;
}
//Move the position to the widget's bottom left corner, since
//that's what the createRect() function expects.
widget_y -= m_widgets[curr_wgt].widget->m_height;
//Assign the widget's position
m_widgets[curr_wgt].widget->m_x = widget_x;
m_widgets[curr_wgt].widget->m_y = widget_y;
//Create widget's rect
if( !(m_widgets[curr_wgt].widget->createRect()) )
{
return false;
}
if( column_pos == -1 )
{
//If we are not inside a column, move the position to the
//top left corner of the next widget.
widget_x += m_widgets[curr_wgt].widget->m_width;
widget_y += m_widgets[curr_wgt].widget->m_height;
}
else
{
//If we are inside a column, we need to move back to the
//columns' X position
const int CENTERED_POS = ( calcColumnWidth( column_pos ) -
m_widgets[curr_wgt].widget->m_width) / 2;
widget_x -= CENTERED_POS;
}
break;
case ET_BREAK:
if( column_pos == -1 )
{
//If we are not inside a column, move to the next line
const int CENTERED_POS = (WGTS_WIDTH -
calcLineWidth( i+1 )) / 2;
widget_x = m_x + CENTERED_POS;
widget_y -= calcLineHeight(line_pos);
line_pos = i + 1;
}
else
{
//If we are inside a column, move to the next widget in the
//same line
widget_x += calcColumnWidth(column_pos);
widget_y += calcColumnHeight(column_pos);
column_pos = -1;
}
break;
case ET_COLUMN_START:
column_pos = i;
break;
}
}
#endif
//Select the first active widget by default
setSelectedWgtToken( WGT_NONE );
@@ -855,7 +542,6 @@ bool WidgetManager::layoutColumn( int& x, int& y, int& pos )
case ET_BREAK:
x += calcColumnWidth( pos );
// y = ??? Unnecesary?;
pos = i;
return true;
@@ -868,206 +554,8 @@ bool WidgetManager::layoutColumn( int& x, int& y, int& pos )
pos = i;
return true;
#if 0
for( int i = 0; i < NUM_ELEMS; ++i )
{
switch( m_elems[i].type )
{
case ET_WGT:
curr_wgt = &m_widgets[ m_elems[ i ].pos ];
curr_wgt->widget->m_x = cursor_x;
//We have to give createRect() the bottom left corner, not the
//top left corner
curr_wgt->widget->m_y = cursor_y - curr_wgt->widget->m_height;
if( !(curr_wgt->widget->createRect()) ) return false;
if( horizontal )
{
cursor_x += curr_wgt->widget->m_width;
}
else if( i + 1 < NUM_ELEMS )
{
switch( m_elems[ i + 1 ].type )
{
case ET_WGT:
cursor_x += ( curr_wgt->widget->m_width -
m_widgets[ m_elems[ i + 1 ].pos ].widget->
m_width ) / 2;
break;
case ET_SWITCH:
cursor_x += ( curr_wgt->widget->m_width -
calcLineWidth( i + 2 ) ) / 2;
break;
case ET_BREAK:
break;
}
cursor_y -= curr_wgt->widget->m_height;
}
break;
case ET_BREAK:
assert( curr_wgt != 0 );
if( horizontal )
{
cursor_y -= calcLineHeight( container_pos.top() + 1 );
}
if( order_was_switched )
{
container_x.pop();
container_y.pop();
container_pos.pop();
}
if( horizontal )
{
// cursor_x.top() -= ( calcLineWidth( cursor_pos.top() ) +
// calcLineWidth( i + 1 )) / 2;
// cursor_x.top() += ( calcLineWidth( cursor_pos.top() ) -
// calcLineWidth( i + 1 )) / 2;
cursor_x = container_x.top() +
( calcLineWidth( container_pos.top() ) -
calcLineWidth( i + 1 )) / 2;
}
else
{
cursor_x = container_x.top() +
calcLineWidth( container_pos.top() ) -
calcColumnWidth( i + 1 );
cursor_y = container_y.top();
}
if( order_was_switched )
{
horizontal = !horizontal;
if( container_x.size() < 2 ) order_was_switched = false;
}
break;
case ET_SWITCH:
horizontal = !horizontal;
container_x.push( cursor_x );
container_y.push( cursor_y );
container_pos.push( i );
if( horizontal )
{
//TODO: Check when the first line is smaller than the rest
// cursor_x = calcColumnWidth( i ) - calcLineWidth( i + 1 );
}
else if( i + 1 < NUM_ELEMS )
{
switch( m_elems[ i + 1 ].type )
{
case ET_WGT:
/*cursor_x += ( calcLineWidth( i ) -
m_widgets[ m_elems[ i + 1 ].pos ].widget->
m_width ) / 2;*/
break;
case ET_SWITCH:
cursor_x += ( calcLineWidth( i ) -
calcLineWidth( i + 2 ) ) / 2;
break;
case ET_BREAK:
break;
}
/* {
cursor_x.top() += calcLineWidth( i ) - calcColumnWidth( i + 1 );*/
}
order_was_switched = true;
break;
}
#endif
}
#if 0
//-----------------------------------------------------------------------------
int WidgetManager::calcLineWidth( const int START_ELEM ) const
{
int curr_wgt;
int total_width = 0;
const int NUM_ELEMS = (int)m_elems.size();
for( int i = START_ELEM; i < NUM_ELEMS; ++i )
{
switch( m_elems[i].type )
{
case ET_WGT:
curr_wgt = m_elems[i].pos;
total_width += m_widgets[curr_wgt].widget->m_width;
break;
case ET_BREAK:
return total_width;
case ET_COLUMN_START:
total_width += calcColumnWidth(i);
while( i < NUM_ELEMS )
{
if( m_elems[i].type == ET_BREAK ) break;
++i;
}
break;
}
}
return total_width;
}
//-----------------------------------------------------------------------------
int WidgetManager::calcLineHeight( const int START_ELEM ) const
{
int curr_wgt;
int line_height = 0;
int column_height;
const int NUM_ELEMS = (int)m_elems.size();
for( int i = START_ELEM; i < NUM_ELEMS; ++i )
{
switch( m_elems[i].type )
{
case ET_WGT:
curr_wgt = m_elems[i].pos;
if( line_height < m_widgets[curr_wgt].widget->m_height )
{
line_height = m_widgets[curr_wgt].widget->m_height;
}
break;
case ET_BREAK:
return line_height;
case ET_COLUMN_START:
column_height = calcColumnHeight(i);
if( line_height < column_height )
{
line_height = column_height;
}
while( i < NUM_ELEMS )
{
if( m_elems[i].type == ET_BREAK ) break;
++i;
}
}
}
return line_height;
}
#endif
/** This getLineWidth() function returns the width of the smallest
* rectangle that contains the widgets in the given line. It expects
@@ -1361,28 +849,6 @@ int WidgetManager::calcLineX( const int POS )
}
}
/*
if( i + 1 < NUM_ELEMS )
{
switch( m_elems[ i + 1 ].type )
{
case ET_WGT:
cursor_x += ( curr_wgt->widget->m_width -
m_widgets[ m_elems[ i + 1 ].pos ].widget->
m_width ) / 2;
break;
case ET_SWITCH:
cursor_x += ( curr_wgt->widget->m_width -
calcLineWidth( i + 2 ) ) / 2;
break;
case ET_BREAK:
break;
}*/
return width;
}
@@ -1393,17 +859,9 @@ int WidgetManager::calcColumnX( const int POS )
WidgetID* curr_wgt = &m_widgets[ m_elems[ POS ].pos ];
switch( m_elems[ POS ].type )
if( m_elems[ POS ].type == ET_WGT )
{
case ET_WGT:
return ( calcColumnWidth( POS ) - curr_wgt->widget->m_width ) / 2;
case ET_SWITCH:
return ( curr_wgt->widget->m_width -
calcLineWidth( POS + 1 )) / 2;
case ET_BREAK:
break;
return ( calcColumnWidth( POS ) - curr_wgt->widget->m_width ) / 2;
}
return 0;

View File

@@ -179,8 +179,10 @@ public:
);
bool breakLine();
bool switchOrder(); //This changes the orientation from horizontal to
//vertical. It's reverted at line breaks;
void switchOrder(); //This changes the orientation from horizontal to
//vertical. It's reverted at line breaks. There are
//no situations where you cannot use a switch order
//so it should always succeed.
void reset();