/
FindShader.cpp
224 lines (175 loc) · 7.87 KB
/
FindShader.cpp
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
#include "FindShader.h"
#include "i18n.h"
#include "imousetoolmanager.h"
#include "ishaderclipboard.h"
#include "ui/materials/MaterialChooser.h"
#include "wxutil/dialog/MessageBox.h"
#include "wxutil/MouseButton.h"
#include "wxutil/Modifier.h"
#include "camera/tools/ShaderClipboardTools.h"
#include "registry/registry.h"
#include "shaderlib.h"
#include <wx/button.h>
#include <wx/stattext.h>
#include <wx/checkbox.h>
#include <fmt/format.h>
namespace ui
{
namespace
{
constexpr const char* const COUNT_TEXT = N_("{0:d} shader(s) replaced.");
const std::string RKEY_ROOT = "user/ui/textures/findShaderDialog/";
const std::string RKEY_PICK_HINT_SHOWN = RKEY_ROOT + "pickHintShown";
constexpr const char* const PICK_TEXTURE_HINT = N_("When picking texture names, click the pick button and use {0}\n"
"in the camera view to pick a material name. The picked texture will be\n"
"filled in the entry box next to the activated button.");
}
FindAndReplaceShader::FindAndReplaceShader(wxWindow* parent) :
DockablePanel(parent),
_lastFocusedEntry(nullptr)
{
populateWindow();
}
FindAndReplaceShader::~FindAndReplaceShader()
{
disconnectListeners();
}
void FindAndReplaceShader::onPanelActivated()
{
connectListeners();
}
void FindAndReplaceShader::onPanelDeactivated()
{
disconnectListeners();
}
void FindAndReplaceShader::connectListeners()
{
_shaderClipboardConn = GlobalShaderClipboard().signal_sourceChanged().connect(
sigc::mem_fun(this, &FindAndReplaceShader::onShaderClipboardChanged));
}
void FindAndReplaceShader::disconnectListeners()
{
_shaderClipboardConn.disconnect();
}
void FindAndReplaceShader::populateWindow()
{
wxPanel* mainPanel = loadNamedPanel(this, "FindReplaceDialogMainPanel");
SetSizer(new wxBoxSizer(wxVERTICAL));
GetSizer()->Add(mainPanel, 1, wxEXPAND);
findNamedObject<wxTextCtrl>(this, "FindReplaceDialogFindEntry")->Connect(
wxEVT_TEXT, wxCommandEventHandler(FindAndReplaceShader::onEntryChanged), NULL, this);
findNamedObject<wxTextCtrl>(this, "FindReplaceDialogReplaceEntry")->Connect(
wxEVT_TEXT, wxCommandEventHandler(FindAndReplaceShader::onEntryChanged), NULL, this);
findNamedObject<wxTextCtrl>(this, "FindReplaceDialogFindEntry")->Connect(
wxEVT_SET_FOCUS, wxFocusEventHandler(FindAndReplaceShader::onEntryFocusChanged), NULL, this);
findNamedObject<wxTextCtrl>(this, "FindReplaceDialogReplaceEntry")->Connect(
wxEVT_SET_FOCUS, wxFocusEventHandler(FindAndReplaceShader::onEntryFocusChanged), NULL, this);
findNamedObject<wxButton>(this, "FindReplaceDialogFindSelectButton")->Connect(
wxEVT_BUTTON, wxCommandEventHandler(FindAndReplaceShader::onChooseFind), NULL, this);
findNamedObject<wxButton>(this, "FindReplaceDialogReplaceSelectButton")->Connect(
wxEVT_BUTTON, wxCommandEventHandler(FindAndReplaceShader::onChooseReplace), NULL, this);
findNamedObject<wxButton>(this, "FindReplaceDialogFindPickButton")->Connect(
wxEVT_BUTTON, wxCommandEventHandler(FindAndReplaceShader::onChoosePick), NULL, this);
findNamedObject<wxButton>(this, "FindReplaceDialogReplacePickButton")->Connect(
wxEVT_BUTTON, wxCommandEventHandler(FindAndReplaceShader::onChoosePick), NULL, this);
findNamedObject<wxButton>(this, "FindReplaceDialogFindPickButton")->SetToolTip(getPickHelpText());
findNamedObject<wxButton>(this, "FindReplaceDialogReplacePickButton")->SetToolTip(getPickHelpText());
findNamedObject<wxButton>(this, "FindReplaceDialogFindButton")->Connect(
wxEVT_BUTTON, wxCommandEventHandler(FindAndReplaceShader::onReplace), NULL, this);
findNamedObject<wxStaticText>(this, "FindReplaceDialogStatusLabel")->SetLabel("");
SetSize(mainPanel->GetMinSize());
Fit();
}
void FindAndReplaceShader::performReplace()
{
auto find = findNamedObject<wxTextCtrl>(this, "FindReplaceDialogFindEntry")->GetValue().ToStdString();
auto replace = findNamedObject<wxTextCtrl>(this, "FindReplaceDialogReplaceEntry")->GetValue().ToStdString();
bool selectedOnly = findNamedObject<wxCheckBox>(this, "FindReplaceDialogSearchCurSelection")->GetValue();
int replaced = scene::findAndReplaceShader(find, replace, selectedOnly);
auto status = findNamedObject<wxStaticText>(this, "FindReplaceDialogStatusLabel");
status->SetLabel(fmt::format(_(COUNT_TEXT), replaced));
}
void FindAndReplaceShader::onChooseFind(wxCommandEvent& ev)
{
// Construct the modal dialog
auto chooser = new MaterialChooser(this, MaterialSelector::TextureFilter::Regular,
findNamedObject<wxTextCtrl>(this, "FindReplaceDialogFindEntry"));
chooser->ShowModal();
chooser->Destroy();
}
void FindAndReplaceShader::onChooseReplace(wxCommandEvent& ev)
{
// Construct the modal dialog
auto chooser = new MaterialChooser(this, MaterialSelector::TextureFilter::Regular,
findNamedObject<wxTextCtrl>(this, "FindReplaceDialogReplaceEntry"));
chooser->ShowModal();
chooser->Destroy();
}
void FindAndReplaceShader::onReplace(wxCommandEvent& ev)
{
performReplace();
}
void FindAndReplaceShader::onChoosePick(wxCommandEvent& ev)
{
// Show the dialog the first time the user clicks this button
if (!registry::getValue<bool>(RKEY_PICK_HINT_SHOWN, false))
{
wxutil::Messagebox::Show(_("Picking Texures"), getPickHelpText(),
IDialog::MessageType::MESSAGE_CONFIRM, this);
registry::setValue(RKEY_PICK_HINT_SHOWN, true);
}
wxButton* pickFindButton = findNamedObject<wxButton>(this, "FindReplaceDialogFindPickButton");
wxButton* pickReplaceButton = findNamedObject<wxButton>(this, "FindReplaceDialogReplacePickButton");
if (ev.GetEventObject() == pickFindButton)
{
pickFindButton->SetBackgroundColour(wxColour(220, 0, 0));
pickReplaceButton->SetBackgroundColour(wxNullColour);
_lastFocusedEntry = findNamedObject<wxTextCtrl>(this, "FindReplaceDialogFindEntry");
}
else if (ev.GetEventObject() == pickReplaceButton)
{
pickFindButton->SetBackgroundColour(wxNullColour);
pickReplaceButton->SetBackgroundColour(wxColour(220, 0, 0));
_lastFocusedEntry = findNamedObject<wxTextCtrl>(this, "FindReplaceDialogReplaceEntry");
}
}
void FindAndReplaceShader::onEntryChanged(wxCommandEvent& ev)
{
findNamedObject<wxStaticText>(this, "FindReplaceDialogStatusLabel")->SetLabel("");
}
void FindAndReplaceShader::onEntryFocusChanged(wxFocusEvent& ev)
{
_lastFocusedEntry = wxDynamicCast(ev.GetEventObject(), wxTextCtrl);
wxButton* pickFindButton = findNamedObject<wxButton>(this, "FindReplaceDialogFindPickButton");
wxButton* pickReplaceButton = findNamedObject<wxButton>(this, "FindReplaceDialogReplacePickButton");
if (_lastFocusedEntry == findNamedObject<wxTextCtrl>(this, "FindReplaceDialogFindEntry"))
{
pickFindButton->SetBackgroundColour(wxColour(220, 0, 0));
pickReplaceButton->SetBackgroundColour(wxNullColour);
}
else if (_lastFocusedEntry == findNamedObject<wxTextCtrl>(this, "FindReplaceDialogReplaceEntry"))
{
pickFindButton->SetBackgroundColour(wxNullColour);
pickReplaceButton->SetBackgroundColour(wxColour(220, 0, 0));
}
ev.Skip();
}
void FindAndReplaceShader::onShaderClipboardChanged()
{
if (_lastFocusedEntry)
{
_lastFocusedEntry->SetValue(GlobalShaderClipboard().getShaderName());
}
}
std::string FindAndReplaceShader::getPickHelpText()
{
// Find the pick texture mouse tool to get an accurate help text
IMouseToolGroup& camGroup = GlobalMouseToolManager().getGroup(IMouseToolGroup::Type::CameraView);
MouseToolPtr tool = camGroup.getMouseToolByName(PickShaderTool::NAME());
unsigned int mapping = camGroup.getMappingForTool(tool);
std::string bindText = wxutil::Modifier::GetModifierString(mapping);
bindText += !bindText.empty() ? "-" : "";
bindText += wxutil::MouseButton::GetButtonString(mapping);
return fmt::format(_(PICK_TEXTURE_HINT), bindText);
}
} // namespace