summaryrefslogtreecommitdiff
path: root/src/gui/widgets/skillrectanglelistbox.h
blob: 68f9f6f85e5d04d6b5fee372f0242e58a15cf883 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 *  The ManaPlus Client
 *  Copyright (C) 2011-2016  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus 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/>.
 */

#ifndef GUI_WIDGETS_SKILLRECTANGLELISTBOX_H
#define GUI_WIDGETS_SKILLRECTANGLELISTBOX_H

#include "const/resources/skill.h"

#include "dragdrop.h"
#include "settings.h"

#include "gui/skin.h"
#include "gui/viewport.h"

#include "gui/widgets/widget.h"

#include "gui/fonts/font.h"

#include "gui/models/skillmodel.h"

#include "gui/popups/popupmenu.h"
#include "gui/popups/skillpopup.h"

#include "utils/delete2.h"
#include "utils/stringutils.h"

#include "render/graphics.h"

#include "localconsts.h"

class SkillModel;

class SkillRectangleListBox final : public Widget,
                                    public MouseListener
{
    public:
        SkillRectangleListBox(const Widget2 *const widget,
                              SkillModel *const model) :
            Widget(widget),
            MouseListener(),
            mHighlightColor(getThemeColor(ThemeColorId::HIGHLIGHT)),
            mTextColor(getThemeColor(ThemeColorId::TEXT)),
            mTextColor2(getThemeColor(ThemeColorId::TEXT_OUTLINE)),
            mCooldownColor(getThemeColor(ThemeColorId::SKILL_COOLDOWN)),
            mForegroundSelectedColor(getThemeColor(
                ThemeColorId::LISTBOX_SELECTED)),
            mForegroundSelectedColor2(getThemeColor(
                ThemeColorId::LISTBOX_SELECTED_OUTLINE)),
            mModel(model),
            mSkin(nullptr),
            mSelected(-1),
            mPadding(2),
            mBoxWidth(80),
            mBoxHeight(70),
            mIconXOffset(24),
            mIconYOffset(10),
            mTextXOffset(0),
            mTextYOffset(44),
            mSkillClicked(false)
        {
            if (theme)
                mSkin = theme->load("listbox.xml", "");

            if (mSkin)
            {
                mPadding = mSkin->getPadding();
                mBoxWidth = mSkin->getOption("boxWidth", 80);
                mBoxHeight = mSkin->getOption("boxHeight", 70);
                mIconXOffset = mSkin->getOption("iconXOffset", 24);
                mIconYOffset = mSkin->getOption("iconYOffset", 10);
                mTextXOffset = mSkin->getOption("textXOffset", 0);
                mTextYOffset = mSkin->getOption("textYOffset", 44);
            }
            Font *const font = getFont();
            int minWidth = font->getWidth("Lvl: 10/10") + mTextXOffset + 2;
            int minHeight = font->getHeight() + mTextYOffset + 2;
            if (mBoxWidth < minWidth)
                mBoxWidth = minWidth;
            if (mBoxHeight < minHeight)
                mBoxHeight = minHeight;
            int maxX = 0;
            int maxY = 0;
            for (int i = 0;
                 i < model->getNumberOfElements();
                 ++i)
            {
                SkillInfo *const e = model->getSkillAt(i);
                if (e)
                {
                    if (e->x > maxX)
                        maxX = e->x;
                    if (e->y > maxY)
                        maxY = e->y;
                }
            }
            maxX ++;
            maxY ++;
            setWidth(maxX * mBoxWidth);
            setHeight(maxY * mBoxHeight);
            addMouseListener(this);
        }

        A_DELETE_COPY(SkillRectangleListBox)

        ~SkillRectangleListBox()
        {
            delete2(mModel)
        }

        SkillInfo *getSelectedInfo() const
        {
            if (!mModel)
                return nullptr;
            const int selected = mSelected;
            if (selected < 0 ||
                selected > mModel->getNumberOfElements())
            {
                return nullptr;
            }

            return mModel->getSkillAt(selected);
        }

        void draw(Graphics *const graphics) override final A_NONNULL(2)
        {
            if (!mModel)
                return;

            SkillModel *const model = mModel;
            updateAlpha();

            int maxX = 0;
            int maxY = 0;
            mHighlightColor.a = CAST_S32(mAlpha * 255.0F);
            graphics->setColor(mHighlightColor);
            Font *const font = getFont();
            if (mSelected >= 0)
            {
                SkillInfo *const e = model->getSkillAt(mSelected);
                if (e)
                {
                    //const SkillData *const data = e->data;
                    const int x = e->x * mBoxWidth + mPadding;
                    const int y = e->y * mBoxHeight + mPadding;

                    graphics->fillRectangle(Rect(x, y,
                        mBoxWidth, mBoxHeight));

                    const int xOffset = (mBoxWidth -
                        font->getWidth(e->skillLevel)) / 2;
                    font->drawString(graphics,
                        mForegroundSelectedColor,
                        mForegroundSelectedColor,
                        e->skillLevel,
                        x + mTextXOffset + xOffset,
                        y + mTextYOffset);
                }
            }

            // +++ need split drawing icons and text
            for (int i = 0;
                 i < model->getNumberOfElements();
                 ++i)
            {
                SkillInfo *const e = model->getSkillAt(i);
                if (e)
                {
                    if (e->x > maxX)
                        maxX = e->x;
                    if (e->y > maxY)
                        maxY = e->y;
                    const SkillData *const data = e->data;
                    const int x = e->x * mBoxWidth + mPadding;
                    const int y = e->y * mBoxHeight + mPadding;

                    graphics->drawImage(data->icon,
                        x + mIconXOffset,
                        y + mIconYOffset);

                    if (i != mSelected)
                    {
                        const int width1 = font->getWidth(e->skillLevel);
                        const int xOffset = (mBoxWidth -
                            width1) / 2;
                        font->drawString(graphics,
                            mTextColor,
                            mTextColor2,
                            e->skillLevel,
                            x + mTextXOffset + xOffset,
                            y + mTextYOffset);
                        if (e->skillLevelWidth < 0)
                        {
                            // Add one for padding
                            e->skillLevelWidth = width1 + 1;
                        }
                    }
                    else
                    {
                        if (e->skillLevelWidth < 0)
                        {
                            // Add one for padding
                            e->skillLevelWidth = font->getWidth(e->skillLevel) + 1;
                        }
                    }
                }
            }
            maxX ++;
            maxY ++;
            setWidth(maxX * mBoxWidth);
            setHeight(maxY * mBoxHeight);
        }

        void safeDraw(Graphics *const graphics) override final A_NONNULL(2)
        {
            SkillRectangleListBox::draw(graphics);
        }

        const SkillInfo *getSkillByEvent(const MouseEvent &event) const
        {
            if (!mModel)
                return nullptr;
            const int posX = (event.getX() - mPadding) / mBoxWidth;
            const int posY = (event.getY() - mPadding) / mBoxHeight;
            for (int i = 0;
                 i < mModel->getNumberOfElements();
                 ++i)
            {
                SkillInfo *const e = mModel->getSkillAt(i);
                if (e)
                {
                    if (posX == e->x && posY == e->y)
                        return e;
                }
            }
            return nullptr;
        }

        int getSelectionByEvent(const MouseEvent &event) const
        {
            if (!mModel)
                return -1;
            const int posX = (event.getX() - mPadding) / mBoxWidth;
            const int posY = (event.getY() - mPadding) / mBoxHeight;
            for (int i = 0;
                 i < mModel->getNumberOfElements();
                 ++i)
            {
                SkillInfo *const e = mModel->getSkillAt(i);
                if (e)
                {
                    if (posX == e->x && posY == e->y)
                        return i;
                }
            }
            return -1;
        }

        void mouseMoved(MouseEvent &event) override final
        {
            if (!viewport || !dragDrop.isEmpty())
                return;

            const SkillInfo *const skill = getSkillByEvent(event);
            if (!skill)
                return;
            skillPopup->show(skill,
                skill->customSelectedLevel,
                skill->customCastType,
                skill->customOffsetX,
                skill->customOffsetY);
            skillPopup->position(viewport->mMouseX,
                viewport->mMouseY);
        }

        void mouseDragged(MouseEvent &event) override final
        {
            if (event.getButton() != MouseButton::LEFT)
                return;
            setSelected(std::max(0, getSelectionByEvent(event)));

            if (dragDrop.isEmpty())
            {
                if (mSkillClicked)
                {
                    mSkillClicked = false;
                    const SkillInfo *const skill = getSkillByEvent(event);
                    if (!skill)
                        return;
                    dragDrop.dragSkill(skill, DragDropSource::Skills);
                    dragDrop.setItem(skill->id + SKILL_MIN_ID);
                    dragDrop.setItemData(strprintf("%d %d %d",
                        CAST_S32(skill->customCastType),
                        skill->customOffsetX,
                        skill->customOffsetY));
                }
            }
        }

        void mousePressed(MouseEvent &event) override final
        {
            const MouseButtonT button = event.getButton();
            if (button == MouseButton::LEFT ||
                button == MouseButton::RIGHT)
            {
                const SkillInfo *const skill = getSkillByEvent(event);
                if (!skill)
                    return;
                event.consume();
                mSkillClicked = true;
                SkillModel *const model = mModel;
                if (model &&
                    mSelected >= 0 &&
                    model->getSkillAt(mSelected) == skill)
                {
                    skillPopup->hide();
                    if (button == MouseButton::LEFT &&
                        event.getX() >
                        getWidth() - mPadding - skill->skillLevelWidth)
                    {
                        popupMenu->showSkillLevelPopup(skill);
                    }
                    else if (button == MouseButton::RIGHT)
                    {
                        popupMenu->showSkillPopup(skill);
                    }
                }
            }
        }

        void mouseReleased(MouseEvent &event) override final
        {
            if (event.getButton() == MouseButton::LEFT)
            {
                setSelected(std::max(0, getSelectionByEvent(event)));
                distributeActionEvent();
            }
        }

        void mouseExited(MouseEvent &event A_UNUSED) override final
        {
            skillPopup->hide();
        }

        void updateAlpha()
        {
            const float alpha = std::max(settings.guiAlpha,
                theme->getMinimumOpacity());

            if (mAlpha != alpha)
                mAlpha = alpha;
        }

        void setSelected(const int selected)
        {
            if (!mModel)
            {
                mSelected = -1;
            }
            else
            {
                if (selected < 0)
                    mSelected = -1;
                else if (selected >= mModel->getNumberOfElements())
                    mSelected = mModel->getNumberOfElements() - 1;
                else
                    mSelected = selected;
            }
        }

    private:
        Color mHighlightColor;
        Color mTextColor;
        Color mTextColor2;
        Color mCooldownColor;
        Color mForegroundSelectedColor;
        Color mForegroundSelectedColor2;
        SkillModel *mModel;
        Skin *mSkin;
        int mSelected;
        int mPadding;
        int mBoxWidth;
        int mBoxHeight;
        int mIconXOffset;
        int mIconYOffset;
        int mTextXOffset;
        int mTextYOffset;
        bool mSkillClicked;
        static float mAlpha;
};

float SkillRectangleListBox::mAlpha = 1.0;

#endif  // GUI_WIDGETS_SKILLRECTANGLELISTBOX_H