Example #1
0
        public ConnectorLabel(StateConnector connector)
        {
            connector.AddHandler(this);
            AddHandler(connector);
            _connector        = connector;
            _OffsetFromBezier = new Size();
            _StringBuilder    = new StringBuilder(50);

            foreach (DictionaryEntry de in _connector.SourceState.Transitions)
            {
                var deValue = de.Value as List <State>;
                foreach (var destinedState in deValue)
                {
                    if (destinedState == _connector.DestinationState)
                    {
                        if (_StringBuilder.ToString().Length > 0)
                        {
                            _StringBuilder.Append(", ");
                        }
                        _StringBuilder.Append((char)de.Key);
                        break;
                    }
                }
            }
        }
Example #2
0
        public void BuildAutomata()
        {
            _bShow             = true;
            _SelCtrlPointIndex = -1;
            _angleStep         = 2 * Math.PI / _drawnStateList.Count;
            int   halfWidth = DisplayRectangle.Width / 2;
            Point ptCenter  = new Point(halfWidth, DisplayRectangle.Height / 2);
            //int r = halfWidth * 2 / 3;
            int               r = -200;
            double            a = 0;
            List <Selectable> selectableList = new List <Selectable>();

            //pre calculate States's position
            foreach (State state in _drawnStateList)
            {
                if (state.Label == "q0")
                {
                    _beginState = state;
                }
                state.X = (int)(r * Math.Cos(a)) + ptCenter.X;
                state.Y = (int)(r * Math.Sin(a)) + ptCenter.Y;
                selectableList.Add(state);
                foreach (DictionaryEntry de in state.Transitions)   //tạo đường nối
                {
                    var deValue = de.Value as List <State>;
                    foreach (var destinedState in deValue)
                    {
                        //try to compact State Connectors
                        StateConnector dupConnector = null;
                        if (state == destinedState)
                        {
                            //phải tìm đường nối cong sẵn có
                            dupConnector = (CurvedStateConnector)selectableList.Find
                                               (obj => obj is CurvedStateConnector &&
                                               ((StateConnector)obj).DestinationState == destinedState &&
                                               ((StateConnector)obj).SourceState == state);
                        }
                        else
                        {
                            dupConnector = (StateConnector)selectableList.Find
                                               (obj => obj is StateConnector &&
                                               ((StateConnector)obj).DestinationState == destinedState &&
                                               ((StateConnector)obj).SourceState == state);
                        }
                        if (dupConnector != null)
                        {
                            continue;
                        }
                        else
                        {
                            StateConnector connector;
                            if (state == destinedState)
                            {
                                //nếu trạng thái đi và đến là cùng một trạng thái
                                //thì ta sẽ tạo đường cong
                                CurvedStateConnector newCurvedConnector = new CurvedStateConnector
                                                                              (state, destinedState);
                                var p = new Point();
                                if (state.Y < 305)
                                {
                                    p = new Point(state.Position.X, state.Position.Y - 100);
                                }
                                else
                                {
                                    p = new Point(state.Position.X, state.Position.Y + 100);
                                }
                                var controlPoints = newCurvedConnector.ControlPoints;
                                controlPoints[0].X = p.X - 30;
                                controlPoints[0].Y = controlPoints[1].Y = p.Y;
                                controlPoints[1].X = p.X + 30;
                                connector          = newCurvedConnector;
                            }
                            else
                            {
                                connector = new StateConnector(state, destinedState);
                            }
                            selectableList.Add(connector);
                        }
                    }
                }
                a += _angleStep;
            }
            List <Selectable> labelList = new List <Selectable>();

            foreach (Selectable _selectable in selectableList)
            {
                if (_selectable is StateConnector)
                {
                    var connector = _selectable as StateConnector;
                    connector.CalcArrow();
                    connector.CalcLabelPosition();
                    labelList.Add(connector.Label);
                }
            }
            selectableList.AddRange(labelList);
            selectableList.Sort(CompareSelectables);
            _selectables = selectableList.ToArray();
        }