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
|
/***************************************************************************
* 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 <vector>
#include <guichan/listmodel.hpp>
#include <guichan/widgets/label.hpp>
#include <guichan/widgets/slider.hpp>
#include "colour.h"
#include "scrollarea.h"
#include "setup_colours.h"
#include "slider.h"
#include "../configuration.h"
Setup_Colours::Setup_Colours() :
mColourLabel("Colour:"),
mSelected(-1)
{
mColourBox = new gcn::ListBox(textColour);
mScroll = new ScrollArea(mColourBox);
mColourLabel.setX(5);
mColourLabel.setY(5);
mColourBox->setDimension(gcn::Rectangle(0, 10 + mColourLabel.getHeight(),
80,
115 - mColourLabel.getHeight()));
mScroll->setDimension(gcn::Rectangle(5, 10 + mColourLabel.getHeight(),
100, 115 - mColourLabel.getHeight()));
mColourBox->setSelected(-1);
mColourBox->setActionEventId("colour_box");
mColourBox->addActionListener(this);
setOpaque(false);
add(&mColourLabel);
add(mScroll);
setupPlacer(140, mLabel1, mSlider1, mText1, "R", "1");
setupPlacer(165, mLabel2, mSlider2, mText2, "G", "2");
setupPlacer(190, mLabel3, mSlider3, mText3, "B", "3");
}
Setup_Colours::~Setup_Colours()
{
delete mLabel1;
delete mSlider1;
delete mText1;
delete mLabel2;
delete mSlider2;
delete mText2;
delete mLabel3;
delete mSlider3;
delete mText3;
delete mScroll;
}
void Setup_Colours::action(const gcn::ActionEvent &event)
{
if (event.getId() == "colour_box")
{
mSelected = mColourBox->getSelected();
int col = textColour->getColourAt(mSelected);
setEntry(mSlider1, mText1, col >> 16);
setEntry(mSlider2, mText2, (col >> 8) & 0xff);
setEntry(mSlider3, mText3, col & 0xff);
return;
}
if (event.getId() == "slider1")
{
char buffer[30];
std::sprintf(buffer, "%d", static_cast<int>(mSlider1->getValue()));
mText1->setText(buffer);
updateColour();
return;
}
if (event.getId() == "slider2")
{
char buffer[30];
std::sprintf(buffer, "%d", static_cast<int>(mSlider2->getValue()));
mText2->setText(buffer);
updateColour();
return;
}
if (event.getId() == "slider3")
{
char buffer[30];
std::sprintf(buffer, "%d", static_cast<int>(mSlider3->getValue()));
mText3->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(mSlider1, mText1, col >> 16);
setEntry(mSlider2, mText2, (col >> 8) & 0xff);
setEntry(mSlider3, mText3, col & 0xff);
}
void Setup_Colours::setupPlacer(int v, gcn::Label *&l, Slider *&s,
TextField *&t, std::string lbl,
std::string sfx)
{
l = new gcn::Label(lbl + ":");
l->setX(5);
l->setY(v - l->getHeight() / 2);
s = new Slider(0, 255);
s->setHeight(10);
s->setX(25);
s->setY(v - s->getHeight() / 2);
s->setWidth(128);
s->setScale(0, 255);
t = new TextField();
t->setX(165);
t->setY(v - t->getHeight() / 2);
t->setWidth(40);
t->setNumeric(true);
t->setRange(0, 255);
t->addListener(this);
s->setActionEventId("slider" + sfx);
s->addActionListener(this);
add(l);
add(s);
add(t);
}
void Setup_Colours::listen(const TextField *tf)
{
if (tf == mText1)
{
mSlider1->setValue(tf->getValue());
updateColour();
return;
}
if (tf == mText2)
{
mSlider2->setValue(tf->getValue());
updateColour();
return;
}
if (tf == mText3)
{
mSlider3->setValue(tf->getValue());
updateColour();
return;
}
}
void Setup_Colours::updateColour()
{
if (mSelected == -1)
{
return;
}
int rgb = static_cast<int>(mSlider1->getValue()) << 16 |
static_cast<int>(mSlider2->getValue()) << 8 |
static_cast<int>(mSlider3->getValue());
textColour->setColourAt(mSelected, rgb);
}
|