Line data Source code
1 : /** 2 : Copyright (c) 2024 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 "XL2dLinearProgress.h" 24 : #include "XL2dLayer.h" 25 : #include "XLAction.h" 26 : 27 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d { 28 : 29 44 : bool LinearProgress::init() { 30 44 : if (!Node::init()) { 31 0 : return false; 32 : } 33 : 34 44 : _line = addChild(Rc<Layer>::create()); 35 44 : _line->setPosition(Vec2(0, 0)); 36 44 : _line->setAnchorPoint(Vec2(0, 0)); 37 : 38 44 : _bar = addChild(Rc<Layer>::create()); 39 44 : _bar->setPosition(Vec2(0, 0)); 40 44 : _bar->setAnchorPoint(Vec2(0, 0)); 41 : 42 44 : setCascadeOpacityEnabled(true); 43 : 44 44 : return true; 45 : } 46 1506 : void LinearProgress::onContentSizeDirty() { 47 1506 : Node::onContentSizeDirty(); 48 1506 : layoutSubviews(); 49 1506 : } 50 : 51 44 : void LinearProgress::onEnter(Scene *scene) { 52 44 : Node::onEnter(scene); 53 : 54 44 : if (_animated) { 55 0 : updateAnimations(); 56 : } 57 44 : } 58 : 59 44 : void LinearProgress::onExit() { 60 44 : stopAllActions(); 61 44 : Node::onExit(); 62 44 : } 63 : 64 798 : void LinearProgress::setAnimated(bool value) { 65 798 : if (_animated != value) { 66 93 : _animated = value; 67 93 : updateAnimations(); 68 : } 69 798 : } 70 1159 : bool LinearProgress::isAnimated() const { 71 1159 : return _animated; 72 : } 73 : 74 2875 : void LinearProgress::setProgress(float value) { 75 2875 : if (_progress != value) { 76 2835 : _progress = value; 77 2835 : _contentSizeDirty = true; 78 : } 79 2875 : } 80 1159 : float LinearProgress::getProgress() const { 81 1159 : return _progress; 82 : } 83 : 84 1506 : void LinearProgress::layoutSubviews() { 85 1506 : _line->setContentSize(_contentSize); 86 1506 : if (!_animated) { 87 710 : auto barSize = Size2(_progress * _contentSize.width, _contentSize.height); 88 710 : _bar->setPosition(Vec2(0, 0)); 89 710 : _bar->setContentSize(barSize); 90 : } else { 91 796 : const float sep = 0.60f; 92 796 : float p = 0.0f; 93 796 : bool invert = false; 94 796 : if (_progress < sep) { 95 791 : p = _progress / sep; 96 : } else { 97 5 : p = (_progress - sep) / (1.0f - sep); 98 5 : invert = true; 99 : } 100 : 101 796 : float start = 0.0f; 102 796 : float end = _contentSize.width; 103 : 104 796 : const float ePos = invert ? 0.15f : 0.45f; 105 796 : const float sPos = invert ? 0.35f : 0.20f; 106 : 107 796 : if (p < (1.0f - ePos)) { 108 791 : end = _contentSize.width * p / (1.0f - ePos); 109 : } 110 : 111 796 : if (p > sPos) { 112 169 : start = _contentSize.width * (p - sPos) / (1.0f - sPos); 113 : } 114 : 115 796 : _bar->setPosition(Vec2(start, 0.0f)); 116 796 : _bar->setContentSize(Size2(end - start, _contentSize.height)); 117 : } 118 1506 : } 119 : 120 93 : void LinearProgress::updateAnimations() { 121 93 : if (_running) { 122 93 : if (_animated) { 123 49 : auto a = Rc<RepeatForever>::create(Rc<ActionProgress>::create(2.0f, 1.0f, [this] (float time) { 124 2126 : setProgress(time); 125 49 : })); 126 49 : a->setTag(2); 127 49 : runAction(a); 128 49 : } else { 129 44 : stopActionByTag(2); 130 : } 131 : } 132 93 : } 133 : 134 44 : void LinearProgress::setLineColor(const Color &c) { 135 44 : _line->setColor(c); 136 44 : } 137 : 138 44 : void LinearProgress::setLineOpacity(float op) { 139 44 : _line->setOpacity(op); 140 44 : } 141 : 142 44 : void LinearProgress::setBarColor(const Color &c) { 143 44 : _bar->setColor(c); 144 44 : } 145 : 146 44 : void LinearProgress::setBarOpacity(float op) { 147 44 : _bar->setOpacity(op); 148 44 : } 149 : 150 : }