| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "gui.h" |
| 21 |
#include "imgui_internal.h" |
| 22 |
|
| 23 |
void FurnaceGUI::drawVolMeter() { |
| 24 |
if (nextWindow==GUI_WINDOW_VOL_METER) { |
| 25 |
volMeterOpen=true; |
| 26 |
ImGui::SetNextWindowFocus(); |
| 27 |
nextWindow=GUI_WINDOW_NOTHING; |
| 28 |
} |
| 29 |
if (!volMeterOpen) return; |
| 30 |
if (--isClipping<0) isClipping=0; |
| 31 |
ImGui::SetNextWindowSizeConstraints(ImVec2(6.0f*dpiScale,6.0f*dpiScale),ImVec2(canvasW,canvasH)); |
| 32 |
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding,ImVec2(0,0)); |
| 33 |
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding,ImVec2(0,0)); |
| 34 |
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,ImVec2(0,0)); |
| 35 |
ImGui::PushStyleVar(ImGuiStyleVar_ItemInnerSpacing,ImVec2(0,0)); |
| 36 |
if (ImGui::Begin("Volume Meter",&volMeterOpen,globalWinFlags,_("Volume Meter"))) { |
| 37 |
ImDrawList* dl=ImGui::GetWindowDrawList(); |
| 38 |
bool aspectRatio=(ImGui::GetWindowSize().x/ImGui::GetWindowSize().y)>1.0; |
| 39 |
|
| 40 |
ImVec2 minArea=ImVec2( |
| 41 |
ImGui::GetWindowPos().x+ImGui::GetCursorPos().x, |
| 42 |
ImGui::GetWindowPos().y+ImGui::GetCursorPos().y |
| 43 |
); |
| 44 |
ImVec2 maxArea=ImVec2( |
| 45 |
ImGui::GetWindowPos().x+ImGui::GetCursorPos().x+ImGui::GetContentRegionAvail().x, |
| 46 |
ImGui::GetWindowPos().y+ImGui::GetCursorPos().y+ImGui::GetContentRegionAvail().y |
| 47 |
); |
| 48 |
ImRect rect=ImRect(minArea,maxArea); |
| 49 |
ImGuiStyle& style=ImGui::GetStyle(); |
| 50 |
ImGui::ItemSize(ImVec2(4.0f,4.0f),style.FramePadding.y); |
| 51 |
ImU32 lowColor=ImGui::GetColorU32(uiColors[GUI_COLOR_VOLMETER_LOW]); |
| 52 |
if (ImGui::ItemAdd(rect,ImGui::GetID("volMeter"))) { |
| 53 |
ImGui::RenderFrame(rect.Min,rect.Max,ImGui::GetColorU32(ImGuiCol_FrameBg),true,style.FrameRounding); |
| 54 |
int outChans=e->getAudioDescGot().outChans; |
| 55 |
for (int i=0; i<outChans; i++) { |
| 56 |
float logPeak=(20*log10(peak[i])/36.0); |
| 57 |
if (logPeak==NAN) logPeak=0.0; |
| 58 |
if (logPeak<-1.0) logPeak=-1.0; |
| 59 |
if (logPeak>0.0) { |
| 60 |
isClipping=8; |
| 61 |
logPeak=0.0; |
| 62 |
} |
| 63 |
logPeak+=1.0; |
| 64 |
ImU32 highColor=ImGui::GetColorU32( |
| 65 |
ImLerp(uiColors[GUI_COLOR_VOLMETER_LOW],uiColors[GUI_COLOR_VOLMETER_HIGH],logPeak) |
| 66 |
); |
| 67 |
ImRect s; |
| 68 |
if (aspectRatio) { |
| 69 |
s=ImRect( |
| 70 |
ImLerp(rect.Min,rect.Max,ImVec2(0,float(i)/outChans)), |
| 71 |
ImLerp(rect.Min,rect.Max,ImVec2(logPeak,float(i+1)/outChans)) |
| 72 |
); |
| 73 |
if (i!=(outChans-1)) s.Max.y-=dpiScale; |
| 74 |
if (isClipping) { |
| 75 |
dl->AddRectFilled(s.Min,s.Max,ImGui::GetColorU32(uiColors[GUI_COLOR_VOLMETER_PEAK])); |
| 76 |
} else { |
| 77 |
dl->AddRectFilledMultiColor(s.Min,s.Max,lowColor,highColor,highColor,lowColor); |
| 78 |
} |
| 79 |
} else { |
| 80 |
s=ImRect( |
| 81 |
ImLerp(rect.Min,rect.Max,ImVec2(float(i)/outChans,1.0-logPeak)), |
| 82 |
ImLerp(rect.Min,rect.Max,ImVec2(float(i+1)/outChans,1.0)) |
| 83 |
); |
| 84 |
if (i!=(outChans-1)) s.Max.x-=dpiScale; |
| 85 |
if (isClipping) { |
| 86 |
dl->AddRectFilled(s.Min,s.Max,ImGui::GetColorU32(uiColors[GUI_COLOR_VOLMETER_PEAK])); |
| 87 |
} else { |
| 88 |
dl->AddRectFilledMultiColor(s.Min,s.Max,highColor,highColor,lowColor,lowColor); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
if (ImGui::IsItemHovered()) { |
| 93 |
if (aspectRatio) { |
| 94 |
ImGui::SetTooltip("%.1fdB",36*((ImGui::GetMousePos().x-ImGui::GetItemRectMin().x)/(rect.Max.x-rect.Min.x)-1.0)); |
| 95 |
} else { |
| 96 |
ImGui::SetTooltip("%.1fdB",-(36+36*((ImGui::GetMousePos().y-ImGui::GetItemRectMin().y)/(rect.Max.y-rect.Min.y)-1.0))); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
ImGui::PopStyleVar(4); |
| 102 |
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) curWindow=GUI_WINDOW_VOL_METER; |
| 103 |
ImGui::End(); |
| 104 |
} |