SliderWindow.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "DeviceContext.h"
  23. #include "Window.h"
  24. #include "UserInterfaceLocal.h"
  25. #include "SliderWindow.h"
  26. /*
  27. ============
  28. idSliderWindow::CommonInit
  29. ============
  30. */
  31. void idSliderWindow::CommonInit() {
  32. value = 0.0;
  33. low = 0.0;
  34. high = 100.0;
  35. stepSize = 1.0;
  36. thumbMat = declManager->FindMaterial("_default");
  37. buddyWin = NULL;
  38. cvar = NULL;
  39. cvar_init = false;
  40. liveUpdate = true;
  41. vertical = false;
  42. scrollbar = false;
  43. verticalFlip = false;
  44. }
  45. idSliderWindow::idSliderWindow(idUserInterfaceLocal *g) : idWindow(g) {
  46. gui = g;
  47. CommonInit();
  48. }
  49. idSliderWindow::~idSliderWindow() {
  50. }
  51. bool idSliderWindow::ParseInternalVar(const char *_name, idTokenParser *src) {
  52. if (idStr::Icmp(_name, "stepsize") == 0 || idStr::Icmp(_name, "step") == 0) {
  53. stepSize = src->ParseFloat();
  54. return true;
  55. }
  56. if (idStr::Icmp(_name, "low") == 0) {
  57. low = src->ParseFloat();
  58. return true;
  59. }
  60. if (idStr::Icmp(_name, "high") == 0) {
  61. high = src->ParseFloat();
  62. return true;
  63. }
  64. if (idStr::Icmp(_name, "vertical") == 0) {
  65. vertical = src->ParseBool();
  66. return true;
  67. }
  68. if (idStr::Icmp(_name, "verticalflip") == 0) {
  69. verticalFlip = src->ParseBool();
  70. return true;
  71. }
  72. if (idStr::Icmp(_name, "scrollbar") == 0) {
  73. scrollbar = src->ParseBool();
  74. return true;
  75. }
  76. if (idStr::Icmp(_name, "thumbshader") == 0) {
  77. ParseString(src, thumbShader);
  78. declManager->FindMaterial(thumbShader);
  79. return true;
  80. }
  81. return idWindow::ParseInternalVar(_name, src);
  82. }
  83. idWinVar *idSliderWindow::GetWinVarByName(const char *_name, bool fixup, drawWin_t** owner) {
  84. if (idStr::Icmp(_name, "value") == 0) {
  85. return &value;
  86. }
  87. if (idStr::Icmp(_name, "cvar") == 0) {
  88. return &cvarStr;
  89. }
  90. if ( idStr::Icmp( _name, "liveUpdate" ) == 0 ) {
  91. return &liveUpdate;
  92. }
  93. if ( idStr::Icmp( _name, "cvarGroup" ) == 0 ) {
  94. return &cvarGroup;
  95. }
  96. return idWindow::GetWinVarByName(_name, fixup, owner);
  97. }
  98. const char *idSliderWindow::HandleEvent(const sysEvent_t *event, bool *updateVisuals) {
  99. if (!(event->evType == SE_KEY && event->evValue2)) {
  100. return "";
  101. }
  102. int key = event->evValue;
  103. if ( event->evValue2 && key == K_MOUSE1 ) {
  104. SetCapture(this);
  105. RouteMouseCoords(0.0f, 0.0f);
  106. return "";
  107. }
  108. if ( key == K_RIGHTARROW || key == K_KP_6 || ( key == K_MOUSE2 && gui->CursorY() > thumbRect.y ) ) {
  109. value = value + stepSize;
  110. }
  111. if ( key == K_LEFTARROW || key == K_KP_4 || ( key == K_MOUSE2 && gui->CursorY() < thumbRect.y ) ) {
  112. value = value - stepSize;
  113. }
  114. if (buddyWin) {
  115. buddyWin->HandleBuddyUpdate(this);
  116. } else {
  117. gui->SetStateFloat( cvarStr, value );
  118. UpdateCvar( false );
  119. }
  120. return "";
  121. }
  122. void idSliderWindow::SetBuddy(idWindow *buddy) {
  123. buddyWin = buddy;
  124. }
  125. void idSliderWindow::PostParse() {
  126. idWindow::PostParse();
  127. value = 0.0;
  128. thumbMat = declManager->FindMaterial(thumbShader);
  129. thumbMat->SetSort( SS_GUI );
  130. thumbWidth = thumbMat->GetImageWidth();
  131. thumbHeight = thumbMat->GetImageHeight();
  132. //vertical = state.GetBool("vertical");
  133. //scrollbar = state.GetBool("scrollbar");
  134. flags |= (WIN_HOLDCAPTURE | WIN_CANFOCUS);
  135. InitCvar();
  136. }
  137. void idSliderWindow::InitWithDefaults(const char *_name, const idRectangle &_rect, const idVec4 &_foreColor, const idVec4 &_matColor, const char *_background, const char *thumbShader, bool _vertical, bool _scrollbar) {
  138. SetInitialState(_name);
  139. rect = _rect;
  140. foreColor = _foreColor;
  141. matColor = _matColor;
  142. thumbMat = declManager->FindMaterial(thumbShader);
  143. thumbMat->SetSort( SS_GUI );
  144. thumbWidth = thumbMat->GetImageWidth();
  145. thumbHeight = thumbMat->GetImageHeight();
  146. background = declManager->FindMaterial(_background);
  147. background->SetSort( SS_GUI );
  148. vertical = _vertical;
  149. scrollbar = _scrollbar;
  150. flags |= WIN_HOLDCAPTURE;
  151. }
  152. void idSliderWindow::SetRange(float _low, float _high, float _step) {
  153. low = _low;
  154. high = _high;
  155. stepSize = _step;
  156. }
  157. void idSliderWindow::SetValue(float _value) {
  158. value = _value;
  159. }
  160. void idSliderWindow::Draw(int time, float x, float y) {
  161. idVec4 color = foreColor;
  162. if ( !cvar && !buddyWin ) {
  163. return;
  164. }
  165. if ( !thumbWidth || !thumbHeight ) {
  166. thumbWidth = thumbMat->GetImageWidth();
  167. thumbHeight = thumbMat->GetImageHeight();
  168. }
  169. UpdateCvar( true );
  170. if ( value > high ) {
  171. value = high;
  172. } else if ( value < low ) {
  173. value = low;
  174. }
  175. float range = high - low;
  176. if ( range <= 0.0f ) {
  177. return;
  178. }
  179. float thumbPos = (range) ? (value - low) / range : 0.0;
  180. if (vertical) {
  181. if ( verticalFlip ) {
  182. thumbPos = 1.f - thumbPos;
  183. }
  184. thumbPos *= drawRect.h - thumbHeight;
  185. thumbPos += drawRect.y;
  186. thumbRect.y = thumbPos;
  187. thumbRect.x = drawRect.x;
  188. } else {
  189. thumbPos *= drawRect.w - thumbWidth;
  190. thumbPos += drawRect.x;
  191. thumbRect.x = thumbPos;
  192. thumbRect.y = drawRect.y;
  193. }
  194. thumbRect.w = thumbWidth;
  195. thumbRect.h = thumbHeight;
  196. if ( hover && !noEvents && Contains(gui->CursorX(), gui->CursorY()) ) {
  197. color = hoverColor;
  198. } else {
  199. hover = false;
  200. }
  201. if ( flags & WIN_CAPTURE ) {
  202. color = hoverColor;
  203. hover = true;
  204. }
  205. dc->DrawMaterial(thumbRect.x, thumbRect.y, thumbRect.w, thumbRect.h, thumbMat, color);
  206. if ( flags & WIN_FOCUS ) {
  207. dc->DrawRect(thumbRect.x+1.0f, thumbRect.y+1.0f, thumbRect.w-2.0f, thumbRect.h-2.0f, 1.0f, color);
  208. }
  209. }
  210. void idSliderWindow::DrawBackground(const idRectangle &_drawRect) {
  211. if ( !cvar && !buddyWin ) {
  212. return;
  213. }
  214. if ( high - low <= 0.0f ) {
  215. return;
  216. }
  217. idRectangle r = _drawRect;
  218. if (!scrollbar) {
  219. if ( vertical ) {
  220. r.y += thumbHeight / 2.f;
  221. r.h -= thumbHeight;
  222. } else {
  223. r.x += thumbWidth / 2.0;
  224. r.w -= thumbWidth;
  225. }
  226. }
  227. idWindow::DrawBackground(r);
  228. }
  229. const char *idSliderWindow::RouteMouseCoords(float xd, float yd) {
  230. float pct;
  231. if (!(flags & WIN_CAPTURE)) {
  232. return "";
  233. }
  234. idRectangle r = drawRect;
  235. r.x = actualX;
  236. r.y = actualY;
  237. r.x += thumbWidth / 2.0;
  238. r.w -= thumbWidth;
  239. if (vertical) {
  240. r.y += thumbHeight / 2;
  241. r.h -= thumbHeight;
  242. if (gui->CursorY() >= r.y && gui->CursorY() <= r.Bottom()) {
  243. pct = (gui->CursorY() - r.y) / r.h;
  244. if ( verticalFlip ) {
  245. pct = 1.f - pct;
  246. }
  247. value = low + (high - low) * pct;
  248. } else if (gui->CursorY() < r.y) {
  249. if ( verticalFlip ) {
  250. value = high;
  251. } else {
  252. value = low;
  253. }
  254. } else {
  255. if ( verticalFlip ) {
  256. value = low;
  257. } else {
  258. value = high;
  259. }
  260. }
  261. } else {
  262. r.x += thumbWidth / 2;
  263. r.w -= thumbWidth;
  264. if (gui->CursorX() >= r.x && gui->CursorX() <= r.Right()) {
  265. pct = (gui->CursorX() - r.x) / r.w;
  266. value = low + (high - low) * pct;
  267. } else if (gui->CursorX() < r.x) {
  268. value = low;
  269. } else {
  270. value = high;
  271. }
  272. }
  273. if (buddyWin) {
  274. buddyWin->HandleBuddyUpdate(this);
  275. } else {
  276. gui->SetStateFloat( cvarStr, value );
  277. }
  278. UpdateCvar( false );
  279. return "";
  280. }
  281. void idSliderWindow::Activate(bool activate, idStr &act) {
  282. idWindow::Activate(activate, act);
  283. if ( activate ) {
  284. UpdateCvar( true, true );
  285. }
  286. }
  287. /*
  288. ============
  289. idSliderWindow::InitCvar
  290. ============
  291. */
  292. void idSliderWindow::InitCvar( ) {
  293. if ( cvarStr[0] == '\0' ) {
  294. if ( !buddyWin ) {
  295. common->Warning( "idSliderWindow::InitCvar: gui '%s' window '%s' has an empty cvar string", gui->GetSourceFile(), name.c_str() );
  296. }
  297. cvar_init = true;
  298. cvar = NULL;
  299. return;
  300. }
  301. cvar = cvarSystem->Find( cvarStr );
  302. if ( !cvar ) {
  303. common->Warning( "idSliderWindow::InitCvar: gui '%s' window '%s' references undefined cvar '%s'", gui->GetSourceFile(), name.c_str(), cvarStr.c_str() );
  304. cvar_init = true;
  305. return;
  306. }
  307. }
  308. /*
  309. ============
  310. idSliderWindow::UpdateCvar
  311. ============
  312. */
  313. void idSliderWindow::UpdateCvar( bool read, bool force ) {
  314. if ( buddyWin || !cvar ) {
  315. return;
  316. }
  317. if ( force || liveUpdate ) {
  318. value = cvar->GetFloat();
  319. if ( value != gui->State().GetFloat( cvarStr ) ) {
  320. if ( read ) {
  321. gui->SetStateFloat( cvarStr, value );
  322. } else {
  323. value = gui->State().GetFloat( cvarStr );
  324. cvar->SetFloat( value );
  325. }
  326. }
  327. }
  328. }
  329. /*
  330. ============
  331. idSliderWindow::RunNamedEvent
  332. ============
  333. */
  334. void idSliderWindow::RunNamedEvent( const char* eventName ) {
  335. idStr event, group;
  336. if ( !idStr::Cmpn( eventName, "cvar read ", 10 ) ) {
  337. event = eventName;
  338. group = event.Mid( 10, event.Length() - 10 );
  339. if ( !group.Cmp( cvarGroup ) ) {
  340. UpdateCvar( true, true );
  341. }
  342. } else if ( !idStr::Cmpn( eventName, "cvar write ", 11 ) ) {
  343. event = eventName;
  344. group = event.Mid( 11, event.Length() - 11 );
  345. if ( !group.Cmp( cvarGroup ) ) {
  346. UpdateCvar( false, true );
  347. }
  348. }
  349. }