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 
119  bool HitTest(const CPoint& point) const
120  {
121  return CRect(m_posX, m_posY, m_posX + m_width, m_posY + m_height).PtInRect(point);
122  }
123  bool IsAllocated() const { return m_isAllocated != NO; }
124  bool FailedToAlloc() const
125  {
126  return m_isAllocated == NORMAL_FAILED || m_isAllocated == LARGE_FAILED;
127  }
128  bool ReadyToRender() const;
129 
130 protected:
131  CGUITexture(float posX, float posY, float width, float height, const CTextureInfo& texture);
132  CGUITexture(const CGUITexture& left);
133 
134  bool CalculateSize();
135  void LoadDiffuseImage();
136  bool AllocateOnDemand();
137  bool UpdateAnimFrame(unsigned int currentTime);
138  void Render(float left,
139  float top,
140  float right,
141  float bottom,
142  float u1,
143  float v1,
144  float u2,
145  float v2,
146  float u3,
147  float v3);
148  static void OrientateTexture(CRect &rect, float width, float height, int orientation);
149  void ResetAnimState();
150 
151  // functions that our implementation classes handle
152  virtual void Allocate() {};
153  virtual void Free() {};
154  virtual void Begin(UTILS::COLOR::Color color) = 0;
155  virtual void Draw(float* x,
156  float* y,
157  float* z,
158  const CRect& texture,
159  const CRect& diffuse,
160  int orientation) = 0;
161  virtual void End() = 0;
162 
163  bool m_visible;
164  UTILS::COLOR::Color m_diffuseColor;
165 
166  float m_posX; // size of the frame
167  float m_posY;
168  float m_width;
169  float m_height;
170 
171  CRect m_vertex; // vertex coords to render
172  bool m_invalid; // if true, we need to recalculate
173  bool m_use_cache;
174  unsigned char m_alpha;
175 
176  float m_frameWidth, m_frameHeight; // size in pixels of the actual frame within the texture
177  float m_texCoordsScaleU, m_texCoordsScaleV; // scale factor for pixel->texture coordinates
178 
179  // animations
180  int m_currentLoop;
181  unsigned int m_currentFrame;
182  uint32_t m_lasttime;
183 
184  float m_diffuseU, m_diffuseV; // size of the diffuse frame (in tex coords)
185  float m_diffuseScaleU, m_diffuseScaleV; // scale factor of the diffuse frame (from texture coords to diffuse tex coords)
186  CPoint m_diffuseOffset; // offset into the diffuse frame (it's not always the origin)
187 
188  bool m_allocateDynamically;
189  enum ALLOCATE_TYPE { NO = 0, NORMAL, LARGE, NORMAL_FAILED, LARGE_FAILED };
190  ALLOCATE_TYPE m_isAllocated;
191 
192  CTextureInfo m_info;
193  CAspectRatio m_aspect;
194 
195  CTextureArray m_diffuse;
196  CTextureArray m_texture;
197 
198 private:
199  static CreateGUITextureFunc m_createGUITextureFunc;
200  static DrawQuadFunc m_drawQuadFunc;
201 };
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:153
Definition: GUIInfoColor.h:30