BsGUIStatusBar.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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::Info:
  85. case (UINT32)DebugChannel::Debug:
  86. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Info);
  87. break;
  88. case (UINT32)DebugChannel::Warning:
  89. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Warning);
  90. textColor = COLOR_WARNING;
  91. break;
  92. case (UINT32)DebugChannel::Error:
  93. iconTexture = BuiltinEditorResources::instance().getLogMessageIcon(LogMessageIcon::Error);
  94. textColor = COLOR_ERROR;
  95. break;
  96. }
  97. WString message = toWString(entry.getMessage());
  98. WString::size_type lfPos = message.find_first_of('\n');
  99. WString::size_type crPos = message.find_first_of('\r');
  100. WString::size_type newlinePos;
  101. if (lfPos >= 0)
  102. {
  103. if (crPos >= 0)
  104. newlinePos = std::min(lfPos, crPos);
  105. else
  106. newlinePos = lfPos;
  107. }
  108. else if (crPos >= 0)
  109. newlinePos = crPos;
  110. else
  111. newlinePos = -1;
  112. if (newlinePos == -1)
  113. {
  114. GUIContent messageContent(HString(message), iconTexture);
  115. mMessage->setContent(messageContent);
  116. mMessage->setTint(textColor);
  117. }
  118. else
  119. {
  120. WString firstLine = message.substr(0, newlinePos);
  121. GUIContent messageContent(HString(firstLine), iconTexture);
  122. mMessage->setContent(messageContent);
  123. mMessage->setTint(textColor);
  124. }
  125. }
  126. void GUIStatusBar::messageBtnClicked()
  127. {
  128. onMessageClicked();
  129. }
  130. const String& GUIStatusBar::getGUITypeName()
  131. {
  132. static String TypeName = "GUIStatusBar";
  133. return TypeName;
  134. }
  135. const String& GUIStatusBar::getGUIBackgroundTypeName()
  136. {
  137. static String TypeName = "GUIStatusBarBg";
  138. return TypeName;
  139. }
  140. const String& GUIStatusBar::getGUIMessageTypeName()
  141. {
  142. static String TypeName = "GUIStatusBarMessage";
  143. return TypeName;
  144. }
  145. }