private static void Swap(ref IReadOnlyArea lhs, ref IReadOnlyArea rhs) { IReadOnlyArea temp = lhs; lhs = rhs; rhs = temp; }
public ShipController( IReadOnlyArea area, Func <ISet <IZoneInfo> > getZoneInfosFunc) : this(getZoneInfosFunc, TimeSpan.FromSeconds(5), 500) { _maxDistance = area.AmountOfZonesX + area.AmountOfZonesY; }
/// <summary> /// Returns whether or not the given map area intersects the current one. If you intend to /// determine/use the exact intersection based on this return value, it is best to instead /// call the <see cref="Area.GetIntersection(IReadOnlyArea, IReadOnlyArea)"/>, and /// check the number of positions in the result (0 if no intersection). /// </summary> /// <param name="area">The area to check.</param> /// <returns>True if the given map area intersects the current one, false otherwise.</returns> public bool Intersects(IReadOnlyArea area) { if (!area.Bounds.Intersects(Bounds)) { return(false); } if (Count <= area.Count) { foreach (Point pos in Positions) { if (area.Contains(pos)) { return(true); } } return(false); } foreach (Point pos in area.Positions) { if (Contains(pos)) { return(true); } } return(false); }
/// <summary> /// Adds all positions in the given map area to this one. /// </summary> /// <param name="area">Area containing positions to add.</param> public void Add(IReadOnlyArea area) { foreach (Point pos in area.Positions) { Add(pos); } }
public ZoneSelectionPanelCreator(IReadOnlyArea area, Panel targetPanel) { if (area == null) { throw new ArgumentNullException(nameof(area)); } _targetPanel = targetPanel ?? throw new ArgumentNullException(nameof(targetPanel)); EventHandler currentClickHandler = null; _buttonsAndFactories = area.GetSupportedZoneConsumptionFactories().Reverse() .Select(factory => { var sample = factory(); var button = new Button { Text = string.Format("{0} ({1})", sample.Name, sample.KeyChar.ToString().ToUpperInvariant()), Dock = DockStyle.Top, BackColor = SystemColors.Control, Parent = _targetPanel }; if (_handleKeyCharActions.ContainsKey(sample.KeyChar)) { throw new InvalidOperationException(string.Format("Could not use key char '{0}' as unique identifier for '{1}'.", sample.KeyChar, sample.Name)); } _handleKeyCharActions.Add(sample.KeyChar, () => button.PerformClick()); currentClickHandler = (sender, e) => { foreach (var btn in _buttonsAndFactories.Keys.Where(btn => btn != button)) { btn.Enabled = true; } button.Enabled = false; _currentFactory = factory; CurrentZoneConsumptionSample = _currentFactory(); }; button.Click += currentClickHandler; return(new KeyValuePair <Button, Func <IAreaConsumption> >(button, factory)); }) .ToDictionary(x => x.Key, x => x.Value); currentClickHandler(this, new EventArgs()); if (_currentFactory == null) { throw new InvalidOperationException(); } }
/// <summary> /// Gets an area containing every position in one or both given areas. /// </summary> /// <param name="area1"/> /// <param name="area2"/> /// <returns>An area containing only those positions in one or both of the given areas.</returns> public static Area GetUnion(IReadOnlyArea area1, IReadOnlyArea area2) { Area retVal = new Area(); retVal.Add(area1); retVal.Add(area2); return(retVal); }
public ZoneSelectionPanelCreator(IReadOnlyArea area, Panel targetPanel) { if (area == null) { throw new ArgumentNullException(nameof(area)); } _targetPanel = targetPanel ?? throw new ArgumentNullException(nameof(targetPanel)); _tooltip = new ToolTip(); EventHandler currentClickHandler = null; _buttonsAndFactories = area.GetSupportedZoneConsumptionFactories() .Select(factory => { var sample = factory(); var button = new Button { Dock = DockStyle.Top, BackColor = SystemColors.Control, Parent = _targetPanel, Image = sample.Tile, Size = new Size(68, 68) }; if (_handleKeyCharActions.ContainsKey(sample.KeyChar)) { throw new InvalidOperationException(string.Format("Could not use key char '{0}' as unique identifier for '{1}'.", sample.KeyChar, sample.Name)); } _handleKeyCharActions.Add(sample.KeyChar, () => button.PerformClick()); currentClickHandler = (sender, e) => { _currentFactory = factory; CurrentZoneConsumptionSample = _currentFactory(); }; button.Click += currentClickHandler; _tooltip.SetToolTip(button, $"{sample.Name} ({sample.KeyChar.ToString().ToUpperInvariant()})"); return(new KeyValuePair <Button, Func <IAreaConsumption> >(button, factory)); }) .ToDictionary(x => x.Key, x => x.Value); currentClickHandler(this, new EventArgs()); if (_currentFactory == null) { throw new InvalidOperationException(); } }
private static IZoneInfoPathNode GenerateIZoneInfoPathNodeMock <T>(IReadOnlyArea area) where T : class, IAreaZoneConsumption { var pathNodeMock = new Mock <IZoneInfoPathNode>(); pathNodeMock .Setup(x => x.ZoneInfo) .Returns(GenerateZoneInfoMockFor <T>(area)); return(pathNodeMock.Object); }
private static IZoneInfoPathNode GenerateIZoneInfoPathNodeMock <T>(IReadOnlyArea area) where T : class, IAreaZoneConsumption { var pathNodeMock = MockRepository.GenerateMock <IZoneInfoPathNode>(); pathNodeMock .Expect(x => x.ZoneInfo) .Return(GenerateZoneInfoMockFor <T>(area)); return(pathNodeMock); }
private static bool HasLessY(IReadOnlyArea area, int y) { for (int i = 0; i < area.Count; i++) { if (area[i].Y < y) { return(true); } } return(false); }
/// <summary> /// Gets an area containing all positions in <paramref name="area1"/>, minus those that are in /// <paramref name="area2"/>. /// </summary> /// <param name="area1"/> /// <param name="area2"/> /// <returns>A area with exactly those positions in <paramref name="area1"/> that are NOT in /// <paramref name="area2"/>.</returns> public static Area GetDifference(IReadOnlyArea area1, IReadOnlyArea area2) { Area retVal = new Area(); foreach (Point pos in area1.Positions) { if (area2.Contains(pos)) { continue; } retVal.Add(pos); } return(retVal); }
/// <summary> /// Returns whether or not the given area is completely contained within the current one. /// </summary> /// <param name="area">Area to check.</param> /// <returns> /// True if the given area is completely contained within the current one, false otherwise. /// </returns> public bool Contains(IReadOnlyArea area) { if (!Bounds.Contains(area.Bounds)) { return(false); } foreach (Point pos in area.Positions) { if (!Contains(pos)) { return(false); } } return(true); }
private static IZoneInfo GenerateZoneInfoMockFor <T>(IReadOnlyArea area) where T : class, IAreaZoneConsumption { var zone = new Mock <IZoneInfo>(); var consumptionState = new Mock <IZoneConsumptionState>(); consumptionState .Setup(x => x.GetZoneConsumption()) .Returns(area .GetSupportedZoneConsumptionFactories() .Single(x => x() is T)() as T ); zone .Setup(x => x.ConsumptionState) .Returns(consumptionState.Object); return(zone.Object); }
/// <summary> /// Gets an area containing exactly those positions contained in both of the given areas. /// </summary> /// <param name="area1"/> /// <param name="area2"/> /// <returns>An area containing exactly those positions contained in both of the given areas.</returns> public static Area GetIntersection(IReadOnlyArea area1, IReadOnlyArea area2) { Area retVal = new Area(); if (!area1.Bounds.Intersects(area2.Bounds)) { return(retVal); } if (area1.Count > area2.Count) { Swap(ref area1, ref area2); } foreach (Point pos in area1.Positions) { if (area2.Contains(pos)) { retVal.Add(pos); } } return(retVal); }
/// <summary> /// Creates a MultiArea that has only the given sub-area. /// </summary> /// <param name="area">Sub-area to add.</param> public MultiArea(IReadOnlyArea area) : this(area.Yield()) { }
public Size GetAreaSize(IReadOnlyArea area) { return(new Size(area.AmountOfZonesX * TileWidthAndSizeInPixels, area.AmountOfZonesY * TileWidthAndSizeInPixels)); }
public void Remove(IReadOnlyArea area) => Remove(area.Positions);
/// <inheritdoc /> public AreaConnectionPointPair SelectConnectionPoints(IReadOnlyArea area1, IReadOnlyArea area2) => new AreaConnectionPointPair(area1.Bounds.Center, area2.Bounds.Center);