Fix crash
[ryzomcore.git] / snowballs2 / client / src / interface.cpp
blob7f66c18d262d1f65f5163e2d28242ac77d164f55
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // Includes
21 #include <nel/misc/types_nl.h>
23 #include <cmath>
24 #include <nel/misc/vectord.h>
25 #include <nel/misc/event_listener.h>
27 #include <nel/3d/u_camera.h>
28 #include <nel/3d/u_driver.h>
29 #include <nel/3d/u_text_context.h>
30 #include <nel/3d/u_instance.h>
31 #include <nel/3d/u_scene.h>
32 #include <nel/3d/u_3d_mouse_listener.h>
33 #include <nel/3d/u_material.h>
34 #include <nel/3d/u_landscape.h>
36 #include "snowballs_client.h"
37 #include "interface.h"
40 // Namespaces
43 using namespace std;
44 using namespace NLMISC;
45 using namespace NL3D;
47 namespace SBCLIENT {
50 // Variables
53 static string QueryString, AnswerString;
54 static float DisplayWidth;
55 static float DisplayHeight, nbLines;
56 static CRGBA DisplayColor;
58 static sint Prompt;
60 static uint FontSize = 20;
63 // Functions
66 // Class that handle the keyboard input when an interface is displayed (login request for example)
67 class CInterfaceListener : public IEventListener
69 virtual void operator() ( const CEvent& event )
71 // ignore keys if interface is not open
72 if (!interfaceOpen ()) return;
74 CEventChar &ec = (CEventChar&)event;
76 // prompt = 2 then we wait a any user key
77 if (Prompt == 2)
79 QueryString = "";
80 AnswerString = "";
81 _Line = "";
82 return;
85 switch ( ec.Char )
87 case 13 : // RETURN : Send the chat message
89 if (_Line.size() == 0)
90 break;
92 QueryString = "";
93 AnswerString = _Line;
95 _LastCommand = _Line;
96 _Line = "";
97 _MaxWidthReached = false;
98 break;
100 case 8 : // BACKSPACE
101 if ( _Line.size() != 0 )
103 _Line.erase( _Line.end()-1 );
104 // _MaxWidthReached = false; // no need
106 break;
108 case 27 : // ESCAPE
109 QueryString = "";
110 AnswerString = "";
111 _LastCommand = _Line;
112 _Line = "";
113 _MaxWidthReached = false;
114 break;
116 default:
118 if ( ! _MaxWidthReached )
120 _Line += (char)ec.Char;
125 public:
126 CInterfaceListener() : _MaxWidthReached( false )
129 const string& line() const
131 return _Line;
134 void setLine(const string &str) { _Line = str; }
136 void setMaxWidthReached( bool b )
138 _MaxWidthReached = b;
141 private:
142 string _Line;
143 bool _MaxWidthReached;
144 string _LastCommand;
147 // Instance of the listener
148 static CInterfaceListener InterfaceListener;
149 static UMaterial InterfaceMaterial = NULL;
152 void initInterface()
154 // Add the keyboard listener in the event server
155 Driver->EventServer.addListener (EventCharId, &InterfaceListener);
157 InterfaceMaterial = Driver->createMaterial();
158 InterfaceMaterial.initUnlit();
159 InterfaceMaterial.setBlendFunc(UMaterial::srcalpha, UMaterial::invsrcalpha);
160 InterfaceMaterial.setBlend(true);
163 void updateInterface()
165 if (QueryString.empty()) return;
167 // Draw background
168 Driver->setMatrixMode2D11 ();
169 InterfaceMaterial.setColor(DisplayColor);
170 CQuad quad;
171 quad.V0.set(0.5f-DisplayWidth/2.0f, 0.5f+DisplayHeight/2.0f, 0);
172 quad.V1.set(0.5f+DisplayWidth/2.0f, 0.5f+DisplayHeight/2.0f, 0);
173 quad.V2.set(0.5f+DisplayWidth/2.0f, 0.5f-DisplayHeight/2.0f, 0);
174 quad.V3.set(0.5f-DisplayWidth/2.0f, 0.5f-DisplayHeight/2.0f, 0);
175 Driver->drawQuad(quad, InterfaceMaterial);
176 //Driver->drawQuad (0.5f-DisplayWidth/2.0f, 0.5f-DisplayHeight/2.0f, 0.5f+DisplayWidth/2.0f, 0.5f+DisplayHeight/2.0f, DisplayColor);
178 // Set the text context
179 TextContext->setHotSpot (UTextContext::MiddleMiddle);
180 TextContext->setColor (CRGBA (255,255,255,255));
181 TextContext->setFontSize (FontSize);
183 // Display the text
184 string str;
185 float y = (nbLines * FontSize / 2.0f) / 600.0f;
187 for (uint i = 0; i < QueryString.size (); i++)
189 if (QueryString[i] == '\n')
191 TextContext->printfAt (0.5f, 0.5f+y, str.c_str());
192 str = "";
193 y -= FontSize / 600.0f;
195 else
197 str += QueryString[i];
200 TextContext->printfAt (0.5f, 0.5f+y, str.c_str());
202 // Display the user input line
203 y-= (2.0f * FontSize) / 600.0f;
204 string str2;
205 switch (Prompt)
207 case 0:
208 str2 = InterfaceListener.line();
209 str2 += "_";
210 break;
211 case 1:
212 str2.resize (InterfaceListener.line().size (), '*');
213 str2 += "_";
214 break;
215 case 2:
216 str2 = "";
217 break;
219 TextContext->printfAt (0.5f, 0.5f+y, str2.c_str());
222 void releaseInterface()
224 // Delete the material
225 Driver->deleteMaterial(InterfaceMaterial);
227 // Remove the keyboard listener from the server
228 Driver->EventServer.removeListener (EventCharId, &InterfaceListener);
231 void askString (const string &queryString, const string &defaultString, sint prompt, const CRGBA &color)
233 nldebug("called askString");
234 nlinfo(queryString.c_str());
235 QueryString = queryString;
237 if (prompt == 2)
238 QueryString += "\n\n(Press any key to continue)";
239 else
240 QueryString += "\n\n(Press enter to validate and ESC to cancel)";
242 nbLines = 1;
243 for (uint i = 0; i < QueryString.size (); i++)
245 if (QueryString[i] == '\n') nbLines++;
248 DisplayWidth = float(queryString.size () * FontSize) / 600.0f;
249 DisplayHeight = float((nbLines + 4) * FontSize) / 600.0f;
250 DisplayColor = color;
252 InterfaceListener.setLine (defaultString);
253 Prompt = prompt;
256 bool haveAnswer (string &answer)
258 bool haveIt = !AnswerString.empty();
259 if (haveIt)
261 answer = AnswerString;
262 AnswerString = "";
264 return haveIt;
267 bool interfaceOpen ()
269 return !QueryString.empty();
272 } /* namespace SBCLIENT */
274 /* end of file */