1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include <GLW/GLWDropDown.h>
22 #include <GLW/GLWTranslate.h>
23 #include <GLW/GLWToolTip.h>
24 #include <client/ScorchedClient.h>
25 
~GLWDropDownI()26 GLWDropDownI::~GLWDropDownI()
27 {
28 
29 }
30 
31 REGISTER_CLASS_SOURCE(GLWDropDown);
32 
GLWDropDown(float x,float y,float w)33 GLWDropDown::GLWDropDown(float x, float y, float w) :
34 	GLWidget(x, y, w, 25.0f),
35 	button_(x + w - 22.0f, y + 2.0f, 20.0f, 21.0f),
36 	handler_(0), current_(0)
37 {
38 	button_.setHandler(this);
39 }
40 
~GLWDropDown()41 GLWDropDown::~GLWDropDown()
42 {
43 
44 }
45 
setHandler(GLWDropDownI * handler)46 void GLWDropDown::setHandler(GLWDropDownI *handler)
47 {
48 	handler_ = handler;
49 }
50 
clear()51 void GLWDropDown::clear()
52 {
53 	current_ = 0;
54 	texts_.clear();
55 }
56 
getCurrentEntry()57 GLWSelectorEntry *GLWDropDown::getCurrentEntry()
58 {
59 	return current_;
60 }
61 
getCurrentPosition()62 int GLWDropDown::getCurrentPosition()
63 {
64 	std::list<GLWSelectorEntry>::iterator itor;
65 	int pos = 0;
66 	for (itor = texts_.begin();
67 		itor != texts_.end();
68 		++itor, pos++)
69 	{
70 		GLWSelectorEntry &entry = *itor;
71 		if (current_ == &entry)
72 		{
73 			return pos;
74 		}
75 	}
76 
77 	return -1;
78 }
79 
setCurrentPosition(int pos)80 void GLWDropDown::setCurrentPosition(int pos)
81 {
82 	int position = 0;
83 	std::list<GLWSelectorEntry>::iterator itor;
84 	for (itor = texts_.begin();
85 		itor != texts_.end();
86 		++itor)
87 	{
88 		GLWSelectorEntry &entry = *itor;
89 		current_ = &entry;
90 		if (position++ >= pos) break;
91 	}
92 
93 	if (handler_)
94 	{
95 		handler_->select(id_, pos, *current_);
96 	}
97 }
98 
addEntry(GLWSelectorEntry text)99 void GLWDropDown::addEntry(GLWSelectorEntry text)
100 {
101 	texts_.push_back(text);
102 	if (!current_)
103 	{
104 		current_ = &texts_.back();
105 	}
106 }
107 
draw()108 void GLWDropDown::draw()
109 {
110 	GLWidget::draw();
111 
112 	glBegin(GL_LINE_LOOP);
113 		drawShadedRoundBox(x_, y_, w_, h_, 10.0f, false);
114 	glEnd();
115 
116 	button_.draw();
117 	float offset = 0.0f;
118 	if(button_.getPressed()) offset = 1.0f;
119 
120 	glColor3f(0.2f, 0.2f, 0.2f);
121 	glBegin(GL_TRIANGLES);
122 		glVertex2d(x_ + w_ - 6.0f + offset, y_ + 17.0f - offset);
123 		glVertex2d(x_ + w_ - 17.0f + offset, y_ + 17.0f - offset);
124 		glVertex2d(x_ + w_ - 12.0f + offset, y_ + 7.0f - offset);
125 	glEnd();
126 }
127 
buttonDown(unsigned int id)128 void GLWDropDown::buttonDown(unsigned int id)
129 {
130 	if (button_.getPressed())
131 	{
132 		GLWSelector::instance()->showSelector(
133 			this,
134 			GLWTranslate::getPosX() + x_,
135 			GLWTranslate::getPosY() + y_ - 7.0f,
136 			texts_,
137 			0,
138 			false);
139 	}
140 }
141 
buttonUp(unsigned int id)142 void GLWDropDown::buttonUp(unsigned int id)
143 {
144 }
145 
mouseDown(int button,float x,float y,bool & skipRest)146 void GLWDropDown::mouseDown(int button, float x, float y, bool &skipRest)
147 {
148 	button_.mouseDown(button, x, y, skipRest);
149 	if (!skipRest)
150 	{
151 		if (inBox(x, y, x_, y_, w_, h_))
152 		{
153 			skipRest = true;
154 			button_.getPressed() = true;
155 			buttonDown(0);
156 		}
157 	}
158 }
159 
setX(float x)160 void GLWDropDown::setX(float x)
161 {
162 	GLWidget::setX(x);
163 	button_.setX(x + w_ - 22.0f);
164 }
165 
setY(float y)166 void GLWDropDown::setY(float y)
167 {
168 	GLWidget::setY(y);
169 	button_.setY(y + 2.0f);
170 }
171 
itemSelected(GLWSelectorEntry * entry,int pos)172 void GLWDropDown::itemSelected(GLWSelectorEntry *entry, int pos)
173 {
174 	button_.getPressed() = false;
175 
176 	int position = 0;
177 	std::list<GLWSelectorEntry>::iterator itor;
178 	for (itor = texts_.begin();
179 		itor != texts_.end();
180 		++itor)
181 	{
182 		GLWSelectorEntry &entry = *itor;
183 		current_ = &entry;
184 		if (position++ >= pos) break;
185 	}
186 
187 	if (handler_)
188 	{
189 		handler_->select(id_, position, *current_);
190 	}
191 }
192 
noItemSelected()193 void GLWDropDown::noItemSelected()
194 {
195 	button_.getPressed() = false;
196 }
197 
mouseUp(int button,float x,float y,bool & skipRest)198 void GLWDropDown::mouseUp(int button, float x, float y, bool &skipRest)
199 {
200 	button_.mouseUp(button, x, y, skipRest);
201 }
202