BsGUIStatusBar.cpp 6.5 KB

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