Exemple #1
0
 private void OnMouseLeftClick(float x, float y)
 {
     if (ViewPortContains(x, y))
     {
         if (CurrentOperation == ObjectOperation.Placing && newObject != null)
         {
             newObject.Pos = new Vector2(x, y) - parent.camera.CurrentOffset;
             WorldObjects.Add(newObject);
             newObject = new WorldObject(newObject, newObject.Pos);
         }
         if (CurrentOperation == ObjectOperation.None && !parent.grid.EditModeEnabled)
         {
             currentSelection = null;
             MouseLeftPressed?.Invoke(x, y);
             ObjectSelection?.Invoke(ref currentSelection);
             if (currentSelection != null)
             {
                 InputStateChanged?.Invoke(false);
             }
             else
             {
                 InputStateChanged?.Invoke(true);
             }
         }
         if (CurrentOperation == ObjectOperation.Placing && currentSelection != null)
         {
             currentSelection.Pos = new Vector2(x, y) + mousePadding - parent.camera.CurrentOffset;
             parent.inputHandler.MouseMovedEvent -= OnMousePosChanged;
             CurrentOperation = ObjectOperation.None;
             InputStateChanged?.Invoke(true);
         }
     }
 }
Exemple #2
0
 private void OnKeyPress(Keys key)
 {
     if (key == Keys.G)
     {
         MoveSelectedItem();
     }
     if (key == Keys.Escape)
     {
         if (CurrentOperation == ObjectOperation.None && currentSelection != null)
         {
             currentSelection = null;
             ObjectSelection?.Invoke(ref currentSelection);
             InputStateChanged?.Invoke(true);
         }
     }
     if (key == Keys.Delete)
     {
         if (CurrentOperation == ObjectOperation.None && currentSelection != null)
         {
             WorldObjects.Remove(currentSelection);
             currentSelection.Destroy();
             currentSelection = null;
             InputStateChanged?.Invoke(true);
             ObjectSelection?.Invoke(ref currentSelection);
         }
     }
     if (key == Keys.D)
     {
         if (CurrentOperation == ObjectOperation.None && currentSelection != null)
         {
             DuplicateObject();
         }
     }
 }
Exemple #3
0
 private void DuplicateObject()
 {
     currentSelection = new WorldObject(currentSelection, parent.inputHandler.GetMousePos() + mousePadding - parent.camera.CurrentOffset);
     WorldObjects.Add(currentSelection);
     CurrentOperation = ObjectOperation.Placing;
     parent.inputHandler.MouseMovedEvent += OnMousePosChanged;
     InputStateChanged?.Invoke(false);
     ObjectSelection?.Invoke(ref currentSelection);
 }
Exemple #4
0
 public void EndObjectAddition()
 {
     if (!inputDisabled)
     {
         CurrentOperation = ObjectOperation.None;
         newObject.Destroy();
         newObject = null;
         parent.inputHandler.MouseMovedEvent -= OnMousePosChanged;
         InputStateChanged?.Invoke(true);
     }
 }
Exemple #5
0
 public void StartObjectAddition(WorldObject worldObj)
 {
     if (!inputDisabled)
     {
         CurrentOperation = ObjectOperation.Placing;
         currentSelection = null;
         newObject        = worldObj;
         parent.inputHandler.MouseMovedEvent += OnMousePosChanged;
         parent.grid.EditModeEnabled          = false;
         InputStateChanged?.Invoke(false);
     }
 }
