BsGUIStatusBar.cpp 5.7 KB

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