Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
QuantumView.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: QuantumView.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  View class for Quantum Destination HUD Overlay
13 */
14 
15 #include "MemDebug.h"
16 #include "QuantumView.h"
17 #include "QuantumDrive.h"
18 #include "HUDView.h"
19 #include "Ship.h"
20 #include "Element.h"
21 #include "Sim.h"
22 #include "StarSystem.h"
23 #include "FormatUtil.h"
24 
25 #include "Color.h"
26 #include "Window.h"
27 #include "Video.h"
28 #include "Screen.h"
29 #include "DataLoader.h"
30 #include "Scene.h"
31 #include "Font.h"
32 #include "FontMgr.h"
33 #include "Keyboard.h"
34 #include "Mouse.h"
35 #include "Game.h"
36 #include "Menu.h"
37 
38 static Color hud_color = Color::Black;
39 
40 // +====================================================================+
41 //
42 // QUANTUM DRIVE DESTINATION MENU:
43 //
44 
45 static Menu* quantum_menu = 0;
46 static bool show_menu = false;
47 
48 void
50 {
51  static int initialized = 0;
52  if (initialized) return;
53 
54  quantum_menu = new(__FILE__,__LINE__) Menu(Game::GetText("QuantumView.menu"));
55 
56  initialized = 1;
57 }
58 
59 void
61 {
62  delete quantum_menu;
63 }
64 
65 // +====================================================================+
66 
68 
70 : View(c), sim(0), ship(0), font(0)
71 {
72  quantum_view = this;
73  sim = Sim::GetSim();
74 
75  width = window->Width();
76  height = window->Height();
77  xcenter = (width / 2.0) - 0.5;
78  ycenter = (height / 2.0) + 0.5;
79  font = FontMgr::Find("HUD");
80 
82  if (hud)
83  SetColor(hud->GetTextColor());
84 }
85 
87 {
88  quantum_view = 0;
89 }
90 
91 void
93 {
94  width = window->Width();
95  height = window->Height();
96  xcenter = (width / 2.0) - 0.5;
97  ycenter = (height / 2.0) + 0.5;
98 }
99 
100 // +--------------------------------------------------------------------+
101 
102 bool
104 {
105  if (obj == ship)
106  ship = 0;
107 
108  return SimObserver::Update(obj);
109 }
110 
111 const char*
113 {
114  return "QuantumView";
115 }
116 
117 // +--------------------------------------------------------------------+
118 
119 void
121 {
122  sim = Sim::GetSim();
123 
124  if (sim && ship != sim->GetPlayerShip()) {
125  ship = sim->GetPlayerShip();
126 
127  if (ship) {
128  if (ship->Life() == 0 || ship->IsDying() || ship->IsDead()) {
129  ship = 0;
130  }
131  else {
132  Observe(ship);
133  }
134  }
135  }
136 
137  if (IsMenuShown()) {
138  Rect menu_rect(width-115, 10, 115, 12);
139 
140  font->SetColor(hud_color);
141  font->SetAlpha(1);
142  font->DrawText(quantum_menu->GetTitle(), 0, menu_rect, DT_CENTER);
143 
144  menu_rect.y += 15;
145 
146  ListIter<MenuItem> item = quantum_menu->GetItems();
147  while (++item) {
148  item->SetEnabled(true);
149 
150  font->DrawText(item->GetText(), 0, menu_rect, DT_LEFT);
151  menu_rect.y += 10;
152  }
153  }
154 }
155 
156 // +--------------------------------------------------------------------+
157 
158 void
160 {
161  HUDView* hud = HUDView::GetInstance();
162  if (hud) {
163  if (hud_color != hud->GetTextColor()) {
164  hud_color = hud->GetTextColor();
165  SetColor(hud_color);
166  }
167  }
168 
169  static double time_til_change = 0;
170 
171  if (time_til_change > 0)
172  time_til_change -= Game::GUITime();
173 
174  if (time_til_change <= 0) {
175  time_til_change = 0;
176 
177  if (show_menu) {
178  QuantumDrive* quantum_drive = 0;
179 
180  if (ship)
181  quantum_drive = ship->GetQuantumDrive();
182 
183  if (quantum_drive && quantum_drive->ActiveState() != QuantumDrive::ACTIVE_READY) {
184  show_menu = false;
185  return;
186  }
187 
188  int max_items = quantum_menu->NumItems();
189 
190  for (int i = 0; i < max_items; i++) {
191  if (Keyboard::KeyDown('1' + i)) {
192  MenuItem* item = quantum_menu->GetItem(i);
193  if (item && item->GetEnabled()) {
194 
195  SimRegion* rgn = (SimRegion*) item->GetData();
196 
197  if (rgn) {
198  quantum_drive->SetDestination(rgn, Point(0,0,0));
199  quantum_drive->Engage();
200  }
201 
202  show_menu = false;
203  time_til_change = 0.3;
204  break;
205  }
206  }
207  }
208  }
209  }
210 }
211 
212 // +--------------------------------------------------------------------+
213 
214 void
216 {
217  hud_color = c;
218 }
219 
220 // +--------------------------------------------------------------------+
221 
222 bool
224 {
225  return show_menu;
226 }
227 
228 void
230 {
231  if (!ship) return;
232 
233  if (!show_menu) {
234  if (ship->IsStarship() && ship->GetQuantumDrive()) {
236  show_menu = true;
237  }
238 
239  for (int i = 0; i < 10; i++) {
240  if (Keyboard::KeyDown('1' + i)) {
241  // just need to clear the key down flag
242  // so we don't process old keystrokes
243  // as valid menu picks...
244  }
245  }
246  }
247 }
248 
249 void
251 {
252  show_menu = false;
253 }
254 
255 // +--------------------------------------------------------------------+
256 
257 Menu*
259 {
260  if (s && sim) {
261  if (s->IsStarship()) {
262  quantum_menu->ClearItems();
263 
264  SimRegion* current_region = ship->GetRegion();
265 
266  if (!current_region) return 0;
267 
268  StarSystem* current_system = current_region->System();
269 
270  List<SimRegion> rgn_list;
271 
273  while (++iter) {
274  SimRegion* rgn = iter.value();
275  StarSystem* rgn_system = rgn->System();
276 
277  if (rgn != ship->GetRegion() && !rgn->IsAirSpace() &&
278  rgn_system == current_system) {
279  rgn_list.append(rgn);
280  }
281  }
282 
283  // sort local regions by distance from star:
284  rgn_list.sort();
285 
286  // now add regions in other star systems:
287  iter.reset();
288  while (++iter) {
289  SimRegion* rgn = iter.value();
290  StarSystem* rgn_system = rgn->System();
291 
292  if (rgn != ship->GetRegion() && rgn->Type() != SimRegion::AIR_SPACE &&
293  rgn_system != current_system && current_region->Links().contains(rgn)) {
294  rgn_list.append(rgn);
295  }
296  }
297 
298  int n = 1;
299  iter.attach(rgn_list);
300  while (++iter) {
301  SimRegion* rgn = iter.value();
302  StarSystem* rgn_system = rgn->System();
303  char text[64];
304 
305  if (rgn_system != current_system)
306  sprintf_s(text, "%d. %s/%s", n++, rgn_system->Name(), rgn->Name());
307  else
308  sprintf_s(text, "%d. %s", n++, rgn->Name());
309 
310  quantum_menu->AddItem(text, (DWORD) rgn);
311  }
312 
313  return quantum_menu;
314  }
315  }
316 
317  return 0;
318 }