BsGUIStatusBar.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "GUI/BsGUIStatusBar.h"
  4. #include "GUI/BsGUILayoutX.h"
  5. #include "GUI/BsGUILayoutY.h"
  6. #include "GUI/BsGUILabel.h"
  7. #include "GUI/BsGUIButton.h"
  8. #include "GUI/BsGUITexture.h"
  9. #include "GUI/BsGUIPanel.h"
  10. #include "GUI/BsGUISpace.h"
  11. #include "Debug/BsDebug.h"
  12. #include "Utility/BsBuiltinEditorResources.h"
  13. using namespace std::placeholders;
  14. namespace bs
  15. {
  16. const Color GUIStatusBar::COLOR_INFO = Color(0.7f, 0.7f, 0.7f);
  17. const Color GUIStatusBar::COLOR_WARNING = Color(192 / 255.0f, 176 / 255.0f, 0.0f);
  18. const Color GUIStatusBar::COLOR_ERROR = Color(192 / 255.0f, 36 / 255.0f, 0.0f);
  19. GUIStatusBar::GUIStatusBar(const PrivatelyConstruct& dummy,
  20. const String& style, const GUIDimensions& dimensions)
  21. :GUIElementContainer(dimensions, style)
  22. {
  23. mPanel = GUIPanel::create();
  24. mBgPanel = GUIPanel::create(1);
  25. _registerChildElement(mPanel);
  26. _registerChildElement(mBgPanel);
  27. mBackground = GUITexture::create(GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getGUIBackgroundTypeName()));
  28. mMessage = GUIButton::create(HString(L""), GUIOptions(GUIOption::flexibleWidth()), getSubStyleName(getGUIMessageTypeName()));
  29. mScene = GUILabel::create(HString(L"Scene: Unnamed"), GUIOptions(GUIOption::fixedWidth(150)));
  30. mProject = GUILabel::create(HString(L"Project: None"), GUIOptions(GUIOption::fixedWidth(200)));
  31. mCompiling = GUILabel::create(HString(L"Compiling..."), GUIOptions(GUIOption::fixedWidth(100)));
  32. GUILayoutY* vertLayout = mPanel->addNewElement<GUILayoutY>();
  33. vertLayout->addNewElement<GUIFixedSpace>(3);
  34. GUILayoutX* horzLayout = vertLayout->addNewElement<GUILayoutX>();
  35. horzLayout->addNewElement<GUIFixedSpace>(10);
  36. horzLayout->addElement(mMessage);
  37. horzLayout->addNewElement<GUIFlexibleSpace>();
  38. horzLayout->addElement(mScene);
  39. horzLayout->addNewElement<GUIFixedSpace>(10);
  40. horzLayout->addElement(mProject);
  41. horzLayout->addNewElement<GUIFixedSpace>(10);
  42. horzLayout->addElement(mCompiling);
  43. horzLayout->addNewElement<GUIFixedSpace>(10);
  44. mBgPanel->addElement(mBackground);
  45. mCompiling->setActive(false);
  46. mLogEntryAddedConn = gDebug().onLogModified.connect(std::bind(&GUIStatusBar::logModified, this));
  47. mMessageBtnPressedConn = mMessage->onClick.connect(std::bind(&GUIStatusBar::messageBtnClicked, this));
  48. }
  49. GUIStatusBar::~GUIStatusBar()
  50. {
  51. mLogEntryAddedConn.disconnect();
  52. mMessageBtnPressedConn.disconnect();
  53. }
  54. GUIStatusBar* GUIStatusBar::create(const GUIOptions& options, const String& style)
  55. {
  56. const String* curStyle = &style;
  57. if (*curStyle == StringUtil::BLANK)
  58. curStyle = &getGUITypeName();
  59. return bs_new<GUIStatusBar>(PrivatelyConstruct(), *curStyle, GUIDimensions::create(options));
  60. }
  61. GUIStatusBar* GUIStatusBar::create(const String& style)
  62. {
  63. const String* curStyle = &style;
  64. if (*curStyle == StringUtil::BLANK)
  65. curStyle = &getGUITypeName();
  66. return bs_new<GUIStatusBar>(PrivatelyConstruct(), *curStyle, GUIDimensions::create());
  67. }
  68. void GUIStatusBar::setProject(const WString& name, bool modified)
  69. {
  70. WStringStream content;
  71. content << L"Project: ";
  72. if (name.size() > 20)
  73. content << name.substr(0, 20) << L"...";
  74. else
  75. content << name;
  76. if (modified)
  77. content << L"*";
  78. mProject->setContent(HString(content.str()));
  79. }
  80. void GUIStatusBar::setScene(const WString& name, bool modified)
  81. {
  82. WStringStream content;
  83. content << L"Scene: ";
  84. if (name.size() > 15)
  85. content << name.substr(0, 15) << L"...";
  86. else
  87. content << name;
  88. if (modified)
  89. content << L"*";
  90. mScene->setContent(HString(content.str()));
  91. }
  92. void GUIStatusBar::setIsCompiling(bool compiling)
  93. {
  94. mCompiling->setActive(compiling);
  95. }
  96. void GUIStatusBar::setTint(const Color& color)
  97. {
  98. mBackground->setTint(color);
  99. mMessage->setTint(color);
  100. }
  101. void GUIStatusBar::_updateLayoutInternal(const GUILayoutData& data)
  102. {
  103. mPanel->_setLayoutData(data);
  104. mPanel->_updateLayoutInternal(data);
  105. mBgPanel->_setLayoutData(data);
  106. mBgPanel->_updateLayoutInternal(data);
  107. }
  108. Vector2I GUIStatusBar::_getOptimalSize() const
  109. {
  110. return mBgPanel->_getOptimalSize();
  111. }
  112. void GUIStatusBar::styleUpdated()
  113. {
  114. mBackground->setStyle(getSubStyleName(getGUIBackgroundTypeName()));
  115. mMessage->setStyle(getSubStyleName(getGUIMessageTypeName()));
  116. }
  117. void GUIStatusBar::logModified()
  118. {
  119. LogEntry entry;
  120. if(!gDebug().getLog().getLastEntry(entry))
  121. {
  122. GUIContent messageContent(HString(L""));
  123. mMessage->setContent(messageContent);
  124. return;
  125. }
  126. HSpriteTexture iconTexture;
  127. Color textColor = COLOR_INFO;
  128. UINT32 logChannel = entry.getChannel();
  129. switch (logChannel)
  130. {
  131. case (UINT32)DebugChannel::Debug:
  132. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Info, 16, false);
  133. break;
  134. case (UINT32)DebugChannel::Warning:
  135. case (UINT32)DebugChannel::CompilerWarning:
  136. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Warning, 16, false);
  137. textColor = COLOR_WARNING;
  138. break;
  139. case (UINT32)DebugChannel::Error:
  140. case (UINT32)DebugChannel::CompilerError:
  141. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Error, 16, false);
  142. textColor = COLOR_ERROR;
  143. break;
  144. }
  145. WString message = toWString(entry.getMessage());
  146. size_t lfPos = message.find_first_of('\n');
  147. size_t crPos = message.find_first_of('\r');
  148. size_t newlinePos;
  149. if (lfPos != WString::npos)
  150. {
  151. if (crPos != WString::npos)
  152. newlinePos = std::min(lfPos, crPos);
  153. else
  154. newlinePos = lfPos;
  155. }
  156. else if (crPos != WString::npos)
  157. newlinePos = crPos;
  158. else
  159. newlinePos = -1;
  160. if (newlinePos == -1)
  161. {
  162. GUIContent messageContent(HString(message), iconTexture);
  163. mMessage->setContent(messageContent);
  164. mMessage->setTint(textColor);
  165. }
  166. else
  167. {
  168. WString firstLine = message.substr(0, newlinePos);
  169. GUIContent messageContent(HString(firstLine), iconTexture);
  170. mMessage->setContent(messageContent);
  171. mMessage->setTint(textColor);
  172. }
  173. }
  174. void GUIStatusBar::messageBtnClicked()
  175. {
  176. onMessageClicked();
  177. }
  178. const String& GUIStatusBar::getGUITypeName()
  179. {
  180. static String TypeName = "GUIStatusBar";
  181. return TypeName;
  182. }
  183. const String& GUIStatusBar::getGUIBackgroundTypeName()
  184. {
  185. static String TypeName = "GUIStatusBarBg";
  186. return TypeName;
  187. }
  188. const String& GUIStatusBar::getGUIMessageTypeName()
  189. {
  190. static String TypeName = "GUIStatusBarMessage";
  191. return TypeName;
  192. }
  193. }