summaryrefslogtreecommitdiff
path: root/src/gui/setup_colours.cpp
blob: 07ec9086d3685cc2dead632e1ff25bc2367c23a0 (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
/***************************************************************************
 *   Copyright (C) 2008 by Douglas Boffey                                  *
 *                                                                         *
 *       DougABoffey@netscape.net                                          *
 *   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     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed with The Mana Experiment                  *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <guichan/listmodel.hpp>
#include <guichan/widgets/label.hpp>
#include <guichan/widgets/slider.hpp>

#include "colour.h"
#include "listbox.h"
#include "scrollarea.h"
#include "setup_colours.h"
#include "slider.h"
#include "textfield.h"

#include "widgets/layouthelper.h"

#include "../configuration.h"

#include "../utils/gettext.h"

Setup_Colours::Setup_Colours() :
    mSelected(-1)
{
    setOpaque(false);

    mColourBox = new ListBox(textColour);
    mColourBox->setActionEventId("colour_box");
    mColourBox->addActionListener(this);

    mScroll = new ScrollArea(mColourBox);
    mScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
    mScroll->setWidth(90);

    mRedLabel = new gcn::Label(_("Red: "));

    mRedText = new TextField();
    mRedText->setWidth(40);
    mRedText->setRange(0, 255);
    mRedText->setNumeric(true);
    mRedText->addListener(this);

    mRedSlider = new Slider(0, 255);
    mRedSlider->setWidth(90);
    mRedSlider->setValue(mRedText->getValue());
    mRedSlider->setActionEventId("slider_red");
    mRedSlider->addActionListener(this);

    mGreenLabel = new gcn::Label(_("Green: "));

    mGreenText = new TextField();
    mGreenText->setWidth(40);
    mGreenText->setRange(0, 255);
    mGreenText->setNumeric(true);
    mGreenText->addListener(this);

    mGreenSlider = new Slider(0, 255);
    mGreenSlider->setWidth(90);
    mGreenSlider->setValue(mGreenText->getValue());
    mGreenSlider->setActionEventId("slider_green");
    mGreenSlider->addActionListener(this);

    mBlueLabel = new gcn::Label(_("Blue: "));

    mBlueText = new TextField();
    mBlueText->setWidth(40);
    mBlueText->setRange(0, 255);
    mBlueText->setNumeric(true);
    mBlueText->addListener(this);

    mBlueSlider = new Slider(0, 255);
    mBlueSlider->setWidth(90);
    mBlueSlider->setValue(mBlueText->getValue());
    mBlueSlider->setActionEventId("slider_blue");
    mBlueSlider->addActionListener(this);

    setOpaque(false);

    // Do the layout
    LayoutHelper h(this);
    ContainerPlacer place = h.getPlacer(0, 0);

    place(0, 0, mScroll, 1, 3).setPadding(2);
    place(1, 0, mRedLabel, 2);
    place(3, 0, mRedSlider);
    place(4, 0, mRedText).setPadding(1);
    place(1, 1, mGreenLabel, 2);
    place(3, 1, mGreenSlider);
    place(4, 1, mGreenText).setPadding(1);
    place(1, 2, mBlueLabel, 2);
    place(3, 2, mBlueSlider);
    place(4, 2, mBlueText).setPadding(1);

    setDimension(gcn::Rectangle(0, 0, 290, 150));
}

Setup_Colours::~Setup_Colours()
{
    delete mRedLabel;
    delete mRedSlider;
    delete mRedText;

    delete mGreenLabel;
    delete mGreenSlider;
    delete mGreenText;

    delete mBlueLabel;
    delete mBlueSlider;
    delete mBlueText;

    delete mScroll;
}

void Setup_Colours::action(const gcn::ActionEvent &event)
{
    if (event.getId() == "colour_box")
    {
        mSelected = mColourBox->getSelected();
        int col = textColour->getColourAt(mSelected);
        setEntry(mRedSlider, mRedText, col >> 16);
        setEntry(mGreenSlider, mGreenText, (col >> 8) & 0xff);
        setEntry(mBlueSlider, mBlueText, col & 0xff);
        return;
    }

    if (event.getId() == "slider_red")
    {
        char buffer[30];
        std::sprintf(buffer, "%d", static_cast<int>(mRedSlider->getValue()));
        mRedText->setText(buffer);
        updateColour();
        return;
    }

    if (event.getId() == "slider_green")
    {
        char buffer[30];
        std::sprintf(buffer, "%d", static_cast<int>(mGreenSlider->getValue()));
        mGreenText->setText(buffer);
        updateColour();
        return;
    }

    if (event.getId() == "slider_blue")
    {
        char buffer[30];
        std::sprintf(buffer, "%d", static_cast<int>(mBlueSlider->getValue()));
        mBlueText->setText(buffer);
        updateColour();
        return;
    }
}

void Setup_Colours::setEntry(Slider *s, TextField *t, int value)
{
    s->setValue(value);
    char buffer[100];
    sprintf(buffer, "%d", value);
    t->setText(buffer);
}

void Setup_Colours::apply()
{
    textColour->commit();
}

void Setup_Colours::cancel()
{
    textColour->rollback();
    int col = textColour->getColourAt(mSelected);
    setEntry(mRedSlider, mRedText, col >> 16);
    setEntry(mGreenSlider, mGreenText, (col >> 8) & 0xff);
    setEntry(mBlueSlider, mBlueText, col & 0xff);
}

void Setup_Colours::listen(const TextField *tf)
{
    if (tf == mRedText)
    {
        mRedSlider->setValue(tf->getValue());
        updateColour();
        return;
    }
    if (tf == mGreenText)
    {
        mGreenSlider->setValue(tf->getValue());
        updateColour();
        return;
    }
    if (tf == mBlueText)
    {
        mBlueSlider->setValue(tf->getValue());
        updateColour();
        return;
    }
}

void Setup_Colours::updateColour()
{
    if (mSelected == -1)
    {
        return;
    }
    int rgb = static_cast<int>(mRedSlider->getValue()) << 16 |
              static_cast<int>(mGreenSlider->getValue()) << 8 |
              static_cast<int>(mBlueSlider->getValue());
    textColour->setColourAt(mSelected, rgb);
}