/// <summary> /// Sets item on map. /// </summary> /// <param name="item">The item to set on map.</param> public void SetItem(ISquareMapItemElement item) { if (item != null) { if (this.ContainsItem(item.Position)) { this.RemoveItem(item); } this.AddItem(item); } }
/// <summary> /// Tries to get item at given position. /// </summary> /// <param name="position">The position to get item for.</param> /// <param name="item">The item instance.</param> /// <returns>True if retrieval was successful, otherwise false.</returns> public bool TryGetItem(Position position, out ISquareMapItemElement item) { item = null; return position.IsInBoundary(this.boundary) && this.items.TryGetValue(position, out item); }
/// <summary> /// Removes given item. /// </summary> /// <param name="item">Item to remove.</param> public void RemoveItem(ISquareMapItemElement item) { /* remove by position */ this.RemoveItem(item.Position); }
/// <summary> /// Clears square map. /// </summary> public void Clear() { /* note on clear: * here all items are removed one by one instead of clearing dictionary. * why? two reasons: * first event detaching has to take place. this is done in remove method. * second ui has to be signalled about such an event - we could have made * another "clear" command as well, but since the map is not that big, * we keep to use item removal. * */ Position[] positions = new Position[this.items.Keys.Count]; lock (this.items) { this.items.Keys.CopyTo(positions, 0); } this.waterSourceItem = null; /* remove one by one */ foreach (Position position in positions) { this.RemoveItem(position); } }
/// <summary> /// Adds item. /// </summary> /// <param name="item">The item to add.</param> public void AddItem(ISquareMapItemElement item) { if (item != null) { /* is water source? */ if (item.Square is IWaterSourceElement) { if (this.waterSourceItem != null) { this.RemoveItem(this.waterSourceItem); } /* mark it */ this.waterSourceItem = item; } /* attach to events */ item.Enter += new EventHandler(OnItemEnter); item.Leave += new EventHandler(OnItemLeave); lock (this.items) { this.items.Add(item.Position, item); } /* tell ui we have added item */ this.ExecuteCommand(PresentationCommands.SquareMap.AddItem, new PresentSquareMapItemParameter(item.Id, item.Position, item.IsActivated, item.IsFixed, item.Square.Id, item.Square.Type)); /* fire event */ if (this.ItemAdded != null) { this.ItemAdded(this, new EventArgs<ISquareMapItemElement>(item)); } /* check if square is water source. if yes, fix it immediately. */ if (item.Square is IWaterSourceElement) { item.Fix(); } } }
/// <summary> /// Initializes navigator. /// </summary> /// <param name="squareMap">The square map this navigator should operate upon.</param> /// <param name="current">The square map item this navigator is pointing to.</param> public void Initialize(ISquareMapElement squareMap, ISquareMapItemElement current) { this.squareMap = squareMap; this.current = current; }
/* active item has changed on square map */ private void OnSquareMapActiveItemChanged(object sender, EventArgs<ISquareMapItemElement> e) { if (e != null) { /* we already have an active item, it's obsolete now. */ if (this.activeItem != null) { /* detach from fix event and restore square */ lock (this.activeItem) { this.activeItem.Fixed -= new EventHandler(OnActiveItemFixed); this.activeItem.RestoreSquare(); } } /* there's a new active item */ if (e.Value != null) { /* assign */ this.activeItem = e.Value; /* attach to fix event and overlay with current pipe */ lock (this.activeItem) { this.activeItem.Fixed += new EventHandler(OnActiveItemFixed); this.activeItem.OverlaySquare(this.CurrentPipe); } } } }