Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/overlays/OrxonoxOverlay.cc @ 8881

Last change on this file since 8881 was 8858, checked in by landauf, 13 years ago

merged output branch back to trunk.

Changes:

  • you have to include util/Output.h instead of util/Debug.h
  • COUT(x) is now called orxout(level)
  • output levels are now defined by an enum instead of numbers. see util/Output.h for the definition
  • it's possible to use output contexts with orxout(level, context). see util/Output.h for some common contexts. you can define more contexts
  • you must use 'endl' at the end of an output message, '\n' does not flush the message

Output levels:

  • instead of COUT(0) use orxout()
  • instead of COUT(1) use orxout(user_error) or orxout(internal_error)
  • instead of COUT(2) use orxout(user_warning) or orxout(internal_warning)
  • instead of COUT(3) use orxout(user_status/user_info) or orxout(internal_status/internal_info)
  • instead of COUT(4) use orxout(verbose)
  • instead of COUT(5) use orxout(verbose_more)
  • instead of COUT(6) use orxout(verbose_ultra)

Guidelines:

  • user_* levels are for the user, visible in the console and the log-file
  • internal_* levels are for developers, visible in the log-file
  • verbose_* levels are for debugging, only visible if the context of the output is activated

Usage in C++:

  • orxout() << "message" << endl;
  • orxout(level) << "message" << endl;
  • orxout(level, context) << "message" << endl;

Usage in Lua:

  • orxout("message")
  • orxout(orxonox.level.levelname, "message")
  • orxout(orxonox.level.levelname, "context", "message")

Usage in Tcl (and in the in-game-console):

  • orxout levelname message
  • orxout_context levelname context message
  • shortcuts: log message, error message, warning message, status message, info message, debug message
  • Property svn:eol-style set to native
