| 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::drawXYOsc() { |
| 24 |
if (nextWindow==GUI_WINDOW_XY_OSC) { |
| 25 |
xyOscOpen=true; |
| 26 |
ImGui::SetNextWindowFocus(); |
| 27 |
nextWindow=GUI_WINDOW_NOTHING; |
| 28 |
} |
| 29 |
if (!xyOscOpen) return; |
| 30 |
ImGui::SetNextWindowSizeConstraints(ImVec2(128.0f*dpiScale,128.0f*dpiScale),ImVec2(canvasW,canvasH)); |
| 31 |
bool noPadding=settings.oscTakesEntireWindow && !xyOscOptions; |
| 32 |
if (noPadding) { |
| 33 |
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding,ImVec2(0,0)); |
| 34 |
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,ImVec2(0,0)); |
| 35 |
ImGui::PushStyleVar(ImGuiStyleVar_ItemInnerSpacing,ImVec2(0,0)); |
| 36 |
} |
| 37 |
if (ImGui::Begin("Oscilloscope (X-Y)",&xyOscOpen,globalWinFlags,_("Oscilloscope (X-Y)"))) { |
| 38 |
if (xyOscOptions) { |
| 39 |
int xyOscXChannelP1=xyOscXChannel+1; |
| 40 |
int xyOscYChannelP1=xyOscYChannel+1; |
| 41 |
|
| 42 |
ImGui::Text(_("X Channel")); |
| 43 |
ImGui::SameLine(); |
| 44 |
if (ImGui::DragInt("##XChannel",&xyOscXChannelP1,1.0f,1,DIV_MAX_OUTPUTS)) { |
| 45 |
xyOscXChannel=MIN(MAX(xyOscXChannelP1,1),DIV_MAX_OUTPUTS)-1; |
| 46 |
} rightClickable |
| 47 |
ImGui::SameLine(); |
| 48 |
ImGui::Checkbox(_("Invert##X"),&xyOscXInvert); |
| 49 |
ImGui::Text(_("Y Channel")); |
| 50 |
ImGui::SameLine(); |
| 51 |
if (ImGui::DragInt("##YChannel",&xyOscYChannelP1,1.0f,1,DIV_MAX_OUTPUTS)) { |
| 52 |
xyOscYChannel=MIN(MAX(xyOscYChannelP1,1),DIV_MAX_OUTPUTS)-1; |
| 53 |
} rightClickable |
| 54 |
ImGui::SameLine(); |
| 55 |
ImGui::Checkbox(_("Invert##Y"),&xyOscYInvert); |
| 56 |
if (ImGui::SliderFloat(_("Zoom"),&xyOscZoom,0.5f,4.0f,"%.2fx")) { |
| 57 |
xyOscZoom=MAX(xyOscZoom,0.0f); |
| 58 |
} rightClickable |
| 59 |
if (ImGui::IsItemHovered()) { |
| 60 |
ImGui::SetTooltip("%.1fdB",20.0f*log10f(xyOscZoom)); |
| 61 |
} |
| 62 |
if (ImGui::IsItemClicked(ImGuiMouseButton_Middle)) { |
| 63 |
xyOscZoom=1.0f; |
| 64 |
} |
| 65 |
if (ImGui::SliderInt(_("Samples"),&xyOscSamples,2,32768)) { |
| 66 |
xyOscSamples=MIN(MAX(xyOscSamples,2),32768); |
| 67 |
} rightClickable |
| 68 |
if (ImGui::SliderFloat(_("Decay Time (ms)"),&xyOscDecayTime,1.0f,1000.0f,"%.1f",ImGuiSliderFlags_Logarithmic)) { |
| 69 |
xyOscDecayTime=MAX(xyOscDecayTime,0.0f); |
| 70 |
} rightClickable |
| 71 |
if (ImGui::SliderFloat(_("Intensity"),&xyOscIntensity,0.0f,5.0f,"%.2f")) { |
| 72 |
xyOscIntensity=MAX(xyOscIntensity,0.0f); |
| 73 |
} rightClickable |
| 74 |
if (ImGui::SliderFloat(_("Line Thickness"),&xyOscThickness,0.0f,10.0f,"%.2f")) { |
| 75 |
xyOscThickness=MAX(xyOscThickness,0.0f); |
| 76 |
} rightClickable |
| 77 |
if (ImGui::Button(_("OK"))) { |
| 78 |
xyOscOptions=false; |
| 79 |
} |
| 80 |
} else { |
| 81 |
ImDrawList* dl=ImGui::GetWindowDrawList(); |
| 82 |
ImGuiWindow* window=ImGui::GetCurrentWindow(); |
| 83 |
ImVec2 size=ImGui::GetContentRegionAvail(); |
| 84 |
|
| 85 |
ImVec2 minArea=window->DC.CursorPos; |
| 86 |
ImVec2 maxArea=ImVec2( |
| 87 |
minArea.x+size.x, |
| 88 |
minArea.y+size.y |
| 89 |
); |
| 90 |
ImRect rect=ImRect(minArea,maxArea); |
| 91 |
ImRect inRect=rect; |
| 92 |
inRect.Min.x+=dpiScale; |
| 93 |
inRect.Min.y+=dpiScale; |
| 94 |
inRect.Max.x-=dpiScale; |
| 95 |
inRect.Max.y-=dpiScale; |
| 96 |
ImVec2 inSqrCenter=rect.GetCenter(); |
| 97 |
float inSqrLength; |
| 98 |
ImRect inSqr=inRect; |
| 99 |
if (rect.GetWidth() > rect.GetHeight()) { |
| 100 |
inSqrLength=inSqr.GetHeight()/2; |
| 101 |
inSqr.Min.x=inSqrCenter.x-inSqrLength; |
| 102 |
inSqr.Max.x=inSqrCenter.x+inSqrLength; |
| 103 |
} else { |
| 104 |
inSqrLength=inSqr.GetWidth()/2; |
| 105 |
inSqr.Min.y=inSqrCenter.y-inSqrLength; |
| 106 |
inSqr.Max.y=inSqrCenter.y+inSqrLength; |
| 107 |
} |
| 108 |
float scaleX=xyOscZoom*inSqrLength*(xyOscXInvert?-1:1); |
| 109 |
float scaleY=xyOscZoom*inSqrLength*(xyOscYInvert?1:-1); |
| 110 |
const ImGuiStyle& style=ImGui::GetStyle(); |
| 111 |
ImU32 color=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_WAVE]); |
| 112 |
color&=~IM_COL32_A_MASK; |
| 113 |
ImU32 borderColor=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BORDER]); |
| 114 |
ImU32 refColor=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_REF]); |
| 115 |
ImU32 guideColor=ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_GUIDE]); |
| 116 |
ImGui::ItemSize(size,style.FramePadding.y); |
| 117 |
if (ImGui::ItemAdd(rect,ImGui::GetID("wsDisplay"))) { |
| 118 |
// background |
| 119 |
dl->AddRectFilledMultiColor( |
| 120 |
inRect.Min, |
| 121 |
inRect.Max, |
| 122 |
ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG1]), |
| 123 |
ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG2]), |
| 124 |
ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG4]), |
| 125 |
ImGui::GetColorU32(uiColors[GUI_COLOR_OSC_BG3]), |
| 126 |
settings.oscRoundedCorners?(8.0f*dpiScale):0.0f |
| 127 |
); |
| 128 |
|
| 129 |
// axis guides |
| 130 |
dl->AddLine( |
| 131 |
ImLerp(rect.Min,rect.Max,ImVec2(0.0f,0.5f)), |
| 132 |
ImLerp(rect.Min,rect.Max,ImVec2(1.0f,0.5f)), |
| 133 |
refColor, |
| 134 |
dpiScale |
| 135 |
); |
| 136 |
|
| 137 |
dl->AddLine( |
| 138 |
ImLerp(rect.Min,rect.Max,ImVec2(0.5f,0.0f)), |
| 139 |
ImLerp(rect.Min,rect.Max,ImVec2(0.5f,1.0f)), |
| 140 |
refColor, |
| 141 |
dpiScale |
| 142 |
); |
| 143 |
|
| 144 |
bool reflect=xyOscXInvert!=xyOscYInvert; |
| 145 |
dl->AddLine( |
| 146 |
ImLerp(inSqr.Min,inSqr.Max,ImVec2(0.0f,reflect?0.0f:1.0f)), |
| 147 |
ImLerp(inSqr.Min,inSqr.Max,ImVec2(1.0f,reflect?1.0f:0.0f)), |
| 148 |
refColor, |
| 149 |
dpiScale |
| 150 |
); |
| 151 |
|
| 152 |
for (int i=1; i<5; i++) { |
| 153 |
float ip = (float)i/8.f; |
| 154 |
dl->AddRect( |
| 155 |
ImLerp(inSqr.Min,inSqr.Max,ImVec2(0.5f-ip,0.5f-ip)), |
| 156 |
ImLerp(inSqr.Min,inSqr.Max,ImVec2(0.5f+ip,0.5f+ip)), |
| 157 |
guideColor, |
| 158 |
0.0f,0,dpiScale |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
// line |
| 163 |
const float* oscBufX=e->oscBuf[xyOscXChannel]; |
| 164 |
const float* oscBufY=e->oscBuf[xyOscYChannel]; |
| 165 |
if (oscBufX!=NULL && oscBufY!=NULL) { |
| 166 |
int pos=e->oscWritePos; |
| 167 |
float lx=inSqrCenter.x; |
| 168 |
float ly=inSqrCenter.y; |
| 169 |
float maxA=xyOscIntensity*256.f; |
| 170 |
float decay=exp2f(-1e3f/e->getAudioDescGot().rate/xyOscDecayTime); |
| 171 |
ImDrawListFlags prevFlags=dl->Flags; |
| 172 |
dl->Flags|=ImDrawFlags_RoundCornersNone; |
| 173 |
if (!settings.oscAntiAlias || safeMode) { |
| 174 |
dl->Flags&=~(ImDrawListFlags_AntiAliasedLines|ImDrawListFlags_AntiAliasedLinesUseTex); |
| 175 |
} |
| 176 |
if (settings.oscEscapesBoundary) { |
| 177 |
dl->PushClipRectFullScreen(); |
| 178 |
} |
| 179 |
for (int i=0; i<xyOscSamples; i++) { |
| 180 |
pos=(pos-1)&32767; |
| 181 |
float x=oscBufX[pos]*scaleX+inSqrCenter.x; |
| 182 |
float y=oscBufY[pos]*scaleY+inSqrCenter.y; |
| 183 |
if (i != 0) { |
| 184 |
float a=maxA/sqrtf((x-lx)*(x-lx)+(y-ly)*(y-ly)); |
| 185 |
if (a>=1) { |
| 186 |
a=MIN(a,255); |
| 187 |
dl->AddLine(ImVec2(lx,ly),ImVec2(x,y),(color|((ImU32)a<<IM_COL32_A_SHIFT)),xyOscThickness*dpiScale); |
| 188 |
} |
| 189 |
maxA*=decay; |
| 190 |
if (maxA<1) { |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
lx=x; |
| 195 |
ly=y; |
| 196 |
} |
| 197 |
if (settings.oscEscapesBoundary) { |
| 198 |
dl->PopClipRect(); |
| 199 |
} |
| 200 |
dl->Flags=prevFlags; |
| 201 |
} |
| 202 |
if (settings.oscBorder) { |
| 203 |
dl->AddRect(inRect.Min,inRect.Max,borderColor,settings.oscRoundedCorners?(8.0f*dpiScale):0.0f,0,1.5f*dpiScale); |
| 204 |
} |
| 205 |
} |
| 206 |
if (ImGui::IsItemHovered()) { |
| 207 |
float valX=20.0f*log10f(fabsf((ImGui::GetMousePos().x-inSqrCenter.x)/scaleX)); |
| 208 |
float valY=20.0f*log10f(fabsf((ImGui::GetMousePos().y-inSqrCenter.y)/scaleY)); |
| 209 |
if (valX<=-INFINITY && valY<=-INFINITY) { |
| 210 |
ImGui::SetTooltip(_("(-Infinity)dB,(-Infinity)dB")); |
| 211 |
} else if (valX<=-INFINITY) { |
| 212 |
ImGui::SetTooltip(_("(-Infinity)dB,%.1fdB"),valY); |
| 213 |
} else if (valY<=-INFINITY) { |
| 214 |
ImGui::SetTooltip(_("%.1fdB,(-Infinity)dB"),valY); |
| 215 |
} else { |
| 216 |
ImGui::SetTooltip(_("%.1fdB,%.1fdB"),valX,valY); |
| 217 |
} |
| 218 |
} |
| 219 |
if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) { |
| 220 |
xyOscOptions=true; |
| 221 |
} |
| 222 |
if (ImGui::IsItemHovered() && CHECK_LONG_HOLD) { |
| 223 |
NOTIFY_LONG_HOLD; |
| 224 |
xyOscOptions=true; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
if (noPadding) { |
| 229 |
ImGui::PopStyleVar(3); |
| 230 |
} |
| 231 |
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) curWindow=GUI_WINDOW_XY_OSC; |
| 232 |
ImGui::End(); |
| 233 |
} |