Merge pull request #7270 from furo321/editor-export-sound

Add export button to sounds
This commit is contained in:
Robert Müller 2023-09-30 16:10:20 +00:00 committed by GitHub
commit d55822bc4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 3 deletions

View file

@ -815,7 +815,7 @@ bool CEditor::CallbackSaveImage(const char *pFileName, int StorageType, void *pU
OutputFormat = IMAGE_FORMAT_R;
break;
default:
pEditor->ShowFileDialogError("Image has invalid format.");
dbg_assert(false, "Image has invalid format.");
return false;
};
@ -841,6 +841,33 @@ bool CEditor::CallbackSaveImage(const char *pFileName, int StorageType, void *pU
}
}
bool CEditor::CallbackSaveSound(const char *pFileName, int StorageType, void *pUser)
{
dbg_assert(StorageType == IStorage::TYPE_SAVE, "Saving only allowed for IStorage::TYPE_SAVE");
CEditor *pEditor = static_cast<CEditor *>(pUser);
char aBuf[IO_MAX_PATH_LENGTH];
// add file extension
if(!str_endswith(pFileName, ".opus"))
{
str_format(aBuf, sizeof(aBuf), "%s.opus", pFileName);
pFileName = aBuf;
}
std::shared_ptr<CEditorSound> pSound = pEditor->m_Map.m_vpSounds[pEditor->m_SelectedSound];
IOHANDLE File = pEditor->Storage()->OpenFile(pFileName, IOFLAG_WRITE, StorageType);
if(File)
{
io_write(File, pSound->m_pData, pSound->m_DataSize);
io_close(File);
pEditor->m_Dialog = DIALOG_NONE;
return true;
}
pEditor->ShowFileDialogError("Failed to open file '%s'.", pFileName);
return false;
}
void CEditor::DoToolbarLayers(CUIRect ToolBar)
{
const bool ModPressed = Input()->ModifierIsPressed();
@ -4287,7 +4314,7 @@ void CEditor::RenderSounds(CUIRect ToolBox)
if(Result == 2)
{
static SPopupMenuId s_PopupSoundId;
UI()->DoPopupMenu(&s_PopupSoundId, UI()->MouseX(), UI()->MouseY(), 120, 56, this, PopupSound);
UI()->DoPopupMenu(&s_PopupSoundId, UI()->MouseX(), UI()->MouseY(), 120, 73, this, PopupSound);
}
}
}
@ -4799,8 +4826,10 @@ void CEditor::RenderFileDialog()
m_PopupEventType = POPEVENT_SAVE;
else if(m_pfnFileDialogFunc == &CallbackSaveCopyMap)
m_PopupEventType = POPEVENT_SAVE_COPY;
else
else if(m_pfnFileDialogFunc == &CallbackSaveImage)
m_PopupEventType = POPEVENT_SAVE_IMG;
else
m_PopupEventType = POPEVENT_SAVE_SOUND;
m_PopupEventActivated = true;
}
else if(m_pfnFileDialogFunc)

View file

@ -995,6 +995,7 @@ public:
POPEVENT_SAVE,
POPEVENT_SAVE_COPY,
POPEVENT_SAVE_IMG,
POPEVENT_SAVE_SOUND,
POPEVENT_LARGELAYER,
POPEVENT_PREVENTUNUSEDTILES,
POPEVENT_IMAGEDIV16,
@ -1288,6 +1289,7 @@ public:
static bool CallbackSaveCopyMap(const char *pFileName, int StorageType, void *pUser);
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);
void PopupSelectImageInvoke(int Current, float x, float y);
int PopupSelectImageResult();

View file

@ -1546,6 +1546,7 @@ CUI::EPopupMenuFunctionResult CEditor::PopupImage(void *pContext, CUIRect View,
if(pEditor->DoButton_MenuItem(&s_ExportButton, "Export", 0, &Slot, 0, "Export the image"))
{
pEditor->InvokeFileDialog(IStorage::TYPE_SAVE, FILETYPE_IMG, "Save image", "Save", "mapres", false, CallbackSaveImage, pEditor);
pEditor->m_FileDialogFileNameInput.Set(pImg->m_aName);
return CUI::POPUP_CLOSE_CURRENT;
}
}
@ -1560,6 +1561,7 @@ CUI::EPopupMenuFunctionResult CEditor::PopupSound(void *pContext, CUIRect View,
static int s_ReaddButton = 0;
static int s_ReplaceButton = 0;
static int s_RemoveButton = 0;
static int s_ExportButton = 0;
CUIRect Slot;
View.HSplitTop(12.0f, &Slot, &View);
@ -1615,6 +1617,15 @@ CUI::EPopupMenuFunctionResult CEditor::PopupSound(void *pContext, CUIRect View,
return CUI::POPUP_CLOSE_CURRENT;
}
View.HSplitTop(5.0f, nullptr, &View);
View.HSplitTop(12.0f, &Slot, &View);
if(pEditor->DoButton_MenuItem(&s_ExportButton, "Export", 0, &Slot, 0, "Export sound"))
{
pEditor->InvokeFileDialog(IStorage::TYPE_SAVE, FILETYPE_SOUND, "Save sound", "Save", "mapres", false, CallbackSaveSound, pEditor);
pEditor->m_FileDialogFileNameInput.Set(pSound->m_aName);
return CUI::POPUP_CLOSE_CURRENT;
}
return CUI::POPUP_KEEP_OPEN;
}
@ -1767,6 +1778,11 @@ CUI::EPopupMenuFunctionResult CEditor::PopupEvent(void *pContext, CUIRect View,
pTitle = "Save image";
pMessage = "The file already exists.\n\nDo you want to overwrite the image?";
}
else if(pEditor->m_PopupEventType == POPEVENT_SAVE_SOUND)
{
pTitle = "Save sound";
pMessage = "The file already exists.\n\nDo you want to overwrite the sound?";
}
else if(pEditor->m_PopupEventType == POPEVENT_LARGELAYER)
{
pTitle = "Large layer";
@ -1891,6 +1907,11 @@ CUI::EPopupMenuFunctionResult CEditor::PopupEvent(void *pContext, CUIRect View,
CallbackSaveImage(pEditor->m_aFileSaveName, IStorage::TYPE_SAVE, pEditor);
return CUI::POPUP_CLOSE_CURRENT;
}
else if(pEditor->m_PopupEventType == POPEVENT_SAVE_SOUND)
{
CallbackSaveSound(pEditor->m_aFileSaveName, IStorage::TYPE_SAVE, pEditor);
return CUI::POPUP_CLOSE_CURRENT;
}
else if(pEditor->m_PopupEventType == POPEVENT_PLACE_BORDER_TILES)
{
pEditor->PlaceBorderTiles();