BsGUIStatusBar.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(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<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. WString content = L"Project: " + name;
  65. if (modified)
  66. content += L"*";
  67. mProject->setContent(HString(content));
  68. }
  69. void GUIStatusBar::setScene(const WString& name, bool modified)
  70. {
  71. WString content = L"Scene: " + name;
  72. if (modified)
  73. content += L"*";
  74. mScene->setContent(HString(content));
  75. }
  76. void GUIStatusBar::setTint(const Color& color)
  77. {
  78. mBackground->setTint(color);
  79. mMessage->setTint(color);
  80. }
  81. void GUIStatusBar::_updateLayoutInternal(const GUILayoutData& data)
  82. {
  83. mPanel->_setLayoutData(data);
  84. mPanel->_updateLayoutInternal(data);
  85. mBgPanel->_setLayoutData(data);
  86. mBgPanel->_updateLayoutInternal(data);
  87. }
  88. Vector2I GUIStatusBar::_getOptimalSize() const
  89. {
  90. return mBgPanel->_getOptimalSize();
  91. }
  92. void GUIStatusBar::styleUpdated()
  93. {
  94. mBackground->setStyle(getSubStyleName(getGUIBackgroundTypeName()));
  95. mMessage->setStyle(getSubStyleName(getGUIMessageTypeName()));
  96. }
  97. void GUIStatusBar::logEntryAdded(const LogEntry& entry)
  98. {
  99. HSpriteTexture iconTexture;
  100. Color textColor = COLOR_INFO;
  101. UINT32 logChannel = entry.getChannel();
  102. switch (logChannel)
  103. {
  104. case (UINT32)DebugChannel::Debug:
  105. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Info);
  106. break;
  107. case (UINT32)DebugChannel::Warning:
  108. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Warning);
  109. textColor = COLOR_WARNING;
  110. break;
  111. case (UINT32)DebugChannel::Error:
  112. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Error);
  113. textColor = COLOR_ERROR;
  114. break;
  115. }
  116. WString message = toWString(entry.getMessage());
  117. WString::size_type lfPos = message.find_first_of('\n');
  118. WString::size_type crPos = message.find_first_of('\r');
  119. WString::size_type newlinePos;
  120. if (lfPos >= 0)
  121. {
  122. if (crPos >= 0)
  123. newlinePos = std::min(lfPos, crPos);
  124. else
  125. newlinePos = lfPos;
  126. }
  127. else if (crPos >= 0)
  128. newlinePos = crPos;
  129. else
  130. newlinePos = -1;
  131. if (newlinePos == -1)
  132. {
  133. GUIContent messageContent(HString(message), iconTexture);
  134. mMessage->setContent(messageContent);
  135. mMessage->setTint(textColor);
  136. }
  137. else
  138. {
  139. WString firstLine = message.substr(0, newlinePos);
  140. GUIContent messageContent(HString(firstLine), iconTexture);
  141. mMessage->setContent(messageContent);
  142. mMessage->setTint(textColor);
  143. }
  144. }
  145. void GUIStatusBar::messageBtnClicked()
  146. {
  147. onMessageClicked();
  148. }
  149. const String& GUIStatusBar::getGUITypeName()
  150. {
  151. static String TypeName = "GUIStatusBar";
  152. return TypeName;
  153. }
  154. const String& GUIStatusBar::getGUIBackgroundTypeName()
  155. {
  156. static String TypeName = "GUIStatusBarBg";
  157. return TypeName;
  158. }
  159. const String& GUIStatusBar::getGUIMessageTypeName()
  160. {
  161. static String TypeName = "GUIStatusBarMessage";
  162. return TypeName;
  163. }
  164. }