1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
/*!
@file
@author Albert Semenov
@date 12/2010
*/
#ifndef MESSAGE_BOX_H_
#define MESSAGE_BOX_H_
#include <MyGUI.h>
#include "MessageBoxStyle.h"
#include "BaseLayout/BaseLayout.h"
namespace MyGUI
{
class Message;
typedef delegates::CMultiDelegate2<Message*, MessageBoxStyle> EventHandle_MessageBoxPtrMessageStyle;
class Message :
public wraps::BaseLayout
{
public:
Message() :
wraps::BaseLayout("MessageBox.layout"),
mWidgetText(nullptr),
mInfoOk(MessageBoxStyle::None),
mInfoCancel(MessageBoxStyle::None),
mSmoothShow(false),
mIcon(nullptr),
mLeftOffset1(0),
mLeftOffset2(0)
{
initialise();
}
Message(const std::string& _layoutName) :
wraps::BaseLayout(_layoutName),
mWidgetText(nullptr),
mInfoOk(MessageBoxStyle::None),
mInfoCancel(MessageBoxStyle::None),
mSmoothShow(false),
mIcon(nullptr),
mLeftOffset1(0),
mLeftOffset2(0)
{
initialise();
}
virtual ~Message()
{
mWidgetText = nullptr;
mIcon = nullptr;
}
/** Set caption text*/
void setCaption(const UString& _value)
{
mMainWidget->castType<Window>()->setCaption(_value);
}
/** Set message text*/
void setMessageText(const UString& _value)
{
if (mWidgetText != nullptr)
mWidgetText->setCaption(_value);
updateSize();
}
/** Create button with specific name*/
MessageBoxStyle addButtonName(const UString& _name)
{
if (mVectorButton.size() >= MessageBoxStyle::_CountUserButtons)
{
MYGUI_LOG(Warning, "Too many buttons in message box, ignored");
return MessageBoxStyle::None;
}
// бит, номер кнопки + смещение до Button1
MessageBoxStyle info = MessageBoxStyle(MessageBoxStyle::Enum(MYGUI_FLAG(mVectorButton.size() + MessageBoxStyle::_IndexUserButton1)));
// запоминаем кнопки для отмены и подтверждения
if (mVectorButton.empty())
mInfoOk = info;
mInfoCancel = info;
Widget* widget = mMainWidget->createWidgetT(mButtonType, mButtonSkin, IntCoord(), Align::Left | Align::Bottom);
Button* button = widget->castType<Button>();
button->eventMouseButtonClick += newDelegate(this, &Message::notifyButtonClick);
button->setCaption(_name);
button->_setInternalData(info);
mVectorButton.push_back(button);
updateSize();
return info;
}
/** Set smooth message showing*/
void setSmoothShow(bool _value)
{
mSmoothShow = _value;
if (mSmoothShow)
{
mMainWidget->setAlpha(ALPHA_MIN);
mMainWidget->setVisible(true);
mMainWidget->castType<Window>()->setVisibleSmooth(true);
}
}
/** Set message icon*/
void setMessageIcon(MessageBoxStyle _value)
{
if (nullptr == mIcon)
return;
if (mIcon->getItemResource() != nullptr)
{
mIcon->setItemName(getIconName(_value.getIconIndex()));
}
else
{
mIcon->setImageIndex(_value.getIconIndex());
}
updateSize();
}
void endMessage(MessageBoxStyle _result)
{
_destroyMessage(_result);
}
void endMessage()
{
_destroyMessage(mInfoCancel);
}
/** Create button using MessageBoxStyle*/
void setMessageButton(MessageBoxStyle _value)
{
clearButton();
std::vector<MessageBoxStyle> buttons = _value.getButtons();
for (size_t index = 0; index < buttons.size(); ++index)
{
// корректируем ее номер
MessageBoxStyle info = buttons[index];
// если бит есть то ставим кнопку
addButtonName(getButtonName(info));
// внутри адд сбрасывается
mVectorButton.back()->_setInternalData(info);
// первая кнопка
if (mVectorButton.size() == 1)
mInfoOk = info;
// последняя кнопка
mInfoCancel = info;
}
updateSize();
}
/** Set message style (button and icon)*/
void setMessageStyle(MessageBoxStyle _value)
{
setMessageButton(_value);
setMessageIcon(_value);
}
void setMessageModal(bool _value)
{
if (_value)
InputManager::getInstance().addWidgetModal(mMainWidget);
else
InputManager::getInstance().removeWidgetModal(mMainWidget);
}
/** Static method for creating message with one command
@param
_modal if true all other GUI elements will be blocked untill message is closed
@param
_style any combination of flags from ViewValueInfo
@param
_button1 ... _button4 specific buttons names
*/
static Message* createMessageBox(
//const UString& _skinName,
const UString& _caption,
const UString& _message,
MessageBoxStyle _style = MessageBoxStyle::Ok | MessageBoxStyle::IconDefault,
const std::string& _layer = "",
bool _modal = true,
const std::string& _button1 = "",
const std::string& _button2 = "",
const std::string& _button3 = "",
const std::string& _button4 = "")
{
Message* mess = new Message();
mess->setCaption(_caption);
mess->setMessageText(_message);
mess->setSmoothShow(true);
mess->setMessageStyle(_style);
if (!_button1.empty())
{
mess->addButtonName(_button1);
if (!_button2.empty())
{
mess->addButtonName(_button2);
if (!_button3.empty())
{
mess->addButtonName(_button3);
}
}
}
if (_modal)
InputManager::getInstance().addWidgetModal(mess->mMainWidget);
return mess;
}
/*events:*/
/** Event : button on message window pressed.\n
signature : void method(tools::Message* _sender, MessageBoxStyle _result)\n
@param _sender widget that called this event
@param _result - id of pressed button
*/
EventHandle_MessageBoxPtrMessageStyle
eventMessageBoxResult;
protected:
void updateSize()
{
ISubWidgetText* text = nullptr;
if (mWidgetText != nullptr)
text = mWidgetText->getSubWidgetText();
IntSize size = text == nullptr ? IntSize() : text->getTextSize();
// минимум высота иконки
if ((nullptr != mIcon) && (mIcon->getImageIndex() != ITEM_NONE))
{
if (size.height < mIcon->getHeight())
size.height = mIcon->getHeight();
size.width += mIcon->getSize().width;
}
size += mOffsetText;
size.width += 3;
int width = ((int)mVectorButton.size() * mButtonSize.width) + (((int)mVectorButton.size() + 1) * mButtonOffset.width);
if (size.width < width)
size.width = width;
int offset = (size.width - width) / 2;
offset += mButtonOffset.width;
size.width += mMainWidget->getWidth() - mMainWidget->getClientCoord().width;
size.height += mMainWidget->getHeight() - mMainWidget->getClientCoord().height;
const IntSize& view = RenderManager::getInstance().getViewSize();
mMainWidget->setCoord((view.width - size.width) / 2, (view.height - size.height) / 2, size.width, size.height);
if (nullptr != mIcon)
{
if (mWidgetText != nullptr)
{
if (mIcon->getImageIndex() != ITEM_NONE)
mWidgetText->setCoord(mLeftOffset2, mWidgetText->getTop(), mWidgetText->getWidth(), mWidgetText->getHeight());
else
mWidgetText->setCoord(mLeftOffset1, mWidgetText->getTop(), mWidgetText->getWidth(), mWidgetText->getHeight());
}
}
for (std::vector<Button*>::iterator iter = mVectorButton.begin(); iter != mVectorButton.end(); ++iter)
{
(*iter)->setCoord(offset, mMainWidget->getClientCoord().height - mButtonOffset.height, mButtonSize.width, mButtonSize.height);
offset += mButtonOffset.width + mButtonSize.width;
}
}
void notifyButtonClick(Widget* _sender)
{
_destroyMessage(*_sender->_getInternalData<MessageBoxStyle>());
}
void clearButton()
{
for (std::vector<Button*>::iterator iter = mVectorButton.begin(); iter != mVectorButton.end(); ++iter)
WidgetManager::getInstance().destroyWidget(*iter);
mVectorButton.clear();
}
/*void onKeyButtonPressed(KeyCode _key, Char _char)
{
Base::onKeyButtonPressed(_key, _char);
if ((_key == KeyCode::Return) || (_key == KeyCode::NumpadEnter))
_destroyMessage(mInfoOk);
else if (_key == KeyCode::Escape)
_destroyMessage(mInfoCancel);
}*/
void _destroyMessage(MessageBoxStyle _result)
{
eventMessageBoxResult(this, _result);
delete this;
}
UString getButtonName(MessageBoxStyle _style) const
{
size_t index = _style.getButtonIndex();
const char* tag = getButtonTag(index);
UString result = LanguageManager::getInstance().replaceTags(utility::toString("#{", tag, "}"));
if (result == tag)
return getButtonName(index);
return result;
}
const char* getIconName(size_t _index) const
{
static const size_t CountIcons = 4;
static const char* IconNames[CountIcons + 1] = { "Info", "Quest", "Error", "Warning", "" };
if (_index >= CountIcons)
return IconNames[CountIcons];
return IconNames[_index];
}
const char* getButtonName(size_t _index) const
{
static const size_t Count = 9;
static const char * Names[Count + 1] = { "Ok", "Yes", "No", "Abort", "Retry", "Ignore", "Cancel", "Try", "Continue", "" };
if (_index >= Count)
return Names[Count];
return Names[_index];
}
const char* getButtonTag(size_t _index) const
{
static const size_t Count = 9;
static const char* Names[Count + 1] = { "MessageBox_Ok", "MessageBox_Yes", "MessageBox_No", "MessageBox_Abort", "MessageBox_Retry", "MessageBox_Ignore", "MessageBox_Cancel", "MessageBox_Try", "MessageBox_Continue", "" };
if (_index >= Count)
return Names[Count];
return Names[_index];
}
private:
void initialise()
{
assignWidget(mWidgetText, "Text", false);
if (mWidgetText != nullptr)
{
mOffsetText.set(mMainWidget->getClientCoord().width - mWidgetText->getWidth(), mMainWidget->getClientCoord().height - mWidgetText->getHeight());
mLeftOffset2 = mLeftOffset1 = mWidgetText->getLeft();
}
assignWidget(mIcon, "Icon", false);
if (mIcon != nullptr)
{
mLeftOffset2 = mIcon->getRight() + 3;
}
mButtonType = Button::getClassTypeName();
if (mMainWidget->isUserString("ButtonSkin"))
mButtonSkin = mMainWidget->getUserString("ButtonSkin");
Widget* widget = nullptr;
assignWidget(widget, "ButtonPlace", false);
if (widget != nullptr)
{
mButtonOffset.set(widget->getLeft(), mMainWidget->getClientCoord().height - widget->getTop());
widget->setVisible(false);
}
assignWidget(widget, "ButtonTemplate", false);
if (widget != nullptr)
{
mButtonSize = widget->getSize();
}
Window* window = mMainWidget->castType<Window>(false);
if (window != nullptr)
window->eventWindowButtonPressed += newDelegate(this, &Message::notifyWindowButtonPressed);
}
void notifyWindowButtonPressed(MyGUI::Window* _sender, const std::string& _name)
{
if (_name == "close")
endMessage();
}
private:
IntSize mOffsetText;
TextBox* mWidgetText;
std::string mButtonSkin;
std::string mButtonType;
IntSize mButtonSize;
IntSize mButtonOffset;
std::vector<Button*> mVectorButton;
MessageBoxStyle mInfoOk;
MessageBoxStyle mInfoCancel;
bool mSmoothShow;
std::string mDefaultCaption;
ImageBox* mIcon;
int mLeftOffset1;
int mLeftOffset2;
};
} // namespace MyGUI
#endif // MESSAGE_BOX_H_
|