Line data Source code
1 : /** 2 : Copyright (c) 2021 Roman Katuntsev <sbkarr@stappler.org> 3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 4 : 5 : Permission is hereby granted, free of charge, to any person obtaining a copy 6 : of this software and associated documentation files (the "Software"), to deal 7 : in the Software without restriction, including without limitation the rights 8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 : copies of the Software, and to permit persons to whom the Software is 10 : furnished to do so, subject to the following conditions: 11 : 12 : The above copyright notice and this permission notice shall be included in 13 : all copies or substantial portions of the Software. 14 : 15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 : THE SOFTWARE. 22 : **/ 23 : 24 : #ifndef XENOLITH_RENDERER_BASIC2D_XL2DSPRITE_H_ 25 : #define XENOLITH_RENDERER_BASIC2D_XL2DSPRITE_H_ 26 : 27 : #include "XLResourceCache.h" 28 : #include "XLDynamicStateNode.h" 29 : #include "XL2dVertexArray.h" 30 : #include "XL2dLinearGradient.h" 31 : 32 : namespace STAPPLER_VERSIONIZED stappler::xenolith::basic2d { 33 : 34 : class Sprite : public DynamicStateNode { 35 : public: 36 : using Autofit = font::Autofit; 37 : 38 : static constexpr uint16_t SamplerIndexDefaultFilterNearest = 0; 39 : static constexpr uint16_t SamplerIndexDefaultFilterLinear = 1; 40 : 41 : Sprite(); 42 : virtual ~Sprite(); 43 : 44 : virtual bool init() override; 45 : virtual bool init(StringView); 46 : virtual bool init(Rc<Texture> &&); 47 : 48 : virtual void setTexture(StringView); 49 : virtual void setTexture(Rc<Texture> &&); 50 : const Rc<Texture> &getTexture() const; 51 : 52 : virtual void setLinearGradient(Rc<LinearGradient> &&); 53 : const Rc<LinearGradient> &getLinearGradient() const; 54 : 55 : // texture rect should be normalized 56 : virtual void setTextureRect(const Rect &); 57 0 : virtual const Rect &getTextureRect() const { return _textureRect; } 58 : 59 : virtual bool visitDraw(FrameInfo &, NodeFlags parentFlags) override; 60 : virtual void draw(FrameInfo &, NodeFlags flags) override; 61 : 62 : virtual void onEnter(xenolith::Scene *) override; 63 : virtual void onExit() override; 64 : virtual void onContentSizeDirty() override; 65 : virtual void onTextureLoaded(); 66 : 67 : virtual void setColorMode(const core::ColorMode &); 68 0 : virtual const core::ColorMode &getColorMode() const { return _colorMode; } 69 : 70 : virtual void setBlendInfo(const core::BlendInfo &); 71 2 : virtual const core::BlendInfo &getBlendInfo() const { return _materialInfo.getBlendInfo(); } 72 : 73 : // used for debug purposes only, follow rules from PipelineMaterialInfo.lineWidth: 74 : // 0.0f - draw triangles, < 0.0f - points, > 0.0f - lines with width 75 : // corresponding pipeline should be precompiled 76 : // points and lines are always RenderingLevel::Transparent, when Default level resolves 77 : virtual void setLineWidth(float); 78 2 : virtual float getLineWidth() const { return _materialInfo.getLineWidth(); } 79 : 80 : virtual void setRenderingLevel(RenderingLevel); 81 5716 : virtual RenderingLevel getRenderingLevel() const { return _renderingLevel; } 82 : 83 : virtual void setNormalized(bool); 84 0 : virtual bool isNormalized() const { return _normalized; } 85 : 86 : virtual void setAutofit(Autofit); 87 0 : virtual Autofit getAutofit() const { return _autofit; } 88 : 89 : virtual void setAutofitPosition(const Vec2 &); 90 0 : virtual const Vec2 &getAutofitPosition() const { return _autofitPos; } 91 : 92 : /** Семплеры определяются во время старта цикла графики (gl::Loop) и неизменны в последствии 93 : * По умолчанию, семплер с индексом 0 использует фильтр nearest, 1 - linear. 94 : * Разработчики приложений могут определять свою схему для семплеров, 95 : * но рекомендуем следовать соглашению в отношении 0 и 1 96 : * 97 : * Если семплер с указанным индексом не определён в движке - поведение не определено 98 : */ 99 : virtual void setSamplerIndex(uint16_t); 100 0 : virtual uint16_t getSamplerIndex() const { return _samplerIdx; } 101 : 102 0 : virtual void setCommandFlags(CommandFlags flags) { _commandFlags = flags; } 103 4 : virtual void addCommandFlags(CommandFlags flags) { _commandFlags |= flags; } 104 0 : virtual void removeCommandFlags(CommandFlags flags) { _commandFlags &= ~flags; } 105 0 : virtual CommandFlags getCommandFlags() const { return _commandFlags; } 106 : 107 : virtual void setTextureLoadedCallback(Function<void()> &&); 108 : 109 : protected: 110 : using DynamicStateNode::init; 111 : 112 : virtual void pushShadowCommands(FrameInfo &, NodeFlags flags, const Mat4 &, 113 : SpanView<TransformVertexData> = SpanView<TransformVertexData>()); 114 : virtual void pushCommands(FrameInfo &, NodeFlags flags); 115 : 116 : virtual MaterialInfo getMaterialInfo() const; 117 : virtual Vector<core::MaterialImage> getMaterialImages() const; 118 : virtual bool isMaterialRevokable() const; 119 : virtual void updateColor() override; 120 : virtual void updateVertexesColor(); 121 : virtual void initVertexes(); 122 : virtual void updateVertexes(); 123 : 124 : virtual void updateBlendAndDepth(); 125 : 126 : virtual RenderingLevel getRealRenderingLevel() const; 127 : 128 : static bool getAutofitParams(Autofit autofit, const Vec2 &autofitValue, const Size2 &contentSize, const Size2 &texSize, 129 : Rect &contentRect, Rect &textureRect); 130 : 131 : virtual bool checkVertexDirty() const; 132 : 133 : String _textureName; 134 : Rc<Texture> _texture; 135 : VertexArray _vertexes; 136 : 137 : uint16_t _samplerIdx = 0; 138 : 139 : bool _materialDirty = true; 140 : bool _normalized = false; 141 : bool _vertexesDirty = true; 142 : bool _vertexColorDirty = true; 143 : 144 : bool _flippedX = false; 145 : bool _flippedY = false; 146 : bool _rotated = false; 147 : bool _isTextureLoaded = false; 148 : 149 : Rect _textureRect = Rect(0.0f, 0.0f, 1.0f, 1.0f); // normalized 150 : 151 : Autofit _autofit = Autofit::None; 152 : Vec2 _autofitPos = Vec2(0.5f, 0.5f); 153 : 154 : Vec2 _textureOrigin; 155 : Size2 _textureSize; 156 : Extent3 _targetTextureSize; 157 : 158 : RenderingLevel _renderingLevel = RenderingLevel::Default; 159 : RenderingLevel _realRenderingLevel = RenderingLevel::Default; 160 : uint64_t _materialId = 0; 161 : CommandFlags _commandFlags = CommandFlags::None; 162 : 163 : Color4F _tmpColor; 164 : core::ColorMode _colorMode; 165 : core::BlendInfo _blendInfo; 166 : core::PipelineMaterialInfo _materialInfo; 167 : 168 : Vector<Rc<core::DependencyEvent>> _pendingDependencies; 169 : Function<void()> _textureLoadedCallback; 170 : 171 : Rc<LinearGradient> _linearGradient; 172 : }; 173 : 174 : } 175 : 176 : #endif /* XENOLITH_RENDERER_BASIC2D_XL2DSPRITE_H_ */