Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/hud.cc @ 10656

Last change on this file since 10656 was 10516, checked in by patrick, 18 years ago

merged playability back to trunk

File size: 16.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "hud.h"
19
20#include "state.h"
21#include "debug.h"
22
23#include "world_entities/weapons/weapon_manager.h"
24#include "glgui_widget.h"
25#include "glgui_box.h"
26#include "glgui_bar.h"
27#include "elements/glgui_energywidgetvertical.h"
28
29#include "glgui_inputline.h"
30#include "specials/glgui_notifier.h"
31#include "elements/glgui_radar.h"
32#include "world_entities/space_ships/space_ship.h"
33
34
35
36/// HACK
37#include "player.h"
38#include "playable.h"
39
40ObjectListDefinition(Hud);
41/**
42 * standard constructor
43 * @todo this constructor is not jet implemented - do it
44*/
45Hud::Hud ()
46{
47  this->registerObject(this, Hud::_objectList);
48
49  //this->setSize2D(
50  this->weaponManager = NULL;
51  this->weaponManagerSecondary = NULL;
52  this->energyWidget = NULL;
53  this->shieldWidget = NULL;
54  this->armorWidget = NULL;
55  //this->leftRect = NULL;
56  //this->rightRect = NULL;
57  this->resX = 1;
58  this->resY = 1;
59
60  this->overlayPercentage = 40;
61  this->overlayActive = false;
62  this->rightRect = new OrxGui::GLGuiImage();
63  this->leftRect = new OrxGui::GLGuiImage();
64
65  this->inputLine = new OrxGui::GLGuiInputLine();
66  this->inputLine->setParent2D(this);
67  this->notifier = new OrxGui::GLGuiNotifier();
68  this->notifier->setParent2D(this);
69  notifier->setAbsCoor2D(100,100);
70
71  this->_radar = new OrxGui::GLGuiRadar();
72  this->radarCenterNode = NULL;
73
74  this->subscribeEvent(ES_ALL, EV_VIDEO_RESIZE);
75  this->subscribeEvent(ES_ALL, SDLK_TAB);
76
77  //this->shipValuesBox = NULL;
78}
79
80
81/**
82 * standard deconstructor
83*/
84Hud::~Hud ()
85{
86  delete this->inputLine;
87  delete this->notifier;
88
89  delete this->_radar;
90  delete this->rightRect;
91  delete this->leftRect;
92  //if (this->shipValuesBox != NULL)
93    //delete this->shipValuesBox;
94
95  // delete what has to be deleted here
96}
97
98
99void Hud::loadParams(const TiXmlElement* root)
100{
101  Element2D::loadParams(root);
102}
103
104void Hud::notifyUser(const std::string& message)
105{
106  this->notifier->pushNotifyMessage(message);
107}
108
109void Hud::setBackGround()
110{}
111
112void Hud::setEnergyWidget(OrxGui::GLGuiWidget* widget)
113{
114  //if (this->shipValuesBox == NULL)
115    //this->createShipValuesBox();
116
117  // decopple old widget
118  if (this->energyWidget != NULL)
119  {
120    this->energyWidget->hide();
121  }
122
123  this->energyWidget = widget;
124  if (this->energyWidget != NULL)
125  {
126    //this->energyWidget->shiftDir2D(270);
127    //dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (this->energyWidget)->setDisplayedName("Electronics");
128    //this->shipValuesBox->pack(this->energyWidget);
129    //dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (this->energyWidget)->setStandardSettings();
130    this->energyWidget->setParent2D(this->leftRect);
131    dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (this->energyWidget)->setDisplayedImage("textures/gui/gui_electronics_icon.png");
132    this->energyWidget->show();
133    /*    this->energyWidget->frontMaterial().setDiffuseMap("hud_energy_bar.png");
134        this->energyWidget->frontMaterial().setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
135  }
136
137  this->updateResolution();
138}
139
140void Hud::setShiledWidget(OrxGui::GLGuiWidget* widget)
141{
142  /*
143  if (this->shipValuesBox == NULL)
144    this->createShipValuesBox();
145  */
146
147  // decopple old widget
148  if (this->shieldWidget != NULL)
149  {
150    this->shieldWidget->hide();
151  }
152
153  this->shieldWidget = widget;
154  if (this->shieldWidget != NULL)
155  {
156    //this->shieldWidget->shiftDir2D(270);
157    //this->shipValuesBox->pack(this->shieldWidget);
158    //dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (this->shieldWidget)->setStandardSettings();
159    this->shieldWidget->setParent2D(this->leftRect);
160    dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (this->shieldWidget)->setDisplayedImage("textures/gui/gui_shield_icon.png");
161    this->shieldWidget->show();
162    /*    this->shieldWidget->frontMaterial().setDiffuseMap("hud_energy_bar.png");
163        this->shieldWidget->frontMaterial().setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
164  }
165  else
166    printf("schild im hud nicht uebergeben!!!!!!!!!!!!!!!!!!!!!!!!!");
167
168  this->updateResolution();
169}
170
171void Hud::setArmorWidget(OrxGui::GLGuiWidget* widget)
172{
173  /*
174  if (this->shipValuesBox == NULL)
175    this->createShipValuesBox();
176  */
177
178// decopple old widget
179  if (this->armorWidget != NULL)
180  {
181    this->armorWidget->hide();
182  }
183
184  this->armorWidget = widget;
185  if (this->armorWidget != NULL)
186  {
187    //this->armorWidget->shiftDir2D(270);
188    //this->shipValuesBox->pack(this->armorWidget);
189    //dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (this->armorWidget)->setStandardSettings();
190    this->armorWidget->setParent2D(this->leftRect);
191    dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (this->armorWidget)->setDisplayedImage("textures/gui/gui_health_icon.png");
192    this->armorWidget->show();
193    /*    this->armorWidget->frontMaterial().setDiffuseMap("hud_energy_bar.png");
194        this->armorWidget->frontMaterial().setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
195  }
196
197  this->updateResolution();
198}
199
200void Hud::setWeaponManager(WeaponManager* weaponMan, WeaponManager* weaponManSec)
201{
202  //clearWeaponManager();
203
204  //Hide all widgets
205  if (this->weaponManager != NULL)
206  {
207    for (unsigned int i = 0; i < this->weaponManager->getSlotCount(); i++)
208    {
209      Weapon* weapon = this->weaponManager->getWeapon(i);
210      if (weapon != NULL)
211      {
212        weapon->getEnergyWidget()->hide();
213        //this->weaponsWidgetsPrim.remove(dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (weapon->getEnergyWidget()));
214      }
215    }
216  }
217
218  if (this->weaponManagerSecondary != NULL)
219  {
220    for (unsigned int i = 0; i < this->weaponManagerSecondary->getSlotCount(); i++)
221    {
222      Weapon* weapon = this->weaponManagerSecondary->getWeapon(i);
223      if (weapon != NULL)
224      {
225        weapon->getEnergyWidget()->hide();
226        //this->weaponsWidgetsSec.remove(dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (weapon->getEnergyWidget()));
227      }
228    }
229  }
230
231  this->weaponManager = weaponMan;
232  this->weaponManagerSecondary = weaponManSec; 
233
234  this->updateWeaponManager();
235  //  this->updateResolution();
236}
237
238/*
239void Hud::clearWeaponManager()
240{
241  //Hide all widgets
242  if (this->weaponManager != NULL)
243  {
244    for (unsigned int i = 0; i < this->weaponManager->getSlotCount(); i++)
245    {
246      Weapon* weapon = this->weaponManager->getWeapon(i);
247      if (weapon != NULL)
248      {
249        weapon->getEnergyWidget()->hide();
250        //this->weaponsWidgetsPrim.remove(dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (weapon->getEnergyWidget()));
251      }
252    }
253  }
254
255  if (this->weaponManagerSecondary != NULL)
256  {
257    for (unsigned int i = 0; i < this->weaponManagerSecondary->getSlotCount(); i++)
258    {
259      Weapon* weapon = this->weaponManagerSecondary->getWeapon(i);
260      if (weapon != NULL)
261      {
262        weapon->getEnergyWidget()->hide();
263        //this->weaponsWidgetsSec.remove(dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (weapon->getEnergyWidget()));
264      }
265    }
266  }
267
268  //this->weaponsWidgetsPrim.clear();
269  //this->weaponsWidgetsSec.clear();
270}
271*/
272
273void Hud::updateWeaponManager()
274{
275  // hide all the Widgets
276 
277  std::list<OrxGui::GLGuiEnergyWidgetVertical*>::iterator weaponWidget;
278  for (weaponWidget = this->weaponsWidgetsPrim.begin(); weaponWidget != this->weaponsWidgetsPrim.end(); weaponWidget++)
279  {
280    (*weaponWidget)->hide();
281  }
282  this->weaponsWidgetsPrim.clear();
283
284  for (weaponWidget = this->weaponsWidgetsSec.begin(); weaponWidget != this->weaponsWidgetsSec.end(); weaponWidget++)
285  {
286    (*weaponWidget)->hide();
287  }
288  this->weaponsWidgetsSec.clear();
289 
290
291  // add all that we need again.
292
293  if (this->weaponManager != NULL)
294    for (unsigned int i = 0; i < this->weaponManager->getSlotCount(); i++)
295    {
296      Weapon* weapon = this->weaponManager->getWeapon(i);
297      if (weapon != NULL)
298      {
299        //PRINTF(0)("WEAPON %s::%s in Slots\n", weapon->getClassCName(), weapon->getName());
300        weapon->getEnergyWidget()->setParent2D(this->rightRect);
301        weapon->getEnergyWidget()->show();
302        weapon->getEnergyWidget()->setBackgroundColor(Color(.8,.2,.11, 0.1));
303        weapon->getEnergyWidget()->setFrontColor(Color( .2,.5,.7,.6));
304        weapon->getEnergyWidget()->setWidgetSize(120,30);
305        //weapon->getEnergyWidget()->frontMaterial().setTransparency(.6);
306        this->weaponsWidgetsPrim.push_back(dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (weapon->getEnergyWidget()));
307      }
308    }
309
310  if (this->weaponManagerSecondary != NULL)
311    for (unsigned int i = 0; i < this->weaponManagerSecondary->getSlotCount(); i++)
312    {
313      Weapon* weapon = this->weaponManagerSecondary->getWeapon(i);
314      if (weapon != NULL)
315      {
316        //PRINTF(0)("WEAPON %s::%s in Slots\n", weapon->getClassCName(), weapon->getName());
317        weapon->getEnergyWidget()->setParent2D(this->rightRect);
318        weapon->getEnergyWidget()->show();
319        weapon->getEnergyWidget()->setBackgroundColor(Color(.8,.2,.11, 0.1));
320        weapon->getEnergyWidget()->setFrontColor(Color( .2,.5,.7,.6));
321        weapon->getEnergyWidget()->setWidgetSize(150,50);
322        //weapon->getEnergyWidget()->frontMaterial().setTransparency(.6);
323        this->weaponsWidgetsSec.push_back(dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (weapon->getEnergyWidget()));
324      }
325    }
326
327  this->updateResolution();
328
329}
330
331void Hud::addWeaponWidget(OrxGui::GLGuiWidget* widget)
332{}
333
334void Hud::removeWeaponWidget(OrxGui::GLGuiWidget* widget)
335{}
336
337void Hud::updateResolution()
338{
339  this->resX = State::getResX();
340  this->resY = State::getResY();
341
342  this->setSize2D(.2 * this->resX, this->resY);
343  this->notifier->setAbsCoor2D(0.7 * this->resX, 0.3 * this->resY);
344  this->notifier->setWidgetSize(0.25 * this->resX, 0.6 * this->resY);
345
346  int overlayWidth = 0;
347  if (overlayPercentage >= 20)
348    overlayWidth = this->resX * (overlayPercentage)/(200);
349  else
350    overlayWidth = this->resX * 0.1;
351  //if (overlayWidth < 100)
352    //overlayWidth = 100;
353
354  this->rightRect->hide();
355  this->leftRect->hide();
356
357  this->leftRect->setParent2D(this);
358  this->leftRect->setWidgetSize(float(overlayWidth), float(this->resY));
359  this->leftRect->setAbsCoor2D(0,0);
360  this->leftRect->setBackgroundTexture(Texture());
361  this->leftRect->setBackgroundColor(Color(0,0,0.7,0.2));
362  this->leftRect->setForegroundTexture(Texture());
363  this->leftRect->setForegroundColor(Color(0,0,0,0));
364
365  this->rightRect->setParent2D(this);
366  this->rightRect->setWidgetSize(float(overlayWidth), float(this->resY));
367  this->rightRect->setAbsCoor2D(this->resX - overlayWidth,0);
368  this->rightRect->setBackgroundTexture(Texture());
369  this->rightRect->setBackgroundColor(Color(0,0,0.7,0.2));
370  this->rightRect->setForegroundTexture(Texture());
371  this->rightRect->setForegroundColor(Color(0,0,0,0));
372
373
374  if (this->overlayActive == true)
375  {
376    this->rightRect->show();
377    this->leftRect->show();
378  }
379
380
381  if (State::getPlayer() && State::getPlayer()->getPlayable() && State::getObjectManager())
382  {
383    PRINTF(4)("UPDATING RADAR\n");
384    this->_radar->setParent2D(this->leftRect);
385    if (radarCenterNode == NULL)
386      this->_radar->setCenterNode(State::getPlayer()->getPlayable());
387    else
388      this->_radar->setCenterNode(this->radarCenterNode);
389
390    //this->_radar->addEntityList(&State::getObjectManager()->getEntityList((OM_LIST)(State::getPlayer()->getPlayable()->getOMListNumber()+1)), Color(.4, .4, 1.0));
391    this->_radar->addEntityList(&State::getObjectManager()->getEntityList(OM_GROUP_00), Color(1, 0, 0));
392    this->_radar->addEntityList(&State::getObjectManager()->getEntityList(OM_GROUP_01), Color(0, 0, 1));
393    this->_radar->setAbsCoor2D(0, 0.01 * this->resY);
394    this->_radar->setWidgetSize(overlayWidth, overlayWidth);
395    this->_radar->setRange(300);
396    this->_radar->show();
397   
398    int statWidgetsNumber = 0;
399    float expectedArmorSizeX = 0;
400    float expectedArmorSizeY = 0;
401    float newSizeY = 0; float newSizeX = 0;
402    float moduloWidth = 0;
403
404    if (this->armorWidget != NULL)
405    {
406      expectedArmorSizeX = 150;
407      expectedArmorSizeY = 50;
408      statWidgetsNumber++;
409    }
410
411    if (this->shieldWidget != NULL)
412      statWidgetsNumber++;
413   
414    if (this->energyWidget != NULL)
415      statWidgetsNumber++;
416
417    if (expectedArmorSizeY * statWidgetsNumber > overlayWidth)
418    {
419      newSizeY = overlayWidth / float(statWidgetsNumber);
420      newSizeX = expectedArmorSizeX;
421      PRINTF(0)("Statwidgets resized\n");
422    }
423    else
424    {
425      newSizeY = expectedArmorSizeY;
426      newSizeX = expectedArmorSizeX;
427      moduloWidth = int(overlayWidth) % int(expectedArmorSizeY * statWidgetsNumber);
428    }
429
430    float posY = overlayWidth + newSizeX;
431
432    if (this->armorWidget != NULL)
433    {
434      this->armorWidget->setSize2D(newSizeX, newSizeY);
435      this->armorWidget->setRelCoor2D((statWidgetsNumber - 1) * newSizeY + 3 * moduloWidth / (statWidgetsNumber + 1),posY);
436    }
437    if (this->shieldWidget != NULL)
438    {
439      this->shieldWidget->setSize2D(newSizeX, newSizeY);
440      this->shieldWidget->setRelCoor2D((statWidgetsNumber - 2) * newSizeY + 2 *moduloWidth / (statWidgetsNumber + 1),posY);
441    }
442    if (this->energyWidget != NULL)
443    {
444      this->energyWidget->setSize2D(newSizeX, newSizeY);
445      this->energyWidget->setRelCoor2D(moduloWidth / (statWidgetsNumber + 1),posY);
446    }
447
448    /*
449    if (this->armorWidget != NULL)
450      this->armorWidget->setRelCoor2D(100,0.2*this->resY + this->armorWidget->getSizeX2D());
451    if (this->shieldWidget != NULL)
452      this->shieldWidget->setRelCoor2D(60,0.2*this->resY + this->armorWidget->getSizeX2D());
453    if (this->energyWidget != NULL)
454      this->energyWidget->setRelCoor2D(20,0.2*this->resY + this->armorWidget->getSizeX2D());
455    */
456    //this->shieldWidget->setRelCoor2D(0.1*this->resX + this->armorWidget->getSizeX2D(),0);
457    //this->energyWidget->setRelCoor2D(0.1*this->resX + this->armorWidget->getSizeX2D() + this->shieldWidget->getSizeX2D(),0);
458  }
459
460  /*
461  if (this->shipValuesBox != NULL)
462  {
463    this->shipValuesBox->setAbsCoor2D(0.2 * this->resX, 0.4 * this->resY);
464    this->shipValuesBox->setWidgetSize(.4 * this->resX, 0.1 * this->resY);
465  }
466  else
467    createShipValuesBox();
468  */
469
470  std::list<OrxGui::GLGuiEnergyWidgetVertical*>::iterator weaponWidget;
471  Vector2D pos = Vector2D(overlayWidth, 0.4*this->resY);
472  float largestWidgetSizeX = 0;
473  //PRINTF(0)("Cur Pos: %f,%f\n",pos.x,pos.y);
474  // out of reasons i can't get behind, this version is segfaulting when calling getSizeX2D or getSizeY2D. the other
475  // element2D- related function works tough.. :s
476
477  for (weaponWidget = this->weaponsWidgetsPrim.begin(); weaponWidget != this->weaponsWidgetsPrim.end(); weaponWidget++)
478  {
479    float ySize = (*weaponWidget)->getSizeY2D();
480    float xSize = (*weaponWidget)->getSizeX2D();
481    if (xSize > largestWidgetSizeX)
482      largestWidgetSizeX = xSize;
483    if (pos.x < ySize)
484    {
485      pos.x = overlayWidth;
486      pos.y += largestWidgetSizeX;
487    }
488    pos.x -= ySize;
489    (*weaponWidget)->setAbsCoor2D(pos.x + this->rightRect->getAbsCoor2D().x, pos.y);
490    //(*weaponWidget)->setAbsCoor2D(0,100);
491    (*weaponWidget)->show();
492    //printf("update thing %s::%s\n", (*weaponWidget)->getClassCName(), (*weaponWidget)->getName());
493  }
494 
495  weaponWidget = this->weaponsWidgetsSec.begin();
496  float expectedWidgetSizeY = 0;
497  if (weaponWidget != this->weaponsWidgetsSec.end())
498  {
499    expectedWidgetSizeY = (*weaponWidget)->getSizeY2D();
500  }
501  pos.y = resY - expectedWidgetSizeY * 0.6;
502  pos.x = overlayWidth + this->rightRect->getAbsCoor2D().x;
503
504  for (weaponWidget = this->weaponsWidgetsSec.begin(); weaponWidget != this->weaponsWidgetsSec.end(); weaponWidget++)
505  {
506    float ySize = (*weaponWidget)->getSizeY2D();
507    float xSize = (*weaponWidget)->getSizeX2D();
508    if (xSize > largestWidgetSizeX)
509      largestWidgetSizeX = xSize;
510    if (pos.x < ySize)
511    {
512      pos.x = overlayWidth;
513      pos.y -= largestWidgetSizeX + expectedWidgetSizeY * 0.6;
514    }
515    pos.x -= ySize;
516    //PRINTF(0)("secweaponwidget y-size: %f/n", (*weaponWidget)->getSizeY2D());
517    (*weaponWidget)->setAbsCoor2D(pos.x, pos.y);//+this->rightRect->getAbsCoor2D().x, pos.y);
518    (*weaponWidget)->show();
519    //printf("update thing %s::%s\n", (*weaponWidget)->getClassCName(), (*weaponWidget)->getName());
520  }
521
522}
523
524void Hud::draw() const
525{
526  //  GLGuiWidget::draw();
527}
528
529
530void Hud::process(const Event &event)
531{
532  if (event.type == EV_VIDEO_RESIZE)
533    this->updateResolution();
534  else if (event.type == SDLK_TAB)
535  {
536    /// TODO SHOW THE INPUT-LINE
537  //  this->inputLine->select();
538  }
539}
540
541/*
542void Hud::createShipValuesBox()
543{
544  this->shipValuesBox = new OrxGui::GLGuiBox(OrxGui::Vertical);
545  //this->shipValuesBox->setWidgetSize(1000,500);
546  //this->shipValuesBox->setBackgroundTexture("textures/gui_container_background.png");
547  this->shipValuesBox->setBackgroundTexture(Texture());
548  this->shipValuesBox->setBackgroundColor(Color(0,0,1,0.5));
549  this->shipValuesBox->setVisibility(true);
550}
551*/
552
553
Note: See TracBrowser for help on using the repository browser.