GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/shortcut/emoteshortcut.cpp Lines: 1 41 2.4 %
Date: 2017-11-29 Branches: 0 26 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  Aethyra Development Team
4
 *  Copyright (C) 2011-2017  The ManaPlus Developers
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/shortcut/emoteshortcut.h"
23
24
#include "configuration.h"
25
#include "settings.h"
26
27
#include "being/localplayer.h"
28
29
#include "net/homunculushandler.h"
30
#include "net/mercenaryhandler.h"
31
#include "net/pethandler.h"
32
33
#include "resources/db/emotedb.h"
34
35
#include "debug.h"
36
37
EmoteShortcut *emoteShortcut = nullptr;
38
39
EmoteShortcut::EmoteShortcut() :
40
    mEmoteSelected(0)
41
{
42
    for (int i = 0; i < SHORTCUT_EMOTES; i++)
43
        mEmotes[i] = 0;
44
}
45
46
EmoteShortcut::~EmoteShortcut()
47
{
48
    save();
49
}
50
51
void EmoteShortcut::load()
52
{
53
    for (unsigned char i = 0, j = 0,
54
         fsz = CAST_U8(EmoteDB::getLast());
55
         i <= fsz && j < SHORTCUT_EMOTES;
56
         i++)
57
    {
58
        const EmoteSprite *const sprite = EmoteDB::getSprite(i, true);
59
        if (sprite != nullptr)
60
        {
61
            mEmotes[j] = CAST_U8(i + 1);
62
            j ++;
63
        }
64
    }
65
}
66
67
void EmoteShortcut::save() const
68
{
69
    for (int i = 0; i < SHORTCUT_EMOTES; i++)
70
    {
71
        const unsigned char emoteId = mEmotes[i] != 0u ? mEmotes[i]
72
            : CAST_U8(0);
73
        serverConfig.setValue("emoteshortcut" + toString(i),
74
            CAST_U32(emoteId));
75
    }
76
}
77
78
void EmoteShortcut::useEmotePlayer(const size_t index) const
79
{
80
    if (index <= CAST_SIZE(SHORTCUT_EMOTES))
81
    {
82
        if (mEmotes[index - 1] > 0)
83
            LocalPlayer::emote(mEmotes[index - 1]);
84
    }
85
}
86
87
void EmoteShortcut::useEmote(const size_t index) const
88
{
89
    if (localPlayer == nullptr)
90
        return;
91
92
    if (index <= CAST_SIZE(SHORTCUT_EMOTES))
93
    {
94
        if (mEmotes[index - 1] > 0)
95
        {
96
            const uint8_t emote = mEmotes[index - 1];
97
            switch (settings.emoteType)
98
            {
99
                case EmoteType::Player:
100
                default:
101
                    LocalPlayer::emote(emote);
102
                    break;
103
                case EmoteType::Pet:
104
                    petHandler->emote(emote);
105
                    break;
106
                case EmoteType::Homunculus:
107
                    homunculusHandler->emote(emote);
108
                    break;
109
                case EmoteType::Mercenary:
110
                    mercenaryHandler->emote(emote);
111
                    break;
112
            }
113
        }
114
    }
115
4
}