Exemple #6
0
 private void MoveSelectedItem()
 {
     if (CurrentOperation == ObjectOperation.None && currentSelection != null)
     {
         CurrentOperation = ObjectOperation.Placing;
         mousePadding     = (currentSelection.Pos + parent.camera.CurrentOffset) - parent.inputHandler.GetMousePos();
         parent.inputHandler.MouseMovedEvent += OnMousePosChanged;
         InputStateChanged?.Invoke(false);
     }
     else if (CurrentOperation == ObjectOperation.Placing && currentSelection != null)
     {
         currentSelection.Pos = parent.inputHandler.GetMousePos() + mousePadding - parent.camera.CurrentOffset;
         parent.inputHandler.MouseMovedEvent -= OnMousePosChanged;
         CurrentOperation = ObjectOperation.None;
         InputStateChanged?.Invoke(true);
     }
 }
        /// <summary>
        /// Closes this window.
        /// </summary>
        public void Close()
        {
            if (!IsOpen)
            {
                return;
            }

            IsOpen = false;

            if (m_TogglesChildren)
            {
                UiWindow[] windows = GetComponentsInChildren <UiWindow>(true);

                foreach (UiWindow window in windows)
                {
                    if (window == this)
                    {
                        continue;
                    }

                    window.Close();
                }
            }

            if (m_Image != null)
            {
                m_Image.enabled = false;
            }

            foreach (Transform t in transform)
            {
                if (t == transform)
                {
                    continue;
                }

                t.gameObject.SetActive(false);
            }

            if (!ShouldBlockInput() && m_IsBlockingInput)
            {
                m_IsBlockingInput = false;
                InputStateChanged?.Invoke(true);
            }
        }
        internal void Initialize(string intersectionId)
        {
            // Check unicity
            var ids = new List <List <TLCObjectBase> >
            {
                new List <TLCObjectBase>(InternalSignalGroups),
                new List <TLCObjectBase>(InternalDetectors),
                new List <TLCObjectBase>(InternalInputs),
                new List <TLCObjectBase>(InternalOutputs),
                new List <TLCObjectBase>(InternalVariables)
            };

            if (InternalIntersections.Count == 0)
            {
                throw new TLCFISessionException("No intersections are present in the collected data; cannot initialize StateManager.", true);
            }
            foreach (var l in ids)
            {
                foreach (var item1 in l)
                {
                    if (l.Any(item2 => item1 != item2 && item1.Id == item2.Id))
                    {
                        throw new DuplicateNameException($"Found duplicate IDs: type {item1.ObjectType}, id {item1.Id}. " +
                                                         "All Ids in TLC config must be unique per type.");
                    }
                }
            }

            // Build complete list
            if (Facilities != null)
            {
                _staticObjects.Add("_f_" + Facilities.Id, Facilities);
            }
            if (SpvhGenerator != null)
            {
                _staticObjects.Add("_sp_" + SpvhGenerator.Id, SpvhGenerator);
            }
            foreach (var sg in InternalSignalGroups)
            {
                sg.ChangedState += (o, e) =>
                {
                    if (RequestedStates.TryGetValue("sg" + sg.Id, out ulong ticks))
                    {
                        // note: wrapping around uint.MaxValue goes by itself in C#
                        _requestedStatesTimings.Enqueue((int)(TicksGenerator.Default.GetCurrentTicks() - ticks));
                        if (_requestedStatesTimings.Count > 50)
                        {
                            _requestedStatesTimings.Dequeue();
                        }
                        AvgResponseToRequestsTime = _requestedStatesTimings.Sum() / (double)_requestedStatesTimings.Count;
                        RequestedStates.Remove("sg" + sg.Id);
                    }
                    SignalGroupStateChanged?.Invoke(this, sg);
                };
                sg.ChangedPredictions += (o, e) =>
                {
                    if (RequestedStates.TryGetValue("pr" + sg.Id, out ulong ticks))
                    {
                        // note: wrapping around uint.MaxValue goes by itself in C#
                        _requestedStatesTimings.Enqueue((int)(TicksGenerator.Default.GetCurrentTicks() - ticks));
                        if (_requestedStatesTimings.Count > 50)
                        {
                            _requestedStatesTimings.Dequeue();
                        }
                        AvgResponseToRequestsTime = _requestedStatesTimings.Sum() / (double)_requestedStatesTimings.Count;
                        RequestedStates.Remove("pr" + sg.Id);
                    }
                    SignalGroupPredictionsChanged?.Invoke(this, sg);
                };
                _staticObjects.Add("_sg_" + sg.Id, sg);
            }
            foreach (var d in InternalDetectors)
            {
                d.ChangedState += (o, e) => { DetectorStateChanged?.Invoke(this, d); };
                _staticObjects.Add("_d_" + d.Id, d);
            }
            foreach (var i in InternalInputs)
            {
                i.ChangedState += (o, e) => { InputStateChanged?.Invoke(this, i); };
                _staticObjects.Add("_i_" + i.Id, i);
            }
            foreach (var o in InternalOutputs)
            {
                o.ChangedState += (o2, e) =>
                {
                    if (RequestedStates.TryGetValue("os" + o.Id, out ulong ticks))
                    {
                        // note: wrapping around uint.MaxValue goes by itself in C#
                        _requestedStatesTimings.Enqueue((int)(TicksGenerator.Default.GetCurrentTicks() - ticks));
                        if (_requestedStatesTimings.Count > 50)
                        {
                            _requestedStatesTimings.Dequeue();
                        }
                        AvgResponseToRequestsTime = _requestedStatesTimings.Sum() / (double)_requestedStatesTimings.Count;
                        RequestedStates.Remove("os" + o.Id);
                    }
                    OutputStateChanged?.Invoke(this, o);
                };
                _staticObjects.Add("_o_" + o.Id, o);

                // Set exclusive: if an output belongs to an intersection, it is exclusive
                if (InternalIntersections.SelectMany(x => x.Outputs).Any(x => x == o.Id))
                {
                    o.Exclusive = true;
                }
            }
            foreach (var v in InternalVariables)
            {
                v.ChangedState += (o, e) => { VariableChanged?.Invoke(this, v); };
                v.ChangedState += (o2, e) =>
                {
                    if (RequestedStates.TryGetValue("va" + v.Id, out ulong ticks))
                    {
                        // note: wrapping around uint.MaxValue goes by itself in C#
                        _requestedStatesTimings.Enqueue((int)(TicksGenerator.Default.GetCurrentTicks() - ticks));
                        if (_requestedStatesTimings.Count > 50)
                        {
                            _requestedStatesTimings.Dequeue();
                        }
                        AvgResponseToRequestsTime = _requestedStatesTimings.Sum() / (double)_requestedStatesTimings.Count;
                        RequestedStates.Remove("va" + v.Id);
                    }
                    VariableChanged?.Invoke(this, v);
                };
                _staticObjects.Add("_v_" + v.Id, v);
            }
            foreach (var i in InternalIntersections)
            {
                _staticObjects.Add("_int_" + i.Id, i);
                i.ChangedState += (o, e) => { IntersectionStateChanged?.Invoke(this, i); };
                if (i.Id == intersectionId)
                {
                    Intersection = i;
                }
            }

            // Initialize properties
            StaticObjects  = new ReadOnlyDictionary <string, object>(_staticObjects);
            DynamicObjects = new Dictionary <string, object>();
            Intersections  = new ReadOnlyCollection <Intersection>(InternalIntersections);
            SignalGroups   = new ReadOnlyCollection <SignalGroup>(InternalSignalGroups);
            Detectors      = new ReadOnlyCollection <Detector>(InternalDetectors);
            Inputs         = new ReadOnlyCollection <Input>(InternalInputs);
            Outputs        = new ReadOnlyCollection <Output>(InternalOutputs);
            Variables      = new ReadOnlyCollection <Variable>(InternalVariables);

            _logger.Info("Initializing data from remote TLC completed. TLC has:");
            _logger.Info("  - {0} intersections", InternalIntersections.Count);
            _logger.Info("  - {0} signalgroups", InternalSignalGroups.Count);
            _logger.Info("  - {0} detectors", InternalDetectors.Count);
            _logger.Info("  - {0} outputs", InternalOutputs.Count);
            _logger.Info("  - {0} inputs", InternalInputs.Count);
            _logger.Info("  - {0} variables", InternalVariables.Count);
            _logger.Info("  - {0} spvehiclegens", SpvhGenerator == null ? 0 : 1);
            foreach (var i in InternalIntersections)
            {
                _logger.Info("  Intersection {0} has:", i.Id);
                _logger.Info("    - {0} signalgroups", i.Signalgroups.Length);
                _logger.Info("    - {0} detectors", i.Detectors.Length);
                _logger.Info("    - {0} outputs", i.Outputs.Length);
                _logger.Info("    - {0} inputs", i.Inputs.Length);
                _logger.Info("    - {0} spvehiclegens", i.Spvehgenerator == null ? 0 : 1);
            }
        }