[2779] | 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 | * Damian 'Mozork' Frick |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "OrxonoxStableHeaders.h" |
---|
| 30 | #include "NotificationOverlay.h" |
---|
| 31 | |
---|
| 32 | #include <OgreOverlayManager.h> |
---|
| 33 | #include <OgreTextAreaOverlayElement.h> |
---|
| 34 | #include <OgrePanelOverlayElement.h> |
---|
| 35 | |
---|
| 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "util/Exception.h" |
---|
| 38 | |
---|
| 39 | #include "Notification.h" |
---|
| 40 | #include "NotificationQueue.h" |
---|
| 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | |
---|
| 45 | NotificationOverlay::NotificationOverlay(BaseObject* creator) : OrxonoxOverlay(creator) |
---|
| 46 | { |
---|
| 47 | this->initialize(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | NotificationOverlay::NotificationOverlay(NotificationQueue* queue, Notification* notification) : OrxonoxOverlay(this) |
---|
| 51 | { |
---|
| 52 | this->initialize(); |
---|
| 53 | |
---|
| 54 | if(notification == NULL || queue == NULL) |
---|
| 55 | { |
---|
| 56 | ThrowException(Argument, "There were NULL-Pointer arguments in NotificationOverlay creation."); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | this->queue_ = queue; |
---|
| 60 | this->defineOverlay(); |
---|
| 61 | |
---|
| 62 | this->processNotification(notification); |
---|
| 63 | |
---|
| 64 | COUT(3) << getCaption() << std::endl; |
---|
| 65 | COUT(3) << getFontSize() << std::endl; |
---|
| 66 | //COUT(3) << getFont() << std::endl; |
---|
| 67 | COUT(3) << getWidth() << std::endl; |
---|
| 68 | COUT(3) << getAlignmentString() << std::endl; |
---|
| 69 | COUT(3) << getTextSize() << std::endl; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | void NotificationOverlay::initialize(void) |
---|
| 73 | { |
---|
| 74 | RegisterObject(NotificationOverlay); |
---|
| 75 | |
---|
| 76 | this->queue_ = NULL; |
---|
| 77 | this->notificationText_ = NULL; |
---|
| 78 | this->width_ = 0; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void NotificationOverlay::defineOverlay(void) |
---|
| 82 | { |
---|
| 83 | this->setFont(this->queue_->getFont()); |
---|
| 84 | this->setFontSize(this->queue_->getFontSize()); |
---|
| 85 | |
---|
| 86 | this->notificationText_ = static_cast<Ogre::TextAreaOverlayElement*>(Ogre::OverlayManager::getSingleton().createOverlayElement("TextArea", "NotificationOverlay_notificationText_" + getUniqueNumberString())); |
---|
| 87 | this->notificationText_->setCharHeight(1.0); |
---|
| 88 | this->notificationText_->setColour(ColourValue(1.0, 1.0, 1.0, 1.0)); |
---|
| 89 | this->notificationText_->setAlignment(Ogre::TextAreaOverlayElement::Center); |
---|
| 90 | this->notificationText_->setCaption(""); |
---|
| 91 | |
---|
| 92 | this->background_->addChild(this->notificationText_); |
---|
| 93 | COUT(3) << this->queue_->getFont() << std::endl; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | NotificationOverlay::~NotificationOverlay() |
---|
| 97 | { |
---|
| 98 | |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void NotificationOverlay::setFont(const std::string & font) |
---|
| 102 | { |
---|
| 103 | if (this->notificationText_ && font != "") |
---|
| 104 | this->notificationText_->setFontName(font); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void NotificationOverlay::setFontSize(float size) |
---|
| 108 | { |
---|
| 109 | if (this->notificationText_ && size >= 0) |
---|
| 110 | this->notificationText_->setCharHeight(size); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | void NotificationOverlay::setWidth(int width) |
---|
| 114 | { |
---|
| 115 | this->width_ = width; |
---|
| 116 | this->notificationText_->setCaption(clipMessage(this->notification_->getMessage())); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | float NotificationOverlay::getFontSize(void) const |
---|
| 120 | { |
---|
| 121 | if (this->notificationText_) |
---|
| 122 | return this->notificationText_->getCharHeight(); |
---|
| 123 | else |
---|
| 124 | return 0; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | bool NotificationOverlay::processNotification(Notification* notification) |
---|
| 128 | { |
---|
| 129 | this->notificationText_->setCaption(clipMessage(notification->getMessage())); |
---|
| 130 | this->notification_ = notification; |
---|
| 131 | return true; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | const std::string NotificationOverlay::clipMessage(const std::string & str) |
---|
| 135 | { |
---|
| 136 | if(str.length() <= (unsigned int)this->queue_->getNotificationLength()) |
---|
| 137 | return str; |
---|
| 138 | return str.substr(0, this->queue_->getNotificationLength()); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | const std::string NotificationOverlay::clipMessage2(const std::string & str) |
---|
| 142 | { |
---|
| 143 | |
---|
| 144 | std::string message = str; |
---|
| 145 | unsigned int i = 0; |
---|
| 146 | |
---|
| 147 | unsigned int found = message.find("\\n", i); |
---|
| 148 | while(found != std::string::npos) |
---|
| 149 | { |
---|
| 150 | message.replace(found, 2, "\n"); |
---|
| 151 | i = found+2; |
---|
| 152 | found = message.find("\\n", i); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | std::string clippedMessage = ""; |
---|
| 156 | int wordLength = 0; |
---|
| 157 | i = 0; |
---|
| 158 | int widthLeft = this->getWidth(); |
---|
| 159 | while(i < message.length()) |
---|
| 160 | { |
---|
| 161 | while(i < message.length() && message[i] != ' ' && message[i] != '\n') |
---|
| 162 | { |
---|
| 163 | i++; |
---|
| 164 | wordLength++; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | if(wordLength <= widthLeft) |
---|
| 168 | { |
---|
| 169 | clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength); |
---|
| 170 | if(i < message.length()) |
---|
| 171 | { |
---|
| 172 | clippedMessage = clippedMessage + message.substr(i,1); |
---|
| 173 | } |
---|
| 174 | widthLeft -= (wordLength+1); |
---|
| 175 | if(message[i] == '\n') |
---|
| 176 | { |
---|
| 177 | widthLeft = this->getWidth() - (wordLength+1); |
---|
| 178 | } |
---|
| 179 | wordLength = 0; |
---|
| 180 | i++; |
---|
| 181 | } |
---|
| 182 | else |
---|
| 183 | { |
---|
| 184 | clippedMessage.push_back('\n'); |
---|
| 185 | clippedMessage = clippedMessage + message.substr(i-wordLength, wordLength); |
---|
| 186 | if(i < message.length()) |
---|
| 187 | { |
---|
| 188 | clippedMessage = clippedMessage + message.substr(i,1); |
---|
| 189 | } |
---|
| 190 | widthLeft = this->getWidth() - (wordLength+1); |
---|
| 191 | i++; |
---|
| 192 | wordLength = 0; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | return clippedMessage; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | void NotificationOverlay::sizeChanged() |
---|
| 200 | { |
---|
| 201 | if (this->rotState_ == Horizontal) |
---|
| 202 | this->overlay_->setScale(size_.y * sizeCorrection_.y, size_.y * sizeCorrection_.y); |
---|
| 203 | else if (this->rotState_ == Vertical) |
---|
| 204 | this->overlay_->setScale(size_.y / (sizeCorrection_.y * sizeCorrection_.y), size_.y * sizeCorrection_.y); |
---|
| 205 | else |
---|
| 206 | this->overlay_->setScale(size_.y, size_.y); |
---|
| 207 | |
---|
| 208 | positionChanged(); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | void NotificationOverlay::setAlignmentString(const std::string& alignment) |
---|
| 212 | { |
---|
| 213 | if (alignment == "right") |
---|
| 214 | this->setAlignment(Ogre::TextAreaOverlayElement::Right); |
---|
| 215 | else if (alignment == "center") |
---|
| 216 | this->setAlignment(Ogre::TextAreaOverlayElement::Center); |
---|
| 217 | else // "left" and default |
---|
| 218 | this->setAlignment(Ogre::TextAreaOverlayElement::Left); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | std::string NotificationOverlay::getAlignmentString() const |
---|
| 222 | { |
---|
| 223 | Ogre::TextAreaOverlayElement::Alignment alignment = this->notificationText_->getAlignment(); |
---|
| 224 | |
---|
| 225 | switch (alignment) |
---|
| 226 | { |
---|
| 227 | case Ogre::TextAreaOverlayElement::Right: |
---|
| 228 | return "right"; |
---|
| 229 | case Ogre::TextAreaOverlayElement::Center: |
---|
| 230 | return "center"; |
---|
| 231 | case Ogre::TextAreaOverlayElement::Left: |
---|
| 232 | return "left"; |
---|
| 233 | default: |
---|
| 234 | assert(false); return ""; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | } |
---|