Skip to content

Commit

Permalink
Improved particle system selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Artyom Shalkhakov committed Feb 8, 2025
1 parent cf160b4 commit ff85551
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 76 deletions.
193 changes: 122 additions & 71 deletions neo/tools/imgui/particleeditor/ParticleEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ void ParticleNew::Start() {

fileSelection = -1;
fileName.Clear();
name.Clear();
errorText.Clear();
dp = NULL;
state = NAME;
Expand Down Expand Up @@ -198,6 +199,105 @@ bool ParticleNew::Draw() {
return accepted;
}

ParticleSelect::ParticleSelect()
: comboParticleSel(-1)
, comboParticle()
, name( "" )
, errorText( "" )
, dp( NULL )
, state(DONE)
{
}

void ParticleSelect::Start() {
comboParticle.Clear();
for ( int i = 0; i < declManager->GetNumDecls( DECL_PARTICLE ); i++ ) {
const idDecl *idp = declManager->DeclByIndex( DECL_PARTICLE, i );
comboParticle.Append( idp->GetName() );
}
comboParticleSel = 0;

name.Clear();
errorText.Clear();
dp = NULL;
state = NAME;

ImGui::OpenPopup( "Particle System Browser" );
}

bool ParticleSelect::Draw() {
if ( state == DONE ) {
return false;
}

bool accepted = false;
bool canceled = false;

if ( ImGui::BeginPopupModal( "Particle System Browser", nullptr, ImGuiWindowFlags_AlwaysAutoResize ) ) {
ImGui::TextColored( ImVec4( 1, 0, 0, 1 ), errorText );

if ( ImGui::InputTextStr( "Name", &name ) ) {
// nop
}

if ( ImGui::BeginListBox( "Particle Systems##prtSystemSelect" ) ) {
for( int i = 0; i < comboParticle.Num(); i++ )
{
if ( name.Length() && comboParticle[i].Find( name.c_str(), false ) == -1 ) {
continue;
}

bool selected = ( i == comboParticleSel );

ImGui::PushID( i );
if ( ImGui::Selectable( comboParticle[i].c_str(), selected ) ) {
comboParticleSel = i;
name = comboParticle[comboParticleSel];
}
if ( selected ) {
ImGui::SetItemDefaultFocus();
}
ImGui::PopID();
}

ImGui::EndListBox();
}

if ( ImGui::Button( "OK" ) ) {
errorText.Clear();

if ( name.IsEmpty() ) {
errorText += "Please enter a name or select a particle system from the list\n";
accepted = false;
}

idDeclParticle *decl = static_cast<idDeclParticle*>( const_cast<idDecl*>( declManager->FindType( DECL_PARTICLE, name.c_str(), false ) ) );
if( !decl ) {
errorText += idStr::Format( "Particle System %s does not exist. Please select a different particle system\n", name.c_str() );
accepted = false;
}

if ( errorText.IsEmpty() ) {
dp = decl;
state = DONE;

accepted = true;
ImGui::CloseCurrentPopup();
}
}
ImGui::SameLine();
if ( ImGui::Button( "Cancel" ) ) {
accepted = false;
state = DONE;
ImGui::CloseCurrentPopup();
}

ImGui::EndPopup();
}

return accepted;
}

ParticleEditor& ParticleEditor::Instance()
{
static ParticleEditor instance;
Expand All @@ -206,12 +306,12 @@ ParticleEditor& ParticleEditor::Instance()

ParticleEditor::ParticleEditor()
: particleNewDlg()
, particleSelectDlg()
, colorDlg( "Color" )
, fadeColorDlg( "Fade Color" )
, entityColorDlg( "Entity Color" )
, particleDropDlg()
, materialDeclSelection(0)
, shouldPopulate(false)
{
isShown = false;
}
Expand All @@ -220,7 +320,7 @@ void ParticleEditor::Draw()
{
int i, num;
bool showTool;
bool clickedNew = false, openedParticleBrowser = false;
bool clickedNew = false, clickedSelect = false;

showTool = isShown;

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

if( ImGui::MenuItem( "Open..", "Ctrl+O" ) )
{
shouldPopulate = true;
openedParticleBrowser = true;
clickedSelect = true;
}

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

if ( particleNewDlg.Draw() ) {
idDeclParticle* dp = particleNewDlg.GetParticle();

if ( dp ) {
comboParticleSel = comboParticle.Append( dp->GetName() );
idDeclParticle *dp = particleNewDlg.GetParticle();

OnCbnSelchangeComboParticles();
}
SetCurParticle( dp );
}

if( openedParticleBrowser )
{
ImGui::OpenPopup( "Particle System Browser" );
if ( clickedSelect) {
particleSelectDlg.Start();
}

if( ImGui::BeginPopup( "Particle System Browser" ) )
{
if ( shouldPopulate ) {
EnumParticles();
shouldPopulate = false;
}
if( comboParticle.Num() > 0 )
{
ImGui::Combo( "Particle Systems", &comboParticleSel, &StringListItemGetter, &comboParticle, comboParticle.Num() );
if( ImGui::Button( "Select" ) )
{
idDeclParticle* newDecl = static_cast<idDeclParticle*>( const_cast<idDecl*>( declManager->FindType( DECL_PARTICLE, comboParticle[comboParticleSel], false ) ) );
if( newDecl )
{
OnCbnSelchangeComboParticles();
}
ImGui::CloseCurrentPopup();
}
}
else
{
ImGui::Text( "There are no particle systems!" );
}

ImGui::SameLine();
if ( particleSelectDlg.Draw() ) {
idDeclParticle *dp = particleSelectDlg.GetParticle();

if( ImGui::Button( "Close" ) )
{
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
SetCurParticle( dp );
}

ImGui::TextUnformatted( inFileText.c_str() );
Expand Down Expand Up @@ -1016,19 +1083,18 @@ void ParticleEditor::OnBnClickedButtonUpdate() {
}

void ParticleEditor::SelectParticle( const char *name ) {
int index = comboParticle.FindIndex( name );
if ( index >= 0 ) {
comboParticleSel = index;
UpdateParticleData();
}
idDeclParticle *decl = static_cast<idDeclParticle*>( const_cast<idDecl*>( declManager->FindType( DECL_PARTICLE, name, false ) ) );

SetCurParticle( decl );
}

void ParticleEditor::SetCurParticle( idDeclParticle *dp ) {
curParticle = dp;
UpdateParticleData();
}

idDeclParticle *ParticleEditor::GetCurParticle() {
int index = comboParticleSel;
if ( index < 0 ) {
return NULL;
}
return static_cast<idDeclParticle *>( const_cast<idDecl *>( declManager->DeclByIndex( DECL_PARTICLE, index ) ) );
return curParticle;
}

void ParticleEditor::UpdateParticleData() {
Expand All @@ -1049,16 +1115,11 @@ void ParticleEditor::UpdateParticleData() {
}
listStagesSel = 0;
OnLbnSelchangeListStages();
inFileText = idStr::Format( "Particle file: %s", idp->GetFileName() );
inFileText = idStr::Format( "%s (%s)", idp->GetName(), idp->GetFileName() );

SetParticleView();
}

void ParticleEditor::OnCbnSelchangeComboParticles() {
UpdateParticleData();
}


void ParticleEditor::OnCbnSelchangeComboPath() {
DlgVarsToCurStage();
CurStageToDlgVars();
Expand Down Expand Up @@ -1243,7 +1304,7 @@ void ParticleEditor::RemoveStageThink()
listStagesItemData.Remove( index, newIndex );
listStagesSel = index;
idp->stages.RemoveIndex( newIndex );
OnCbnSelchangeComboParticles();
UpdateParticleData();
ShowCurrentStage();
}
}
Expand Down Expand Up @@ -1476,16 +1537,6 @@ void ParticleEditor::OnBnClickedButtonSave() {
idp->Save();
}

void ParticleEditor::EnumParticles() {
comboParticle.Clear();
for ( int i = 0; i < declManager->GetNumDecls( DECL_PARTICLE ); i++ ) {
const idDecl *idp = declManager->DeclByIndex( DECL_PARTICLE, i );
comboParticle.Append( idp->GetName() );
}
comboParticleSel = 0;
OnCbnSelchangeComboParticles();
}

/*void VectorCallBack(idQuat rotation) {
ParticleEditor::Instance().SetVectorControlUpdate( rotation );
}*/
Expand Down Expand Up @@ -1550,7 +1601,7 @@ void ParticleEditor::Reset()
sliderFadeFraction.SetRange( 0, 20 );
sliderFadeFraction.SetValueRange( 0.0f, 1.0f );

EnumParticles();
UpdateParticleData();
SetParticleView();

buttonSaveParticleEntitiesEnabled = false;
Expand Down
30 changes: 25 additions & 5 deletions neo/tools/imgui/particleeditor/ParticleEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ class ParticleNew {
state_t state;
};

class ParticleSelect {
public:
ParticleSelect();

void Start();
bool Draw();

ID_INLINE idDeclParticle* GetParticle() { return dp; }

private:
enum state_t { DONE = 0, NAME };

int comboParticleSel;
idList<idStr> comboParticle;
idStr name;
idStr errorText;
idDeclParticle * dp;
state_t state;
};

class ParticleEditor {

public:
Expand All @@ -151,7 +171,7 @@ class ParticleEditor {
return isShown;
}

protected:
private:
void OnCbnSelchangeComboParticles();
void OnCbnSelchangeComboPath();
void OnLbnSelchangeListStages();
Expand Down Expand Up @@ -180,6 +200,7 @@ class ParticleEditor {
bool isShown;

ParticleNew particleNewDlg;
ParticleSelect particleSelectDlg;

idStr inFileText;

Expand All @@ -194,6 +215,8 @@ class ParticleEditor {
idList<idStr> materialDecls;
idStr materialDeclName;

idDeclParticle * curParticle;

// edit controls
bool editControlsEnabled;

Expand Down Expand Up @@ -262,9 +285,6 @@ class ParticleEditor {
bool comboCustomPathEnabled;
bool checkEntityColorEnabled;

bool shouldPopulate;
idList<idStr> comboParticle;
int comboParticleSel;
idList<idStr> listStages;
idHashIndex listStagesItemData;
int listStagesSel;
Expand Down Expand Up @@ -322,12 +342,12 @@ class ParticleEditor {
int visualization;

private:
void EnumParticles();
void AddStage( bool clone );
void RemoveStage();
void RemoveStageThink();
void ShowStage();
void HideStage();
void SetCurParticle( idDeclParticle *dp );
idDeclParticle * GetCurParticle();
idParticleStage * GetCurStage();
void ClearDlgVars();
Expand Down

0 comments on commit ff85551

Please sign in to comment.