Skip to content

Commit

Permalink
#6045: Save/restore a few MaterialEditor settings
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Aug 6, 2022
1 parent 9115f4c commit 027cb57
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 12 deletions.
62 changes: 58 additions & 4 deletions radiant/ui/materials/editor/MaterialEditor.cpp
Expand Up @@ -38,7 +38,6 @@
#include "CheckBoxBinding.h"
#include "MapExpressionEntry.h"
#include "TexturePreview.h"
#include "string/trim.h"
#include "ui/common/ShaderChooser.h"

namespace ui
Expand All @@ -48,14 +47,20 @@ namespace
{
const char* const DIALOG_TITLE = N_("Material Editor");

const char* const ICON_STAGE_VISIBLE = "visible.png";
const char* const ICON_STAGE_INVISIBLE = "invisible.png";
const char* const ICON_GLOBAL_SETTINGS = "icon_texture.png";
constexpr const char* const ICON_STAGE_VISIBLE = "visible.png";
constexpr const char* const ICON_STAGE_INVISIBLE = "invisible.png";
constexpr const char* const ICON_GLOBAL_SETTINGS = "icon_texture.png";

const std::string RKEY_ROOT = "user/ui/materialEditor/";
const std::string RKEY_SPLIT_POS = RKEY_ROOT + "splitPos";
const std::string RKEY_WINDOW_STATE = RKEY_ROOT + "window";

const std::string RKEY_LIGHTING_MODE_ENABLED = RKEY_ROOT + "lightModeEnabled";
const std::string RKEY_LAST_SELECTED_MATERIAL = RKEY_ROOT + "lastSelectedMaterial";
const std::string RKEY_PREVIEW_LIGHT_CLASSNAME = RKEY_ROOT + "previewLightClassname";
const std::string RKEY_PREVIEW_ROOM_MATERIAL = RKEY_ROOT + "previewRoomMaterial";
const std::string RKEY_PREVIEW_OBJECT_TYPE = RKEY_ROOT + "previewObjectType";

const char* const CUSTOM_BLEND_TYPE = N_("Custom");

// Columns for the stages list
Expand Down Expand Up @@ -213,8 +218,12 @@ int MaterialEditor::ShowModal()
_treeView->SetSelectedDeclName(_materialToPreselect);
}

loadSettings();

int returnCode = DialogBase::ShowModal();

saveSettings();

// Tell the position tracker to save the information
_windowPosition.saveToPath(RKEY_WINDOW_STATE);

Expand All @@ -235,6 +244,51 @@ int MaterialEditor::ShowModal(const std::string& materialToPreselect)
return ShowModal();
}

void MaterialEditor::loadSettings()
{
if (_materialToPreselect.empty())
{
_materialToPreselect = registry::getValue<std::string>(RKEY_LAST_SELECTED_MATERIAL);

if (!_materialToPreselect.empty())
{
_treeView->SetSelectedDeclName(_materialToPreselect);
}
}

_preview->setStartupLightingMode(registry::getValue<bool>(RKEY_LIGHTING_MODE_ENABLED));

auto storedClassname = registry::getValue<std::string>(RKEY_PREVIEW_LIGHT_CLASSNAME);

if (!storedClassname.empty())
{
_preview->setLightClassname(storedClassname);
}

auto storedRoomMaterial = registry::getValue<std::string>(RKEY_PREVIEW_ROOM_MATERIAL);

if (!storedRoomMaterial.empty())
{
_preview->setRoomMaterial(storedRoomMaterial);
}

auto storedTestModelType = registry::getValue<std::string>(RKEY_PREVIEW_OBJECT_TYPE);

if (!storedTestModelType.empty())
{
_preview->setTestModelType(MaterialPreview::GetTestModelType(storedTestModelType));
}
}

void MaterialEditor::saveSettings()
{
registry::setValue(RKEY_LAST_SELECTED_MATERIAL, _treeView->GetSelectedDeclName());
registry::setValue(RKEY_LIGHTING_MODE_ENABLED, _preview->getLightingModeEnabled());
registry::setValue(RKEY_PREVIEW_LIGHT_CLASSNAME, _preview->getLightClassname());
registry::setValue(RKEY_PREVIEW_ROOM_MATERIAL, _preview->getRoomMaterial());
registry::setValue(RKEY_PREVIEW_OBJECT_TYPE, MaterialPreview::GetTestModelTypeName(_preview->getTestModelType()));
}

