JNR
uiWindow.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 #include "stdafx.h"
31 #include "GUI.h"
32 
33 uiPointer* uiWindow::_pPointer = NULL;
34 
36 {
37  _pParent = NULL;
38  _bVisible = true;
39  _bEnabled = true;
40 
41  m_bMouseInside = false;
42  rememver_ldn = false;
43  rememver_rdn = false;
44  l = r = 0;
45 }
46 
47 uiWindow::~uiWindow(void)
48 {
49 }
50 
51 void uiWindow::move(laPoint3 ptNewPos, unsigned nHReference, unsigned nVReference)
52 {
53  // Get container size
54  //
55  laPoint3 ptParentSz;
56  if(!_pParent)
57  {
58  laSettings *s = laSystemIntegrator::getSettings();
59  ptParentSz.x( s->graphics_resolution_w );
60  ptParentSz.y( s->graphics_resolution_h );
61  }
62  else
63  {
64  ptParentSz.x( _pParent->getSize().x() - 2*_pParent->getDecorationSize().x() );
65  ptParentSz.y( _pParent->getSize().y() - 2*_pParent->getDecorationSize().y() );
66  }
67 
68  // Horizontal alignment
69  //
70  switch(nHReference)
71  {
72  case M_AL:
73  _ptPos.x( ptNewPos.x() );
74  break;
75  case M_AR:
76  _ptPos.x( (ptParentSz.x() - getSize().x()) - ptNewPos.x() );
77  break;
78  case M_AC:
79  _ptPos.x( (ptParentSz.x() - getSize().x())/2.0 - ptNewPos.x() );
80  break;
81  }
82 
83  // Vertical alignment
84  //
85  switch(nVReference)
86  {
87  case M_AT:
88  _ptPos.y( ptNewPos.y() );
89  break;
90  case M_AB:
91  _ptPos.y( (ptParentSz.y() - getSize().y()) - ptNewPos.y() );
92  break;
93  case M_AM:
94  _ptPos.y( (ptParentSz.y() - getSize().y())/2.0 - ptNewPos.y() );
95  break;
96  }
97 }
98 
99 void uiWindow::create(laPoint3 pos, laPoint3 size)
100 {
101  ASSERT(_pPointer, "Nil pointer");
102  ASSERT(_pSkin, "Nil skin");
103 
104  _ptPos = pos;
105  _ptSize = size;
106  _uID = rand();
107 
108  _ptPointerOldPos = _pPointer->_ptPos;
109 }
110 
111 // Load from .cui file
112 //
114 {
115  laPoint3 pos, sz;
116 
117  fp->readObj(&pos, M_FALSE);
118  fp->readObj(&sz, M_FALSE);
119 
120  //Call the virtual method
121  //i.e. the overridden version of it
122  this->create(pos, sz);
123 }
124 
126 {
127  _uID = 0;
128 }
129 
131 {
132  ASSERT(_pPointer, "Nil pointer");
133 
134  // Get absolute window position
135  laPoint3 pos = this->getAbsolutePos();
136 
137  // Mouse tracking flag
138  bool bInside = false;
139 
140  // Check if mouse pointer is inside the window area
141  if( (pos.x() <= _pPointer->_ptPos.x()) && (pos.y() <= _pPointer->_ptPos.y()) &&
142  (pos.x()+_ptSize.x() >= _pPointer->_ptPos.x()) && (pos.y()+_ptSize.y() >= _pPointer->_ptPos.y()) )
143  {
144  bInside = true;
145  }
146 
147  // Compare with previous cursor pos and trigger a message
148  if( (bInside==true) && (m_bMouseInside==false) ) this->onMouseIn(); //mouse in
149  if( (bInside==false) && (m_bMouseInside==true) ) this->onMouseOut(); //mouse out
150 
151  // Mouse move message
152  if( (bInside==true) && (m_bMouseInside==true) )
153  {
154  _ptPointerOffest += ( _pPointer->_ptPos - _ptPointerOldPos );
155 
156  if( (_ptPointerOffest.lenght() >= CUI__WINDOW_DRAGSTEP) )
157  {
158  this->onMouseMove( getRelativePointerPos() );
159  _ptPointerOffest = laPoint3();
160  }
161  }
162  else
163  {
164  //clear offset
165  _ptPointerOffest = laPoint3();
166  }
167 
168  _ptPointerOldPos = _pPointer->_ptPos;
169 
170  // Update mouse in/out status
171  m_bMouseInside = bInside;
172 
173  // Handle clicks
174  if(m_bMouseInside)
175  {
176  if((in::mouse_b0())&&(!r)) this->onDown(0);
177  if((in::mouse_b1())&&(!l)) this->onDown(1);
178 
179  if((!in::mouse_b0())&&(r)) this->onUp(0);
180  if((!in::mouse_b1())&&(l)) this->onUp(1);
181 
182  l = in::mouse_b1();
183  r = in::mouse_b0();
184  }
185 }
186 
187 //Get absolute pos
189 {
190  if( !_pParent ) return getPos();
191  else
192  {
193  laPoint3 posParent = _pParent->getAbsolutePos() + _pParent->getDecorationSize();
194  return ( posParent + getPos() );
195  }
196 }
197 
199 {
200  /*if(m_bMouseInside)
201  {
202  gr_old::p2d_outline(_ptPos.x(), _ptPos.y(), _ptSize.x(), _ptSize.y(), 1,0,0);
203  }
204  else
205  {
206  gr_old::p2d_outline(_ptPos.x(), _ptPos.y(), _ptSize.x(), _ptSize.y(), 0,1,0);
207  }*/
208 }
209 
210 void uiWindow::onMouseIn()
211 {
212  //do nothing
213 }
214 
215 void uiWindow::onMouseOut()
216 {
217  //do nothing
218 }
219 
220 void uiWindow::onMouseMove(laPoint3 ptRelativePos)
221 {
222  //do nothing
223 }
224 
225 void uiWindow::onClick(unsigned nButton)
226 {
227  //do nothing
228 }
229 
230 /*void uiWindow::OnRClick()
231 {
232  //do nothing
233 }*/
234 
235 void uiWindow::onDown(unsigned nButton)
236 {
237  //rememver_ldn = true;
238 }
239 
240 void uiWindow::onUp(unsigned nButton)
241 {
242  //if(rememver_ldn)
243  //{
244  this->onClick(nButton);
245  //rememver_ldn = false;
246  //}
247 }
248 
249 /*void uiWindow::OnRDown()
250 {
251  rememver_rdn = true;
252 }
253 
254 void uiWindow::OnRUp()
255 {
256  //if(rememver_rdn)
257  //{
258  this->OnRClick();
259  //rememver_rdn = false;
260  //}
261 }*/
262 
263 void uiWindow::onCommand(unsigned long uID, unsigned long nCmd)
264 {
265  //do nothing
266 }
laPoint3 _ptPos
Windos position (relative to parent)
Definition: uiWindow.h:75
#define M_AB
Text align bottom.
virtual void load(class laFileParser *fp)
Load a dialog layout form .cui file.
Definition: uiWindow.cpp:113
virtual void move(laPoint3 ptNewPos, unsigned nHReference=M_AL, unsigned nVReference=M_AT)
Change window position.
Definition: uiWindow.cpp:51
#define M_AM
Text align middle.
#define M_AL
Text align left.
#define M_AT
Text align top.
#define M_AR
Text align right.
laPoint3 _ptSize
Window size.
Definition: uiWindow.h:76
bool _bVisible
Window visibility flag.
Definition: uiWindow.h:86
uiWindow * _pParent
Pointer to parent window, if any.
Definition: uiWindow.h:71
laPoint3 getAbsolutePos()
Get absolute window position.
Definition: uiWindow.cpp:188
Game Settings.
Definition: laSettings.h:38
uiWindow(void)
Class constructor.
Definition: uiWindow.cpp:35
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiWindow.cpp:99
#define M_AC
Text align center.
laPoint3 getRelativePointerPos()
Get mouse pointer position relative to window position.
Definition: uiWindow.h:81
bool _bEnabled
Window interactivity flag.
Definition: uiWindow.h:89
virtual void reply()
Handle input message.
Definition: uiWindow.cpp:130
virtual void draw()
Display the window.
Definition: uiWindow.cpp:198
laPoint3 getPos()
Get relative window position with regard to parent.
Definition: uiWindow.h:132
virtual void onCommand(unsigned long uID, unsigned long nCmd)
Command event handler.
Definition: uiWindow.cpp:263
File Parser.
Definition: laFileParser.h:41
virtual void kill()
Discard window and all children.
Definition: uiWindow.cpp:125
GUI Mouse Pointer.
Definition: uiPointer.h:41