BsGUIStatusBar.cpp 6.5 KB

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