internal void add(Button button) { string key = button.ns + "." + button.id; if (!firstCreation.ContainsKey(key)) { firstCreation.Add(key, DateTime.UtcNow.getSeconds()); creationCounts.Add(key, 1); } else if (creationCounts[key] >= 0) { creationCounts[key]++; } check(); }
internal FolderSettingsDialog(string texturePath, string toolTip) : base() { this.ToolTip = toolTip; Rect = new Rect(300, 300, Mathf.Max(Screen.width / 4, 350), 0); Title = "Folder Settings"; Dialog = true; Modal = true; iconPickerCommand = new Command(ToolbarManager.NAMESPACE_INTERNAL, "openIconPicker"); iconPickerCommand.TexturePath = texturePath; iconPickerCommand.OnClick += (e) => { openIconPicker(); }; iconPickerButton = new Button(iconPickerCommand); }
internal static Button createToolbarDropdown() { Button button = new Button(NAMESPACE_INTERNAL, "dropdown"); button.toolbarDropdown = true; button.TexturePath = TEXTURE_PATH_DROPDOWN; return button; }
internal static Button createToolbarDropdown() { Command dropdownCommand = new Command(ToolbarManager.NAMESPACE_INTERNAL, "dropdown"); dropdownCommand.TexturePath = TEXTURE_PATH_DROPDOWN; Button button = new Button(dropdownCommand); button.toolbarDropdown = true; return button; }
private Toolbar createFolder(string id, string texturePath, string toolTip, bool visible) { if (visible) { // close all other folders first foreach (Toolbar folder in folders.Values) { folder.Visible = false; } } RelativePosition relativePosition = lastChildPosition.ContainsKey(id) ? lastChildPosition[id] : RelativePosition.DEFAULT; Toolbar newFolder = new Toolbar(Mode.FOLDER, this, relativePosition); newFolder.Visible = visible; folders.Add(id, newFolder); Command folderCommand = new Command(ToolbarManager.NAMESPACE_INTERNAL, id); folderCommand.TexturePath = texturePath; folderCommand.ToolTip = toolTip; Button folderButton = null; folderCommand.OnClick += (e) => { switch (e.MouseButton) { case 0: newFolder.Visible = !newFolder.Visible; if (newFolder.Visible) { foreach (Toolbar otherFolder in folders.Values.Where(f => !f.Equals(newFolder))) { otherFolder.Visible = false; } } break; case 1: openFolderButtonDropdownMenu(newFolder, getPosition(folderButton) + new Vector2(rect.x + PADDING, rect.y + PADDING + folderButton.Size.y + BUTTON_SPACING)); break; } }; folderButton = new Button(folderCommand, this); folderButtons.Add(folderButton, newFolder); add(folderButton); savedFolderSettings.Add(id, new FolderSettings() { buttons = new HashSet<string>(), texturePath = texturePath, toolTip = toolTip }); newFolder.OnVisibleChange += () => { if (!newFolder.Visible) { lastChildPosition.addOrUpdate(id, newFolder.relativePosition); } }; return newFolder; }
private void buttonDrag(DragEvent e) { if (e.draggable.Dragging) { Rectangle dragRect = buttonOrderDraggables[e.draggable]; if (draggedButton == null) { draggedButton = buttons.SingleOrDefault(b => getRect(b).shift(new Vector2(rect.x + PADDING, rect.y + PADDING)).Equals(dragRect.Rect)); } if (draggedButton != null) { draggedButtonRect = dragRect.Rect; Vector2 mousePos = Utils.getMousePosition(); buttonOrderHoveredButton = buttons.SingleOrDefault( b => !b.Equals(draggedButton) && !b.Equals(dropdownMenuButton) && getRect(b).shift(new Vector2(rect.x + PADDING, rect.y + PADDING)).Contains(mousePos)); if (buttonOrderHoveredButton != null) { Rect hoveredButtonRect = getRect(buttonOrderHoveredButton).shift(new Vector2(rect.x + PADDING, rect.y + PADDING)); Toolbar folder = folderButtons.ContainsKey(buttonOrderHoveredButton) ? folderButtons[buttonOrderHoveredButton] : null; if ((folder != null) && // disallow folders in folders !folderButtons.ContainsKey(draggedButton)) { float widthOneThird = hoveredButtonRect.width / 3; float middleX = hoveredButtonRect.x + widthOneThird; if (new Rect(middleX, hoveredButtonRect.y, widthOneThird, hoveredButtonRect.height).Contains(mousePos)) { // middle section buttonOrderDropMarker.Rect = hoveredButtonRect; } else if (new Rect(hoveredButtonRect.x, hoveredButtonRect.y, widthOneThird, hoveredButtonRect.height).Contains(mousePos)) { // left section buttonOrderDropMarker.Rect = new Rect( hoveredButtonRect.x - DropMarker.MARKER_LINE_WIDTH, hoveredButtonRect.y, DropMarker.MARKER_LINE_WIDTH, hoveredButtonRect.height); } else { // right section buttonOrderDropMarker.Rect = new Rect( hoveredButtonRect.x + hoveredButtonRect.width, hoveredButtonRect.y, DropMarker.MARKER_LINE_WIDTH, hoveredButtonRect.height); } } else { bool leftSide = new Rect(hoveredButtonRect.x, hoveredButtonRect.y, hoveredButtonRect.width / 2, hoveredButtonRect.height).Contains(mousePos); // TODO: improve this to show a horizontal drop marker instead of a vertical one for single-column toolbars buttonOrderDropMarker.Rect = new Rect( leftSide ? (hoveredButtonRect.x - DropMarker.MARKER_LINE_WIDTH) : (hoveredButtonRect.x + hoveredButtonRect.width), hoveredButtonRect.y, DropMarker.MARKER_LINE_WIDTH, hoveredButtonRect.height); } } buttonOrderDropMarker.Visible = buttonOrderHoveredButton != null; } } else { if ((draggedButton != null) && (buttonOrderHoveredButton != null)) { Rect hoveredButtonRect = getRect(buttonOrderHoveredButton).shift(new Vector2(rect.x + PADDING, rect.y + PADDING)); Vector2 mousePos = Utils.getMousePosition(); bool leftSide = false; bool intoFolder = false; Toolbar folder = folderButtons.ContainsKey(buttonOrderHoveredButton) ? folderButtons[buttonOrderHoveredButton] : null; if (folder != null) { float widthOneThird = hoveredButtonRect.width / 3; float middleX = hoveredButtonRect.x + widthOneThird; if (new Rect(middleX, hoveredButtonRect.y, widthOneThird, hoveredButtonRect.height).Contains(mousePos)) { intoFolder = true; } else if (new Rect(hoveredButtonRect.x, hoveredButtonRect.y, widthOneThird, hoveredButtonRect.height).Contains(mousePos)) { leftSide = true; } } else { leftSide = new Rect(hoveredButtonRect.x, hoveredButtonRect.y, hoveredButtonRect.width / 2, hoveredButtonRect.height).Contains(mousePos); } if (intoFolder) { moveButtonToFolder(draggedButton, folder); } else { int draggedButtonIdx = buttons.IndexOf(draggedButton); int hoveredButtonIdx = buttons.IndexOf(buttonOrderHoveredButton); if (!leftSide) { hoveredButtonIdx++; } buttons.RemoveAt(draggedButtonIdx); if (hoveredButtonIdx > draggedButtonIdx) { hoveredButtonIdx--; } buttons.Insert(hoveredButtonIdx, draggedButton); } savedButtonOrder = buttons.Where(b => !b.Equals(dropdownMenuButton)).Select(b => b.FullId).ToList(); Dictionary<string, FolderSettings> newSavedFolderSettings = new Dictionary<string, FolderSettings>(); foreach (KeyValuePair<string, Toolbar> entry in folders) { HashSet<string> folderButtonIds = new HashSet<string>(entry.Value.buttons.Select(b => b.FullId)); newSavedFolderSettings.Add(entry.Key, new FolderSettings() { toolTip = savedFolderSettings[entry.Key].toolTip, buttons = folderButtonIds }); } savedFolderSettings = newSavedFolderSettings; fireChange(); } // reset draggables, drop marker, and dragged button hookButtonOrderDraggables(false); hookButtonOrderDraggables(true); } }
internal Vector2 getPosition(Button button) { Vector2 position = new Vector2(float.MinValue, float.MinValue); bool done = false; calculateButtonPositions((b, pos) => { if (!done && b.Equals(button)) { position.x = pos.x - PADDING; position.y = pos.y - PADDING; done = true; } }); return position; }
private void updateVisibleButtons() { Log.debug("updating visible buttons"); // destroy all buttons except folder buttons foreach (Button button in new List<Button>(buttons.Where(b => !b.Equals(dropdownMenuButton) && !folderButtons.ContainsKey(b)))) { button.Destroy(); } mouseHoverButton = null; // create buttons according to configured visible buttons HashSet<string> buttonIds = new HashSet<string>(savedVisibleButtons); foreach (Command command in ToolbarManager.InternalInstance.Commands.Where(c => c.IsInternal)) { buttonIds.Add(command.FullId); } foreach (string id in buttonIds) { Command command = ToolbarManager.InternalInstance.Commands.SingleOrDefault(c => c.FullId == id); if (command != null) { Button button = new Button(command, this); add(button); } } setupConfigureVisibleButtonsButton(); // move existing buttons according to saved folder contents foreach (Button button in new List<Button>(buttons)) { string buttonId = button.FullId; string folderId = savedFolderSettings.SingleOrDefault(kv => kv.Value.buttons.Contains(buttonId)).Key; if (folderId != null) { moveButtonToFolder(button, folders[folderId]); } } }
private int compareButtonsUserOrder(Button b1, Button b2) { string id1 = b1.ns + "." + b1.id; string id2 = b2.ns + "." + b2.id; int idx1 = savedButtonOrder.IndexOf(id1); int idx2 = savedButtonOrder.IndexOf(id2); if ((idx1 >= 0) && (idx2 >= 0)) { return idx1 - idx2; } else if ((idx1 >= 0) && (idx2 < 0)) { return -1; } else if ((idx1 < 0) && (idx2 >= 0)) { return 1; } else { return compareButtonsNaturalOrder(b1, b2); } }
private int compareButtonsNaturalOrder(Button b1, Button b2) { string id1 = b1.ns + "." + b1.id; string id2 = b2.ns + "." + b2.id; return StringComparer.CurrentCultureIgnoreCase.Compare(id1, id2); }
internal void add(Button button) { // destroy old button with the same ID Button oldButton = buttons.SingleOrDefault(b => (b.ns == button.ns) && (b.id == button.id)); if (oldButton != null) { oldButton.Destroy(); } // same with any folders Toolbar folder = folders.Values.SingleOrDefault(f => f.buttons.Any(b => (b.ns == button.ns) && (b.id == button.id))); if (folder != null) { oldButton = folder.buttons.SingleOrDefault(b => (b.ns == button.ns) && (b.id == button.id)); if (oldButton != null) { oldButton.Destroy(); } } // move button to correct folder if necessary string buttonId = button.ns + "." + button.id; string folderId = savedFolderSettings.Where(kv => kv.Value.buttons.Contains(buttonId)).Select(kv => kv.Key).SingleOrDefault(); if ((folderId != null) && folders.ContainsKey(folderId)) { // move to folder folders[folderId].add(button); } else { // add to toolbar button.OnDestroy += buttonDestroyed; buttons.Add(button); sortButtons(buttons, compareButtonsUserOrder); ButtonCreationCounter.Instance.add(button); } // show/hide button if (button.ns != Button.NAMESPACE_INTERNAL) { button.UserVisible = (mode == Mode.TOOLBAR) ? savedVisibleButtons.Contains(button.ns + "." + button.id) : parentToolbar.savedVisibleButtons.Contains(button.ns + "." + button.id); } }
/// <summary> /// Set up a button to configure visible buttons if there is currently no button visible, /// but if there are buttons that could be made visible by the player. /// </summary> private void setupConfigureVisibleButtonsButton() { if (mode == Mode.TOOLBAR) { Button button = new Button(Button.NAMESPACE_INTERNAL, "configureVisibleButtons", this); button.TexturePath = "000_Toolbar/new-button-available"; button.ToolTip = "Configure Visible Toolbar Buttons"; button.OnClick += (e) => { toggleVisibleButtonsSelector(); }; button.Visibility = new FunctionVisibility(() => { bool contentsExist = buttons.Any((b) => (b.ns != Button.NAMESPACE_INTERNAL) && !b.Equals(dropdownMenuButton) && b.EffectivelyVisible); bool contentsVisible = buttons.Any((b) => (b.ns != Button.NAMESPACE_INTERNAL) && !b.Equals(dropdownMenuButton) && b.EffectivelyUserVisible); return contentsExist && !contentsVisible; }); buttons.Add(button); } }
private void remove(Button button) { button.OnDestroy -= buttonDestroyed; buttons.Remove(button); if (folderButtons.ContainsKey(button)) { folderButtons.Remove(button); } }
private void fireButtonSelectionChanged(Button button) { if (OnButtonSelectionChanged != null) { OnButtonSelectionChanged(button); } }
public IButton add(string ns, string id) { if (running) { Button button = new Button(ns, id, toolbar); toolbar.add(button); return button; } else { throw new NotSupportedException("cannot add button to stale ToolbarManager instance"); } }
private void remove(Button button) { button.OnDestroy -= buttonDestroyed; buttons.Remove(button); if (folderButtons.ContainsKey(button)) { folderButtons.Remove(button); } if ((mouseHoverButton != null) && mouseHoverButton.Equals(button)) { mouseHoverButton = null; } }
/// <summary> /// Set up a button to configure visible buttons if there is currently no button visible, /// but if there are buttons that could be made visible by the player. /// </summary> private void setupConfigureVisibleButtonsButton() { if (mode == Mode.TOOLBAR) { Log.debug("setting up button to configure visible buttons"); Command command = new Command(ToolbarManager.NAMESPACE_INTERNAL, "configureVisibleButtons"); command.TexturePath = "000_Toolbar/new-button-available"; command.ToolTip = "Configure Visible Toolbar Buttons"; command.OnClick += (e) => { toggleVisibleButtonsSelector(); }; command.Visibility = new FunctionVisibility(() => { bool contentsExist = ToolbarManager.InternalInstance.Commands.Any(c => !c.IsInternal && c.EffectivelyVisible); bool contentsVisible = buttons.Any(b => !b.IsInternal || folderButtons.ContainsKey(b)); return contentsExist && !contentsVisible; }); Button button = new Button(command, this); add(button); } }
private Toolbar createFolder(string id, string toolTip, bool visible) { if (visible) { // close all other folders first foreach (Toolbar folder in folders.Values) { folder.Visible = false; } } Toolbar newFolder = new Toolbar(Mode.FOLDER, this); newFolder.Visible = visible; folders.Add(id, newFolder); Button folderButton = new Button(Button.NAMESPACE_INTERNAL, id, this); folderButton.TexturePath = "000_Toolbar/folder"; folderButton.ToolTip = toolTip; folderButton.OnClick += (e) => { switch (e.MouseButton) { case 0: newFolder.Visible = !newFolder.Visible; if (newFolder.Visible) { foreach (Toolbar otherFolder in folders.Values.Where(f => !f.Equals(newFolder))) { otherFolder.Visible = false; } } break; case 1: openFolderButtonDropdownMenu(newFolder, getPosition(folderButton) + new Vector2(rect.x + PADDING, rect.y + PADDING + folderButton.Size.y + BUTTON_SPACING)); break; } }; folderButtons.Add(folderButton, newFolder); add(folderButton); savedFolderSettings.Add(id, new FolderSettings() { buttons = new HashSet<string>(), toolTip = toolTip }); return newFolder; }
internal void add(Button button) { // destroy old button with the same ID Button oldButton = buttons.SingleOrDefault(b => b.FullId == button.FullId); if (oldButton != null) { oldButton.Destroy(); } // same with any folders Toolbar folder = folders.Values.SingleOrDefault(f => f.buttons.Any(b => b.FullId == button.FullId)); if (folder != null) { oldButton = folder.buttons.SingleOrDefault(b => b.FullId == button.FullId); if (oldButton != null) { oldButton.Destroy(); } } // move button to correct folder if necessary string buttonId = button.FullId; string folderId = savedFolderSettings.Where(kv => kv.Value.buttons.Contains(buttonId)).Select(kv => kv.Key).SingleOrDefault(); if ((folderId != null) && folders.ContainsKey(folderId)) { // move to folder folders[folderId].add(button); } else { // add to toolbar button.OnDestroy += buttonDestroyed; buttons.Add(button); visibleButtons.reset(); sortButtons(buttons, compareButtonsUserOrder); } }
internal Toolbar(Mode mode = Mode.TOOLBAR, Toolbar parentToolbar = null) { this.mode = mode; this.parentToolbar = parentToolbar; autoHideUnimportantButtonAlpha.a = 0.4f; rect = new Rectangle(new Rect(DEFAULT_X, DEFAULT_Y, DEFAULT_WIDTH, float.MinValue)); if (mode == Mode.TOOLBAR) { dropdownMenuButton = Button.createToolbarDropdown(); dropdownMenuButton.OnClick += (e) => toggleDropdownMenu(); buttons.Add(dropdownMenuButton); setupConfigureVisibleButtonsButton(); draggable = new Draggable(rect, PADDING, (pos) => !dropdownMenuButtonContains(pos) && !resizable.HandleRect.Contains(pos)); resizable = new Resizable(rect, PADDING, (pos) => !dropdownMenuButtonContains(pos)); draggable.OnDrag += (e) => { resizable.Enabled = !draggable.Dragging; toolbarDrag(); }; resizable.OnDrag += (e) => { draggable.Enabled = !resizable.Dragging; toolbarResize(); }; CursorGrabbing.Instance.add(draggable); CursorGrabbing.Instance.add(resizable); CursorGrabbing.Instance.add(this); } else { showBorder = parentToolbar.showBorder; useKSPSkin_ = parentToolbar.UseKSPSkin; } }
internal Rect getRect(Button button) { Rect rect = new Rect(float.MinValue, float.MinValue, float.MinValue, float.MinValue); bool done = false; calculateButtonPositions((b, pos) => { if (!done && b.Equals(button)) { rect = new Rect(pos.x - PADDING, pos.y - PADDING, b.Size.x, b.Size.y); done = true; } }); return rect; }
private void handleMouseHover(Button currentMouseHoverButton) { if ((mouseHoverButton == null) && (currentMouseHoverButton != null)) { currentMouseHoverButton.mouseEnter(); } else if ((mouseHoverButton != null) && (currentMouseHoverButton == null)) { mouseHoverButton.mouseLeave(); } else if ((mouseHoverButton != null) && (currentMouseHoverButton != null) && !currentMouseHoverButton.Equals(mouseHoverButton)) { mouseHoverButton.mouseLeave(); currentMouseHoverButton.mouseEnter(); } mouseHoverButton = currentMouseHoverButton; }
private int compareButtonsUserOrder(Button b1, Button b2) { string id1 = b1.FullId; string id2 = b2.FullId; int idx1 = savedButtonOrder.IndexOf(id1); int idx2 = savedButtonOrder.IndexOf(id2); if ((idx1 >= 0) && (idx2 >= 0)) { return idx1 - idx2; } else if ((idx1 >= 0) && (idx2 < 0)) { return -1; } else if ((idx1 < 0) && (idx2 >= 0)) { return 1; } else { return b1.command.CompareTo(b2.command); } }
private void hookButtonOrderDraggables(bool enabled) { if (enabled) { calculateButtonPositions((button, pos) => { if (!button.Equals(dropdownMenuButton)) { Rectangle buttonRect = new Rectangle(new Rect(rect.x + pos.x, rect.y + pos.y, button.Size.x, button.Size.y)); Draggable draggable = new Draggable(buttonRect, 0, null); draggable.Enabled = true; draggable.OnDrag += buttonDrag; buttonOrderDraggables.Add(draggable, buttonRect); } }); buttonOrderDropMarker = new DropMarker(); } else { foreach (Draggable d in buttonOrderDraggables.Keys) { d.OnDrag -= buttonDrag; } buttonOrderDraggables.Clear(); buttonOrderDropMarker = null; } draggedButton = null; }
internal Toolbar(Mode mode = Mode.TOOLBAR, Toolbar parentToolbar = null, RelativePosition preferredRelativePosition = RelativePosition.DEFAULT) { this.mode = mode; this.parentToolbar = parentToolbar; this.relativePosition = preferredRelativePosition; visibleButtons = new VisibleButtons(buttons, isEffectivelyUserVisible); autoHideUnimportantButtonAlpha.a = 0.4f; rect = new Rectangle(new Rect(DEFAULT_X, DEFAULT_Y, DEFAULT_WIDTH, float.MinValue)); if (mode == Mode.TOOLBAR) { dropdownMenuButton = Button.createToolbarDropdown(); dropdownMenuButton.OnClick += (e) => toggleDropdownMenu(); buttons.Add(dropdownMenuButton); draggable = new Draggable(rect, PADDING, (pos) => !dropdownMenuButtonContains(pos) && !resizable.HandleRect.Contains(pos)); resizable = new Resizable(rect, PADDING, (pos) => !dropdownMenuButtonContains(pos)); draggable.OnDrag += (e) => { resizable.Enabled = !draggable.Dragging; toolbarDrag(); }; resizable.OnDrag += (e) => { draggable.Enabled = !resizable.Dragging; toolbarResize(); }; CursorGrabbing.Instance.add(draggable); CursorGrabbing.Instance.add(resizable); CursorGrabbing.Instance.add(this); ToolbarManager.InternalInstance.OnCommandAdded += updateVisibleButtons; } else { showBorder = parentToolbar.showBorder; useKSPSkin_ = parentToolbar.UseKSPSkin; } }
private bool isEffectivelyUserVisible(Button button) { if (button.command.EffectivelyVisible) { if (!button.IsInternal) { string id = button.FullId; return (mode == Mode.TOOLBAR) ? savedVisibleButtons.Contains(id) : parentToolbar.savedVisibleButtons.Contains(id); } else { return true; } } else { return false; } }
internal static Button createMenuOption(string text) { Command menuOptionCommand = new Command(ToolbarManager.NAMESPACE_INTERNAL, "menuOption"); menuOptionCommand.Text = text; Button button = new Button(menuOptionCommand); return button; }
private void moveButtonToFolder(Button button, Toolbar folder) { remove(button); folder.add(button); }
internal DestroyEvent(Button button) { this.button = button; }
internal static Button createMenuOption(string text) { Button button = new Button("dummy", "dummy"); button.Text = text; return button; }