//if mouse moves the cell over is the mouse pointer currently positioned is drawn here public void OnMouseMove(Object sender, MouseEventArgs e) { HitTestResult aHTR = VisualTreeHelper.HitTest(this, e.GetPosition(sender as IInputElement)); DrawingVisualWithDataContext aDrawingVisualWithDataContext = aHTR.VisualHit as DrawingVisualWithDataContext; if (aDrawingVisualWithDataContext != null) { if (aDrawingVisualWithDataContext.DataContext != null) { //here is the returning to former appereance of cell where mouse pointer was positioned before if (myLastPointedDrawingVisual != null && myLastPointedDrawingVisual.DataContext != null) { CellVM aCellVM_LP = myLastPointedDrawingVisual.DataContext as CellVM; if (aCellVM_LP != null) { CreateCAGridCellGeometry(aCellVM_LP, ref myLastPointedDrawingVisual, false); } } CellVM aCellVM = aDrawingVisualWithDataContext.DataContext as CellVM; if (aCellVM != null) { CreateCAGridCellGeometry(aCellVM, ref aDrawingVisualWithDataContext, true); myLastPointedDrawingVisual = aDrawingVisualWithDataContext; } } } }
//this method gets the frame color of specific cell, it depends on if the cell is selected, marked, or if the mouse pointer is over cell. private Color?GetFrameColor(CellVM cellVMIn, bool isMouseOverIn) { CAGrid2DVM aCAGrid2DVM = this.DataContext as CAGrid2DVM; if (aCAGrid2DVM != null) { if (cellVMIn.IsSelected == true) { return(aCAGrid2DVM.SelectedSelectionFrameColor); } else if (cellVMIn.IsMarked) { return(aCAGrid2DVM.SelectedMarkingColor); } else if (isMouseOverIn == true) { return(aCAGrid2DVM.SelectedMouseOverColor); } else { return(aCAGrid2DVM.SelectedBackgroundColor); } } return(null); }
//this method creates a specific cell private DrawingVisualWithDataContext CreateCell(CellVM cellVMIn) { var aDrawingVisualWithDataContext = new DrawingVisualWithDataContext(); aDrawingVisualWithDataContext.DataContext = cellVMIn; cellVMIn.CellStateChangedEvent += OnCellStateChanged; CreateCAGridCellGeometry(cellVMIn, ref aDrawingVisualWithDataContext, false); return(aDrawingVisualWithDataContext); }
//this method draws the specific cell private void CreateCAGridCellGeometry(CellVM cellVMIn, ref DrawingVisualWithDataContext drawingVisualWithDataContextIn, bool isMouseOverIn) { CAGrid2DVM aCAGrid2DVM = this.DataContext as CAGrid2DVM; if (aCAGrid2DVM != null) { DrawingContext dc = drawingVisualWithDataContextIn.RenderOpen(); int aGridPenWidth = 0; ConstantLists.LineThicknessValue.TryGetValue(aCAGrid2DVM.GridThickness, out aGridPenWidth); // the currently selected grid thickness int aFramePenWidth = 0; ConstantLists.LineThicknessValue.TryGetValue(aCAGrid2DVM.SelFrameThickness, out aFramePenWidth); // the currently selected frame thickness //three brushes are drawn one after one, and that will create different areas of cell - grid, frame and content //Brush for grid Brush aGridBrush = new SolidColorBrush(aCAGrid2DVM.SelectedGridColor); Rect aGridRect = new Rect(cellVMIn.X, cellVMIn.Y, aCAGrid2DVM.CellGridWidth, aCAGrid2DVM.CellGridHeight); dc.DrawRectangle(aGridBrush, null, aGridRect); //Brush for frame Brush aFrameBrush = new SolidColorBrush((Color)GetFrameColor(cellVMIn, isMouseOverIn)); Rect aFrameRect = new Rect(cellVMIn.X + aGridPenWidth, cellVMIn.Y + aGridPenWidth, aCAGrid2DVM.CellGridWidth - 2 * aGridPenWidth, aCAGrid2DVM.CellGridHeight - 2 * aGridPenWidth); dc.DrawRectangle(aFrameBrush, null, aFrameRect); Color?aStateColor = aCAGrid2DVM.GetFillColorForState(cellVMIn.State); // current color of this cell if (aStateColor == null) { throw new CAExplorerException(); } // brush for content Brush aContentBrush = new SolidColorBrush((Color)aStateColor); Rect aContentRect = new Rect(cellVMIn.X + aGridPenWidth + aFramePenWidth, cellVMIn.Y + aGridPenWidth + aFramePenWidth, aCAGrid2DVM.CellObjectWidth, aCAGrid2DVM.CellObjectHeight); dc.DrawRectangle(aContentBrush, null, aContentRect); aGridBrush.Freeze(); aContentBrush.Freeze(); aFrameBrush.Freeze(); dc.Close(); } }
//if cell state of some cell changed, it is needed to redraw the changed cell. public void OnCellStateChanged(Object sender, EventArgs e) { for (int i = 0; i < myVisualChildren.Count; i++) { DrawingVisualWithDataContext aDrawingVisualWithDataContext = myVisualChildren[i] as DrawingVisualWithDataContext; if (aDrawingVisualWithDataContext != null) { CellVM aCellVM = aDrawingVisualWithDataContext.DataContext as CellVM; if (aCellVM != null) { if (aCellVM.IsSelected == true) { CreateCAGridCellGeometry(aCellVM, ref aDrawingVisualWithDataContext, false); } } } } }
//if mouse button is pressed then current cell will be selected public void OnMouseDown(Object sender, MouseEventArgs e) { HitTestResult aHTR = VisualTreeHelper.HitTest(this, e.GetPosition(sender as IInputElement)); DrawingVisualWithDataContext aDrawingVisualWithDataContext = aHTR.VisualHit as DrawingVisualWithDataContext; if (aDrawingVisualWithDataContext != null) { if (aDrawingVisualWithDataContext.DataContext != null) { //here is the returning of before selected cell to normal state if (myLastSelectedDrawingVisual != null && myLastSelectedDrawingVisual.DataContext != null) { CellVM aCellVM_LS = myLastSelectedDrawingVisual.DataContext as CellVM; if (aCellVM_LS != null) { aCellVM_LS.IsSelected = false; CreateCAGridCellGeometry(aCellVM_LS, ref myLastSelectedDrawingVisual, false); } } CellVM aCellVM = aDrawingVisualWithDataContext.DataContext as CellVM; if (aCellVM != null) { CAGrid2DVM aCAGrid2DVM = this.DataContext as CAGrid2DVM; if (aCAGrid2DVM != null) { aCellVM.IsSelected = true; CreateCAGridCellGeometry(aCellVM, ref aDrawingVisualWithDataContext, false); aCAGrid2DVM.SelectedCellViewModel = aCellVM; myLastSelectedDrawingVisual = aDrawingVisualWithDataContext; } } } } }
//in this method the cells are modified if someting changed private void ModifyCAGridCells() { if (myVisualChildren != null) { CAGrid2DVM aCAGrid2DVM = DataContext as CAGrid2DVM; if (aCAGrid2DVM != null) { for (int i = 0; i < myVisualChildren.Count; i++) { DrawingVisualWithDataContext aDVWithDC = myVisualChildren[i] as DrawingVisualWithDataContext; if (aDVWithDC != null) { CellVM aCellVM = aDVWithDC.DataContext as CellVM; if (aCellVM != null) { CreateCAGridCellGeometry(aCellVM, ref aDVWithDC, false); } } } } } }