Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 22, 2008, 12:06:55 AM (16 years ago)
Author:
rgrieder
Message:
  • added blankString to String so you can return ""; even if it's a const std::string&
  • fixed several bugs with aspect correct and margin alignment
  • added console commands for OrxonoxOverlays and OverlayGroups for rotate, scale and scroll (you can access the by name (from name=.. in xml file), e.g. "OrxonoxOverlay rotateOverlay SpeedBar 90)
  • converted everything in overlays/ to 4 spaces/tab ;)
  • removed all using namespace Ogre;
  • added background_ Panel to OrxonoxOverlay, since most of the derived classes can use that
  • should work now, but I'll have to test on a tardis box first
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/hud/src/orxonox/overlays/OrxonoxOverlay.cc

    r1614 r1615  
    3030#include "OrxonoxOverlay.h"
    3131
     32#include <cmath>
    3233#include <OgreOverlayManager.h>
    3334#include <OgrePanelOverlayElement.h>
    3435#include "util/Convert.h"
     36#include "util/String.h"
    3537#include "core/CoreIncludes.h"
    3638#include "GraphicsEngine.h"
     
    3840namespace orxonox
    3941{
    40   unsigned int OrxonoxOverlay::hudOverlayCounter_s = 0;
    41 
    42   OrxonoxOverlay::OrxonoxOverlay()
    43     : overlay_(0)
    44     , background_(0)
    45     , windowAspectRatio_(1.0f)
    46     , bCorrectAspect_(false)
    47     , size_(1.0f, 1.0f)
    48     , sizeCorrection_(1.0f, 1.0f)
    49     , angle_(0.0f)
    50     , position_(0.0f, 0.0f)
    51     , origin_(0.0f, 0.0f)
    52   {
    53     RegisterObject(OrxonoxOverlay);
    54   }
    55 
    56   OrxonoxOverlay::~OrxonoxOverlay()
    57   {
    58     if (this->background_)
    59       Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->background_);
    60   }
    61 
    62   void OrxonoxOverlay::XMLPort(Element& xmlElement, XMLPort::Mode mode)
    63   {
    64     BaseObject::XMLPort(xmlElement, mode);
    65 
    66     if (mode == XMLPort::LoadObject)
    67     {
    68       overlay_ = Ogre::OverlayManager::getSingleton().create("OrxonoxOverlay"
    69             + convertToString(hudOverlayCounter_s++) + "_" + this->getName());
    70 
    71       this->windowResized(GraphicsEngine::getSingleton().getWindowWidth(),
    72             GraphicsEngine::getSingleton().getWindowHeight());
    73 
    74       // create background
    75       this->background_ = static_cast<Ogre::PanelOverlayElement*>(
    76           Ogre::OverlayManager::getSingleton().createOverlayElement("Panel", getUniqueNumberStr() + "_Background"));
    77       this->overlay_->add2D(this->background_);
    78     }
    79 
    80     XMLPortParam(OrxonoxOverlay, "correctAspect", setAspectCorrection, getAspectCorrection, xmlElement, mode);
    81     XMLPortParam(OrxonoxOverlay, "size", setSize, getUncorrectedSize, xmlElement, mode);
    82     XMLPortParam(OrxonoxOverlay, "rotation", setRotation, getRotation, xmlElement, mode);
    83     XMLPortParam(OrxonoxOverlay, "origin", setOrigin, getOrigin, xmlElement, mode);
    84     XMLPortParam(OrxonoxOverlay, "position", setPosition, getPosition, xmlElement, mode);
    85     XMLPortParam(OrxonoxOverlay, "background", setBackgroundMaterial, getBackgroundMaterial, xmlElement, mode);
    86 
    87     if (mode == XMLPort::LoadObject)
    88     {
    89       this->overlay_->show();
    90       if (!this->isVisible())
    91           this->overlay_->hide();
    92 
    93       this->sizeChanged();
    94       this->positionChanged();
    95       this->angleChanged();
    96     }
    97   }
    98 
    99   void OrxonoxOverlay::setBackgroundMaterial(const std::string& material)
    100   {
    101     if (this->background_ && material != "")
    102       this->background_->setMaterialName(material);
    103   }
    104 
    105   std::string OrxonoxOverlay::getBackgroundMaterial() const
    106   {
    107     if (this->background_)
    108       return this->background_->getMaterialName();
    109     else
    110       return "";
    111   }
    112 
    113   void OrxonoxOverlay::changedVisibility()
    114   {
    115     if (this->overlay_)
    116     {
    117       if (this->isVisible())
    118         this->overlay_->show();
    119       else
    120         this->overlay_->hide();
    121     }
    122   }
    123 
    124   void OrxonoxOverlay::windowResized(int newWidth, int newHeight)
    125   {
    126     this->windowAspectRatio_ = newWidth/(float)newHeight;
    127 
    128     this->setAspectCorrection(this->bCorrectAspect_);
    129   }
    130 
    131   void OrxonoxOverlay::setAspectCorrection(bool val)
    132   {
    133     if (val)
    134     {
    135       // note: this is only an approximation that is mostly valid when the
    136       // magnitude of the width is about the magnitude of the height.
    137       // Correctly we would have to take the square root of width*height
    138       this->sizeCorrection_.x = 2.0 / (this->windowAspectRatio_ + 1.0);
    139       this->sizeCorrection_.y = this->windowAspectRatio_ * this->sizeCorrection_.x;
    140     }
    141     else
    142     {
    143       this->sizeCorrection_ = Vector2::UNIT_SCALE;
    144     }
    145 
    146     this->bCorrectAspect_ = val;
    147     this->sizeChanged();
    148   }
    149 
    150   /**
     42    unsigned int OrxonoxOverlay::hudOverlayCounter_s = 0;
     43    std::map<std::string, OrxonoxOverlay*> OrxonoxOverlay::overlays_s;
     44
     45    SetConsoleCommand(OrxonoxOverlay, scaleOverlay, false).setAccessLevel(AccessLevel::User);
     46    SetConsoleCommand(OrxonoxOverlay, scrollOverlay, false).setAccessLevel(AccessLevel::User);
     47    SetConsoleCommand(OrxonoxOverlay, rotateOverlay, false).setAccessLevel(AccessLevel::User);
     48
     49    OrxonoxOverlay::OrxonoxOverlay()
     50        : overlay_(0)
     51        , background_(0)
     52        , windowAspectRatio_(1.0f)
     53        , bCorrectAspect_(false)
     54        , size_(1.0f, 1.0f)
     55        , sizeCorrection_(1.0f, 1.0f)
     56        , angle_(0.0f)
     57        , position_(0.0f, 0.0f)
     58        , origin_(0.0f, 0.0f)
     59    {
     60        RegisterObject(OrxonoxOverlay);
     61    }
     62
     63    OrxonoxOverlay::~OrxonoxOverlay()
     64    {
     65        if (this->background_)
     66            Ogre::OverlayManager::getSingleton().destroyOverlayElement(this->background_);
     67
     68        std::map<std::string, OrxonoxOverlay*>::iterator it = overlays_s.find(this->getName());
     69        if (it != overlays_s.end())
     70            overlays_s.erase(it);
     71    }
     72
     73    void OrxonoxOverlay::XMLPort(Element& xmlElement, XMLPort::Mode mode)
     74    {
     75        BaseObject::XMLPort(xmlElement, mode);
     76
     77        if (mode == XMLPort::LoadObject)
     78        {
     79            if (overlays_s.find(this->getName()) != overlays_s.end())
     80            {
     81                COUT(1) << "Overlay names should be unique or you cannnot access them via console." << std::endl;
     82            }
     83            overlays_s[this->getName()] = this;
     84
     85            overlay_ = Ogre::OverlayManager::getSingleton().create("OrxonoxOverlay_overlay_"
     86                + convertToString(hudOverlayCounter_s++));
     87
     88            // create background
     89            this->background_ = static_cast<Ogre::PanelOverlayElement*>(
     90                Ogre::OverlayManager::getSingleton().createOverlayElement("Panel",
     91                "OrxonoxOverlay_background_" + convertToString(hudOverlayCounter_s++)));
     92            this->overlay_->add2D(this->background_);
     93
     94            this->windowResized(GraphicsEngine::getSingleton().getWindowWidth(),
     95                GraphicsEngine::getSingleton().getWindowHeight());
     96        }
     97
     98        XMLPortParam(OrxonoxOverlay, "correctAspect", setAspectCorrection, getAspectCorrection, xmlElement, mode);
     99        XMLPortParam(OrxonoxOverlay, "size", setSize, getUncorrectedSize, xmlElement, mode);
     100        XMLPortParam(OrxonoxOverlay, "rotation", setRotation, getRotation, xmlElement, mode);
     101        XMLPortParam(OrxonoxOverlay, "origin", setOrigin, getOrigin, xmlElement, mode);
     102        XMLPortParam(OrxonoxOverlay, "position", setPosition, getPosition, xmlElement, mode);
     103        XMLPortParam(OrxonoxOverlay, "background", setBackgroundMaterial, getBackgroundMaterial, xmlElement, mode);
     104
     105        if (mode == XMLPort::LoadObject)
     106        {
     107            this->overlay_->show();
     108            if (!this->isVisible())
     109                this->overlay_->hide();
     110
     111            this->sizeChanged();
     112            this->positionChanged();
     113            this->angleChanged();
     114        }
     115    }
     116
     117    void OrxonoxOverlay::setBackgroundMaterial(const std::string& material)
     118    {
     119        if (this->background_ && material != "")
     120            this->background_->setMaterialName(material);
     121    }
     122
     123    const std::string& OrxonoxOverlay::getBackgroundMaterial() const
     124    {
     125        if (this->background_)
     126            return this->background_->getMaterialName();
     127        else
     128            return blankString;
     129    }
     130
     131    void OrxonoxOverlay::changedVisibility()
     132    {
     133        if (this->overlay_)
     134        {
     135            if (this->isVisible())
     136                this->overlay_->show();
     137            else
     138                this->overlay_->hide();
     139        }
     140    }
     141
     142    void OrxonoxOverlay::windowResized(int newWidth, int newHeight)
     143    {
     144        this->windowAspectRatio_ = newWidth/(float)newHeight;
     145
     146        this->setAspectCorrection(this->bCorrectAspect_);
     147    }
     148
     149    void OrxonoxOverlay::setAspectCorrection(bool val)
     150    {
     151        this->bCorrectAspect_ = val;
     152        this->sizeCorrectionChanged();
     153    }
     154
     155    void OrxonoxOverlay::sizeCorrectionChanged()
     156    {
     157        if (this->bCorrectAspect_)
     158        {
     159            float angle = this->angle_.valueDegrees();
     160            float tempAspect;
     161            if (angle > 89.0 && angle < 91.0 || angle > 269 && angle < 271)
     162                tempAspect = 1.0 / this->windowAspectRatio_;
     163            else if (angle > 359 && angle < 1 || angle > 179 && angle < 181)
     164                tempAspect = this->windowAspectRatio_;
     165            else
     166                tempAspect = 1.0;
     167
     168            // note: this is only an approximation that is mostly valid when the
     169            // magnitude of the width is about the magnitude of the height.
     170            // Correctly we would have to take the square root of width*height
     171            this->sizeCorrection_.x = 2.0 / (tempAspect + 1.0);
     172            this->sizeCorrection_.y = tempAspect * this->sizeCorrection_.x;
     173        }
     174        else
     175        {
     176            this->sizeCorrection_ = Vector2::UNIT_SCALE;
     177        }
     178        this->sizeChanged();
     179    }
     180
     181    /**
    151182    @remarks
    152       This function can be overriden by any derivative.
    153   */
    154   void OrxonoxOverlay::sizeChanged()
    155   {
    156     this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y);
    157     positionChanged();
    158   }
    159 
    160   /**
     183        This function can be overriden by any derivative.
     184    */
     185    void OrxonoxOverlay::sizeChanged()
     186    {
     187        this->overlay_->setScale(size_.x * sizeCorrection_.x, size_.y * sizeCorrection_.y);
     188        positionChanged();
     189    }
     190
     191    /**
    161192    @remarks
    162       This function can be overriden by any derivative.
    163   */
    164   void OrxonoxOverlay::angleChanged()
    165   {
    166     this->overlay_->setRotate(this->angle_);
    167   }
    168 
    169   /**
     193        This function can be overriden by any derivative.
     194    */
     195    void OrxonoxOverlay::angleChanged()
     196    {
     197        this->overlay_->setRotate(this->angle_);
     198        this->sizeCorrectionChanged();
     199    }
     200
     201    /**
    170202    @remarks
    171       This function can be overriden by any derivative.
    172   */
    173   void OrxonoxOverlay::positionChanged()
    174   {
    175     Vector2 scroll = (position_ - 0.5 - size_ * sizeCorrection_ * (origin_ - 0.5)) * 2.0;
    176     this->overlay_->setScroll(scroll.x, -scroll.y);
    177   }
     203        This function can be overriden by any derivative.
     204    */
     205    void OrxonoxOverlay::positionChanged()
     206    {
     207        float angle = abs(this->angle_.valueRadians());
     208        angle -= Ogre::Math::PI * (int)(angle / (Ogre::Math::PI));
     209        if (angle > Ogre::Math::PI * 0.5)
     210            angle = Ogre::Math::PI - angle;
     211        Vector2 actualSize = size_ * sizeCorrection_;
     212        float radius = actualSize.length();
     213        float phi = atan(actualSize.y / actualSize.x);
     214        Vector2 boundingBox(radius * cos(angle - phi), radius * sin(angle + phi));
     215        Vector2 scroll = (position_ - 0.5 - boundingBox * (origin_ - 0.5)) * 2.0;
     216        this->overlay_->setScroll(scroll.x, -scroll.y);
     217    }
     218
     219
     220    /*static*/ void OrxonoxOverlay::scaleOverlay(const std::string& name, float scale)
     221    {
     222        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
     223        if (it != overlays_s.end())
     224            (*it).second->scale(Vector2(scale, scale));
     225    }
     226
     227    /*static*/ void OrxonoxOverlay::scrollOverlay(const std::string& name, const Vector2& scroll)
     228    {
     229        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
     230        if (it != overlays_s.end())
     231            (*it).second->scroll(scroll);
     232    }
     233
     234    /*static*/ void OrxonoxOverlay::rotateOverlay(const std::string& name, const Degree& angle)
     235    {
     236        std::map<std::string, OrxonoxOverlay*>::const_iterator it = overlays_s.find(name);
     237        if (it != overlays_s.end())
     238            (*it).second->rotate(angle);
     239    }
    178240}
Note: See TracChangeset for help on using the changeset viewer.