3es  0.7
3estext3d.h
1 //
2 // author: Kazys Stepanas
3 //
4 #ifndef _3ESTEXT3D_H_
5 #define _3ESTEXT3D_H_
6 
7 #include "3es-core.h"
8 
9 #include "3esshape.h"
10 
11 #include <cstdint>
12 #include <cstring>
13 
14 namespace tes
15 {
20  class _3es_coreAPI Text3D : public Shape
21  {
22  public:
23  static const Vector3f DefaultFacing;
24 
25  //Text3D(const char *text, uint16_t textLength, const V3Arg &pos = V3Arg(0, 0, 0), int fontSize = 12);
26  //Text3D(const char *text, uint16_t textLength, const V3Arg &pos, const V3Arg &facing, int fontSize = 12);
27  //Text3D(const char *text, uint16_t textLength, uint32_t id, const V3Arg &pos = V3Arg(0, 0, 0), int fontSize = 12);
28  //Text3D(const char *text, uint16_t textLength, uint32_t id, const V3Arg &pos, const V3Arg &facing, int fontSize = 12);
29  //Text3D(const char *text, uint16_t textLength, uint32_t id, uint16_t category, const V3Arg &pos = V3Arg(0, 0, 0), int fontSize = 12);
30  //Text3D(const char *text, uint16_t textLength, uint32_t id, uint16_t category, const V3Arg &pos, const V3Arg &facing, int fontSize = 12);
31  Text3D(const char *text, const V3Arg &pos = V3Arg(0, 0, 0), int fontSize = 12);
32  Text3D(const char *text, const V3Arg &pos, const V3Arg &facing, int fontSize = 12);
33  Text3D(const char *text, uint32_t id, const V3Arg &pos = V3Arg(0, 0, 0), int fontSize = 12);
34  Text3D(const char *text, uint32_t id, const V3Arg &pos, const V3Arg &facing, int fontSize = 12);
35  Text3D(const char *text, uint32_t id, uint16_t category, const V3Arg &pos = V3Arg(0, 0, 0), int fontSize = 12);
39  Text3D(const char *text, uint32_t id, uint16_t category, const V3Arg &pos, const V3Arg &facing, int fontSize = 12);
40 
41  ~Text3D();
42 
43  bool screenFacing() const;
44  Text3D &setScreenFacing(bool worldSpace);
45 
46  Text3D &setFacing(const V3Arg &toCamera);
47  Vector3f facing() const;
48 
49  int fontSize() const;
50  Text3D &setFontSize(int size);
51 
52  inline char *text() const { return _text; }
53  inline uint16_t textLength() const { return _textLength; }
54 
55  Text3D &setText(const char *text, uint16_t textLength);
56 
57  virtual bool writeCreate(PacketWriter &stream) const override;
58 
59  Shape *clone() const override;
60 
61  protected:
62  void onClone(Text3D *copy) const;
63 
64  private:
65  char *_text;
66  uint16_t _textLength;
67  };
68 
69 
70  //inline Text3D::Text3D(const char *text, uint16_t textLength, const V3Arg &pos, int fontSize)
71  // : Shape(SIdText3D)
72  // , _text(nullptr)
73  // , _textLength(0)
74  //{
75  // setPosition(pos);
76  // setText(text, textLength);
77  // setFontSize(fontSize);
78  //}
79 
80 
81  //inline Text3D::Text3D(const char *text, uint16_t textLength, const V3Arg &pos, const V3Arg &facing, int fontSize)
82  // : Shape(SIdText3D)
83  // , _text(nullptr)
84  // , _textLength(0)
85  //{
86  // setPosition(pos);
87  // setText(text, textLength);
88  // setFontSize(fontSize);
89  //}
90 
91 
92  //inline Text3D::Text3D(const char *text, uint16_t textLength, uint32_t id, const V3Arg &pos, int fontSize)
93  // : Shape(SIdText3D, id)
94  // , _text(nullptr)
95  // , _textLength(0)
96  //{
97  // setPosition(pos);
98  // setText(text, textLength);
99  // setFontSize(fontSize);
100  //}
101 
102 
103  //inline Text3D::Text3D(const char *text, uint16_t textLength, uint32_t id, const V3Arg &pos, const V3Arg &facing, int fontSize)
104  // : Shape(SIdText3D, id)
105  // , _text(nullptr)
106  // , _textLength(0)
107  //{
108  // setPosition(pos);
109  // setText(text, textLength);
110  // setFontSize(fontSize);
111  //}
112 
113 
114  //inline Text3D::Text3D(const char *text, uint16_t textLength, uint32_t id, uint16_t category, const V3Arg &pos, int fontSize)
115  // : Shape(SIdText3D, id, category)
116  // , _text(nullptr)
117  // , _textLength(0)
118  //{
119  // setPosition(pos);
120  // setText(text, textLength);
121  // setFontSize(fontSize);
122  //}
123 
124 
125  //inline Text3D::Text3D(const char *text, uint16_t textLength, uint32_t id, uint16_t category, const V3Arg &pos, const V3Arg &facing, int fontSize)
126  // : Shape(SIdText3D, id, category)
127  // , _text(nullptr)
128  // , _textLength(0)
129  //{
130  // setPosition(pos);
131  // setText(text, textLength);
132  // setFontSize(fontSize);
133  //}
134 
135 
136  inline Text3D::Text3D(const char *text, const V3Arg &pos, int fontSize)
137  : Shape(SIdText3D)
138  , _text(nullptr)
139  , _textLength(0)
140  {
141  setPosition(pos);
142  setText(text, (uint16_t)strlen(text));
143  setFontSize(fontSize);
144  }
145 
146 
147  inline Text3D::Text3D(const char *text, const V3Arg &pos, const V3Arg &facing, int fontSize)
148  : Shape(SIdText3D)
149  , _text(nullptr)
150  , _textLength(0)
151  {
152  setPosition(pos);
153  setText(text, (uint16_t)strlen(text));
154  setFontSize(fontSize);
155  }
156 
157 
158  inline Text3D::Text3D(const char *text, uint32_t id, const V3Arg &pos, int fontSize)
159  : Shape(SIdText3D, id)
160  , _text(nullptr)
161  , _textLength(0)
162  {
163  setPosition(pos);
164  setText(text, (uint16_t)strlen(text));
165  setFontSize(fontSize);
166  }
167 
168 
169  inline Text3D::Text3D(const char *text, uint32_t id, const V3Arg &pos, const V3Arg &facing, int fontSize)
170  : Shape(SIdText3D, id)
171  , _text(nullptr)
172  , _textLength(0)
173  {
174  setPosition(pos);
175  setText(text, (uint16_t)strlen(text));
176  setFontSize(fontSize);
177  }
178 
179 
180  inline Text3D::Text3D(const char *text, uint32_t id, uint16_t category, const V3Arg &pos, int fontSize)
181  : Shape(SIdText3D, id, category)
182  , _text(nullptr)
183  , _textLength(0)
184  {
185  setPosition(pos);
186  setText(text, (uint16_t)strlen(text));
187  setFontSize(fontSize);
188  }
189 
190 
191  inline Text3D::Text3D(const char *text, uint32_t id, uint16_t category, const V3Arg &pos, const V3Arg &facing, int fontSize)
192  : Shape(SIdText3D, id, category)
193  , _text(nullptr)
194  , _textLength(0)
195  {
196  setPosition(pos);
197  setText(text, (uint16_t)strlen(text));
198  setFontSize(fontSize);
199  }
200 
201 
202  inline bool Text3D::screenFacing() const
203  {
204  return (_data.flags & Text3DFScreenFacing) != 0;
205  }
206 
207 
208  inline Text3D &Text3D::setScreenFacing(bool worldSpace)
209  {
210  _data.flags &= ~Text3DFScreenFacing;
211  _data.flags |= Text3DFScreenFacing * !!worldSpace;
212  return *this;
213  }
214 
215 
216  inline Text3D &Text3D::setFacing(const V3Arg &toCamera)
217  {
218  setScreenFacing(false);
219  Quaternionf rot;
220  if (toCamera.v3.dot(DefaultFacing) > -0.9998f)
221  {
222  rot = Quaternionf(DefaultFacing, toCamera);
223  }
224  else
225  {
226  rot.setAxisAngle(Vector3f::axisx, float(M_PI));
227  }
228  setRotation(rot);
229  return *this;
230  }
231 
232 
233  inline Vector3f Text3D::facing() const
234  {
235  Quaternionf rot = rotation();
236  return rot * DefaultFacing;
237  }
238 
239 
240  inline int Text3D::fontSize() const
241  {
242  return (int)_data.attributes.scale[2];
243  }
244 
245 
246  inline Text3D &Text3D::setFontSize(int size)
247  {
248  _data.attributes.scale[2] = (float)size;
249  return *this;
250  }
251 }
252 
253 #endif // _3ESTEXT3D_H_
A base class for encapsulating a shape which is to be represented remotely.
Definition: 3esshape.h:39
float scale[3]
Object scale.
Definition: 3esmessages.h:358
Text is oriented to face the screen.
Definition: 3esmessages.h:126
static const Vector3< T > axisx
The vector (1, 0, 0).
Definition: 3esvector3.h:44
Definition: 3esbounds.h:13
Quaternion< T > & setAxisAngle(const Vector3< T > &axis, const T &angle)
Sets this quaternion from an axis of rotation and the angle of rotation about that axis (radians)...
Vector3f v3
Vector 3 value.
Definition: 3esv3arg.h:45
Represents a vector in R3.
Definition: 3esvector3.h:14
A helper structure used to convert from float or double pointers to Vector3f arguments.
Definition: 3esv3arg.h:14
uint16_t flags
Flags controlling the appearance and creation of the object.
Definition: 3esmessages.h:406
Quaternion< float > Quaternionf
Defines a single precision quaternion.
Definition: 3esquaternion.h:198
ObjectAttributes attributes
Initial transformation and colour.
Definition: 3esmessages.h:408
A utility class for writing payload data to a PacketHeader.
Definition: 3espacketwriter.h:34
T dot(const Vector3< T > &other) const
Calculates the dot product of this.other.
Definition: 3esvector3.h:422
A shape 3D world position and perspective adjusted text, optionally screen facing.
Definition: 3estext3d.h:20