summaryrefslogtreecommitdiff
path: root/src/gui/questswindow.cpp
blob: adbbdedaa0dae8dffe652908784e6e3b315b9e95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 *  The Mana Client
 *  Copyright (C) 2025  The Mana Developers
 *
 *  This file is part of The Mana Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "questswindow.h"

#include "configuration.h"

#include "gui/setup.h"

#include "gui/widgets/browserbox.h"
#include "gui/widgets/checkbox.h"
#include "gui/widgets/itemlinkhandler.h"
#include "gui/widgets/layout.h"
#include "gui/widgets/listbox.h"
#include "gui/widgets/scrollarea.h"

#include "net/net.h"
#include "net/playerhandler.h"

#include "resources/questdb.h"

#include "utils/gettext.h"

#include <guichan/font.hpp>

#include <algorithm>

class QuestsModel final : public gcn::ListModel
{
public:
    int getNumberOfElements() override
    { return mQuests.size(); }

    std::string getElementAt(int i) override
    { return mQuests[i].name(); }

    const std::vector<QuestEntry> &getQuests() const
    { return mQuests; }

    void setQuests(const std::vector<QuestEntry> &quests)
    { mQuests = quests; }

private:
    std::vector<QuestEntry> mQuests;
};


class QuestsListBox final : public ListBox
{
public:
    QuestsListBox(QuestsModel *model)
        : ListBox(model)
    {}

    unsigned getRowHeight() const override;

    void draw(gcn::Graphics *graphics) override;
};

unsigned QuestsListBox::getRowHeight() const
{
    auto rowHeight = ListBox::getRowHeight();

    if (auto icon = gui->getTheme()->getIcon("complete"))
        rowHeight = std::max<unsigned>(rowHeight, icon->getHeight() + 2);

    return rowHeight;
}

void QuestsListBox::draw(gcn::Graphics *gcnGraphics)
{
    if (!mListModel)
        return;

    auto *graphics = static_cast<Graphics *>(gcnGraphics);
    auto *model = static_cast<QuestsModel *>(getListModel());

    const int rowHeight = getRowHeight();

    auto theme = gui->getTheme();
    auto completeIcon = theme->getIcon("complete");
    auto incompleteIcon = theme->getIcon("incomplete");

    // Draw filled rectangle around the selected list element
    if (mSelected >= 0)
    {
        auto highlightColor = Theme::getThemeColor(Theme::HIGHLIGHT);
        highlightColor.a = gui->getTheme()->getGuiAlpha();
        graphics->setColor(highlightColor);
        graphics->fillRectangle(gcn::Rectangle(0, rowHeight * mSelected,
                                               getWidth(), rowHeight));
    }

    // Draw the list elements
    graphics->setFont(getFont());
    graphics->setColor(Theme::getThemeColor(Theme::TEXT));

    const int fontHeight = getFont()->getHeight();
    int y = 0;
    for (auto &quest : model->getQuests())
    {
        int x = 1;

        if (const Image *icon = quest.completed ? completeIcon : incompleteIcon)
        {
            graphics->drawImage(icon, x, y + (rowHeight - icon->getHeight()) / 2);
            x += icon->getWidth() + 4;
        }

        graphics->drawText(quest.name(), x, y + (rowHeight - fontHeight) / 2);
        y += rowHeight;
    }
}


QuestsWindow::QuestsWindow()
    : Window(_("Quests"))
    , mQuestsModel(std::make_unique<QuestsModel>())
    , mQuestsListBox(new QuestsListBox(mQuestsModel.get()))
    , mHideCompletedCheckBox(new CheckBox(_("Hide completed"), config.hideCompletedQuests))
    , mQuestDetails(new BrowserBox(BrowserBox::AUTO_WRAP))
    , mLinkHandler(std::make_unique<ItemLinkHandler>())
{
    setWindowName("Quests");
    setupWindow->registerWindowForReset(this);
    setResizable(true);
    setCloseButton(true);
    setSaveVisible(true);

    setDefaultSize(387, 307, WindowAlignment::Center);
    setMinWidth(316);
    setMinHeight(179);

    mQuestsListBox->addSelectionListener(this);
    mHideCompletedCheckBox->setActionEventId("hideCompleted");
    mHideCompletedCheckBox->addActionListener(this);

    auto questListScrollArea = new ScrollArea(mQuestsListBox);
    questListScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);

    mQuestDetails->setLinkHandler(mLinkHandler.get());
    mQuestDetailsScrollArea = new ScrollArea(mQuestDetails);
    mQuestDetailsScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);

    auto place = getPlacer(0, 0);
    place(0, 0, questListScrollArea, 2, 2).setPadding(2);
    place(2, 0, mQuestDetailsScrollArea, 3, 2).setPadding(2);
    place = getPlacer(0, 1);
    place(0, 0, mHideCompletedCheckBox);

    getLayout().setRowHeight(1, 0); // Don't scale up the bottom row

    listen(Event::QuestsChannel);

    refreshQuestList();
    loadWindowState();
}

