Skip to content

Commit

Permalink
Merge pull request ddnet#7356 from dobrykafe/pr-editor-stop-sound-pre…
Browse files Browse the repository at this point in the history
…view

Editor: stop/pause sound previews
  • Loading branch information
Robyt3 authored Oct 23, 2023
2 parents 7a2004e + 35e5c11 commit fec7e89
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/engine/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2983,7 +2983,11 @@ void CClient::Run()
g_Config.m_DbgGraphs ^= 1;

if(CtrlShiftKey(KEY_E, LastE))
{
if(g_Config.m_ClEditor)
m_pEditor->OnClose();
g_Config.m_ClEditor = g_Config.m_ClEditor ^ 1;
}

// render
{
Expand Down
2 changes: 2 additions & 0 deletions src/engine/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class IEditor : public IInterface
virtual void OnRender() = 0;
virtual void OnActivate() = 0;
virtual void OnWindowResize() = 0;
virtual void OnClose() = 0;
virtual void OnDialogClose() = 0;
virtual bool HasUnsavedData() const = 0;
virtual bool HandleMapDrop(const char *pFilename, int StorageType) = 0;
virtual bool Load(const char *pFilename, int StorageType) = 0;
Expand Down
40 changes: 39 additions & 1 deletion src/game/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ bool CEditor::CallbackSaveSound(const char *pFileName, int StorageType, void *pU
{
io_write(File, pSound->m_pData, pSound->m_DataSize);
io_close(File);
pEditor->OnDialogClose();
pEditor->m_Dialog = DIALOG_NONE;
return true;
}
Expand All @@ -901,9 +902,16 @@ void CEditor::DoAudioPreview(CUIRect View, const void *pPlayPauseButtonID, const
(m_Dialog == DIALOG_NONE && CLineInput::GetActiveInput() == nullptr && Input()->KeyPress(KEY_SPACE)))
{
if(Sound()->IsPlaying(SampleID))
{
Sound()->Pause(SampleID);
}
else
{
if(SampleID != m_ToolbarPreviewSound && m_ToolbarPreviewSound && Sound()->IsPlaying(m_ToolbarPreviewSound))
Sound()->Pause(m_ToolbarPreviewSound);

Sound()->Play(CSounds::CHN_GUI, SampleID, ISound::FLAG_PREVIEW);
}
}
}
// stop button
Expand Down Expand Up @@ -1343,10 +1351,15 @@ void CEditor::DoToolbarSounds(CUIRect ToolBar)
if(m_SelectedSound >= 0 && (size_t)m_SelectedSound < m_Map.m_vpSounds.size())
{
const std::shared_ptr<CEditorSound> pSelectedSound = m_Map.m_vpSounds[m_SelectedSound];
if(pSelectedSound->m_SoundID != m_ToolbarPreviewSound && m_ToolbarPreviewSound && Sound()->IsPlaying(m_ToolbarPreviewSound))
Sound()->Stop(m_ToolbarPreviewSound);
m_ToolbarPreviewSound = pSelectedSound->m_SoundID;

static int s_PlayPauseButton, s_StopButton, s_SeekBar = 0;
DoAudioPreview(ToolBarBottom, &s_PlayPauseButton, &s_StopButton, &s_SeekBar, pSelectedSound->m_SoundID);
DoAudioPreview(ToolBarBottom, &s_PlayPauseButton, &s_StopButton, &s_SeekBar, m_ToolbarPreviewSound);
}
else
m_ToolbarPreviewSound = -1;
}

static void Rotate(const CPoint *pCenter, CPoint *pPoint, float Rotation)
Expand Down Expand Up @@ -4062,6 +4075,7 @@ bool CEditor::AddSound(const char *pFileName, int StorageType, void *pUser)
}
}

