BsGUIStatusBar.cpp 6.6 KB

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