//-----------------------------------------------------------------------------
// Purpose: Callback for when the panel size has been changed
//-----------------------------------------------------------------------------
void EditablePanel::OnSizeChanged(int wide, int tall)
{
	BaseClass::OnSizeChanged(wide, tall);
	InvalidateLayout();

	int dx = wide - _baseWide;
	int dy = tall - _baseTall;

	for (int i = 0; i < GetChildCount(); i++)
	{
		// perform auto-layout on the child panel
		Panel *child = GetChild(i);

		int x, y, w, t;
		child->GetBounds(x, y, w, t);

		if (child->GetPinCorner() == PIN_TOPRIGHT || child->GetPinCorner() == PIN_BOTTOMRIGHT)
		{
			// move along with the right edge
			x += dx;
		}

		if (child->GetPinCorner() == PIN_BOTTOMLEFT || child->GetPinCorner() == PIN_BOTTOMRIGHT)
		{
			// move along with the lower edge
			y += dy;
		}

		// check for resize
		if (child->GetAutoResize() == AUTORESIZE_RIGHT || child->GetAutoResize() == AUTORESIZE_DOWNANDRIGHT)
		{
			w += dx;
		}

		if (child->GetAutoResize() == AUTORESIZE_DOWN || child->GetAutoResize() == AUTORESIZE_DOWNANDRIGHT)
		{
			t += dy;
		}

		// make sure the child isn't too big...
		if(x+w>wide)
		{
			continue;
		}
		
		if(y+t>tall)
		{
			continue;
		}

		child->SetBounds(x, y, w, t);
		child->InvalidateLayout();
	}
	Repaint();

	// update the baselines
	_baseWide = wide;
	_baseTall = tall;
}
//=============================================================================
void GenericPanelList::ScrollToPanelItem( unsigned short index )
{
	if( index < m_PanelItems.Count() )
	{
		Panel* targetPanelItem = m_PanelItems[index];

		int targetX, targetY, targetWide, targetTall;
		targetPanelItem->GetBounds( targetX, targetY, targetWide, targetTall );

		int x, y, wide, tall;
		m_PnlItemRegion->GetBounds( x, y, wide, tall );

		int firstVisi = GetFirstVisibleItemNumber();
		int lastVisi  = GetLastVisibleItemNumber();

		if( index <= firstVisi || index >= lastVisi  ) // outside or on the border (allow re-alignment if it's at the border)
		{
			int travelDistance = 0;

			if( index <= firstVisi ) // the target item is above the clipping region
			{
				travelDistance = targetY - m_PanelItemBorder; //align to top
			}
			else // below the clipping region
			{
				travelDistance = targetY - ( lastVisi - firstVisi ) * ( targetTall + m_PanelItemBorder ) - m_PanelItemBorder; //align to be last item

				int topX, topY;
				m_PanelItems[0]->GetPos( topX, topY );
				if( topY - travelDistance > 0 ) //don't pull the topmost item away from the top
					travelDistance = topY - m_PanelItemBorder;
			}

			m_ScrVerticalScroll->SetValue( m_ScrVerticalScroll->GetValue() + travelDistance );

			for( int i = 0; i < m_PanelItems.Count(); ++i )
			{
				int itemX, itemY;
				m_PanelItems[i]->GetPos( itemX, itemY );
				m_PanelItems[i]->SetPos( itemX, itemY - travelDistance );
			}

			UpdateArrows( );
			UpdatePanels( );
		}
	}
}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int AnimationController::GetRelativeOffset( AnimAlign_t& align, bool xcoord )
{
	if ( !align.relativePosition )
		return 0;

	Panel *panel = GetParent()->FindChildByName(g_ScriptSymbols.String(align.alignPanel), true);
	if ( !panel )
		return 0;

	int x, y, w, h;
	panel->GetBounds( x, y, w, h );

	int offset =0;
	switch ( align.alignment )
	{
	default:
	case a_northwest:
		offset = xcoord ? x : y;
		break;
	case a_north:
		offset = xcoord ? ( x + w ) / 2 : y;
		break;
	case a_northeast:
		offset = xcoord ? ( x + w ) : y;
		break;
	case a_west:
		offset = xcoord ? x : ( y + h ) / 2;
		break;
	case a_center:
		offset = xcoord ? ( x + w ) / 2 : ( y + h ) / 2;
		break;
	case a_east:
		offset = xcoord ? ( x + w ) : ( y + h ) / 2;
		break;
	case a_southwest:
		offset = xcoord ? x : ( y + h );
		break;
	case a_south:
		offset = xcoord ? ( x + w ) / 2 : ( y + h );
		break;
	case a_southeast:
		offset = xcoord ? ( x + w ) : ( y +  h );
		break;
	}

	return offset;
}
//-----------------------------------------------------------------------------
// Purpose: Callback for when the panel size has been changed
//-----------------------------------------------------------------------------
void EditablePanel::OnSizeChanged(int wide, int tall)
{
	BaseClass::OnSizeChanged(wide, tall);
	InvalidateLayout();

	for (int i = 0; i < GetChildCount(); i++)
	{
		// perform auto-layout on the child panel
		Panel *child = GetChild(i);
		if ( !child )
			continue;

		int x, y, w, h;
		child->GetBounds( x, y, w, h );

		int px, py;
		child->GetPinOffset( px, py );

		int ox, oy;
		child->GetResizeOffset( ox, oy );

		int ex;
		int ey;

		AutoResize_e resize = child->GetAutoResize(); 
		bool bResizeHoriz = ( resize == AUTORESIZE_RIGHT || resize == AUTORESIZE_DOWNANDRIGHT );
		bool bResizeVert = ( resize == AUTORESIZE_DOWN || resize == AUTORESIZE_DOWNANDRIGHT );

		PinCorner_e pinCorner = child->GetPinCorner();
		if ( pinCorner == PIN_TOPRIGHT || pinCorner == PIN_BOTTOMRIGHT )
		{
			// move along with the right edge
			ex = wide + px;
			x = bResizeHoriz ? ox : ex - w;
		}
		else
		{
			x = px;
			ex = bResizeHoriz ? wide + ox : px + w;
		}

		if ( pinCorner == PIN_BOTTOMLEFT || pinCorner == PIN_BOTTOMRIGHT )
		{
			// move along with the right edge
			ey = tall + py;
			y = bResizeVert ? oy : ey - h;
		}
		else
		{
			y = py;
			ey = bResizeVert ? tall + oy : py + h;
		}

		// Clamp..
		if ( ex < x )
		{
			ex = x;
		}
		if ( ey < y )
		{
			ey = y;
		}

		child->SetBounds( x, y, ex - x, ey - y );
		child->InvalidateLayout();
	}
	Repaint();
}