~ubuntu-branches/ubuntu/precise/supertuxkart/precise

« back to all changes in this revision

Viewing changes to src/guiengine/widgets/bubble_widget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2011-02-24 22:36:25 UTC
  • mfrom: (1.1.9 upstream) (6.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110224223625-ygrjfpg92obovuch
Tags: 0.7+dfsg1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  SuperTuxKart - a fun racing game with go-kart
 
2
//  Copyright (C) 2010 Marianne Gagnon
 
3
//
 
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.
 
8
//
 
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.
 
13
//
 
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.
 
17
 
 
18
#include "guiengine/engine.hpp"
 
19
#include "guiengine/widgets/bubble_widget.hpp"
 
20
#include <irrlicht.h>
 
21
using namespace irr::core;
 
22
using namespace irr::gui;
 
23
using namespace irr;
 
24
 
 
25
using namespace GUIEngine;
 
26
 
 
27
// ----------------------------------------------------------------------------
 
28
 
 
29
BubbleWidget::BubbleWidget() : Widget(WTYPE_BUBBLE)
 
30
{
 
31
    m_zoom = 0.0f;
 
32
}
 
33
 
 
34
// ----------------------------------------------------------------------------
 
35
 
 
36
void BubbleWidget::add()
 
37
{
 
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();
 
40
        
 
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?
 
45
    
 
46
    IGUIStaticText* irrwidget;
 
47
    irrwidget = GUIEngine::getGUIEnv()->addStaticText(message.c_str(), m_shrinked_size,
 
48
                                                      false, true /* word wrap */, m_parent, getNewID());
 
49
        
 
50
    // find expanded bubble size
 
51
    int text_height = irrwidget->getTextHeight();
 
52
 
 
53
    m_expanded_size = m_shrinked_size;
 
54
    const int additionalNeededSize = std::max(0, text_height - m_shrinked_size.getHeight());
 
55
 
 
56
    if (additionalNeededSize > 0)
 
57
    {
 
58
        m_expanded_size.UpperLeftCorner.Y  -= additionalNeededSize/2 + 10;
 
59
        m_expanded_size.LowerRightCorner.Y += additionalNeededSize/2 + 10;
 
60
        
 
61
        // reduce text to fit in the available space if it's too long
 
62
        while (text_height > m_shrinked_size.getHeight())
 
63
        {
 
64
            message = message.subString(0, message.size() - 10) + "...";
 
65
            irrwidget->setText(message.c_str());
 
66
            text_height = irrwidget->getTextHeight();
 
67
        }
 
68
    }
 
69
    m_shrinked_text = message;
 
70
    
 
71
    m_element = irrwidget;
 
72
    irrwidget->setTextAlignment( align, valign );
 
73
    
 
74
    m_id = m_element->getID();
 
75
    
 
76
    m_element->setTabOrder(m_id);
 
77
    m_element->setTabStop(true);
 
78
    
 
79
    m_element->setNotClipped(true);
 
80
    irrwidget->setDrawBorder(true);
 
81
}
 
82
 
 
83
void BubbleWidget::updateSize()
 
84
{
 
85
    core::rect<s32> currsize = m_shrinked_size;
 
86
    
 
87
    const int y1_top    = m_shrinked_size.UpperLeftCorner.Y;
 
88
    const int y1_bottom = m_shrinked_size.LowerRightCorner.Y;
 
89
    
 
90
    const int y2_top    = m_expanded_size.UpperLeftCorner.Y;
 
91
    const int y2_bottom = m_expanded_size.LowerRightCorner.Y;
 
92
    
 
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);
 
96
 
 
97
    m_element->setRelativePosition(currsize);
 
98
    
 
99
    if (m_zoom > 0.5f)
 
100
    {
 
101
        getIrrlichtElement<IGUIStaticText>()->setText(getText().c_str());
 
102
    }
 
103
    else
 
104
    {
 
105
        getIrrlichtElement<IGUIStaticText>()->setText(m_shrinked_text.c_str());
 
106
    }
 
107
}
 
108
 
 
109
// ----------------------------------------------------------------------------
 
110
 
 
111
EventPropagation BubbleWidget::focused(const int playerID)
 
112
{
 
113
    if (m_element != NULL)
 
114
    {
 
115
        // bring element to top (with a hack because irrlicht does not appear to offer a built-in way to do this)
 
116
        m_element->grab();
 
117
        
 
118
        IGUIElement* parent = m_parent;
 
119
        if (parent == NULL) parent = GUIEngine::getGUIEnv()->getRootGUIElement();
 
120
        
 
121
        parent->removeChild(m_element);
 
122
        parent->addChild(m_element);
 
123
        m_element->drop();
 
124
    }
 
125
    return EVENT_LET;
 
126
}
 
127
 
 
128
// ----------------------------------------------------------------------------
 
129
/*
 
130
void BubbleWidget::unfocused(const int playerID)
 
131
{
 
132
    if (m_element != NULL)
 
133
    {
 
134
        IGUIStaticText* widget = getIrrlichtElement<IGUIStaticText>();
 
135
        //widget->setDrawBorder(false);
 
136
        widget->setRelativePosition(m_shrinked_size);
 
137
        widget->setText(m_shrinked_text.c_str());
 
138
    }
 
139
}
 
140
*/
 
141
// ----------------------------------------------------------------------------