Esempio n. 1
0
        protected void WriteItem(Point2D cellPos, ISpatial2DThing <T> item)
        {
            if (item.TopLeft.Equals(cellPos))
            {
                WriteStartElement("g");

                if (!string.IsNullOrEmpty(item.Id))
                {
                    XmlWriter.WriteAttributeString("id", item.Id);
                }

                var itemNode = ObjectFormatter.Invoke(item.Data);

                foreach (var pos in item.Positions)
                {
                    WriteRect(pos, item.Colour, new Colour());
                }

                if (itemNode != null)
                {
                    var cent = item.RotationalCentre;

                    WriteStartElement("text");

                    XmlWriter.WriteAttributeString("x", ((cent.X + 0.5) * _unitWidth).ToString());
                    XmlWriter.WriteAttributeString("y", ((cent.Y + 0.5) * _unitHeight).ToString());

                    itemNode.WriteTo(XmlWriter);

                    XmlWriter.WriteEndElement();
                }

                XmlWriter.WriteEndElement();
            }
        }
Esempio n. 2
0
        public bool Equals(ISpatial2DThing <T> other)
        {
            if (other == null)
            {
                return(false);
            }

            if (ReferenceEquals(other, this))
            {
                return(true);
            }

            if (other.Data == null && Data != null)
            {
                return(false);
            }

            if (!other.Data.Equals(Data))
            {
                return(false);
            }

            if (!string.Equals(Id, other.Id))
            {
                return(false);
            }

            if (other.Positions.Count() != Positions.Count())
            {
                return(false);
            }

            return(other.Positions.Zip(Positions, (p1, p2) => p1.Equals(p2)).All(x => x));
        }
Esempio n. 3
0
        /// <summary>
        /// Removes an object from the grid (returns false if the item isn't within the grid)
        /// </summary>
        public bool RemoveObject(ISpatial2DThing <T> thing)
        {
            bool found = false;

            Write(() =>
            {
                var find = _items.Values.Select(v => new { list = v, items = v.Where(x => x.Equals(thing)).ToList() }).ToList();

                foreach (var affected in find)
                {
                    foreach (var item in affected.items)
                    {
                        found |= affected.list.Remove(item);
                    }
                }
            });

            thing.Moved       -= ThingMovedEventHandler;
            thing.BeforeMoved -= ThingBeforeMovedEventHandler;

            if (found)
            {
                FireItemRemoved(thing);

                FireModified();
            }

            return(found);
        }
Esempio n. 4
0
        protected virtual void WriteItem(ISpatial2DThing <T> item)
        {
            var xnode = _objectFormatter.Invoke(item.Data);

            if (xnode != null)
            {
                xnode.WriteTo(_output);
            }
        }
Esempio n. 5
0
        private void FireItemRemoved(ISpatial2DThing <T> thing)
        {
            var ev = ItemRemoved;

            if (ev != null)
            {
                ev.Invoke(this, new ObjectEvent <ISpatial2DThing <T> >(thing));
            }
        }
Esempio n. 6
0
        private void SetObject(ISpatial2DThing <T> thing, bool simulate, bool fireAdded = true)
        {
            Write(() =>
            {
                IList <ISpatial2DThing <T> > items;

                if (thing.Id != null && _items.SelectMany(i => i.Value).Any(x => string.Equals(thing.Id, x.Id)))
                {
                    throw new InvalidOperationException("Item ID already used within grid");
                }

                if (!_items.TryGetValue(thing.TopLeft, out items))
                {
                    _items[thing.TopLeft] = items = new List <ISpatial2DThing <T> >();
                }

                if (!thing.IsWithin(this))
                {
                    throw new ObjectOutOfBoundsException(thing);
                }

                if (items.Contains(thing))
                {
                    throw new InvalidOperationException("Item already added to grid");
                }

                if (!AllowOverlapping && (items.Any() || _items.Values.Any(m => m.Any(x => x.Overlaps(thing)))))
                {
                    throw new InvalidOperationException("Item overlaps existing item");
                }

                if (!simulate)
                {
                    items.Add(thing);

                    thing.BeforeMoved += ThingBeforeMovedEventHandler;

                    thing.Moved += ThingMovedEventHandler;
                }
            });

            if (!simulate && fireAdded)
            {
                var ev = ItemAdded;

                if (ev != null)
                {
                    ev.Invoke(this, new ObjectEvent <ISpatial2DThing <T> >(thing));
                }

                FireModified();
            }
        }
Esempio n. 7
0
        public virtual void WriteCell(Point2D cellPos, int cellIndex, IEnumerable <ISpatial2DThing <T> > contents)
        {
            ISpatial2DThing <T> last = contents.LastOrDefault();

            WriteStartCell();

            if (last != null)
            {
                _output.WriteAttributeString("color", last.Colour.ToHex());
                _output.WriteAttributeString("label", last.Label);
                _output.WriteAttributeString("selected", last.Selected.ToString());
            }

            foreach (var item in contents)
            {
                WriteItem(item);
            }

            _output.WriteEndElement();
        }
Esempio n. 8
0
        public override void WriteCell(Point2D cellPos, int cellIndex, IEnumerable <ISpatial2DThing <T> > contents)
        {
            WriteStartCell();

            ISpatial2DThing <T> last = contents.LastOrDefault();

            if (last != null && !last.Colour.IsTransparent)
            {
                XmlWriter.WriteAttributeString("style", "color: " + last.Colour.ToHex());
            }

            if (contents.Count() == 1 && !string.IsNullOrEmpty(last.Id))
            {
                XmlWriter.WriteAttributeString("id", last.Id);
            }

            foreach (var item in contents)
            {
                WriteItem(item);
            }

            XmlWriter.WriteEndElement();
        }
Esempio n. 9
0
        private void FireItemMoved(ISpatial2DThing <T> item)
        {
            if (_disableMoveEvents)
            {
                return;
            }

            var actions = _actions.Values.Where(r => r.Condition(this, item.Positions)).ToList();

            foreach (var action in actions)
            {
                action.RuleType.Invoke(this, item);
            }

            var ev = ItemMoved;

            if (ev != null)
            {
                ev.Invoke(this, new ObjectEvent <ISpatial2DThing <T> >(item));
            }

            FireModified();
        }
Esempio n. 10
0
        protected virtual void WriteItem(ISpatial2DThing <T> item)
        {
            var s = _objectFormatter.Invoke(item.Data);

            _output.Write((s != null && s.Contains(Delimitter.ToString())) ? "\"" + s + "\"" : s);
        }
Esempio n. 11
0
 /// <summary>
 /// Places a spatial thing onto the grid
 /// </summary>
 public void SetObject(ISpatial2DThing <T> thing)
 {
     SetObject(thing, false);
 }