SimpleWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 "SimpleWindow.h"
  26. idSimpleWindow::idSimpleWindow(idWindow *win) {
  27. gui = win->GetGui();
  28. dc = win->dc;
  29. drawRect = win->drawRect;
  30. clientRect = win->clientRect;
  31. textRect = win->textRect;
  32. origin = win->origin;
  33. fontNum = win->fontNum;
  34. name = win->name;
  35. matScalex = win->matScalex;
  36. matScaley = win->matScaley;
  37. borderSize = win->borderSize;
  38. textAlign = win->textAlign;
  39. textAlignx = win->textAlignx;
  40. textAligny = win->textAligny;
  41. background = win->background;
  42. flags = win->flags;
  43. textShadow = win->textShadow;
  44. visible = win->visible;
  45. text = win->text;
  46. rect = win->rect;
  47. backColor = win->backColor;
  48. matColor = win->matColor;
  49. foreColor = win->foreColor;
  50. borderColor = win->borderColor;
  51. textScale = win->textScale;
  52. rotate = win->rotate;
  53. shear = win->shear;
  54. backGroundName = win->backGroundName;
  55. if (backGroundName.Length()) {
  56. background = declManager->FindMaterial(backGroundName);
  57. background->SetSort( SS_GUI );
  58. background->SetImageClassifications( 1 ); // just for resource tracking
  59. }
  60. backGroundName.SetMaterialPtr(&background);
  61. //
  62. // added parent
  63. mParent = win->GetParent();
  64. //
  65. hideCursor = win->hideCursor;
  66. idWindow *parent = win->GetParent();
  67. if (parent) {
  68. if (text.NeedsUpdate()) {
  69. parent->AddUpdateVar(&text);
  70. }
  71. if (visible.NeedsUpdate()) {
  72. parent->AddUpdateVar(&visible);
  73. }
  74. if (rect.NeedsUpdate()) {
  75. parent->AddUpdateVar(&rect);
  76. }
  77. if (backColor.NeedsUpdate()) {
  78. parent->AddUpdateVar(&backColor);
  79. }
  80. if (matColor.NeedsUpdate()) {
  81. parent->AddUpdateVar(&matColor);
  82. }
  83. if (foreColor.NeedsUpdate()) {
  84. parent->AddUpdateVar(&foreColor);
  85. }
  86. if (borderColor.NeedsUpdate()) {
  87. parent->AddUpdateVar(&borderColor);
  88. }
  89. if (textScale.NeedsUpdate()) {
  90. parent->AddUpdateVar(&textScale);
  91. }
  92. if (rotate.NeedsUpdate()) {
  93. parent->AddUpdateVar(&rotate);
  94. }
  95. if (shear.NeedsUpdate()) {
  96. parent->AddUpdateVar(&shear);
  97. }
  98. if (backGroundName.NeedsUpdate()) {
  99. parent->AddUpdateVar(&backGroundName);
  100. }
  101. }
  102. }
  103. idSimpleWindow::~idSimpleWindow() {
  104. }
  105. void idSimpleWindow::StateChanged( bool redraw ) {
  106. if ( redraw && background && background->CinematicLength() ) {
  107. background->UpdateCinematic( gui->GetTime() );
  108. }
  109. }
  110. void idSimpleWindow::SetupTransforms(float x, float y) {
  111. static idMat3 trans;
  112. static idVec3 org;
  113. trans.Identity();
  114. org.Set( origin.x + x, origin.y + y, 0 );
  115. if ( rotate ) {
  116. static idRotation rot;
  117. static idVec3 vec( 0, 0, 1 );
  118. rot.Set( org, vec, rotate );
  119. trans = rot.ToMat3();
  120. }
  121. static idMat3 smat;
  122. smat.Identity();
  123. if (shear.x() || shear.y()) {
  124. smat[0][1] = shear.x();
  125. smat[1][0] = shear.y();
  126. trans *= smat;
  127. }
  128. if ( !trans.IsIdentity() ) {
  129. dc->SetTransformInfo( org, trans );
  130. }
  131. }
  132. void idSimpleWindow::DrawBackground(const idRectangle &drawRect) {
  133. if (backColor.w() > 0) {
  134. dc->DrawFilledRect(drawRect.x, drawRect.y, drawRect.w, drawRect.h, backColor);
  135. }
  136. if (background) {
  137. if (matColor.w() > 0) {
  138. float scalex, scaley;
  139. if ( flags & WIN_NATURALMAT ) {
  140. scalex = drawRect.w / background->GetImageWidth();
  141. scaley = drawRect.h / background->GetImageHeight();
  142. } else {
  143. scalex = matScalex;
  144. scaley = matScaley;
  145. }
  146. dc->DrawMaterial(drawRect.x, drawRect.y, drawRect.w, drawRect.h, background, matColor, scalex, scaley);
  147. }
  148. }
  149. }
  150. void idSimpleWindow::DrawBorderAndCaption(const idRectangle &drawRect) {
  151. if (flags & WIN_BORDER) {
  152. if (borderSize) {
  153. dc->DrawRect(drawRect.x, drawRect.y, drawRect.w, drawRect.h, borderSize, borderColor);
  154. }
  155. }
  156. }
  157. void idSimpleWindow::CalcClientRect(float xofs, float yofs) {
  158. drawRect = rect;
  159. if ( flags & WIN_INVERTRECT ) {
  160. drawRect.x = rect.x() - rect.w();
  161. drawRect.y = rect.y() - rect.h();
  162. }
  163. drawRect.x += xofs;
  164. drawRect.y += yofs;
  165. clientRect = drawRect;
  166. if (rect.h() > 0.0 && rect.w() > 0.0) {
  167. if (flags & WIN_BORDER && borderSize != 0.0) {
  168. clientRect.x += borderSize;
  169. clientRect.y += borderSize;
  170. clientRect.w -= borderSize;
  171. clientRect.h -= borderSize;
  172. }
  173. textRect = clientRect;
  174. textRect.x += 2.0;
  175. textRect.w -= 2.0;
  176. textRect.y += 2.0;
  177. textRect.h -= 2.0;
  178. textRect.x += textAlignx;
  179. textRect.y += textAligny;
  180. }
  181. origin.Set( rect.x() + ( rect.w() / 2 ), rect.y() + ( rect.h() / 2 ) );
  182. }
  183. void idSimpleWindow::Redraw(float x, float y) {
  184. if (!visible) {
  185. return;
  186. }
  187. CalcClientRect(0, 0);
  188. dc->SetFont(fontNum);
  189. drawRect.Offset(x, y);
  190. clientRect.Offset(x, y);
  191. textRect.Offset(x, y);
  192. SetupTransforms(x, y);
  193. if ( flags & WIN_NOCLIP ) {
  194. dc->EnableClipping( false );
  195. }
  196. DrawBackground(drawRect);
  197. DrawBorderAndCaption(drawRect);
  198. if ( textShadow ) {
  199. idStr shadowText = text;
  200. idRectangle shadowRect = textRect;
  201. shadowText.RemoveColors();
  202. shadowRect.x += textShadow;
  203. shadowRect.y += textShadow;
  204. dc->DrawText( shadowText, textScale, textAlign, colorBlack, shadowRect, !( flags & WIN_NOWRAP ), -1 );
  205. }
  206. dc->DrawText(text, textScale, textAlign, foreColor, textRect, !( flags & WIN_NOWRAP ), -1);
  207. dc->SetTransformInfo(vec3_origin, mat3_identity);
  208. if ( flags & WIN_NOCLIP ) {
  209. dc->EnableClipping( true );
  210. }
  211. drawRect.Offset(-x, -y);
  212. clientRect.Offset(-x, -y);
  213. textRect.Offset(-x, -y);
  214. }
  215. int idSimpleWindow::GetWinVarOffset( idWinVar *wv, drawWin_t* owner) {
  216. int ret = -1;
  217. if ( wv == &rect ) {
  218. ret = (int)&( ( idSimpleWindow * ) 0 )->rect;
  219. }
  220. if ( wv == &backColor ) {
  221. ret = (int)&( ( idSimpleWindow * ) 0 )->backColor;
  222. }
  223. if ( wv == &matColor ) {
  224. ret = (int)&( ( idSimpleWindow * ) 0 )->matColor;
  225. }
  226. if ( wv == &foreColor ) {
  227. ret = (int)&( ( idSimpleWindow * ) 0 )->foreColor;
  228. }
  229. if ( wv == &borderColor ) {
  230. ret = (int)&( ( idSimpleWindow * ) 0 )->borderColor;
  231. }
  232. if ( wv == &textScale ) {
  233. ret = (int)&( ( idSimpleWindow * ) 0 )->textScale;
  234. }
  235. if ( wv == &rotate ) {
  236. ret = (int)&( ( idSimpleWindow * ) 0 )->rotate;
  237. }
  238. if ( ret != -1 ) {
  239. owner->simp = this;
  240. }
  241. return ret;
  242. }
  243. idWinVar *idSimpleWindow::GetWinVarByName(const char *_name) {
  244. idWinVar *retVar = NULL;
  245. if (idStr::Icmp(_name, "background") == 0) {
  246. retVar = &backGroundName;
  247. }
  248. if (idStr::Icmp(_name, "visible") == 0) {
  249. retVar = &visible;
  250. }
  251. if (idStr::Icmp(_name, "rect") == 0) {
  252. retVar = &rect;
  253. }
  254. if (idStr::Icmp(_name, "backColor") == 0) {
  255. retVar = &backColor;
  256. }
  257. if (idStr::Icmp(_name, "matColor") == 0) {
  258. retVar = &matColor;
  259. }
  260. if (idStr::Icmp(_name, "foreColor") == 0) {
  261. retVar = &foreColor;
  262. }
  263. if (idStr::Icmp(_name, "borderColor") == 0) {
  264. retVar = &borderColor;
  265. }
  266. if (idStr::Icmp(_name, "textScale") == 0) {
  267. retVar = &textScale;
  268. }
  269. if (idStr::Icmp(_name, "rotate") == 0) {
  270. retVar = &rotate;
  271. }
  272. if (idStr::Icmp(_name, "shear") == 0) {
  273. retVar = &shear;
  274. }
  275. if (idStr::Icmp(_name, "text") == 0) {
  276. retVar = &text;
  277. }
  278. return retVar;
  279. }
  280. /*
  281. ========================
  282. idSimpleWindow::WriteToSaveGame
  283. ========================
  284. */
  285. void idSimpleWindow::WriteToSaveGame( idFile *savefile ) {
  286. savefile->Write( &flags, sizeof( flags ) );
  287. savefile->Write( &drawRect, sizeof( drawRect ) );
  288. savefile->Write( &clientRect, sizeof( clientRect ) );
  289. savefile->Write( &textRect, sizeof( textRect ) );
  290. savefile->Write( &origin, sizeof( origin ) );
  291. savefile->Write( &fontNum, sizeof( fontNum ) );
  292. savefile->Write( &matScalex, sizeof( matScalex ) );
  293. savefile->Write( &matScaley, sizeof( matScaley ) );
  294. savefile->Write( &borderSize, sizeof( borderSize ) );
  295. savefile->Write( &textAlign, sizeof( textAlign ) );
  296. savefile->Write( &textAlignx, sizeof( textAlignx ) );
  297. savefile->Write( &textAligny, sizeof( textAligny ) );
  298. savefile->Write( &textShadow, sizeof( textShadow ) );
  299. text.WriteToSaveGame( savefile );
  300. visible.WriteToSaveGame( savefile );
  301. rect.WriteToSaveGame( savefile );
  302. backColor.WriteToSaveGame( savefile );
  303. matColor.WriteToSaveGame( savefile );
  304. foreColor.WriteToSaveGame( savefile );
  305. borderColor.WriteToSaveGame( savefile );
  306. textScale.WriteToSaveGame( savefile );
  307. rotate.WriteToSaveGame( savefile );
  308. shear.WriteToSaveGame( savefile );
  309. backGroundName.WriteToSaveGame( savefile );
  310. int stringLen;
  311. if ( background ) {
  312. stringLen = strlen( background->GetName() );
  313. savefile->Write( &stringLen, sizeof( stringLen ) );
  314. savefile->Write( background->GetName(), stringLen );
  315. } else {
  316. stringLen = 0;
  317. savefile->Write( &stringLen, sizeof( stringLen ) );
  318. }
  319. }
  320. /*
  321. ========================
  322. idSimpleWindow::ReadFromSaveGame
  323. ========================
  324. */
  325. void idSimpleWindow::ReadFromSaveGame( idFile *savefile ) {
  326. savefile->Read( &flags, sizeof( flags ) );
  327. savefile->Read( &drawRect, sizeof( drawRect ) );
  328. savefile->Read( &clientRect, sizeof( clientRect ) );
  329. savefile->Read( &textRect, sizeof( textRect ) );
  330. savefile->Read( &origin, sizeof( origin ) );
  331. savefile->Read( &fontNum, sizeof( fontNum ) );
  332. savefile->Read( &matScalex, sizeof( matScalex ) );
  333. savefile->Read( &matScaley, sizeof( matScaley ) );
  334. savefile->Read( &borderSize, sizeof( borderSize ) );
  335. savefile->Read( &textAlign, sizeof( textAlign ) );
  336. savefile->Read( &textAlignx, sizeof( textAlignx ) );
  337. savefile->Read( &textAligny, sizeof( textAligny ) );
  338. savefile->Read( &textShadow, sizeof( textShadow ) );
  339. text.ReadFromSaveGame( savefile );
  340. visible.ReadFromSaveGame( savefile );
  341. rect.ReadFromSaveGame( savefile );
  342. backColor.ReadFromSaveGame( savefile );
  343. matColor.ReadFromSaveGame( savefile );
  344. foreColor.ReadFromSaveGame( savefile );
  345. borderColor.ReadFromSaveGame( savefile );
  346. textScale.ReadFromSaveGame( savefile );
  347. rotate.ReadFromSaveGame( savefile );
  348. shear.ReadFromSaveGame( savefile );
  349. backGroundName.ReadFromSaveGame( savefile );
  350. int stringLen;
  351. savefile->Read( &stringLen, sizeof( stringLen ) );
  352. if ( stringLen > 0 ) {
  353. idStr backName;
  354. backName.Fill( ' ', stringLen );
  355. savefile->Read( &(backName)[0], stringLen );
  356. background = declManager->FindMaterial( backName );
  357. background->SetSort( SS_GUI );
  358. } else {
  359. background = NULL;
  360. }
  361. }
  362. /*
  363. ===============================
  364. */
  365. size_t idSimpleWindow::Size() {
  366. size_t sz = sizeof(*this);
  367. sz += name.Size();
  368. sz += text.Size();
  369. sz += backGroundName.Size();
  370. return sz;
  371. }