File size: 15.6 KB
RevLine 
[1588]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[1622]29/**
30@file
31@brief Definition of the OrxonoxOverlay class.
32*/
33
[1601]34#include "OrxonoxOverlay.h"
[1588]35
[1615]36#include <cmath>
[1622]37#include <OgreOverlay.h>
[1588]38#include <OgreOverlayManager.h>
[1614]39#include <OgrePanelOverlayElement.h>
[2896]40#include <OgreRenderWindow.h>
[6417]41#include <OgreMaterialManager.h>
42#include <OgreTechnique.h>
43#include <OgrePass.h>
[2896]44
[1588]45#include "util/Convert.h"
[2171]46#include "util/Exception.h"
[2896]47#include "core/GameMode.h"
[1588]48#include "core/CoreIncludes.h"
[1616]49#include "core/XMLPort.h"
[7284]50#include "core/command/ConsoleCommand.h"
[1588]51
[5980]52#include "OverlayGroup.h"
53
[1588]54namespace orxonox
55{
[1615]56    unsigned int OrxonoxOverlay::hudOverlayCounter_s = 0;
57    std::map<std::string, OrxonoxOverlay*> OrxonoxOverlay::overlays_s;
[1588]58
[7284]59    SetConsoleCommand("OrxonoxOverlay", "scaleOverlay",     &OrxonoxOverlay::scaleOverlay);
60    SetConsoleCommand("OrxonoxOverlay", "scrollOverlay",    &OrxonoxOverlay::scrollOverlay);
61    SetConsoleCommand("OrxonoxOverlay", "toggleVisibility", &OrxonoxOverlay::toggleVisibility);
[8309]62    SetConsoleCommand("OrxonoxOverlay", "show",     &OrxonoxOverlay::showOverlay);
[7284]63    SetConsoleCommand("OrxonoxOverlay", "rotateOverlay",    &OrxonoxOverlay::rotateOverlay);
[1588]64
[2087]65    OrxonoxOverlay::OrxonoxOverlay(BaseObject* creator)
66        : BaseObject(creator)
[1615]67    {
68        RegisterObject(OrxonoxOverlay);
[2087]69
[2662]70        this->owner_ = 0;
71        this->group_ = 0;
72
[2896]73        if (!GameMode::showsGraphics())
[2171]74            ThrowException(NoGraphics, "Can't create OrxonoxOverlay, graphics engine not initialized");
75
[2087]76        // create the Ogre::Overlay
77        overlay_ = Ogre::OverlayManager::getSingleton().create("OrxonoxOverlay_overlay_"
[3280]78            + multi_cast<std::string>(hudOverlayCounter_s++));
[2087]79
80        // create background panel (can be used to show any picture)
81        this->background_ = static_cast<Ogre::PanelOverlayElement*>(
82            Ogre::OverlayManager::getSingleton().createOverlayElement("Panel",
[3280]83            "OrxonoxOverlay_background_" + multi_cast<std::string>(hudOverlayCounter_s++)));
[2087]84        this->overlay_->add2D(this->background_);
85
[2896]86        // Get aspect ratio from the render window. Later on, we get informed automatically
[3327]87        this->windowAspectRatio_ = static_cast<float>(this->getWindowWidth()) / this->getWindowHeight();
[2087]88
[6417]89        this->size_ = Vector2(1.0f, 1.0f);
90        this->pickPoint_= Vector2(0.0f, 0.0f);
91        this->position_ = Vector2(0.0f, 0.0f);
92        this->angle_ = Degree(0.0);
93        this->bCorrectAspect_ = false;
94        this->rotState_ = Horizontal;
95        this->angleChanged(); // updates all other values as well
[2087]96
97        setBackgroundMaterial("");
[1615]98    }
[1614]99
[1622]100    /**
101    @brief
102        Make sure everything gets removed/destroyed.
103    @remark
104        We assume that the Ogre::OverlayManager exists (there is an assert in Ogre for that!)
105    */
[1615]106    OrxonoxOverlay::~OrxonoxOverlay()
107    {
[2087]108        if (this->isInitialized())
109        {
110            // erase ourself from the map with all overlays
111            std::map<std::string, OrxonoxOverlay*>::iterator it = overlays_s.find(this->getName());
112            if (it != overlays_s.end())
113                overlays_s.erase(it);
[1622]114
115            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->background_);
116            Ogre::OverlayManager::getSingleton().destroy(this->overlay_);
[2087]117        }
[1615]118    }
119
[1622]120    /**
121    @brief
122        Loads the OrxonoxOverlay.
[1747]123
[1622]124        This has to be called before usage, otherwise strange behaviour is
125        guaranteed! (there should be no segfaults however).
126    @copydoc
127        BaseObject::XMLPort()
128    */
[7401]129    void OrxonoxOverlay::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[1590]130    {
[7401]131        SUPER(OrxonoxOverlay, XMLPort, xmlelement, mode);
[1588]132
[7401]133        XMLPortParam(OrxonoxOverlay, "size",      setSize,      getSize,      xmlelement, mode);
134        XMLPortParam(OrxonoxOverlay, "pickpoint", setPickPoint, getPickPoint, xmlelement, mode);
135        XMLPortParam(OrxonoxOverlay, "position",  setPosition,  getPosition,  xmlelement, mode);
136        XMLPortParam(OrxonoxOverlay, "rotation",  setRotation,  getRotation,  xmlelement, mode);
137        XMLPortParam(OrxonoxOverlay, "correctaspect", setAspectCorrection,   getAspectCorrection,   xmlelement, mode);
138        XMLPortParam(OrxonoxOverlay, "background",    setBackgroundMaterial, getBackgroundMaterial, xmlelement, mode);
[8706]139        XMLPortParam(OrxonoxOverlay, "backgroundtex", setBackgroundTexture,  getBackgroundTexture,  xmlelement, mode);
[2087]140    }
[1614]141
[2087]142    void OrxonoxOverlay::changedName()
143    {
[2662]144        SUPER(OrxonoxOverlay, changedName);
145
[2087]146        OrxonoxOverlay::overlays_s.erase(this->getOldName());
[1590]147
[2087]148        if (OrxonoxOverlay::overlays_s.find(this->getName()) != OrxonoxOverlay::overlays_s.end())
[8858]149            orxout(internal_warning) << "Overlay names should be unique or you cannnot access them via console. Name: \"" << this->getName() << '"' << endl;
[1588]150
[2087]151        OrxonoxOverlay::overlays_s[this->getName()] = this;
[1590]152    }
[1588]153
[1622]154    //! Only sets the background material name if not ""
[1615]155    void OrxonoxOverlay::setBackgroundMaterial(const std::string& material)
156    {
[6417]157        if (this->background_ && !material.empty())
[1615]158            this->background_->setMaterialName(material);
159    }
[1588]160
[1622]161    //! Returns the the material name of the background
[1615]162    const std::string& OrxonoxOverlay::getBackgroundMaterial() const
163    {
164        if (this->background_)
165            return this->background_->getMaterialName();
166        else
[2087]167            return BLANKSTRING;
[1615]168    }
[1614]169
[8706]170    //! Sets the background texture name and creates a new material if necessary
171    void OrxonoxOverlay::setBackgroundTexture(const std::string& texture)
172    {
173        if (this->background_ && this->background_->getMaterial().isNull() && !texture.empty())
174        {
175            // create new material
176            const std::string& materialname = "generated_material" + getUniqueNumberString();
177            Ogre::MaterialPtr material = static_cast<Ogre::MaterialPtr>(Ogre::MaterialManager::getSingleton().create(materialname, "General"));
178            material->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
179            Ogre::TextureUnitState* textureUnitState_ = material->getTechnique(0)->getPass(0)->createTextureUnitState();
180            textureUnitState_->setTextureName(texture);
181            textureUnitState_->setNumMipmaps(0);
182            this->background_->setMaterialName(materialname);
183        }
184    }
185
186    //! Returns the the texture name of the background
187    const std::string& OrxonoxOverlay::getBackgroundTexture() const
188    {
189        if (this->background_)
190        {
191            Ogre::TextureUnitState* tempTx = this->background_->getMaterial()->getTechnique(0)->getPass(0)->getTextureUnitState(0);
192            return tempTx->getTextureName();
193        }
194        else
195            return BLANKSTRING;
196    }
197
[1622]198    //! Called by BaseObject when visibility has changed.
[1615]199    void OrxonoxOverlay::changedVisibility()
[1595]200    {
[5980]201        SUPER( OrxonoxOverlay, changedVisibility );
[6417]202
[1622]203        if (!this->overlay_)
204            return;
205
[5980]206        // only set to visible if corresponding OverlayGroup is also visible
207        if (this->isVisible() && (!this->getOverlayGroup() || this->getOverlayGroup()->isVisible()) )
[1622]208            this->overlay_->show();
209        else
210            this->overlay_->hide();
[1595]211    }
[1588]212
[1622]213    /**
214    @brief
[2896]215        Called by the GraphicsManager whenever the window size changes.
[1622]216        Calculates the aspect ratio only.
217    */
[2896]218    void OrxonoxOverlay::windowResized(unsigned int newWidth, unsigned int newHeight)
[1615]219    {
[3301]220        this->windowAspectRatio_ = static_cast<float>(newWidth) / newHeight;
[1622]221        this->sizeCorrectionChanged();
[1615]222    }
[1590]223
[1622]224    /**
225    @brief
226        Called whenever the rotation angle has changed.
227    */
228    void OrxonoxOverlay::angleChanged()
[1595]229    {
[1622]230        if (!this->overlay_)
231            return;
232
233        this->overlay_->setRotate(this->angle_);
[1615]234        this->sizeCorrectionChanged();
[1595]235    }
[1615]236
[1622]237    /**
238    @brief
239        Called whenever the aspect ratio or the angle has changed.
240        The method calculates a correction factor for the size to compensate
241        for aspect distortion if desired.
242    @remarks
243        This only works when the angle is about 0 or 90 degrees.
244    */
[1615]245    void OrxonoxOverlay::sizeCorrectionChanged()
[1595]246    {
[1615]247        if (this->bCorrectAspect_)
248        {
249            float angle = this->angle_.valueDegrees();
[1618]250            if (angle < 0.0)
251                angle = -angle;
[3301]252            angle -= 180.0f * static_cast<int>(angle / 180.0);
[1618]253
[1622]254            // take the reverse if angle is about 90 degrees
[1615]255            float tempAspect;
[3196]256            if (angle > 89.0f && angle < 91.0f)
[1632]257            {
[6502]258                tempAspect = 1.0f / this->windowAspectRatio_;
[1632]259                rotState_ = Vertical;
260            }
[1618]261            else if (angle > 179 || angle < 1)
[1632]262            {
[1615]263                tempAspect = this->windowAspectRatio_;
[1632]264                rotState_ = Horizontal;
265            }
[1615]266            else
[1632]267            {
[3196]268                tempAspect = 1.0f;
[1632]269                rotState_ = Inbetween;
270            }
[1615]271
272            // note: this is only an approximation that is mostly valid when the
273            // magnitude of the width is about the magnitude of the height.
274            // Correctly we would have to take the square root of width*height
[3196]275            this->sizeCorrection_.x = 2.0f / (tempAspect + 1.0f);
[1615]276            this->sizeCorrection_.y = tempAspect * this->sizeCorrection_.x;
277        }
278        else
279        {
280            this->sizeCorrection_ = Vector2::UNIT_SCALE;
281        }
[1622]282
[1615]283        this->sizeChanged();
[1595]284    }
285
[1615]286    /**
[1622]287    @brief
288        Sets the overlay size using the actual corrected size.
[1615]289    */
290    void OrxonoxOverlay::sizeChanged()
291    {
[1622]292        if (!this->overlay_)
293            return;
294
[1615]295        this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y);
296        positionChanged();
297    }
[1595]298
[1615]299    /**
[1622]300    @brief
301        Determines the position of the overlay.
302        This works also well when a rotation angle is applied. The overlay always
303        gets aligned correctly as well as possible.
[1615]304    */
[1622]305    void OrxonoxOverlay::positionChanged()
[1615]306    {
[1622]307        if (!this->overlay_)
308            return;
[1595]309
[1622]310        // transform the angle to a range of 0 - pi/2 first.
[1617]311        float angle = this->angle_.valueRadians();
312        if (angle < 0.0)
313            angle = -angle;
[7184]314        angle -= math::pi * static_cast<int>(angle / (math::pi));
[8351]315        if (angle > math::pi_2)
[7184]316            angle = math::pi - angle;
[1747]317
[1622]318        // do some mathematical fiddling for a bounding box
[1615]319        Vector2 actualSize = size_ * sizeCorrection_;
320        float radius = actualSize.length();
321        float phi = atan(actualSize.y / actualSize.x);
322        Vector2 boundingBox(radius * cos(angle - phi), radius * sin(angle + phi));
[1622]323
324        // calculate the scrolling offset
325        Vector2 scroll = (position_ - 0.5 - boundingBox * (pickPoint_ - 0.5)) * 2.0;
[1615]326        this->overlay_->setScroll(scroll.x, -scroll.y);
327    }
[1598]328
[1615]329
[1622]330    //########### Console commands ############
331
332    /**
333    @brief
334        Scales an overlay by its name.
335    @param name
336        The name of the overlay defined BaseObject::setName() (usually done with the "name"
[1623]337        attribute in the xml file).
[7401]338    @param scale
339        The scaling factor
[1622]340    */
[1615]341    /*static*/ void OrxonoxOverlay::scaleOverlay(const std::string& name, float scale)
342    {
343        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
344        if (it != overlays_s.end())
[6417]345            it->second->scale(Vector2(scale, scale));
[1615]346    }
347
[1622]348    /**
349    @brief
[2993]350        Toggles the visibility of an Overlay by it's name.
351    @param name
352        The name of the overlay defined BaseObject::setName() (usually done with the "name"
353        attribute in the xml file).
354    */
355    /*static*/ void OrxonoxOverlay::toggleVisibility(const std::string& name)
356    {
357        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
358        if (it != overlays_s.end())
359        {
[6417]360            OrxonoxOverlay* overlay= it->second;
[2993]361            if(overlay->isVisible())
[6711]362            {
[2993]363                overlay->hide();
[8858]364                orxout(verbose, context::misc::overlays) << "HIDE " << name << endl;
[6711]365            }
[2993]366            else
[6711]367            {
[2993]368                overlay->show();
[8858]369                orxout(verbose, context::misc::overlays) << "SHOW " << name << endl;
[6711]370            }
[2993]371        }
372    }
[8309]373   
374    /**
375    @brief
376        Shows Overlay by it's name.
377    @param name
378        The name of the overlay defined BaseObject::setName() (usually done with the "name"
379        attribute in the xml file).
380    */
381    /*static*/ void OrxonoxOverlay::showOverlay(const std::string& name)
382    {
383        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
384        if (it != overlays_s.end())
385        {
386            OrxonoxOverlay* overlay= it->second;
387            if(overlay->isVisible())
388                overlay->changedVisibility();
389            else
390                overlay->show();
391        }
392    }
[2993]393
394    /**
395    @brief
[1622]396        Scrolls an overlay by its name.
397    @param name
398        The name of the overlay defined BaseObject::setName() (usually done with the "name"
[1623]399        attribute in the xml file).
[7401]400    @param scroll
401        The relative translation of the overlay
[1622]402    */
[1615]403    /*static*/ void OrxonoxOverlay::scrollOverlay(const std::string& name, const Vector2& scroll)
404    {
405        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
406        if (it != overlays_s.end())
[6417]407            it->second->scroll(scroll);
[1615]408    }
409
[1622]410    /**
411    @brief
412        Rotates an overlay by its name.
413    @param name
414        The name of the overlay defined BaseObject::setName() (usually done with the "name"
[1623]415        attribute in the xml file).
[7401]416    @param angle
417        The rotation angle in degree
[1622]418    */
[1615]419    /*static*/ void OrxonoxOverlay::rotateOverlay(const std::string& name, const Degree& angle)
420    {
421        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
422        if (it != overlays_s.end())
[6417]423            it->second->rotate(angle);
[1615]424    }
[6057]425
426    void OrxonoxOverlay::setOverlayGroup(OverlayGroup* group)
427    {
428        if (group != this->group_)
429        {
430            if (this->group_)
431                this->group_->removeElement(this);
432            this->group_ = group;
433            this->changedOverlayGroup();
434        }
435    }
[6417]436
[8706]437    void OrxonoxOverlay::setBackgroundAlpha(float alpha)
438    {
[6417]439        Ogre::MaterialPtr ptr = this->background_->getMaterial();
440        Ogre::TextureUnitState* tempTx = ptr->getTechnique(0)->getPass(0)->getTextureUnitState(0);
441        tempTx->setAlphaOperation(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, alpha);
442    }
[8706]443
444    void OrxonoxOverlay::setBackgroundColour(ColourValue colour)
445    {
446        Ogre::MaterialPtr ptr = this->background_->getMaterial();
447        Ogre::TextureUnitState* tempTx = ptr->getTechnique(0)->getPass(0)->getTextureUnitState(0);
448        tempTx->setColourOperationEx(Ogre::LBX_MODULATE, Ogre::LBS_MANUAL, Ogre::LBS_CURRENT, colour);
449    }
[1588]450}
Note: See TracBrowser for help on using the repository browser.