protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { //是否自己下 if (IsRedTurn != IsRedSelected) { return; } //判断棋局是否开始 if (!IsRedReady || !IsBlueReady) { return; } Point location = e.GetPosition(this); HitTestResult result = VisualTreeHelper.HitTest(this, location); ChessmanControl chessmanControl = result.VisualHit as ChessmanControl; //如果选中的和之前的一样或没有选中任何棋子 if (chessmanControl == null && _currentChessmanControl == null || chessmanControl == _currentChessmanControl) { return; } if (_currentChessmanControl == null) { //判断是否选中自己的棋子 if (chessmanControl.Chessman.IsRed == IsRedSelected) { chessmanControl.isSelected = true; _currentChessmanControl = chessmanControl; } } else //判断是否选择其他子 if (chessmanControl != null && IsRedSelected == chessmanControl.Chessman.IsRed) { _currentChessmanControl.isSelected = false; chessmanControl.isSelected = true; _currentChessmanControl = chessmanControl; } else { //得到实际需要移动的位置 int xActual = (int)((Math.Round((location.X - CellWidth) / CellWidth))); int yActual = (int)(Math.Round((location.Y - CellWidth) / CellWidth)); //判断是否和上次选中的一样 if (_currentChessmanControl.Chessman.Location.X != xActual || _currentChessmanControl.Chessman.Location.Y != yActual) { _currentChessmanControl.Chessman.Location = new ChessPoint(xActual, yActual); } } }
protected override Size ArrangeOverride(Size finalSize) { for (int i = 0; ChessmanCollection != null && i < ChessmanCollection.Count; i++) { ChessmanControl item = ChessmanCollection[i] as ChessmanControl; item.Arrange(new Rect( new Point(item.Chessman.Location.X * CellWidth - item.DesiredSize.Width / 2 + CellWidth, item.Chessman.Location.Y * CellWidth - item.DesiredSize.Height / 2 + CellWidth), item.DesiredSize)); } return(this.DesiredSize); }
/// <summary> /// 初始化提示器 /// </summary> void InitializeReminder() { int frameCount = 0; CompositionTargetRenderingListener _renderingListener = new CompositionTargetRenderingListener(); ChessmanControl tempChessmanControl = null; _renderingListener.Rendering += delegate { if (tempChessmanControl == null) { tempChessmanControl = _lastMoveChessman; } if (tempChessmanControl != null) { if (frameCount % 20 == 0) { tempChessmanControl.Opacity = tempChessmanControl.Opacity > 0 ? 0 : 1; } frameCount++; if (tempChessmanControl != _lastMoveChessman || //已经换了棋子 frameCount > 120 || //提醒大于规定 !IsRedReady || //红方没有开始,或已经结束 !IsBlueReady) //蓝方没有开始,或已经结束 { tempChessmanControl.Opacity = 1; frameCount = 0; tempChessmanControl = null; _renderingListener.StopListening(); } } }; _dispatcherTimer.Interval = TimeSpan.FromSeconds(10); _dispatcherTimer.Tick += delegate { _renderingListener.StartListening(); }; }