BsGUIStatusBar.cpp 4.7 KB

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