00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "MyGUI_Precompiled.h"
00024 #include "MyGUI_Message.h"
00025 #include "MyGUI_ResourceSkin.h"
00026 #include "MyGUI_WidgetManager.h"
00027 #include "MyGUI_LayerManager.h"
00028 #include "MyGUI_InputManager.h"
00029 #include "MyGUI_ResourceManager.h"
00030 #include "MyGUI_Gui.h"
00031 #include "MyGUI_ControllerManager.h"
00032 #include "MyGUI_StaticImage.h"
00033 #include "MyGUI_LanguageManager.h"
00034
00035 namespace MyGUI
00036 {
00037
00038 const float MESSAGE_ALPHA_MAX = 0.5f;
00039 const float MESSAGE_ALPHA_MIN = 0.0f;
00040 const float MESSAGE_SPEED_COEF = 3.0f;
00041
00042 Message::Message() :
00043 mWidgetText(nullptr),
00044 mInfoOk(MessageBoxStyle::None),
00045 mInfoCancel(MessageBoxStyle::None),
00046 mSmoothShow(false),
00047 mWidgetFade(nullptr),
00048 mIcon(nullptr),
00049 mLeftOffset1(0),
00050 mLeftOffset2(0)
00051 {
00052 }
00053
00054 void Message::_initialise(WidgetStyle _style, const IntCoord& _coord, Align _align, ResourceSkin* _info, WidgetPtr _parent, ICroppedRectangle * _croppedParent, IWidgetCreator * _creator, const std::string& _name)
00055 {
00056 Base::_initialise(_style, _coord, _align, _info, _parent, _croppedParent, _creator, _name);
00057
00058 initialiseWidgetSkin(_info);
00059 }
00060
00061 Message::~Message()
00062 {
00063 shutdownWidgetSkin();
00064 }
00065
00066 void Message::baseChangeWidgetSkin(ResourceSkin* _info)
00067 {
00068 shutdownWidgetSkin();
00069 Base::baseChangeWidgetSkin(_info);
00070 initialiseWidgetSkin(_info);
00071 }
00072
00073 void Message::initialiseWidgetSkin(ResourceSkin* _info)
00074 {
00075
00076 for (VectorWidgetPtr::iterator iter=mWidgetChildSkin.begin(); iter!=mWidgetChildSkin.end(); ++iter)
00077 {
00078 if (*(*iter)->_getInternalData<std::string>() == "Text")
00079 {
00080 MYGUI_DEBUG_ASSERT( ! mWidgetText, "widget already assigned");
00081 mWidgetText = (*iter);
00082 mOffsetText.set(mCoord.width - mWidgetText->getWidth(), mCoord.height - mWidgetText->getHeight());
00083 mLeftOffset2 = mLeftOffset1 = mWidgetText->getLeft();
00084 }
00085 else if (*(*iter)->_getInternalData<std::string>() == "Icon")
00086 {
00087 MYGUI_DEBUG_ASSERT( ! mIcon, "widget already assigned");
00088 mIcon = (*iter)->castType<StaticImage>();
00089 }
00090 }
00091 MYGUI_ASSERT(nullptr != mWidgetText, "Child Text not found in skin (MessageBox must have widget for text)");
00092
00093 if (mIcon != nullptr)
00094 {
00095 mLeftOffset2 = mIcon->getRight() + 3;
00096 }
00097
00098
00099 const MapString& properties = _info->getProperties();
00100 if (!properties.empty())
00101 {
00102 MapString::const_iterator iter = properties.find("ButtonSkin");
00103 if (iter != properties.end()) mButtonSkin = iter->second;
00104 iter = properties.find("ButtonType");
00105 if (iter != properties.end()) mButtonType = iter->second;
00106 iter = properties.find("ButtonSize");
00107 if (iter != properties.end()) mButtonSize = IntSize::parse(iter->second);
00108 iter = properties.find("ButtonOffset");
00109 if (iter != properties.end()) mButtonOffset = IntSize::parse(iter->second);
00110 iter = properties.find("DefaultLayer");
00111 if (iter != properties.end()) mDefaultLayer = iter->second;
00112 iter = properties.find("FadeSkin");
00113 if (iter != properties.end()) mFadeSkin = iter->second;
00114 iter = properties.find("FadeLayer");
00115 if (iter != properties.end()) mFadeLayer = iter->second;
00116 }
00117
00118 }
00119
00120 void Message::shutdownWidgetSkin()
00121 {
00122 mWidgetText = nullptr;
00123 mIcon = nullptr;
00124 }
00125
00126 void Message::setMessageText(const UString& _message)
00127 {
00128 mWidgetText->setCaption(_message);
00129 updateSize();
00130 }
00131
00132 MessageBoxStyle Message::addButtonName(const UString& _name)
00133 {
00134
00135 if (mVectorButton.size() >= MessageBoxStyle::_CountUserButtons)
00136 {
00137 MYGUI_LOG(Warning, "Too many buttons in message box, ignored");
00138 return MessageBoxStyle::None;
00139 }
00140
00141 MessageBoxStyle info = MessageBoxStyle(MessageBoxStyle::Enum(MYGUI_FLAG(mVectorButton.size() + MessageBoxStyle::_IndexUserButton1)));
00142
00143
00144 if (mVectorButton.empty()) mInfoOk = info;
00145 mInfoCancel = info;
00146
00147 WidgetPtr button = createWidgetT(mButtonType, mButtonSkin, IntCoord(), Align::Left | Align::Bottom);
00148 button->eventMouseButtonClick = newDelegate(this, &Message::notifyButtonClick);
00149 button->setCaption(_name);
00150 button->_setInternalData(info);
00151 mVectorButton.push_back(button);
00152
00153 updateSize();
00154 return info;
00155 }
00156
00157 void Message::setMessageIcon(MessageBoxStyle _icon)
00158 {
00159 if (nullptr == mIcon) return;
00160 if (mIcon->getItemResource() != nullptr)
00161 {
00162 mIcon->setItemName( getIconName(_icon.getIconIndex()) );
00163 }
00164 else
00165 {
00166 mIcon->setImageIndex(_icon.getIconIndex());
00167 }
00168
00169 updateSize();
00170 }
00171
00172 void Message::setMessageButton(MessageBoxStyle _info)
00173 {
00174 clearButton();
00175
00176 std::vector<MessageBoxStyle> buttons = _info.getButtons();
00177
00178 for (size_t index=0; index<buttons.size(); ++index)
00179 {
00180
00181 MessageBoxStyle info = buttons[index];
00182
00183
00184 addButtonName(getButtonName(info));
00185
00186
00187 mVectorButton.back()->_setInternalData(info);
00188
00189
00190 if (mVectorButton.size() == 1) mInfoOk = info;
00191
00192 mInfoCancel = info;
00193 }
00194
00195 updateSize();
00196 }
00197
00198 void Message::setMessageStyle(MessageBoxStyle _style)
00199 {
00200 setMessageButton(_style);
00201 setMessageIcon(_style);
00202 }
00203
00204 void Message::notifyButtonClick(MyGUI::WidgetPtr _sender)
00205 {
00206 _destroyMessage(*_sender->_getInternalData<MessageBoxStyle>());
00207 }
00208
00209 void Message::clearButton()
00210 {
00211 for (VectorWidgetPtr::iterator iter=mVectorButton.begin(); iter!=mVectorButton.end(); ++iter)
00212 {
00213 WidgetManager::getInstance().destroyWidget(*iter);
00214 }
00215 mVectorButton.clear();
00216 }
00217
00218 void Message::onKeyButtonPressed(KeyCode _key, Char _char)
00219 {
00220 Base::onKeyButtonPressed(_key, _char);
00221 if ((_key == KeyCode::Return) || (_key == KeyCode::NumpadEnter)) _destroyMessage(mInfoOk);
00222 else if (_key == KeyCode::Escape) _destroyMessage(mInfoCancel);
00223 }
00224
00225 void Message::_destroyMessage(MessageBoxStyle _result)
00226 {
00227 eventMessageBoxResult(this, _result);
00228 if (nullptr != mWidgetFade)
00229 {
00230 if (mSmoothShow)
00231 {
00232 ControllerFadeAlpha* controller = createControllerFadeAlpha(MESSAGE_ALPHA_MIN, MESSAGE_SPEED_COEF, false);
00233 controller->eventPostAction = newDelegate(action::actionWidgetDestroy);
00234 ControllerManager::getInstance().addItem(mWidgetFade, controller);
00235 }
00236 else
00237 {
00238 WidgetManager::getInstance().destroyWidget(mWidgetFade);
00239 }
00240 }
00241 if (mSmoothShow) destroySmooth();
00242 else WidgetManager::getInstance().destroyWidget(this);
00243 }
00244
00245 void Message::setSmoothShow(bool _smooth)
00246 {
00247 mSmoothShow = _smooth;
00248 if (mSmoothShow)
00249 {
00250 setAlpha(ALPHA_MIN);
00251 setVisible(true);
00252 setVisibleSmooth(true);
00253 }
00254 }
00255
00256 void Message::setWindowFade(bool _fade)
00257 {
00258 return;
00259
00260 if (_fade)
00261 {
00262 if (nullptr == mWidgetFade)
00263 {
00264 Gui& gui = Gui::getInstance();
00265 mWidgetFade = gui.createWidgetT(Widget::getClassTypeName(), mFadeSkin, IntCoord(0, 0, gui.getViewSize().width, gui.getViewSize().height), Align::Stretch, mFadeLayer);
00266 if (mSmoothShow)
00267 {
00268 mWidgetFade->setVisible(false);
00269
00270 ControllerFadeAlpha* controller = createControllerFadeAlpha(MESSAGE_ALPHA_MAX, MESSAGE_SPEED_COEF, false);
00271 ControllerManager::getInstance().addItem(mWidgetFade, controller);
00272 }
00273 else
00274 {
00275 mWidgetFade->setAlpha(MESSAGE_ALPHA_MAX);
00276 }
00277 }
00278 }
00279 else
00280 {
00281 if (nullptr != mWidgetFade)
00282 {
00283 WidgetManager::getInstance().destroyWidget(mWidgetFade);
00284 mWidgetFade = nullptr;
00285 }
00286 }
00287 }
00288
00289 const char * Message::getIconName(size_t _index)
00290 {
00291 static const size_t CountIcons = 4;
00292 static const char * IconNames[CountIcons + 1] = { "Info", "Quest", "Error", "Warning", "" };
00293 if (_index >= CountIcons) return IconNames[CountIcons];
00294 return IconNames[_index];
00295 }
00296
00297 MyGUI::MessagePtr Message::createMessageBox(
00298 const std::string& _skin,
00299 const UString& _caption,
00300 const UString& _message,
00301 MessageBoxStyle _style,
00302 const std::string& _layer,
00303 bool _modal,
00304 const std::string& _button1,
00305 const std::string& _button2,
00306 const std::string& _button3,
00307 const std::string& _button4)
00308 {
00309 MessagePtr mess = Gui::getInstance().createWidget<Message>(_skin, IntCoord(), Align::Default, _layer);
00310
00311 mess->setCaption(_caption);
00312 mess->setMessageText(_message);
00313
00314 mess->setSmoothShow(true);
00315 if (_modal) mess->setWindowFade(true);
00316
00317 mess->setMessageStyle(_style);
00318
00319 if (false == _button1.empty())
00320 {
00321 mess->addButtonName(_button1);
00322 if (false == _button2.empty())
00323 {
00324 mess->addButtonName(_button2);
00325 if (false == _button3.empty())
00326 {
00327 mess->addButtonName(_button3);
00328 }
00329 }
00330 }
00331
00332 if (_layer.empty()) LayerManager::getInstance().attachToLayerNode(mess->getDefaultLayer(), mess);
00333 if (_modal) InputManager::getInstance().addWidgetModal(mess);
00334
00335 return mess;
00336 }
00337
00338 void Message::updateSize()
00339 {
00340 ISubWidgetText* text = mWidgetText->getSubWidgetText();
00341 IntSize size = text ? text->getTextSize() : IntSize();
00342
00343 if ((nullptr != mIcon) && (mIcon->getImageIndex() != ITEM_NONE))
00344 {
00345 if (size.height < mIcon->getHeight()) size.height = mIcon->getHeight();
00346 size.width += mIcon->getSize().width;
00347 }
00348 size += mOffsetText;
00349 size.width += 3;
00350
00351 int width = ((int)mVectorButton.size() * mButtonSize.width) + (((int)mVectorButton.size()+1) * mButtonOffset.width);
00352 if (size.width < width) size.width = width;
00353
00354 int offset = (size.width - width)/2;
00355 offset += mButtonOffset.width;
00356
00357 const IntSize& view = Gui::getInstance().getViewSize();
00358 setCoord((view.width-size.width)/2, (view.height-size.height)/2, size.width, size.height);
00359
00360 if (nullptr != mIcon)
00361 {
00362 if (mIcon->getImageIndex() != ITEM_NONE) mWidgetText->setCoord(mLeftOffset2, mWidgetText->getTop(), mWidgetText->getWidth(), mWidgetText->getHeight());
00363 else mWidgetText->setCoord(mLeftOffset1, mWidgetText->getTop(), mWidgetText->getWidth(), mWidgetText->getHeight());
00364 }
00365
00366 for (VectorWidgetPtr::iterator iter=mVectorButton.begin(); iter!=mVectorButton.end(); ++iter)
00367 {
00368 (*iter)->setCoord(offset, mCoord.height - mButtonOffset.height, mButtonSize.width, mButtonSize.height);
00369 offset += mButtonOffset.width + mButtonSize.width;
00370 }
00371 }
00372
00373 ControllerFadeAlpha* Message::createControllerFadeAlpha(float _alpha, float _coef, bool _enable)
00374 {
00375 ControllerItem* item = ControllerManager::getInstance().createItem(ControllerFadeAlpha::getClassTypeName());
00376 ControllerFadeAlpha* controller = item->castType<ControllerFadeAlpha>();
00377
00378 controller->setAlpha(_alpha);
00379 controller->setCoef(_coef);
00380 controller->setEnabled(_enable);
00381
00382 return controller;
00383 }
00384
00385 void Message::setMessageModal(bool _value)
00386 {
00387 if (_value) InputManager::getInstance().addWidgetModal(this);
00388 else InputManager::getInstance().removeWidgetModal(this);
00389 }
00390
00391 UString Message::getButtonName(MessageBoxStyle _style)
00392 {
00393 size_t index = _style.getButtonIndex();
00394 const char* tag = getButtonTag(index);
00395 UString result = LanguageManager::getInstance().replaceTags(utility::toString("#{", tag, "}"));
00396 if (result == tag) return getButtonName(index);
00397 return result;
00398 }
00399
00400 const char * Message::getButtonName(size_t _index)
00401 {
00402 static const size_t Count = 9;
00403 static const char * Names[Count + 1] = { "Ok", "Yes", "No", "Abort", "Retry", "Ignore", "Cancel", "Try", "Continue", "" };
00404 if (_index >= Count) return Names[Count];
00405 return Names[_index];
00406 }
00407
00408 const char * Message::getButtonTag(size_t _index)
00409 {
00410 static const size_t Count = 9;
00411 static const char * Names[Count + 1] = { "MyGUI_MessageBox_Ok", "MyGUI_MessageBox_Yes", "MyGUI_MessageBox_No", "MyGUI_MessageBox_Abort", "MyGUI_MessageBox_Retry", "MyGUI_MessageBox_Ignore", "MyGUI_MessageBox_Cancel", "MyGUI_MessageBox_Try", "MyGUI_MessageBox_Continue", "" };
00412 if (_index >= Count) return Names[Count];
00413 return Names[_index];
00414 }
00415
00416 void Message::endMessage(MessageBoxStyle _result)
00417 {
00418 _destroyMessage(_result);
00419 }
00420
00421 void Message::endMessage()
00422 {
00423 _destroyMessage(mInfoCancel);
00424 }
00425
00426 void Message::setProperty(const std::string& _key, const std::string& _value)
00427 {
00428 if (_key == "Message_Caption") setCaption(_value);
00429 else if (_key == "Message_Message") setMessageText(_value);
00430 else if (_key == "Message_Modal") setMessageModal(utility::parseValue<bool>(_value));
00431 else if (_key == "Message_Button") setMessageButton(MessageBoxStyle::parse(_value));
00432 else if (_key == "Message_AddButton") addButtonName(_value);
00433 else if (_key == "Message_SmoothShow") setSmoothShow(utility::parseValue<bool>(_value));
00434 else if (_key == "Message_Fade") setWindowFade(utility::parseValue<bool>(_value));
00435 else Base::setProperty(_key, _value);
00436 }
00437
00438 }