Skip to content

Commit cf160b4

Browse files
author
Artyom Shalkhakov
committed
Allow managing multiple particle systems in one file.
1 parent 76bddbf commit cf160b4

File tree

2 files changed

+149
-61
lines changed

2 files changed

+149
-61
lines changed

neo/tools/imgui/particleeditor/ParticleEditor.cpp

Lines changed: 127 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,133 @@ bool RangeSlider::Draw( const char *label, float itemWidth, float sliderWidth )
7979
return changed;
8080
}
8181

82+
ParticleNew::ParticleNew()
83+
: fileSelection(-1)
84+
, prtFiles()
85+
, fileName( "" )
86+
, name( "" )
87+
, errorText( "" )
88+
, dp( NULL )
89+
, state(DONE)
90+
{
91+
}
92+
93+
void ParticleNew::Start() {
94+
prtFiles.Clear();
95+
96+
idFileList* files = fileSystem->ListFiles( "particles", ".prt", true, true );
97+
for( int i = 0; i < files->GetNumFiles(); i++ )
98+
{
99+
idStr file = files->GetFile( i );
100+
101+
file.StripPath();
102+
file.StripFileExtension();
103+
104+
prtFiles.Append( file );
105+
}
106+
fileSystem->FreeFileList( files );
107+
108+
fileSelection = -1;
109+
fileName.Clear();
110+
errorText.Clear();
111+
dp = NULL;
112+
state = NAME;
113+
114+
ImGui::OpenPopup( "New Particle System" );
115+
}
116+
117+
bool ParticleNew::Draw() {
118+
if ( state == DONE ) {
119+
return false;
120+
}
121+
122+
bool accepted = false;
123+
bool canceled = false;
124+
125+
if ( ImGui::BeginPopupModal( "New Particle System", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) ) {
126+
ImGui::TextColored( ImVec4( 1, 0, 0, 1 ), errorText );
127+
128+
if ( ImGui::InputTextStr( "File Name", &fileName ) ) {
129+
// nop
130+
}
131+
132+
if ( ImGui::BeginListBox( "Files##prtFileSelect" ) ) {
133+
for( int i = 0; i < prtFiles.Num(); i++ )
134+
{
135+
if ( fileName.Length() && prtFiles[i].Find( fileName.c_str(), false ) == -1 ) {
136+
continue;
137+
}
138+
139+
bool selected = ( i == fileSelection );
140+
141+
ImGui::PushID( i );
142+
if ( ImGui::Selectable( prtFiles[i].c_str(), selected ) ) {
143+
fileSelection = i;
144+
fileName = prtFiles[fileSelection];
145+
}
146+
if ( selected ) {
147+
ImGui::SetItemDefaultFocus();
148+
}
149+
ImGui::PopID();
150+
}
151+
152+
ImGui::EndListBox();
153+
}
154+
155+
if ( ImGui::InputTextStr( "Name", &name ) ) {
156+
// nop
157+
}
158+
159+
if ( ImGui::Button( "OK" ) ) {
160+
errorText.Clear();
161+
162+
if ( name.IsEmpty() ) {
163+
errorText += "Please enter a name\n";
164+
accepted = false;
165+
}
166+
167+
idDeclParticle *newDecl = static_cast<idDeclParticle*>( const_cast<idDecl*>( declManager->FindType( DECL_PARTICLE, name.c_str(), false ) ) );
168+
if( newDecl ) {
169+
errorText += idStr::Format( "Particle System %s already exists in %s. Please select a different name\n", name.c_str(), newDecl->GetFileName() );
170+
accepted = false;
171+
}
172+
173+
if ( errorText.IsEmpty() ) {
174+
idStr fullName;
175+
176+
fullName = "particles/";
177+
fullName += fileName;
178+
fullName += ".prt";
179+
180+
// create it
181+
dp = static_cast<idDeclParticle*>( declManager->CreateNewDecl( DECL_PARTICLE, name.c_str(), fullName.c_str() ) );
182+
state = DONE;
183+
184+
accepted = true;
185+
ImGui::CloseCurrentPopup();
186+
}
187+
}
188+
ImGui::SameLine();
189+
if ( ImGui::Button( "Cancel" ) ) {
190+
accepted = false;
191+
state = DONE;
192+
ImGui::CloseCurrentPopup();
193+
}
194+
195+
ImGui::EndPopup();
196+
}
197+
198+
return accepted;
199+
}
200+
82201
ParticleEditor& ParticleEditor::Instance()
83202
{
84203
static ParticleEditor instance;
85204
return instance;
86205
}
87206

