Skip to content

Commit

Permalink
terrain_project_importer.as: generate & check unique proj. name
Browse files Browse the repository at this point in the history
  • Loading branch information
ohlidalp committed Dec 9, 2024
1 parent 042dde6 commit 7fb1b9e
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions resources/scripts/terrain_project_importer.as
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ array<string> convertedRaceFileNames;
string fileBeingWritten = "";
GenericDocumentClass@ convertedTerrn2;
int topWrittenRace = -1;
bool nameAlreadyExists = false;

//#region Game Callbacks

Expand Down Expand Up @@ -112,22 +113,9 @@ void drawUI()
{
case STAGE_BUTTON:
{
if (@game.getTerrain() != null && stage == STAGE_BUTTON )
{
ImGui::SetNextItemWidth(375);
if (ImGui::InputText("Project name", projectName))
{
updateTerrainNameInDocument();
}

if ( ImGui::Button("Import terrain as project & convert races from script to terrn2 [Races]"))
{
projectName = generateSafeFileName(projectName); // sanitize user input
stage = STAGE_PUSHMSG;
}
}
break;
}
drawConvertButton();
break;
}
case STAGE_ERROR:
{
ImGui::Separator();
Expand Down Expand Up @@ -251,9 +239,21 @@ void drawDetectedRaces()

//#endregion

//#region STAGE_INIT
void initializeRacesData()
{
TerrainClass@ terrain = game.getTerrain();

// generate unique name
projectName = generateSafeFileName(terrain.getTerrainName() + " [project]");
CacheEntryClass@ existingEntry = modcache.findEntryByFilename(LOADER_TYPE_TERRAIN, /*partial:*/false, projectName+'.terrn2');
int numExisting = 0;
while (@existingEntry != null)
{
numExisting++;
projectName = generateSafeFileName(terrain.getTerrainName() + " [project" + (numExisting+1) + "]");
@existingEntry = modcache.findEntryByFilename(LOADER_TYPE_TERRAIN, /*partial:*/false, projectName+'.terrn2');
}

// find the terrain script
array<int> nids = game.getRunningScripts();
int terrnScriptNid = -1;
Expand Down Expand Up @@ -312,9 +312,6 @@ void initializeRacesData()
convertedRaces.insertLast(null);
convertedRaceFileNames.insertLast("");
}

TerrainClass@ terrain = game.getTerrain();
projectName = generateSafeFileName(terrain.getTerrainName() + " [Races] ~"+thisScript);
}
//#endregion

Expand Down Expand Up @@ -470,7 +467,35 @@ void fixupTerrn2Document()
}
//#endregion

// nothing for STAGE_IDLE
// #region STAGE_BUTTON
void drawConvertButton()
{
TerrainClass@ terrain = game.getTerrain();
if (@terrain == null )
{
ImGui::Text("ERROR - No terrain loaded!");
return;
}

ImGui::SetNextItemWidth(375);
if (ImGui::InputText("Project name", projectName))
{
projectName = generateSafeFileName(projectName); // convert user input to filename
CacheEntryClass@ existingEntry = modcache.findEntryByFilename(LOADER_TYPE_TERRAIN, /*partial:*/false, projectName+'.terrn2');
nameAlreadyExists = (@existingEntry != null);
}

if (nameAlreadyExists)
{
ImGui::TextColored(color(0.6, 0.8, 0.1, 1.0), "Name already exists");
}
else if (ImGui::Button("Import terrain as project & convert races from script to terrn2 [Races]"))
{
updateTerrainNameInDocument(); // update terrn2 name from user input
stage = STAGE_PUSHMSG;
}
}
//#endregion

//#region STAGE_PUSHMSG
void pushMsgRequestCreateProject()
Expand Down Expand Up @@ -659,7 +684,7 @@ void advanceImportOneStep()
case STAGE_FIXTERRN2:
{
fixupTerrn2Document();
if (stage != STAGE_ERROR) { stage = STAGE_IDLE; }
if (stage != STAGE_ERROR) { stage = STAGE_BUTTON; }
break;
}

Expand Down

0 comments on commit 7fb1b9e

Please sign in to comment.