JNR
uiButton.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 // uiButton.cpp
32 // Dependent: -
33 //
34 // UI Push button
35 //
36 #include "stdafx.h"
37 #include "GUI.h"
38 
40 {
41  pReceiver = NULL;
42  m_bImageButton = false;
43 
44  //Toggle flags
45  togglePersistent(M_FALSE);
46  toggleRepetitive(M_FALSE);
47 
48  m_bImageButton = false;
49 }
50 
51 uiButton::~uiButton(void)
52 {
53 }
54 
55 void uiButton::create(laPoint3 pos, laPoint3 size)
56 {
57  //get textures
58  texbtn_n = _pSkin->parameter("TX_BUTTON").texture();
59  texbtn_p = _pSkin->parameter("TX_BUTTONP").texture();
60  texbtn = texbtn_n;
61 
62  //Create label flags
63  uiLabel::create(pos, size);
64 }
65 
67  uiLabel::kill();
68 }
69 
70 //Draw button outline
71 //
73 {
74  laRenderer* r = ::laSystemIntegrator::getRenderer();
75  double dEdgeWidth = (texbtn.w)*0.25;
76 
77  r->vquadsMakeXYRect(0,
78  _ptPos, laPoint3(dEdgeWidth, _ptSize.y()),
79  laPoint2(), laPoint2(0.25, 1));
80 
81  r->vquadsMakeXYRect(1,
82  _ptPos + laPoint3(dEdgeWidth, 0), _ptSize - laPoint3(2*dEdgeWidth, 0),
83  laPoint2(0.25, 0), laPoint2(0.26, 1));
84 
85  r->vquadsMakeXYRect(2,
86  _ptPos + laPoint3(_ptSize.x()-dEdgeWidth, 0), laPoint3(dEdgeWidth, _ptSize.y()),
87  laPoint2(0.25, 0), laPoint2(-0.25, 1));
88 
89  texbtn.use();
90  r->vquadsDraw(3);
91 }
92 
94 {
95  laRenderer* r = ::laSystemIntegrator::getRenderer();
96 
97  //Cast shadow
98  r->transPush();
99  r->transTranslate( laPoint3(3,3) );
100  r->styleSet( _pSkin->parameter("CL_SHADOW").color() );
101  _drawButton();
102  r->transPop();
103 
104  //Draw button
105  laColor col = _pSkin->parameter("CL_BUTTON").color();
106  if(!isEnabled()) { col[0]*= 0.8; col[1]*= 0.8; col[2]*= 0.8; }
107  r->styleSet( col );
108 
109  if(texbtn.id() == texbtn_p.id())
110  {
111  //pressed
112  r->transPush();
113  r->transTranslate( laPoint3(1,1) );
114  _drawButton();
115  uiLabel::draw();
116  r->transPop();
117  }
118  else
119  {
120  //unpressed
121  _drawButton();
122  uiLabel::draw();
123  }
124 
125  //Draw image, if any
126  if(m_bImageButton)
127  {
128  r->vquadsMakeXYRect(0,
129  _ptPos + laPoint3(16, 7 ), _ptSize + laPoint3( -2*16, -2*7 ),
130  laPoint2(0,0), laPoint2(1, 1));
131 
132  r->styleSet(laColor(255));
133  m_uImg.use();
134  r->vquadsDrawSingle();
135  }
136 }
137 
138 
140 {
141  //Continuously send messages to the receiver
142  if( _bRepetitiveInformer && (texbtn.id() == texbtn_p.id()))
143  {
144  if(pReceiver!=NULL) pReceiver->onCommand(getID(), 0);
145  else _pParent->onCommand(getID(), 0);
146  }
147 
148  uiLabel::reply();
149 
150 }
151 
152 void uiButton::onDown(unsigned nButton)
153 {
154  //If pressed
155  if(texbtn.id() == texbtn_p.id())
156  {
157  //revert to unpressed state
158  texbtn = texbtn_n;
159  }
160  else
161  {
162  //press
163  texbtn = texbtn_p;
164 
165  //Notify the receiver
166  if(_bPersistent)
167  {
168  if(pReceiver!=NULL) pReceiver->onCommand(getID(), 0);
169  else _pParent->onCommand(getID(), 0);
170  }
171  }
172 }
173 
174 void uiButton::onUp(unsigned nButton)
175 {
176  //if not persistent
177  if(!_bPersistent)
178  {
179  //revert to unpressed state
180  texbtn = texbtn_n;
181 
182  //Notify the receiver
183  if(pReceiver!=NULL) pReceiver->onCommand(getID(), 0);
184  else _pParent->onCommand(getID(), 0);
185  }
186 }
187 
188 void uiButton::onMouseOut()
189 {
190  //If not persistent
191  if(!_bPersistent)
192  {
193  //revert to unpressed state
194  texbtn = texbtn_n;
195  }
196 }
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiButton.cpp:55
void kill()
Discard window and all children.
Definition: uiLabel.cpp:68
void reply()
Handle input message.
Definition: uiButton.cpp:139
laPoint3 _ptPos
Windos position (relative to parent)
Definition: uiWindow.h:75
virtual void vquadsDrawSingle(laPoint2 *ar_uv=NULL)=0
Draw a single VQ (Note this is slower than drawing an array of VQ and should be avoided) ...
2D Point
Definition: laPoint_novec.h:41
void draw()
Draw event.
Definition: uiButton.cpp:93
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
laPoint3 _ptSize
Window size.
Definition: uiWindow.h:76
uiButton(void)
Bla.
Definition: uiButton.cpp:39
uiWindow * _pParent
Pointer to parent window, if any.
Definition: uiWindow.h:71
void kill()
Discard window and all children.
Definition: uiButton.cpp:66
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 _drawButton()
Draw button outline.
Definition: uiButton.cpp:72
virtual void draw()
Draw event.
Definition: uiLabel.cpp:73
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
virtual void reply()
Handle input message.
Definition: uiWindow.cpp:130
virtual void onCommand(unsigned long uID, unsigned long nCmd)
Command event handler.
Definition: uiWindow.cpp:263