void QuestsWindow::action(const gcn::ActionEvent &event)
{
    if (event.getId() == "hideCompleted")
    {
        config.hideCompletedQuests = mHideCompletedCheckBox->isSelected();
        refreshQuestList();
    }
}

void QuestsWindow::valueChanged(const gcn::SelectionEvent &event)
{
    if (mSelectedQuestIndex != mQuestsListBox->getSelected())
        updateQuestDetails();
}

void QuestsWindow::event(Event::Channel channel, const Event &event)
{
    if (channel == Event::QuestsChannel)
    {
        if (event.getType() == Event::QuestVarsChanged)
            refreshQuestList();
    }
}

void QuestsWindow::refreshQuestList()
{
    // Store the currently selected quest state and varId to preserve selection
    const QuestState *selectedQuestState = nullptr;
    int selectedVarId = -1;
    if (mSelectedQuestIndex >= 0 && mSelectedQuestIndex < mQuestsModel->getNumberOfElements())
    {
        const auto &selectedQuest = mQuestsModel->getQuests().at(mSelectedQuestIndex);
        selectedQuestState = selectedQuest.state;
        selectedVarId = selectedQuest.varId;
    }

    auto &questVars = Net::getPlayerHandler()->getQuestVars();
    auto newQuests = QuestDB::getQuestsEntries(questVars, config.hideCompletedQuests);

    // Put completed quests at the top
    std::stable_sort(newQuests.begin(), newQuests.end(), [](const QuestEntry &a, const QuestEntry &b) {
        return a.completed > b.completed;
    });

    mQuestsModel->setQuests(newQuests);

    if (!selectedQuestState)
        return;

    // Try to find and reselect the same quest, preferring exact state match
    int newSelectedIndex = -1;

    for (int i = 0; i < static_cast<int>(newQuests.size()); ++i)
    {
        if (newQuests[i].state == selectedQuestState)
        {
            newSelectedIndex = i;
            break;
        }
        else if (newSelectedIndex == -1 && newQuests[i].varId == selectedVarId)
        {
            newSelectedIndex = i;
            // Don't break here - continue looking for exact state match
        }
    }

    if (mSelectedQuestIndex != newSelectedIndex)
        mQuestsListBox->setSelected(newSelectedIndex);
    else
        updateQuestDetails();
}

void QuestsWindow::updateQuestDetails()
{
    mQuestDetails->clearRows();

    mSelectedQuestIndex = mQuestsListBox->getSelected();
    if (mSelectedQuestIndex < 0 || mSelectedQuestIndex >= mQuestsModel->getNumberOfElements())
        return;

    const QuestEntry &quest = mQuestsModel->getQuests().at(mSelectedQuestIndex);
    for (const auto &row : quest.rows())
    {
        switch (row.type)
        {
        case QuestRowType::Text:
            mQuestDetails->addRow(row.text);
            break;
        case QuestRowType::Name:
            mQuestDetails->addRow("[" + row.text + "]");
            break;
        case QuestRowType::Reward:
            mQuestDetails->addRow(strprintf(_("Reward: %s"), row.text.c_str()));
            break;
        case QuestRowType::Giver:
            mQuestDetails->addRow(strprintf(_("Quest Giver: %s"), row.text.c_str()));
            break;
        case QuestRowType::Coordinates:
            mQuestDetails->addRow(strprintf(_("Coordinates: %s (%d, %d)"),
                                            row.text.c_str(), row.x, row.y));
            break;
        case QuestRowType::NPC:
            mQuestDetails->addRow(strprintf(_("NPC: %s"), row.text.c_str()));
            break;
        }
    }
}