///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Removes a single widget that was added to the grid /// </summary> /// /// <param name="widget">The widget to remove</param> /// /// Usage example: /// <code> /// Picture pic = grid.Add(new Picture("1.png"), "picName"); /// Picture pic2 = grid.Add(new Picture("2.png"), "picName2") /// grid.remove(pic); /// grid.remove(grid.get("picName2")); /// </code> /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public override void Remove(Widget widget) { // Find the widget in the grid for (int row = 0; row < m_GridWidgets.Count; ++row) { for (int col = 0; col < m_GridWidgets[row].Count; ++col) { if (m_GridWidgets[row][col] == widget) { // Remove the widget from the grid m_GridWidgets [row].RemoveAt (col); m_ObjBorders [row].RemoveAt (col); m_ObjLayout [row].RemoveAt (col); // Check if this is the last column if (m_ColumnWidth.Count == m_GridWidgets[row].Count + 1) { // Check if there is another row with this many columns bool rowFound = false; for (int i = 0; i < m_GridWidgets.Count; ++i) { if (m_GridWidgets[i].Count >= m_ColumnWidth.Count) { rowFound = true; break; } } // Erase the last column if no other row is using it if (rowFound == false) m_ColumnWidth.RemoveAt(m_ColumnWidth.Count - 1); } // If the row is empty then remove it as well if (m_GridWidgets[row].Count == 0) { m_GridWidgets.RemoveAt (row); m_ObjBorders.RemoveAt (row); m_ObjLayout.RemoveAt (row); m_RowHeight.RemoveAt (row); } // Update the positions of all remaining widgets UpdatePositionsOfAllWidgets(); } } } }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Changes the layout of a given widget /// </summary> /// /// <param name="widget">The widget for which the layout should be changed</param> /// <param name="layout">The new layout</param> /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void changeWidgetLayout(Widget widget, Layouts layout) { // Find the widget in the grid for (int row = 0; row < m_GridWidgets.Count; ++row) { for (int col = 0; col < m_GridWidgets[row].Count; ++col) { if (m_GridWidgets[row][col] == widget) { // Change the layout of the widget m_ObjLayout[row][col] = layout; // Recalculate the position of the widget { // Calculate the available space which is distributed when widgets are positionned. Vector2f availableSpace = new Vector2f(0, 0); Vector2f minSize = GetMinSize(); if (m_Size.X > minSize.X) availableSpace.X = m_Size.X - minSize.X; if (m_Size.Y > minSize.Y) availableSpace.Y = m_Size.Y - minSize.Y; Vector2f availSpaceOffset = new Vector2f(0.5f * availableSpace.X / m_ColumnWidth.Count, 0.5f * availableSpace.Y / m_RowHeight.Count); float left = 0; float top = 0; for (int i = 0; i < row; ++i) top += m_RowHeight[i] + 2 * availSpaceOffset.Y; for (int i = 0; i < col; ++i) left += m_ColumnWidth[i] + 2 * availSpaceOffset.X; switch (m_ObjLayout[row][col]) { case Layouts.UpperLeft: left += m_ObjBorders [row] [col].Left + availSpaceOffset.X; top += m_ObjBorders [row] [col].Top + availSpaceOffset.Y; break; case Layouts.Up: left += m_ObjBorders[row][col].Left + (((m_ColumnWidth[col] - m_ObjBorders[row][col].Left - m_ObjBorders[row][col].Right) - m_GridWidgets[row][col].FullSize.X) / 2.0f) + availSpaceOffset.X; top += m_ObjBorders[row][col].Top + availSpaceOffset.Y; break; case Layouts.UpperRight: left += m_ColumnWidth[col] - m_ObjBorders[row][col].Right - m_GridWidgets[row][col].FullSize.X + availSpaceOffset.X; top += m_ObjBorders[row][col].Top + availSpaceOffset.Y; break; case Layouts.Right: left += m_ColumnWidth[col] - m_ObjBorders[row][col].Right - m_GridWidgets[row][col].FullSize.X + availSpaceOffset.X; top += m_ObjBorders[row][col].Top + (((m_RowHeight[row] - m_ObjBorders[row][col].Top - m_ObjBorders[row][col].Bottom) - m_GridWidgets[row][col].FullSize.Y) / 2.0f) + availSpaceOffset.Y; break; case Layouts.BottomRight: left += m_ColumnWidth[col] - m_ObjBorders[row][col].Right - m_GridWidgets[row][col].FullSize.X + availSpaceOffset.X; top += m_RowHeight[row] - m_ObjBorders[row][col].Bottom - m_GridWidgets[row][col].FullSize.Y + availSpaceOffset.Y; break; case Layouts.Bottom: left += m_ObjBorders[row][col].Left + (((m_ColumnWidth[col] - m_ObjBorders[row][col].Left - m_ObjBorders[row][col].Right) - m_GridWidgets[row][col].FullSize.X) / 2.0f) + availSpaceOffset.X; top += m_RowHeight[row] - m_ObjBorders[row][col].Bottom - m_GridWidgets[row][col].FullSize.Y + availSpaceOffset.Y; break; case Layouts.BottomLeft: left += m_ObjBorders[row][col].Left + availSpaceOffset.X; top += m_RowHeight[row] - m_ObjBorders[row][col].Bottom - m_GridWidgets[row][col].FullSize.Y + availSpaceOffset.Y; break; case Layouts.Left: left += m_ObjBorders[row][col].Left + availSpaceOffset.X; top += m_ObjBorders[row][col].Top + (((m_RowHeight[row] - m_ObjBorders[row][col].Top - m_ObjBorders[row][col].Bottom) - m_GridWidgets[row][col].FullSize.Y) / 2.0f) + availSpaceOffset.Y; break; case Layouts.Center: left += m_ObjBorders[row][col].Left + (((m_ColumnWidth[col] - m_ObjBorders[row][col].Left - m_ObjBorders[row][col].Right) - m_GridWidgets[row][col].FullSize.X) / 2.0f) + availSpaceOffset.X; top += m_ObjBorders[row][col].Top + (((m_RowHeight[row] - m_ObjBorders[row][col].Top - m_ObjBorders[row][col].Bottom) - m_GridWidgets[row][col].FullSize.Y) / 2.0f) + availSpaceOffset.Y; break; } m_GridWidgets[row][col].Position = new Vector2f(left, top); } } } } }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Add a widget to the grid /// </summary> /// /// <param name="widget">Fully created widget that will be added to the grid</param> /// <param name="row">The row in which the widget should be placed</param> /// <param name="col">The column in which the widget should be placed</param> /// <param name="borders">Distance from the grid square to the widget (left, top, right, bottom)</param> /// <param name="layout">Where the widget is located in the square</param> /// /// Usage example: /// <code> /// Picture pic = grid.Add(new Picture("1.png")); /// pic.Size = new Vector2f(400, 300); /// grid.AddWidget(pic, 0, 0); /// </code> /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void AddWidget(Widget widget, uint row, uint col, Borders borders, Layouts layout) { // Create the row if it didn't exist yet while (m_GridWidgets.Count < row + 1) m_GridWidgets.Add (new List<Widget>()); while (m_ObjBorders.Count < row + 1) m_ObjBorders.Add (new List<Borders>()); while (m_ObjLayout.Count < row + 1) m_ObjLayout.Add (new List<Layouts>()); while (m_GridWidgets[(int)row].Count < col + 1) m_GridWidgets [(int)row].Add (null); while (m_ObjBorders[(int)row].Count < col + 1) m_ObjBorders [(int)row].Add (new Borders(0, 0, 0, 0)); while (m_ObjLayout[(int)row].Count < col + 1) m_ObjLayout [(int)row].Add (Layouts.Center); // If this is a new row then reserve some space for it while (m_RowHeight.Count < row + 1) m_RowHeight.Add(0); // If this is the first row to have so many columns then reserve some space for it if (m_ColumnWidth.Count < col + 1) m_ColumnWidth.Add(0); // Add the widget to the grid m_GridWidgets[(int)row][(int)col] = widget; m_ObjBorders[(int)row][(int)col] = borders; m_ObjLayout[(int)row][(int)col] = layout; // Update the widgets UpdateWidgets(); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Changes borders of a given widget /// </summary> /// /// <param name="widget">The widget to which borders should be added</param> /// <param name="borders">The new borders around the widget</param> /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void ChangeWidgetBorders(Widget widget, Borders borders) { // Find the widget in the grid for (int row = 0; row < m_GridWidgets.Count; ++row) { for (int col = 0; col < m_GridWidgets[row].Count; ++col) { if (m_GridWidgets[row][col] == widget) { // Change borders of the widget m_ObjBorders[row][col] = borders; // Update all widgets UpdateWidgets(); } } } }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Copy constructor /// </summary> /// /// <param name="copy">Instance to copy</param> /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public Widget(Widget copy) : base(copy) { FocusedCallback = copy.FocusedCallback; UnfocusedCallback = copy.UnfocusedCallback; MouseEnteredCallback = copy.MouseEnteredCallback; MouseLeftCallback = copy.MouseLeftCallback; m_Enabled = copy.m_Enabled; m_Visible = copy.m_Visible; m_WidgetPhase = copy.m_WidgetPhase; m_Parent = copy.m_Parent; m_Opacity = copy.m_Opacity; m_AllowFocus = copy.m_AllowFocus; m_AnimatedWidget = copy.m_AnimatedWidget; m_DraggableWidget = copy.m_DraggableWidget; m_Container = copy.m_Container; m_Callback = new CallbackArgs(); m_Callback.Id = copy.m_Callback.Id; }