Skip to content

Commit

Permalink
Merge pull request ddnet#9053 from KebsCS/pr-custom-editor-entities
Browse files Browse the repository at this point in the history
Add custom editor entities
  • Loading branch information
Robyt3 authored Sep 28, 2024
2 parents 2a64dac + 486de46 commit 4031cd1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/game/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,37 @@ bool CEditor::CallbackSaveSound(const char *pFileName, int StorageType, void *pU
return false;
}

bool CEditor::CallbackCustomEntities(const char *pFileName, int StorageType, void *pUser)
{
CEditor *pEditor = (CEditor *)pUser;

char aBuf[IO_MAX_PATH_LENGTH];
IStorage::StripPathAndExtension(pFileName, aBuf, sizeof(aBuf));

if(std::find(pEditor->m_vSelectEntitiesFiles.begin(), pEditor->m_vSelectEntitiesFiles.end(), std::string(aBuf)) != pEditor->m_vSelectEntitiesFiles.end())
{
pEditor->ShowFileDialogError("Custom entities cannot have the same name as default entities.");
return false;
}

CImageInfo ImgInfo;
if(!pEditor->Graphics()->LoadPng(ImgInfo, pFileName, StorageType))
{
pEditor->ShowFileDialogError("Failed to load image from file '%s'.", pFileName);
return false;
}

pEditor->m_SelectEntitiesImage = aBuf;
pEditor->m_AllowPlaceUnusedTiles = -1;
pEditor->m_PreventUnusedTilesWasWarned = false;

pEditor->Graphics()->UnloadTexture(&pEditor->m_EntitiesTexture);
pEditor->m_EntitiesTexture = pEditor->Graphics()->LoadTextureRawMove(ImgInfo, pEditor->GetTextureUsageFlag());

pEditor->m_Dialog = DIALOG_NONE;
return true;
}

void CEditor::DoAudioPreview(CUIRect View, const void *pPlayPauseButtonId, const void *pStopButtonId, const void *pSeekBarId, int SampleId)
{
CUIRect Button, SeekBar;
Expand Down
1 change: 1 addition & 0 deletions src/game/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ class CEditor : public IEditor
static bool CallbackAddTileart(const char *pFilepath, int StorageType, void *pUser);
static bool CallbackSaveImage(const char *pFileName, int StorageType, void *pUser);
static bool CallbackSaveSound(const char *pFileName, int StorageType, void *pUser);
static bool CallbackCustomEntities(const char *pFileName, int StorageType, void *pUser);

void PopupSelectImageInvoke(int Current, float x, float y);
int PopupSelectImageResult();
Expand Down
10 changes: 8 additions & 2 deletions src/game/editor/popups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ CUi::EPopupMenuFunctionResult CEditor::PopupMenuSettings(void *pContext, CUIRect
pEditor->m_vSelectEntitiesFiles.clear();
pEditor->Storage()->ListDirectory(IStorage::TYPE_ALL, "editor/entities", EntitiesListdirCallback, pEditor);
std::sort(pEditor->m_vSelectEntitiesFiles.begin(), pEditor->m_vSelectEntitiesFiles.end());
pEditor->m_vSelectEntitiesFiles.emplace_back("Custom…");

static SPopupMenuId s_PopupEntitiesId;
pEditor->Ui()->DoPopupMenu(&s_PopupEntitiesId, Slot.x, Slot.y + Slot.h, 250, pEditor->m_vSelectEntitiesFiles.size() * 14.0f + 10.0f, pEditor, PopupEntities);
Expand Down Expand Up @@ -2767,12 +2768,17 @@ CUi::EPopupMenuFunctionResult CEditor::PopupEntities(void *pContext, CUIRect Vie
{
if(pEditor->m_vSelectEntitiesFiles[i] != pEditor->m_SelectEntitiesImage)
{
if(i == pEditor->m_vSelectEntitiesFiles.size() - 1)
{
pEditor->InvokeFileDialog(IStorage::TYPE_ALL, FILETYPE_IMG, "Custom Entities", "Load", "assets/entities", false, CallbackCustomEntities, pEditor);
return CUi::POPUP_CLOSE_CURRENT;
}

pEditor->m_SelectEntitiesImage = pEditor->m_vSelectEntitiesFiles[i];
pEditor->m_AllowPlaceUnusedTiles = pEditor->m_SelectEntitiesImage == "DDNet" ? 0 : -1;
pEditor->m_PreventUnusedTilesWasWarned = false;

if(pEditor->m_EntitiesTexture.IsValid())
pEditor->Graphics()->UnloadTexture(&pEditor->m_EntitiesTexture);
pEditor->Graphics()->UnloadTexture(&pEditor->m_EntitiesTexture);

char aBuf[IO_MAX_PATH_LENGTH];
str_format(aBuf, sizeof(aBuf), "editor/entities/%s.png", pName);
Expand Down

0 comments on commit 4031cd1

Please sign in to comment.