1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
4 // This file is part of Scorched3D.
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>
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()
40 instance_
= new GLWToolTip
;
45 GLWToolTip::GLWToolTip() :
46 GameStateI("GLWToolTip"),
47 lastTip_(0), currentTip_(0),
48 timeDrawn_(0.0f
), timeSeen_(0.0),
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();
73 if (x
< mouseX
&& mouseX
< x
+ w
&&
74 y
< mouseY
&& mouseY
< y
+ h
)
76 static ToolTip singleTip
;
77 singleTip
.setText(type
, title
, text
);
83 currentTip_
= &singleTip
;
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();
101 if (x
< mouseX
&& mouseX
< x
+ w
&&
102 y
< mouseY
&& mouseY
< y
+ h
)
115 void GLWToolTip::setupTip(ToolTip
*tip
)
118 tipTextWidth_
= 0.0f
;
119 tipTextHeight_
= 0.0f
;
120 tipTitle_
= tip
->getTitle();
121 tipText_
= tip
->getText();
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
;
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
)
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_
;
180 if (lastTip_
) timeSeen_
+= timeDrawn_
;
181 else timeSeen_
-= timeDrawn_
;
182 refreshTime_
+= timeDrawn_
;
186 float(OptionsDisplay::instance()->getToolTipTime()) / 1000.0f
;
187 if (timeSeen_
<= -showTime
)
189 timeSeen_
= -showTime
;
193 if ((refreshTime_
> 1.0f
||
194 tipX_
!= currentX_
||
195 tipY_
!= currentY_
) && lastTip_
)
202 lastTip_
->populate();
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
)
216 else tipOffY_
= 0.0f
;
219 float alpha
= timeSeen_
*
220 float(OptionsDisplay::instance()->getToolTipSpeed());
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)
242 posX
+= tipW_
+ 5.0f
;
244 int camHeight
= GLViewPort::getHeight();
245 if (posY
> camHeight
/ 2)
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(
271 glVertex2f(posX
+ 10.0f
, posY
);
273 glColor4f(0.9f
, 0.9f
, 1.0f
, 0.5f
* alpha
);
275 glBegin(GL_LINE_LOOP
);
276 GLWidget::drawRoundBox(
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_
);
293 std::list
<LangString
>::iterator itor
;
294 std::list
<LangString
>::iterator enditor
= tipTexts_
.end();
295 for (itor
= tipTexts_
.begin(); itor
!= enditor
; itor
++)
299 GLWFont::instance()->getGameFont()->drawA(
300 color
, alpha
, 9, posX
+ 6.0f
,