1
// SuperTuxKart - a fun racing game with go-kart
2
// Copyright (C) 2010 Marianne Gagnon
4
// This program is free software; you can redistribute it and/or
5
// modify it under the terms of the GNU General Public License
6
// as published by the Free Software Foundation; either version 3
7
// of the License, or (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
#include "guiengine/engine.hpp"
19
#include "guiengine/widgets/bubble_widget.hpp"
21
using namespace irr::core;
22
using namespace irr::gui;
25
using namespace GUIEngine;
27
// ----------------------------------------------------------------------------
29
BubbleWidget::BubbleWidget() : Widget(WTYPE_BUBBLE)
34
// ----------------------------------------------------------------------------
36
void BubbleWidget::add()
38
m_shrinked_size = rect<s32>(m_x, m_y, m_x + m_w - BUBBLE_MARGIN_ON_RIGHT, m_y + m_h);
39
stringw message = getText();
41
EGUI_ALIGNMENT align = EGUIA_UPPERLEFT;
42
if (m_properties[PROP_TEXT_ALIGN] == "center") align = EGUIA_CENTER;
43
else if (m_properties[PROP_TEXT_ALIGN] == "right") align = EGUIA_LOWERRIGHT;
44
EGUI_ALIGNMENT valign = EGUIA_CENTER ; //TODO: make label v-align configurable through XML file?
46
IGUIStaticText* irrwidget;
47
irrwidget = GUIEngine::getGUIEnv()->addStaticText(message.c_str(), m_shrinked_size,
48
false, true /* word wrap */, m_parent, getNewID());
50
// find expanded bubble size
51
int text_height = irrwidget->getTextHeight();
53
m_expanded_size = m_shrinked_size;
54
const int additionalNeededSize = std::max(0, text_height - m_shrinked_size.getHeight());
56
if (additionalNeededSize > 0)
58
m_expanded_size.UpperLeftCorner.Y -= additionalNeededSize/2 + 10;
59
m_expanded_size.LowerRightCorner.Y += additionalNeededSize/2 + 10;
61
// reduce text to fit in the available space if it's too long
62
while (text_height > m_shrinked_size.getHeight())
64
message = message.subString(0, message.size() - 10) + "...";
65
irrwidget->setText(message.c_str());
66
text_height = irrwidget->getTextHeight();
69
m_shrinked_text = message;
71
m_element = irrwidget;
72
irrwidget->setTextAlignment( align, valign );
74
m_id = m_element->getID();
76
m_element->setTabOrder(m_id);
77
m_element->setTabStop(true);
79
m_element->setNotClipped(true);
80
irrwidget->setDrawBorder(true);
83
void BubbleWidget::updateSize()
85
core::rect<s32> currsize = m_shrinked_size;
87
const int y1_top = m_shrinked_size.UpperLeftCorner.Y;
88
const int y1_bottom = m_shrinked_size.LowerRightCorner.Y;
90
const int y2_top = m_expanded_size.UpperLeftCorner.Y;
91
const int y2_bottom = m_expanded_size.LowerRightCorner.Y;
93
currsize.UpperLeftCorner.Y = (int)(y1_top + (y2_top - y1_top)*m_zoom);
94
currsize.LowerRightCorner.Y = (int)(y1_bottom
95
+(y2_bottom - y1_bottom)*m_zoom);
97
m_element->setRelativePosition(currsize);
101
getIrrlichtElement<IGUIStaticText>()->setText(getText().c_str());
105
getIrrlichtElement<IGUIStaticText>()->setText(m_shrinked_text.c_str());
109
// ----------------------------------------------------------------------------
111
EventPropagation BubbleWidget::focused(const int playerID)
113
if (m_element != NULL)
115
// bring element to top (with a hack because irrlicht does not appear to offer a built-in way to do this)
118
IGUIElement* parent = m_parent;
119
if (parent == NULL) parent = GUIEngine::getGUIEnv()->getRootGUIElement();
121
parent->removeChild(m_element);
122
parent->addChild(m_element);
128
// ----------------------------------------------------------------------------
130
void BubbleWidget::unfocused(const int playerID)
132
if (m_element != NULL)
134
IGUIStaticText* widget = getIrrlichtElement<IGUIStaticText>();
135
//widget->setDrawBorder(false);
136
widget->setRelativePosition(m_shrinked_size);
137
widget->setText(m_shrinked_text.c_str());
141
// ----------------------------------------------------------------------------