Winvar.h (18693B)
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 #ifndef __WINVAR_H__ 30 #define __WINVAR_H__ 31 32 #include "Rectangle.h" 33 34 static const char *VAR_GUIPREFIX = "gui::"; 35 static const int VAR_GUIPREFIX_LEN = strlen(VAR_GUIPREFIX); 36 37 class idWindow; 38 class idWinVar { 39 public: 40 idWinVar(); 41 virtual ~idWinVar(); 42 43 void SetGuiInfo(idDict *gd, const char *_name); 44 const char *GetName() const { 45 if (name) { 46 if (guiDict && *name == '*') { 47 return guiDict->GetString(&name[1]); 48 } 49 return name; 50 } 51 return ""; 52 } 53 void SetName(const char *_name) { 54 delete []name; 55 name = NULL; 56 if (_name) { 57 name = new (TAG_OLD_UI) char[strlen(_name)+1]; 58 strcpy(name, _name); 59 } 60 } 61 62 idWinVar &operator=( const idWinVar &other ) { 63 guiDict = other.guiDict; 64 SetName(other.name); 65 return *this; 66 } 67 68 idDict *GetDict() const { return guiDict; } 69 bool NeedsUpdate() { return (guiDict != NULL); } 70 71 virtual void Init(const char *_name, idWindow* win) = 0; 72 virtual void Set(const char *val) = 0; 73 virtual void Update() = 0; 74 virtual const char *c_str() const = 0; 75 virtual size_t Size() { size_t sz = (name) ? strlen(name) : 0; return sz + sizeof(*this); } 76 77 virtual void WriteToSaveGame( idFile *savefile ) = 0; 78 virtual void ReadFromSaveGame( idFile *savefile ) = 0; 79 80 virtual float x() const = 0; 81 82 void SetEval(bool b) { 83 eval = b; 84 } 85 bool GetEval() { 86 return eval; 87 } 88 89 protected: 90 idDict *guiDict; 91 char *name; 92 bool eval; 93 }; 94 95 class idWinBool : public idWinVar { 96 public: 97 idWinBool() : idWinVar() {}; 98 ~idWinBool() {}; 99 virtual void Init(const char *_name, idWindow *win) { idWinVar::Init(_name, win); 100 if (guiDict) { 101 data = guiDict->GetBool(GetName()); 102 } 103 } 104 int operator==( const bool &other ) { return (other == data); } 105 bool &operator=( const bool &other ) { 106 data = other; 107 if (guiDict) { 108 guiDict->SetBool(GetName(), data); 109 } 110 return data; 111 } 112 idWinBool &operator=( const idWinBool &other ) { 113 idWinVar::operator=(other); 114 data = other.data; 115 return *this; 116 } 117 118 operator bool() const { return data; } 119 120 virtual void Set(const char *val) { 121 data = ( atoi( val ) != 0 ); 122 if (guiDict) { 123 guiDict->SetBool(GetName(), data); 124 } 125 } 126 127 virtual void Update() { 128 const char *s = GetName(); 129 if ( guiDict && s[0] != '\0' ) { 130 data = guiDict->GetBool( s ); 131 } 132 } 133 134 virtual const char *c_str() const {return va("%i", data); } 135 136 // SaveGames 137 virtual void WriteToSaveGame( idFile *savefile ) { 138 savefile->Write( &eval, sizeof( eval ) ); 139 savefile->Write( &data, sizeof( data ) ); 140 } 141 virtual void ReadFromSaveGame( idFile *savefile ) { 142 savefile->Read( &eval, sizeof( eval ) ); 143 savefile->Read( &data, sizeof( data ) ); 144 } 145 146 virtual float x() const { return data ? 1.0f : 0.0f; }; 147 148 protected: 149 bool data; 150 }; 151 152 class idWinStr : public idWinVar { 153 public: 154 idWinStr() : idWinVar() {}; 155 ~idWinStr() {}; 156 virtual void Init(const char *_name, idWindow *win) { 157 idWinVar::Init(_name, win); 158 if (guiDict) { 159 const char * name = GetName(); 160 if ( name[0] == 0 ) { 161 data = ""; 162 } else { 163 data = guiDict->GetString( name ); 164 } 165 } 166 } 167 int operator==( const idStr &other ) const { 168 return (other == data); 169 } 170 int operator==( const char *other ) const { 171 return (data == other); 172 } 173 idStr &operator=( const idStr &other ) { 174 data = other; 175 if (guiDict) { 176 guiDict->Set(GetName(), data); 177 } 178 return data; 179 } 180 idWinStr &operator=( const idWinStr &other ) { 181 idWinVar::operator=(other); 182 data = other.data; 183 return *this; 184 } 185 operator const char *() const { 186 return data.c_str(); 187 } 188 operator const idStr &() const { 189 return data; 190 } 191 int LengthWithoutColors() { 192 if (guiDict && name && *name) { 193 data = guiDict->GetString(GetName()); 194 } 195 return data.LengthWithoutColors(); 196 } 197 int Length() { 198 if (guiDict && name && *name) { 199 data = guiDict->GetString(GetName()); 200 } 201 return data.Length(); 202 } 203 void RemoveColors() { 204 if (guiDict && name && *name) { 205 data = guiDict->GetString(GetName()); 206 } 207 data.RemoveColors(); 208 } 209 virtual const char *c_str() const { 210 return data.c_str(); 211 } 212 213 virtual void Set(const char *val) { 214 data = val; 215 if ( guiDict ) { 216 guiDict->Set(GetName(), data); 217 } 218 } 219 220 virtual void Update() { 221 const char *s = GetName(); 222 if ( guiDict && s[0] != '\0' ) { 223 data = guiDict->GetString( s ); 224 } 225 } 226 227 virtual size_t Size() { 228 size_t sz = idWinVar::Size(); 229 return sz +data.Allocated(); 230 } 231 232 // SaveGames 233 virtual void WriteToSaveGame( idFile *savefile ) { 234 savefile->Write( &eval, sizeof( eval ) ); 235 236 int len = data.Length(); 237 savefile->Write( &len, sizeof( len ) ); 238 if ( len > 0 ) { 239 savefile->Write( data.c_str(), len ); 240 } 241 } 242 virtual void ReadFromSaveGame( idFile *savefile ) { 243 savefile->Read( &eval, sizeof( eval ) ); 244 245 int len; 246 savefile->Read( &len, sizeof( len ) ); 247 if ( len > 0 ) { 248 data.Fill( ' ', len ); 249 savefile->Read( &data[0], len ); 250 } 251 } 252 253 // return wether string is emtpy 254 virtual float x() const { return data[0] ? 1.0f : 0.0f; }; 255 256 protected: 257 idStr data; 258 }; 259 260 class idWinInt : public idWinVar { 261 public: 262 idWinInt() : idWinVar() {}; 263 ~idWinInt() {}; 264 virtual void Init(const char *_name, idWindow *win) { 265 idWinVar::Init(_name, win); 266 if (guiDict) { 267 data = guiDict->GetInt(GetName()); 268 } 269 } 270 int &operator=( const int &other ) { 271 data = other; 272 if (guiDict) { 273 guiDict->SetInt(GetName(), data); 274 } 275 return data; 276 } 277 idWinInt &operator=( const idWinInt &other ) { 278 idWinVar::operator=(other); 279 data = other.data; 280 return *this; 281 } 282 operator int () const { 283 return data; 284 } 285 virtual void Set(const char *val) { 286 data = atoi(val);; 287 if (guiDict) { 288 guiDict->SetInt(GetName(), data); 289 } 290 } 291 292 virtual void Update() { 293 const char *s = GetName(); 294 if ( guiDict && s[0] != '\0' ) { 295 data = guiDict->GetInt( s ); 296 } 297 } 298 virtual const char *c_str() const { 299 return va("%i", data); 300 } 301 302 // SaveGames 303 virtual void WriteToSaveGame( idFile *savefile ) { 304 savefile->Write( &eval, sizeof( eval ) ); 305 savefile->Write( &data, sizeof( data ) ); 306 } 307 virtual void ReadFromSaveGame( idFile *savefile ) { 308 savefile->Read( &eval, sizeof( eval ) ); 309 savefile->Read( &data, sizeof( data ) ); 310 } 311 312 // no suitable conversion 313 virtual float x() const { assert( false ); return 0.0f; }; 314 315 protected: 316 int data; 317 }; 318 319 class idWinFloat : public idWinVar { 320 public: 321 idWinFloat() : idWinVar() {}; 322 ~idWinFloat() {}; 323 virtual void Init(const char *_name, idWindow *win) { 324 idWinVar::Init(_name, win); 325 if (guiDict) { 326 data = guiDict->GetFloat(GetName()); 327 } 328 } 329 idWinFloat &operator=( const idWinFloat &other ) { 330 idWinVar::operator=(other); 331 data = other.data; 332 return *this; 333 } 334 float &operator=( const float &other ) { 335 data = other; 336 if (guiDict) { 337 guiDict->SetFloat(GetName(), data); 338 } 339 return data; 340 } 341 operator float() const { 342 return data; 343 } 344 virtual void Set(const char *val) { 345 data = atof(val); 346 if (guiDict) { 347 guiDict->SetFloat(GetName(), data); 348 } 349 } 350 virtual void Update() { 351 const char *s = GetName(); 352 if ( guiDict && s[0] != '\0' ) { 353 data = guiDict->GetFloat( s ); 354 } 355 } 356 virtual const char *c_str() const { 357 return va("%f", data); 358 } 359 360 virtual void WriteToSaveGame( idFile *savefile ) { 361 savefile->Write( &eval, sizeof( eval ) ); 362 savefile->Write( &data, sizeof( data ) ); 363 } 364 virtual void ReadFromSaveGame( idFile *savefile ) { 365 savefile->Read( &eval, sizeof( eval ) ); 366 savefile->Read( &data, sizeof( data ) ); 367 } 368 369 virtual float x() const { return data; }; 370 protected: 371 float data; 372 }; 373 374 class idWinRectangle : public idWinVar { 375 public: 376 idWinRectangle() : idWinVar() {}; 377 ~idWinRectangle() {}; 378 virtual void Init(const char *_name, idWindow *win) { 379 idWinVar::Init(_name, win); 380 if (guiDict) { 381 idVec4 v = guiDict->GetVec4(GetName()); 382 data.x = v.x; 383 data.y = v.y; 384 data.w = v.z; 385 data.h = v.w; 386 } 387 } 388 389 int operator==( const idRectangle &other ) const { 390 return (other == data); 391 } 392 393 idWinRectangle &operator=( const idWinRectangle &other ) { 394 idWinVar::operator=(other); 395 data = other.data; 396 return *this; 397 } 398 idRectangle &operator=( const idVec4 &other ) { 399 data = other; 400 if (guiDict) { 401 guiDict->SetVec4(GetName(), other); 402 } 403 return data; 404 } 405 406 idRectangle &operator=( const idRectangle &other ) { 407 data = other; 408 if (guiDict) { 409 idVec4 v = data.ToVec4(); 410 guiDict->SetVec4(GetName(), v); 411 } 412 return data; 413 } 414 415 operator const idRectangle&() const { 416 return data; 417 } 418 419 float x() const { 420 return data.x; 421 } 422 float y() const { 423 return data.y; 424 } 425 float w() const { 426 return data.w; 427 } 428 float h() const { 429 return data.h; 430 } 431 float Right() const { 432 return data.Right(); 433 } 434 float Bottom() const { 435 return data.Bottom(); 436 } 437 idVec4 &ToVec4() { 438 static idVec4 ret; 439 ret = data.ToVec4(); 440 return ret; 441 } 442 virtual void Set(const char *val) { 443 if ( strchr ( val, ',' ) ) { 444 sscanf( val, "%f,%f,%f,%f", &data.x, &data.y, &data.w, &data.h ); 445 } else { 446 sscanf( val, "%f %f %f %f", &data.x, &data.y, &data.w, &data.h ); 447 } 448 if (guiDict) { 449 idVec4 v = data.ToVec4(); 450 guiDict->SetVec4(GetName(), v); 451 } 452 } 453 virtual void Update() { 454 const char *s = GetName(); 455 if ( guiDict && s[0] != '\0' ) { 456 idVec4 v = guiDict->GetVec4( s ); 457 data.x = v.x; 458 data.y = v.y; 459 data.w = v.z; 460 data.h = v.w; 461 } 462 } 463 464 virtual const char *c_str() const { 465 return data.ToVec4().ToString(); 466 } 467 468 virtual void WriteToSaveGame( idFile *savefile ) { 469 savefile->Write( &eval, sizeof( eval ) ); 470 savefile->Write( &data, sizeof( data ) ); 471 } 472 virtual void ReadFromSaveGame( idFile *savefile ) { 473 savefile->Read( &eval, sizeof( eval ) ); 474 savefile->Read( &data, sizeof( data ) ); 475 } 476 477 protected: 478 idRectangle data; 479 }; 480 481 class idWinVec2 : public idWinVar { 482 public: 483 idWinVec2() : idWinVar() {}; 484 ~idWinVec2() {}; 485 virtual void Init(const char *_name, idWindow *win) { 486 idWinVar::Init(_name, win); 487 if (guiDict) { 488 data = guiDict->GetVec2(GetName()); 489 } 490 } 491 int operator==( const idVec2 &other ) const { 492 return (other == data); 493 } 494 idWinVec2 &operator=( const idWinVec2 &other ) { 495 idWinVar::operator=(other); 496 data = other.data; 497 return *this; 498 } 499 500 idVec2 &operator=( const idVec2 &other ) { 501 data = other; 502 if (guiDict) { 503 guiDict->SetVec2(GetName(), data); 504 } 505 return data; 506 } 507 float x() const { 508 return data.x; 509 } 510 float y() const { 511 return data.y; 512 } 513 virtual void Set(const char *val) { 514 if ( strchr ( val, ',' ) ) { 515 sscanf( val, "%f,%f", &data.x, &data.y ); 516 } else { 517 sscanf( val, "%f %f", &data.x, &data.y); 518 } 519 if (guiDict) { 520 guiDict->SetVec2(GetName(), data); 521 } 522 } 523 operator const idVec2&() const { 524 return data; 525 } 526 virtual void Update() { 527 const char *s = GetName(); 528 if ( guiDict && s[0] != '\0' ) { 529 data = guiDict->GetVec2( s ); 530 } 531 } 532 virtual const char *c_str() const { 533 return data.ToString(); 534 } 535 void Zero() { 536 data.Zero(); 537 } 538 539 virtual void WriteToSaveGame( idFile *savefile ) { 540 savefile->Write( &eval, sizeof( eval ) ); 541 savefile->Write( &data, sizeof( data ) ); 542 } 543 virtual void ReadFromSaveGame( idFile *savefile ) { 544 savefile->Read( &eval, sizeof( eval ) ); 545 savefile->Read( &data, sizeof( data ) ); 546 } 547 548 protected: 549 idVec2 data; 550 }; 551 552 class idWinVec4 : public idWinVar { 553 public: 554 idWinVec4() : idWinVar() {}; 555 ~idWinVec4() {}; 556 virtual void Init(const char *_name, idWindow *win) { 557 idWinVar::Init(_name, win); 558 if (guiDict) { 559 data = guiDict->GetVec4(GetName()); 560 } 561 } 562 int operator==( const idVec4 &other ) const { 563 return (other == data); 564 } 565 idWinVec4 &operator=( const idWinVec4 &other ) { 566 idWinVar::operator=(other); 567 data = other.data; 568 return *this; 569 } 570 idVec4 &operator=( const idVec4 &other ) { 571 data = other; 572 if (guiDict) { 573 guiDict->SetVec4(GetName(), data); 574 } 575 return data; 576 } 577 operator const idVec4&() const { 578 return data; 579 } 580 581 float x() const { 582 return data.x; 583 } 584 585 float y() const { 586 return data.y; 587 } 588 589 float z() const { 590 return data.z; 591 } 592 593 float w() const { 594 return data.w; 595 } 596 virtual void Set(const char *val) { 597 if ( strchr ( val, ',' ) ) { 598 sscanf( val, "%f,%f,%f,%f", &data.x, &data.y, &data.z, &data.w ); 599 } else { 600 sscanf( val, "%f %f %f %f", &data.x, &data.y, &data.z, &data.w); 601 } 602 if ( guiDict ) { 603 guiDict->SetVec4( GetName(), data ); 604 } 605 } 606 virtual void Update() { 607 const char *s = GetName(); 608 if ( guiDict && s[0] != '\0' ) { 609 data = guiDict->GetVec4( s ); 610 } 611 } 612 virtual const char *c_str() const { 613 return data.ToString(); 614 } 615 616 void Zero() { 617 data.Zero(); 618 if ( guiDict ) { 619 guiDict->SetVec4(GetName(), data); 620 } 621 } 622 623 const idVec3 &ToVec3() const { 624 return data.ToVec3(); 625 } 626 627 virtual void WriteToSaveGame( idFile *savefile ) { 628 savefile->Write( &eval, sizeof( eval ) ); 629 savefile->Write( &data, sizeof( data ) ); 630 } 631 virtual void ReadFromSaveGame( idFile *savefile ) { 632 savefile->Read( &eval, sizeof( eval ) ); 633 savefile->Read( &data, sizeof( data ) ); 634 } 635 636 protected: 637 idVec4 data; 638 }; 639 640 class idWinVec3 : public idWinVar { 641 public: 642 idWinVec3() : idWinVar() {}; 643 ~idWinVec3() {}; 644 virtual void Init(const char *_name, idWindow *win) { 645 idWinVar::Init(_name, win); 646 if (guiDict) { 647 data = guiDict->GetVector(GetName()); 648 } 649 } 650 int operator==( const idVec3 &other ) const { 651 return (other == data); 652 } 653 idWinVec3 &operator=( const idWinVec3 &other ) { 654 idWinVar::operator=(other); 655 data = other.data; 656 return *this; 657 } 658 idVec3 &operator=( const idVec3 &other ) { 659 data = other; 660 if (guiDict) { 661 guiDict->SetVector(GetName(), data); 662 } 663 return data; 664 } 665 operator const idVec3&() const { 666 return data; 667 } 668 669 float x() const { 670 return data.x; 671 } 672 673 float y() const { 674 return data.y; 675 } 676 677 float z() const { 678 return data.z; 679 } 680 681 virtual void Set(const char *val) { 682 sscanf( val, "%f %f %f", &data.x, &data.y, &data.z); 683 if (guiDict) { 684 guiDict->SetVector(GetName(), data); 685 } 686 } 687 virtual void Update() { 688 const char *s = GetName(); 689 if ( guiDict && s[0] != '\0' ) { 690 data = guiDict->GetVector( s ); 691 } 692 } 693 virtual const char *c_str() const { 694 return data.ToString(); 695 } 696 697 void Zero() { 698 data.Zero(); 699 if (guiDict) { 700 guiDict->SetVector(GetName(), data); 701 } 702 } 703 704 virtual void WriteToSaveGame( idFile *savefile ) { 705 savefile->Write( &eval, sizeof( eval ) ); 706 savefile->Write( &data, sizeof( data ) ); 707 } 708 virtual void ReadFromSaveGame( idFile *savefile ) { 709 savefile->Read( &eval, sizeof( eval ) ); 710 savefile->Read( &data, sizeof( data ) ); 711 } 712 713 protected: 714 idVec3 data; 715 }; 716 717 class idWinBackground : public idWinStr { 718 public: 719 idWinBackground() : idWinStr() { 720 mat = NULL; 721 }; 722 ~idWinBackground() {}; 723 virtual void Init(const char *_name, idWindow *win) { 724 idWinStr::Init(_name, win); 725 if (guiDict) { 726 data = guiDict->GetString(GetName()); 727 } 728 } 729 int operator==( const idStr &other ) const { 730 return (other == data); 731 } 732 int operator==( const char *other ) const { 733 return (data == other); 734 } 735 idStr &operator=( const idStr &other ) { 736 data = other; 737 if (guiDict) { 738 guiDict->Set(GetName(), data); 739 } 740 if (mat) { 741 if ( data == "" ) { 742 (*mat) = NULL; 743 } else { 744 (*mat) = declManager->FindMaterial(data); 745 } 746 } 747 return data; 748 } 749 idWinBackground &operator=( const idWinBackground &other ) { 750 idWinVar::operator=(other); 751 data = other.data; 752 mat = other.mat; 753 if (mat) { 754 if ( data == "" ) { 755 (*mat) = NULL; 756 } else { 757 (*mat) = declManager->FindMaterial(data); 758 } 759 } 760 return *this; 761 } 762 operator const char *() const { 763 return data.c_str(); 764 } 765 operator const idStr &() const { 766 return data; 767 } 768 int Length() { 769 if (guiDict) { 770 data = guiDict->GetString(GetName()); 771 } 772 return data.Length(); 773 } 774 virtual const char *c_str() const { 775 return data.c_str(); 776 } 777 778 virtual void Set(const char *val) { 779 data = val; 780 if (guiDict) { 781 guiDict->Set(GetName(), data); 782 } 783 if (mat) { 784 if ( data == "" ) { 785 (*mat) = NULL; 786 } else { 787 (*mat) = declManager->FindMaterial(data); 788 } 789 } 790 } 791 792 virtual void Update() { 793 const char *s = GetName(); 794 if ( guiDict && s[0] != '\0' ) { 795 data = guiDict->GetString( s ); 796 if (mat) { 797 if ( data == "" ) { 798 (*mat) = NULL; 799 } else { 800 (*mat) = declManager->FindMaterial(data); 801 } 802 } 803 } 804 } 805 806 virtual size_t Size() { 807 size_t sz = idWinVar::Size(); 808 return sz +data.Allocated(); 809 } 810 811 void SetMaterialPtr( const idMaterial **m ) { 812 mat = m; 813 } 814 815 virtual void WriteToSaveGame( idFile *savefile ) { 816 savefile->Write( &eval, sizeof( eval ) ); 817 818 int len = data.Length(); 819 savefile->Write( &len, sizeof( len ) ); 820 if ( len > 0 ) { 821 savefile->Write( data.c_str(), len ); 822 } 823 } 824 virtual void ReadFromSaveGame( idFile *savefile ) { 825 savefile->Read( &eval, sizeof( eval ) ); 826 827 int len; 828 savefile->Read( &len, sizeof( len ) ); 829 if ( len > 0 ) { 830 data.Fill( ' ', len ); 831 savefile->Read( &data[0], len ); 832 } 833 if ( mat ) { 834 if ( len > 0 ) { 835 (*mat) = declManager->FindMaterial( data ); 836 } else { 837 (*mat) = NULL; 838 } 839 } 840 } 841 842 protected: 843 idStr data; 844 const idMaterial **mat; 845 }; 846 847 /* 848 ================ 849 idMultiWinVar 850 multiplexes access to a list if idWinVar* 851 ================ 852 */ 853 class idMultiWinVar : public idList< idWinVar * > { 854 public: 855 void Set( const char *val ); 856 void Update(); 857 void SetGuiInfo( idDict *dict ); 858 }; 859 860 #endif /* !__WINVAR_H__ */ 861