Merge from development branch heightmap to main.
[scorched3d.git] / src / client / GLW / GLWToolTip.cpp
blobc33e5b6d7c4060114ccbfec06299ef1d3c860ca0
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2003
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 <GLEXT/GLState.h>
22 #include <GLEXT/GLViewPort.h>
23 #include <GLW/GLWToolTip.h>
24 #include <GLW/GLWidget.h>
25 #include <GLW/GLWFont.h>
26 #include <client/ScorchedClient.h>
27 #include <graph/OptionsDisplay.h>
28 #include <common/Defines.h>
29 #include <string.h>
31 static Vector color(0.1f, 0.1f, 0.4f);
32 static Vector selectedColor(0.9f, 0.9f, 1.0f);
34 GLWToolTip *GLWToolTip::instance_ = 0;
36 GLWToolTip *GLWToolTip::instance()
38 if (!instance_)
40 instance_ = new GLWToolTip;
42 return instance_;
45 GLWToolTip::GLWToolTip() :
46 GameStateI("GLWToolTip"),
47 lastTip_(0), currentTip_(0),
48 timeDrawn_(0.0f), timeSeen_(0.0),
49 refreshTime_(100.0f),
50 tipX_(0.0f), tipY_(0.0f),
51 tipW_(0.0f), tipH_(0.0f),
52 tipOffX_(0.0f), tipOffY_(0.0f)
56 GLWToolTip::~GLWToolTip()
60 bool GLWToolTip::addToolTip(ToolTip::ToolTipType type,
61 const LangString &title, const LangString &text,
62 float x, float y, float w, float h)
64 if (!OptionsDisplay::instance()->getShowContextInfo() &&
65 (type & ToolTip::ToolTipInfo)) return false;
66 if (!OptionsDisplay::instance()->getShowContextHelp() &&
67 (type & ToolTip::ToolTipHelp)) return false;
69 int mouseX = ScorchedClient::instance()->getGameState().getMouseX();
70 int mouseY = ScorchedClient::instance()->getGameState().getMouseY();
72 bool result = false;
73 if (x < mouseX && mouseX < x + w &&
74 y < mouseY && mouseY < y + h)
76 static ToolTip singleTip;
77 singleTip.setText(type, title, text);
79 currentX_ = x;
80 currentY_ = y;
81 currentW_ = w;
82 currentH_ = h;
83 currentTip_ = &singleTip;
85 result = true;
87 return result;
90 bool GLWToolTip::addToolTip(ToolTip *tip, float x, float y, float w, float h)
92 if (!OptionsDisplay::instance()->getShowContextInfo() &&
93 (tip->getType() & ToolTip::ToolTipInfo)) return false;
94 if (!OptionsDisplay::instance()->getShowContextHelp() &&
95 (tip->getType() & ToolTip::ToolTipHelp)) return false;
97 int mouseX = ScorchedClient::instance()->getGameState().getMouseX();
98 int mouseY = ScorchedClient::instance()->getGameState().getMouseY();
100 bool result = false;
101 if (x < mouseX && mouseX < x + w &&
102 y < mouseY && mouseY < y + h)
104 currentX_ = x;
105 currentY_ = y;
106 currentW_ = w;
107 currentH_ = h;
108 currentTip_ = tip;
110 result = true;
112 return result;
115 void GLWToolTip::setupTip(ToolTip *tip)
117 currentTip_ = tip;
118 tipTextWidth_ = 0.0f;
119 tipTextHeight_ = 0.0f;
120 tipTitle_ = tip->getTitle();
121 tipText_ = tip->getText();
122 tipTexts_.clear();
125 void GLWToolTip::calculateTip(ToolTip *tip)
127 if (tipTextWidth_ != 0.0f) return;
129 tipTextHeight_ = 24.0f;
131 int pos, startpos = 0;
132 LangString tipText = tipText_;
133 tipText.append(LANG_STRING("\n"));
134 while ((pos = tipText.find(LANG_STRING("\n"), startpos)) != LangString::npos)
136 LangString part = LangString(tipText, startpos, pos - startpos);
137 tipTexts_.push_back(part);
138 tipTextHeight_ += 10.0f;
140 startpos = pos + 1;
143 std::list<LangString>::iterator itor;
144 std::list<LangString>::iterator enditor = tipTexts_.end();
145 for (itor = tipTexts_.begin(); itor != enditor; itor++)
147 float width = float(GLWFont::instance()->getGameFont()->
148 getWidth(9,(*itor))) + 10.0f;
149 if (width > tipTextWidth_) tipTextWidth_ = width;
152 float width = float(GLWFont::instance()->getGameFont()->
153 getWidth(11, tipTitle_)) + 10.0f;
154 if (width > tipTextWidth_) tipTextWidth_ = width;
157 void GLWToolTip::clearToolTip(float x, float y, float w, float h)
159 int mouseX = ScorchedClient::instance()->getGameState().getMouseX();
160 int mouseY = ScorchedClient::instance()->getGameState().getMouseY();
162 if (x < mouseX && mouseX < x + w &&
163 y < mouseY && mouseY < y + h)
165 currentTip_ = 0;
169 void GLWToolTip::simulate(const unsigned state, float frameTime)
171 timeDrawn_ += frameTime;
174 void GLWToolTip::draw(const unsigned state)
176 if (currentTip_ != lastTip_) refreshTime_ = 100.0f;
177 lastTip_ = currentTip_;
178 currentTip_ = 0;
180 if (lastTip_) timeSeen_ += timeDrawn_;
181 else timeSeen_ -= timeDrawn_;
182 refreshTime_ += timeDrawn_;
183 timeDrawn_ = 0.0f;
185 float showTime =
186 float(OptionsDisplay::instance()->getToolTipTime()) / 1000.0f;
187 if (timeSeen_ <= -showTime)
189 timeSeen_ = -showTime;
190 return;
193 if ((refreshTime_ > 1.0f ||
194 tipX_ != currentX_ ||
195 tipY_ != currentY_) && lastTip_)
197 tipX_ = currentX_;
198 tipY_ = currentY_;
199 tipW_ = currentW_;
200 tipH_ = currentH_;
202 lastTip_->populate();
203 setupTip(lastTip_);
204 refreshTime_ = 0.0f;
206 if (lastTip_)
208 calculateTip(lastTip_);
210 if (lastTip_->getType() & ToolTip::ToolTipAlignLeft)
211 tipOffX_ = -currentW_ - tipTextWidth_ - 10;
212 else tipOffX_ = 0.0f;
214 if (lastTip_->getType() & ToolTip::ToolTipAlignBottom)
215 tipOffY_ = 15.0f;
216 else tipOffY_ = 0.0f;
219 float alpha = timeSeen_ *
220 float(OptionsDisplay::instance()->getToolTipSpeed());
221 if (alpha > 1.0f)
223 alpha = 1.0f;
224 timeSeen_ = 1.0f /
225 float(OptionsDisplay::instance()->getToolTipSpeed());
228 GLState currentState(GLState::TEXTURE_OFF | GLState::DEPTH_OFF);
230 float posX = tipX_ + tipOffX_;
231 float posY = tipY_ + tipOffY_;
232 float posW = tipTextWidth_;
233 float posH = tipTextHeight_;
235 int camWidth = GLViewPort::getWidth();
236 if (posX > camWidth / 2)
238 posX -= posW + 5.0f;
240 else
242 posX += tipW_ + 5.0f;
244 int camHeight = GLViewPort::getHeight();
245 if (posY > camHeight / 2)
247 posY -= posH;
249 else
251 posY += 15.0f;
254 if (posX < 0) posX = 0;
255 else if (posX + posW > camWidth) posX -= posX + posW - camWidth;
258 if (OptionsDisplay::instance()->getSmoothLines())
260 glEnable(GL_LINE_SMOOTH);
263 GLState currentStateBlend(GLState::BLEND_ON);
264 glColor4f(0.5f, 0.5f, 1.0f, 0.8f * alpha);
265 glBegin(GL_TRIANGLE_FAN);
266 glVertex2f(posX + 10.0f, posY + 2.0f);
267 glVertex2f(posX + 10.0f, posY);
268 GLWidget::drawRoundBox(
269 posX, posY,
270 posW, posH, 10.0f);
271 glVertex2f(posX + 10.0f, posY);
272 glEnd();
273 glColor4f(0.9f, 0.9f, 1.0f, 0.5f * alpha);
274 glLineWidth(2.0f);
275 glBegin(GL_LINE_LOOP);
276 GLWidget::drawRoundBox(
277 posX, posY,
278 posW, posH, 10.0f);
279 glEnd();
280 glLineWidth(1.0f);
282 if (OptionsDisplay::instance()->getSmoothLines())
284 glDisable(GL_LINE_SMOOTH);
288 float pos = posY + posH - 16.0f;
289 GLWFont::instance()->getGameFont()->drawA(selectedColor, alpha, 11, posX + 3.0f,
290 pos, 0.0f, tipTitle_);
291 pos -= 2.0f;
293 std::list<LangString>::iterator itor;
294 std::list<LangString>::iterator enditor = tipTexts_.end();
295 for (itor = tipTexts_.begin(); itor != enditor; itor++)
297 pos -= 10.0f;
299 GLWFont::instance()->getGameFont()->drawA(
300 color, alpha, 9, posX + 6.0f,
301 pos, 0.0f, (*itor));