1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
21 #include <nel/misc/types_nl.h>
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"
44 using namespace NLMISC
;
53 static string QueryString
, AnswerString
;
54 static float DisplayWidth
;
55 static float DisplayHeight
, nbLines
;
56 static CRGBA DisplayColor
;
60 static uint FontSize
= 20;
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
87 case 13 : // RETURN : Send the chat message
89 if (_Line
.size() == 0)
97 _MaxWidthReached
= false;
100 case 8 : // BACKSPACE
101 if ( _Line
.size() != 0 )
103 _Line
.erase( _Line
.end()-1 );
104 // _MaxWidthReached = false; // no need
111 _LastCommand
= _Line
;
113 _MaxWidthReached
= false;
118 if ( ! _MaxWidthReached
)
120 _Line
+= (char)ec
.Char
;
126 CInterfaceListener() : _MaxWidthReached( false )
129 const string
& line() const
134 void setLine(const string
&str
) { _Line
= str
; }
136 void setMaxWidthReached( bool b
)
138 _MaxWidthReached
= b
;
143 bool _MaxWidthReached
;
147 // Instance of the listener
148 static CInterfaceListener InterfaceListener
;
149 static UMaterial InterfaceMaterial
= NULL
;
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;
168 Driver
->setMatrixMode2D11 ();
169 InterfaceMaterial
.setColor(DisplayColor
);
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
);
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());
193 y
-= FontSize
/ 600.0f
;
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
;
208 str2
= InterfaceListener
.line();
212 str2
.resize (InterfaceListener
.line().size (), '*');
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
;
238 QueryString
+= "\n\n(Press any key to continue)";
240 QueryString
+= "\n\n(Press enter to validate and ESC to cancel)";
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
);
256 bool haveAnswer (string
&answer
)
258 bool haveIt
= !AnswerString
.empty();
261 answer
= AnswerString
;
267 bool interfaceOpen ()
269 return !QueryString
.empty();
272 } /* namespace SBCLIENT */