SimpleWindow.cpp (11863B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 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. 23 24 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. 25 26 =========================================================================== 27 */ 28 29 #pragma hdrstop 30 #include "../idlib/precompiled.h" 31 32 #include "DeviceContext.h" 33 #include "Window.h" 34 #include "UserInterfaceLocal.h" 35 #include "SimpleWindow.h" 36 37 38 idSimpleWindow::idSimpleWindow(idWindow *win) { 39 gui = win->GetGui(); 40 drawRect = win->drawRect; 41 clientRect = win->clientRect; 42 textRect = win->textRect; 43 origin = win->origin; 44 font = win->font; 45 name = win->name; 46 matScalex = win->matScalex; 47 matScaley = win->matScaley; 48 borderSize = win->borderSize; 49 textAlign = win->textAlign; 50 textAlignx = win->textAlignx; 51 textAligny = win->textAligny; 52 background = win->background; 53 flags = win->flags; 54 textShadow = win->textShadow; 55 56 visible = win->visible; 57 text = win->text; 58 rect = win->rect; 59 backColor = win->backColor; 60 matColor = win->matColor; 61 foreColor = win->foreColor; 62 borderColor = win->borderColor; 63 textScale = win->textScale; 64 rotate = win->rotate; 65 shear = win->shear; 66 backGroundName = win->backGroundName; 67 if (backGroundName.Length()) { 68 background = declManager->FindMaterial(backGroundName); 69 background->SetSort( SS_GUI ); 70 } 71 backGroundName.SetMaterialPtr(&background); 72 73 // 74 // added parent 75 mParent = win->GetParent(); 76 // 77 78 hideCursor = win->hideCursor; 79 80 idWindow *parent = win->GetParent(); 81 if (parent) { 82 if (text.NeedsUpdate()) { 83 parent->AddUpdateVar(&text); 84 } 85 if (visible.NeedsUpdate()) { 86 parent->AddUpdateVar(&visible); 87 } 88 if (rect.NeedsUpdate()) { 89 parent->AddUpdateVar(&rect); 90 } 91 if (backColor.NeedsUpdate()) { 92 parent->AddUpdateVar(&backColor); 93 } 94 if (matColor.NeedsUpdate()) { 95 parent->AddUpdateVar(&matColor); 96 } 97 if (foreColor.NeedsUpdate()) { 98 parent->AddUpdateVar(&foreColor); 99 } 100 if (borderColor.NeedsUpdate()) { 101 parent->AddUpdateVar(&borderColor); 102 } 103 if (textScale.NeedsUpdate()) { 104 parent->AddUpdateVar(&textScale); 105 } 106 if (rotate.NeedsUpdate()) { 107 parent->AddUpdateVar(&rotate); 108 } 109 if (shear.NeedsUpdate()) { 110 parent->AddUpdateVar(&shear); 111 } 112 if (backGroundName.NeedsUpdate()) { 113 parent->AddUpdateVar(&backGroundName); 114 } 115 } 116 } 117 118 idSimpleWindow::~idSimpleWindow() { 119 120 } 121 122 void idSimpleWindow::StateChanged( bool redraw ) { 123 } 124 125 void idSimpleWindow::SetupTransforms(float x, float y) { 126 static idMat3 trans; 127 static idVec3 org; 128 129 trans.Identity(); 130 org.Set( origin.x + x, origin.y + y, 0 ); 131 if ( rotate ) { 132 static idRotation rot; 133 static idVec3 vec( 0, 0, 1 ); 134 rot.Set( org, vec, rotate ); 135 trans = rot.ToMat3(); 136 } 137 138 static idMat3 smat; 139 smat.Identity(); 140 if (shear.x() || shear.y()) { 141 smat[0][1] = shear.x(); 142 smat[1][0] = shear.y(); 143 trans *= smat; 144 } 145 146 if ( !trans.IsIdentity() ) { 147 dc->SetTransformInfo( org, trans ); 148 } 149 } 150 151 void idSimpleWindow::DrawBackground(const idRectangle &drawRect) { 152 if (backColor.w() > 0) { 153 dc->DrawFilledRect(drawRect.x, drawRect.y, drawRect.w, drawRect.h, backColor); 154 } 155 156 if (background) { 157 if (matColor.w() > 0) { 158 float scalex, scaley; 159 if ( flags & WIN_NATURALMAT ) { 160 scalex = drawRect.w / background->GetImageWidth(); 161 scaley = drawRect.h / background->GetImageHeight(); 162 } else { 163 scalex = matScalex; 164 scaley = matScaley; 165 } 166 dc->DrawMaterial(drawRect.x, drawRect.y, drawRect.w, drawRect.h, background, matColor, scalex, scaley); 167 } 168 } 169 } 170 171 void idSimpleWindow::DrawBorderAndCaption(const idRectangle &drawRect) { 172 if (flags & WIN_BORDER) { 173 if (borderSize) { 174 dc->DrawRect(drawRect.x, drawRect.y, drawRect.w, drawRect.h, borderSize, borderColor); 175 } 176 } 177 } 178 179 void idSimpleWindow::CalcClientRect(float xofs, float yofs) { 180 181 drawRect = rect; 182 183 if ( flags & WIN_INVERTRECT ) { 184 drawRect.x = rect.x() - rect.w(); 185 drawRect.y = rect.y() - rect.h(); 186 } 187 188 drawRect.x += xofs; 189 drawRect.y += yofs; 190 191 clientRect = drawRect; 192 if (rect.h() > 0.0 && rect.w() > 0.0) { 193 194 if (flags & WIN_BORDER && borderSize != 0.0) { 195 clientRect.x += borderSize; 196 clientRect.y += borderSize; 197 clientRect.w -= borderSize; 198 clientRect.h -= borderSize; 199 } 200 201 textRect = clientRect; 202 textRect.x += 2.0; 203 textRect.w -= 2.0; 204 textRect.y += 2.0; 205 textRect.h -= 2.0; 206 textRect.x += textAlignx; 207 textRect.y += textAligny; 208 209 } 210 origin.Set( rect.x() + ( rect.w() / 2 ), rect.y() + ( rect.h() / 2 ) ); 211 212 } 213 214 215 void idSimpleWindow::Redraw(float x, float y) { 216 217 if (!visible) { 218 return; 219 } 220 221 CalcClientRect(0, 0); 222 dc->SetFont( font ); 223 drawRect.Offset(x, y); 224 clientRect.Offset(x, y); 225 textRect.Offset(x, y); 226 SetupTransforms(x, y); 227 if ( flags & WIN_NOCLIP ) { 228 dc->EnableClipping( false ); 229 } 230 DrawBackground(drawRect); 231 DrawBorderAndCaption(drawRect); 232 if ( textShadow ) { 233 idStr shadowText = text; 234 idRectangle shadowRect = textRect; 235 236 shadowText.RemoveColors(); 237 shadowRect.x += textShadow; 238 shadowRect.y += textShadow; 239 240 dc->DrawText( shadowText, textScale, textAlign, colorBlack, shadowRect, !( flags & WIN_NOWRAP ), -1 ); 241 } 242 dc->DrawText(text, textScale, textAlign, foreColor, textRect, !( flags & WIN_NOWRAP ), -1); 243 dc->SetTransformInfo(vec3_origin, mat3_identity); 244 if ( flags & WIN_NOCLIP ) { 245 dc->EnableClipping( true ); 246 } 247 drawRect.Offset(-x, -y); 248 clientRect.Offset(-x, -y); 249 textRect.Offset(-x, -y); 250 } 251 252 int idSimpleWindow::GetWinVarOffset( idWinVar *wv, drawWin_t* owner) { 253 int ret = -1; 254 255 if ( wv == &rect ) { 256 ret = (int)&( ( idSimpleWindow * ) 0 )->rect; 257 } 258 259 if ( wv == &backColor ) { 260 ret = (int)&( ( idSimpleWindow * ) 0 )->backColor; 261 } 262 263 if ( wv == &matColor ) { 264 ret = (int)&( ( idSimpleWindow * ) 0 )->matColor; 265 } 266 267 if ( wv == &foreColor ) { 268 ret = (int)&( ( idSimpleWindow * ) 0 )->foreColor; 269 } 270 271 if ( wv == &borderColor ) { 272 ret = (int)&( ( idSimpleWindow * ) 0 )->borderColor; 273 } 274 275 if ( wv == &textScale ) { 276 ret = (int)&( ( idSimpleWindow * ) 0 )->textScale; 277 } 278 279 if ( wv == &rotate ) { 280 ret = (int)&( ( idSimpleWindow * ) 0 )->rotate; 281 } 282 283 if ( ret != -1 ) { 284 owner->simp = this; 285 } 286 return ret; 287 } 288 289 idWinVar *idSimpleWindow::GetWinVarByName(const char *_name) { 290 idWinVar *retVar = NULL; 291 if (idStr::Icmp(_name, "background") == 0) { 292 retVar = &backGroundName; 293 } 294 if (idStr::Icmp(_name, "visible") == 0) { 295 retVar = &visible; 296 } 297 if (idStr::Icmp(_name, "rect") == 0) { 298 retVar = ▭ 299 } 300 if (idStr::Icmp(_name, "backColor") == 0) { 301 retVar = &backColor; 302 } 303 if (idStr::Icmp(_name, "matColor") == 0) { 304 retVar = &matColor; 305 } 306 if (idStr::Icmp(_name, "foreColor") == 0) { 307 retVar = &foreColor; 308 } 309 if (idStr::Icmp(_name, "borderColor") == 0) { 310 retVar = &borderColor; 311 } 312 if (idStr::Icmp(_name, "textScale") == 0) { 313 retVar = &textScale; 314 } 315 if (idStr::Icmp(_name, "rotate") == 0) { 316 retVar = &rotate; 317 } 318 if (idStr::Icmp(_name, "shear") == 0) { 319 retVar = &shear; 320 } 321 if (idStr::Icmp(_name, "text") == 0) { 322 retVar = &text; 323 } 324 return retVar; 325 } 326 327 /* 328 ======================== 329 idSimpleWindow::WriteToSaveGame 330 ======================== 331 */ 332 void idSimpleWindow::WriteToSaveGame( idFile *savefile ) { 333 334 savefile->Write( &flags, sizeof( flags ) ); 335 savefile->Write( &drawRect, sizeof( drawRect ) ); 336 savefile->Write( &clientRect, sizeof( clientRect ) ); 337 savefile->Write( &textRect, sizeof( textRect ) ); 338 savefile->Write( &origin, sizeof( origin ) ); 339 savefile->Write( &matScalex, sizeof( matScalex ) ); 340 savefile->Write( &matScaley, sizeof( matScaley ) ); 341 savefile->Write( &borderSize, sizeof( borderSize ) ); 342 savefile->Write( &textAlign, sizeof( textAlign ) ); 343 savefile->Write( &textAlignx, sizeof( textAlignx ) ); 344 savefile->Write( &textAligny, sizeof( textAligny ) ); 345 savefile->Write( &textShadow, sizeof( textShadow ) ); 346 savefile->WriteString( font->GetName() ); 347 348 text.WriteToSaveGame( savefile ); 349 visible.WriteToSaveGame( savefile ); 350 rect.WriteToSaveGame( savefile ); 351 backColor.WriteToSaveGame( savefile ); 352 matColor.WriteToSaveGame( savefile ); 353 foreColor.WriteToSaveGame( savefile ); 354 borderColor.WriteToSaveGame( savefile ); 355 textScale.WriteToSaveGame( savefile ); 356 rotate.WriteToSaveGame( savefile ); 357 shear.WriteToSaveGame( savefile ); 358 backGroundName.WriteToSaveGame( savefile ); 359 360 int stringLen; 361 362 if ( background ) { 363 stringLen = strlen( background->GetName() ); 364 savefile->Write( &stringLen, sizeof( stringLen ) ); 365 savefile->Write( background->GetName(), stringLen ); 366 } else { 367 stringLen = 0; 368 savefile->Write( &stringLen, sizeof( stringLen ) ); 369 } 370 371 } 372 373 /* 374 ======================== 375 idSimpleWindow::ReadFromSaveGame 376 ======================== 377 */ 378 void idSimpleWindow::ReadFromSaveGame( idFile *savefile ) { 379 380 savefile->Read( &flags, sizeof( flags ) ); 381 savefile->Read( &drawRect, sizeof( drawRect ) ); 382 savefile->Read( &clientRect, sizeof( clientRect ) ); 383 savefile->Read( &textRect, sizeof( textRect ) ); 384 savefile->Read( &origin, sizeof( origin ) ); 385 /* if ( savefile->GetFileVersion() < BUILD_NUMBER_8TH_ANNIVERSARY_1 ) { 386 int fontNum; 387 savefile->Read( &fontNum, sizeof( fontNum ) ); 388 font = renderSystem->RegisterFont( "" ); 389 } */ 390 savefile->Read( &matScalex, sizeof( matScalex ) ); 391 savefile->Read( &matScaley, sizeof( matScaley ) ); 392 savefile->Read( &borderSize, sizeof( borderSize ) ); 393 savefile->Read( &textAlign, sizeof( textAlign ) ); 394 savefile->Read( &textAlignx, sizeof( textAlignx ) ); 395 savefile->Read( &textAligny, sizeof( textAligny ) ); 396 savefile->Read( &textShadow, sizeof( textShadow ) ); 397 // if ( savefile->GetFileVersion() >= BUILD_NUMBER_8TH_ANNIVERSARY_1 ) { 398 idStr fontName; 399 savefile->ReadString( fontName ); 400 font = renderSystem->RegisterFont( fontName ); 401 // } 402 403 text.ReadFromSaveGame( savefile ); 404 visible.ReadFromSaveGame( savefile ); 405 rect.ReadFromSaveGame( savefile ); 406 backColor.ReadFromSaveGame( savefile ); 407 matColor.ReadFromSaveGame( savefile ); 408 foreColor.ReadFromSaveGame( savefile ); 409 borderColor.ReadFromSaveGame( savefile ); 410 textScale.ReadFromSaveGame( savefile ); 411 rotate.ReadFromSaveGame( savefile ); 412 shear.ReadFromSaveGame( savefile ); 413 backGroundName.ReadFromSaveGame( savefile ); 414 415 int stringLen; 416 417 savefile->Read( &stringLen, sizeof( stringLen ) ); 418 if ( stringLen > 0 ) { 419 idStr backName; 420 421 backName.Fill( ' ', stringLen ); 422 savefile->Read( &(backName)[0], stringLen ); 423 424 background = declManager->FindMaterial( backName ); 425 background->SetSort( SS_GUI ); 426 } else { 427 background = NULL; 428 } 429 430 } 431 432 433 /* 434 =============================== 435 */ 436 437 size_t idSimpleWindow::Size() { 438 size_t sz = sizeof(*this); 439 sz += name.Size(); 440 sz += text.Size(); 441 sz += backGroundName.Size(); 442 return sz; 443 }