From 5b609c8e06f85f21e8a51c74c222a27d6c133389 Mon Sep 17 00:00:00 2001 From: dobrykafe <121701317+dobrykafe@users.noreply.github.com> Date: Thu, 5 Oct 2023 21:28:58 +0200 Subject: [PATCH] extract `CEditor::DoAudioPreview`, also use in file dialog --- src/game/editor/editor.cpp | 214 ++++++++++++++++++------------------- src/game/editor/editor.h | 2 + 2 files changed, 105 insertions(+), 111 deletions(-) diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index a67e0140d..ba7465117 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -891,6 +891,104 @@ bool CEditor::CallbackSaveSound(const char *pFileName, int StorageType, void *pU return false; } +void CEditor::DoAudioPreview(CUIRect View, const void *pPlayPauseButtonID, const void *pStopButtonID, const void *pSeekBarID, const int SampleID) +{ + CUIRect Button, SeekBar; + // play/pause button + { + View.VSplitLeft(View.h, &Button, &View); + if(DoButton_FontIcon(pPlayPauseButtonID, Sound()->IsPlaying(SampleID) ? FONT_ICON_PAUSE : FONT_ICON_PLAY, 0, &Button, 0, "Play/pause audio preview", IGraphics::CORNER_ALL) || + (m_Dialog == DIALOG_NONE && CLineInput::GetActiveInput() == nullptr && Input()->KeyPress(KEY_SPACE))) + { + if(Sound()->IsPlaying(SampleID)) + Sound()->Pause(SampleID); + else + Sound()->Play(CSounds::CHN_GUI, SampleID, ISound::FLAG_PREVIEW); + } + } + // stop button + { + View.VSplitLeft(2.0f, nullptr, &View); + View.VSplitLeft(View.h, &Button, &View); + if(DoButton_FontIcon(pStopButtonID, FONT_ICON_STOP, 0, &Button, 0, "Stop audio preview", IGraphics::CORNER_ALL)) + { + Sound()->Stop(SampleID); + } + } + // do seekbar + { + View.VSplitLeft(5.0f, nullptr, &View); + const float Cut = std::min(View.w, 200.0f); + View.VSplitLeft(Cut, &SeekBar, &View); + SeekBar.HMargin(2.5f, &SeekBar); + + const float Rounding = 5.0f; + const void *pId = pSeekBarID; + + char aBuffer[64]; + const float CurrentTime = Sound()->GetSampleCurrentTime(SampleID); + const float TotalTime = Sound()->GetSampleTotalTime(SampleID); + + // draw seek bar + SeekBar.Draw(ColorRGBA(0, 0, 0, 0.5f), IGraphics::CORNER_ALL, Rounding); + + // draw filled bar + const float Amount = CurrentTime / TotalTime; + CUIRect FilledBar = SeekBar; + FilledBar.w = 2 * Rounding + (FilledBar.w - 2 * Rounding) * Amount; + FilledBar.Draw(ColorRGBA(1, 1, 1, 0.5f), IGraphics::CORNER_ALL, Rounding); + + // draw time + char aCurrentTime[32]; + str_time_float(CurrentTime, TIME_HOURS, aCurrentTime, sizeof(aCurrentTime)); + char aTotalTime[32]; + str_time_float(TotalTime, TIME_HOURS, aTotalTime, sizeof(aTotalTime)); + str_format(aBuffer, sizeof(aBuffer), "%s / %s", aCurrentTime, aTotalTime); + UI()->DoLabel(&SeekBar, aBuffer, SeekBar.h * 0.70f, TEXTALIGN_MC); + + // do the logic + const bool Inside = UI()->MouseInside(&SeekBar); + + if(UI()->CheckActiveItem(pId)) + { + if(!UI()->MouseButton(0)) + UI()->SetActiveItem(nullptr); + else + { + float s_PrevAmount = 0.0f; + float AmountSeek = clamp((UI()->MouseX() - SeekBar.x - Rounding) / (float)(SeekBar.w - 2 * Rounding), 0.0f, 1.0f); + + if(Input()->ShiftIsPressed()) + { + AmountSeek = s_PrevAmount + (AmountSeek - s_PrevAmount) * 0.05f; + if(AmountSeek >= 0.0f && AmountSeek <= 1.0f && absolute(s_PrevAmount - AmountSeek) >= 0.0001f) + { + Sound()->SetSampleCurrentTime(SampleID, AmountSeek); + } + } + else + { + if(AmountSeek >= 0.0f && AmountSeek <= 1.0f && absolute(s_PrevAmount - AmountSeek) >= 0.001f) + { + s_PrevAmount = AmountSeek; + Sound()->SetSampleCurrentTime(SampleID, AmountSeek); + } + } + } + } + else if(UI()->HotItem() == pId) + { + if(UI()->MouseButton(0)) + { + UI()->SetActiveItem(pId); + } + } + + if(Inside) + UI()->SetHotItem(pId); + } +} + void CEditor::DoToolbarLayers(CUIRect ToolBar) { const bool ModPressed = Input()->ModifierIsPressed(); @@ -1257,107 +1355,15 @@ void CEditor::DoToolbarLayers(CUIRect ToolBar) void CEditor::DoToolbarSounds(CUIRect ToolBar) { - CUIRect ToolBarTop, ToolBarBottom, Button, SeekBar; + CUIRect ToolBarTop, ToolBarBottom; ToolBar.HSplitMid(&ToolBarTop, &ToolBarBottom, 5.0f); if(m_SelectedSound >= 0 && (size_t)m_SelectedSound < m_Map.m_vpSounds.size()) { const std::shared_ptr pSelectedSound = m_Map.m_vpSounds[m_SelectedSound]; - // play/pause button - { - ToolBarBottom.VSplitLeft(ToolBarBottom.h, &Button, &ToolBarBottom); - static int s_PlayPauseButton; - if(DoButton_FontIcon(&s_PlayPauseButton, Sound()->IsPlaying(pSelectedSound->m_SoundID) ? FONT_ICON_PAUSE : FONT_ICON_PLAY, 0, &Button, 0, "Play/pause audio preview", IGraphics::CORNER_ALL) || - (m_Dialog == DIALOG_NONE && CLineInput::GetActiveInput() == nullptr && Input()->KeyPress(KEY_SPACE))) - { - if(Sound()->IsPlaying(pSelectedSound->m_SoundID)) - Sound()->Pause(pSelectedSound->m_SoundID); - else - Sound()->Play(CSounds::CHN_GUI, pSelectedSound->m_SoundID, ISound::FLAG_PREVIEW); - } - } - // stop button - { - ToolBarBottom.VSplitLeft(2.0f, nullptr, &ToolBarBottom); - ToolBarBottom.VSplitLeft(ToolBarBottom.h, &Button, &ToolBarBottom); - static int s_StopButton; - if(DoButton_FontIcon(&s_StopButton, FONT_ICON_STOP, 0, &Button, 0, "Stop audio preview", IGraphics::CORNER_ALL)) - { - Sound()->Stop(pSelectedSound->m_SoundID); - } - } - // do seekbar - { - ToolBarBottom.VSplitLeft(5.0f, nullptr, &ToolBarBottom); - ToolBarBottom.VSplitLeft(200.0f, &SeekBar, &ToolBarBottom); - const float Rounding = 5.0f; - - static int s_SeekBarID = 0; - void *pId = &s_SeekBarID; - - char aBuffer[64]; - float CurrentTime = Sound()->GetSampleCurrentTime(pSelectedSound->m_SoundID); - float TotalTime = Sound()->GetSampleTotalTime(pSelectedSound->m_SoundID); - - // draw seek bar - SeekBar.Draw(ColorRGBA(0, 0, 0, 0.5f), IGraphics::CORNER_ALL, Rounding); - - // draw filled bar - float Amount = CurrentTime / TotalTime; - CUIRect FilledBar = SeekBar; - FilledBar.w = 2 * Rounding + (FilledBar.w - 2 * Rounding) * Amount; - FilledBar.Draw(ColorRGBA(1, 1, 1, 0.5f), IGraphics::CORNER_ALL, Rounding); - - // draw time - char aCurrentTime[32]; - str_time_float(CurrentTime, TIME_HOURS, aCurrentTime, sizeof(aCurrentTime)); - char aTotalTime[32]; - str_time_float(TotalTime, TIME_HOURS, aTotalTime, sizeof(aTotalTime)); - str_format(aBuffer, sizeof(aBuffer), "%s / %s", aCurrentTime, aTotalTime); - UI()->DoLabel(&SeekBar, aBuffer, SeekBar.h * 0.70f, TEXTALIGN_MC); - - // do the logic - const bool Inside = UI()->MouseInside(&SeekBar); - - if(UI()->CheckActiveItem(pId)) - { - if(!UI()->MouseButton(0)) - UI()->SetActiveItem(nullptr); - else - { - static float s_PrevAmount = 0.0f; - float AmountSeek = clamp((UI()->MouseX() - SeekBar.x - Rounding) / (float)(SeekBar.w - 2 * Rounding), 0.0f, 1.0f); - - if(Input()->ShiftIsPressed()) - { - AmountSeek = s_PrevAmount + (AmountSeek - s_PrevAmount) * 0.05f; - if(AmountSeek >= 0.0f && AmountSeek <= 1.0f && absolute(s_PrevAmount - AmountSeek) >= 0.0001f) - { - Sound()->SetSampleCurrentTime(pSelectedSound->m_SoundID, AmountSeek); - } - } - else - { - if(AmountSeek >= 0.0f && AmountSeek <= 1.0f && absolute(s_PrevAmount - AmountSeek) >= 0.001f) - { - s_PrevAmount = AmountSeek; - Sound()->SetSampleCurrentTime(pSelectedSound->m_SoundID, AmountSeek); - } - } - } - } - else if(UI()->HotItem() == pId) - { - if(UI()->MouseButton(0)) - { - UI()->SetActiveItem(pId); - } - } - - if(Inside) - UI()->SetHotItem(pId); - } + static int s_PlayPauseButton, s_StopButton, s_SeekBar = 0; + DoAudioPreview(ToolBarBottom, &s_PlayPauseButton, &s_StopButton, &s_SeekBar, pSelectedSound->m_SoundID); } } @@ -4746,25 +4752,11 @@ void CEditor::RenderFileDialog() Preview.Margin(10.0f, &Preview); if(m_FilePreviewState == PREVIEW_LOADED) { - CUIRect Button; Preview.HSplitTop(20.0f, &Preview, nullptr); - Preview.VSplitLeft(Preview.h, &Button, &Preview); Preview.VSplitLeft(Preview.h / 4.0f, nullptr, &Preview); - static int s_PlayStopButton; - if(DoButton_FontIcon(&s_PlayStopButton, Sound()->IsPlaying(m_FilePreviewSound) ? FONT_ICON_STOP : FONT_ICON_PLAY, 0, &Button, 0, "Play/stop audio preview", IGraphics::CORNER_ALL)) - { - if(Sound()->IsPlaying(m_FilePreviewSound)) - Sound()->Stop(m_FilePreviewSound); - else - Sound()->Play(CSounds::CHN_GUI, m_FilePreviewSound, 0); - } - - char aTotalTime[32]; - char aTotalTimeLabel[64]; - str_time_float(Sound()->GetSampleTotalTime(m_FilePreviewSound), TIME_HOURS, aTotalTime, sizeof(aTotalTime)); - str_format(aTotalTimeLabel, sizeof(aTotalTimeLabel), "Duration: %s", aTotalTime); - UI()->DoLabel(&Preview, aTotalTimeLabel, 12.0f, TEXTALIGN_ML); + static int s_PlayPauseButton, s_StopButton, s_SeekBar = 0; + DoAudioPreview(Preview, &s_PlayPauseButton, &s_StopButton, &s_SeekBar, m_FilePreviewSound); } else if(m_FilePreviewState == PREVIEW_ERROR) { diff --git a/src/game/editor/editor.h b/src/game/editor/editor.h index 62f80048c..8e3946f8f 100644 --- a/src/game/editor/editor.h +++ b/src/game/editor/editor.h @@ -875,6 +875,8 @@ public: void SortImages(); bool SelectLayerByTile(); + void DoAudioPreview(CUIRect View, const void *pPlayPauseButtonID, const void *pStopButtonID, const void *pSeekBarID, const int SampleID); + // Tile Numbers For Explanations - TODO: Add/Improve tiles and explanations enum {