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 "MaterialIconSprite.h" 24 : #include "MaterialStyleContainer.h" 25 : #include "XLFrameInfo.h" 26 : #include "XLAction.h" 27 : 28 : namespace STAPPLER_VERSIONIZED stappler::xenolith::material2d { 29 : 30 436 : bool IconSprite::init(IconName icon) { 31 436 : if (!VectorSprite::init(Size2(24.0f, 24.0f))) { 32 0 : return false; 33 : } 34 : 35 436 : setContentSize(Size2(24.0f, 24.0f)); 36 : 37 436 : _iconName = icon; 38 : 39 436 : if (_iconName != IconName::None) { 40 44 : updateIcon(); 41 : } 42 : 43 436 : return true; 44 : } 45 : 46 246 : void IconSprite::setIconName(IconName name) { 47 246 : if (_iconName != name) { 48 246 : _iconName = name; 49 246 : updateIcon(); 50 : } 51 246 : } 52 : 53 176 : void IconSprite::setProgress(float pr) { 54 176 : if (_progress != pr) { 55 0 : _progress = pr; 56 0 : updateIcon(); 57 : } 58 176 : } 59 : 60 0 : float IconSprite::getProgress() const { 61 0 : return _progress; 62 : } 63 : 64 50 : void IconSprite::setBlendColor(ColorRole rule, float value) { 65 50 : if (_blendColorRule != rule || _blendValue != value) { 66 30 : _blendColorRule = rule; 67 30 : _blendValue = value; 68 : } 69 50 : } 70 : 71 20 : void IconSprite::setBlendColor(const Color4F &c, float value) { 72 20 : setBlendColor(ColorRole::Undefined, 0.0f); 73 20 : _blendColor = c; 74 20 : _blendValue = value; 75 20 : } 76 : 77 0 : void IconSprite::setPreserveOpacity(bool value) { 78 0 : _preserveOpacity = value; 79 0 : } 80 : 81 0 : bool IconSprite::isPreserveOpacity() const { 82 0 : return _preserveOpacity; 83 : } 84 : 85 4584 : bool IconSprite::visitDraw(FrameInfo &frame, NodeFlags parentFlags) { 86 4584 : if (!_visible) { 87 2180 : return false; 88 : } 89 : 90 2404 : auto style = frame.getComponent<SurfaceInterior>(SurfaceInterior::ComponentFrameTag); 91 2404 : auto styleContainer = frame.getComponent<StyleContainer>(StyleContainer::ComponentFrameTag); 92 2404 : if (style) { 93 2404 : auto &s = style->getStyle(); 94 : 95 2404 : if (styleContainer) { 96 2404 : if (auto scheme = styleContainer->getScheme(s.schemeTag)) { 97 2404 : if (_blendValue > 0.0f && _blendColorRule != ColorRole::Undefined) { 98 0 : auto c = scheme->get(_blendColorRule); 99 0 : if (c != _blendColor) { 100 0 : _blendColor = c; 101 : } 102 : } 103 : } 104 : } 105 : 106 2404 : auto color = s.colorOn.asColor4F(); 107 2404 : if (_blendValue > 0.0f) { 108 0 : color = color * (1.0f - _blendValue) + _blendColor * _blendValue; 109 : } 110 : 111 2404 : if (color != getColor()) { 112 486 : setColor(color, !_preserveOpacity); 113 : } 114 : } 115 : 116 2404 : return VectorSprite::visitDraw(frame, parentFlags); 117 2404 : } 118 : 119 10 : void IconSprite::animate() { 120 : // TODO 121 10 : } 122 : 123 0 : void IconSprite::animate(float targetProgress, float duration) { 124 0 : stopAllActionsByTag("IconSprite::animate"_tag); 125 0 : runAction(Rc<ActionProgress>::create(duration, _progress, targetProgress, [this] (float value) { 126 0 : setProgress(value); 127 0 : }), "IconSprite::animate"_tag); 128 0 : } 129 : 130 290 : void IconSprite::updateIcon() { 131 290 : _image->clear(); 132 290 : drawIcon(*_image, _iconName, _progress); 133 290 : } 134 : 135 : }