private PositionSpec Render3D(Graphics g, IEnumerable <Face3D> frame, Point mousePos) { g.SmoothingMode = SmoothingMode.AntiAlias; var pos = PositionSpec.Default; foreach (var face in frame) { var brush = new SolidBrush(face.Color); var vertice = face.Vertices.Select(p => new PointF((float)p.X, (float)p.Y)).ToArray(); var facePos = new PositionSpec() { Position = face.Position, Positions = face.Positions }; g.FillPolygon(brush, vertice); g.DrawPolygon(new Pen(Color.Black, 1), vertice); var gp = new GraphicsPath(); gp.AddPolygon(vertice); if (gp.IsVisible(mousePos)) { pos = facePos; } } return(pos); }
/// <summary> /// Gets or sets the selection of the given cube and face position /// </summary> /// <param name="key"></param> /// <returns></returns> public Selection this[PositionSpec key] { get { return this._selections.ContainsKey(key) ? this._selections[key] : Selection.None; } set { if (this._selections.ContainsKey(key)) this._selections[key] = value; } }
/// <summary> /// Gets or sets the selection of the given cube and face position /// </summary> /// <param name="key"></param> /// <returns></returns> public Selection this[PositionSpec key] { get { if (_selections.ContainsKey(key)) return _selections[key]; else return Selection.None; } set { if (_selections.ContainsKey(key)) _selections[key] = value; } }
// ** RENDERING ** /// <summary> /// Updates the cubeModel (including the selection) /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { if (_buffer[_currentBufferIndex] != null) { bool threeD = this.DrawingMode == RubiksCubeLib.CubeModel.DrawingMode.ThreeDimensional; PositionSpec selectedPos = threeD ? Render(e.Graphics, _buffer[_currentBufferIndex], PointToClient(Cursor.Position)) : Render2D(e.Graphics, _buffer[_currentBufferIndex], PointToClient(Cursor.Position)); // disallow changes of current selection while color picker is visible if (!this.ContextMenuStrip.Visible && this.MouseHandling) { ResetFaceSelection(Selection.None); // set selections _selections[_oldSelection] = Selection.Second; _selections[selectedPos] |= Selection.First; _currentSelection = selectedPos; } } }
/// <summary> /// Gets or sets the selection of the given cube and face position /// </summary> /// <param name="key"></param> /// <returns></returns> public Selection this[PositionSpec key] { get { if (_selections.ContainsKey(key)) { return(_selections[key]); } else { return(Selection.None); } } set { if (_selections.ContainsKey(key)) { _selections[key] = value; } } }
/// <summary> /// Set a selection to all entries in the selection collection /// </summary> /// <param name="selection">New selection</param> private void ResetFaceSelection(Selection selection) { this.Rubik.Cubes.ForEach(c => c.Faces.Where(f => f.Color != Color.Black).ToList().ForEach(f => { PositionSpec pos = new PositionSpec() { FacePosition = f.Position, CubePosition = c.Position.Flags }; if (_selections[pos].HasFlag(Selection.Possible)) { _selections[pos] = Selection.Possible | selection; } else if (_selections[pos].HasFlag(Selection.NotPossible)) { _selections[pos] = selection | Selection.NotPossible; } else { _selections[pos] = selection; } })); }
public PositionSpec Render2D(Graphics g, IEnumerable<Face3D> frame, Point mousePos) { g.SmoothingMode = SmoothingMode.AntiAlias; PositionSpec pos = PositionSpec.Default; int square = 0, borderX = 5, borderY = 5; if (((double)(this.Screen.Width - 10) / (double)(this.Screen.Height - 10)) > (4.0 / 3.0)) { square = (int)(this.Screen.Height / 9.0); borderX = (this.Screen.Width - 12 * square) / 2; } else { square = (int)(this.Screen.Width / 12.0); borderY = (this.Screen.Height - 9 * square) / 2; } List<Face3D> faces = new List<Face3D>(); foreach (Cube c in this.Rubik.Cubes) faces.AddRange(c.Faces.Where(f => c.Position.Flags.HasFlag(CubeFlagService.FromFacePosition(f.Position))).Select(f => new Face3D(null, f.Color, f.Position, c.Position.Flags))); frame = faces; foreach (Face3D face in frame) { #region CalculatePoints int x = 0, y = 0; int xOffs = borderX, yOffs = borderY; if (face.Position.HasFlag(FacePosition.Front)) { xOffs += 3 * square; yOffs += 3 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.X) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Y) * (-1) + 1) * square; } if (face.Position.HasFlag(FacePosition.Top)) { xOffs += 3 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.X) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Z) + 1) * square; } if (face.Position.HasFlag(FacePosition.Bottom)) { xOffs += 3 * square; yOffs += 6 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.X) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Z) * (-1) + 1) * square; } if (face.Position.HasFlag(FacePosition.Left)) { yOffs += 3 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.Z) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Y) * (-1) + 1) * square; } if (face.Position.HasFlag(FacePosition.Right)) { xOffs += 6 * square; yOffs += 3 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.Z) * (-1) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Y) * (-1) + 1) * square; } if (face.Position.HasFlag(FacePosition.Back)) { xOffs += 9 * square; yOffs += 3 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.X) * (-1) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Y) * (-1) + 1) * square; } #endregion Point[] parr = new Point[] { new Point(x, y), new Point(x, y + square), new Point(x + square, y + square), new Point(x + square, y) }; Brush b = new SolidBrush(face.Color); double factor = ((Math.Sin((double)Environment.TickCount / (double)200) + 1) / 4) + 0.75; PositionSpec facePos = new PositionSpec() { FacePosition = face.Position, CubePosition = face.MasterPosition }; if (this.MouseHandling) { if (_selections[facePos].HasFlag(Selection.Second)) b = new HatchBrush(HatchStyle.Percent75, Color.Black, face.Color); else if (_selections[facePos].HasFlag(Selection.NotPossible)) b = new SolidBrush(Color.FromArgb(face.Color.A, (int)(face.Color.R * 0.3), (int)(face.Color.G * 0.3), (int)(face.Color.B * 0.3))); else if (_selections[facePos].HasFlag(Selection.First)) b = new HatchBrush(HatchStyle.Percent30, Color.Black, face.Color); else if (_selections[facePos].HasFlag(Selection.Possible)) b = new SolidBrush(Color.FromArgb(face.Color.A, (int)(Math.Min(face.Color.R * factor, 255)), (int)(Math.Min(face.Color.G * factor, 255)), (int)(Math.Min(face.Color.B * factor, 255)))); else b = new SolidBrush(face.Color); } else b = new SolidBrush(face.Color); g.FillPolygon(b, parr); g.DrawPolygon(new Pen(Color.Black, 1), parr); GraphicsPath gp = new GraphicsPath(); gp.AddPolygon(parr); if (gp.IsVisible(mousePos)) pos = facePos; } g.DrawRectangle(Pens.Black, 0, this.Height - 25, this.Width - 1, 24); //g.DrawLine(Pens.Black, 0, this.Height - 25, this.Width, this.Height - 25); g.DrawString(string.Format("[{0}] | {1}", _currentSelection.CubePosition, _currentSelection.FacePosition), this.Font, Brushes.Black, 5, this.Height - 20); g.DrawRectangle(Pens.Black, 0, this.Height - 50, this.Width - 1, 25); g.DrawString(this.State, this.Font, Brushes.Black, 5, this.Height - 45); g.DrawRectangle(Pens.Black, 0, 0, this.Width - 1, this.Height - 50); return pos; }
/// <summary> /// Renders the current frame /// </summary> /// <param name="g">Graphics</param> /// <param name="frame">Frame to render</param> /// <param name="mousePos">Current mouse position</param> /// <returns></returns> private PositionSpec Render(Graphics g, IEnumerable<Face3D> frame, Point mousePos) { g.SmoothingMode = SmoothingMode.AntiAlias; PositionSpec pos = PositionSpec.Default; var _brushIndex = 0; foreach (var face in frame) { var parr = face.Vertices.Select(p => new PointF((float)p.X, (float)p.Y)).ToArray(); //Verbetering 1 : Minder Calls om objecten van BRush te Disposen if (face.Color == Color.Orange) { _brushIndex = 0; } else if (face.Color == Color.Red) { _brushIndex = 1; } else if (face.Color == Color.Yellow) { _brushIndex = 2; } else if (face.Color == Color.White) { _brushIndex = 3; } else if (face.Color == Color.Blue) { _brushIndex = 4; } else if (face.Color == Color.Green) { _brushIndex = 5; } //b = _brushes[_brushIndex]; //b = new SolidBrush(face.Color); var factor = ((Math.Sin(Environment.TickCount / (double)200) + 1) / 4) + 0.75; var facePos = new PositionSpec { FacePosition = face.Position, CubePosition = face.MasterPosition }; Brush b; if (this.MouseHandling) { if (this._selections[facePos].HasFlag(Selection.Second)) b = this._brushes[_brushIndex + 6]; else if (this._selections[facePos].HasFlag(Selection.NotPossible)) b = this._brushes[_brushIndex + 12]; else if (this._selections[facePos].HasFlag(Selection.First)) b = this._brushes[_brushIndex + 18]; else if (this._selections[facePos].HasFlag(Selection.Possible)) b = new SolidBrush(Color.FromArgb(face.Color.A, (int)(Math.Min(face.Color.R * factor, 255)), (int)(Math.Min(face.Color.G * factor, 255)), (int)(Math.Min(face.Color.B * factor, 255)))); else b = this._brushes[_brushIndex]; } else b = this._brushes[_brushIndex]; g.FillPolygon(b, parr); using (var pen = new Pen(Color.Black, 1)) { g.DrawPolygon(pen, parr); } using (GraphicsPath gp = new GraphicsPath()) { gp.AddPolygon(parr); if (gp.IsVisible(mousePos)) pos = facePos; } } using (var solidBrush = new SolidBrush(this.BackColor)) { g.FillRectangle(solidBrush, 0, this.Height - 25, this.Width - 1, 24); } g.DrawRectangle(Pens.Black, 0, this.Height - 25, this.Width - 1, 24); g.DrawString(string.Format("[{0}] | {1}", _currentSelection.CubePosition, _currentSelection.FacePosition), this.Font, Brushes.Black, 5, this.Height - 20); using (var solidBrush = new SolidBrush(this.BackColor)) { g.FillRectangle(solidBrush, 0, this.Height - 50, this.Width - 1, 25); } g.DrawRectangle(Pens.Black, 0, this.Height - 50, this.Width - 1, 25); g.DrawString(this.State, this.Font, Brushes.Black, 5, this.Height - 45); g.DrawRectangle(Pens.Black, 0, 0, this.Width - 1, this.Height - 50); return pos; }
// *** METHODS *** /// <summary> /// Adds a new entry to the collection /// </summary> /// <param name="key">Cube position and face position</param> /// <param name="value">Selection</param> public void Add(PositionSpec key, Selection value) { _selections.Add(key, value); }
/// <summary> /// Detection and execution of mouse-controlled layer rotations /// </summary> protected override void OnMouseClick(MouseEventArgs e) { if (this.MouseHandling) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { if (_oldSelection.IsDefault) { if (_currentSelection.IsDefault) { _selections.Reset(); _oldSelection = PositionSpec.Default; _currentSelection = PositionSpec.Default; } else { if (!CubePosition.IsCorner(_currentSelection.CubePosition)) { _oldSelection = _currentSelection; this.Rubik.Cubes.ForEach(c => c.Faces.Where(f => f.Color != Color.Black).ToList().ForEach(f => { PositionSpec pos = new PositionSpec() { CubePosition = c.Position.Flags, FacePosition = f.Position }; if (_currentSelection.CubePosition != c.Position.Flags && !CubePosition.IsCenter(c.Position.Flags) && _currentSelection.FacePosition == f.Position) { CubeFlag assocLayer = CubeFlagService.FromFacePosition(_currentSelection.FacePosition); CubeFlag commonLayer = CubeFlagService.GetFirstNotInvalidCommonFlag(_currentSelection.CubePosition, c.Position.Flags, assocLayer); if (commonLayer != CubeFlag.None && c.Position.HasFlag(commonLayer)) { _selections[pos] |= Selection.Possible; } else { _selections[pos] |= Selection.NotPossible; } } else { _selections[pos] |= Selection.NotPossible; } })); this.State = string.Format("First selection [{0}] | {1}", _currentSelection.CubePosition, _currentSelection.FacePosition); } else { _selections.Reset(); this.State = "Error: Invalid first selection, must not be a corner"; } } } else { if (_currentSelection.IsDefault) { this.State = "Ready"; } else { if (_currentSelection.CubePosition != _oldSelection.CubePosition) { if (!CubePosition.IsCenter(_currentSelection.CubePosition)) { if (_oldSelection.FacePosition == _currentSelection.FacePosition) { CubeFlag assocLayer = CubeFlagService.FromFacePosition(_oldSelection.FacePosition); CubeFlag commonLayer = CubeFlagService.GetFirstNotInvalidCommonFlag(_oldSelection.CubePosition, _currentSelection.CubePosition, assocLayer); bool direction = true; if (commonLayer == CubeFlag.TopLayer || commonLayer == CubeFlag.MiddleLayer || commonLayer == CubeFlag.BottomLayer) { if (((_oldSelection.FacePosition == FacePosition.Back) && _currentSelection.CubePosition.HasFlag(CubeFlag.RightSlice)) || ((_oldSelection.FacePosition == FacePosition.Left) && _currentSelection.CubePosition.HasFlag(CubeFlag.BackSlice)) || ((_oldSelection.FacePosition == FacePosition.Front) && _currentSelection.CubePosition.HasFlag(CubeFlag.LeftSlice)) || ((_oldSelection.FacePosition == FacePosition.Right) && _currentSelection.CubePosition.HasFlag(CubeFlag.FrontSlice))) { direction = false; } if (commonLayer == CubeFlag.TopLayer || commonLayer == CubeFlag.MiddleLayer) { direction = !direction; } } if (commonLayer == CubeFlag.LeftSlice || commonLayer == CubeFlag.MiddleSliceSides || commonLayer == CubeFlag.RightSlice) { if (((_oldSelection.FacePosition == FacePosition.Bottom) && _currentSelection.CubePosition.HasFlag(CubeFlag.BackSlice)) || ((_oldSelection.FacePosition == FacePosition.Back) && _currentSelection.CubePosition.HasFlag(CubeFlag.TopLayer)) || ((_oldSelection.FacePosition == FacePosition.Top) && _currentSelection.CubePosition.HasFlag(CubeFlag.FrontSlice)) || ((_oldSelection.FacePosition == FacePosition.Front) && _currentSelection.CubePosition.HasFlag(CubeFlag.BottomLayer))) { direction = false; } if (commonLayer == CubeFlag.LeftSlice) { direction = !direction; } } if (commonLayer == CubeFlag.BackSlice || commonLayer == CubeFlag.MiddleSlice || commonLayer == CubeFlag.FrontSlice) { if (((_oldSelection.FacePosition == FacePosition.Top) && _currentSelection.CubePosition.HasFlag(CubeFlag.RightSlice)) || ((_oldSelection.FacePosition == FacePosition.Right) && _currentSelection.CubePosition.HasFlag(CubeFlag.BottomLayer)) || ((_oldSelection.FacePosition == FacePosition.Bottom) && _currentSelection.CubePosition.HasFlag(CubeFlag.LeftSlice)) || ((_oldSelection.FacePosition == FacePosition.Left) && _currentSelection.CubePosition.HasFlag(CubeFlag.TopLayer))) { direction = false; } if (commonLayer == CubeFlag.FrontSlice || commonLayer == CubeFlag.MiddleSlice) { direction = !direction; } } if (commonLayer != CubeFlag.None) { RotateLayerAnimated(commonLayer, direction); } else { this.State = "Error: Invalid second selection, does not specify distinct layer"; } } else { this.State = "Error: Invalid second selection, must match orientation of first selection"; } } else { this.State = "Error: Invalid second selection, must not be a center"; } } else { this.State = "Error: Invalid second selection, must not be first selection"; } } _selections.Reset(); _oldSelection = PositionSpec.Default; _currentSelection = PositionSpec.Default; } } } base.OnMouseClick(e); }
/// <summary> /// Renders the current frame /// </summary> /// <param name="g">Graphics</param> /// <param name="frame">Frame to render</param> /// <param name="mousePos">Current mouse position</param> /// <returns></returns> private PositionSpec Render(Graphics g, IEnumerable <Face3D> frame, Point mousePos) { g.SmoothingMode = SmoothingMode.AntiAlias; PositionSpec pos = PositionSpec.Default; foreach (Face3D face in frame) { PointF[] parr = face.Vertices.Select(p => new PointF((float)p.X, (float)p.Y)).ToArray(); Brush b = new SolidBrush(face.Color); double factor = ((Math.Sin((double)Environment.TickCount / (double)200) + 1) / 4) + 0.75; PositionSpec facePos = new PositionSpec() { FacePosition = face.Position, CubePosition = face.MasterPosition }; if (this.MouseHandling) { if (_selections[facePos].HasFlag(Selection.Second)) { b = new HatchBrush(HatchStyle.Percent75, Color.Black, face.Color); } else if (_selections[facePos].HasFlag(Selection.NotPossible)) { b = new SolidBrush(Color.FromArgb(face.Color.A, (int)(face.Color.R * 0.3), (int)(face.Color.G * 0.3), (int)(face.Color.B * 0.3))); } else if (_selections[facePos].HasFlag(Selection.First)) { b = new HatchBrush(HatchStyle.Percent30, Color.Black, face.Color); } else if (_selections[facePos].HasFlag(Selection.Possible)) { b = new SolidBrush(Color.FromArgb(face.Color.A, (int)(Math.Min(face.Color.R * factor, 255)), (int)(Math.Min(face.Color.G * factor, 255)), (int)(Math.Min(face.Color.B * factor, 255)))); } else { b = new SolidBrush(face.Color); } } else { b = new SolidBrush(face.Color); } g.FillPolygon(b, parr); g.DrawPolygon(new Pen(Color.Black, 1), parr); GraphicsPath gp = new GraphicsPath(); gp.AddPolygon(parr); if (gp.IsVisible(mousePos)) { pos = facePos; } } g.FillRectangle(new SolidBrush(this.BackColor), 0, this.Height - 25, this.Width - 1, 24); g.DrawRectangle(Pens.Black, 0, this.Height - 25, this.Width - 1, 24); g.DrawString(string.Format("[{0}] | {1}", _currentSelection.CubePosition, _currentSelection.FacePosition), this.Font, Brushes.Black, 5, this.Height - 20); g.FillRectangle(new SolidBrush(this.BackColor), 0, this.Height - 50, this.Width - 1, 25); g.DrawRectangle(Pens.Black, 0, this.Height - 50, this.Width - 1, 25); g.DrawString(this.State, this.Font, Brushes.Black, 5, this.Height - 45); g.DrawRectangle(Pens.Black, 0, 0, this.Width - 1, this.Height - 50); return(pos); }
/// <summary> /// Detection and execution of mouse-controlled layer rotations /// </summary> /// <param name="e">todo: describe e parameter on OnMouseClick</param> protected override void OnMouseClick(MouseEventArgs e) { if (this.MouseHandling && e.Button == MouseButtons.Left) { if (this._oldSelection.IsDefault) { if (this._currentSelection.IsDefault) { this._selections.Reset(); this._oldSelection = PositionSpec.Default; this._currentSelection = PositionSpec.Default; } else { if (!CubePosition.IsCorner(this._currentSelection.CubePosition)) { this._oldSelection = this._currentSelection; this.Rubik.Cubes.ForEach(c => c.Faces.Where(f => f.Color != Color.Black).ToList().ForEach(f => { var pos = new PositionSpec { CubePosition = c.Position.Flags, FacePosition = f.Position }; if (this._currentSelection.CubePosition != c.Position.Flags && !CubePosition.IsCenter(c.Position.Flags) && this._currentSelection.FacePosition == f.Position) { var assocLayer = CubeFlagService.FromFacePosition(this._currentSelection.FacePosition); var commonLayer = CubeFlagService.GetFirstNotInvalidCommonFlag(this._currentSelection.CubePosition, c.Position.Flags, assocLayer); if (commonLayer != CubeFlag.None && c.Position.HasFlag(commonLayer)) { this._selections[pos] |= Selection.Possible; } else { this._selections[pos] |= Selection.NotPossible; } } else { this._selections[pos] |= Selection.NotPossible; } })); this.State = $"First selection [{this._currentSelection.CubePosition}] | {this._currentSelection.FacePosition}"; } else { this._selections.Reset(); this.State = "Error: Invalid first selection, must not be a corner"; } } } else { if (this._currentSelection.IsDefault) { this.State = "Ready"; } else { if (this._currentSelection.CubePosition != this._oldSelection.CubePosition) { if (!CubePosition.IsCenter(this._currentSelection.CubePosition)) { if (this._oldSelection.FacePosition == this._currentSelection.FacePosition) { var assocLayer = CubeFlagService.FromFacePosition(this._oldSelection.FacePosition); var commonLayer = CubeFlagService.GetFirstNotInvalidCommonFlag(this._oldSelection.CubePosition, this._currentSelection.CubePosition, assocLayer); var direction = true; if (commonLayer == CubeFlag.TopLayer || commonLayer == CubeFlag.MiddleLayer || commonLayer == CubeFlag.BottomLayer) { if (((this._oldSelection.FacePosition == FacePosition.Back) && this._currentSelection.CubePosition.HasFlag(CubeFlag.RightSlice)) || ((this._oldSelection.FacePosition == FacePosition.Left) && this._currentSelection.CubePosition.HasFlag(CubeFlag.BackSlice)) || ((this._oldSelection.FacePosition == FacePosition.Front) && this._currentSelection.CubePosition.HasFlag(CubeFlag.LeftSlice)) || ((this._oldSelection.FacePosition == FacePosition.Right) && this._currentSelection.CubePosition.HasFlag(CubeFlag.FrontSlice))) direction = false; if (commonLayer == CubeFlag.TopLayer || commonLayer == CubeFlag.MiddleLayer) direction = !direction; } if (commonLayer == CubeFlag.LeftSlice || commonLayer == CubeFlag.MiddleSliceSides || commonLayer == CubeFlag.RightSlice) { if (((this._oldSelection.FacePosition == FacePosition.Bottom) && this._currentSelection.CubePosition.HasFlag(CubeFlag.BackSlice)) || ((this._oldSelection.FacePosition == FacePosition.Back) && this._currentSelection.CubePosition.HasFlag(CubeFlag.TopLayer)) || ((this._oldSelection.FacePosition == FacePosition.Top) && this._currentSelection.CubePosition.HasFlag(CubeFlag.FrontSlice)) || ((this._oldSelection.FacePosition == FacePosition.Front) && this._currentSelection.CubePosition.HasFlag(CubeFlag.BottomLayer))) direction = false; if (commonLayer == CubeFlag.LeftSlice) direction = !direction; } if (commonLayer == CubeFlag.BackSlice || commonLayer == CubeFlag.MiddleSlice || commonLayer == CubeFlag.FrontSlice) { if (((this._oldSelection.FacePosition == FacePosition.Top) && this._currentSelection.CubePosition.HasFlag(CubeFlag.RightSlice)) || ((this._oldSelection.FacePosition == FacePosition.Right) && this._currentSelection.CubePosition.HasFlag(CubeFlag.BottomLayer)) || ((this._oldSelection.FacePosition == FacePosition.Bottom) && this._currentSelection.CubePosition.HasFlag(CubeFlag.LeftSlice)) || ((this._oldSelection.FacePosition == FacePosition.Left) && this._currentSelection.CubePosition.HasFlag(CubeFlag.TopLayer))) direction = false; if (commonLayer == CubeFlag.FrontSlice || commonLayer == CubeFlag.MiddleSlice) direction = !direction; } if (commonLayer != CubeFlag.None) { this.RotateLayerAnimated(commonLayer, direction); } else { this.State = "Error: Invalid second selection, does not specify distinct layer"; } } else { this.State = "Error: Invalid second selection, must match orientation of first selection"; } } else { this.State = "Error: Invalid second selection, must not be a center"; } } else { this.State = "Error: Invalid second selection, must not be first selection"; } } this._selections.Reset(); this._oldSelection = PositionSpec.Default; this._currentSelection = PositionSpec.Default; } } base.OnMouseClick(e); }
/// <summary> /// Renders the current frame /// </summary> /// <param name="g">Graphics</param> /// <param name="frame">Frame to render</param> /// <param name="mousePos">Current mouse position</param> /// <returns></returns> private PositionSpec Render(Graphics g, IEnumerable<Face3D> frame, Point mousePos) { g.SmoothingMode = SmoothingMode.AntiAlias; PositionSpec pos = PositionSpec.Default; foreach (Face3D face in frame) { PointF[] parr = face.Vertices.Select(p => new PointF((float)p.X, (float)p.Y)).ToArray(); Brush b = new SolidBrush(face.Color); double factor = ((Math.Sin((double)Environment.TickCount / (double)200) + 1) / 4) + 0.75; PositionSpec facePos = new PositionSpec() { FacePosition = face.Position, CubePosition = face.MasterPosition }; if (this.MouseHandling) { if (_selections[facePos].HasFlag(Selection.Second)) b = new HatchBrush(HatchStyle.Percent75, Color.Black, face.Color); else if (_selections[facePos].HasFlag(Selection.NotPossible)) b = new SolidBrush(Color.FromArgb(face.Color.A, (int)(face.Color.R * 0.3), (int)(face.Color.G * 0.3), (int)(face.Color.B * 0.3))); else if (_selections[facePos].HasFlag(Selection.First)) b = new HatchBrush(HatchStyle.Percent30, Color.Black, face.Color); else if (_selections[facePos].HasFlag(Selection.Possible)) b = new SolidBrush(Color.FromArgb(face.Color.A, (int)(Math.Min(face.Color.R * factor, 255)), (int)(Math.Min(face.Color.G * factor, 255)), (int)(Math.Min(face.Color.B * factor, 255)))); else b = new SolidBrush(face.Color); } else b = new SolidBrush(face.Color); g.FillPolygon(b, parr); g.DrawPolygon(new Pen(Color.Black, 1), parr); GraphicsPath gp = new GraphicsPath(); gp.AddPolygon(parr); if (gp.IsVisible(mousePos)) pos = facePos; } g.FillRectangle(new SolidBrush(this.BackColor), 0, this.Height - 25, this.Width - 1, 24); g.DrawRectangle(Pens.Black, 0, this.Height - 25, this.Width - 1, 24); g.DrawString(string.Format("[{0}] | {1}", _currentSelection.CubePosition, _currentSelection.FacePosition), this.Font, Brushes.Black, 5, this.Height - 20); g.FillRectangle(new SolidBrush(this.BackColor), 0, this.Height - 50, this.Width - 1, 25); g.DrawRectangle(Pens.Black, 0, this.Height - 50, this.Width - 1, 25); g.DrawString(this.State, this.Font, Brushes.Black, 5, this.Height - 45); g.DrawRectangle(Pens.Black, 0, 0, this.Width - 1, this.Height - 50); return pos; }
public PositionSpec Render2D(Graphics g, IEnumerable <Face3D> frame, Point mousePos) { g.SmoothingMode = SmoothingMode.AntiAlias; PositionSpec pos = PositionSpec.Default; int square = 0, borderX = 5, borderY = 5; if (((double)(this.Screen.Width - 10) / (double)(this.Screen.Height - 10)) > (4.0 / 3.0)) { square = (int)(this.Screen.Height / 9.0); borderX = (this.Screen.Width - 12 * square) / 2; } else { square = (int)(this.Screen.Width / 12.0); borderY = (this.Screen.Height - 9 * square) / 2; } List <Face3D> faces = new List <Face3D>(); foreach (Cube c in this.Rubik.Cubes) { faces.AddRange(c.Faces.Where(f => c.Position.Flags.HasFlag(CubeFlagService.FromFacePosition(f.Position))).Select(f => new Face3D(null, f.Color, f.Position, c.Position.Flags))); } frame = faces; foreach (Face3D face in frame) { #region CalculatePoints int x = 0, y = 0; int xOffs = borderX, yOffs = borderY; if (face.Position.HasFlag(FacePosition.Front)) { xOffs += 3 * square; yOffs += 3 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.X) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Y) * (-1) + 1) * square; } if (face.Position.HasFlag(FacePosition.Top)) { xOffs += 3 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.X) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Z) + 1) * square; } if (face.Position.HasFlag(FacePosition.Bottom)) { xOffs += 3 * square; yOffs += 6 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.X) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Z) * (-1) + 1) * square; } if (face.Position.HasFlag(FacePosition.Left)) { yOffs += 3 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.Z) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Y) * (-1) + 1) * square; } if (face.Position.HasFlag(FacePosition.Right)) { xOffs += 6 * square; yOffs += 3 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.Z) * (-1) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Y) * (-1) + 1) * square; } if (face.Position.HasFlag(FacePosition.Back)) { xOffs += 9 * square; yOffs += 3 * square; CubePosition cubePos = new CubePosition(face.MasterPosition); x = xOffs + (CubeFlagService.ToInt(cubePos.X) * (-1) + 1) * square; y = yOffs + (CubeFlagService.ToInt(cubePos.Y) * (-1) + 1) * square; } #endregion Point[] parr = new Point[] { new Point(x, y), new Point(x, y + square), new Point(x + square, y + square), new Point(x + square, y) }; Brush b = new SolidBrush(face.Color); double factor = ((Math.Sin((double)Environment.TickCount / (double)200) + 1) / 4) + 0.75; PositionSpec facePos = new PositionSpec() { FacePosition = face.Position, CubePosition = face.MasterPosition }; if (this.MouseHandling) { if (_selections[facePos].HasFlag(Selection.Second)) { b = new HatchBrush(HatchStyle.Percent75, Color.Black, face.Color); } else if (_selections[facePos].HasFlag(Selection.NotPossible)) { b = new SolidBrush(Color.FromArgb(face.Color.A, (int)(face.Color.R * 0.3), (int)(face.Color.G * 0.3), (int)(face.Color.B * 0.3))); } else if (_selections[facePos].HasFlag(Selection.First)) { b = new HatchBrush(HatchStyle.Percent30, Color.Black, face.Color); } else if (_selections[facePos].HasFlag(Selection.Possible)) { b = new SolidBrush(Color.FromArgb(face.Color.A, (int)(Math.Min(face.Color.R * factor, 255)), (int)(Math.Min(face.Color.G * factor, 255)), (int)(Math.Min(face.Color.B * factor, 255)))); } else { b = new SolidBrush(face.Color); } } else { b = new SolidBrush(face.Color); } g.FillPolygon(b, parr); g.DrawPolygon(new Pen(Color.Black, 1), parr); GraphicsPath gp = new GraphicsPath(); gp.AddPolygon(parr); if (gp.IsVisible(mousePos)) { pos = facePos; } } g.DrawRectangle(Pens.Black, 0, this.Height - 25, this.Width - 1, 24); //g.DrawLine(Pens.Black, 0, this.Height - 25, this.Width, this.Height - 25); g.DrawString(string.Format("[{0}] | {1}", _currentSelection.CubePosition, _currentSelection.FacePosition), this.Font, Brushes.Black, 5, this.Height - 20); g.DrawRectangle(Pens.Black, 0, this.Height - 50, this.Width - 1, 25); g.DrawString(this.State, this.Font, Brushes.Black, 5, this.Height - 45); g.DrawRectangle(Pens.Black, 0, 0, this.Width - 1, this.Height - 50); return(pos); }
// *** METHODS *** /// <summary> /// Returns true if this and given PositionSpec is equal /// </summary> /// <param name="compare">Defines the PositionSpec to be compared with</param> /// <returns></returns> public bool Equals(PositionSpec compare) => (compare.CubePosition == this.CubePosition && compare.FacePosition == this.FacePosition);