Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
window.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2003 Mike Byron
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #include "vegastrike.h"
23 
24 #include "window.h"
25 
26 #include "eventmanager.h"
27 #include "groupcontrol.h"
28 #include "windowcontroller.h"
29 
30 #include "eventmanager.h"
31 
32 //For drawing the cursor.
33 #include "gfx/aux_texture.h"
34 #include "gfx/sprite.h"
35 
36 //The outside boundaries of the window.
37 void Window::setRect( const Rect &r )
38 {
39  m_rect = r;
40 }
42 {
44 }
46 {
47  setRect( Rect( -size.width/2.0, -size.height/2.0, size.width, size.height ) );
48 }
49 
50 //Initially display the window.
51 //Call this when all the properties and controls of the window are set.
52 void Window::open( void )
53 {
55 }
56 
57 //Done with the window.
58 void Window::close( void )
59 {
60  processCommand( "Window::Close", NULL );
61 }
62 
63 //Manage controls.
65 {
66  m_controls->addChild( c );
67 }
69 {
71 }
72 
73 //Take a control away from this window and save it elsewhere.
75 {
77 }
78 
79 //Find a control using its id. NULL returned if none found.
80 //Note that the control may be hidden.
81 Control* Window::findControlById( const std::string &id )
82 {
83  return m_controls->findControlById( id );
84 }
85 
86 //Draw/redraw the whole window.
87 void Window::draw( void )
88 {
90  m_controls->draw();
91 }
92 
93 //Draw window background.
95 {
97  if ( !isClear( m_color ) )
99  if ( !isClear( m_outlineColor ) )
101 }
102 
103 //Read window properties and controls from an XML file.
104 void Window::readFromXml( const std::string &fileName ) {}
105 
106 //OVERRIDES
108 {
109  if ( m_controls->processMouseDown( event ) )
110  return true;
111  return EventResponder::processMouseDown( event );
112 }
113 
114 bool Window::processMouseUp( const InputEvent &event )
115 {
116  if ( m_controls->processMouseUp( event ) )
117  return true;
118  return EventResponder::processMouseUp( event );
119 }
120 
122 {
123  if ( m_controls->processMouseMove( event ) )
124  return true;
125  return EventResponder::processMouseMove( event );
126 }
127 
129 {
130  if ( m_controls->processMouseDrag( event ) )
131  return true;
132  return EventResponder::processMouseDrag( event );
133 }
134 
135 bool Window::processCommand( const EventCommandId &command, Control *control )
136 {
137  //The controller should see the command first.
138  if (m_controller != NULL)
139  if ( m_controller->processWindowCommand( command, control ) )
140  return true;
141  //Default command responders.
142  if (command == "Window::Close") {
143  //Close the window!
144  //CAREFUL! This may delete this object! Should be the last line in the function.
146  return true;
147  }
148  return EventResponder::processCommand( command, control );
149 }
150 
151 //CONSTRUCTION
152 Window::Window( void ) :
153  m_rect( 0.0, 0.0, 0.0, 0.0 )
154  , m_color( GUI_OPAQUE_BLACK() )
155  , m_outlineColor( GUI_CLEAR )
156  , m_outlineWidth( 1.0 )
157  , m_deleteOnClose( true )
158  , m_controls( NULL )
159  , m_controller( NULL )
160 {
161  m_controls = new GroupControl();
162 }
163 
165 {
167 }
168 
170 
171 extern void ConditionalCursorDraw( bool );
172 //Draw all visible windows.
174 {
175  GFXHudMode( true ); //Load identity matrices.
177 
180  GFXDisable( LIGHTING );
181  GFXDisable( CULLFACE );
182  GFXClear( GFXTRUE );
185  GFXDisable( TEXTURE1 );
186  GFXEnable( TEXTURE0 );
187  //Just loop through all the windows, and remember if anything gets drawn.
188  //Since the first window in the list is drawn first, z-order is
189  //maintained. First entry is the bottom window, last is the top window.
190  //FIXME mbyron -- I think the event manager needs to get involved with window z-order.
191  //(Mouse events should go to windows in zorder, shouldn't they?)
192  for (size_t i = 0; i < m_windows.size(); i++) {
193  if ( m_windows[i]->controller() )
194  m_windows[i]->controller()->draw();
195  if ( i < m_windows.size() ) m_windows[i]->draw();
196  }
197  //Emulate EndGUIFrame.
198  static VSSprite MouseVSSprite( "mouse.spr", BILINEAR, GFXTRUE );
199  static Texture dummy( "white.bmp", 0, NEAREST, TEXTURE2D, TEXTURE_2D, GFXTRUE );
200  GFXDisable( CULLFACE );
201  ConditionalCursorDraw( true );
202  //Figure position of cursor sprite.
203  float sizex = 0.0, sizey = 0.0;
204  const Point loc = globalEventManager().mouseLoc();
205  MouseVSSprite.GetSize( sizex, sizey );
206  float tempx = 0.0, tempy = 0.0;
207  MouseVSSprite.GetPosition( tempx, tempy );
208  MouseVSSprite.SetPosition( tempx+loc.x+sizex/2, tempy+loc.y+sizey/2 );
209 
210  dummy.MakeActive();
213 
214  //Draw the cursor sprite.
215  GFXEnable( TEXTURE0 );
217  GFXDisable( TEXTURE1 );
218  MouseVSSprite.Draw();
219 
220  GFXHudMode( false );
221  GFXEnable( CULLFACE );
222  MouseVSSprite.SetPosition( tempx, tempy );
223 }
224 
225 //A new window has been created and is ready to be drawn.
227 {
228  m_windows.push_back( w );
230 }
231 
232 //A window has been closed.
233 void WindowManager::closeWindow( Window *w, //Old window.
234  bool deleteWindow //True = delete window.
235  )
236 {
237  vector< Window* >::iterator iter;
238  for (iter = m_windows.begin(); iter != m_windows.end(); iter++)
239  if ( (*iter) == w ) {
240  m_windows.erase( iter );
241  globalEventManager().removeResponder( w ); //Have to do this now.
242  if (deleteWindow)
244  break;
245  }
246 }
247 
248 //Close all windows.
250 {
251  while (m_windows.size() > 0)
252  m_windows.back()->close();
253  //m_windows.erase(m_windows.begin());
254 }
255 
256 //Pointer to the one, global window manager.
258 
259 //Get the one window manager.
261 {
262  if (globalWindowManagerPtr == NULL)
263  globalWindowManagerPtr = new WindowManager;
264  return *globalWindowManagerPtr;
265 }
266