git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / client / GLW / GLWSelector.cpp
blob4d81fe110acafb92f4fc0a4e66130644f49c01e3
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
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.
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.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <GLW/GLWSelector.h>
22 #include <GLW/GLWSelectorPart.h>
23 #include <GLEXT/GLViewPort.h>
24 #include <client/ScorchedClient.h>
25 #include <common/Defines.h>
26 #include <graph/OptionsDisplay.h>
28 GLWSelectorEntry::GLWSelectorEntry(const LangString &text,
29 ToolTip *tooltip,
30 bool selected,
31 GLTexture *icon,
32 void *userData,
33 const std::string &dataText) :
34 text_(text),
35 tip_(tooltip),
36 selected_(selected),
37 icon_(icon),
38 userData_(userData),
39 dataText_(dataText),
40 color_(1.0f, 1.0f, 1.0f),
41 textureWidth_(0),
42 seperator_(false)
47 GLWSelector *GLWSelector::instance_ = 0;
49 GLWSelector *GLWSelector::instance()
51 if (!instance_)
53 instance_ = new GLWSelector();
55 return instance_;
58 GLWSelector::GLWSelector() :
59 GLWWindow("", 0.0f, 0.0f, 0.0f, 0.0f, 0, ""),
60 showState_(0)
62 visible_ = false;
63 windowLevel_ = 1000;
66 GLWSelector::~GLWSelector()
70 void GLWSelector::showSelector(GLWSelectorI *user,
71 float x, float y,
72 std::list<GLWSelectorEntry> &entries,
73 unsigned int showState,
74 bool transparent)
77 hideSelector();
79 showState_ = showState;
80 visible_ = true;
81 w_ = 100000.0f;
82 h_ = 100000.0f;
83 user_ = user;
85 int basePosition = 0;
86 float left = x;
87 std::list<GLWSelectorEntry> partEntries;
88 std::list<GLWSelectorEntry>::iterator itor;
89 for (itor = entries.begin();
90 itor != entries.end();
91 itor++)
93 partEntries.push_back(*itor);
94 if ((int) partEntries.size() * 20 >= GLViewPort::getHeight() - 40)
96 GLWSelectorPart *part = new GLWSelectorPart(
97 user, basePosition,
98 left, y,
99 partEntries,
100 transparent,
101 0, 0); // Parent
102 left += part->getSelectedWidth() + 10.0f;
103 addPart(part);
104 basePosition += (int) partEntries.size();
105 partEntries.clear();
108 if (partEntries.size() > 0)
110 GLWSelectorPart *part = new GLWSelectorPart(
111 user, basePosition,
112 left, y,
113 partEntries,
114 transparent,
115 0, 0); // Parent
116 addPart(part);
120 void GLWSelector::hideSelector()
122 visible_ = false;
123 w_ = 0;
124 h_ = 0;
125 user_ = 0;
127 while (!parts_.empty())
129 GLWSelectorPart *part = parts_.front();
130 parts_.pop_front();
131 delete part;
135 void GLWSelector::addPart(GLWSelectorPart *part)
137 parts_.push_back(part);
140 void GLWSelector::rmPart(GLWSelectorPart *part)
142 if (part->getChild()) rmPart(part->getChild());
143 parts_.remove(part);
144 delete part;
147 void GLWSelector::draw()
149 if (!visible_) return;
151 if (showState_ != 0 &&
152 ScorchedClient::instance()->getGameState().getState() != showState_)
154 if (user_) user_->noItemSelected();
155 hideSelector();
156 return;
159 unsigned int state = 0;
160 if (OptionsDisplay::instance()->getSmoothLines())
162 state |= GLState::BLEND_ON;
163 glEnable(GL_LINE_SMOOTH);
165 GLState currentState(state);
167 std::list<GLWSelectorPart *>::iterator itor;
168 for (itor = parts_.begin();
169 itor != parts_.end();
170 itor++)
172 GLWSelectorPart *part = (*itor);
173 part->draw();
176 if (OptionsDisplay::instance()->getSmoothLines())
178 glDisable(GL_LINE_SMOOTH);
182 void GLWSelector::mouseDown(int button, float mouseX, float mouseY, bool &hitMenu)
184 // Override default window behaviour
185 if (!visible_) return;
187 hitMenu = true;
189 if ((showState_ != 0) &&
190 (ScorchedClient::instance()->getGameState().getState() != showState_))
192 if (user_) user_->noItemSelected();
193 hideSelector();
194 return;
197 bool hit = false;
198 std::list<GLWSelectorPart *>::reverse_iterator itor;
199 for (itor = parts_.rbegin();
200 itor != parts_.rend();
201 itor++)
203 GLWSelectorPart *part = (*itor);
204 part->mouseDown(mouseX, mouseY, hit);
205 if (hit) break;
208 if (!hit && user_) user_->noItemSelected();
210 hideSelector();
213 void GLWSelector::mouseUp(int button, float x, float y, bool &skipRest)
215 // Override and disable default window behaviour
218 void GLWSelector::mouseDrag(int button, float mx, float my, float x, float y, bool &skipRest)
220 // Override and disable default window behaviour
223 void GLWSelector::keyDown(char *buffer, unsigned int keyState,
224 KeyboardHistory::HistoryElement *history, int hisCount,
225 bool &skipRest)
227 // Override and disable default window behaviour