JNR
uiInventory.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 #include "RPG.h"
33 
34 #define M_ICON_SPACING 5
35 #define M_RESIZE_SPEED 100
36 
37 uiInventory::uiInventory(rpgInventory* pi)
38 {
39  _pInventory = pi;
40  _nSelected = 0;
41 
42  create(laPoint3(), laPoint3());
43  caption.setText("");
44 
45  _mkPrevious.set(KEY_LBRACKET, 1,1);
46  _mkNext.set(KEY_RBRACKET, 1,1);
47 
48  _bEnableSelection = _bHorizontal = M_TRUE;
49 }
50 
51 uiInventory::~uiInventory(void)
52 {
53 }
54 
56 {
57  // Ensure all icons are created
58  _lazy_create_all_icons();
59 
61 
62  laRenderer *pr = laSystemIntegrator::getRenderer();
63  rpgInventory::iterator i;
64 
65  pr->transPush();
66  pr->transTranslate( _ptPos + laPoint3( getDecorationSize().x(), getDecorationSize().y()*0.8) );
67 
68  // Draw icons
69  //
70  double offset=0;
71 
72  for(i=_pInventory->begin(); i!=_pInventory->end(); i++)
73  {
74  uiIcon3D *pIcon = (*i)->pIcon;
75 
76  if(_bHorizontal)
77  {
78  pIcon->move( laPoint3(offset, 10) );
79  offset += pIcon->getSize().x() + M_ICON_SPACING;
80  }
81  else
82  {
83  pIcon->move( laPoint3(0, offset));
84  offset += pIcon->getSize().y() + M_ICON_SPACING;
85  }
86 
87  pIcon->draw();
88  }
89 
90  pr->transPop();
91 }
92 
94 {
95  // Ensure all icons are created
96  _lazy_create_all_icons();
97 
98  // Pass the reply() to all icons
99  // TODO: Rely on the uiContainer instead
100  //
101  for(rpgInventory::iterator i=_pInventory->begin(); i!=_pInventory->end(); i++)
102  (*i)->pIcon->reply();
103 
104  // Subroutines
105  //
106  if(_bEnableSelection) _reply_to_keyboard();
107 
108  if(_bHorizontal) _reply_to_resize_h();
109  else _reply_to_resize_v();
110 
112 }
113 
114 void uiInventory::_reply_to_keyboard()
115 {
116  laInputManager* pi = laSystemIntegrator::getInput();
117  laTimer* pt = laSystemIntegrator::getEnvironment()->getTimer();
118 
119  // Change selection
120  //
121  if( _mkPrevious.animate(pi, *pt) )
122  {
123  _mkPrevious.reset();
124  select( selection() - 1);
125  }
126 
127  if( _mkNext.animate(pi, *pt) )
128  {
129  _mkNext.reset();
130  select( selection() + 1);
131  }
132 
133  if(pi->key(KEY_1) && (_pInventory->size()>=1) ) select(0);
134  if(pi->key(KEY_2) && (_pInventory->size()>=2) ) select(1);
135  if(pi->key(KEY_3) && (_pInventory->size()>=3) ) select(2);
136  if(pi->key(KEY_4) && (_pInventory->size()>=4) ) select(3);
137  if(pi->key(KEY_5) && (_pInventory->size()>=5) ) select(4);
138  if(pi->key(KEY_6) && (_pInventory->size()>=6) ) select(5);
139  if(pi->key(KEY_7) && (_pInventory->size()>=7) ) select(6);
140  if(pi->key(KEY_8) && (_pInventory->size()>=8) ) select(7);
141  if(pi->key(KEY_9) && (_pInventory->size()>=9) ) select(8);
142  if(pi->key(KEY_0) && (_pInventory->size()>=10) ) select(9);
143 
144  // Hook inventory index
145  //
146  if( (_nSelected < _pInventory->size()) && (_bEnableSelection) )
147  {
148  _lazy_create_icon( _pInventory->at(_nSelected) ); // force load icon
149  _pInventory->at(_nSelected)->pIcon->select(); // select at the icon obj
150  _pInventory->activate(_nSelected); // select at the invoentory obj
151 
152  caption.setText( _pInventory->at(_nSelected)->strName );
153  }
154 }
155 
156 void uiInventory::_reply_to_resize_h()
157 {
158  laInputManager* pi = laSystemIntegrator::getInput();
159  laTimer* pt = laSystemIntegrator::getEnvironment()->getTimer();
160  rpgInventory::iterator i;
161 
162  // Calculate new size target
163  //
164  _ptNewSize.x(0);
165  _ptNewSize.y(0);
166 
167  for(i=_pInventory->begin(); i!=_pInventory->end(); i++)
168  {
169  _ptNewSize[0] += (*i)->pIcon->getSize().x();
170  if( (*i)->pIcon->getSize().y() > _ptNewSize.y()) _ptNewSize[1] = (*i)->pIcon->getSize().y();
171  }
172 
173  _ptNewSize += laPoint3(tangle.w*2, tangle.h*2);
174 
175  // Resize to fit all icons
176  //
177  if( _ptNewSize.x() - _ptSize.x() > 1 ) _ptSize[0] += M_RESIZE_SPEED*pt->delta();
178  else if( _ptNewSize.x() - _ptSize.x() < -1 ) _ptSize[0] -= M_RESIZE_SPEED*pt->delta();
179 
180  _ptSize[0] = M_MAX(_ptSize.x(), tangle.w*2 + _pSkin->parameter("TX_ICON3D_SEL").texture().w);
181  _ptSize[1] = M_MAX(_ptNewSize.y(), tangle.h*2 + _pSkin->parameter("TX_ICON3D_SEL").texture().h);
182 
183  //_ptPos = laPoint3( (w-_ptSize.x())/2.0, h - _ptSize.y() + tangle.h );
184 }
185 
186 void uiInventory::_reply_to_resize_v()
187 {
188  laInputManager* pi = laSystemIntegrator::getInput();
189  laTimer* pt = laSystemIntegrator::getEnvironment()->getTimer();
190  rpgInventory::iterator i;
191 
192  // Calculate new size target
193  //
194  _ptNewSize.x(0);
195  _ptNewSize.y(0);
196 
197  for(i=_pInventory->begin(); i!=_pInventory->end(); i++)
198  {
199  if( (*i)->pIcon->getSize().x() > _ptNewSize.x()) _ptNewSize[0] = (*i)->pIcon->getSize().x();
200 
201  _ptNewSize[1] += (*i)->pIcon->getSize().y();
202 
203  }
204 
205  _ptNewSize += laPoint3(tangle.w*2, tangle.h*2);
206 
207  // Resize to fit all icons
208  //
209  if( _ptNewSize.x() - _ptSize.x() > 1 ) _ptSize[0] += M_RESIZE_SPEED*pt->delta();
210  else if( _ptNewSize.x() - _ptSize.x() < -1 ) _ptSize[0] -= M_RESIZE_SPEED*pt->delta();
211 
212  if( _ptNewSize.y() - _ptSize.y() > 1 ) _ptSize[1] += M_RESIZE_SPEED*pt->delta();
213  else if( _ptNewSize.y() - _ptSize.y() < -1 ) _ptSize[1] -= M_RESIZE_SPEED*pt->delta();
214 
215  //_ptSize[0] = M_MAX(_ptNewSize.x(), tangle.w*2 + _pSkin->parameter("TX_ICON3D_SEL").texture().w);
216  //_ptSize[1] = M_MAX(_ptSize.y(), tangle.h*2 + _pSkin->parameter("TX_ICON3D_SEL").texture().h);
217 }
218 
219 void uiInventory::_lazy_create_icon( rpgItem *ps )
220 {
221  if( ! ( ps->pIcon ) )
222  {
223  MLOG("Creating icon for '%s'", ps->strName);
224  ps->createIcon();
225  //ps->pIcon->_pParent = this;
226  MLOG("Icon created.");
227  }
228 
229  ASSERT(ps->pIcon, "Nil icon");
230 }
231 
232 // Ensure all icons are created
233 //
234 void uiInventory::_lazy_create_all_icons()
235 {
236  rpgInventory::iterator i;
237 
238  for(i=_pInventory->begin(); i!=_pInventory->end(); i++) {
239  _lazy_create_icon(*i);
240  }
241 }
242 
243 void uiInventory::select( int n )
244 {
245  if(! _bEnableSelection ) return;
246 
247  if( n < 0) n = _pInventory->size() - 1;
248  else if( n >= _pInventory->size()) n = 0;
249 
250  _lazy_create_icon( _pInventory->at(_nSelected) );
251  _lazy_create_icon( _pInventory->at(n) );
252 
253  _pInventory->at(_nSelected)->pIcon->select(M_FALSE);
254  _nSelected = n;
255  _pInventory->at(_nSelected)->pIcon->select();
256 }
257 
258 
259 uiIcon3D* uiInventory::getIcon(unsigned n)
260 {
261  ASSERT(_pInventory, "Nil inventory" );
262  ASSERT(n < _pInventory->size(), "Icon out of bounds", n );
263  ASSERT(_pInventory->at(n)->pIcon, "Nil icon" );
264 
265  return _pInventory->at(n)->pIcon;
266 }
laPoint3 _ptPos
Windos position (relative to parent)
Definition: uiWindow.h:75
laTimer * getTimer()
Get inegrated timer.
Definition: laEnvironment.h:85
virtual void move(laPoint3 ptNewPos, unsigned nHReference=M_AL, unsigned nVReference=M_AT)
Change window position.
Definition: uiWindow.cpp:51
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiContainer.cpp:97
uiLabel caption
Caption.
Definition: uiContainer.h:83
laPoint3 _ptSize
Window size.
Definition: uiWindow.h:76
virtual void draw()
Display the window.
virtual void reply()
Handle input message.
Definition: uiInventory.cpp:93
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
virtual void draw()
Display the window.
Definition: uiIcon3D.cpp:68
virtual void reply()
Handle input message.
GUI icon object that can siplay 3D models as icons.
Definition: uiIcon3D.h:52
virtual void draw()
Display the window.
Definition: uiInventory.cpp:55