BsGUIStatusBar.cpp 5.4 KB

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