void MaterialEditor::setupSourceTextPanel(wxWindow* previewPanel)
{
auto sourceTextPanel = new wxCollapsiblePane(previewPanel, wxID_ANY, _("Material Source Text"));
Expand Down
3 changes: 3 additions & 0 deletions radiant/ui/materials/editor/MaterialEditor.h
Expand Up @@ -105,6 +105,9 @@ class MaterialEditor :
bool _onDeleteEvent() override;

private:
void loadSettings();
void saveSettings();

// Asks user to save each unmodified material.
// Returns true if it is safe to go ahead and close the dialog
bool okToCloseDialog();
Expand Down
73 changes: 65 additions & 8 deletions radiant/ui/materials/editor/MaterialPreview.cpp
Expand Up @@ -29,7 +29,8 @@ MaterialPreview::MaterialPreview(wxWindow* parent) :
RenderPreview(parent, true),
_sceneIsReady(false),
_roomMaterial(game::current::getValue<std::string>(GKEY_DEFAULT_ROOM_MATERIAL)),
_defaultCamDistanceFactor(2.0f)
_defaultCamDistanceFactor(2.0f),
_lightClassname(getDefaultLightDef())
{
_testModelSkin = std::make_unique<TestModelSkin>("model");
_testRoomSkin = std::make_unique<TestModelSkin>("room");
Expand All @@ -48,7 +49,7 @@ MaterialPreview::~MaterialPreview()

void MaterialPreview::setupToolbar()
{
// Add one additional toolbar for particle-related stuff
// Add one additional toolbar
wxToolBar* toolbar = new wxToolBar(_mainPanel, wxID_ANY);
toolbar->SetToolBitmapSize(wxSize(16, 16));

Expand Down Expand Up @@ -103,6 +104,44 @@ void MaterialPreview::setRoomMaterial(const std::string& material)
signal_SceneChanged().emit();
}

MaterialPreview::TestModel MaterialPreview::getTestModelType()
{
if (_testModelCubeButton->IsToggled()) return TestModel::Cube;
if (_testModelSphereButton->IsToggled()) return TestModel::Sphere;
if (_testModelTilesButton->IsToggled()) return TestModel::Tiles;

return TestModel::Cube;
}

void MaterialPreview::setTestModelType(TestModel type)
{
_testModelCubeButton->SetToggle(type == TestModel::Cube);
_testModelSphereButton->SetToggle(type == TestModel::Sphere);
_testModelTilesButton->SetToggle(type == TestModel::Tiles);

setupTestModel();
}

std::string MaterialPreview::GetTestModelTypeName(TestModel type)
{
switch (type)
{
case TestModel::Cube: return "Cube";
case TestModel::Sphere: return "Sphere";
case TestModel::Tiles: return "Tiles";
default: throw std::invalid_argument("Unknown preview model type");
}
}

MaterialPreview::TestModel MaterialPreview::GetTestModelType(const std::string& typeName)
{
if (typeName == "Cube") return TestModel::Cube;
if (typeName == "Sphere") return TestModel::Sphere;
if (typeName == "Tiles") return TestModel::Tiles;

return TestModel::Cube; // in case bad serialization data reaches this point
}

std::string MaterialPreview::getDefaultLightDef()
{
auto className = game::current::getValue<std::string>(GKEY_DEFAULT_LIGHT_DEF);
Expand Down Expand Up @@ -237,8 +276,7 @@ void MaterialPreview::setupSceneGraph()
getScene()->setRoot(_rootNode);

// Set up the light
_light = GlobalEntityModule().createEntity(
GlobalEntityClassManager().findClass(getDefaultLightDef()));
_light = GlobalEntityModule().createEntity(GlobalEntityClassManager().findClass(_lightClassname));

Node_getEntity(_light)->setKeyValue("light_radius", "750 750 750");
Node_getEntity(_light)->setKeyValue("origin", "150 150 150");
Expand Down Expand Up @@ -276,6 +314,8 @@ void MaterialPreview::setupRoom()

void MaterialPreview::setupTestModel()
{
if (!_entity) return; // too early

if (_entity && _model)
{
scene::removeNodeFromParent(_model);
Expand Down Expand Up @@ -304,7 +344,21 @@ void MaterialPreview::setupTestModel()

void MaterialPreview::onTestModelSelectionChanged(wxCommandEvent& ev)
{
setupTestModel();
if (!ev.IsChecked()) return; // ígnore un-check events

if (ev.GetId() == _testModelCubeButton->GetId())
{
setTestModelType(TestModel::Cube);
}
else if (ev.GetId() == _testModelSphereButton->GetId())
{
setTestModelType(TestModel::Sphere);
}
else if (ev.GetId() == _testModelTilesButton->GetId())
{
setTestModelType(TestModel::Tiles);
}

queueDraw();
}

Expand All @@ -315,14 +369,17 @@ sigc::signal<void>& MaterialPreview::signal_SceneChanged()

std::string MaterialPreview::getLightClassname()
{
return _light ? Node_getEntity(_light)->getEntityClass()->getDeclName() : "";
return _lightClassname;
}

void MaterialPreview::setLightClassname(const std::string& className)
{
if (!_light || className.empty()) return;
// Store this value locally, even if we don't have any light to adjust yet
_lightClassname = className;

if (!_light || _lightClassname.empty()) return;

_light = changeEntityClassname(_light, className);
_light = changeEntityClassname(_light, _lightClassname);
signal_SceneChanged().emit();
}

Expand Down
14 changes: 14 additions & 0 deletions radiant/ui/materials/editor/MaterialPreview.h
Expand Up @@ -34,6 +34,7 @@ class MaterialPreview :
std::shared_ptr<TestModelSkin> _testRoomSkin;

std::string _roomMaterial;
std::string _lightClassname;

float _defaultCamDistanceFactor;

Expand All @@ -44,6 +45,13 @@ class MaterialPreview :
sigc::signal<void> _sigSceneChanged;

public:
enum class TestModel
{
Cube,
Sphere,
Tiles,
};

MaterialPreview(wxWindow* parent);

virtual ~MaterialPreview();
Expand All @@ -63,6 +71,12 @@ class MaterialPreview :
const std::string& getRoomMaterial();
void setRoomMaterial(const std::string& material);

TestModel getTestModelType();
void setTestModelType(TestModel type);

static std::string GetTestModelTypeName(TestModel type);
static TestModel GetTestModelType(const std::string& typeName);

sigc::signal<void>& signal_SceneChanged();

protected:
Expand Down

0 comments on commit 027cb57

Please sign in to comment.