xbmc
GUITexture.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "TextureManager.h"
12 #include "guiinfo/GUIInfoColor.h"
13 #include "utils/ColorUtils.h"
14 #include "utils/Geometry.h"
15 
16 #include <functional>
17 
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
27 
29 {
30 public:
31  enum ASPECT_RATIO { AR_STRETCH = 0, AR_SCALE, AR_KEEP, AR_CENTER };
32  CAspectRatio(ASPECT_RATIO aspect = AR_STRETCH)
33  {
34  ratio = aspect;
35  align = ASPECT_ALIGN_CENTER | ASPECT_ALIGNY_CENTER;
36  scaleDiffuse = true;
37  };
38  bool operator!=(const CAspectRatio &right) const
39  {
40  if (ratio != right.ratio) return true;
41  if (align != right.align) return true;
42  if (scaleDiffuse != right.scaleDiffuse) return true;
43  return false;
44  };
45 
46  ASPECT_RATIO ratio;
47  uint32_t align;
48  bool scaleDiffuse;
49 };
50 
52 {
53 public:
54  CTextureInfo();
55  explicit CTextureInfo(const std::string &file);
56  bool useLarge;
57  CRect border; // scaled - unneeded if we get rid of scale on load
58  bool m_infill{
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
64 };
65 
66 class CGUITexture;
67 
68 using CreateGUITextureFunc = std::function<CGUITexture*(
69  float posX, float posY, float width, float height, const CTextureInfo& texture)>;
70 using DrawQuadFunc = std::function<void(
71  const CRect& coords, UTILS::COLOR::Color color, CTexture* texture, const CRect* texCoords)>;
72 
74 {
75 public:
76  virtual ~CGUITexture() = default;
77 
78  static void Register(const CreateGUITextureFunc& createFunction,
79  const DrawQuadFunc& drawQuadFunction);
80 
81  static CGUITexture* CreateTexture(
82  float posX, float posY, float width, float height, const CTextureInfo& texture);
83  virtual CGUITexture* Clone() const = 0;
84 
85  static void DrawQuad(const CRect& coords,
86  UTILS::COLOR::Color color,
87  CTexture* texture = nullptr,
88  const CRect* texCoords = nullptr);
89 
90  bool Process(unsigned int currentTime);
91  void Render();
92 
93  void DynamicResourceAlloc(bool bOnOff);
94  bool AllocResources();
95  void FreeResources(bool immediately = false);
96  void SetInvalid();
97 
98  bool SetVisible(bool visible);
99  bool SetAlpha(unsigned char alpha);
100  bool SetDiffuseColor(UTILS::COLOR::Color color, const CGUIListItem* item = nullptr);
101  bool SetPosition(float x, float y);
102  bool SetWidth(float width);
103  bool SetHeight(float height);
104  bool SetFileName(const std::string &filename);
105  void SetUseCache(const bool useCache = true);
106  bool SetAspectRatio(const CAspectRatio &aspect);
107 
108  const std::string& GetFileName() const { return m_info.filename; }
109  float GetTextureWidth() const { return m_frameWidth; }
110  float GetTextureHeight() const { return m_frameHeight; }
111  float GetWidth() const { return m_width; }
112  float GetHeight() const { return m_height; }
113  float GetXPosition() const { return m_posX; }
114  float GetYPosition() const { return m_posY; }
115  int GetOrientation() const;
116  const CRect& GetRenderRect() const { return m_vertex; }
117  bool IsLazyLoaded() const { return m_info.useLarge; }
118 
123  KODI::GUILIB::GUIINFO::CGUIInfoColor GetDiffuseColor() const { return m_info.diffuseColor; }
124 
125  bool HitTest(const CPoint& point) const
126  {
127  return CRect(m_posX, m_posY, m_posX + m_width, m_posY + m_height).PtInRect(point);
128  }
129  bool IsAllocated() const { return m_isAllocated != NO; }
130  bool FailedToAlloc() const
131  {
132  return m_isAllocated == NORMAL_FAILED || m_isAllocated == LARGE_FAILED;
133  }
134  bool ReadyToRender() const;
135 
136 protected:
137  CGUITexture(float posX, float posY, float width, float height, const CTextureInfo& texture);
138  CGUITexture(const CGUITexture& left);
139 
140  bool CalculateSize();
141  bool AllocateOnDemand();
142  bool UpdateAnimFrame(unsigned int currentTime);
143  void Render(float left,
144  float top,
145  float right,
146  float bottom,
147  float u1,
148  float v1,
149  float u2,
150  float v2,
151  float u3,
152  float v3);
153  static void OrientateTexture(CRect &rect, float width, float height, int orientation);
154  void ResetAnimState();
155 
156  // functions that our implementation classes handle
157  virtual void Allocate() {};
158  virtual void Free() {};
159  virtual void Begin(UTILS::COLOR::Color color) = 0;
160  virtual void Draw(float* x,
161  float* y,
162  float* z,
163  const CRect& texture,
164  const CRect& diffuse,
165  int orientation) = 0;
166  virtual void End() = 0;
167 
168  bool m_visible;
169  UTILS::COLOR::Color m_diffuseColor;
170 
171  float m_posX; // size of the frame
172  float m_posY;
173  float m_width;
174  float m_height;
175 
176  CRect m_vertex; // vertex coords to render
177  bool m_invalid; // if true, we need to recalculate
178  bool m_use_cache;
179  unsigned char m_alpha;
180 
181  float m_frameWidth, m_frameHeight; // size in pixels of the actual frame within the texture
182  float m_texCoordsScaleU, m_texCoordsScaleV; // scale factor for pixel->texture coordinates
183 
184  // animations
185  int m_currentLoop;
186  unsigned int m_currentFrame;
187  uint32_t m_lasttime;
188 
189  float m_diffuseU, m_diffuseV; // size of the diffuse frame (in tex coords)
190  float m_diffuseScaleU, m_diffuseScaleV; // scale factor of the diffuse frame (from texture coords to diffuse tex coords)
191  CPoint m_diffuseOffset; // offset into the diffuse frame (it's not always the origin)
192 
193  bool m_allocateDynamically;
194  enum ALLOCATE_TYPE { NO = 0, NORMAL, LARGE, NORMAL_FAILED, LARGE_FAILED };
195  ALLOCATE_TYPE m_isAllocated;
196 
197  CTextureInfo m_info;
198  CAspectRatio m_aspect;
199 
200  CTextureArray m_diffuse;
201  CTextureArray m_texture;
202 
203 private:
204  static CreateGUITextureFunc m_createGUITextureFunc;
205  static DrawQuadFunc m_drawQuadFunc;
206 };
Definition: GUIListItem.h:30
Definition: TextureManager.h:29
Definition: GUITexture.h:28
Base texture class, subclasses of which depend on the render spec (DX, GL etc.)
Definition: Texture.h:34
Definition: GUITexture.h:51
Definition: GUITexture.h:73
virtual void Free()
called after our textures have been allocated
Definition: GUITexture.h:158
KODI::GUILIB::GUIINFO::CGUIInfoColor GetDiffuseColor() const
Get the diffuse color (info color) associated to this texture.
Definition: GUITexture.h:123
Definition: GUIInfoColor.h:30