JNR
fxAnimatedText.h
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 #pragma once
31 
32 #define M_ANIMETED_TEXT_MAX_LINE 256
33 
34 class _laIntroTextLine{
35 public:
36  unsigned nEmotion;
37  char strText[M_ANIMETED_TEXT_MAX_LINE];
38 
39  double dBias;
40  double dRandBias;
41 
42  _laIntroTextLine(laFileParser *fp)
43  {
44  fp->readUnsigned(&nEmotion);
45  fp->readText(strText);
46  }
47 };
48 
49 class fxAnimatedText: public laLoadableObj
50 {
51  //Text lines
52  std::vector<_laIntroTextLine> _vLines;
53  unsigned _nVisible;
54  laTimeTrig _ttLineAnim;
55 
56  //Style
57  laFont* _pFont;
58  double _dFontSz;
59  double _dLineSpacing;
60  unsigned _align;
61  laColor _color;
62 
63  // FX flags
64  //
65  M_BOOL _bFX_Slide;
66  double _dFX_Slide_rangex;
67  double _dFX_Slide_rangey;
68  double _dFX_Slide_speedx;
69  double _dFX_Slide_speedy;
70 
71  M_BOOL _bFX_Ramble;
72  double _dFX_Ramble_rangex;
73  double _dFX_Ramble_rangey;
74  double _dFX_Ramble_speedx;
75  double _dFX_Ramble_speedy;
76 
77  M_BOOL _bFX_Chaos;
78  double _dFX_Chaos_arange;
79  //double _dFX_Chaos_aspeed;
80 
81  //Tex anim aids
82  double _ang;
83  double _dir;
84 
85 
86 
87 public:
88  fxAnimatedText(void);
89  ~fxAnimatedText(void);
90 
91  // Set params
92  void parameters(class laFont* pf,
93  double fsz = 15, double line_spacing = 1.5,
94  laColor color = laColor(255,255,255), double text_speed = 1.6, unsigned align = M_AC)
95  {
96  _pFont = pf;
97  _dFontSz = fsz;
98  _align = align;
99  _dLineSpacing = line_spacing;
100  _color = color;
101  _ttLineAnim.parameters(text_speed);
102  };
103 
104  double getFontSz() { return _dFontSz; };
105  void fontSz(double sz) { _dFontSz = sz; };
106  unsigned fontAlign() { return _align; }
107 
108  void enableSlideFX(M_BOOL enable = M_TRUE,
109  double rangex = /*50*/ 0, double rangey = 0, double speedx = 3, double speedy = 3 )
110  {
111  _bFX_Slide = enable;
112  _dFX_Slide_rangex = rangex;
113  _dFX_Slide_rangey = rangey;
114  _dFX_Slide_speedx = speedx;
115  _dFX_Slide_speedy = speedy;
116  }
117 
118  void enableRambleFX(M_BOOL enable = M_TRUE,
119  double rangex = 5, double rangey = 0, double speedx = 0.3, double speedy = 0.3 )
120  {
121  _bFX_Ramble = enable;
122  _dFX_Ramble_rangex = rangex;
123  _dFX_Ramble_rangey = rangey;
124  _dFX_Ramble_speedx = speedx;
125  _dFX_Ramble_speedy = speedy;
126  }
127 
128  void enableChaosFX(M_BOOL enable = M_TRUE,
129  double ang_range = 3 )
130  {
131  _bFX_Chaos = enable;
132  _dFX_Chaos_arange = ang_range;
133  //_dFX_Chaos_aspeed = ang_speed;
134  }
135 
136  //Force animation
137  //
138  void force()
139  {
140  _nVisible = _vLines.size();
141  _ttLineAnim.force();
142  for(unsigned i=0; i<_nVisible; i++) _vLines[i].dBias = 0;
143  }
144 
145  void reset()
146  {
147  _nVisible = 1;
148  _ttLineAnim.reset();
149  for(unsigned i=0; i<totalCount(); i++)
150  {
151  _vLines[i].dBias = 1;
152  _vLines[i].dRandBias = (rand()%1000)/1000.0;
153  }
154  }
155 
156  M_BOOL isFinished()
157  {
158  if( visibleCount() < totalCount() ) return M_FALSE;
159  return _ttLineAnim.isElapsed();
160  }
161 
162  //Get parameters & state
163  unsigned currentEmotion() { return _vLines[_nVisible-1].nEmotion; }
164  unsigned visibleCount() { return _nVisible; }
165  unsigned totalCount() { return _vLines.size(); }
166 
167  double height() { return totalCount() * ( _dFontSz * _dLineSpacing ); }
168  double width() {
169  unsigned lmax=0, nmax=0;
170 
171  for(unsigned i=0; i<totalCount(); i++)
172  {
173  double l = strlen( _vLines[i].strText );
174  if( l > lmax ) { lmax = l; nmax=i; }
175  }
176 
177  _pFont->ctlSize(_dFontSz);
178  return _pFont->lenght( _vLines[nmax].strText );
179  }
180 
181  //Locate and replace in the dialogue
182  //This can be used to insert game variables
183  void replace(char* strFind, char* strFormat, ...);
184 
185  // Animate messages
186  void animate(laTimer &t);
187 
188  // Draw active messages at their current displacemnts
189  void draw(laRenderer *r, laPoint3 pos);
190 
191  //Load or reload intro file
192  virtual void load(laFileParser *fp);
193 };
Font and Text Drawing.
Definition: laFont.h:41
Interface for loadable objects.
Definition: laLoadableObj.h:43
#define M_AC
Text align center.
Time-triggered events.
Definition: laTimeTrig.h:41
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
File Parser.
Definition: laFileParser.h:41