UserInterface.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code 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 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "ListGUILocal.h"
  23. #include "DeviceContext.h"
  24. #include "Window.h"
  25. #include "UserInterfaceLocal.h"
  26. extern idCVar r_skipGuiShaders; // 1 = don't render any gui elements on surfaces
  27. idUserInterfaceManagerLocal uiManagerLocal;
  28. idUserInterfaceManager * uiManager = &uiManagerLocal;
  29. /*
  30. ===============================================================================
  31. idUserInterfaceManagerLocal
  32. ===============================================================================
  33. */
  34. void idUserInterfaceManagerLocal::Init() {
  35. screenRect = idRectangle(0, 0, 640, 480);
  36. dc.Init();
  37. }
  38. void idUserInterfaceManagerLocal::Shutdown() {
  39. guis.DeleteContents( true );
  40. demoGuis.DeleteContents( true );
  41. dc.Shutdown();
  42. }
  43. void idUserInterfaceManagerLocal::Touch( const char *name ) {
  44. idUserInterface *gui = Alloc();
  45. gui->InitFromFile( name );
  46. // delete gui;
  47. }
  48. void idUserInterfaceManagerLocal::WritePrecacheCommands( idFile *f ) {
  49. int c = guis.Num();
  50. for( int i = 0; i < c; i++ ) {
  51. char str[1024];
  52. sprintf( str, "touchGui %s\n", guis[i]->Name() );
  53. common->Printf( "%s", str );
  54. f->Printf( "%s", str );
  55. }
  56. }
  57. void idUserInterfaceManagerLocal::SetSize( float width, float height ) {
  58. dc.SetSize( width, height );
  59. }
  60. void idUserInterfaceManagerLocal::BeginLevelLoad() {
  61. int c = guis.Num();
  62. for ( int i = 0; i < c; i++ ) {
  63. if ( (guis[ i ]->GetDesktop()->GetFlags() & WIN_MENUGUI) == 0 ) {
  64. guis[ i ]->ClearRefs();
  65. /*
  66. delete guis[ i ];
  67. guis.RemoveIndex( i );
  68. i--; c--;
  69. */
  70. }
  71. }
  72. }
  73. void idUserInterfaceManagerLocal::EndLevelLoad() {
  74. int c = guis.Num();
  75. for ( int i = 0; i < c; i++ ) {
  76. if ( guis[i]->GetRefs() == 0 ) {
  77. //common->Printf( "purging %s.\n", guis[i]->GetSourceFile() );
  78. // use this to make sure no materials still reference this gui
  79. bool remove = true;
  80. for ( int j = 0; j < declManager->GetNumDecls( DECL_MATERIAL ); j++ ) {
  81. const idMaterial *material = static_cast<const idMaterial *>(declManager->DeclByIndex( DECL_MATERIAL, j, false ));
  82. if ( material->GlobalGui() == guis[i] ) {
  83. remove = false;
  84. break;
  85. }
  86. }
  87. if ( remove ) {
  88. delete guis[ i ];
  89. guis.RemoveIndex( i );
  90. i--; c--;
  91. }
  92. }
  93. }
  94. }
  95. void idUserInterfaceManagerLocal::Reload( bool all ) {
  96. ID_TIME_T ts;
  97. int c = guis.Num();
  98. for ( int i = 0; i < c; i++ ) {
  99. if ( !all ) {
  100. fileSystem->ReadFile( guis[i]->GetSourceFile(), NULL, &ts );
  101. if ( ts <= guis[i]->GetTimeStamp() ) {
  102. continue;
  103. }
  104. }
  105. guis[i]->InitFromFile( guis[i]->GetSourceFile() );
  106. common->Printf( "reloading %s.\n", guis[i]->GetSourceFile() );
  107. }
  108. }
  109. void idUserInterfaceManagerLocal::ListGuis() const {
  110. int c = guis.Num();
  111. common->Printf( "\n size refs name\n" );
  112. size_t total = 0;
  113. int copies = 0;
  114. int unique = 0;
  115. for ( int i = 0; i < c; i++ ) {
  116. idUserInterfaceLocal *gui = guis[i];
  117. size_t sz = gui->Size();
  118. bool isUnique = guis[i]->interactive;
  119. if ( isUnique ) {
  120. unique++;
  121. } else {
  122. copies++;
  123. }
  124. common->Printf( "%6.1fk %4i (%s) %s ( %i transitions )\n", sz / 1024.0f, guis[i]->GetRefs(), isUnique ? "unique" : "copy", guis[i]->GetSourceFile(), guis[i]->desktop->NumTransitions() );
  125. total += sz;
  126. }
  127. common->Printf( "===========\n %i total Guis ( %i copies, %i unique ), %.2f total Mbytes", c, copies, unique, total / ( 1024.0f * 1024.0f ) );
  128. }
  129. bool idUserInterfaceManagerLocal::CheckGui( const char *qpath ) const {
  130. idFile *file = fileSystem->OpenFileRead( qpath );
  131. if ( file ) {
  132. fileSystem->CloseFile( file );
  133. return true;
  134. }
  135. return false;
  136. }
  137. idUserInterface *idUserInterfaceManagerLocal::Alloc( void ) const {
  138. return new idUserInterfaceLocal();
  139. }
  140. void idUserInterfaceManagerLocal::DeAlloc( idUserInterface *gui ) {
  141. if ( gui ) {
  142. int c = guis.Num();
  143. for ( int i = 0; i < c; i++ ) {
  144. if ( guis[i] == gui ) {
  145. delete guis[i];
  146. guis.RemoveIndex( i );
  147. return;
  148. }
  149. }
  150. }
  151. }
  152. idUserInterface *idUserInterfaceManagerLocal::FindGui( const char *qpath, bool autoLoad, bool needUnique, bool forceNOTUnique ) {
  153. int c = guis.Num();
  154. for ( int i = 0; i < c; i++ ) {
  155. idUserInterfaceLocal *gui = guis[i];
  156. if ( !idStr::Icmp( guis[i]->GetSourceFile(), qpath ) ) {
  157. if ( !forceNOTUnique && ( needUnique || guis[i]->IsInteractive() ) ) {
  158. break;
  159. }
  160. guis[i]->AddRef();
  161. return guis[i];
  162. }
  163. }
  164. if ( autoLoad ) {
  165. idUserInterface *gui = Alloc();
  166. if ( gui->InitFromFile( qpath ) ) {
  167. gui->SetUniqued( forceNOTUnique ? false : needUnique );
  168. return gui;
  169. } else {
  170. delete gui;
  171. }
  172. }
  173. return NULL;
  174. }
  175. idUserInterface *idUserInterfaceManagerLocal::FindDemoGui( const char *qpath ) {
  176. int c = demoGuis.Num();
  177. for ( int i = 0; i < c; i++ ) {
  178. if ( !idStr::Icmp( demoGuis[i]->GetSourceFile(), qpath ) ) {
  179. return demoGuis[i];
  180. }
  181. }
  182. return NULL;
  183. }
  184. idListGUI * idUserInterfaceManagerLocal::AllocListGUI( void ) const {
  185. return new idListGUILocal();
  186. }
  187. void idUserInterfaceManagerLocal::FreeListGUI( idListGUI *listgui ) {
  188. delete listgui;
  189. }
  190. /*
  191. ===============================================================================
  192. idUserInterfaceLocal
  193. ===============================================================================
  194. */
  195. idUserInterfaceLocal::idUserInterfaceLocal() {
  196. cursorX = cursorY = 0.0;
  197. desktop = NULL;
  198. loading = false;
  199. active = false;
  200. interactive = false;
  201. uniqued = false;
  202. bindHandler = NULL;
  203. //so the reg eval in gui parsing doesn't get bogus values
  204. time = 0;
  205. refs = 1;
  206. }
  207. idUserInterfaceLocal::~idUserInterfaceLocal() {
  208. delete desktop;
  209. desktop = NULL;
  210. }
  211. const char *idUserInterfaceLocal::Name() const {
  212. return source;
  213. }
  214. const char *idUserInterfaceLocal::Comment() const {
  215. if ( desktop ) {
  216. return desktop->GetComment();
  217. }
  218. return "";
  219. }
  220. bool idUserInterfaceLocal::IsInteractive() const {
  221. return interactive;
  222. }
  223. bool idUserInterfaceLocal::InitFromFile( const char *qpath, bool rebuild, bool cache ) {
  224. if ( !( qpath && *qpath ) ) {
  225. // FIXME: Memory leak!!
  226. return false;
  227. }
  228. int sz = sizeof( idWindow );
  229. sz = sizeof( idSimpleWindow );
  230. loading = true;
  231. if ( rebuild ) {
  232. delete desktop;
  233. desktop = new idWindow( this );
  234. } else if ( desktop == NULL ) {
  235. desktop = new idWindow( this );
  236. }
  237. source = qpath;
  238. state.Set( "text", "Test Text!" );
  239. idParser src( LEXFL_NOFATALERRORS | LEXFL_NOSTRINGCONCAT | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_ALLOWBACKSLASHSTRINGCONCAT );
  240. //Load the timestamp so reload guis will work correctly
  241. fileSystem->ReadFile(qpath, NULL, &timeStamp);
  242. src.LoadFile( qpath );
  243. if ( src.IsLoaded() ) {
  244. idToken token;
  245. while( src.ReadToken( &token ) ) {
  246. if ( idStr::Icmp( token, "windowDef" ) == 0 ) {
  247. desktop->SetDC( &uiManagerLocal.dc );
  248. if ( desktop->Parse( &src, rebuild ) ) {
  249. desktop->SetFlag( WIN_DESKTOP );
  250. desktop->FixupParms();
  251. }
  252. continue;
  253. }
  254. }
  255. state.Set( "name", qpath );
  256. } else {
  257. desktop->SetDC( &uiManagerLocal.dc );
  258. desktop->SetFlag( WIN_DESKTOP );
  259. desktop->name = "Desktop";
  260. desktop->text = va( "Invalid GUI: %s", qpath );
  261. desktop->rect = idRectangle( 0.0f, 0.0f, 640.0f, 480.0f );
  262. desktop->drawRect = desktop->rect;
  263. desktop->foreColor = idVec4( 1.0f, 1.0f, 1.0f, 1.0f );
  264. desktop->backColor = idVec4( 0.0f, 0.0f, 0.0f, 1.0f );
  265. desktop->SetupFromState();
  266. common->Warning( "Couldn't load gui: '%s'", qpath );
  267. }
  268. interactive = desktop->Interactive();
  269. if ( uiManagerLocal.guis.Find( this ) == NULL ) {
  270. uiManagerLocal.guis.Append( this );
  271. }
  272. loading = false;
  273. return true;
  274. }
  275. const char *idUserInterfaceLocal::HandleEvent( const sysEvent_t *event, int _time, bool *updateVisuals ) {
  276. time = _time;
  277. if ( bindHandler && event->evType == SE_KEY && event->evValue2 == 1 ) {
  278. const char *ret = bindHandler->HandleEvent( event, updateVisuals );
  279. bindHandler = NULL;
  280. return ret;
  281. }
  282. if ( event->evType == SE_MOUSE ) {
  283. cursorX += event->evValue;
  284. cursorY += event->evValue2;
  285. if (cursorX < 0) {
  286. cursorX = 0;
  287. }
  288. if (cursorY < 0) {
  289. cursorY = 0;
  290. }
  291. }
  292. if ( desktop ) {
  293. return desktop->HandleEvent( event, updateVisuals );
  294. }
  295. return "";
  296. }
  297. void idUserInterfaceLocal::HandleNamedEvent ( const char* eventName ) {
  298. desktop->RunNamedEvent( eventName );
  299. }
  300. void idUserInterfaceLocal::Redraw( int _time ) {
  301. if ( r_skipGuiShaders.GetInteger() > 5 ) {
  302. return;
  303. }
  304. if ( !loading && desktop ) {
  305. time = _time;
  306. uiManagerLocal.dc.PushClipRect( uiManagerLocal.screenRect );
  307. desktop->Redraw( 0, 0 );
  308. uiManagerLocal.dc.PopClipRect();
  309. }
  310. }
  311. void idUserInterfaceLocal::DrawCursor() {
  312. if ( !desktop || desktop->GetFlags() & WIN_MENUGUI ) {
  313. uiManagerLocal.dc.DrawCursor(&cursorX, &cursorY, 32.0f );
  314. } else {
  315. uiManagerLocal.dc.DrawCursor(&cursorX, &cursorY, 64.0f );
  316. }
  317. }
  318. const idDict &idUserInterfaceLocal::State() const {
  319. return state;
  320. }
  321. void idUserInterfaceLocal::DeleteStateVar( const char *varName ) {
  322. state.Delete( varName );
  323. }
  324. void idUserInterfaceLocal::SetStateString( const char *varName, const char *value ) {
  325. state.Set( varName, value );
  326. }
  327. void idUserInterfaceLocal::SetStateBool( const char *varName, const bool value ) {
  328. state.SetBool( varName, value );
  329. }
  330. void idUserInterfaceLocal::SetStateInt( const char *varName, const int value ) {
  331. state.SetInt( varName, value );
  332. }
  333. void idUserInterfaceLocal::SetStateFloat( const char *varName, const float value ) {
  334. state.SetFloat( varName, value );
  335. }
  336. const char* idUserInterfaceLocal::GetStateString( const char *varName, const char* defaultString ) const {
  337. return state.GetString(varName, defaultString);
  338. }
  339. bool idUserInterfaceLocal::GetStateBool( const char *varName, const char* defaultString ) const {
  340. return state.GetBool(varName, defaultString);
  341. }
  342. int idUserInterfaceLocal::GetStateInt( const char *varName, const char* defaultString ) const {
  343. return state.GetInt(varName, defaultString);
  344. }
  345. float idUserInterfaceLocal::GetStateFloat( const char *varName, const char* defaultString ) const {
  346. return state.GetFloat(varName, defaultString);
  347. }
  348. void idUserInterfaceLocal::StateChanged( int _time, bool redraw ) {
  349. time = _time;
  350. if (desktop) {
  351. desktop->StateChanged( redraw );
  352. }
  353. if ( state.GetBool( "noninteractive" ) ) {
  354. interactive = false;
  355. }
  356. else {
  357. if (desktop) {
  358. interactive = desktop->Interactive();
  359. } else {
  360. interactive = false;
  361. }
  362. }
  363. }
  364. const char *idUserInterfaceLocal::Activate(bool activate, int _time) {
  365. time = _time;
  366. active = activate;
  367. if ( desktop ) {
  368. activateStr = "";
  369. desktop->Activate( activate, activateStr );
  370. return activateStr;
  371. }
  372. return "";
  373. }
  374. void idUserInterfaceLocal::Trigger(int _time) {
  375. time = _time;
  376. if ( desktop ) {
  377. desktop->Trigger();
  378. }
  379. }
  380. void idUserInterfaceLocal::ReadFromDemoFile( class idDemoFile *f ) {
  381. idStr work;
  382. f->ReadDict( state );
  383. source = state.GetString("name");
  384. if (desktop == NULL) {
  385. f->Log("creating new gui\n");
  386. desktop = new idWindow(this);
  387. desktop->SetFlag( WIN_DESKTOP );
  388. desktop->SetDC( &uiManagerLocal.dc );
  389. desktop->ReadFromDemoFile(f);
  390. } else {
  391. f->Log("re-using gui\n");
  392. desktop->ReadFromDemoFile(f, false);
  393. }
  394. f->ReadFloat( cursorX );
  395. f->ReadFloat( cursorY );
  396. bool add = true;
  397. int c = uiManagerLocal.demoGuis.Num();
  398. for ( int i = 0; i < c; i++ ) {
  399. if ( uiManagerLocal.demoGuis[i] == this ) {
  400. add = false;
  401. break;
  402. }
  403. }
  404. if (add) {
  405. uiManagerLocal.demoGuis.Append(this);
  406. }
  407. }
  408. void idUserInterfaceLocal::WriteToDemoFile( class idDemoFile *f ) {
  409. idStr work;
  410. f->WriteDict( state );
  411. if (desktop) {
  412. desktop->WriteToDemoFile(f);
  413. }
  414. f->WriteFloat( cursorX );
  415. f->WriteFloat( cursorY );
  416. }
  417. bool idUserInterfaceLocal::WriteToSaveGame( idFile *savefile ) const {
  418. int len;
  419. const idKeyValue *kv;
  420. const char *string;
  421. int num = state.GetNumKeyVals();
  422. savefile->Write( &num, sizeof( num ) );
  423. for( int i = 0; i < num; i++ ) {
  424. kv = state.GetKeyVal( i );
  425. len = kv->GetKey().Length();
  426. string = kv->GetKey().c_str();
  427. savefile->Write( &len, sizeof( len ) );
  428. savefile->Write( string, len );
  429. len = kv->GetValue().Length();
  430. string = kv->GetValue().c_str();
  431. savefile->Write( &len, sizeof( len ) );
  432. savefile->Write( string, len );
  433. }
  434. savefile->Write( &active, sizeof( active ) );
  435. savefile->Write( &interactive, sizeof( interactive ) );
  436. savefile->Write( &uniqued, sizeof( uniqued ) );
  437. savefile->Write( &time, sizeof( time ) );
  438. len = activateStr.Length();
  439. savefile->Write( &len, sizeof( len ) );
  440. savefile->Write( activateStr.c_str(), len );
  441. len = pendingCmd.Length();
  442. savefile->Write( &len, sizeof( len ) );
  443. savefile->Write( pendingCmd.c_str(), len );
  444. len = returnCmd.Length();
  445. savefile->Write( &len, sizeof( len ) );
  446. savefile->Write( returnCmd.c_str(), len );
  447. savefile->Write( &cursorX, sizeof( cursorX ) );
  448. savefile->Write( &cursorY, sizeof( cursorY ) );
  449. desktop->WriteToSaveGame( savefile );
  450. return true;
  451. }
  452. bool idUserInterfaceLocal::ReadFromSaveGame( idFile *savefile ) {
  453. int num;
  454. int i, len;
  455. idStr key;
  456. idStr value;
  457. savefile->Read( &num, sizeof( num ) );
  458. state.Clear();
  459. for( i = 0; i < num; i++ ) {
  460. savefile->Read( &len, sizeof( len ) );
  461. key.Fill( ' ', len );
  462. savefile->Read( &key[0], len );
  463. savefile->Read( &len, sizeof( len ) );
  464. value.Fill( ' ', len );
  465. savefile->Read( &value[0], len );
  466. state.Set( key, value );
  467. }
  468. savefile->Read( &active, sizeof( active ) );
  469. savefile->Read( &interactive, sizeof( interactive ) );
  470. savefile->Read( &uniqued, sizeof( uniqued ) );
  471. savefile->Read( &time, sizeof( time ) );
  472. savefile->Read( &len, sizeof( len ) );
  473. activateStr.Fill( ' ', len );
  474. savefile->Read( &activateStr[0], len );
  475. savefile->Read( &len, sizeof( len ) );
  476. pendingCmd.Fill( ' ', len );
  477. savefile->Read( &pendingCmd[0], len );
  478. savefile->Read( &len, sizeof( len ) );
  479. returnCmd.Fill( ' ', len );
  480. savefile->Read( &returnCmd[0], len );
  481. savefile->Read( &cursorX, sizeof( cursorX ) );
  482. savefile->Read( &cursorY, sizeof( cursorY ) );
  483. desktop->ReadFromSaveGame( savefile );
  484. return true;
  485. }
  486. size_t idUserInterfaceLocal::Size() {
  487. size_t sz = sizeof(*this) + state.Size() + source.Allocated();
  488. if ( desktop ) {
  489. sz += desktop->Size();
  490. }
  491. return sz;
  492. }
  493. void idUserInterfaceLocal::RecurseSetKeyBindingNames( idWindow *window ) {
  494. int i;
  495. idWinVar *v = window->GetWinVarByName( "bind" );
  496. if ( v ) {
  497. SetStateString( v->GetName(), idKeyInput::KeysFromBinding( v->GetName() ) );
  498. }
  499. i = 0;
  500. while ( i < window->GetChildCount() ) {
  501. idWindow *next = window->GetChild( i );
  502. if ( next ) {
  503. RecurseSetKeyBindingNames( next );
  504. }
  505. i++;
  506. }
  507. }
  508. /*
  509. ==============
  510. idUserInterfaceLocal::SetKeyBindingNames
  511. ==============
  512. */
  513. void idUserInterfaceLocal::SetKeyBindingNames( void ) {
  514. if ( !desktop ) {
  515. return;
  516. }
  517. // walk the windows
  518. RecurseSetKeyBindingNames( desktop );
  519. }
  520. /*
  521. ==============
  522. idUserInterfaceLocal::SetCursor
  523. ==============
  524. */
  525. void idUserInterfaceLocal::SetCursor( float x, float y ) {
  526. cursorX = x;
  527. cursorY = y;
  528. }