GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/speechbubble.cpp Lines: 20 41 48.8 %
Date: 2021-03-17 Branches: 16 44 36.4 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  The Legend of Mazzeroth Development Team
4
 *  Copyright (C) 2008-2009  The Mana World Development Team
5
 *  Copyright (C) 2009-2010  The Mana Developers
6
 *  Copyright (C) 2011-2019  The ManaPlus Developers
7
 *  Copyright (C) 2019-2021  Andrei Karas
8
 *
9
 *  This file is part of The ManaPlus Client.
10
 *
11
 *  This program is free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  any later version.
15
 *
16
 *  This program is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU General Public License
22
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
#include "gui/popups/speechbubble.h"
26
27
#include "gui/gui.h"
28
#include "gui/skin.h"
29
#include "gui/viewport.h"
30
31
#include "gui/fonts/font.h"
32
33
#include "gui/widgets/label.h"
34
#include "gui/widgets/staticbrowserbox.h"
35
36
#include "debug.h"
37
38
1
SpeechBubble::SpeechBubble() :
39
    Popup("Speech", "speechbubble.xml"),
40
    mText(),
41


4
    mSpacing(mSkin != nullptr ? mSkin->getOption("spacing") : 2),
42

1
    mCaption(new Label(this)),
43
    mSpeechBox(new StaticBrowserBox(this, Opaque_true,
44


13
        "speechbrowserbox.xml"))
45
{
46
1
    setContentSize(140, 46);
47
1
    setMinWidth(8);
48
1
    setMinHeight(8);
49
50
1
    mCaption->setFont(boldFont);
51
2
    mSpeechBox->setOpaque(Opaque_false);
52
3
    mSpeechBox->setForegroundColorAll(
53
        getThemeColor(ThemeColorId::BUBBLE_TEXT, 255U),
54
1
        getThemeColor(ThemeColorId::BUBBLE_TEXT_OUTLINE, 255U));
55
1
}
56
57
1
void SpeechBubble::postInit()
58
{
59
2
    Popup::postInit();
60
1
    add(mCaption);
61
1
    add(mSpeechBox);
62
1
    requestMoveToBackground();
63
1
}
64
65
void SpeechBubble::setCaption(const std::string &name,
66
                              const Color *const color1,
67
                              const Color *const color2)
68
{
69
    mCaption->setCaption(name);
70
    mCaption->adjustSize();
71
    mCaption->setForegroundColorAll(*color1, *color2);
72
}
73
74
void SpeechBubble::setText(const std::string &text, const bool showName)
75
{
76
    if (text == mText && (mCaption->getWidth() <= mSpeechBox->getWidth()))
77
        return;
78
79
    mSpeechBox->setForegroundColorAll(
80
        getThemeColor(ThemeColorId::BUBBLE_TEXT, 255U),
81
        getThemeColor(ThemeColorId::BUBBLE_TEXT_OUTLINE, 255U));
82
83
    int width = mCaption->getWidth();
84
    mSpeechBox->clearRows();
85
    mSpeechBox->addRow(text,
86
        false);
87
    mSpeechBox->updateHeight();
88
89
    const int speechWidth = mSpeechBox->getWidth();
90
    const int nameHeight = showName ? mCaption->getHeight() + mSpacing : 0;
91
92
    if (width < speechWidth)
93
        width = speechWidth;
94
95
    setContentSize(width, getFont()->getHeight() + nameHeight);
96
    mCaption->setPosition(0, 0);
97
    mSpeechBox->setPosition(0, nameHeight);
98
}
99
100
void SpeechBubble::requestMoveToBackground()
101
{
102
1
    windowContainer->moveWidgetAfter(viewport, this);
103
2
}