MarkerWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. // included for image uploading for player stat graph
  23. #include "../renderer/Image.h"
  24. #include "DeviceContext.h"
  25. #include "Window.h"
  26. #include "UserInterfaceLocal.h"
  27. #include "MarkerWindow.h"
  28. class idImage;
  29. void idMarkerWindow::CommonInit() {
  30. numStats = 0;
  31. currentTime = -1;
  32. currentMarker = -1;
  33. stopTime = -1;
  34. imageBuff = NULL;
  35. markerMat = NULL;
  36. markerStop = NULL;
  37. }
  38. idMarkerWindow::idMarkerWindow(idDeviceContext *d, idUserInterfaceLocal *g) : idWindow(d, g) {
  39. dc = d;
  40. gui = g;
  41. CommonInit();
  42. }
  43. idMarkerWindow::idMarkerWindow(idUserInterfaceLocal *g) : idWindow(g) {
  44. gui = g;
  45. CommonInit();
  46. }
  47. idMarkerWindow::~idMarkerWindow() {
  48. }
  49. bool idMarkerWindow::ParseInternalVar(const char *_name, idParser *src) {
  50. if (idStr::Icmp(_name, "markerMat") == 0) {
  51. idStr str;
  52. ParseString(src, str);
  53. markerMat = declManager->FindMaterial(str);
  54. markerMat->SetSort( SS_GUI );
  55. return true;
  56. }
  57. if (idStr::Icmp(_name, "markerStop") == 0) {
  58. idStr str;
  59. ParseString(src, str);
  60. markerStop = declManager->FindMaterial(str);
  61. markerStop->SetSort( SS_GUI );
  62. return true;
  63. }
  64. if (idStr::Icmp(_name, "markerColor") == 0) {
  65. ParseVec4(src, markerColor);
  66. return true;
  67. }
  68. return idWindow::ParseInternalVar(_name, src);
  69. }
  70. idWinVar *idMarkerWindow::GetWinVarByName(const char *_name, bool fixup) {
  71. return idWindow::GetWinVarByName(_name, fixup);
  72. }
  73. const char *idMarkerWindow::HandleEvent(const sysEvent_t *event, bool *updateVisuals) {
  74. if (!(event->evType == SE_KEY && event->evValue2)) {
  75. return "";
  76. }
  77. int key = event->evValue;
  78. if (event->evValue2 && key == K_MOUSE1) {
  79. gui->GetDesktop()->SetChildWinVarVal("markerText", "text", "");
  80. idRectangle r;
  81. int c = markerTimes.Num();
  82. int i;
  83. for (i = 0; i < c; i++) {
  84. markerData_t &md = markerTimes[i];
  85. if (md.rect.Contains(gui->CursorX(), gui->CursorY())) {
  86. currentMarker = i;
  87. gui->SetStateInt( "currentMarker", md.time );
  88. stopTime = md.time;
  89. gui->GetDesktop()->SetChildWinVarVal("markerText", "text", va("Marker set at %.2i:%.2i", md.time / 60 / 60, (md.time / 60) % 60));
  90. gui->GetDesktop()->SetChildWinVarVal("markerText", "visible", "1");
  91. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "matcolor", "1 1 1 1");
  92. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "text", "");
  93. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "background", md.mat->GetName());
  94. break;
  95. }
  96. }
  97. if ( i == c ) {
  98. // no marker selected;
  99. currentMarker = -1;
  100. gui->SetStateInt( "currentMarker", currentTime );
  101. stopTime = currentTime;
  102. gui->GetDesktop()->SetChildWinVarVal("markerText", "text", va("Marker set at %.2i:%.2i", currentTime / 60 / 60, (currentTime / 60) % 60));
  103. gui->GetDesktop()->SetChildWinVarVal("markerText", "visible", "1");
  104. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "matcolor", "0 0 0 0");
  105. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "text", "No Preview");
  106. }
  107. float pct = gui->State().GetFloat( "loadPct" );
  108. int len = gui->State().GetInt( "loadLength" );
  109. if (stopTime > len * pct) {
  110. return "cmdDemoGotoMarker";
  111. }
  112. } else if (key == K_MOUSE2) {
  113. stopTime = -1;
  114. gui->GetDesktop()->SetChildWinVarVal("markerText", "text", "");
  115. gui->SetStateInt( "currentMarker", -1 );
  116. return "cmdDemoGotoMarker";
  117. } else if (key == K_SPACE) {
  118. return "cmdDemoPauseFrame";
  119. }
  120. return "";
  121. }
  122. void idMarkerWindow::PostParse() {
  123. idWindow::PostParse();
  124. }
  125. static const int HEALTH_MAX = 100;
  126. static const int COMBAT_MAX = 100;
  127. static const int RATE_MAX = 125;
  128. static const int STAMINA_MAX = 12;
  129. void idMarkerWindow::Draw(int time, float x, float y) {
  130. float pct;
  131. idRectangle r = clientRect;
  132. int len = gui->State().GetInt( "loadLength" );
  133. if (len == 0) {
  134. len = 1;
  135. }
  136. if (numStats > 1) {
  137. int c = markerTimes.Num();
  138. if (c > 0) {
  139. for (int i = 0; i < c; i++) {
  140. markerData_t &md = markerTimes[i];
  141. if (md.rect.w == 0) {
  142. md.rect.x = (r.x + r.w * ((float)md.time / len)) - 8;
  143. md.rect.y = r.y + r.h - 20;
  144. md.rect.w = 16;
  145. md.rect.h = 16;
  146. }
  147. dc->DrawMaterial(md.rect.x, md.rect.y, md.rect.w, md.rect.h, markerMat, markerColor);
  148. }
  149. }
  150. }
  151. r.y += 10;
  152. if (r.w > 0 && r.Contains(gui->CursorX(), gui->CursorY())) {
  153. pct = (gui->CursorX() - r.x) / r.w;
  154. currentTime = len * pct;
  155. r.x = (gui->CursorX() > r.x + r.w - 40) ? gui->CursorX() - 40 : gui->CursorX();
  156. r.y = gui->CursorY() - 15;
  157. r.w = 40;
  158. r.h = 20;
  159. dc->DrawText(va("%.2i:%.2i", currentTime / 60 / 60, (currentTime / 60) % 60), 0.25, 0, idDeviceContext::colorWhite, r, false);
  160. }
  161. if (stopTime >= 0 && markerStop) {
  162. r = clientRect;
  163. r.y += (r.h - 32) / 2;
  164. pct = (float)stopTime / len;
  165. r.x += (r.w * pct) - 16;
  166. idVec4 color(1, 1, 1, 0.65f);
  167. dc->DrawMaterial(r.x, r.y, 32, 32, markerStop, color);
  168. }
  169. }
  170. const char *idMarkerWindow::RouteMouseCoords(float xd, float yd) {
  171. const char * ret = idWindow::RouteMouseCoords(xd, yd);
  172. idRectangle r;
  173. int i, c = markerTimes.Num();
  174. int len = gui->State().GetInt( "loadLength" );
  175. if (len == 0) {
  176. len = 1;
  177. }
  178. for (i = 0; i < c; i++) {
  179. markerData_t &md = markerTimes[i];
  180. if (md.rect.Contains(gui->CursorY(), gui->CursorX())) {
  181. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "background", md.mat->GetName());
  182. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "matcolor", "1 1 1 1");
  183. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "text", "");
  184. break;
  185. }
  186. }
  187. if (i >= c) {
  188. if (currentMarker == -1) {
  189. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "matcolor", "0 0 0 0");
  190. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "text", "No Preview");
  191. } else {
  192. markerData_t &md = markerTimes[currentMarker];
  193. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "background", md.mat->GetName());
  194. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "matcolor", "1 1 1 1");
  195. gui->GetDesktop()->SetChildWinVarVal("markerBackground", "text", "");
  196. }
  197. }
  198. return ret;
  199. }
  200. void idMarkerWindow::Point(int x, int y, dword *out, dword color) {
  201. int index = (63-y) * 512 + x;
  202. if (index >= 0 && index < 512 * 64) {
  203. out[index] = color;
  204. } else {
  205. common->Warning("Out of bounds on point %i : %i", x, y);
  206. }
  207. }
  208. void idMarkerWindow::Line(int x1, int y1, int x2, int y2, dword* out, dword color) {
  209. int deltax = abs(x2 - x1);
  210. int deltay = abs(y2 - y1);
  211. int incx = (x1 > x2) ? -1 : 1;
  212. int incy = (y1 > y2) ? -1 : 1;
  213. int right, up, dir;
  214. if (deltax > deltay) {
  215. right = deltay * 2;
  216. up = right - deltax * 2;
  217. dir = right - deltax;
  218. while (deltax-- >= 0) {
  219. Point(x1, y1, out, color);
  220. x1 += incx;
  221. y1 += (dir > 0) ? incy : 0;
  222. dir += (dir > 0) ? up : right;
  223. }
  224. } else {
  225. right = deltax * 2;
  226. up = right - deltay * 2;
  227. dir = right - deltay;
  228. while ( deltay-- >= 0) {
  229. Point(x1, y1, out, color);
  230. x1 += (dir > 0) ? incx : 0;
  231. y1 += incy;
  232. dir += (dir > 0) ? up : right;
  233. }
  234. }
  235. }
  236. void idMarkerWindow::Activate(bool activate, idStr &act) {
  237. idWindow::Activate(activate, act);
  238. if (activate) {
  239. int i;
  240. gui->GetDesktop()->SetChildWinVarVal("markerText", "text", "");
  241. imageBuff = (dword*)Mem_Alloc(512*64*4);
  242. markerTimes.Clear();
  243. currentMarker = -1;
  244. currentTime = -1;
  245. stopTime = -1;
  246. statData = gui->State().GetString( "statData" );
  247. numStats = 0;
  248. if (statData.Length()) {
  249. idFile *file = fileSystem->OpenFileRead(statData);
  250. if (file) {
  251. file->Read(&numStats, sizeof(numStats));
  252. file->Read(loggedStats, numStats * sizeof(loggedStats[0]));
  253. for (i = 0; i < numStats; i++) {
  254. if (loggedStats[i].health < 0) {
  255. loggedStats[i].health = 0;
  256. }
  257. if (loggedStats[i].stamina < 0) {
  258. loggedStats[i].stamina = 0;
  259. }
  260. if (loggedStats[i].heartRate < 0) {
  261. loggedStats[i].heartRate = 0;
  262. }
  263. if (loggedStats[i].combat < 0) {
  264. loggedStats[i].combat = 0;
  265. }
  266. }
  267. fileSystem->CloseFile(file);
  268. }
  269. }
  270. if (numStats > 1 && background) {
  271. idStr markerPath = statData;
  272. markerPath.StripFilename();
  273. idFileList *markers;
  274. markers = fileSystem->ListFiles( markerPath, ".tga", false, true );
  275. idStr name;
  276. for ( i = 0; i < markers->GetNumFiles(); i++ ) {
  277. name = markers->GetFile( i );
  278. markerData_t md;
  279. md.mat = declManager->FindMaterial( name );
  280. md.mat->SetSort( SS_GUI );
  281. name.StripPath();
  282. name.StripFileExtension();
  283. md.time = atoi(name);
  284. markerTimes.Append(md);
  285. }
  286. fileSystem->FreeFileList( markers );
  287. memset(imageBuff, 0, 512*64*4);
  288. float step = 511.0f / (numStats - 1);
  289. float startX = 0;
  290. float x1, y1, x2, y2;
  291. x1 = 0 - step;
  292. for (i = 0; i < numStats-1; i++) {
  293. x1 += step;
  294. x2 = x1 + step;
  295. y1 = 63 * ((float)loggedStats[i].health / HEALTH_MAX);
  296. y2 = 63 * ((float)loggedStats[i+1].health / HEALTH_MAX);
  297. Line(x1, y1, x2, y2, imageBuff, 0xff0000ff);
  298. y1 = 63 * ((float)loggedStats[i].heartRate / RATE_MAX);
  299. y2 = 63 * ((float)loggedStats[i+1].heartRate / RATE_MAX);
  300. Line(x1, y1, x2, y2, imageBuff, 0xff00ff00);
  301. // stamina not quite as high on graph so health does not get obscured with both at 100%
  302. y1 = 62 * ((float)loggedStats[i].stamina / STAMINA_MAX);
  303. y2 = 62 * ((float)loggedStats[i+1].stamina / STAMINA_MAX);
  304. Line(x1, y1, x2, y2, imageBuff, 0xffff0000);
  305. y1 = 63 * ((float)loggedStats[i].combat / COMBAT_MAX);
  306. y2 = 63 * ((float)loggedStats[i+1].combat / COMBAT_MAX);
  307. Line(x1, y1, x2, y2, imageBuff, 0xff00ffff);
  308. }
  309. const shaderStage_t *stage = background->GetStage(0);
  310. if (stage) {
  311. stage->texture.image->UploadScratch((byte*)imageBuff, 512, 64);
  312. }
  313. Mem_Free(imageBuff);
  314. }
  315. }
  316. }
  317. void idMarkerWindow::MouseExit() {
  318. idWindow::MouseExit();
  319. }
  320. void idMarkerWindow::MouseEnter() {
  321. idWindow::MouseEnter();
  322. }