//Called when a cell's propertyChanged event is called. TExt or BGColor public void OnPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "Text") { BasicCell temporary = sender as BasicCell; string cellname = temporary.GetCellName(); //Delete the dependencies because the text has changed, need to edit DeleteDependency(cellname); //if the cell text is a formula, create an new expression tree and set the new dependencies! if (temporary.Text != "" && temporary.Text[0] == '=' && temporary.Text.Length > 1) { string textlength = temporary.Text.Substring(2); Int32.TryParse(textlength, out int textint); if (textint > 50 || textint == 0) { temporary.setValue("!(bad Reference)"); } ExpressionTree tree = new ExpressionTree(temporary.Text.Substring(1)); SetDependency(cellname, tree.GetVariables()); } EvaluateCell(sender as Cell); } else if (e.PropertyName == "BGColor") { CellPropertyChanged(sender, new PropertyChangedEventArgs("BGColor")); } }
void CheckQueue(Queue <GameObject> openQueue, HashSet <GameObject> closedList, int orderInlayer) { GameObject _baseObj = openQueue.Dequeue(); int _baseDump = _baseObj.GetComponent <BasicCell> ().DumpNumber; if (_baseDump <= 0) { return; } GameObject _seaObj = GameObject.Instantiate(this.SeaObj, _baseObj.transform, false) as GameObject; _seaObj.GetComponent <BasicCell> ().SetOrderInLayer(orderInlayer); this.SeaList.Add(_seaObj); if (!closedList.Contains(_baseObj)) { closedList.Add(_baseObj); } BasicCell _parentScript = _baseObj.GetComponent <BasicCell> (); if (!_parentScript.HasNeighboursCross()) { return; } foreach (var _neighbourObj in _parentScript.NeighboursCross) { int _dumpStep = Random.Range(this.SeaDumpMin, this.SeaDumpMax); if (closedList.Contains(_neighbourObj)) { continue; } int _nextDumper = _baseDump - _dumpStep; if (_nextDumper <= 0) { _neighbourObj.GetComponent <BasicCell> ().DumpNumber = 0; if (!closedList.Contains(_neighbourObj)) { closedList.Add(_neighbourObj); } continue; } if (openQueue.Contains(_neighbourObj)) { continue; } _neighbourObj.GetComponent <BasicCell> ().DumpNumber = _nextDumper; openQueue.Enqueue(_neighbourObj); } }
protected virtual void CreateBasicCell() { List <Energy> energy = new List <Energy>(); if (WithdrawEnergy(Settings.Instance.basicCellEnergyCost, out energy)) { basicCell = GameManager.Instance.CreateBasicCell(); basicCell.rectTransform.anchoredPosition = rectTransform.anchoredPosition; basicCell.AddEnergy(energy); LinkCells(this, basicCell); } }
public void SetBasicCells() { for (int _row = 0; _row < this.CellRowNumber; _row++) { for (int _col = 0; _col < this.CellColNumber; _col++) { GameObject _basicInstance = GameObject.Instantiate(this.BasicObj, this.transform) as GameObject; this.CellList [_row, _col] = _basicInstance; BasicCell _basicCell = _basicInstance.GetComponent <BasicCell> (); _basicCell.Init(_row, _col, this.GridSideLength); } } }
/// <summary> /// Sets the spreadsheet 50*26 cells /// </summary> /// <param name="numRows"></param> /// <param name="numColumns"></param> public Spreadsheet(int numRows, int numColumns) { cells = new Cell[numRows, numColumns]; dependencyDict = new Dictionary <string, HashSet <string> >(); for (int i = 0; i < numRows; i++) { for (int c = 0; c < numColumns; c++) { char letter = (char)(c + 64); cells[i, c] = new BasicCell(i + 1, letter); cells[i, c].PropertyChanged += OnPropertyChanged; } } rowCount = numRows; columnCount = numColumns; }
//Called when a cell's propertyChanged event is called. public void OnPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "Text") { BasicCell temporary = sender as BasicCell; string cellname = temporary.ColumnIndex.ToString() + temporary.RowIndex.ToString(); //Delete the dependencies because the text has changed, need to edit DeleteDependency(cellname); //if the cell text is a formula, create an new expression tree and set the new dependencies! if (temporary.Text != "" && temporary.Text[0] == '=' && temporary.Text.Length > 1) { ExpTree tree = new ExpTree(temporary.Text.Substring(1)); SetDependency(cellname, tree.GetVariables()); } EvaluateCell(sender as Cell); } }
/// <summary> /// Here is where each cell that's changed essentially has a delegate that notifies its respective event has changed. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void OnPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "Text") { BasicCell temporary = sender as BasicCell; string cellname = temporary.GetCellName(); DeleteDependency(cellname); if (temporary.Text != "" && temporary.Text[0] == '=' && temporary.Text.Length > 1) { ExpressionTree tree = new ExpressionTree(temporary.Text.Substring(1)); SetDependency(cellname, tree.GetVariables()); } EvaluateCell(sender as Cell); } else if (e.PropertyName == "BGColor") { CellPropertyChanged(sender, new PropertyChangedEventArgs("BGColor")); } }
public virtual void OnEndDrag(PointerEventData eventData) { if (!placed) { if (eventData.pointerId != pointerId) { return; } pointerId = NON_EXISTING_TOUCH; Place(); } if (basicCell) { basicCell.OnEndDrag(eventData); basicCell = null; } }
//Constructor public Spreadsheet(int numRows, int numColumns) { cells = new Cell[numRows, numColumns]; for (int i = 0; i < numRows; i++) { for (int c = 0; c < numColumns; c++) { //convert to a Char char letter = (char)(c + 64); //create cell cells[i, c] = new BasicCell(i + 1, letter); //subscribe to the cell's propertyChanged event cells[i, c].PropertyChanged += OnPropertyChanged; } } rowCount = numRows; columnCount = numColumns; }
//Constructor public Spreadsheet(int numRows, int numColumns) { //create cell array cells = new Cell[numRows, numColumns]; dependencyDict = new Dictionary <string, HashSet <string> >(); for (int i = 0; i < numRows; i++) { for (int c = 0; c < numColumns; c++) { //convert to a Char char letter = (char)(c + 64); //create cell cells[i, c] = new BasicCell(i + 1, letter); //subscribe to the cell's propertyChanged event cells[i, c].PropertyChanged += OnPropertyChanged; } } rowCount = numRows; columnCount = numColumns; }
//helper function to evaluate the text value of a cell to determine the value value private void EvaluateCell(Cell cell) { //make BasicCell to evaluate the cell BasicCell evalCell = cell as BasicCell; //variable for errors, if true, we have an error and should just return null bool error = false; //First check to see if it's empty if (string.IsNullOrWhiteSpace(evalCell.Text)) { //if text is empty, the value should be empty evalCell.setValue(""); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } //next check to see if there is an '=' to make it a formula (and that it's more than just the =... else if (evalCell.Text.Length > 1 && evalCell.Text[0] == '=') { //first get rid of the = at (0) string text = evalCell.Text.Substring(1); //create an expression tree! ExpTree evalTree = new ExpTree(text); // get the variables from the tree string[] variables = evalTree.GetVariables(); //go through each variable. They are the locations of each cell needed for the formula. foreach (string variableName in variables) { //First check to make sure that there is even a value to reference (call our new GetCell) if (GetCell(variableName) == null) { //there was nothing to reference. Tell the user through the cell and cell prop changed evalCell.setValue("ERROR: BAD VAR REFERENCE."); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); //break out of the loop & set error to true error = true; break; } // We have determine that the cell reference is valid. Set the variable to the expTree variable //get the cell we need to edit Cell variableCell = GetCell(variableName); double variableValue; //We will need to chck to make sure it work //if the cell's value is empty, set the variable to 0. if (string.IsNullOrEmpty(variableCell.Value)) { evalTree.SetVar(variableName, 0); } //if the value of the cell is not a number, set to 0 else if (!double.TryParse(variableCell.Value, out variableValue)) { evalTree.SetVar(variableName, 0); } //ELSE: should be valid! Set to the value! else { evalTree.SetVar(variableName, variableValue); } //Don't have to worry about circular references, but self references could be bad here string cellToEval = evalCell.ColumnIndex.ToString() + evalCell.RowIndex.ToString(); if (variableName == cellToEval) { evalCell.setValue("ERROR: VAR REF SELF."); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); error = true; break; } } //if there is an error, stop here and return if (error) { return; } //Now, all variables should be set and we can evaluate the formula using the expression tree evalCell.setValue(evalTree.Eval().ToString()); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } //last if it's neither of the above, it's not an formula, just test the value to the text of the original cell else { evalCell.setValue(evalCell.Text); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } //VERY LAST THING WE NEED IS TO UPDATE DEPENDENCIES! And evaluate all cells that were dependent on the one we just changed. string cellName = evalCell.GetCellName(); if (dependencyDict.ContainsKey(cellName)) { foreach (string dependentCell in dependencyDict[cellName]) { EvaluateCell(GetCell(dependentCell)); } } }
/// <summary> /// Function used to evaluate all the cells. /// LOGIC LIVES HERE COULDN'T ENCAPSULATE FURTHER /// </summary> /// <param name="cell"></param> private void EvaluateCell(Cell cell) { BasicCell evalCell = cell as BasicCell; bool error = false; if (string.IsNullOrWhiteSpace(evalCell.Text)) { evalCell.setValue(""); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } else if (evalCell.Text.Length > 1 && evalCell.Text[0] == '=') { string text = evalCell.Text.Substring(1); ExpressionTree evalTree = new ExpressionTree(text); string[] variables = evalTree.GetVariables(); foreach (string variableName in variables) { if (GetCell(variableName) == null) { CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); error = true; break; } Cell variableCell = GetCell(variableName); double variableValue; if (string.IsNullOrEmpty(variableCell.Value)) { evalTree.SetVariable(variableName, 0); } else if (!double.TryParse(variableCell.Value, out variableValue)) { evalTree.SetVariable(variableName, 0); } else { evalTree.SetVariable(variableName, variableValue); } string cellToEval = evalCell.ColumnIndex.ToString() + evalCell.RowIndex.ToString(); if (variableName == cellToEval) { evalCell.setValue("!(Self Reference)"); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); error = true; break; } } if (error) { return; } foreach (string variableName in variables) { if (IsCircularReference(variableName, evalCell.GetCellName())) { evalCell.setValue("!(Circular Reference!)"); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); error = true; break; } } if (error) { return; } evalCell.setValue(evalTree.Evaluate().ToString()); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } else { evalCell.setValue(evalCell.Text); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } string cellName = evalCell.GetCellName(); if (dependencyDict.ContainsKey(cellName)) { foreach (string dependentCell in dependencyDict[cellName]) { EvaluateCell(GetCell(dependentCell)); } } }
private void Cell_OnDestroyedByBomba(BasicCell sender) { this.cells.Remove(sender); }
//helper function to evaluate the text value of a cell to determine the value value private void EvaluateCell(Cell cell) { //make BasicCell to evaluate the cell BasicCell evalCell = cell as BasicCell; //First check to see if it's empty if (string.IsNullOrWhiteSpace(evalCell.Text)) { //if text is empty, the value should be empty evalCell.setValue(""); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } //next check to see if there is an '=' to make it a formula (and that it's more than just the =... else if (evalCell.Text.Length > 1 && evalCell.Text[0] == '=') { //first get rid of the = at (0) string text = evalCell.Text.Substring(1); //At this point, there should be two or threecharacters left in the string text and it should be a cell int tempCol; int tempRow; if (text.Length == 2) { tempCol = text[0] - 65; tempRow = text[1] - 49; } else //3 characters in text { tempCol = text[0] - 65; text = text.Substring(1); int.TryParse(text, out tempRow); tempRow--; } //get the cell that is referred to Cell temp = GetCell(tempRow, tempCol); //if we got null back, that cell does not exist if (cell == null) { evalCell.setValue("null"); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } //otherwise it was a valid cell, set the value to the text of the referenced cell else { evalCell.setValue(temp.Text); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } } //last if it's neither of the above, it's not an formula, just test the value to the text of the original cell else { evalCell.setValue(evalCell.Text); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } }