Perception Tool Kit (PTK) API Guide
String.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2018 Texas Instruments Incorporated
4  *
5  * All rights reserved not granted herein.
6  *
7  * Limited License.
8  *
9  * Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
10  * license under copyrights and patents it now or hereafter owns or controls to make,
11  * have made, use, import, offer to sell and sell ("Utilize") this software subject to the
12  * terms herein. With respect to the foregoing patent license, such license is granted
13  * solely to the extent that any such patent is necessary to Utilize the software alone.
14  * The patent license shall not apply to any combinations which include this software,
15  * other than combinations with devices manufactured by or for TI ("TI Devices").
16  * No hardware patent is licensed hereunder.
17  *
18  * Redistributions must preserve existing copyright notices and reproduce this license
19  * (including the above copyright notice and the disclaimer and (if applicable) source
20  * code license limitations below) in the documentation and/or other materials provided
21  * with the distribution
22  *
23  * Redistribution and use in binary form, without modification, are permitted provided
24  * that the following conditions are met:
25  *
26  * * No reverse engineering, decompilation, or disassembly of this software is
27  * permitted with respect to any software provided in binary form.
28  *
29  * * any redistribution and use are licensed by TI for use only with TI Devices.
30  *
31  * * Nothing shall obligate TI to provide you with source code for the software
32  * licensed and provided to you in object code.
33  *
34  * If software source code is provided to you, modification and redistribution of the
35  * source code are permitted provided that the following conditions are met:
36  *
37  * * any redistribution and use of the source code, including any resulting derivative
38  * works, are licensed by TI for use only with TI Devices.
39  *
40  * * any redistribution and use of any object code compiled from the source code
41  * and any resulting derivative works, are licensed by TI for use only with TI Devices.
42  *
43  * Neither the name of Texas Instruments Incorporated nor the names of its suppliers
44  *
45  * may be used to endorse or promote products derived from this software without
46  * specific prior written permission.
47  *
48  * DISCLAIMER.
49  *
50  * THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
51  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53  * IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
55  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
57  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
58  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59  * OF THE POSSIBILITY OF SUCH DAMAGE.
60  *
61  */
62 #pragma once
63 #ifndef PTK_GUI_STRING_H
64 #define PTK_GUI_STRING_H
65 
66 /*
67  * @todo: Unify the buffers in the renderer so that strings of a certain type can be rendered
68  * with one call; use a uniform buffer object with color vectors and add a single id per char
69  * indicating which color to use
70  * @todo: Take another look at the use of short/long. The use of shorts caused problems with stuff
71  * that went off screen but it wasn't fixed fully, five years ago
72  */
73 
79 #include <string>
80 
81 #include <perception/gui/GLlibs.h>
82 
83 #include <glm/glm.hpp>
84 #include <glm/gtc/type_precision.hpp>
85 #include <glm/gtc/type_ptr.hpp>
86 
87 #include <perception/gui/Font.h>
89 #include <perception/gui/Visible.h>
90 
91 namespace ptk {
92 
93 class Renderer;
94 class Font;
95 
100 class String : public ZOrderable<String>, public Visible<String> {
101 private:
102  //
103  std::string _source;
104  bool _init;
105  bool _gInit;
106  bool _modified;
107  int _fontId;
110 
111  // Start of the string
112  float _x;
113  float _y;
114 
115  // Start of string, normalized
118 
119  // End of string, normalized
122 
123  // Bounds for where we are allowed to draw our string (should not emit vertices greater than these)
124  GLint _bMinX;
125  GLint _bMaxX;
126  GLint _bMinY;
127  GLint _bMaxY;
128 
129  // Buffers to copy to GPU
130  glm::u16vec2 * _vertcoords;
131  glm::u16vec2 * _texcoords;
133  glm::vec4 _color;
134 
135  // Array size counters
136  size_t _strLen;
137  size_t _maxCount;
140 
141  // opengl buffer identifiers
145 
146  // Drawing and management helper methods
147  void drawChar(const Font::char_info& ci,
148  GLshort curX,
149  GLshort curY,
150  int indexOffset,
151  int vertexOffset);
152  bool hasCapacity(size_t count);
153  void increaseCapacity(size_t minCapacity, size_t copyCount);
154  void findPen(const std::string& source);
155  void findPenDraw(const std::string& source);
156 
157 public:
158  String(Renderer *r, Font* font);
159  ~String(void);
160 
161  // Disable copy constructor and assignment operator.
162  String(const String&) = delete;
163  String& operator=(const String&) = delete;
164 
165  // Accessors
166  size_t length(void) const { return _source.length(); }
167  int getFontId(void) const { return _fontId; }
168  Font* getFont(void) const { return _font; }
169  int getVertexCount(void) const { return _vertexCount; }
170  int getIndexCount(void) const { return _indexCount; }
171  const std::string& getText(void) const { return _source; }
172 
173  // Text adjustment
174  void drawText(const std::string& source, float normX, float normY);
175  void drawText(const std::string& source);
176  String *translate(float normX, float normY);
177  String *setPosition(float normX, float normY);
178  String *setColor(const glm::vec4& color) { _color = color; return this; }
179  void recompute() { drawText(_source, _x, _y); }
180 
181  // Update the opacity
182  void setOpacity(float alpha);
183  void setOpacity(GLubyte alpha);
184 
185  // Bounding box adjustment and reading
186  void setMinX(float minX);
187  void setMinY(float minY);
188  void setMaxX(float maxX);
189  void setMaxY(float maxY);
190  float getWidthf(void) const { return _font->getStringWidthf(_source); }
191  float getHeightf(void) const { return _font->getHeight(); }
192  float getX(void) const { return _x; }
193  float getY(void) const { return _y; }
194 
195  // Text modification
196  String *append(const std::string& source);
197  String *remove(size_t start);
198  String *remove(size_t start, size_t length);
199  String *insert(const std::string& source, int start);
200 
201  // Rendering
202  void graphicsInit(void);
203  void render(void);
204 };
205 
206 };
207 
208 #endif
int getIndexCount(void) const
Definition: String.h:170
float getStringWidthf(const std::string &text)
String * translate(float normX, float normY)
GLint _bMaxY
Definition: String.h:127
Definition: ZOrderable.h:75
void drawText(const std::string &source, float normX, float normY)
bool _modified
Definition: String.h:106
float getWidthf(void) const
Definition: String.h:190
size_t _maxCount
Definition: String.h:137
Definition: Visible.h:73
void graphicsInit(void)
glm::u16vec2 * _vertcoords
Definition: String.h:130
String * append(const std::string &source)
void render(void)
String * insert(const std::string &source, int start)
float _y
Definition: String.h:113
GLint _bMaxX
Definition: String.h:125
Definition: Font.h:87
GLuint _vbo[2]
Definition: String.h:143
float getHeight(void) const
Definition: Font.h:160
void increaseCapacity(size_t minCapacity, size_t copyCount)
void setOpacity(float alpha)
Font * _font
Definition: String.h:108
Definition: Renderer.h:169
String & operator=(const String &)=delete
GLint _bMinX
Definition: String.h:124
GLuint _ibo
Definition: String.h:144
float _x
Definition: String.h:112
bool hasCapacity(size_t count)
Definition: sensor.h:74
int _fontId
Definition: String.h:107
float getY(void) const
Definition: String.h:193
GLushort _curX
Definition: String.h:120
size_t length(void) const
Definition: String.h:166
struct Renderer Renderer
Definition: c/Renderer.h:81
GLushort _startX
Definition: String.h:116
std::string _source
Definition: String.h:103
GLushort _curY
Definition: String.h:121
void setMinX(float minX)
int _indexCount
Definition: String.h:139
void findPen(const std::string &source)
void recompute()
Definition: String.h:179
int getFontId(void) const
Definition: String.h:167
const std::string & getText(void) const
Definition: String.h:171
GLushort * _index
Definition: String.h:132
glm::u16vec2 * _texcoords
Definition: String.h:131
void drawChar(const Font::char_info &ci, GLshort curX, GLshort curY, int indexOffset, int vertexOffset)
void setMaxX(float maxX)
size_t _strLen
Definition: String.h:136
int _vertexCount
Definition: String.h:138
void findPenDraw(const std::string &source)
Font * getFont(void) const
Definition: String.h:168
void setMaxY(float maxY)
String * setPosition(float normX, float normY)
GLint _bMinY
Definition: String.h:126
void setMinY(float minY)
float getHeightf(void) const
Definition: String.h:191
Definition: String.h:100
GLuint _vao
Definition: String.h:142
Renderer * _renderer
Definition: String.h:109
glm::vec4 _color
Definition: String.h:133
bool _init
Definition: String.h:104
int getVertexCount(void) const
Definition: String.h:169
String * setColor(const glm::vec4 &color)
Definition: String.h:178
Definition: Font.h:81
GLushort _startY
Definition: String.h:117
bool _gInit
Definition: String.h:105
String(Renderer *r, Font *font)
float getX(void) const
Definition: String.h:192