| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using bs;
- namespace bs.Editor
- {
- /** @addtogroup Library
- * @{
- */
- /// <summary>
- /// Manages GUI for the content area of the library window. Content area displays resources as a grid or list of
- /// resource icons.
- /// </summary>
- internal class LibraryGUIContent
- {
- internal const int TOP_MARGIN = 8;
- internal const int LIST_ENTRY_SPACING = 6;
- private GUIPanel mainPanel;
- private GUILayout main;
- private GUIPanel overlay;
- private GUIPanel underlay;
- private GUIPanel deepUnderlay;
- private GUIPanel renameOverlay;
- private LibraryWindow window;
- private GUIScrollArea parent;
- private int tileSize;
- private bool gridLayout;
- private int elementsPerRow;
- private int paddingLeft;
- private int paddingRight;
- private List<LibraryGUIEntry> entries = new List<LibraryGUIEntry>();
- private Dictionary<string, LibraryGUIEntry> entryLookup = new Dictionary<string, LibraryGUIEntry>();
- /// <summary>
- /// Area of the content area relative to the parent window.
- /// </summary>
- public Rect2I Bounds
- {
- get { return main.Bounds; }
- }
- /// <summary>
- /// Number of elements per row. Only relevant for grid layouts.
- /// </summary>
- public int ElementsPerRow
- {
- get { return elementsPerRow; }
- }
- /// <summary>
- /// Returns the padding to the left of the first element in a row of elements in a grid layout.
- /// </summary>
- public int PaddingLeft
- {
- get { return paddingLeft; }
- }
- /// <summary>
- /// Returns the padding to the right of the last element in a row of elements in a grid layout.
- /// </summary>
- public int PaddingRight
- {
- get { return paddingRight; }
- }
- /// <summary>
- /// Determines is the content display in a grid (true) or list (false) layout.
- /// </summary>
- public bool GridLayout
- {
- get { return gridLayout; }
- }
- /// <summary>
- /// Sizes of a single resource tile in grid layout. Size in list layout is fixed.
- /// </summary>
- public int TileSize
- {
- get { return tileSize; }
- }
- /// <summary>
- /// Returns objects representing each of the displayed resource icons.
- /// </summary>
- public List<LibraryGUIEntry> Entries
- {
- get { return entries; }
- }
- /// <summary>
- /// Returns parent window the content area is part of.
- /// </summary>
- public LibraryWindow Window
- {
- get { return window; }
- }
- /// <summary>
- /// Returns a GUI panel that can be used for displaying elements underneath the resource tiles.
- /// </summary>
- public GUIPanel Underlay
- {
- get { return underlay; }
- }
- /// <summary>
- /// Returns a GUI panel that can be used for displaying elements underneath the resource tiles. Displays under
- /// <see cref="Underlay"/>.
- /// </summary>
- public GUIPanel DeepUnderlay
- {
- get { return deepUnderlay; }
- }
- /// <summary>
- /// Returns a GUI panel that can be used for displaying elements above the resource tiles.
- /// </summary>
- public GUIPanel Overlay
- {
- get { return overlay; }
- }
- /// <summary>
- /// Returns a GUI panel that can be used for displaying rename input box. Displayed on top of the resource tiles
- /// and the standard overlay.
- /// </summary>
- public GUIPanel RenameOverlay
- {
- get { return renameOverlay; }
- }
- /// <summary>
- /// Constructs a new GUI library content object.
- /// </summary>
- /// <param name="window">Parent window the content area is part of.</param>
- /// <param name="parent">Scroll area the content area is part of.</param>
- public LibraryGUIContent(LibraryWindow window, GUIScrollArea parent)
- {
- this.window = window;
- this.parent = parent;
- }
- /// <summary>
- /// Refreshes the contents of the content area. Must be called at least once after construction.
- /// </summary>
- /// <param name="viewType">Determines how to display the resource tiles.</param>
- /// <param name="entriesToDisplay">Project library entries to display.</param>
- /// <param name="bounds">Bounds within which to lay out the content entries.</param>
- public void Refresh(ProjectViewType viewType, LibraryEntry[] entriesToDisplay, Rect2I bounds)
- {
- if (mainPanel != null)
- mainPanel.Destroy();
- entries.Clear();
- entryLookup.Clear();
- mainPanel = parent.Layout.AddPanel();
- GUIPanel contentPanel = mainPanel.AddPanel(1);
- overlay = mainPanel.AddPanel(0);
- underlay = mainPanel.AddPanel(2);
- deepUnderlay = mainPanel.AddPanel(3);
- renameOverlay = mainPanel.AddPanel(-1);
- main = contentPanel.AddLayoutY();
- List<ResourceToDisplay> resourcesToDisplay = new List<ResourceToDisplay>();
- foreach (var entry in entriesToDisplay)
- {
- if (entry.Type == LibraryEntryType.Directory)
- resourcesToDisplay.Add(new ResourceToDisplay(entry.Path, LibraryGUIEntryType.Single));
- else
- {
- FileEntry fileEntry = (FileEntry)entry;
- ResourceMeta[] metas = fileEntry.ResourceMetas;
- if (metas.Length > 0)
- {
- if (metas.Length == 1)
- resourcesToDisplay.Add(new ResourceToDisplay(entry.Path, LibraryGUIEntryType.Single));
- else
- {
- resourcesToDisplay.Add(new ResourceToDisplay(entry.Path, LibraryGUIEntryType.MultiFirst));
- for (int i = 1; i < metas.Length - 1; i++)
- {
- string path = Path.Combine(entry.Path, metas[i].SubresourceName);
- resourcesToDisplay.Add(new ResourceToDisplay(path, LibraryGUIEntryType.MultiElement));
- }
- string lastPath = Path.Combine(entry.Path, metas[metas.Length - 1].SubresourceName);
- resourcesToDisplay.Add(new ResourceToDisplay(lastPath, LibraryGUIEntryType.MultiLast));
- }
- }
- }
- }
- int minHorzElemSpacing = 0;
- if (viewType == ProjectViewType.List16)
- {
- tileSize = 16;
- gridLayout = false;
- elementsPerRow = 1;
- minHorzElemSpacing = 0;
- int elemWidth = bounds.width;
- int elemHeight = tileSize;
- main.AddSpace(TOP_MARGIN);
- for (int i = 0; i < resourcesToDisplay.Count; i++)
- {
- ResourceToDisplay entry = resourcesToDisplay[i];
- LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, main, entry.path, i, elemWidth, elemHeight, 0,
- entry.type);
- entries.Add(guiEntry);
- entryLookup[guiEntry.path] = guiEntry;
- if (i != resourcesToDisplay.Count - 1)
- main.AddSpace(LIST_ENTRY_SPACING);
- }
- main.AddFlexibleSpace();
- }
- else
- {
- int elemWidth = 0;
- int elemHeight = 0;
- int vertElemSpacing = 0;
- switch (viewType)
- {
- case ProjectViewType.Grid64:
- tileSize = 64;
- elemWidth = tileSize;
- elemHeight = tileSize + 36;
- minHorzElemSpacing = 10;
- vertElemSpacing = 12;
- break;
- case ProjectViewType.Grid48:
- tileSize = 48;
- elemWidth = tileSize;
- elemHeight = tileSize + 36;
- minHorzElemSpacing = 8;
- vertElemSpacing = 10;
- break;
- case ProjectViewType.Grid32:
- tileSize = 32;
- elemWidth = tileSize + 16;
- elemHeight = tileSize + 48;
- minHorzElemSpacing = 6;
- vertElemSpacing = 10;
- break;
- }
- gridLayout = true;
- int availableWidth = bounds.width;
- elementsPerRow = MathEx.FloorToInt((availableWidth - minHorzElemSpacing) / (float)(elemWidth + minHorzElemSpacing));
- int numRows = MathEx.CeilToInt(resourcesToDisplay.Count / (float)Math.Max(elementsPerRow, 1));
- int neededHeight = numRows * elemHeight + TOP_MARGIN;
- if (numRows > 0)
- neededHeight += (numRows - 1)* vertElemSpacing;
- bool requiresScrollbar = neededHeight > bounds.height;
- if (requiresScrollbar)
- {
- availableWidth -= parent.ScrollBarWidth;
- elementsPerRow = MathEx.FloorToInt((availableWidth - minHorzElemSpacing) / (float)(elemWidth + minHorzElemSpacing));
- }
- int extraRowSpace = availableWidth - minHorzElemSpacing * 2 - elementsPerRow * elemWidth;
- paddingLeft = minHorzElemSpacing;
- paddingRight = minHorzElemSpacing;
- float horzSpacing = 0.0f;
- // Distribute the spacing to the padding, as there are not in-between spaces to apply it to
- if (extraRowSpace > 0 && elementsPerRow < 2)
- {
- int extraPadding = extraRowSpace / 2;
- paddingLeft += extraPadding;
- paddingRight += (extraRowSpace - extraPadding);
- extraRowSpace = 0;
- }
- else
- horzSpacing = extraRowSpace / (float)(elementsPerRow - 1);
- elementsPerRow = Math.Max(elementsPerRow, 1);
- main.AddSpace(TOP_MARGIN);
- GUILayoutX rowLayout = main.AddLayoutX();
- rowLayout.AddSpace(paddingLeft);
- float spacingCounter = 0.0f;
- int elemsInRow = 0;
- for (int i = 0; i < resourcesToDisplay.Count; i++)
- {
- if (elemsInRow == elementsPerRow && elemsInRow > 0)
- {
- main.AddSpace(vertElemSpacing);
- rowLayout = main.AddLayoutX();
- rowLayout.AddSpace(paddingLeft);
- elemsInRow = 0;
- spacingCounter = 0.0f;
- }
- ResourceToDisplay entry = resourcesToDisplay[i];
- elemsInRow++;
- if (elemsInRow != elementsPerRow)
- spacingCounter += horzSpacing;
- int spacing = (int)spacingCounter;
- spacingCounter -= spacing;
- LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, rowLayout, entry.path, i, elemWidth, elemHeight,
- spacing, entry.type);
- entries.Add(guiEntry);
- entryLookup[guiEntry.path] = guiEntry;
- if (elemsInRow == elementsPerRow)
- rowLayout.AddSpace(paddingRight);
- rowLayout.AddSpace(spacing);
- }
- int extraElements = elementsPerRow - elemsInRow;
- int extraSpacing = 0;
- if (extraElements > 1)
- {
- spacingCounter += (extraElements - 1) * horzSpacing;
- extraSpacing += (int)spacingCounter;
- }
- if (extraElements > 0)
- rowLayout.AddSpace(elemWidth * extraElements + extraSpacing + paddingRight);
- main.AddFlexibleSpace();
- }
- // Fix bounds as that makes GUI updates faster
- underlay.Bounds = main.Bounds;
- overlay.Bounds = main.Bounds;
- deepUnderlay.Bounds = main.Bounds;
- renameOverlay.Bounds = main.Bounds;
- for (int i = 0; i < entries.Count; i++)
- {
- LibraryGUIEntry guiEntry = entries[i];
- guiEntry.Initialize();
- }
- }
- /// <summary>
- /// Called every frame.
- /// </summary>
- public void Update()
- {
- for (int i = 0; i < entries.Count; i++)
- entries[i].Update();
- }
- /// <summary>
- /// Changes the visual representation of an element at the specified path as being hovered over.
- /// </summary>
- /// <param name="path">Project library path to the element to mark.</param>
- /// <param name="hovered">True if mark as hovered, false to reset to normal.</param>
- public void MarkAsHovered(string path, bool hovered)
- {
- if (!string.IsNullOrEmpty(path))
- {
- LibraryGUIEntry previousUnderCursorElem;
- if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
- previousUnderCursorElem.MarkAsHovered(hovered);
- }
- }
- /// <summary>
- /// Changes the visual representation of an element at the specified path as being pinged.
- /// </summary>
- /// <param name="path">Project library path to the element to mark.</param>
- /// <param name="pinged">True if mark as pinged, false to reset to normal.</param>
- public void MarkAsPinged(string path, bool pinged)
- {
- if (!string.IsNullOrEmpty(path))
- {
- LibraryGUIEntry previousUnderCursorElem;
- if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
- previousUnderCursorElem.MarkAsPinged(pinged);
- }
- }
- /// <summary>
- /// Changes the visual representation of an element at the specified path as being cut.
- /// </summary>
- /// <param name="path">Project library path to the element to mark.</param>
- /// <param name="cut">True if mark as cut, false to reset to normal.</param>
- public void MarkAsCut(string path, bool cut)
- {
- if (!string.IsNullOrEmpty(path))
- {
- LibraryGUIEntry previousUnderCursorElem;
- if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
- previousUnderCursorElem.MarkAsCut(cut);
- }
- }
- /// <summary>
- /// Changes the visual representation of an element at the specified path as being selected.
- /// </summary>
- /// <param name="path">Project library path to the element to mark.</param>
- /// <param name="selected">True if mark as selected, false to reset to normal.</param>
- public void MarkAsSelected(string path, bool selected)
- {
- if (!string.IsNullOrEmpty(path))
- {
- LibraryGUIEntry previousUnderCursorElem;
- if (entryLookup.TryGetValue(path, out previousUnderCursorElem))
- previousUnderCursorElem.MarkAsSelected(selected);
- }
- }
- /// <summary>
- /// Attempts to find a resource tile element at the specified coordinates.
- /// </summary>
- /// <param name="scrollPos">Coordinates relative to the scroll area the content area is part of.</param>
- /// <returns>True if found an entry, false otherwise.</returns>
- public LibraryGUIEntry FindElementAt(Vector2I scrollPos)
- {
- foreach (var element in entries)
- {
- if (element.bounds.Contains(scrollPos))
- return element;
- }
- return null;
- }
- /// <summary>
- /// Attempts to find all resource tile elements overlapping the specified bounds.
- /// </summary>
- /// <param name="scrollBounds">Bounds to check for overlap, specified relative to the scroll area the content area
- /// is part of.</param>
- /// <returns>A list of found entries.</returns>
- public LibraryGUIEntry[] FindElementsOverlapping(Rect2I scrollBounds)
- {
- List<LibraryGUIEntry> elements = new List<LibraryGUIEntry>();
- foreach (var element in entries)
- {
- if (element.Bounds.Overlaps(scrollBounds))
- elements.Add(element);
- }
- return elements.ToArray();
- }
- /// <summary>
- /// Attempts to find a resource tile element with the specified path.
- /// </summary>
- /// <param name="path">Project library path to the element.</param>
- /// <param name="entry">Found element, or null if none found.</param>
- /// <returns>True if an element was found, false otherwise.</returns>
- public bool TryGetEntry(string path, out LibraryGUIEntry entry)
- {
- return entryLookup.TryGetValue(path, out entry);
- }
- /// <summary>
- /// Helper structure containing information about a single entry to display in the library.
- /// </summary>
- private struct ResourceToDisplay
- {
- public ResourceToDisplay(string path, LibraryGUIEntryType type)
- {
- this.path = path;
- this.type = type;
- }
- public string path;
- public LibraryGUIEntryType type;
- }
- }
- /** @} */
- }
|