JNR
uiEdit.cpp
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 //
31 // uiEdit.h
32 // Dependent: -
33 //
34 // UI Editbox
35 //
36 #include "stdafx.h"
37 #include "GUI.h"
38 
39 uiEdit* uiEdit::pactive = NULL;
40 
41 uiEdit::uiEdit():uiLabel()
42 {
43 }
44 
45 uiEdit::~uiEdit()
46 {
47 }
48 
49 void uiEdit::create(laPoint3 pos, laPoint3 size)
50 {
51  //Textures
52  caret = _pSkin->parameter("TX_CARET").texture();
53  texbtn = _pSkin->parameter("TX_EDIT").texture();
54 
55  //Create label
56  uiLabel::create(pos, size);
57  pactive = this;
58  align(M_AL, M_AM);
59  _bCustomColor = M_TRUE;
60 
61  //Setup timers
62  //_ttBlink.setElapse(0.5);
63  //_ttBlink.setHold(0.5);
64  //_ttBlink.setCycling(M_TRUE);
65  _ttBlink.parameters(0.5, 0.5, M_TRUE);
66  _ttBlink.enable(M_TRUE);
67  _ttBlink.reset();
68 
69  //_ttType.setElapse(0.1);
70  //_ttType.setHold(0.1);
71  //_ttType.setCycling(M_FALSE);
72  _ttType.parameters(0.1, 0.1, M_FALSE);
73  _ttType.enable(M_FALSE);
74  _ttType.reset();
75 }
76 
78 {
79  uiLabel::kill();
80 }
81 
83 {
84  laRenderer* pr = ::laSystemIntegrator::getRenderer();
85 
86  double dLWidth = (texbtn.w)/4.0;
87 
88  //Draw outlne
89  //
90  pr->vquadsMakeXYRect(0,
91  _ptPos, laPoint3(dLWidth, _ptSize.y()),
92  laPoint2(), laPoint2(1/4.0, 1));
93 
94  pr->vquadsMakeXYRect(1,
95  _ptPos + laPoint3(dLWidth, 0),
96  _ptSize - laPoint3(2*dLWidth, 0),
97  laPoint2(0.25, 0), laPoint2(0.8-0.25, 1));
98 
99  pr->vquadsMakeXYRect(2,
100  _ptPos + laPoint3(_ptSize.x(), 0), laPoint3(-dLWidth, _ptSize.y()),
101  laPoint2(), laPoint2(0.25, 1));
102 
103  pr->styleSet( _pSkin->parameter("CL_CONTAINER").color() );
104  texbtn.use();
105  pr->vquadsDraw(3);
106 
107  //Draw label and carret
108  //
109  pr->transPush();
110  pr->transTranslate( laPoint3( dLWidth, 0) );
111  _rgbCustom = _pSkin->parameter("CL_EDIT_TXT").color();
112 
113  if( (pactive == this) && (_ttBlink.isElapsed()) )
114  {
115  strcat(_strText, "|");
116  uiLabel::draw();
117  _strText[strlen(_strText)-1] = '\0';
118  }
119  else uiLabel::draw();
120 
121  pr->transPop();
122 }
123 
125 {
126  laTimer* t = laSystemIntegrator::getEnvironment()->getTimer();
127  _ttBlink.animate(*t);
128  _ttType.animate(*t);
129 
130  //Default to uiLabel::reply() if not active
131  if(pactive!=this)
132  {
133  uiLabel::reply();
134  return;
135  }
136 
137  //Check typing timer
138  if( _ttType.isEnabled())
139  {
140  if(_ttType.isElapsed())
141  {
142  //Reset and continue
143  _ttType.reset();
144  _ttType.enable(M_FALSE);
145  }
146  else
147  {
148  //Timer not elapsed yet; default to uiLabel::reply()
149  uiLabel::reply();
150  return;
151  }
152  }
153 
154  //Read pressed key
155  char c = in::ascii();
156 
157  if(in::key(KEY_BACK)) //handle backspaces
158  {
159  unsigned l = strlen(_strText);
160  if(l>0) _strText[l-1]='\0';
161  }
162  else if(c!=0) //handle alpha-numeric keys
163  {
164  //check if text fits in the edit area
165  laFont *f = _pSkin->parameter("FN_DEFAULT").font();
166  if( f->lenght(_strText) >= getSize().x()-2*16)
167  {
168  uiLabel::reply();
169  return;
170  }
171 
172  //append character
173  unsigned l = strlen(_strText);
174  _strText[l] = c;
175  _strText[l+1] = 0;
176  }
177 
178  //Enable timing (wait some time before next key is processed)
179  _ttType.enable(M_TRUE);
180 
181  //Default action
182  uiLabel::reply();
183 }
184 
185 void uiEdit::onClick(unsigned nButton)
186 {
187  pactive = this;
188 }
void kill()
Discard window and all children.
Definition: uiLabel.cpp:68
laPoint3 _ptPos
Windos position (relative to parent)
Definition: uiWindow.h:75
virtual void kill()
Discard window and all children.
Definition: uiEdit.cpp:77
laTimer * getTimer()
Get inegrated timer.
Definition: laEnvironment.h:85
Font and Text Drawing.
Definition: laFont.h:41
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiEdit.cpp:49
char _strText[256]
Text string.
Definition: uiLabel.h:44
2D Point
Definition: laPoint_novec.h:41
void vquadsMakeXYRect(unsigned nIndex, const laPoint3 &pos, const laPoint3 &sz, const laPoint2 &uv, const laPoint2 &uv_sz)
Makes a quad aligned to the XY plane; (handy for GUI rendering )
Definition: laRenderer.h:328
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiLabel.cpp:52
#define M_AM
Text align middle.
#define M_AL
Text align left.
laPoint3 _ptSize
Window size.
Definition: uiWindow.h:76
M_BOOL _bCustomColor
Custom color, or use skin color if not set.
Definition: uiLabel.h:56
GUI Editbox.
Definition: uiEdit.h:44
void draw()
Draw event.
Definition: uiEdit.cpp:82
virtual void vquadsDraw(unsigned nQuads, laPoint2 *ar_uv=NULL, laColor *ar_color=NULL, M_BOOL bBillboards=M_FALSE, M_BOOL bUseColorArrays=M_FALSE)=0
Draw an array of VQ, starting with the psecified pointers (or the first VQ if null) ...
void reply()
Handle input message.
Definition: uiEdit.cpp:124
virtual void draw()
Draw event.
Definition: uiLabel.cpp:73
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
Text Label.
Definition: uiLabel.h:41
virtual void reply()
Handle input message.
Definition: uiWindow.cpp:130