/// <summary> /// Draw all the available labels onto the screen (drawArea). /// </summary> /// <param name="drawArea">The area we are drawing on</param> public void Draw(DrawArea drawArea) { this.drawArea = drawArea; if (!Properties.Settings.Default.showLabels) { return; } float closestDistanceSquared = float.MaxValue; foreach (StorableLabel label in labels.Labels) { if (!dragging || !label.Equals(draggingLabelToReplace)) { DrawLabel(label); } float distanceSquared = (float)WorldLocation.GetDistanceSquared2D(label.WorldLocation, drawArea.MouseLocation); if (distanceSquared < closestDistanceSquared) { closestDistanceSquared = distanceSquared; closestToMouseLabel = label; } } if (dragging) { DrawLabel(draggingLabel); } }
/// <summary> /// Remove a label from the list /// </summary> /// <param name="oldLabel">the label to remove</param> internal void Delete(StorableLabel oldLabel) { if (_labels.Contains(oldLabel)) { _labels.Remove(oldLabel); } }
/// <summary> /// Replace an existing label with a new one in the same place. This acts as a modification of the label which itself /// is not possible since labels are immutable /// </summary> /// <param name="oldLabel">The label to be replaced (should already be in the list of labels</param> /// <param name="newLabel">The new lable to put in the list</param> internal void Replace(StorableLabel oldLabel, StorableLabel newLabel) { int index = _labels.IndexOf(oldLabel); if (index != -1) { _labels[index] = newLabel; } }
/// <summary> /// Callback that is called when the user clicks on the menuitem connected to a new label /// </summary> /// <param name="oldLabel">The old label that needs to be replaced</param> /// <param name="mouseX">Current X-location of the mouse to determine popup location</param> /// <param name="mouseY">Current Y-location of the mouse to determine popu location</param> private void ModifyLabel(StorableLabel oldLabel, int mouseX, int mouseY) { var labelInputPopup = new EditLabel(oldLabel.LabelText, mouseX, mouseY, (newLabelText) => { if (newLabelText == null) { labels.Delete(oldLabel); } else { var newLabel = new StorableLabel(oldLabel.WorldLocation, newLabelText); labels.Replace(oldLabel, newLabel); } }, allowDelete: true); TrackViewer.Localize(labelInputPopup); labelInputPopup.ShowDialog(); }
/// <summary> /// Do the dragging itself, meaning moving the label to a different position and show that /// </summary> internal void OnLeftMouseMoved() { if (!dragging) { return; } //We have three locations. Where the original/reference label. Where the dragging starts, and where the mouse now is //The new location is then 'original' + 'current' - 'start'. draggingStartLocation = draggingStartLocation.NormalizeTo(drawArea.MouseLocation.TileX, drawArea.MouseLocation.TileZ); WorldLocation shiftedLocation = new WorldLocation( draggingLabelToReplace.WorldLocation.TileX + drawArea.MouseLocation.TileX - draggingStartLocation.TileX, draggingLabelToReplace.WorldLocation.TileZ + drawArea.MouseLocation.TileZ - draggingStartLocation.TileZ, draggingLabelToReplace.WorldLocation.Location.X + drawArea.MouseLocation.Location.X - draggingStartLocation.Location.X, 0, draggingLabelToReplace.WorldLocation.Location.Z + drawArea.MouseLocation.Location.Z - draggingStartLocation.Location.Z, true); draggingLabel = new StorableLabel(shiftedLocation, draggingLabelToReplace.LabelText); }
/// <summary> /// Initiate dragging /// </summary> internal void OnLeftMouseClick() { draggingStartLocation = drawArea.MouseLocation; draggingLabelToReplace = closestToMouseLabel; dragging = true; }
/// <summary> /// Helper routine to just draw one label /// </summary> /// <param name="label">The lable to draw</param> private void DrawLabel(StorableLabel label) { drawArea.DrawExpandingString(label.WorldLocation, label.LabelText, 0, -fontHeight / 2); }
/// <summary> /// Add a label /// </summary> /// <param name="location">The location in MSTS coordinates of the label</param> /// <param name="text">The text of the label</param> public void Add(WorldLocation location, string text) { var newLabel = new StorableLabel(location, text); _labels.Add(newLabel); }