ChoiceWindow.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 "DeviceContext.h"
  23. #include "Window.h"
  24. #include "UserInterfaceLocal.h"
  25. #include "ChoiceWindow.h"
  26. /*
  27. ============
  28. idChoiceWindow::InitVars
  29. ============
  30. */
  31. void idChoiceWindow::InitVars( ) {
  32. if ( cvarStr.Length() ) {
  33. cvar = cvarSystem->Find( cvarStr );
  34. if ( !cvar ) {
  35. common->Warning( "idChoiceWindow::InitVars: gui '%s' window '%s' references undefined cvar '%s'", gui->GetSourceFile(), name.c_str(), cvarStr.c_str() );
  36. return;
  37. }
  38. updateStr.Append( &cvarStr );
  39. }
  40. if ( guiStr.Length() ) {
  41. updateStr.Append( &guiStr );
  42. }
  43. updateStr.SetGuiInfo( gui->GetStateDict() );
  44. updateStr.Update();
  45. }
  46. /*
  47. ============
  48. idChoiceWindow::CommonInit
  49. ============
  50. */
  51. void idChoiceWindow::CommonInit() {
  52. currentChoice = 0;
  53. choiceType = 0;
  54. cvar = NULL;
  55. liveUpdate = true;
  56. choices.Clear();
  57. }
  58. idChoiceWindow::idChoiceWindow(idDeviceContext *d, idUserInterfaceLocal *g) : idWindow(d, g) {
  59. dc = d;
  60. gui = g;
  61. CommonInit();
  62. }
  63. idChoiceWindow::idChoiceWindow(idUserInterfaceLocal *g) : idWindow(g) {
  64. gui = g;
  65. CommonInit();
  66. }
  67. idChoiceWindow::~idChoiceWindow() {
  68. }
  69. void idChoiceWindow::RunNamedEvent( const char* eventName ) {
  70. idStr event, group;
  71. if ( !idStr::Cmpn( eventName, "cvar read ", 10 ) ) {
  72. event = eventName;
  73. group = event.Mid( 10, event.Length() - 10 );
  74. if ( !group.Cmp( updateGroup ) ) {
  75. UpdateVars( true, true );
  76. }
  77. } else if ( !idStr::Cmpn( eventName, "cvar write ", 11 ) ) {
  78. event = eventName;
  79. group = event.Mid( 11, event.Length() - 11 );
  80. if ( !group.Cmp( updateGroup ) ) {
  81. UpdateVars( false, true );
  82. }
  83. }
  84. }
  85. void idChoiceWindow::UpdateVars( bool read, bool force ) {
  86. if ( force || liveUpdate ) {
  87. if ( cvar && cvarStr.NeedsUpdate() ) {
  88. if ( read ) {
  89. cvarStr.Set( cvar->GetString() );
  90. } else {
  91. cvar->SetString( cvarStr.c_str() );
  92. }
  93. }
  94. if ( !read && guiStr.NeedsUpdate() ) {
  95. guiStr.Set( va( "%i", currentChoice ) );
  96. }
  97. }
  98. }
  99. const char *idChoiceWindow::HandleEvent(const sysEvent_t *event, bool *updateVisuals) {
  100. int key;
  101. bool runAction = false;
  102. bool runAction2 = false;
  103. if ( event->evType == SE_KEY ) {
  104. key = event->evValue;
  105. if ( key == K_RIGHTARROW || key == K_KP_RIGHTARROW || key == K_MOUSE1) {
  106. // never affects the state, but we want to execute script handlers anyway
  107. if ( !event->evValue2 ) {
  108. RunScript( ON_ACTIONRELEASE );
  109. return cmd;
  110. }
  111. currentChoice++;
  112. if (currentChoice >= choices.Num()) {
  113. currentChoice = 0;
  114. }
  115. runAction = true;
  116. }
  117. if ( key == K_LEFTARROW || key == K_KP_LEFTARROW || key == K_MOUSE2) {
  118. // never affects the state, but we want to execute script handlers anyway
  119. if ( !event->evValue2 ) {
  120. RunScript( ON_ACTIONRELEASE );
  121. return cmd;
  122. }
  123. currentChoice--;
  124. if (currentChoice < 0) {
  125. currentChoice = choices.Num() - 1;
  126. }
  127. runAction = true;
  128. }
  129. if ( !event->evValue2 ) {
  130. // is a key release with no action catch
  131. return "";
  132. }
  133. } else if ( event->evType == SE_CHAR ) {
  134. key = event->evValue;
  135. int potentialChoice = -1;
  136. for ( int i = 0; i < choices.Num(); i++ ) {
  137. if ( toupper(key) == toupper(choices[i][0]) ) {
  138. if ( i < currentChoice && potentialChoice < 0 ) {
  139. potentialChoice = i;
  140. } else if ( i > currentChoice ) {
  141. potentialChoice = -1;
  142. currentChoice = i;
  143. break;
  144. }
  145. }
  146. }
  147. if ( potentialChoice >= 0 ) {
  148. currentChoice = potentialChoice;
  149. }
  150. runAction = true;
  151. runAction2 = true;
  152. } else {
  153. return "";
  154. }
  155. if ( runAction ) {
  156. RunScript( ON_ACTION );
  157. }
  158. if ( choiceType == 0 ) {
  159. cvarStr.Set( va( "%i", currentChoice ) );
  160. } else if ( values.Num() ) {
  161. cvarStr.Set( values[ currentChoice ] );
  162. } else {
  163. cvarStr.Set( choices[ currentChoice ] );
  164. }
  165. UpdateVars( false );
  166. if ( runAction2 ) {
  167. RunScript( ON_ACTIONRELEASE );
  168. }
  169. return cmd;
  170. }
  171. void idChoiceWindow::ValidateChoice() {
  172. if ( currentChoice < 0 || currentChoice >= choices.Num() ) {
  173. currentChoice = 0;
  174. }
  175. if ( choices.Num() == 0 ) {
  176. choices.Append( "No Choices Defined" );
  177. }
  178. }
  179. void idChoiceWindow::UpdateChoice() {
  180. if ( !updateStr.Num() ) {
  181. return;
  182. }
  183. UpdateVars( true );
  184. updateStr.Update();
  185. if ( choiceType == 0 ) {
  186. // ChoiceType 0 stores current as an integer in either cvar or gui
  187. // If both cvar and gui are defined then cvar wins, but they are both updated
  188. if ( updateStr[ 0 ]->NeedsUpdate() ) {
  189. currentChoice = atoi( updateStr[ 0 ]->c_str() );
  190. }
  191. ValidateChoice();
  192. } else {
  193. // ChoiceType 1 stores current as a cvar string
  194. int c = ( values.Num() ) ? values.Num() : choices.Num();
  195. int i;
  196. for ( i = 0; i < c; i++ ) {
  197. if ( idStr::Icmp( cvarStr.c_str(), ( values.Num() ) ? values[i] : choices[i] ) == 0 ) {
  198. break;
  199. }
  200. }
  201. if (i == c) {
  202. i = 0;
  203. }
  204. currentChoice = i;
  205. ValidateChoice();
  206. }
  207. }
  208. bool idChoiceWindow::ParseInternalVar(const char *_name, idParser *src) {
  209. if (idStr::Icmp(_name, "choicetype") == 0) {
  210. choiceType = src->ParseInt();
  211. return true;
  212. }
  213. if (idStr::Icmp(_name, "currentchoice") == 0) {
  214. currentChoice = src->ParseInt();
  215. return true;
  216. }
  217. return idWindow::ParseInternalVar(_name, src);
  218. }
  219. idWinVar *idChoiceWindow::GetWinVarByName(const char *_name, bool fixup, drawWin_t** owner) {
  220. if ( idStr::Icmp( _name, "choices" ) == 0 ) {
  221. return &choicesStr;
  222. }
  223. if ( idStr::Icmp( _name, "values" ) == 0 ) {
  224. return &choiceVals;
  225. }
  226. if ( idStr::Icmp( _name, "cvar" ) == 0 ) {
  227. return &cvarStr;
  228. }
  229. if ( idStr::Icmp( _name, "gui" ) == 0 ) {
  230. return &guiStr;
  231. }
  232. if ( idStr::Icmp( _name, "liveUpdate" ) == 0 ) {
  233. return &liveUpdate;
  234. }
  235. if ( idStr::Icmp( _name, "updateGroup" ) == 0 ) {
  236. return &updateGroup;
  237. }
  238. return idWindow::GetWinVarByName(_name, fixup, owner);
  239. }
  240. // update the lists whenever the WinVar have changed
  241. void idChoiceWindow::UpdateChoicesAndVals( void ) {
  242. idToken token;
  243. idStr str2, str3;
  244. idLexer src;
  245. if ( latchedChoices.Icmp( choicesStr ) ) {
  246. choices.Clear();
  247. src.FreeSource();
  248. src.SetFlags( LEXFL_NOFATALERRORS | LEXFL_ALLOWPATHNAMES | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_ALLOWBACKSLASHSTRINGCONCAT );
  249. src.LoadMemory( choicesStr, choicesStr.Length(), "<ChoiceList>" );
  250. if ( src.IsLoaded() ) {
  251. while( src.ReadToken( &token ) ) {
  252. if ( token == ";" ) {
  253. if ( str2.Length() ) {
  254. str2.StripTrailingWhitespace();
  255. str2 = common->GetLanguageDict()->GetString( str2 );
  256. choices.Append(str2);
  257. str2 = "";
  258. }
  259. continue;
  260. }
  261. str2 += token;
  262. str2 += " ";
  263. }
  264. if ( str2.Length() ) {
  265. str2.StripTrailingWhitespace();
  266. choices.Append( str2 );
  267. }
  268. }
  269. latchedChoices = choicesStr.c_str();
  270. }
  271. if ( choiceVals.Length() && latchedVals.Icmp( choiceVals ) ) {
  272. values.Clear();
  273. src.FreeSource();
  274. src.SetFlags( LEXFL_ALLOWPATHNAMES | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_ALLOWBACKSLASHSTRINGCONCAT );
  275. src.LoadMemory( choiceVals, choiceVals.Length(), "<ChoiceVals>" );
  276. str2 = "";
  277. bool negNum = false;
  278. if ( src.IsLoaded() ) {
  279. while( src.ReadToken( &token ) ) {
  280. if (token == "-") {
  281. negNum = true;
  282. continue;
  283. }
  284. if (token == ";") {
  285. if (str2.Length()) {
  286. str2.StripTrailingWhitespace();
  287. values.Append( str2 );
  288. str2 = "";
  289. }
  290. continue;
  291. }
  292. if ( negNum ) {
  293. str2 += "-";
  294. negNum = false;
  295. }
  296. str2 += token;
  297. str2 += " ";
  298. }
  299. if ( str2.Length() ) {
  300. str2.StripTrailingWhitespace();
  301. values.Append( str2 );
  302. }
  303. }
  304. if ( choices.Num() != values.Num() ) {
  305. common->Warning( "idChoiceWindow:: gui '%s' window '%s' has value count unequal to choices count", gui->GetSourceFile(), name.c_str());
  306. }
  307. latchedVals = choiceVals.c_str();
  308. }
  309. }
  310. void idChoiceWindow::PostParse() {
  311. idWindow::PostParse();
  312. UpdateChoicesAndVals();
  313. InitVars();
  314. UpdateChoice();
  315. UpdateVars(false);
  316. flags |= WIN_CANFOCUS;
  317. }
  318. void idChoiceWindow::Draw(int time, float x, float y) {
  319. idVec4 color = foreColor;
  320. UpdateChoicesAndVals();
  321. UpdateChoice();
  322. // FIXME: It'd be really cool if textAlign worked, but a lot of the guis have it set wrong because it used to not work
  323. textAlign = 0;
  324. if ( textShadow ) {
  325. idStr shadowText = choices[currentChoice];
  326. idRectangle shadowRect = textRect;
  327. shadowText.RemoveColors();
  328. shadowRect.x += textShadow;
  329. shadowRect.y += textShadow;
  330. dc->DrawText( shadowText, textScale, textAlign, colorBlack, shadowRect, false, -1 );
  331. }
  332. if ( hover && !noEvents && Contains(gui->CursorX(), gui->CursorY()) ) {
  333. color = hoverColor;
  334. } else {
  335. hover = false;
  336. }
  337. if ( flags & WIN_FOCUS ) {
  338. color = hoverColor;
  339. }
  340. dc->DrawText( choices[currentChoice], textScale, textAlign, color, textRect, false, -1 );
  341. }
  342. void idChoiceWindow::Activate( bool activate, idStr &act ) {
  343. idWindow::Activate( activate, act );
  344. if ( activate ) {
  345. // sets the gui state based on the current choice the window contains
  346. UpdateChoice();
  347. }
  348. }