BsGUIStatusBar.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(1.0f, 1.0f, 1.0f, 1.0f);
  15. const Color GUIStatusBar::COLOR_WARNING = Color(192 / 255.0f, 176 / 255.0f, 0.0f, 1.0f);
  16. const Color GUIStatusBar::COLOR_ERROR = Color(192 / 255.0f, 36 / 255.0f, 0.0f, 1.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().onLogEntryAdded.connect(std::bind(&GUIStatusBar::logEntryAdded, this, _1));
  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::logEntryAdded(const LogEntry& entry)
  116. {
  117. HSpriteTexture iconTexture;
  118. Color textColor = COLOR_INFO;
  119. UINT32 logChannel = entry.getChannel();
  120. switch (logChannel)
  121. {
  122. case (UINT32)DebugChannel::Debug:
  123. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Info, 16);
  124. break;
  125. case (UINT32)DebugChannel::Warning:
  126. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Warning, 16);
  127. textColor = COLOR_WARNING;
  128. break;
  129. case (UINT32)DebugChannel::Error:
  130. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Error, 16);
  131. textColor = COLOR_ERROR;
  132. break;
  133. }
  134. WString message = toWString(entry.getMessage());
  135. WString::size_type lfPos = message.find_first_of('\n');
  136. WString::size_type crPos = message.find_first_of('\r');
  137. WString::size_type newlinePos;
  138. if (lfPos >= 0)
  139. {
  140. if (crPos >= 0)
  141. newlinePos = std::min(lfPos, crPos);
  142. else
  143. newlinePos = lfPos;
  144. }
  145. else if (crPos >= 0)
  146. newlinePos = crPos;
  147. else
  148. newlinePos = -1;
  149. if (newlinePos == -1)
  150. {
  151. GUIContent messageContent(HString(message), iconTexture);
  152. mMessage->setContent(messageContent);
  153. mMessage->setTint(textColor);
  154. }
  155. else
  156. {
  157. WString firstLine = message.substr(0, newlinePos);
  158. GUIContent messageContent(HString(firstLine), iconTexture);
  159. mMessage->setContent(messageContent);
  160. mMessage->setTint(textColor);
  161. }
  162. }
  163. void GUIStatusBar::messageBtnClicked()
  164. {
  165. onMessageClicked();
  166. }
  167. const String& GUIStatusBar::getGUITypeName()
  168. {
  169. static String TypeName = "GUIStatusBar";
  170. return TypeName;
  171. }
  172. const String& GUIStatusBar::getGUIBackgroundTypeName()
  173. {
  174. static String TypeName = "GUIStatusBarBg";
  175. return TypeName;
  176. }
  177. const String& GUIStatusBar::getGUIMessageTypeName()
  178. {
  179. static String TypeName = "GUIStatusBarMessage";
  180. return TypeName;
  181. }
  182. }