From e86174921ba4b889f8af4ee3671e280432359406 Mon Sep 17 00:00:00 2001 From: tildearrow Date: Wed, 13 Apr 2022 03:24:49 -0500 Subject: [PATCH] GUI: partially add a piano --- src/gui/gui.cpp | 5 ++- src/gui/gui.h | 6 +++ src/gui/pattern.cpp | 6 +++ src/gui/piano.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 106 insertions(+), 3 deletions(-) diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index dfa066f36..9787a4da6 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -3650,7 +3650,10 @@ FurnaceGUI::FurnaceGUI(): oscTotal(0), oscZoom(0.5f), oscZoomSlider(false), - followLog(true) { + followLog(true), + pianoOctaves(7), + pianoOptions(false), + pianoOffset(6) { // value keys valueKeys[SDLK_0]=0; valueKeys[SDLK_1]=1; diff --git a/src/gui/gui.h b/src/gui/gui.h index 84292825f..4b9ca05ad 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -1048,6 +1048,12 @@ class FurnaceGUI { // log window bool followLog; + // piano + int pianoOctaves; + bool pianoOptions; + float pianoKeyHit[180]; + int pianoOffset; + void drawSSGEnv(unsigned char type, const ImVec2& size); void drawWaveform(unsigned char type, bool opz, const ImVec2& size); void drawAlgorithm(unsigned char alg, FurnaceGUIFMAlgs algType, const ImVec2& size); diff --git a/src/gui/pattern.cpp b/src/gui/pattern.cpp index de0e5a61f..166815145 100644 --- a/src/gui/pattern.cpp +++ b/src/gui/pattern.cpp @@ -494,6 +494,12 @@ void FurnaceGUI::drawPattern() { ImVec4 chanHeadHover=chanHead; if (e->keyHit[i]) { keyHit[i]=0.2; + if (!muted) { + int note=e->getChanState(i)->note+60; + if (note>=0 && note<180) { + pianoKeyHit[note]=1.0; + } + } e->keyHit[i]=false; } if (settings.guiColorsBase) { diff --git a/src/gui/piano.cpp b/src/gui/piano.cpp index aa18996f3..a4f0c64cb 100644 --- a/src/gui/piano.cpp +++ b/src/gui/piano.cpp @@ -19,6 +19,20 @@ #include "gui.h" #include "guiConst.h" +#include "imgui.h" +#include "imgui_internal.h" + +const float topKeyStarts[5]={ + 0.9f/7.0f, 2.1f/7.0f, 3.9f/7.0f, 5.0f/7.0f, 6.1f/7.0f +}; + +const int topKeyNotes[5]={ + 1, 3, 6, 8, 10 +}; + +const int bottomKeyNotes[7]={ + 0, 2, 4, 5, 7, 9, 11 +}; // TODO: actually implement a piano! void FurnaceGUI::drawPiano() { @@ -28,7 +42,81 @@ void FurnaceGUI::drawPiano() { nextWindow=GUI_WINDOW_NOTHING; } if (!pianoOpen) return; - if (ImGui::Begin("Piano",&pianoOpen)) { + if (ImGui::Begin("Piano",&pianoOpen,(pianoOptions)?0:ImGuiWindowFlags_NoTitleBar)) { + if (ImGui::BeginTable("PianoLayout",pianoOptions?2:1,ImGuiTableFlags_BordersInnerV)) { + if (pianoOptions) { + ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthFixed); + } + ImGui::TableSetupColumn("c1",ImGuiTableColumnFlags_WidthStretch); + + ImGui::TableNextRow(); + if (pianoOptions) { + ImGui::TableNextColumn(); + ImGui::Button("Options"); + } + + ImGui::TableNextColumn(); + ImGuiWindow* window=ImGui::GetCurrentWindow(); + ImVec2 size=ImGui::GetContentRegionAvail(); + ImDrawList* dl=ImGui::GetWindowDrawList(); + + ImVec2 minArea=window->DC.CursorPos; + ImVec2 maxArea=ImVec2( + minArea.x+size.x, + minArea.y+size.y + ); + ImRect rect=ImRect(minArea,maxArea); + + // render piano + //ImGui::ItemSize(size,ImGui::GetStyle().FramePadding.y); + if (ImGui::ItemAdd(rect,ImGui::GetID("pianoDisplay"))) { + int bottomNotes=7*pianoOctaves; + for (int i=0; i=180) continue; + float pkh=pianoKeyHit[note]*0.5; + ImVec4 color=ImVec4(1.0f-pkh,1.0f-pkh,1.0f-pkh,1.0f); + ImVec2 p0=ImLerp(rect.Min,rect.Max,ImVec2((float)i/bottomNotes,0.0f)); + ImVec2 p1=ImLerp(rect.Min,rect.Max,ImVec2((float)(i+1)/bottomNotes,1.0f)); + p1.x-=dpiScale; + dl->AddRectFilled(p0,p1,ImGui::ColorConvertFloat4ToU32(color)); + } + + for (int i=0; i=180) continue; + float pkh=pianoKeyHit[note]*0.5; + ImVec4 color=ImVec4(pkh,pkh,pkh,1.0f); + ImVec2 p0=ImLerp(op0,op1,ImVec2(topKeyStarts[j]-0.05f,0.0f)); + ImVec2 p1=ImLerp(op0,op1,ImVec2(topKeyStarts[j]+0.05f,0.64f)); + dl->AddRectFilled(p0,p1,0xff000000); + p0.x+=dpiScale; + p1.x-=dpiScale; + p1.y-=dpiScale; + dl->AddRectFilled(p0,p1,ImGui::ColorConvertFloat4ToU32(color)); + } + } + + const float reduction=ImGui::GetIO().DeltaTime*60.0f*0.12; + for (int i=0; i<180; i++) { + pianoKeyHit[i]-=reduction; + if (pianoKeyHit[i]<0) pianoKeyHit[i]=0; + } + } + + if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) { + pianoOptions=!pianoOptions; + } + + ImGui::EndTable(); + } + /* for (int i=0; igetTotalChannelCount(); i++) { DivChannelState* cs=e->getChanState(i); if (cs->keyOn) { @@ -40,7 +128,7 @@ void FurnaceGUI::drawPiano() { } ImGui::Text("%d: %s",i,noteName); } - } + }*/ } if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) curWindow=GUI_WINDOW_PIANO; ImGui::End();