2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "TextureManager.h"
12 #include "guiinfo/GUIInfoColor.h"
13 #include "utils/ColorUtils.h"
14 #include "utils/Geometry.h"
18 // image alignment for <aspect>keep</aspect>, <aspect>scale</aspect> or <aspect>center</aspect>
19 #define ASPECT_ALIGN_CENTER 0
20 #define ASPECT_ALIGN_LEFT 1
21 #define ASPECT_ALIGN_RIGHT 2
22 #define ASPECT_ALIGNY_CENTER 0
23 #define ASPECT_ALIGNY_TOP 4
24 #define ASPECT_ALIGNY_BOTTOM 8
25 #define ASPECT_ALIGN_MASK 3
26 #define ASPECT_ALIGNY_MASK ~3
31 enum ASPECT_RATIO
{ AR_STRETCH
= 0, AR_SCALE
, AR_KEEP
, AR_CENTER
};
32 CAspectRatio(ASPECT_RATIO aspect
= AR_STRETCH
)
35 align
= ASPECT_ALIGN_CENTER
| ASPECT_ALIGNY_CENTER
;
38 bool operator!=(const CAspectRatio
&right
) const
40 if (ratio
!= right
.ratio
) return true;
41 if (align
!= right
.align
) return true;
42 if (scaleDiffuse
!= right
.scaleDiffuse
) return true;
55 explicit CTextureInfo(const std::string
&file
);
57 CRect border
; // scaled - unneeded if we get rid of scale on load
59 true}; // if false, the main body of a texture is not drawn. useful for borders with no inner filling
60 int orientation
; // orientation of the texture (0 - 7 == EXIForientation - 1)
61 std::string diffuse
; // diffuse overlay texture
62 KODI::GUILIB::GUIINFO::CGUIInfoColor diffuseColor
; // diffuse color
63 std::string filename
; // main texture file
68 using CreateGUITextureFunc
= std::function
<CGUITexture
*(
69 float posX
, float posY
, float width
, float height
, const CTextureInfo
& texture
)>;
70 using DrawQuadFunc
= std::function
<void(const CRect
& coords
,
71 UTILS::COLOR::Color color
,
73 const CRect
* texCoords
,
75 const bool blending
)>;
80 virtual ~CGUITexture() = default;
82 static void Register(const CreateGUITextureFunc
& createFunction
,
83 const DrawQuadFunc
& drawQuadFunction
);
85 static CGUITexture
* CreateTexture(
86 float posX
, float posY
, float width
, float height
, const CTextureInfo
& texture
);
87 virtual CGUITexture
* Clone() const = 0;
89 static void DrawQuad(const CRect
& coords
,
90 UTILS::COLOR::Color color
,
91 CTexture
* texture
= nullptr,
92 const CRect
* texCoords
= nullptr,
93 const float depth
= 1.0,
94 const bool blending
= true);
96 bool Process(unsigned int currentTime
);
97 void Render(int32_t depthOffset
= 0, int32_t overrideDepth
= -1);
99 void DynamicResourceAlloc(bool bOnOff
);
100 bool AllocResources();
101 void FreeResources(bool immediately
= false);
104 bool SetVisible(bool visible
);
105 bool SetAlpha(unsigned char alpha
);
106 bool SetDiffuseColor(UTILS::COLOR::Color color
, const CGUIListItem
* item
= nullptr);
107 bool SetPosition(float x
, float y
);
108 bool SetWidth(float width
);
109 bool SetHeight(float height
);
110 bool SetFileName(const std::string
&filename
);
111 void SetUseCache(const bool useCache
= true);
112 bool SetAspectRatio(const CAspectRatio
&aspect
);
114 const std::string
& GetFileName() const { return m_info
.filename
; }
115 float GetTextureWidth() const { return m_frameWidth
; }
116 float GetTextureHeight() const { return m_frameHeight
; }
117 float GetWidth() const { return m_width
; }
118 float GetHeight() const { return m_height
; }
119 float GetXPosition() const { return m_posX
; }
120 float GetYPosition() const { return m_posY
; }
121 int GetOrientation() const;
122 const CRect
& GetRenderRect() const { return m_vertex
; }
123 bool IsLazyLoaded() const { return m_info
.useLarge
; }
126 * @brief Get the diffuse color (info color) associated to this texture
127 * @return the infocolor associated to this texture
129 KODI::GUILIB::GUIINFO::CGUIInfoColor
GetDiffuseColor() const { return m_info
.diffuseColor
; }
131 bool HitTest(const CPoint
& point
) const
133 return CRect(m_posX
, m_posY
, m_posX
+ m_width
, m_posY
+ m_height
).PtInRect(point
);
135 bool IsAllocated() const { return m_isAllocated
!= NO
; }
136 bool FailedToAlloc() const
138 return m_isAllocated
== NORMAL_FAILED
|| m_isAllocated
== LARGE_FAILED
;
140 bool ReadyToRender() const;
143 CGUITexture(float posX
, float posY
, float width
, float height
, const CTextureInfo
& texture
);
144 CGUITexture(const CGUITexture
& left
);
146 bool CalculateSize();
147 bool AllocateOnDemand();
148 bool UpdateAnimFrame(unsigned int currentTime
);
149 void Render(float left
,
159 static void OrientateTexture(CRect
&rect
, float width
, float height
, int orientation
);
160 void ResetAnimState();
162 // functions that our implementation classes handle
163 virtual void Allocate() {}; ///< called after our textures have been allocated
164 virtual void Free() {}; ///< called after our textures have been freed
165 virtual void Begin(UTILS::COLOR::Color color
) = 0;
166 virtual void Draw(float* x
,
169 const CRect
& texture
,
170 const CRect
& diffuse
,
171 int orientation
) = 0;
172 virtual void End() = 0;
175 UTILS::COLOR::Color m_diffuseColor
;
177 float m_posX
; // size of the frame
183 CRect m_vertex
; // vertex coords to render
184 bool m_invalid
; // if true, we need to recalculate
186 unsigned char m_alpha
;
188 float m_frameWidth
, m_frameHeight
; // size in pixels of the actual frame within the texture
189 float m_texCoordsScaleU
, m_texCoordsScaleV
; // scale factor for pixel->texture coordinates
193 unsigned int m_currentFrame
;
196 float m_diffuseU
, m_diffuseV
; // size of the diffuse frame (in tex coords)
197 float m_diffuseScaleU
, m_diffuseScaleV
; // scale factor of the diffuse frame (from texture coords to diffuse tex coords)
198 CPoint m_diffuseOffset
; // offset into the diffuse frame (it's not always the origin)
200 bool m_allocateDynamically
;
201 enum ALLOCATE_TYPE
{ NO
= 0, NORMAL
, LARGE
, NORMAL_FAILED
, LARGE_FAILED
};
202 ALLOCATE_TYPE m_isAllocated
;
205 CAspectRatio m_aspect
;
207 CTextureArray m_diffuse
;
208 CTextureArray m_texture
;
211 static CreateGUITextureFunc m_createGUITextureFunc
;
212 static DrawQuadFunc m_drawQuadFunc
;