Skip to content

Commit

Permalink
#5342: Refactor the ShaderDefinitionView to make it more generic. Add…
Browse files Browse the repository at this point in the history
… MaterialDefinitionView specialised for viewing materials.
  • Loading branch information
codereader committed Oct 2, 2020
1 parent 2285f2c commit 3bc0db0
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 155 deletions.
3 changes: 2 additions & 1 deletion radiant/Makefile.am
Expand Up @@ -106,7 +106,8 @@ darkradiant_SOURCES = main.cpp \
ui/common/TexturePreviewCombo.cpp \
ui/common/ShaderSelector.cpp \
ui/common/ShaderChooser.cpp \
ui/common/ShaderDefinitionView.cpp \
ui/common/DefinitionView.cpp \
ui/common/MaterialDefinitionView.cpp \
ui/common/RenderableAABB.cpp \
ui/common/CommandEntry.cpp \
ui/common/EntityChooser.cpp \
Expand Down
105 changes: 105 additions & 0 deletions radiant/ui/common/DefinitionView.cpp
@@ -0,0 +1,105 @@
#include "DefinitionView.h"

#include "i18n.h"
#include "ishaders.h"
#include "imainframe.h"

#include "wxutil/SourceView.h"

#include <wx/sizer.h>
#include <wx/stattext.h>

namespace ui
{

DefinitionView::DefinitionView(const std::string& title) :
wxutil::DialogBase(title),
_view(nullptr)
{
SetSizer(new wxBoxSizer(wxVERTICAL));

_panel = new wxPanel(this, wxID_ANY);
_panel->SetSizer(new wxBoxSizer(wxVERTICAL));

auto* table = new wxFlexGridSizer(2, 2, 6, 6);

auto* nameLabel = new wxStaticText(_panel, wxID_ANY, _("Name:"));
auto* materialFileLabel = new wxStaticText(_panel, wxID_ANY, _("Defined in:"));

_declName = new wxStaticText(_panel, wxID_ANY, "");
_declName->SetFont(_declName->GetFont().Bold());

_filename = new wxStaticText(_panel, wxID_ANY, "");
_filename->SetFont(_filename->GetFont().Bold());

nameLabel->SetMinSize(wxSize(90, -1));
materialFileLabel->SetMinSize(wxSize(90, -1));

table->Add(nameLabel, 0, wxALIGN_CENTER_VERTICAL);
table->Add(_declName, 0, wxALIGN_CENTER_VERTICAL);

table->Add(materialFileLabel, 0, wxALIGN_CENTER_VERTICAL);
table->Add(_filename, 0, wxALIGN_CENTER_VERTICAL);

auto* defLabel = new wxStaticText(_panel, wxID_ANY, _("Definition:"));

_panel->GetSizer()->Add(table, 0);
_panel->GetSizer()->Add(defLabel, 0, wxTOP, 6);

GetSizer()->Add(_panel, 1, wxEXPAND | wxALL, 12);
GetSizer()->Add(CreateStdDialogButtonSizer(wxOK), 0, wxALIGN_RIGHT | wxBOTTOM | wxRIGHT, 12);
}

void DefinitionView::addSourceView(wxutil::SourceViewCtrl* view)
{
_view = view;
_panel->GetSizer()->Add(_view, 1, wxEXPAND | wxTOP, 6);
}

int DefinitionView::ShowModal()
{
// Let subclasses load the values into the controls
update();

FitToScreen(0.6f, 0.66f);

return DialogBase::ShowModal();
}

void DefinitionView::update()
{
if (isEmpty())
{
// Null-ify the contents
_declName->SetLabelMarkup("");
_filename->SetLabelMarkup("");

_view->Enable(false);
return;
}

// Add the shader and file name
auto declName = getDeclName();

_declName->SetLabel(declName);
_filename->SetLabel(getDeclFileName());

_view->Enable(true);

// Surround the definition with curly braces, these are not included
std::string definition = declName + "\n{\n\r";
definition += getDefinition();
definition += "\n\r}";

// Value Updates are only possible when read-only is false
_view->SetReadOnly(false);
_view->SetValue(definition);
_view->SetReadOnly(true);
}

wxWindow* DefinitionView::getMainPanel()
{
return _panel;
}

} // namespace ui
49 changes: 49 additions & 0 deletions radiant/ui/common/DefinitionView.h
@@ -0,0 +1,49 @@
#pragma once

#include <string>
#include <wx/panel.h>
#include "wxutil/dialog/DialogBase.h"

namespace wxutil { class SourceViewCtrl; }
class wxStaticText;

namespace ui
{

class DefinitionView :
public wxutil::DialogBase
{
private:
wxStaticText* _declName;
wxStaticText* _filename;

wxPanel* _panel;

// Will be created by subclasses
wxutil::SourceViewCtrl* _view;

public:
DefinitionView(const std::string& title);

int ShowModal() override;

protected:
void update();

// Whether there is anything to show. If false, the view will be disabled.
virtual bool isEmpty() const = 0;

// Get the name of the declaration
virtual std::string getDeclName() = 0;

// Get the file name the declaration is defined in
virtual std::string getDeclFileName() = 0;

// Provide the definition without the name and the outermost curly braces
virtual std::string getDefinition() = 0;

wxWindow* getMainPanel();
void addSourceView(wxutil::SourceViewCtrl* view);
};

} // namespace ui
41 changes: 41 additions & 0 deletions radiant/ui/common/MaterialDefinitionView.cpp
@@ -0,0 +1,41 @@
#include "MaterialDefinitionView.h"

#include "wxutil/SourceView.h"

namespace ui
{

MaterialDefinitionView::MaterialDefinitionView(const std::string& shaderName) :
DefinitionView(_("View Shader Definition"))
{
addSourceView(new wxutil::D3MaterialSourceViewCtrl(getMainPanel()));
_material = GlobalMaterialManager().getMaterialForName(shaderName);
}

void MaterialDefinitionView::setShader(const std::string& shader)
{
_material = GlobalMaterialManager().getMaterialForName(shader);
update();
}

bool MaterialDefinitionView::isEmpty() const
{
return !_material;
}

std::string MaterialDefinitionView::getDeclName()
{
return _material ? _material->getName() : std::string();
}

std::string MaterialDefinitionView::getDeclFileName()
{
return _material ? _material->getShaderFileName() : std::string();
}

std::string MaterialDefinitionView::getDefinition()
{
return _material ? _material->getDefinition() : std::string();
}

}
28 changes: 28 additions & 0 deletions radiant/ui/common/MaterialDefinitionView.h
@@ -0,0 +1,28 @@
#pragma once

#include "ishaders.h"
#include "DefinitionView.h"

namespace ui
{

class MaterialDefinitionView:
public DefinitionView
{
private:
// The material which should be previewed
MaterialPtr _material;

public:
MaterialDefinitionView(const std::string& shaderName);

void setShader(const std::string& shader);

protected:
bool isEmpty() const override;
std::string getDeclName() override;
std::string getDeclFileName() override;
std::string getDefinition() override;
};

}
109 changes: 0 additions & 109 deletions radiant/ui/common/ShaderDefinitionView.cpp

This file was deleted.

35 changes: 0 additions & 35 deletions radiant/ui/common/ShaderDefinitionView.h

This file was deleted.

0 comments on commit 3bc0db0

Please sign in to comment.