Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged playability back to trunk

File size: 16.9 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[6437]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[6437]18#include "hud.h"
[1853]19
[6441]20#include "state.h"
[8976]21#include "debug.h"
[6441]22
[6442]23#include "world_entities/weapons/weapon_manager.h"
[8448]24#include "glgui_widget.h"
[10368]25#include "glgui_box.h"
26#include "glgui_bar.h"
27#include "elements/glgui_energywidgetvertical.h"
[6442]28
[8518]29#include "glgui_inputline.h"
30#include "specials/glgui_notifier.h"
[8994]31#include "elements/glgui_radar.h"
[10368]32#include "world_entities/space_ships/space_ship.h"
[8518]33
[8996]34
35
36/// HACK
37#include "player.h"
38#include "playable.h"
39
[9869]40ObjectListDefinition(Hud);
[3245]41/**
[4838]42 * standard constructor
43 * @todo this constructor is not jet implemented - do it
[3245]44*/
[6437]45Hud::Hud ()
[3365]46{
[9869]47  this->registerObject(this, Hud::_objectList);
[4320]48
[6441]49  //this->setSize2D(
[6442]50  this->weaponManager = NULL;
[10368]51  this->weaponManagerSecondary = NULL;
[6440]52  this->energyWidget = NULL;
53  this->shieldWidget = NULL;
54  this->armorWidget = NULL;
[10368]55  //this->leftRect = NULL;
56  //this->rightRect = NULL;
[6441]57  this->resX = 1;
58  this->resY = 1;
[6442]59
[10368]60  this->overlayPercentage = 40;
61  this->overlayActive = false;
62  this->rightRect = new OrxGui::GLGuiImage();
63  this->leftRect = new OrxGui::GLGuiImage();
64
[8518]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
[8995]71  this->_radar = new OrxGui::GLGuiRadar();
[10368]72  this->radarCenterNode = NULL;
[8994]73
[8990]74  this->subscribeEvent(ES_ALL, EV_VIDEO_RESIZE);
[9003]75  this->subscribeEvent(ES_ALL, SDLK_TAB);
[8518]76
[10368]77  //this->shipValuesBox = NULL;
[3365]78}
[1853]79
80
[3245]81/**
[4838]82 * standard deconstructor
[3245]83*/
[6437]84Hud::~Hud ()
[3543]85{
[8518]86  delete this->inputLine;
87  delete this->notifier;
[8994]88
[8995]89  delete this->_radar;
[10368]90  delete this->rightRect;
91  delete this->leftRect;
92  //if (this->shipValuesBox != NULL)
93    //delete this->shipValuesBox;
94
[3543]95  // delete what has to be deleted here
96}
[6438]97
98
99void Hud::loadParams(const TiXmlElement* root)
100{
[6512]101  Element2D::loadParams(root);
[6438]102}
103
[8518]104void Hud::notifyUser(const std::string& message)
[6438]105{
[8518]106  this->notifier->pushNotifyMessage(message);
[6438]107}
108
[8518]109void Hud::setBackGround()
110{}
111
[8145]112void Hud::setEnergyWidget(OrxGui::GLGuiWidget* widget)
[6438]113{
[10368]114  //if (this->shipValuesBox == NULL)
115    //this->createShipValuesBox();
116
[6440]117  // decopple old widget
118  if (this->energyWidget != NULL)
119  {
120    this->energyWidget->hide();
121  }
[6438]122
[6440]123  this->energyWidget = widget;
124  if (this->energyWidget != NULL)
125  {
[10368]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);
[10449]131    dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (this->energyWidget)->setDisplayedImage("textures/gui/gui_electronics_icon.png");
[6440]132    this->energyWidget->show();
[8518]133    /*    this->energyWidget->frontMaterial().setDiffuseMap("hud_energy_bar.png");
134        this->energyWidget->frontMaterial().setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/
[6440]135  }
136
[6441]137  this->updateResolution();
[6438]138}
139
[8145]140void Hud::setShiledWidget(OrxGui::GLGuiWidget* widget)
[10368]141{
142  /*
143  if (this->shipValuesBox == NULL)
144    this->createShipValuesBox();
145  */
[6438]146
[10368]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);
[10449]160    dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (this->shieldWidget)->setDisplayedImage("textures/gui/gui_shield_icon.png");
[10368]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
[8145]171void Hud::setArmorWidget(OrxGui::GLGuiWidget* widget)
[10368]172{
173  /*
174  if (this->shipValuesBox == NULL)
175    this->createShipValuesBox();
176  */
[6438]177
[10368]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);
[10449]191    dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (this->armorWidget)->setDisplayedImage("textures/gui/gui_health_icon.png");
[10368]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)
[6442]201{
[10368]202  //clearWeaponManager();
203
204  //Hide all widgets
[6442]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();
[10368]213        //this->weaponsWidgetsPrim.remove(dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (weapon->getEnergyWidget()));
[6442]214      }
215    }
216  }
217
[10368]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
[6442]231  this->weaponManager = weaponMan;
[10368]232  this->weaponManagerSecondary = weaponManSec; 
[6442]233
[6445]234  this->updateWeaponManager();
[8996]235  //  this->updateResolution();
[6438]236}
237
[10368]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
[6443]273void Hud::updateWeaponManager()
274{
275  // hide all the Widgets
[10368]276 
277  std::list<OrxGui::GLGuiEnergyWidgetVertical*>::iterator weaponWidget;
278  for (weaponWidget = this->weaponsWidgetsPrim.begin(); weaponWidget != this->weaponsWidgetsPrim.end(); weaponWidget++)
[6443]279  {
280    (*weaponWidget)->hide();
281  }
[10368]282  this->weaponsWidgetsPrim.clear();
[6442]283
[10368]284  for (weaponWidget = this->weaponsWidgetsSec.begin(); weaponWidget != this->weaponsWidgetsSec.end(); weaponWidget++)
285  {
286    (*weaponWidget)->hide();
287  }
288  this->weaponsWidgetsSec.clear();
289 
290
[6443]291  // add all that we need again.
[10368]292
[6443]293  if (this->weaponManager != NULL)
294    for (unsigned int i = 0; i < this->weaponManager->getSlotCount(); i++)
295    {
[8518]296      Weapon* weapon = this->weaponManager->getWeapon(i);
297      if (weapon != NULL)
298      {
[9406]299        //PRINTF(0)("WEAPON %s::%s in Slots\n", weapon->getClassCName(), weapon->getName());
[10368]300        weapon->getEnergyWidget()->setParent2D(this->rightRect);
[8518]301        weapon->getEnergyWidget()->show();
[8619]302        weapon->getEnergyWidget()->setBackgroundColor(Color(.8,.2,.11, 0.1));
[8518]303        weapon->getEnergyWidget()->setFrontColor(Color( .2,.5,.7,.6));
[10368]304        weapon->getEnergyWidget()->setWidgetSize(120,30);
305        //weapon->getEnergyWidget()->frontMaterial().setTransparency(.6);
306        this->weaponsWidgetsPrim.push_back(dynamic_cast<OrxGui::GLGuiEnergyWidgetVertical*> (weapon->getEnergyWidget()));
[8518]307      }
[6443]308    }
[10368]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
[6443]327  this->updateResolution();
[10368]328
[6443]329}
330
[8145]331void Hud::addWeaponWidget(OrxGui::GLGuiWidget* widget)
[8518]332{}
[6438]333
[8145]334void Hud::removeWeaponWidget(OrxGui::GLGuiWidget* widget)
[8518]335{}
[6438]336
[6441]337void Hud::updateResolution()
338{
[6498]339  this->resX = State::getResX();
340  this->resY = State::getResY();
[8976]341
342  this->setSize2D(.2 * this->resX, this->resY);
[8989]343  this->notifier->setAbsCoor2D(0.7 * this->resX, 0.3 * this->resY);
344  this->notifier->setWidgetSize(0.25 * this->resX, 0.6 * this->resY);
[8976]345
[10368]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;
[8995]353
[10368]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
[8996]381  if (State::getPlayer() && State::getPlayer()->getPlayable() && State::getObjectManager())
382  {
[9869]383    PRINTF(4)("UPDATING RADAR\n");
[10368]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);
[9014]395    this->_radar->setRange(300);
[8996]396    this->_radar->show();
[10368]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);
[8996]458  }
459
[10368]460  /*
461  if (this->shipValuesBox != NULL)
[6441]462  {
[10368]463    this->shipValuesBox->setAbsCoor2D(0.2 * this->resX, 0.4 * this->resY);
464    this->shipValuesBox->setWidgetSize(.4 * this->resX, 0.1 * this->resY);
[6441]465  }
[10368]466  else
467    createShipValuesBox();
468  */
[6442]469
[10368]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
[6442]476
[10368]477  for (weaponWidget = this->weaponsWidgetsPrim.begin(); weaponWidget != this->weaponsWidgetsPrim.end(); weaponWidget++)
[6442]478  {
[10368]479    float ySize = (*weaponWidget)->getSizeY2D();
480    float xSize = (*weaponWidget)->getSizeX2D();
481    if (xSize > largestWidgetSizeX)
482      largestWidgetSizeX = xSize;
483    if (pos.x < ySize)
[8987]484    {
[10368]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;
[8987]503
[10368]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;
[8987]514    }
[10368]515    pos.x -= ySize;
516    //PRINTF(0)("secweaponwidget y-size: %f/n", (*weaponWidget)->getSizeY2D());
[10516]517    (*weaponWidget)->setAbsCoor2D(pos.x, pos.y);//+this->rightRect->getAbsCoor2D().x, pos.y);
[8976]518    (*weaponWidget)->show();
[9406]519    //printf("update thing %s::%s\n", (*weaponWidget)->getClassCName(), (*weaponWidget)->getName());
[6442]520  }
[10368]521
[6441]522}
523
[8990]524void Hud::draw() const
[6441]525{
[8990]526  //  GLGuiWidget::draw();
[6441]527}
528
[8990]529
530void Hud::process(const Event &event)
[6441]531{
[8990]532  if (event.type == EV_VIDEO_RESIZE)
533    this->updateResolution();
[9003]534  else if (event.type == SDLK_TAB)
535  {
536    /// TODO SHOW THE INPUT-LINE
537  //  this->inputLine->select();
538  }
[10368]539}
[9003]540
[10368]541/*
542void Hud::createShipValuesBox()
543{
544  this->shipValuesBox = new OrxGui::GLGuiBox(OrxGui::Vertical);
545  //this->shipValuesBox->setWidgetSize(1000,500);
[10420]546  //this->shipValuesBox->setBackgroundTexture("textures/gui_container_background.png");
[10368]547  this->shipValuesBox->setBackgroundTexture(Texture());
548  this->shipValuesBox->setBackgroundColor(Color(0,0,1,0.5));
549  this->shipValuesBox->setVisibility(true);
[6441]550}
[10368]551*/
[6441]552
553
Note: See TracBrowser for help on using the repository browser.