1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
|
/*
===========================================================================
Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
Doom 3 Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
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.
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.
===========================================================================
*/
#include "tools/edit_gui_common.h"
#include "MaterialDoc.h"
#include "MaterialView.h"
/**
* Constructor for MaterialDoc.
*/
MaterialDoc::MaterialDoc(void) {
modified = false;
applyWaiting = false;
sourceModify = false;
}
/**
* Destructor for MaterialDoc.
*/
MaterialDoc::~MaterialDoc(void) {
ClearEditMaterial();
}
/**
* Initializes the MaterialDoc instance with a specific idMaterial. This method will
* parse the material into the internal dictionary representation and optionally
* allow the idMaterial object to reparse the source.
* @param material The idMaterial instance to use.
* @param parseMaterial Flag to determine if the material should be parsed into the editor representation.
* @param parseRenderMaterial Flag to determine if the idMaterial object should be reparsed.
*/
void MaterialDoc::SetRenderMaterial(idMaterial* material, bool parseMaterial, bool parseRenderMatierial) {
renderMaterial = material;
if(!parseMaterial || !renderMaterial)
return;
if(parseRenderMatierial) {
char *declText = (char *) _alloca( material->GetTextLength() + 1 );
material->GetText( declText );
renderMaterial->GetText(declText);
ParseMaterialText(declText);
}
ClearEditMaterial();
name = material->GetName();
idLexer src;
char *declText = (char *) _alloca( material->GetTextLength() + 1 );
material->GetText( declText );
renderMaterial->GetText(declText);
src.LoadMemory(declText, strlen(declText), "Material");
ParseMaterial(&src);
}
/**
* Returns the number of stages in this material.
*/
int MaterialDoc::GetStageCount() {
return editMaterial.stages.Num();
}
/**
* Returns the index of the stage with the specified type and name or -1
* if the stage does not exist.
* @param stageType The type of stage to find.
* @param name The name of the stage to find.
*/
int MaterialDoc::FindStage(int stageType, const char* name) {
for(int i = 0; i < editMaterial.stages.Num(); i++) {
int type = GetAttributeInt(i, "stagetype");
idStr localname = GetAttribute(i, "name");
if(stageType == type && !localname.Icmp(name))
return i;
}
return -1;
}
/**
* Returns a copy of the specified stage.
* @param stage The stage to return.
*/
MEStage_t MaterialDoc::GetStage(int stage) {
assert(stage >= 0 && stage < GetStageCount());
return *editMaterial.stages[stage];
}
/**
* Specifies the enabled state of a single stage.
* @param stage The stage to change.
* @param enabled The enabled state.
*/
void MaterialDoc::EnableStage(int stage, bool enabled) {
assert(stage >= 0 && stage < GetStageCount());
editMaterial.stages[stage]->enabled = enabled;
OnMaterialChanged();
}
/**
* Sets the enabled state of all stages.
* @param enabled The enabled state.
*/
void MaterialDoc::EnableAllStages(bool enabled) {
for(int i = 0; i < GetStageCount(); i++) {
editMaterial.stages[i]->enabled = enabled;
}
}
/**
* Returns the enabled state of a stage.
* @param stage The stage to check.
*/
bool MaterialDoc::IsStageEnabled(int stage) {
assert(stage >= 0 && stage < GetStageCount());
return editMaterial.stages[stage]->enabled;
}
/**
* Returns an attribute string from the material or a stage.
* @param stage The stage or -1 for the material.
* @param attribName The name of the attribute.
* @param defaultString The default value if the attribute is not specified.
*/
const char* MaterialDoc::GetAttribute(int stage, const char* attribName, const char* defaultString) {
if(stage == -1) {
return editMaterial.materialData.GetString(attribName, defaultString);
} else {
assert(stage >= 0 && stage < GetStageCount());
MEStage_t* pStage = editMaterial.stages[stage];
return pStage->stageData.GetString(attribName, defaultString);
}
}
/**
* Returns an attribute int from the material or a stage.
* @param stage The stage or -1 for the material.
* @param attribName The name of the attribute.
* @param defaultString The default value if the attribute is not specified.
*/
int MaterialDoc::GetAttributeInt(int stage, const char* attribName, const char* defaultString) {
if(stage == -1) {
return editMaterial.materialData.GetInt(attribName, defaultString);
} else {
assert(stage >= 0 && stage < GetStageCount());
MEStage_t* pStage = editMaterial.stages[stage];
return pStage->stageData.GetInt(attribName, defaultString);
}
}
/**
* Returns an attribute float from the material or a stage.
* @param stage The stage or -1 for the material.
* @param attribName The name of the attribute.
* @param defaultString The default value if the attribute is not specified.
*/
float MaterialDoc::GetAttributeFloat(int stage, const char* attribName, const char* defaultString) {
if(stage == -1) {
return editMaterial.materialData.GetFloat(attribName, defaultString);
} else {
assert(stage >= 0 && stage < GetStageCount());
MEStage_t* pStage = editMaterial.stages[stage];
return pStage->stageData.GetFloat(attribName, defaultString);
}
}
/**
* Returns an attribute bool from the material or a stage.
* @param stage The stage or -1 for the material.
* @param attribName The name of the attribute.
* @param defaultString The default value if the attribute is not specified.
*/
bool MaterialDoc::GetAttributeBool(int stage, const char* attribName, const char* defaultString) {
if(stage == -1) {
return editMaterial.materialData.GetBool(attribName, defaultString);
} else {
assert(stage >= 0 && stage < GetStageCount());
MEStage_t* pStage = editMaterial.stages[stage];
return pStage->stageData.GetBool(attribName, defaultString);
}
}
/**
* Sets an attribute string in the material or a stage.
* @param stage The stage or -1 for the material.
* @param attribName The name of the attribute.
* @param value The value to set.
* @param addUndo Flag that specifies if the system should add an undo operation.
*/
void MaterialDoc::SetAttribute(int stage, const char* attribName, const char* value, bool addUndo) {
//Make sure we need to set the attribute
idStr orig = GetAttribute(stage, attribName);
if(orig.Icmp(value)) {
idDict* dict;
if(stage == -1) {
dict = &editMaterial.materialData;
} else {
assert(stage >= 0 && stage < GetStageCount());
dict = &editMaterial.stages[stage]->stageData;
}
if(addUndo) {
//Create a new Modifier for this change so we can undo and redo later
AttributeMaterialModifierString* mod = new AttributeMaterialModifierString(manager, name, stage, attribName, value, orig);
manager->AddMaterialUndoModifier(mod);
}
dict->Set(attribName, value);
manager->AttributeChanged(this, stage, attribName);
OnMaterialChanged();
}
}
/**
* Sets an attribute int in the material or a stage.
* @param stage The stage or -1 for the material.
* @param attribName The name of the attribute.
* @param value The value to set.
* @param addUndo Flag that specifies if the system should add an undo operation.
*/
void MaterialDoc::SetAttributeInt(int stage, const char* attribName, int value, bool addUndo) {
//Make sure we need to set the attribute
int orig = GetAttributeInt(stage, attribName);
if(orig != value) {
idDict* dict;
if(stage == -1) {
dict = &editMaterial.materialData;
} else {
assert(stage >= 0 && stage < GetStageCount());
dict = &editMaterial.stages[stage]->stageData;
}
dict->SetInt(attribName, value);
manager->AttributeChanged(this, stage, attribName);
OnMaterialChanged();
}
}
/**
* Sets an attribute float in the material or a stage.
* @param stage The stage or -1 for the material.
* @param attribName The name of the attribute.
* @param value The value to set.
* @param addUndo Flag that specifies if the system should add an undo operation.
*/
void MaterialDoc::SetAttributeFloat(int stage, const char* attribName, float value, bool addUndo) {
//Make sure we need to set the attribute
float orig = GetAttributeFloat(stage, attribName);
if(orig != value) {
idDict* dict;
if(stage == -1) {
dict = &editMaterial.materialData;
} else {
assert(stage >= 0 && stage < GetStageCount());
dict = &editMaterial.stages[stage]->stageData;
}
dict->SetFloat(attribName, value);
manager->AttributeChanged(this, stage, attribName);
OnMaterialChanged();
}
}
/**
* Sets an attribute bool in the material or a stage.
* @param stage The stage or -1 for the material.
* @param attribName The name of the attribute.
* @param value The value to set.
* @param addUndo Flag that specifies if the system should add an undo operation.
*/
void MaterialDoc::SetAttributeBool(int stage, const char* attribName, bool value, bool addUndo) {
//Make sure we need to set the attribute
bool orig = GetAttributeBool(stage, attribName);
if(orig != value) {
idDict* dict;
if(stage == -1) {
dict = &editMaterial.materialData;
} else {
assert(stage >= 0 && stage < GetStageCount());
dict = &editMaterial.stages[stage]->stageData;
}
if(addUndo) {
//Create a new Modifier for this change so we can undo and redo later
AttributeMaterialModifierBool* mod = new AttributeMaterialModifierBool(manager, name, stage, attribName, value, orig);
manager->AddMaterialUndoModifier(mod);
}
dict->SetBool(attribName, value);
manager->AttributeChanged(this, stage, attribName);
OnMaterialChanged();
}
}
/**
* Sets the material name.
* @param materialName The new name of the material.
* @param addUndo Flag that specifies if the system should add an undo operation.
*/
void MaterialDoc::SetMaterialName(const char* materialName, bool addUndo) {
idStr oldName = name;
declManager->RenameDecl(DECL_MATERIAL, oldName, materialName);
name = renderMaterial->GetName();
if(addUndo) {
RenameMaterialModifier* mod = new RenameMaterialModifier(manager, name, oldName);
manager->AddMaterialUndoModifier(mod);
}
manager->MaterialNameChanged(oldName, this);
OnMaterialChanged();
//Need to do an instant apply for material name changes
ApplyMaterialChanges();
}
/**
* Sets the entire dictionary for a material or stage
* @param stage The stage or -1 for the material.
* @param data The dictionary to copy.
*/
void MaterialDoc::SetData(int stage, idDict* data) {
idDict* dict;
if(stage == -1) {
dict = &editMaterial.materialData;
} else {
assert(stage >= 0 && stage < GetStageCount());
dict = &editMaterial.stages[stage]->stageData;
}
dict->Clear();
dict->Copy(*data);
}
/**
* Called when the editor modifies the source of the material.
* @param text The new source text.
*/
void MaterialDoc::SourceModify(SourceModifyOwner* owner) {
sourceModifyOwner = owner;
sourceModify = true;
OnMaterialChanged();
}
/**
* Returns true if the source text of this material has been edited.
*/
bool MaterialDoc::IsSourceModified() {
return sourceModify;
}
/**
* Applies any source changes to the edit representation of the material.
*/
void MaterialDoc::ApplySourceModify(idStr& text) {
if(sourceModify) {
//Changes in the source need to clear any undo redo buffer because we have no idea what has changed
manager->ClearUndo();
manager->ClearRedo();
ClearEditMaterial();
idLexer src;
src.LoadMemory(text, text.Length(), "Material");
src.SetFlags(
LEXFL_NOSTRINGCONCAT | // multiple strings seperated by whitespaces are not concatenated
LEXFL_NOSTRINGESCAPECHARS | // no escape characters inside strings
LEXFL_ALLOWPATHNAMES | // allow path seperators in names
LEXFL_ALLOWMULTICHARLITERALS | // allow multi character literals
LEXFL_ALLOWBACKSLASHSTRINGCONCAT | // allow multiple strings seperated by '\' to be concatenated
LEXFL_NOFATALERRORS // just set a flag instead of fatal erroring
);
idToken token;
if(!src.ReadToken(&token)) {
src.Warning( "Missing decl name" );
return;
}
ParseMaterial(&src);
sourceModify = false;
//Check to see if the name has changed
if(token.Icmp(name)) {
SetMaterialName(token, false);
}
}
}
/**
* Returns the appropriate source for the editing
*/
const char* MaterialDoc::GetEditSourceText() {
return GenerateSourceText();
}
/**
* Adds a stage to the material.
* @param stageType The type of the stage: normal or special.
* @param stageName The name of the stage.
* @param addUndo Flag that specifies if the system should add an undo operation.
*/
void MaterialDoc::AddStage(int stageType, const char* stageName, bool addUndo) {
MEStage_t* newStage = new MEStage_t();
int index = editMaterial.stages.Append(newStage);
newStage->stageData.Set("name", stageName);
newStage->stageData.SetInt("stagetype", stageType);
newStage->enabled = true;
if(addUndo) {
StageInsertModifier* mod = new StageInsertModifier(manager, name, index, stageType, stageName);
manager->AddMaterialUndoModifier(mod);
}
manager->StageAdded(this, index);
OnMaterialChanged();
}
/**
* Inserts a new stage to the material at a specified location.
* @param stage The location to insert the stage.
* @param stageType The type of the stage: normal or special.
* @param stageName The name of the stage.
* @param addUndo Flag that specifies if the system should add an undo operation.
*/
void MaterialDoc::InsertStage(int stage, int stageType, const char* stageName, bool addUndo) {
MEStage_t* newStage = new MEStage_t();
editMaterial.stages.Insert(newStage, stage);
newStage->stageData.Set("name", stageName);
newStage->stageData.SetInt("stagetype", stageType);
newStage->enabled = true;
if(addUndo) {
StageInsertModifier* mod = new StageInsertModifier(manager, name, stage, stageType, stageName);
manager->AddMaterialUndoModifier(mod);
}
manager->StageAdded(this, stage);
OnMaterialChanged();
}
/**
* Removes a stage from the material.
* @param stage The stage to remove.
* @param addUndo Flag that specifies if the system should add an undo operation.
*/
void MaterialDoc::RemoveStage(int stage, bool addUndo) {
assert(stage >= 0 && stage < GetStageCount());
if(addUndo) {
//Add modifier to undo this operation
StageDeleteModifier* mod = new StageDeleteModifier(manager, name, stage, editMaterial.stages[stage]->stageData);
manager->AddMaterialUndoModifier(mod);
}
//delete the stage and remove it from the list
delete editMaterial.stages[stage];
editMaterial.stages.RemoveIndex(stage);
manager->StageDeleted(this, stage);
OnMaterialChanged();
}
/**
* Removes all stages from the material.
*/
void MaterialDoc::ClearStages() {
//Delete each stage and clear the list
for(int i = GetStageCount() - 1; i >= 0; i--) {
RemoveStage(i);
}
}
/**
* Moves a stage from one location to another.
* @param from The original location of the stage.
* @param to The new location of the stage.
* @param addUndo Flag that specifies if the system should add an undo operation.
*/
void MaterialDoc::MoveStage(int from, int to, bool addUndo) {
assert(from >= 0 && from < GetStageCount());
assert(to >= 0 && to < GetStageCount());
int origFrom = from;
int origTo = to;
if(from < to)
to++;
MEStage_t* pMove = editMaterial.stages[from];
editMaterial.stages.Insert(pMove, to);
if(from > to)
from++;
editMaterial.stages.RemoveIndex(from);
manager->StageMoved(this, origFrom, origTo);
if(addUndo) {
StageMoveModifier *mod = new StageMoveModifier(manager, name, origFrom, origTo);
manager->AddMaterialUndoModifier(mod);
}
OnMaterialChanged();
}
/**
* Applies any changes to the material
* @param force If true then the material will be applied regardless of the number of changes.
*/
void MaterialDoc::ApplyMaterialChanges(bool force) {
if(force || applyWaiting) {
if(sourceModify && sourceModifyOwner) {
idStr text = sourceModifyOwner->GetSourceText();
ApplySourceModify(text);
}
ReplaceSourceText();
char *declText = (char *) _alloca( renderMaterial->GetTextLength() + 1 );
renderMaterial->GetText( declText );
renderMaterial->GetText(declText);
ParseMaterialText(declText);
applyWaiting = false;
assert(manager);
manager->MaterialApplied(this);
}
}
/**
* Saves the material.
*/
void MaterialDoc::Save() {
EnableAllStages(true);
//Apply the material so that the renderMaterial has the source text
if(!deleted) {
ApplyMaterialChanges(true);
} else {
//Replace the text with nothing
renderMaterial->SetText(" ");
}
if(renderMaterial->Save()) {
modified = false;
//Notify the world
assert(manager);
manager->MaterialSaved(this);
} else {
MessageBox(GetMaterialEditorWindow(), va("Unable to save '%s'. It may be read-only", name.c_str()), "Save Error", MB_OK | MB_ICONERROR);
}
}
/**
* Deletes the material.
*/
void MaterialDoc::Delete() {
deleted = true;
OnMaterialChanged();
}
/**
* Sets the proper internal states and notifies the MaterialDocManager once a material has been changed.
*/
void MaterialDoc::OnMaterialChanged() {
modified = true;
applyWaiting = true;
assert(manager);
manager->MaterialChanged(this);
}
/**
* Passes text to a render material for parsing.
* @param source The text that sould be applied to the idMaterial.
*/
void MaterialDoc::ParseMaterialText(const char* source) {
/*idLexer src;
src.LoadMemory(source, strlen(source), "material");
src.SetFlags(
LEXFL_NOSTRINGCONCAT | // multiple strings seperated by whitespaces are not concatenated
LEXFL_NOSTRINGESCAPECHARS | // no escape characters inside strings
LEXFL_ALLOWPATHNAMES | // allow path seperators in names
LEXFL_ALLOWMULTICHARLITERALS | // allow multi character literals
LEXFL_ALLOWBACKSLASHSTRINGCONCAT | // allow multiple strings seperated by '\' to be concatenated
LEXFL_NOFATALERRORS // just set a flag instead of fatal erroring
);
//Skip the name becuase the material parsing code expects it
src.SkipUntilString("{");*/
//Now let the material parse the text
renderMaterial->Parse(source, strlen(source));
}
/**
* Parses the source text from an idMaterial and initializes the editor dictionary representation
* of the material.
* @param src The idLexer object that contains the material text.
*/
void MaterialDoc::ParseMaterial(idLexer* src) {
idToken token;
//Parse past the name
src->SkipUntilString("{");
while ( 1 ) {
if ( !src->ExpectAnyToken( &token ) ) {
//Todo: Add some error checking here
return;
}
if ( token == "}" ) {
break;
}
if(ParseMaterialDef(&token, src, MaterialDefManager::MATERIAL_DEF_MATERIAL, &editMaterial.materialData)) {
continue;
}
if ( !token.Icmp( "diffusemap" ) ) {
//Added as a special stage
idStr str;
src->ReadRestOfLine( str );
AddSpecialMapStage("diffusemap", str);
}
else if ( !token.Icmp( "specularmap" ) ) {
idStr str;
src->ReadRestOfLine( str );
AddSpecialMapStage("specularmap", str);
}
else if ( !token.Icmp( "bumpmap" ) ) {
idStr str;
src->ReadRestOfLine( str );
AddSpecialMapStage("bumpmap", str);
}
else if( token == "{" ) {
ParseStage(src);
}
}
}
/**
* Parses a single stage from the source text from an idMaterial and initializes the editor dictionary
* representation of the material.
* @param src The idLexer object that contains the material text.
*/
void MaterialDoc::ParseStage(idLexer* src) {
MEStage_t* newStage = new MEStage_t();
int index = editMaterial.stages.Append(newStage);
newStage->stageData.SetInt("stagetype", STAGE_TYPE_NORMAL);
newStage->enabled = true;
idToken token;
while ( 1 ) {
if ( !src->ExpectAnyToken( &token ) ) {
//Todo: Add some error checking here
return;
}
if ( token == "}" ) {
break;
}
if(ParseMaterialDef(&token, src, MaterialDefManager::MATERIAL_DEF_STAGE, &newStage->stageData)) {
continue;
}
if(!token.Icmp("name")) {
idStr str;
src->ReadRestOfLine( str );
str.StripTrailing('\"');
str.StripLeading('\"');
newStage->stageData.Set("name", str);
continue;
}
}
idStr name;
newStage->stageData.GetString("name", "", name);
if(name.Length() <= 0)
newStage->stageData.Set("name", va("Stage %d", index+1));
}
/**
* Adds a special stage to the material.
* @param stageName The name of the special stage bumpmap, diffusemap or specularmap
* @param map The map for the special stage.
*/
void MaterialDoc::AddSpecialMapStage(const char* stageName, const char* map) {
MEStage_t* newStage = new MEStage_t();
int index = editMaterial.stages.Append(newStage);
newStage->stageData.Set("name", stageName);
newStage->stageData.Set("map", map);
newStage->stageData.SetInt("stagetype", STAGE_TYPE_SPECIALMAP);
newStage->enabled = true;
}
/**
* Finds the appropriate material definition for the supplied token and initializes the
* internal dictionary data.
* @param token The token to lookup
* @param src The idLexer that contains the material source text.
* @param type The type of attribute grouping to use material, stage or special stage.
* @param dict The dictionary to initialize.
*/
bool MaterialDoc::ParseMaterialDef(idToken* token, idLexer* src, int type, idDict* dict) {
MaterialDefList* defs = MaterialDefManager::GetMaterialDefs(type);
for(int i = 0; i < defs->Num(); i++) {
if(!token->Icmp((*defs)[i]->dictName)) {
switch((*defs)[i]->type) {
case MaterialDef::MATERIAL_DEF_TYPE_STRING:
{
idStr str;
src->ReadRestOfLine( str );
if((*defs)[i]->quotes) {
str.StripTrailing('\"');
str.StripLeading('\"');
}
dict->Set((*defs)[i]->dictName, str);
}
break;
case MaterialDef::MATERIAL_DEF_TYPE_BOOL:
{
src->SkipRestOfLine();
dict->SetBool((*defs)[i]->dictName, true);
}
break;
case MaterialDef::MATERIAL_DEF_TYPE_FLOAT:
{
idStr str;
src->ReadRestOfLine( str );
dict->Set((*defs)[i]->dictName, str);
}
break;
case MaterialDef::MATERIAL_DEF_TYPE_INT:
{
idStr str;
src->ReadRestOfLine( str );
dict->Set((*defs)[i]->dictName, str);
}
break;
}
return true;
}
}
return false;
}
/**
* Cleans up the edit material by deleting the stage data structures.
*/
void MaterialDoc::ClearEditMaterial() {
for(int i = 0; i < GetStageCount(); i++) {
delete editMaterial.stages[i];
}
editMaterial.stages.Clear();
editMaterial.materialData.Clear();
}
/**
* Writes the internal dictionary data to the standard format.
*/
const char* MaterialDoc::GenerateSourceText() {
idFile_Memory f;
f.WriteFloatString("\n\n/*\n"
"\tGenerated by the Material Editor.\n"
"\tType 'materialeditor' at the console to launch the material editor.\n"
"*/\n" );
f.WriteFloatString("%s\n", name.c_str());
f.WriteFloatString( "{\n" );
WriteMaterialDef(-1, &f, MaterialDefManager::MATERIAL_DEF_MATERIAL, 1);
for(int i = 0; i < editMaterial.stages.Num(); i++) {
if(editMaterial.stages[i]->enabled) {
WriteStage(i, &f);
}
}
f.WriteFloatString( "}\n" );
return f.GetDataPtr();
}
/**
* Writes the internal dictionary data to the standard format and replaces the
* idMaterial source text with the newly generated text.
*/
void MaterialDoc::ReplaceSourceText() {
renderMaterial->SetText(GenerateSourceText());
}
/**
* Writes a single stage.
* @param stage The stage to write.
* @param file The file where the stage should be wirtten
*/
void MaterialDoc::WriteStage(int stage, idFile_Memory* file) {
//idStr stageName = GetAttribute(stage, "name");
int type = GetAttributeInt(stage, "stagetype");
//if(!stageName.Icmp("diffusemap") || !stageName.Icmp("specularmap") || !stageName.Icmp("bumpmap")) {
if(type == STAGE_TYPE_SPECIALMAP) {
WriteSpecialMapStage(stage, file);
return;
}
file->WriteFloatString( "\t{\n" );
idStr name = GetAttribute(stage, "name");
if(name.Length() > 0) {
file->WriteFloatString("\t\tname\t\"%s\"\n", name.c_str());
}
WriteMaterialDef(stage, file, MaterialDefManager::MATERIAL_DEF_STAGE, 2);
file->WriteFloatString( "\t}\n" );
}
/**
* Writes a single special stage.
* @param stage The stage to write.
* @param file The file where the stage should be wirtten
*/
void MaterialDoc::WriteSpecialMapStage(int stage, idFile_Memory* file) {
idStr stageName = GetAttribute(stage, "name");
idStr map = GetAttribute(stage, "map");
file->WriteFloatString( "\t%s\t%s\n", stageName.c_str(), map.c_str() );
}
/**
* Writes a set of material attributes to a file.
* @param stage The stage to write or -1 for the material.
* @param file The file where the stage should be wirtten.
* @param type The attribute grouping to use.
* @param indent The number of tabs to indent the text.
*/
void MaterialDoc::WriteMaterialDef(int stage, idFile_Memory* file, int type, int indent) {
idStr prefix = "";
for(int i = 0; i < indent; i++) {
prefix += "\t";
}
MaterialDefList* defs = MaterialDefManager::GetMaterialDefs(type);
for(int i = 0; i < defs->Num(); i++) {
switch((*defs)[i]->type) {
case MaterialDef::MATERIAL_DEF_TYPE_STRING:
{
idStr attrib = GetAttribute(stage, (*defs)[i]->dictName);
if(attrib.Length() > 0) {
if((*defs)[i]->quotes)
file->WriteFloatString("%s%s\t\"%s\"\n", prefix.c_str(), (*defs)[i]->dictName.c_str(), attrib.c_str());
else
file->WriteFloatString("%s%s\t%s\n", prefix.c_str(), (*defs)[i]->dictName.c_str(), attrib.c_str());
}
}
break;
case MaterialDef::MATERIAL_DEF_TYPE_BOOL:
{
if(GetAttributeBool(stage, (*defs)[i]->dictName))
file->WriteFloatString("%s%s\t\n",prefix.c_str(), (*defs)[i]->dictName.c_str());
}
break;
case MaterialDef::MATERIAL_DEF_TYPE_FLOAT:
{
float val = GetAttributeFloat(stage, (*defs)[i]->dictName);
file->WriteFloatString("%s%s\t%f\n", prefix.c_str(), (*defs)[i]->dictName.c_str(), val);
}
break;
case MaterialDef::MATERIAL_DEF_TYPE_INT:
{
int val = GetAttributeInt(stage, (*defs)[i]->dictName);
file->WriteFloatString("%s%s\t%d\n", prefix.c_str(), (*defs)[i]->dictName.c_str(), val);
}
break;
}
}
}
|