88207
ParticleEditor::ParticleEditor()
89-
: fileSelection(0)
208+
: particleNewDlg()
90209
, colorDlg( "Color" )
91210
, fadeColorDlg( "Fade Color" )
92211
, entityColorDlg( "Entity Color" )
@@ -138,68 +257,18 @@ void ParticleEditor::Draw()
138257
ImGui::EndMenuBar();
139258
}
140259

141-
if( clickedNew )
142-
{
143-
ImGui::OpenPopup( "New Particle System" );
260+
if ( clickedNew ) {
261+
particleNewDlg.Start();
144262
}
145263

146-
if( ImGui::BeginPopupModal( "New Particle System" ) )
147-
{
148-
if( prtFiles.Num() == 0 )
149-
{
150-
idFileList* files = fileSystem->ListFiles( "particles", ".prt", true, true );
151-
for( int i = 0; i < files->GetNumFiles(); i++ )
152-
{
153-
prtFiles.Append( files->GetFile( i ) );
154-
}
155-
fileSystem->FreeFileList( files );
156-
}
157-
158-
ImGui::BeginListBox( "##prtFileSelect" );
159-
for( int i = 0; i < prtFiles.Num(); i++ )
160-
{
161-
if( ImGui::ListBox( "Files", &fileSelection, StringListItemGetter, &prtFiles, prtFiles.Num() ) )
162-
{
163-
fileName = prtFiles[fileSelection];
164-
}
165-
}
166-
ImGui::EndListBox();
167-
168-
ImGui::SameLine();
169-
ImGui::SmallButton( "New File" );
170-
171-
ImGui::InputTextStr( "Particle System Name", &fileName );
172-
if( ImGui::Button( "Create" ) )
173-
{
174-
idStr prtName = fileName;
175-
prtName.StripPath();
176-
prtName.StripFileExtension();
264+
if ( particleNewDlg.Draw() ) {
265+
idDeclParticle* dp = particleNewDlg.GetParticle();
177266

178-
if( !prtName.IsEmpty() )
179-
{
180-
idDeclParticle *newDecl = static_cast<idDeclParticle*>( const_cast<idDecl*>( declManager->FindType( DECL_PARTICLE, prtName.c_str(), false ) ) );
181-
if( !newDecl )
182-
{
183-
// create it
184-
newDecl = static_cast<idDeclParticle*>( declManager->CreateNewDecl( DECL_PARTICLE, prtName.c_str(), fileName.c_str() ) );
185-
}
267+
if ( dp ) {
268+
comboParticleSel = comboParticle.Append( dp->GetName() );
186269

187-
comboParticleSel = comboParticle.Append( prtName );
188-
189-
OnCbnSelchangeComboParticles();
190-
191-
ImGui::CloseCurrentPopup();
192-
}
193-
}
194-
195-
ImGui::SameLine();
196-
if( ImGui::Button( "Close" ) )
197-
{
198-
prtFiles.Clear();
199-
ImGui::CloseCurrentPopup();
270+
OnCbnSelchangeComboParticles();
200271
}
201-
202-
ImGui::EndPopup();
203272
}
204273

205274
if( openedParticleBrowser )

neo/tools/imgui/particleeditor/ParticleEditor.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ class ParticleDrop {
107107
state_t state;
108108
};
109109

110+
class ParticleNew {
111+
public:
112+
ParticleNew();
113+
114+
void Start();
115+
bool Draw();
116+
117+
ID_INLINE idDeclParticle* GetParticle() { return dp; }
118+
119+
private:
120+
enum state_t { DONE = 0, NAME };
121+
122+
int fileSelection;
123+
idList<idStr> prtFiles;
124+
idStr fileName;
125+
idStr name;
126+
idStr errorText;
127+
idDeclParticle * dp;
128+
state_t state;
129+
};
130+
110131
class ParticleEditor {
111132

112133
public:
@@ -158,9 +179,7 @@ class ParticleEditor {
158179
private:
159180
bool isShown;
160181

161-
int fileSelection;
162-
idList<idStr> prtFiles;
163-
idStr fileName;
182+
ParticleNew particleNewDlg;
164183

165184
idStr inFileText;
166185

0 commit comments

Comments
 (0)