GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/shortcut/shortcutbase.cpp Lines: 0 49 0.0 %
Date: 2021-03-17 Branches: 0 56 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2009  The Mana World Development Team
4
 *  Copyright (C) 2011-2019  The ManaPlus Developers
5
 *  Copyright (C) 2009-2021  Andrei Karas
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "gui/shortcut/shortcutbase.h"
24
25
#include "configuration.h"
26
27
#include "resources/item/item.h"
28
29
#include "debug.h"
30
31
ShortcutBase::ShortcutBase(const std::string &itemName,
32
                           const std::string &colorName,
33
                           const int maxSize) :
34
    mItems(new int[maxSize]),
35
    mItemColors(new ItemColor[maxSize]),
36
    mItemName(itemName),
37
    mColorName(colorName),
38
    mMaxSize(maxSize),
39
    mItemSelected(-1),
40
    mItemColorSelected(ItemColor_one)
41
{
42
    clear(false);
43
    load();
44
}
45
46
ShortcutBase::~ShortcutBase()
47
{
48
    delete [] mItems;
49
    mItems = nullptr;
50
    delete [] mItemColors;
51
    mItemColors = nullptr;
52
}
53
54
void ShortcutBase::load()
55
{
56
    const Configuration *cfg = &serverConfig;
57
58
    for (size_t i = 0; i < mMaxSize; i++)
59
    {
60
        const std::string num = toString(CAST_S32(i));
61
        const int itemId = cfg->getValue(mItemName + num, -1);
62
        const ItemColor itemColor = fromInt(
63
            cfg->getValue(mColorName + num, -1),
64
            ItemColor);
65
66
        if (itemId != -1)
67
        {
68
            mItems[i] = itemId;
69
            mItemColors[i] = itemColor;
70
        }
71
    }
72
}
73
74
void ShortcutBase::save() const
75
{
76
    for (size_t i = 0; i < mMaxSize; i++)
77
    {
78
        const int itemId = mItems[i] != 0 ? mItems[i] : -1;
79
        const std::string num = toString(CAST_S32(i));
80
        if (itemId != -1)
81
        {
82
            const int itemColor = (mItemColors[i] != ItemColor_zero)
83
                ? toInt(mItemColors[i], int) : 1;
84
            serverConfig.setValue(mItemName + num, itemId);
85
            serverConfig.setValue(mColorName + num, itemColor);
86
        }
87
        else
88
        {
89
            serverConfig.deleteKey(mItemName + num);
90
            serverConfig.deleteKey(mColorName + num);
91
        }
92
    }
93
}
94
95
void ShortcutBase::setItemSelected(const Item *const item)
96
{
97
    if (item != nullptr)
98
    {
99
        mItemSelected = item->getId();
100
        mItemColorSelected = item->getColor();
101
    }
102
    else
103
    {
104
        mItemSelected = -1;
105
        mItemColorSelected = ItemColor_one;
106
    }
107
}
108
109
void ShortcutBase::setItem(const size_t index)
110
{
111
    if (index >= mMaxSize)
112
        return;
113
114
    mItems[index] = mItemSelected;
115
    mItemColors[index] = mItemColorSelected;
116
    save();
117
}
118
119
void ShortcutBase::clear(const bool isSave)
120
{
121
    for (size_t i = 0; i < mMaxSize; i++)
122
    {
123
        mItems[i] = -1;
124
        mItemColors[i] = ItemColor_one;
125
    }
126
    if (isSave)
127
        save();
128
}