private PointAxial?handleEdges(PointAxial ip, Direction dir, bool?isPositive) { if (_file.Grid.Size == 1) { return(new PointAxial(0, 0)); } if (ip.InGrid(_file.Grid.Size)) { return(ip); } var x = ip.Q; var z = ip.R; var y = -x - z; var xBigger = Math.Abs(x) >= _file.Grid.Size; var yBigger = Math.Abs(y) >= _file.Grid.Size; var zBigger = Math.Abs(z) >= _file.Grid.Size; // Move the pointer back to the hex near the edge ip -= dir.Vector; // If two values are still in range, we are wrapping around an edge (not a corner). if (!xBigger && !yBigger) { return(new PointAxial(ip.Q + ip.R, -ip.R)); } else if (!yBigger && !zBigger) { return(new PointAxial(-ip.Q, ip.Q + ip.R)); } else if (!zBigger && !xBigger) { return(new PointAxial(-ip.R, -ip.Q)); } // If two values are out of range, we navigated into a corner. if (isPositive == null) { return(null); } // We teleport to a location that depends on the current memory value. if ((!xBigger && !isPositive.Value) || (!yBigger && isPositive.Value)) { return(new PointAxial(ip.Q + ip.R, -ip.R)); } else if ((!yBigger && !isPositive.Value) || (!zBigger && isPositive.Value)) { return(new PointAxial(-ip.Q, ip.Q + ip.R)); } else if ((!zBigger && !isPositive.Value) || (!xBigger && isPositive.Value)) { return(new PointAxial(-ip.R, -ip.Q)); } // This should never be reached throw new InvalidOperationException(); }
public PointAxial?FromScreen(int x, int y) { var r = (y - YPadding) / (double)YTextSpacing - (Grid.Size - 1); var q = ((x - XPadding) / (double)XTextSpacing - r) / 2 - (Grid.Size - 1); var result = new PointAxial((int)Math.Round(q), (int)Math.Round(r)); return(result.InGrid(Grid.Size) ? result : (PointAxial?)null); }
public char this[PointAxial coords] { get { var tup = axialToIndex(coords); return(tup == null ? '.' : _grid[tup.Item1][tup.Item2]); } set { var tup = axialToIndex(coords); if (tup != null) { _grid[tup.Item1][tup.Item2] = value; } } }
public Grid resize(int size) { var result = new Grid(size, new char[] {}); int copy_size = Math.Min(Size, size); for (int R = -copy_size + 1; R < copy_size; ++R) { for (int Q = -copy_size - Math.Min(R, 0) + 1; Q < copy_size - Math.Max(R, 0); ++Q) { var p = new PointAxial(Q, R); result[p] = this[p]; } } return(result); }
private void mouseDown(object sender, MouseEventArgs e) { if (_lastRendering != null) { _lastMouseDown = _file.FromScreen(e.X, e.Y); if (e.Button == MouseButtons.Right) { mnuContext.Show(ctImage, e.Location); } else { DlgMessage.Show("The position you clicked is: " + _lastMouseDown.ToString(), "Axial coordinates", DlgType.Info); } } }
private Tuple <int, int> axialToIndex(PointAxial coords) { var x = coords.Q; var z = coords.R; var y = -x - z; if (Ut.Max(Math.Abs(x), Math.Abs(y), Math.Abs(z)) >= Size) { return(null); } var i = z + Size - 1; var j = x + Math.Min(i, Size - 1); return(Tuple.Create(i, j)); }
public int GetY(PointAxial p) { return((p.R + Grid.Size - 1) * YTextSpacing + YPadding); }
public int GetX(PointAxial p) { return((2 * (p.Q + Grid.Size - 1) + p.R) * XTextSpacing + XPadding); }
private void lstKeyDown(object sender, KeyEventArgs e) { if (_lastRendering == null) { return; } switch (e.KeyCode) { case Keys.Left: if (leftHeld) // allow holding a key to repeat { heldUsed = true; moveSelection(Direction.West); } else { leftHeld = true; heldUsed = false; } e.Handled = true; break; case Keys.Right: if (rightHeld) // allow holding a key to repeat { heldUsed = true; moveSelection(Direction.East); } else { rightHeld = true; heldUsed = false; } e.Handled = true; break; case Keys.Up: if (leftHeld) { heldUsed = true; moveSelection(Direction.NorthWest); } else if (rightHeld) { heldUsed = true; moveSelection(Direction.NorthEast); } else { return; // default handle (change list selection) } e.Handled = true; break; case Keys.Down: if (leftHeld) { heldUsed = true; moveSelection(Direction.SouthWest); } else if (rightHeld) { heldUsed = true; moveSelection(Direction.SouthEast); } else { return; } e.Handled = true; break; case Keys.Enter: editPath(); return; case Keys.Insert: startPath(); return; case Keys.Home: setStartPos(); return; case Keys.OemOpenBrackets: if (e.Control && _file.Grid.Size > 1) { _file.Grid = _file.Grid.resize(_file.Grid.Size - 1); if (!_selection.InGrid(_file.Grid.Size)) { _selection = new PointAxial(0, 0); } rerender(); _anyChanges = true; } break; case Keys.OemCloseBrackets: if (e.Control) { _file.Grid = _file.Grid.resize(_file.Grid.Size + 1); rerender(); _anyChanges = true; } break; case Keys.Escape: toggleCursor(); break; default: return; } }
private void moveSelection(Direction dir) { _selection = handleEdges(_selection + dir.Vector, dir, true).Value; rerender(); }