From 195e3031b7bdcbb5c77054cf496ca8353049f517 Mon Sep 17 00:00:00 2001 From: codereader Date: Fri, 19 Feb 2021 06:54:20 +0100 Subject: [PATCH] #5532: Add utility mapping Material::SurfaceType <=> string --- install/ui/materialeditor.fbp | 34 +++++------ install/ui/materialeditor.xrc | 26 ++++----- libs/materials/ParseLib.h | 41 +++++++++++++ radiant/ui/materials/MaterialEditor.cpp | 28 ++++++++- radiant/ui/materials/MaterialEditor.h | 1 + radiantcore/shaders/ShaderTemplate.cpp | 76 ++++++------------------- radiantcore/shaders/ShaderTemplate.h | 1 + tools/msvc/libs.vcxproj | 1 + tools/msvc/libs.vcxproj.filters | 6 ++ 9 files changed, 123 insertions(+), 91 deletions(-) create mode 100644 libs/materials/ParseLib.h diff --git a/install/ui/materialeditor.fbp b/install/ui/materialeditor.fbp index b03c07e980..623b4118f5 100644 --- a/install/ui/materialeditor.fbp +++ b/install/ui/materialeditor.fbp @@ -167,7 +167,7 @@ wxTAB_TRAVERSAL - + bSizer2 wxVERTICAL @@ -712,7 +712,7 @@ 0 0 wxID_ANY - Description: + Material: 0 0 @@ -721,7 +721,7 @@ 0 1 - m_staticText57 + m_staticText571 1 @@ -743,9 +743,9 @@ 0 - wxEXPAND|wxLEFT - 1 - + wxALIGN_CENTER_VERTICAL + 0 + 1 1 1 @@ -759,6 +759,7 @@ 1 0 + 1 1 @@ -776,12 +777,11 @@ 0 - 0 1 - MaterialDescription + MaterialType 1 @@ -789,6 +789,7 @@ 1 Resizable + 0 1 @@ -799,7 +800,6 @@ wxFILTER_NONE wxDefaultValidator - @@ -837,7 +837,7 @@ 0 0 wxID_ANY - Material: + Description: 0 0 @@ -846,7 +846,7 @@ 0 1 - m_staticText571 + m_staticText57 1 @@ -868,9 +868,9 @@ 0 - wxALIGN_CENTER_VERTICAL - 0 - + wxEXPAND|wxLEFT + 1 + 1 1 1 @@ -884,7 +884,6 @@ 1 0 - 1 1 @@ -902,11 +901,12 @@ 0 + 0 1 - MaterialType + MaterialDescription 1 @@ -914,7 +914,6 @@ 1 Resizable - 0 1 @@ -925,6 +924,7 @@ wxFILTER_NONE wxDefaultValidator + diff --git a/install/ui/materialeditor.xrc b/install/ui/materialeditor.xrc index a4d7c66cc2..365832f194 100644 --- a/install/ui/materialeditor.xrc +++ b/install/ui/materialeditor.xrc @@ -127,35 +127,35 @@ wxALIGN_CENTER_VERTICAL|wxALL 0 - - + + -1 - - wxEXPAND|wxLEFT + + wxALIGN_CENTER_VERTICAL 0 - - + + 0 + wxALIGN_CENTER_VERTICAL|wxALL 0 - - + + -1 - - wxALIGN_CENTER_VERTICAL + + wxEXPAND|wxLEFT 0 - - 0 - + + diff --git a/libs/materials/ParseLib.h b/libs/materials/ParseLib.h new file mode 100644 index 0000000000..c7f612dd1f --- /dev/null +++ b/libs/materials/ParseLib.h @@ -0,0 +1,41 @@ +#pragma once + +#include +#include "ishaders.h" + +namespace shaders +{ + +constexpr std::pair SurfaceTypeMapping[] +{ + { "metal", Material::SURFTYPE_METAL }, + { "stone", Material::SURFTYPE_STONE }, + { "flesh", Material::SURFTYPE_FLESH }, + { "wood", Material::SURFTYPE_WOOD }, + { "cardboard", Material::SURFTYPE_CARDBOARD }, + { "liquid", Material::SURFTYPE_LIQUID }, + { "glass", Material::SURFTYPE_GLASS }, + { "plastic", Material::SURFTYPE_PLASTIC }, + { "ricochet", Material::SURFTYPE_RICOCHET }, + { "surftype10", Material::SURFTYPE_10 }, + { "surftype11", Material::SURFTYPE_11 }, + { "surftype12", Material::SURFTYPE_12 }, + { "surftype13", Material::SURFTYPE_13 }, + { "surftype14", Material::SURFTYPE_14 }, + { "surftype15", Material::SURFTYPE_15 } +}; + +inline std::string getStringForSurfaceType(Material::SurfaceType type) +{ + for (const auto& pair : SurfaceTypeMapping) + { + if (type == pair.second) + { + return pair.first; + } + } + + return std::string(); +} + +} diff --git a/radiant/ui/materials/MaterialEditor.cpp b/radiant/ui/materials/MaterialEditor.cpp index 3f9bed4483..385d0f9eef 100644 --- a/radiant/ui/materials/MaterialEditor.cpp +++ b/radiant/ui/materials/MaterialEditor.cpp @@ -8,6 +8,7 @@ #include #include "wxutil/SourceView.h" #include "fmt/format.h" +#include "materials/ParseLib.h" namespace ui { @@ -77,6 +78,7 @@ MaterialEditor::MaterialEditor() : previewPanel->GetSizer()->Add(_preview->getWidget(), 1, wxEXPAND); previewPanel->GetSizer()->Add(_sourceView, 1, wxEXPAND); + setupMaterialProperties(); setupMaterialStageView(); // Set the default size of the window @@ -118,12 +120,24 @@ void MaterialEditor::_onClose(wxCommandEvent& ev) void MaterialEditor::ShowDialog(const cmd::ArgumentList& args) { - MaterialEditor* editor = new MaterialEditor; + auto* editor = new MaterialEditor; editor->ShowModal(); editor->Destroy(); } +void MaterialEditor::setupMaterialProperties() +{ + auto* typeDropdown = getControl("MaterialType"); + + typeDropdown->AppendString(""); // empty string for undefined + + for (const auto& pair : shaders::SurfaceTypeMapping) + { + typeDropdown->AppendString(pair.first); + } +} + void MaterialEditor::setupMaterialStageView() { // Stage view @@ -165,6 +179,18 @@ void MaterialEditor::updateMaterialPropertiesFromMaterial() { getControl("MaterialDescription")->SetValue(_material->getDescription()); + // Type dropdown + auto* materialTypeDropdown = getControl("MaterialType"); + if (_material->getSurfaceType() == Material::SURFTYPE_DEFAULT) + { + materialTypeDropdown->Select(0); + } + else + { + auto surfType = shaders::getStringForSurfaceType(_material->getSurfaceType()); + materialTypeDropdown->Select(materialTypeDropdown->FindString(surfType)); + } + // Surround the definition with curly braces, these are not included auto definition = fmt::format("{0}\n{{{1}}}", _material->getName(), _material->getDefinition()); _sourceView->SetValue(definition); diff --git a/radiant/ui/materials/MaterialEditor.h b/radiant/ui/materials/MaterialEditor.h index 874ce44d26..ef59f3df65 100644 --- a/radiant/ui/materials/MaterialEditor.h +++ b/radiant/ui/materials/MaterialEditor.h @@ -45,6 +45,7 @@ class MaterialEditor : private: void setupMaterialStageView(); + void setupMaterialProperties(); void updateControlsFromMaterial(); void updateMaterialPropertiesFromMaterial(); diff --git a/radiantcore/shaders/ShaderTemplate.cpp b/radiantcore/shaders/ShaderTemplate.cpp index 76b84f5e3c..7b6cec9fd0 100644 --- a/radiantcore/shaders/ShaderTemplate.cpp +++ b/radiantcore/shaders/ShaderTemplate.cpp @@ -13,6 +13,7 @@ #include #include "ShaderExpression.h" +#include "materials/ParseLib.h" namespace shaders { @@ -938,6 +939,20 @@ bool ShaderTemplate::parseStageModifiers(parser::DefTokeniser& tokeniser, return true; } +bool ShaderTemplate::parseMaterialType(parser::DefTokeniser& tokeniser, const std::string& token) +{ + for (const auto& pair : SurfaceTypeMapping) + { + if (token == pair.first) + { + _surfaceType = pair.second; + return true; + } + } + + return false; +} + bool ShaderTemplate::parseSurfaceFlags(parser::DefTokeniser& tokeniser, const std::string& token) { @@ -1032,66 +1047,6 @@ bool ShaderTemplate::parseSurfaceFlags(parser::DefTokeniser& tokeniser, else if (token == "nosteps") { _surfaceFlags |= Material::SURF_NOSTEPS; - } - else if (token == "metal") - { - _surfaceType = Material::SURFTYPE_METAL; - } - else if (token == "stone") - { - _surfaceType = Material::SURFTYPE_STONE; - } - else if (token == "flesh") - { - _surfaceType = Material::SURFTYPE_FLESH; - } - else if (token == "wood") - { - _surfaceType = Material::SURFTYPE_WOOD; - } - else if (token == "cardboard") - { - _surfaceType = Material::SURFTYPE_CARDBOARD; - } - else if (token == "liquid") - { - _surfaceType = Material::SURFTYPE_LIQUID; - } - else if (token == "glass") - { - _surfaceType = Material::SURFTYPE_GLASS; - } - else if (token == "plastic") - { - _surfaceType = Material::SURFTYPE_PLASTIC; - } - else if (token == "ricochet") - { - _surfaceType = Material::SURFTYPE_RICOCHET; - } - else if (token == "surftype10") - { - _surfaceType = Material::SURFTYPE_10; - } - else if (token == "surftype11") - { - _surfaceType = Material::SURFTYPE_11; - } - else if (token == "surftype12") - { - _surfaceType = Material::SURFTYPE_12; - } - else if (token == "surftype13") - { - _surfaceType = Material::SURFTYPE_13; - } - else if (token == "surftype14") - { - _surfaceType = Material::SURFTYPE_14; - } - else if (token == "surftype15") - { - _surfaceType = Material::SURFTYPE_15; } else if (token == "guisurf") { @@ -1184,6 +1139,7 @@ void ShaderTemplate::parseDefinition() if (parseLightKeywords(tokeniser, token)) continue; if (parseBlendShortcuts(tokeniser, token)) continue; if (parseSurfaceFlags(tokeniser, token)) continue; + if (parseMaterialType(tokeniser, token)) continue; rWarning() << "Material keyword not recognised: " << token << std::endl; diff --git a/radiantcore/shaders/ShaderTemplate.h b/radiantcore/shaders/ShaderTemplate.h index c5d55081d3..560deaa31b 100644 --- a/radiantcore/shaders/ShaderTemplate.h +++ b/radiantcore/shaders/ShaderTemplate.h @@ -284,6 +284,7 @@ class ShaderTemplate bool parseBlendMaps(parser::DefTokeniser&, const std::string&); bool parseStageModifiers(parser::DefTokeniser&, const std::string&); bool parseSurfaceFlags(parser::DefTokeniser&, const std::string&); + bool parseMaterialType(parser::DefTokeniser&, const std::string&); bool parseCondition(parser::DefTokeniser&, const std::string&); IShaderExpressionPtr parseSingleExpressionTerm(parser::DefTokeniser& tokeniser); diff --git a/tools/msvc/libs.vcxproj b/tools/msvc/libs.vcxproj index 7bf021a5cb..d1445e8eb1 100644 --- a/tools/msvc/libs.vcxproj +++ b/tools/msvc/libs.vcxproj @@ -159,6 +159,7 @@ + diff --git a/tools/msvc/libs.vcxproj.filters b/tools/msvc/libs.vcxproj.filters index b974080a20..12649b7ab2 100644 --- a/tools/msvc/libs.vcxproj.filters +++ b/tools/msvc/libs.vcxproj.filters @@ -280,6 +280,9 @@ render + + materials + @@ -324,5 +327,8 @@ {b88685b4-a515-4b1d-b6aa-2c1499b227df} + + {60865a71-2a02-47bd-b5e2-8e7ec339d27b} + \ No newline at end of file