BsGUIStatusBar.cpp 4.7 KB

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