GUILayoutWithBackground.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2019 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup GUI-Editor
  9. * @{
  10. */
  11. /// <summary>
  12. /// Helper class that creates a GUI layout with a texture background.
  13. /// </summary>
  14. public class GUILayoutWithBackground
  15. {
  16. /// <summary>
  17. /// Content layout into which you should add your own GUI elements.
  18. /// </summary>
  19. public GUILayout Layout { get; }
  20. /// <summary>
  21. /// Root panel of the content layout, background panel and any helper backgrounds. Can be used for changing size,
  22. /// destroying or hiding the GUI elements. You should not add or remove GUI elements from the panel.
  23. /// </summary>
  24. public GUIPanel MainPanel { get; }
  25. /// <summary>
  26. /// Constructs a new object.
  27. /// </summary>
  28. /// <param name="mainPanel">Root panel in which all the child GUI elements of this object belong to.</param>
  29. /// <param name="layout">Content layout that's to be provided to the user.</param>
  30. private GUILayoutWithBackground(GUIPanel mainPanel, GUILayout layout)
  31. {
  32. MainPanel = mainPanel;
  33. Layout = layout;
  34. }
  35. /// <summary>
  36. /// Creates a new GUI layout of the specified type, with a texture background.
  37. /// </summary>
  38. /// <typeparam name="T">Type of layout to create.</typeparam>
  39. /// <param name="layout">Parent layout to add the layout to.</param>
  40. /// <param name="background">Texture to display on the background.</param>
  41. /// <param name="backgroundColor">Color to apply to the background texture.</param>
  42. /// <param name="padding">Optional padding to apply between element borders and content.</param>
  43. /// <returns>New GUI layout with background object.</returns>
  44. public static GUILayoutWithBackground Create<T>(GUILayout layout, SpriteTexture background, Color backgroundColor,
  45. RectOffset padding = new RectOffset()) where T : GUILayout, new()
  46. {
  47. GUIPanel mainPanel = layout.AddPanel();
  48. GUILayoutX mainLayout = mainPanel.AddLayoutX();
  49. GUILayout contentLayout;
  50. if (padding.top > 0 || padding.bottom > 0)
  51. {
  52. GUILayoutY paddingVertLayout = mainLayout.AddLayoutY();
  53. if (padding.top > 0)
  54. paddingVertLayout.AddSpace(padding.top);
  55. if (padding.left > 0 || padding.right > 0)
  56. {
  57. GUILayoutX paddingHorzLayout = paddingVertLayout.AddLayoutX();
  58. if (padding.left > 0)
  59. paddingHorzLayout.AddSpace(padding.left);
  60. contentLayout = new T();
  61. paddingHorzLayout.AddElement(contentLayout);
  62. if (padding.right > 0)
  63. paddingHorzLayout.AddSpace(padding.right);
  64. }
  65. else
  66. {
  67. contentLayout = new T();
  68. paddingVertLayout.AddElement(contentLayout);
  69. }
  70. if (padding.bottom > 0)
  71. paddingVertLayout.AddSpace(padding.bottom);
  72. }
  73. else
  74. {
  75. if (padding.left > 0 || padding.right > 0)
  76. {
  77. GUILayoutX paddingHorzLayout = mainLayout.AddLayoutX();
  78. if (padding.left > 0)
  79. paddingHorzLayout.AddSpace(padding.left);
  80. contentLayout = new T();
  81. paddingHorzLayout.AddElement(contentLayout);
  82. if (padding.right > 0)
  83. paddingHorzLayout.AddSpace(padding.right);
  84. }
  85. else
  86. {
  87. contentLayout = new T();
  88. mainLayout.AddElement(contentLayout);
  89. }
  90. }
  91. GUIPanel bgPanel = mainPanel.AddPanel(1);
  92. GUITexture bgTexture = new GUITexture(Builtin.WhiteTexture);
  93. bgTexture.SetTint(backgroundColor);
  94. bgPanel.AddElement(bgTexture);
  95. return new GUILayoutWithBackground(mainPanel, contentLayout);
  96. }
  97. }
  98. /** @} */
  99. }