Skip to content

Commit ff85551

Browse files
author
Artyom Shalkhakov
committed
Improved particle system selection.
1 parent cf160b4 commit ff85551

File tree

2 files changed

+147
-76
lines changed

2 files changed

+147
-76
lines changed

neo/tools/imgui/particleeditor/ParticleEditor.cpp

Lines changed: 122 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ void ParticleNew::Start() {
107107

108108
fileSelection = -1;
109109
fileName.Clear();
110+
name.Clear();
110111
errorText.Clear();
111112
dp = NULL;
112113
state = NAME;
@@ -198,6 +199,105 @@ bool ParticleNew::Draw() {
198199
return accepted;
199200
}
200201

202+
ParticleSelect::ParticleSelect()
203+
: comboParticleSel(-1)
204+
, comboParticle()
205+
, name( "" )
206+
, errorText( "" )
207+
, dp( NULL )
208+
, state(DONE)
209+
{
210+
}
211+
212+
void ParticleSelect::Start() {
213+
comboParticle.Clear();
214+
for ( int i = 0; i < declManager->GetNumDecls( DECL_PARTICLE ); i++ ) {
215+
const idDecl *idp = declManager->DeclByIndex( DECL_PARTICLE, i );
216+
comboParticle.Append( idp->GetName() );
217+
}
218+
comboParticleSel = 0;
219+
220+
name.Clear();
221+
errorText.Clear();
222+
dp = NULL;
223+
state = NAME;
224+
225+
ImGui::OpenPopup( "Particle System Browser" );
226+
}
227+
228+
bool ParticleSelect::Draw() {
229+
if ( state == DONE ) {
230+
return false;
231+
}
232+
233+
bool accepted = false;
234+
bool canceled = false;
235+
236+
if ( ImGui::BeginPopupModal( "Particle System Browser", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) ) {
237+
ImGui::TextColored( ImVec4( 1, 0, 0, 1 ), errorText );
238+
239+
if ( ImGui::InputTextStr( "Name", &name ) ) {
240+
// nop
241+
}
242+
243+
if ( ImGui::BeginListBox( "Particle Systems##prtSystemSelect" ) ) {
244+
for( int i = 0; i < comboParticle.Num(); i++ )
245+
{
246+
if ( name.Length() && comboParticle[i].Find( name.c_str(), false ) == -1 ) {
247+
continue;
248+
}
249+
250+
bool selected = ( i == comboParticleSel );
251+
252+
ImGui::PushID( i );
253+
if ( ImGui::Selectable( comboParticle[i].c_str(), selected ) ) {
254+
comboParticleSel = i;
255+
name = comboParticle[comboParticleSel];
256+
}
257+
if ( selected ) {
258+
ImGui::SetItemDefaultFocus();
259+
}
260+
ImGui::PopID();
261+
}
262+
263+
ImGui::EndListBox();
264+
}
265+
266+
if ( ImGui::Button( "OK" ) ) {
267+
errorText.Clear();
268+
269+
if ( name.IsEmpty() ) {
270+
errorText += "Please enter a name or select a particle system from the list\n";
271+
accepted = false;
272+
}
273+
274+
idDeclParticle *decl = static_cast<idDeclParticle*>( const_cast<idDecl*>( declManager->FindType( DECL_PARTICLE, name.c_str(), false ) ) );
275+
if( !decl ) {
276+
errorText += idStr::Format( "Particle System %s does not exist. Please select a different particle system\n", name.c_str() );
277+
accepted = false;
278+
}
279+
280+
if ( errorText.IsEmpty() ) {
281+
dp = decl;
282+
state = DONE;
283+
284+
accepted = true;
285+
ImGui::CloseCurrentPopup();
286+
}
287+
}
288+
ImGui::SameLine();
289+
if ( ImGui::Button( "Cancel" ) ) {
290+
accepted = false;
291+
state = DONE;
292+
ImGui::CloseCurrentPopup();
293+
}
294+
295+
ImGui::EndPopup();
296+
}
297+
298+
return accepted;
299+
}
300+
201301
ParticleEditor& ParticleEditor::Instance()
202302
{
203303
static ParticleEditor instance;
@@ -206,12 +306,12 @@ ParticleEditor& ParticleEditor::Instance()
206306

207307
ParticleEditor::ParticleEditor()
208308
: particleNewDlg()
309+
, particleSelectDlg()
209310
, colorDlg( "Color" )
210311
, fadeColorDlg( "Fade Color" )
211312
, entityColorDlg( "Entity Color" )
212313
, particleDropDlg()
213314
, materialDeclSelection(0)
214-
, shouldPopulate(false)
215315
{
216316
isShown = false;
217317
}
@@ -220,7 +320,7 @@ void ParticleEditor::Draw()
220320
{
221321
int i, num;
222322
bool showTool;
223-
bool clickedNew = false, openedParticleBrowser = false;
323+
bool clickedNew = false, clickedSelect = false;
224324

225325
showTool = isShown;
226326

@@ -238,8 +338,7 @@ void ParticleEditor::Draw()
238338

239339
if( ImGui::MenuItem( "Open..", "Ctrl+O" ) )
240340
{
241-
shouldPopulate = true;
242-
openedParticleBrowser = true;
341+
clickedSelect = true;
243342
}
244343

245344
if( ImGui::MenuItem( "Save", "Ctrl+S" ) )
@@ -262,51 +361,19 @@ void ParticleEditor::Draw()
262361
}
263362

264363
if ( particleNewDlg.Draw() ) {
265-
idDeclParticle* dp = particleNewDlg.GetParticle();
266-
267-
if ( dp ) {
268-
comboParticleSel = comboParticle.Append( dp->GetName() );
364+
idDeclParticle *dp = particleNewDlg.GetParticle();
269365

270-
OnCbnSelchangeComboParticles();
271-
}
366+
SetCurParticle( dp );
272367
}
273368

274-
if( openedParticleBrowser )
275-
{
276-
ImGui::OpenPopup( "Particle System Browser" );
369+
if ( clickedSelect) {
370+
particleSelectDlg.Start();
277371
}
278372

279-
if( ImGui::BeginPopup( "Particle System Browser" ) )
280-
{
281-
if ( shouldPopulate ) {
282-
EnumParticles();
283-
shouldPopulate = false;
284-
}
285-
if( comboParticle.Num() > 0 )
286-
{
287-
ImGui::Combo( "Particle Systems", &comboParticleSel, &StringListItemGetter, &comboParticle, comboParticle.Num() );
288-
if( ImGui::Button( "Select" ) )
289-
{
290-
idDeclParticle* newDecl = static_cast<idDeclParticle*>( const_cast<idDecl*>( declManager->FindType( DECL_PARTICLE, comboParticle[comboParticleSel], false ) ) );
291-
if( newDecl )
292-
{
293-
OnCbnSelchangeComboParticles();
294-
}
295-
ImGui::CloseCurrentPopup();
296-
}
297-
}
298-
else
299-
{
300-
ImGui::Text( "There are no particle systems!" );
301-
}
302-
303-
ImGui::SameLine();
373+
if ( particleSelectDlg.Draw() ) {
374+
idDeclParticle *dp = particleSelectDlg.GetParticle();
304375

305-
if( ImGui::Button( "Close" ) )
306-
{
307-
ImGui::CloseCurrentPopup();
308-
}
309-
ImGui::EndPopup();
376+
SetCurParticle( dp );
310377
}
311378

312379
ImGui::TextUnformatted( inFileText.c_str() );
@@ -1016,19 +1083,18 @@ void ParticleEditor::OnBnClickedButtonUpdate() {
10161083
}
10171084

10181085
void ParticleEditor::SelectParticle( const char *name ) {
1019-
int index = comboParticle.FindIndex( name );
1020-
if ( index >= 0 ) {
1021-
comboParticleSel = index;
1022-
UpdateParticleData();
1023-
}
1086+
idDeclParticle *decl = static_cast<idDeclParticle*>( const_cast<idDecl*>( declManager->FindType( DECL_PARTICLE, name, false ) ) );
1087+
1088+
SetCurParticle( decl );
1089+
}
1090+
1091+
void ParticleEditor::SetCurParticle( idDeclParticle *dp ) {
1092+
curParticle = dp;
1093+
UpdateParticleData();
10241094
}
10251095

10261096
idDeclParticle *ParticleEditor::GetCurParticle() {
1027-
int index = comboParticleSel;
1028-
if ( index < 0 ) {
1029-
return NULL;
1030-
}
1031-
return static_cast<idDeclParticle *>( const_cast<idDecl *>( declManager->DeclByIndex( DECL_PARTICLE, index ) ) );
1097+
return curParticle;
10321098
}
10331099

10341100
void ParticleEditor::UpdateParticleData() {
@@ -1049,16 +1115,11 @@ void ParticleEditor::UpdateParticleData() {
10491115
}
10501116
listStagesSel = 0;
10511117
OnLbnSelchangeListStages();
1052-
inFileText = idStr::Format( "Particle file: %s", idp->GetFileName() );
1118+
inFileText = idStr::Format( "%s (%s)", idp->GetName(), idp->GetFileName() );
10531119

10541120
SetParticleView();
10551121
}
10561122

1057-
void ParticleEditor::OnCbnSelchangeComboParticles() {
1058-
UpdateParticleData();
1059-
}
1060-
1061-
10621123
void ParticleEditor::OnCbnSelchangeComboPath() {
10631124
DlgVarsToCurStage();
10641125
CurStageToDlgVars();
@@ -1243,7 +1304,7 @@ void ParticleEditor::RemoveStageThink()
12431304
listStagesItemData.Remove( index, newIndex );
12441305
listStagesSel = index;
12451306
idp->stages.RemoveIndex( newIndex );
1246-
OnCbnSelchangeComboParticles();
1307+
UpdateParticleData();
12471308
ShowCurrentStage();
12481309
}
12491310
}
@@ -1476,16 +1537,6 @@ void ParticleEditor::OnBnClickedButtonSave() {
14761537
idp->Save();
14771538
}
14781539

1479-
void ParticleEditor::EnumParticles() {
1480-
comboParticle.Clear();
1481-
for ( int i = 0; i < declManager->GetNumDecls( DECL_PARTICLE ); i++ ) {
1482-
const idDecl *idp = declManager->DeclByIndex( DECL_PARTICLE, i );
1483-
comboParticle.Append( idp->GetName() );
1484-
}
1485-
comboParticleSel = 0;
1486-
OnCbnSelchangeComboParticles();
1487-
}
1488-
14891540
/*void VectorCallBack(idQuat rotation) {
14901541
ParticleEditor::Instance().SetVectorControlUpdate( rotation );
14911542
}*/
@@ -1550,7 +1601,7 @@ void ParticleEditor::Reset()
15501601
sliderFadeFraction.SetRange( 0, 20 );
15511602
sliderFadeFraction.SetValueRange( 0.0f, 1.0f );
15521603

1553-
EnumParticles();
1604+
UpdateParticleData();
15541605
SetParticleView();
15551606

15561607
buttonSaveParticleEntitiesEnabled = false;

neo/tools/imgui/particleeditor/ParticleEditor.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ class ParticleNew {
128128
state_t state;
129129
};
130130

131+
class ParticleSelect {
132+
public:
133+
ParticleSelect();
134+
135+
void Start();
136+
bool Draw();
137+
138+
ID_INLINE idDeclParticle* GetParticle() { return dp; }
139+
140+
private:
141+
enum state_t { DONE = 0, NAME };
142+
143+
int comboParticleSel;
144+
idList<idStr> comboParticle;
145+
idStr name;
146+
idStr errorText;
147+
idDeclParticle * dp;
148+
state_t state;
149+
};
150+
131151
class ParticleEditor {
132152

133153
public:
@@ -151,7 +171,7 @@ class ParticleEditor {
151171
return isShown;
152172
}
153173

154-
protected:
174+
private:
155175
void OnCbnSelchangeComboParticles();
156176
void OnCbnSelchangeComboPath();
157177
void OnLbnSelchangeListStages();
@@ -180,6 +200,7 @@ class ParticleEditor {
180200
bool isShown;
181201

182202
ParticleNew particleNewDlg;
203+
ParticleSelect particleSelectDlg;
183204

184205
idStr inFileText;
185206

@@ -194,6 +215,8 @@ class ParticleEditor {
194215
idList<idStr> materialDecls;
195216
idStr materialDeclName;
196217

218+
idDeclParticle * curParticle;
219+
197220
// edit controls
198221
bool editControlsEnabled;
199222

@@ -262,9 +285,6 @@ class ParticleEditor {
262285
bool comboCustomPathEnabled;
263286
bool checkEntityColorEnabled;
264287

265-
bool shouldPopulate;
266-
idList<idStr> comboParticle;
267-
int comboParticleSel;
268288
idList<idStr> listStages;
269289
idHashIndex listStagesItemData;
270290
int listStagesSel;
@@ -322,12 +342,12 @@ class ParticleEditor {
322342
int visualization;
323343

324344
private:
325-
void EnumParticles();
326345
void AddStage( bool clone );
327346
void RemoveStage();
328347
void RemoveStageThink();
329348
void ShowStage();
330349
void HideStage();
350+
void SetCurParticle( idDeclParticle *dp );
331351
idDeclParticle * GetCurParticle();
332352
idParticleStage * GetCurStage();
333353
void ClearDlgVars();

0 commit comments

Comments
 (0)