/// <summary> /// Paints the transition. /// </summary> /// <param name="graphics"><see cref="Graphics"/> instance for painting.</param> public void Paint(Graphics graphics) { // paint background StateView.DrawingTools.SolidBrush.Color = ColorSettings.StateTransition; graphics.FillRectangle(StateView.DrawingTools.SolidBrush, Bounds); // paint border StateView.DrawingTools.Pen.Color = ColorSettings.ForState(StateView.SelectState, StateView.StartingState); graphics.DrawRectangle(StateView.DrawingTools.Pen, Bounds); // paint event text StateView.DrawingTools.SolidBrush.Color = ColorSettings.StateTransitionText; graphics.DrawString(Transition.Event != null ? Transition.Event.Name : String.Empty, StateView.DrawingTools.Font, StateView.DrawingTools.SolidBrush, textLocation); // paint transition if (Transition.StateTo != null) { StateView stateToView = StateView.ScreenControl.FindStateViewForState(Transition.StateTo); if (stateToView != null) { PointF startingPoint, afterStartingPoint, afterStartingPointBezier; PointF endingPoint, beforeEndingPointBezier; float bezierWidthGap = 80; float afterStartWidthGap = 40; // right side of the transition if ((stateToView.Location.X + stateToView.Bounds.Width / 2f) - (Location.X + Bounds.Width / 2f) >= 0) { startingPoint = new PointF(Location.X + Bounds.Width, Location.Y + Bounds.Height / 2f); afterStartingPoint = new PointF(startingPoint.X + afterStartWidthGap, startingPoint.Y); afterStartingPointBezier = new PointF(startingPoint.X + bezierWidthGap, startingPoint.Y); } // left side of the transition else { startingPoint = new PointF(Location.X, Location.Y + Bounds.Height / 2f); afterStartingPoint = new PointF(startingPoint.X - afterStartWidthGap, startingPoint.Y); afterStartingPointBezier = new PointF(startingPoint.X - bezierWidthGap, startingPoint.Y); } // left side of the state if (Math.Abs(stateToView.Location.X - afterStartingPoint.X) < Math.Abs(stateToView.Location.X + stateToView.Bounds.Width - afterStartingPoint.X)) { endingPoint = new PointF(stateToView.Location.X, stateToView.Location.Y + stateToView.TitleHeight / 2f); beforeEndingPointBezier = new PointF(endingPoint.X - bezierWidthGap, endingPoint.Y); } // right side of the state else { endingPoint = new PointF(stateToView.Location.X + stateToView.Bounds.Width, stateToView.Location.Y + stateToView.TitleHeight / 2f); beforeEndingPointBezier = new PointF(endingPoint.X + bezierWidthGap, endingPoint.Y); } graphics.DrawBezier(StateView.DrawingTools.LinePen, startingPoint, afterStartingPointBezier, beforeEndingPointBezier, endingPoint); } } }
/// <summary> /// Handles the KeyDown event of the StateMachineView control. /// </summary> private void StateMachineView_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete && state == StateType.Default && SelectedState != null) { if (StateMachine.States.Count <= 1) { Messages.ShowWarning("State cannot be removed. At least one state must exist."); return; } if (MessageBox.Show("Are you sure?", "Delete State", MessageBoxButtons.OKCancel) == DialogResult.OK) { // remove state from all transitions foreach (State stateMachineState in StateMachine.States) { if (stateMachineState != SelectedState.State) { foreach (Transition transition in stateMachineState.Transitions) { if (transition.StateTo == SelectedState.State) { transition.StateTo = null; } } } } // remove from state machine StateMachine.States.Remove(SelectedState.State); if (StateMachine.StartingState == SelectedState.State) { StateMachine.StartingState = null; } States.Remove(SelectedState); SelectedState.Dispose(); SelectedState = States[0]; if (SelectedStateChanged != null) { SelectedStateChanged(this, EventArgs.Empty); } Messages.ShowInfo("State deleted."); if (AfterDeletingState != null) { AfterDeletingState(this, EventArgs.Empty); } } } }
/// <summary> /// Initializes a new instance of the <see cref="TransitionView"/> class. /// </summary> /// <param name="stateView">The state view where will be the transition used.</param> /// <param name="transition">The transition to use.</param> public TransitionView(StateView stateView, Transition transition) { if (stateView == null) { throw new ArgumentException("StateView cannot be null."); } if (transition == null) { throw new ArgumentException("Transition cannot be null."); } _stateView = stateView; _transition = transition; Transition.EventChanged += new EventHandler(Transition_EventChanged); Transition.StateToChanged += new EventHandler(Transition_StateToChanged); Event = transition.Event; }
/// <summary> /// Handles the NameChanged event of the Event. /// Updates the state view where the transition is used. /// </summary> private void Event_NameChanged(object sender, EventArgs e) { StateView.Refresh(); }
/// <summary> /// Handles the MouseMove event of the StateMachineView control. /// </summary> private void StateMachineView_MouseMove(object sender, MouseEventArgs e) { // select control if is not focused if (!Focused) { Select(); } // update mouse scene position mouseScenePosition = PointAtScene(e.Location); // If the action of moving selected nodes at the scripting scene is active // we will change the location of selected nodes by distance we moved from the last changing. if (state == StateType.MovingState) { // calculate move vector for selected nodes at the scene Point moveVector = e.Location.Sub(lastPosition); // change the location of every selected nodes at the scene SelectedState.Location = SelectedState.Location.Add(moveVector); // set actual position for the next changing lastPosition = e.Location; wholeMovement = wholeMovement.Add(moveVector); Invalidate(); } // If the action of changing the position of the scripting scene is active // we will change the position of the scene by distance we moved from the last changing. else if (state == StateType.MovingScene) { // change the position of the scene Position = Position.Add(lastPosition.Sub(e.Location)); // set actual position for the next changing lastPosition = e.Location; Invalidate(); } else if (state == StateType.Default || state == StateType.ConnectingStates) { // hover state under mouse cursor bool found = false; for (int i = States.Count - 1; i >= 0; --i) { if (States[i].Bounds.Contains(mouseScenePosition)) { if (hoveredState != States[i]) { if (hoveredState != null && hoveredState.SelectState != SelectState.Select) { hoveredState.SelectState = SelectState.Default; } // set as hovered hoveredState = States[i]; if (hoveredState.SelectState != SelectState.Select) { hoveredState.SelectState = SelectState.Hover; } Invalidate(); } found = true; break; } } if (!found && hoveredState != null) { if (hoveredState.SelectState != SelectState.Select) { hoveredState.SelectState = SelectState.Default; } hoveredState = null; Invalidate(); } if (state == StateType.ConnectingStates) { Invalidate(); } } }
/// <summary> /// Handles the MouseDown event of the StateMachineView control. /// </summary> private void StateMachineView_MouseDown(object sender, MouseEventArgs e) { // focus control if is not focused if (!Focused) { Focus(); } // If left mouse button is down, no action is in progress and some node is under mouse cursor // we will start the action of moving selected nodes at the scripting scene. if (e.Button == MouseButtons.Left && state == StateType.Default) { if (SelectedState != null) { SelectedState.SelectState = SelectState.Default; } if (hoveredState != null) { SelectedState = hoveredState; SelectedState.SelectState = SelectState.Select; hoveredState = null; if (SelectedState.TransitionContains(mouseScenePosition, out selectedTransition)) { // activate this action state = StateType.ConnectingStates; } else { // activate this action state = StateType.MovingState; // init lastPosition = e.Location; wholeMovement = new Point(); } if (SelectedStateChanged != null) { SelectedStateChanged(this, EventArgs.Empty); } Invalidate(); } else if (SelectedState != null) { SelectedState = null; if (SelectedStateChanged != null) { SelectedStateChanged(this, EventArgs.Empty); } Invalidate(); } } // If right mouse button is down and no action is in progress // we will start changing the position of the scripting scene (moving scene). else if (e.Button == MouseButtons.Right && state == StateType.Default) { // activate this action state = StateType.MovingScene; // init lastPosition = e.Location; } }
/// <summary> /// Handles the EventChanged event of the Transition. /// Updates the state view where the transition is used. /// </summary> private void Transition_EventChanged(object sender, EventArgs e) { Event = Transition.Event; StateView.Refresh(); }
/// <summary> /// Handles the StateToChanged event of the Transition. /// Updates the state view where the transition is used. /// </summary> private void Transition_StateToChanged(object sender, EventArgs e) { StateView.Refresh(); }