void Start() { enemies = new List <Enemy>(); // not sure spawners = new List <Spawner>(FindObjectsOfType <Spawner>()); process = new ProcessState(); spawn = new SpawnState(spawners); arrange = new ArrangeState(FindObjectOfType <GridManager>()); fire = new FireState(); if (null == processButton) { Debug.Log("no process button"); } else { process.AddReadyButton(processButton); } process.nextState = spawn; spawn.nextState = arrange; arrange.nextState = fire; fire.nextState = process; currentState = process; }
///////////////////////////////////////////////////////////////////////////////////////////////////// // PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Positions the specified elements and determines a size for a <see cref="FrameworkElement"/>-derived class. /// </summary> /// <param name="elements">The elements to be arranged.</param> /// <param name="finalSize"> /// The final area within the parent that this element should use to arrange itself and the specified elements. /// </param> /// <returns>The actual size used.</returns> public override Size ArrangeElements(IList <UIElement> elements, Size finalSize) { // Cache and reset layout pending flag bool isLayoutUpdatePending = this.IsLayoutUpdatePending; this.IsLayoutUpdatePending = false; // Iterate over the elements and arrange foreach (UIElement element in elements) { if (element != null) { Size desiredSize = element.DesiredSize; // Calculate a random x/y position that keeps the element in the view double x = Math.Max(random.NextDouble() * (finalSize.Width - desiredSize.Width), 0); double y = Math.Max(random.NextDouble() * (finalSize.Height - desiredSize.Height), 0); // Update the arrange state with the new arrange rect, but if there are leaving elements then don't move // the element ArrangeState state = new ArrangeState(element, false, isLayoutUpdatePending); if (!this.HasLeavingChildren) { state.ArrangeRect = new Rect(x, y, desiredSize.Width, desiredSize.Height); } else { state.ArrangeRect = state.PreviousArrangeRect; } PanelBase.SetArrangeState(element, state); } } return(finalSize); }
/// <summary> /// Method which arranges the give child /// based on given arrange state. /// /// Determines the start position of the child /// based on its display index, frozen count of /// datagrid, current horizontal offset etc. /// </summary> private void ArrangeChild( UIElement child, int displayIndex, ArrangeState arrangeState) { Debug.Assert(child != null, "child cannot be null."); double childWidth = 0.0; IProvideDataGridColumn cell = child as IProvideDataGridColumn; // Determine if this child was clipped in last arrange for the sake of frozen columns if (child == _clippedChildForFrozenBehaviour) { arrangeState.OldClippedChild = child; _clippedChildForFrozenBehaviour = null; } // Width determinition of the child to be arranged. It is // display value if available else the ActualWidth if (cell != null) { Debug.Assert(cell.Column != null, "column cannot be null."); childWidth = cell.Column.Width.DisplayValue; if (DoubleUtil.IsNaN(childWidth)) { childWidth = cell.Column.ActualWidth; } } else { childWidth = child.DesiredSize.Width; } Rect rcChild = new Rect(new Size(childWidth, arrangeState.ChildHeight)); // Determinition of start point for children to arrange. Lets say the there are 5 columns of which 2 are frozen. // If the datagrid is scrolled horizontally. Following is the snapshot of arrange /* * * *| <Cell3> | <Unarranged space> | <RowHeader> | <Cell1> | <Cell2> | <Right Clip of Cell4> | <Cell5> |* * | <Visible region> |* */ if (displayIndex < arrangeState.FrozenColumnCount) { // For all the frozen children start from the horizontal offset // and arrange increamentally rcChild.X = arrangeState.NextFrozenCellStart; arrangeState.NextFrozenCellStart += childWidth; arrangeState.DataGridHorizontalScrollStartX += childWidth; } else { // For arranging non frozen children arrange which ever can be arranged // from the start to horizontal offset. This would fill out the space left by // frozen children. The next one child will be arranged and clipped accordingly past frozen // children. The remaining children will arranged in the remaining space. if (DoubleUtil.LessThanOrClose(arrangeState.NextNonFrozenCellStart, arrangeState.ViewportStartX)) { if (DoubleUtil.LessThanOrClose(arrangeState.NextNonFrozenCellStart + childWidth, arrangeState.ViewportStartX)) { rcChild.X = arrangeState.NextNonFrozenCellStart; arrangeState.NextNonFrozenCellStart += childWidth; } else { double cellChoppedWidth = arrangeState.ViewportStartX - arrangeState.NextNonFrozenCellStart; if (DoubleUtil.AreClose(cellChoppedWidth, 0.0)) { rcChild.X = arrangeState.NextFrozenCellStart; arrangeState.NextNonFrozenCellStart = arrangeState.NextFrozenCellStart + childWidth; } else { rcChild.X = arrangeState.NextFrozenCellStart - cellChoppedWidth; double clipWidth = childWidth - cellChoppedWidth; arrangeState.NewClippedChild = child; _childClipForFrozenBehavior.Rect = new Rect(cellChoppedWidth, 0, clipWidth, rcChild.Height); arrangeState.NextNonFrozenCellStart = arrangeState.NextFrozenCellStart + clipWidth; } } } else { rcChild.X = arrangeState.NextNonFrozenCellStart; arrangeState.NextNonFrozenCellStart += childWidth; } } child.Arrange(rcChild); }
/// <summary> /// Arrange /// /// Iterates over the columns in the display index order and looks if /// it the corresponding child is realized. If yes then arranges it. /// </summary> protected override Size ArrangeOverride(Size arrangeSize) { IList children = RealizedChildren; ArrangeState arrangeState = new ArrangeState(); arrangeState.ChildHeight = arrangeSize.Height; DataGrid parentDataGrid = ParentDataGrid; /* * determine the horizontal offset, cells panel offset and other coordinates used for arrange of children */ if (parentDataGrid != null) { parentDataGrid.QueueInvalidateCellsPanelHorizontalOffset(); SetDataGridCellPanelWidth(children, arrangeSize.Width); InitializeArrangeState(arrangeState); } List<RealizedColumnsBlock> displayIndexBlockList = RealizedColumnsDisplayIndexBlockList; if (displayIndexBlockList != null && displayIndexBlockList.Count > 0) { double averageColumnWidth = parentDataGrid.InternalColumns.AverageColumnWidth; List<RealizedColumnsBlock> blockList = RealizedColumnsBlockList; Debug_VerifyRealizedIndexCountVsDisplayIndexCount(blockList, displayIndexBlockList); // Get realized children not in realized list, so that they dont participate in arrange List<int> additionalChildIndices = GetRealizedChildrenNotInBlockList(blockList, children); int displayIndexBlockIndex = -1; RealizedColumnsBlock displayIndexBlock = displayIndexBlockList[++displayIndexBlockIndex]; bool pastLastBlock = false; for (int i = 0, count = parentDataGrid.Columns.Count; i < count; i++) { bool realizedChild = InBlockOrNextBlock(displayIndexBlockList, i, ref displayIndexBlockIndex, ref displayIndexBlock, out pastLastBlock); if (pastLastBlock) { break; } // Arrange the child if it is realized if (realizedChild) { int columnIndex = parentDataGrid.ColumnIndexFromDisplayIndex(i); RealizedColumnsBlock block = GetRealizedBlockForColumn(blockList, columnIndex); int childIndex = block.StartIndexOffset + columnIndex - block.StartIndex; if (additionalChildIndices != null) { for (int j = 0, additionalChildrenCount = additionalChildIndices.Count; j < additionalChildrenCount && additionalChildIndices[j] <= childIndex; j++) { childIndex++; } } ArrangeChild(children[childIndex] as UIElement, i, arrangeState); } else { DataGridColumn column = parentDataGrid.ColumnFromDisplayIndex(i); if (!column.IsVisible) { continue; } double childSize = GetColumnEstimatedMeasureWidth(column, averageColumnWidth); Debug.Assert(i >= arrangeState.FrozenColumnCount, "Frozen cells should have been realized or not visible"); arrangeState.NextNonFrozenCellStart += childSize; } } if (additionalChildIndices != null) { for (int i = 0, count = additionalChildIndices.Count; i < count; i++) { UIElement child = children[additionalChildIndices[i]] as UIElement; child.Arrange(new Rect()); } } } FinishArrange(arrangeState); return arrangeSize; }
/// <summary> /// Helper method which which ends the arrange by setting values /// from arrange state to appropriate fields. /// </summary> /// <param name="arrangeState"></param> private void FinishArrange(ArrangeState arrangeState) { DataGrid parentDataGrid = ParentDataGrid; // Update the NonFrozenColumnsViewportHorizontalOffset property of datagrid if (parentDataGrid != null) { parentDataGrid.NonFrozenColumnsViewportHorizontalOffset = arrangeState.DataGridHorizontalScrollStartX; } // Remove the clip on previous clipped child if (arrangeState.OldClippedChild != null) { arrangeState.OldClippedChild.CoerceValue(ClipProperty); } // Add the clip on new child to be clipped for the sake of frozen columns. _clippedChildForFrozenBehaviour = arrangeState.NewClippedChild; if (_clippedChildForFrozenBehaviour != null) { _clippedChildForFrozenBehaviour.CoerceValue(ClipProperty); } }
/// <summary> /// Helper method to initialize the arrange state /// </summary> /// <param name="arrangeState"></param> private void InitializeArrangeState(ArrangeState arrangeState) { DataGrid parentDataGrid = ParentDataGrid; double horizontalOffset = parentDataGrid.HorizontalScrollOffset; double cellsPanelOffset = parentDataGrid.CellsPanelHorizontalOffset; arrangeState.NextFrozenCellStart = horizontalOffset; arrangeState.NextNonFrozenCellStart -= cellsPanelOffset; arrangeState.ViewportStartX = horizontalOffset - cellsPanelOffset; arrangeState.FrozenColumnCount = parentDataGrid.FrozenColumnCount; }