Line data Source code
1 : /** 2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #include "XL2dRoundedProgress.h" 24 : #include "XLAction.h" 25 : 26 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d { 27 : 28 88 : RoundedProgress::~RoundedProgress() { } 29 : 30 44 : bool RoundedProgress::init(Layout l) { 31 44 : if (!LayerRounded::init(Color::Grey_500, 0.0f)) { 32 0 : return false; 33 : } 34 : 35 44 : setOpacity(1.0f); 36 : 37 44 : _layout = l; 38 44 : setCascadeOpacityEnabled(true); 39 : 40 44 : _bar = addChild(Rc<LayerRounded>::create(Color::Black, 0.0f), ZOrder(1)); 41 44 : _bar->setPosition(Vec2(0, 0)); 42 44 : _bar->setAnchorPoint(Vec2(0, 0)); 43 44 : _bar->setOpacity(1.0f); 44 : 45 44 : return true; 46 : } 47 : 48 1410 : void RoundedProgress::setLayout(Layout l) { 49 1410 : if (_layout != l) { 50 88 : _layout = l; 51 88 : _contentSizeDirty = true; 52 : } 53 1410 : } 54 : 55 1410 : RoundedProgress::Layout RoundedProgress::getLayout() const { 56 1410 : return _layout; 57 : } 58 : 59 1410 : void RoundedProgress::setInverted(bool value) { 60 1410 : if (_inverted != value) { 61 44 : _inverted = value; 62 44 : _contentSizeDirty = true; 63 : } 64 1410 : } 65 : 66 1410 : bool RoundedProgress::isInverted() const { 67 1410 : return _inverted; 68 : } 69 : 70 1546 : void RoundedProgress::onContentSizeDirty() { 71 1546 : LayerRounded::onContentSizeDirty(); 72 : 73 1546 : auto l = _layout; 74 1546 : if (l == Auto) { 75 487 : if (_contentSize.width > _contentSize.height) { 76 487 : l = Horizontal; 77 : } else { 78 0 : l = Vertical; 79 : } 80 : } 81 : 82 1546 : if (l == Horizontal) { 83 1109 : float width = _contentSize.width * _barScale; 84 1109 : if (width < _contentSize.height) { 85 0 : width = _contentSize.height; 86 : } 87 1109 : if (width > _contentSize.width) { 88 0 : width = _contentSize.width; 89 : } 90 : 91 1109 : float diff = _contentSize.width - width; 92 : 93 1109 : _bar->setContentSize(Size2(width, _contentSize.height)); 94 1109 : _bar->setPosition(Vec2(diff * (_inverted?(1.0f - _progress):_progress), 0)); 95 : } else { 96 437 : float height = _contentSize.height * _barScale; 97 437 : if (height < _contentSize.width) { 98 437 : height = _contentSize.width; 99 : } 100 437 : if (height > _contentSize.height) { 101 437 : height = _contentSize.height; 102 : } 103 : 104 437 : float diff = _contentSize.height - height; 105 : 106 437 : _bar->setContentSize(Size2(_contentSize.width, height)); 107 437 : _bar->setPosition(Vec2(0.0f, diff * (_inverted?(1.0f - _progress):_progress))); 108 : } 109 1546 : } 110 : 111 36 : void RoundedProgress::setBorderRadius(float value) { 112 36 : LayerRounded::setBorderRadius(value); 113 36 : _bar->setBorderRadius(value); 114 36 : } 115 : 116 1410 : void RoundedProgress::setProgress(float value, float anim) { 117 1410 : if (value < 0.0f) { 118 0 : value = 0.0f; 119 1410 : } else if (value > 1.0f) { 120 0 : value = 1.0f; 121 : } 122 1410 : if (_progress != value) { 123 1366 : if (!_actionManager || anim == 0.0f) { 124 617 : _progress = value; 125 617 : _contentSizeDirty = true; 126 : } else { 127 749 : stopActionByTag(129); 128 2311 : auto a = Rc<ActionProgress>::create(anim, value, [this] (float time) { 129 2311 : _progress = time; 130 2311 : _contentSizeDirty = true; 131 749 : }); 132 749 : a->setSourceProgress(_progress); 133 749 : a->setTag(129); 134 749 : runAction(a); 135 749 : } 136 : } 137 1410 : } 138 : 139 1159 : float RoundedProgress::getProgress() const { 140 1159 : return _progress; 141 : } 142 : 143 1410 : void RoundedProgress::setBarScale(float value) { 144 1410 : if (_barScale != value) { 145 1410 : _barScale = value; 146 1410 : _contentSizeDirty = true; 147 : } 148 1410 : } 149 : 150 1159 : float RoundedProgress::getBarScale() const { 151 1159 : return _barScale; 152 : } 153 : 154 44 : void RoundedProgress::setLineColor(const Color4F &c) { 155 44 : setColor(c); 156 44 : } 157 : 158 44 : void RoundedProgress::setLineOpacity(float o) { 159 44 : setOpacity(o); 160 44 : } 161 : 162 44 : void RoundedProgress::setBarColor(const Color4F &c) { 163 44 : _bar->setColor(c); 164 44 : } 165 : 166 44 : void RoundedProgress::setBarOpacity(float o) { 167 44 : _bar->setOpacity(o); 168 44 : } 169 : 170 : }