1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "GUI/BsGUIProgressBar.h"
4#include "GUI/BsGUIWidget.h"
5#include "GUI/BsGUISkin.h"
6#include "GUI/BsGUITexture.h"
7#include "GUI/BsGUIDimensions.h"
8
9using namespace std::placeholders;
10
11namespace bs
12{
13 GUIProgressBar::GUIProgressBar(const String& styleName, const GUIDimensions& dimensions)
14 :GUIElementContainer(dimensions, styleName), mPercent(0)
15 {
16 mBar = GUITexture::create(getSubStyleName(getBarStyleType()));
17 mBackground = GUITexture::create(getSubStyleName(getBackgroundStyleType()));
18
19 mBackground->_setElementDepth(mBar->_getRenderElementDepthRange());
20
21 _registerChildElement(mBar);
22 _registerChildElement(mBackground);
23 }
24
25 const String& GUIProgressBar::getBarStyleType()
26 {
27 static String HANDLE_STYLE_TYPE = "ProgressBarFill";
28 return HANDLE_STYLE_TYPE;
29 }
30
31 const String& GUIProgressBar::getBackgroundStyleType()
32 {
33 static String BACKGROUND_STYLE_TYPE = "ProgressBarBackground";
34 return BACKGROUND_STYLE_TYPE;
35 }
36
37 Vector2I GUIProgressBar::_getOptimalSize() const
38 {
39 Vector2I optimalSize = mBar->_getOptimalSize();
40
41 Vector2I backgroundSize = mBackground->_getOptimalSize();
42 optimalSize.x = std::max(optimalSize.x, backgroundSize.x);
43 optimalSize.y = std::max(optimalSize.y, backgroundSize.y);
44
45 return optimalSize;
46 }
47
48 void GUIProgressBar::_updateLayoutInternal(const GUILayoutData& data)
49 {
50 mBackground->_setLayoutData(data);
51
52 const GUIElementStyle* style = _getStyle();
53
54 GUILayoutData barLayoutData = data;
55
56 barLayoutData.area.x += style->margins.left;
57 barLayoutData.area.y += style->margins.top;
58
59 UINT32 maxProgressBarWidth = std::max((UINT32)0, (UINT32)(data.area.width - style->margins.left - style->margins.right));
60 UINT32 progressBarHeight = std::max((UINT32)0, (UINT32)(data.area.height - style->margins.top - style->margins.bottom));
61
62 barLayoutData.area.width = (UINT32)Math::floorToInt(maxProgressBarWidth * mPercent);
63 barLayoutData.area.height = progressBarHeight;
64
65 mBar->_setLayoutData(barLayoutData);
66 }
67
68 void GUIProgressBar::styleUpdated()
69 {
70 mBar->setStyle(getSubStyleName(getBarStyleType()));
71 mBackground->setStyle(getSubStyleName(getBackgroundStyleType()));
72 }
73
74 void GUIProgressBar::setPercent(float pct)
75 {
76 mPercent = pct;
77 _markLayoutAsDirty();
78 }
79
80 void GUIProgressBar::setTint(const Color& color)
81 {
82 mBar->setTint(color);
83 mBackground->setTint(color);
84 }
85
86 GUIProgressBar* GUIProgressBar::create(const String& styleName)
87 {
88 return new (bs_alloc<GUIProgressBar>()) GUIProgressBar(getStyleName<GUIProgressBar>(styleName), GUIDimensions::create());
89 }
90
91 GUIProgressBar* GUIProgressBar::create(const GUIOptions& options, const String& styleName)
92 {
93 return new (bs_alloc<GUIProgressBar>()) GUIProgressBar(getStyleName<GUIProgressBar>(styleName), GUIDimensions::create(options));
94 }
95
96 const String& GUIProgressBar::getGUITypeName()
97 {
98 static String typeName = "ProgressBar";
99 return typeName;
100 }
101}