summaryrefslogtreecommitdiff
path: root/src/gui/scrollarea.cpp
blob: b1afa183b7b873f2742a9a35afe3b1c14d87f19a (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
/*
 *  The Mana World
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World 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.
 *
 *  The Mana World 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 The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id$
 */

#include "scrollarea.h"
#include "gui.h"
#include "../resources/resourcemanager.h"
#include "../main.h"

ScrollArea::ScrollArea():
    gcn::ScrollArea()
{
    init();
}

ScrollArea::ScrollArea(gcn::Widget *widget):
    gcn::ScrollArea(widget)
{
    init();
}

void ScrollArea::init()
{
    // Draw background by default
    setBorderSize(2);
    opaque = true;

    // Load the background skin
    ResourceManager *resman = ResourceManager::getInstance();
    Image *textbox = resman->getImage("graphics/gui/textbox.png");
    int bggridx[4] = {0, 9, 16, 25};
    int bggridy[4] = {0, 4, 19, 24};
    int a = 0, x, y;

    // Load GUI alpha setting
    guiAlpha = config.getValue("guialpha", 0.8f);

    // Set GUI alpha level
    textbox->setAlpha(guiAlpha);

    for (y = 0; y < 3; y++) {
        for (x = 0; x < 3; x++) {
            background.grid[a] = textbox->getSubImage(
                    bggridx[x], bggridy[y],
                    bggridx[x + 1] - bggridx[x] + 1,
                    bggridy[y + 1] - bggridy[y] + 1);
            a++;
        }
    }

    // Load vertical scrollbar skin
    Image *vscroll = resman->getImage("graphics/gui/vscroll_grey.png");
    int vsgridx[4] = {0, 4, 7, 11};
    int vsgridy[4] = {0, 4, 15, 19};
    a = 0;

    for (y = 0; y < 3; y++) {
        for (x = 0; x < 3; x++) {
            vMarker.grid[a] = vscroll->getSubImage(
                    vsgridx[x], vsgridy[y],
                    vsgridx[x + 1] - vsgridx[x],
                    vsgridy[y + 1] - vsgridy[y]);
            a++;
        }
    }

    hscroll_left_default = resman->getImage("graphics/gui/hscroll_left_default.png");
    hscroll_right_default = resman->getImage("graphics/gui/hscroll_right_default.png");
    vscroll_down_default = resman->getImage("graphics/gui/vscroll_down_default.png");
    vscroll_up_default = resman->getImage("graphics/gui/vscroll_up_default.png");
    hscroll_left_pressed = resman->getImage("graphics/gui/hscroll_left_pressed.png");
    hscroll_right_pressed = resman->getImage("graphics/gui/hscroll_right_pressed.png");
    vscroll_down_pressed = resman->getImage("graphics/gui/vscroll_down_pressed.png");
    vscroll_up_pressed = resman->getImage("graphics/gui/vscroll_up_pressed.png");
}

void ScrollArea::draw(gcn::Graphics *graphics)
{
    checkPolicies();

    int alpha = getBaseColor().a;
    gcn::Color highlightColor = getBaseColor() + 0x303030;
    highlightColor.a = alpha;
    gcn::Color shadowColor = getBaseColor() - 0x303030;
    shadowColor.a = alpha;

    if (mVBarVisible)
    {
        drawUpButton(graphics);
        drawDownButton(graphics);
        drawVBar(graphics);
        drawVMarker(graphics);
    }

    if (mHBarVisible)
    {
        drawLeftButton(graphics);
        drawRightButton(graphics);
        drawHBar(graphics);
        drawHMarker(graphics);
    }

    if (mHBarVisible && mVBarVisible)
    {
        graphics->setColor(getBaseColor());
        graphics->fillRectangle(gcn::Rectangle(getWidth() - mScrollbarWidth,
                    getHeight() - mScrollbarWidth,
                    mScrollbarWidth,
                    mScrollbarWidth));
    }

    if (mContent)
    {
        gcn::Rectangle contdim = mContent->getDimension();
        graphics->pushClipArea(getContentDimension());

        if (mContent->getBorderSize() > 0)
        {
            gcn::Rectangle rec = mContent->getDimension();
            rec.x -= mContent->getBorderSize();
            rec.y -= mContent->getBorderSize();
            rec.width += 2 * mContent->getBorderSize();
            rec.height += 2 * mContent->getBorderSize();
            graphics->pushClipArea(rec);
            mContent->drawBorder(graphics);
            graphics->popClipArea();
        }

        graphics->pushClipArea(contdim);
        mContent->draw(graphics);
        graphics->popClipArea();
        graphics->popClipArea();
    }
}

void ScrollArea::drawBorder(gcn::Graphics *graphics)
{
    int x, y, w, h, bs;
    getAbsolutePosition(x, y);
    bs = getBorderSize();
    w = getWidth() + bs * 2;
    h = getHeight() + bs * 2;
    x -= bs;
    y -= bs;

    if (isOpaque()) {
        ((Graphics*)graphics)->drawImageRect(x, y, w, h, background);
    }
}

void ScrollArea::setOpaque(bool opaque)
{
    this->opaque = opaque;

    if (opaque) {
        setBorderSize(2);
    }
    else {
        setBorderSize(0);
    }
}

bool ScrollArea::isOpaque()
{
    return opaque;
}

void ScrollArea::drawUpButton(gcn::Graphics *graphics)
{
    gcn::Rectangle dim = getUpButtonDimension();
    int x, y;
    getAbsolutePosition(x, y);
    if (mUpButtonPressed)
        vscroll_up_pressed->draw(screen, x + dim.x, y + dim.y);
    else
        vscroll_up_default->draw(screen, x + dim.x, y + dim.y);
}

void ScrollArea::drawDownButton(gcn::Graphics *graphics)
{
    gcn::Rectangle dim = getDownButtonDimension();
    int x, y;
    getAbsolutePosition(x, y);
    if (mDownButtonPressed)
        vscroll_down_pressed->draw(screen, x + dim.x, y + dim.y);
    else
        vscroll_down_default->draw(screen, x + dim.x, y + dim.y);

}

void ScrollArea::drawLeftButton(gcn::Graphics *graphics)
{
    gcn::Rectangle dim = getLeftButtonDimension();
    int x, y;
    getAbsolutePosition(x, y);
    if (mLeftButtonPressed)
        hscroll_left_pressed->draw(screen, x + dim.x, y + dim.y);
    else
        hscroll_left_default->draw(screen, x + dim.x, y + dim.y);

}

void ScrollArea::drawRightButton(gcn::Graphics *graphics)
{
    gcn::Rectangle dim = getRightButtonDimension();
    int x, y;
    getAbsolutePosition(x, y);
    if (mRightButtonPressed)
        hscroll_right_pressed->draw(screen, x + dim.x, y + dim.y);
    else
        hscroll_right_default->draw(screen, x + dim.x, y + dim.y);

}

void ScrollArea::drawVBar(gcn::Graphics *graphics)
{
    int x, y;
    gcn::Rectangle dim = getVerticalBarDimension();
    getAbsolutePosition(x, y);

    ((Graphics*)graphics)->drawImageRect(
            x + dim.x, y + dim.y, dim.width, dim.height, background);
}

void ScrollArea::drawHBar(gcn::Graphics *graphics)
{
    int x, y;
    gcn::Rectangle dim = getHorizontalBarDimension();
    getAbsolutePosition(x, y);

    ((Graphics*)graphics)->drawImageRect(
            x + dim.x, y + dim.y, dim.width, dim.height, background);
}

void ScrollArea::drawVMarker(gcn::Graphics *graphics)
{
    int x, y;
    gcn::Rectangle dim = getVerticalMarkerDimension();
    getAbsolutePosition(x, y);

    ((Graphics*)graphics)->drawImageRect(
            x + dim.x, y + dim.y, dim.width, dim.height, vMarker);
}

void ScrollArea::drawHMarker(gcn::Graphics *graphics)
{
    int x, y;
    gcn::Rectangle dim = getHorizontalMarkerDimension();
    getAbsolutePosition(x, y);

    ((Graphics*)graphics)->drawImageRect(
            x + dim.x, y + dim.y, dim.width, dim.height, vMarker);
}