pEditor->OnDialogClose();
pEditor->m_Dialog = DIALOG_NONE;
return true;
}
Expand Down Expand Up @@ -4113,6 +4127,7 @@ bool CEditor::ReplaceSound(const char *pFileName, int StorageType, bool CheckDup
pSound->m_pData = pData;
pSound->m_DataSize = DataSize;

OnDialogClose();
m_Dialog = DIALOG_NONE;
return true;
}
Expand Down Expand Up @@ -4913,7 +4928,10 @@ void CEditor::RenderFileDialog()
ButtonBar.VSplitRight(ButtonSpacing, &ButtonBar, nullptr);
ButtonBar.VSplitRight(50.0f, &ButtonBar, &Button);
if(DoButton_Editor(&s_CancelButton, "Cancel", 0, &Button, 0, nullptr) || (s_ListBox.Active() && UI()->ConsumeHotkey(CUI::HOTKEY_ESCAPE)))
{
OnDialogClose();
m_Dialog = DIALOG_NONE;
}

ButtonBar.VSplitRight(ButtonSpacing, &ButtonBar, nullptr);
ButtonBar.VSplitRight(50.0f, &ButtonBar, &Button);
Expand Down Expand Up @@ -7157,7 +7175,10 @@ void CEditor::RenderMenubar(CUIRect MenuBar)

static int s_CloseButton = 0;
if(DoButton_Editor(&s_CloseButton, "×", 0, &Close, 0, "Exits from the editor") || (m_Dialog == DIALOG_NONE && !UI()->IsPopupOpen() && !m_PopupEventActivated && Input()->KeyPress(KEY_ESCAPE)))
{
OnClose();
g_Config.m_ClEditor = 0;
}
}

void CEditor::Render()
Expand Down Expand Up @@ -7911,6 +7932,23 @@ void CEditor::OnWindowResize()
UI()->OnWindowResize();
}

void CEditor::OnClose()
{
if(m_ToolbarPreviewSound && Sound()->IsPlaying(m_ToolbarPreviewSound))
Sound()->Pause(m_ToolbarPreviewSound);
if(m_FilePreviewSound && Sound()->IsPlaying(m_FilePreviewSound))
Sound()->Pause(m_FilePreviewSound);
}

void CEditor::OnDialogClose()
{
if(m_FilePreviewSound)
{
Sound()->UnloadSample(m_FilePreviewSound);
m_FilePreviewSound = -1;
}
}

void CEditor::LoadCurrentMap()
{
if(Load(m_pClient->GetCurrentMapPath(), IStorage::TYPE_SAVE))
Expand Down
6 changes: 6 additions & 0 deletions src/game/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ class CEditor : public IEditor
m_FilePreviewSound = -1;
m_FilePreviewState = PREVIEW_UNLOADED;

m_ToolbarPreviewSound = -1;

m_SelectEntitiesImage = "DDNet";

m_ResetZoomEnvelope = true;
Expand Down Expand Up @@ -416,6 +418,8 @@ class CEditor : public IEditor
void OnRender() override;
void OnActivate() override;
void OnWindowResize() override;
void OnClose() override;
void OnDialogClose() override;
bool HasUnsavedData() const override { return m_Map.m_Modified; }
void UpdateMentions() override { m_Mentions++; }
void ResetMentions() override { m_Mentions = 0; }
Expand Down Expand Up @@ -568,6 +572,8 @@ class CEditor : public IEditor
CImageInfo m_FilePreviewImageInfo;
bool m_FileDialogOpening;

int m_ToolbarPreviewSound;

struct CFilelistItem
{
char m_aFilename[IO_MAX_PATH_LENGTH];
Expand Down
4 changes: 4 additions & 0 deletions src/game/editor/popups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ CUI::EPopupMenuFunctionResult CEditor::PopupMenuFile(void *pContext, CUIRect Vie
pEditor->m_PopupEventActivated = true;
}
else
{
pEditor->OnClose();
g_Config.m_ClEditor = 0;
}
return CUI::POPUP_CLOSE_CURRENT;
}

Expand Down Expand Up @@ -1972,6 +1975,7 @@ CUI::EPopupMenuFunctionResult CEditor::PopupEvent(void *pContext, CUIRect View,
{
if(pEditor->m_PopupEventType == POPEVENT_EXIT)
{
pEditor->OnClose();
g_Config.m_ClEditor = 0;
}
else if(pEditor->m_PopupEventType == POPEVENT_LOAD)
Expand Down

0 comments on commit fec7e89

Please sign in to comment.