GUI: add touch input primitives

This commit is contained in:
tildearrow 2022-05-17 12:46:52 -05:00
parent 68c32749ff
commit f96d5b4e81
4 changed files with 262 additions and 29 deletions

View File

@ -240,6 +240,27 @@ void FurnaceGUI::drawDebug() {
}
ImGui::TreePop();
}
if (ImGui::TreeNode("Touch Point Information")) {
ImGui::Text("active:");
ImGui::Indent();
for (TouchPoint& i: activePoints) {
ImGui::Text("- %d: %.1f, %.1f (%.2f)",i.id,i.x,i.y,i.x);
}
ImGui::Unindent();
ImGui::Text("pressed:");
ImGui::Indent();
for (TouchPoint& i: pressedPoints) {
ImGui::Text("- %d: %.1f, %.1f (%.2f)",i.id,i.x,i.y,i.x);
}
ImGui::Unindent();
ImGui::Text("released:");
ImGui::Indent();
for (TouchPoint& i: releasedPoints) {
ImGui::Text("- %d: %.1f, %.1f (%.2f)",i.id,i.x,i.y,i.x);
}
ImGui::Unindent();
ImGui::TreePop();
}
if (ImGui::TreeNode("Playground")) {
if (pgSys<0 || pgSys>=e->song.systemLen) pgSys=0;
if (ImGui::BeginCombo("System",fmt::sprintf("%d. %s",pgSys+1,e->getSystemName(e->song.system[pgSys])).c_str())) {

View File

@ -2210,6 +2210,11 @@ int _processEvent(void* instance, SDL_Event* event) {
}
int FurnaceGUI::processEvent(SDL_Event* ev) {
#ifdef IS_MOBILE
if (ev->type==SDL_APP_WILLENTERBACKGROUND) {
// TODO: save "last state" and potentially suspend engine
}
#endif
if (ev->type==SDL_KEYDOWN) {
if (!ev->key.repeat && latchTarget==0 && !wantCaptureKeyboard && (ev->key.keysym.mod&(~(KMOD_NUM|KMOD_CAPS|KMOD_SCROLL)))==0) {
if (settings.notePreviewBehavior==0) return 1;
@ -2285,6 +2290,95 @@ int FurnaceGUI::processEvent(SDL_Event* ev) {
return 1;
}
#define FIND_POINT(p,pid) \
for (TouchPoint& i: activePoints) { \
if (i.id==pid) { \
p=&i; \
} \
}
void FurnaceGUI::processPoint(SDL_Event& ev) {
switch (ev.type) {
case SDL_MOUSEMOTION: {
TouchPoint* point=NULL;
FIND_POINT(point,-1);
if (point!=NULL) {
point->x=ev.motion.x;
point->y=ev.motion.y;
#ifdef __APPLE__
point->x*=dpiScale;
point->y*=dpiScale;
#endif
}
break;
}
case SDL_MOUSEBUTTONDOWN: {
for (size_t i=0; i<activePoints.size(); i++) {
TouchPoint& point=activePoints[i];
if (point.id==-1) {
releasedPoints.push_back(point);
activePoints.erase(activePoints.begin()+i);
break;
}
}
TouchPoint newPoint(ev.button.x,ev.button.y);
#ifdef __APPLE__
newPoint->x*=dpiScale;
newPoint->y*=dpiScale;
#endif
activePoints.push_back(newPoint);
pressedPoints.push_back(newPoint);
break;
}
case SDL_MOUSEBUTTONUP: {
for (size_t i=0; i<activePoints.size(); i++) {
TouchPoint& point=activePoints[i];
if (point.id==-1) {
releasedPoints.push_back(point);
activePoints.erase(activePoints.begin()+i);
break;
}
}
break;
}
case SDL_FINGERMOTION: {
TouchPoint* point=NULL;
FIND_POINT(point,ev.tfinger.fingerId);
if (point!=NULL) {
point->x=ev.tfinger.x*scrW*dpiScale;
point->y=ev.tfinger.y*scrH*dpiScale;
point->z=ev.tfinger.pressure;
}
break;
}
case SDL_FINGERDOWN: {
for (size_t i=0; i<activePoints.size(); i++) {
TouchPoint& point=activePoints[i];
if (point.id==ev.tfinger.fingerId) {
releasedPoints.push_back(point);
activePoints.erase(activePoints.begin()+i);
break;
}
}
TouchPoint newPoint(ev.tfinger.fingerId,ev.tfinger.x*scrW*dpiScale,ev.tfinger.y*scrH*dpiScale,ev.tfinger.pressure);
activePoints.push_back(newPoint);
pressedPoints.push_back(newPoint);
break;
}
case SDL_FINGERUP: {
for (size_t i=0; i<activePoints.size(); i++) {
TouchPoint& point=activePoints[i];
if (point.id==ev.tfinger.fingerId) {
releasedPoints.push_back(point);
activePoints.erase(activePoints.begin()+i);
break;
}
}
break;
}
}
}
bool FurnaceGUI::loop() {
SDL_SetEventFilter(_processEvent,this);
@ -2300,6 +2394,7 @@ bool FurnaceGUI::loop() {
while (SDL_PollEvent(&ev)) {
WAKE_UP;
ImGui_ImplSDL2_ProcessEvent(&ev);
processPoint(ev);
switch (ev.type) {
case SDL_MOUSEMOTION: {
int motionX=ev.motion.x;
@ -3634,6 +3729,9 @@ bool FurnaceGUI::loop() {
wheelX=0;
wheelY=0;
pressedPoints.clear();
releasedPoints.clear();
if (willCommit) {
commitSettings();
willCommit=false;
@ -3766,6 +3864,12 @@ bool FurnaceGUI::init() {
}
#endif
#ifdef IS_MOBILE
SDL_GetWindowSize(sdlWin,&scrW,&scrH);
scrW/=dpiScale;
scrH/=dpiScale;
#endif
#if !(defined(__APPLE__) || defined(_WIN32))
if (icon!=NULL) {
SDL_SetWindowIcon(sdlWin,icon);
@ -4165,8 +4269,12 @@ FurnaceGUI::FurnaceGUI():
chanOscWaveCorr(true),
followLog(true),
pianoOctaves(7),
pianoOctavesEdit(4),
pianoOptions(false),
pianoSharePosition(false),
pianoOffset(6),
pianoOffsetEdit(6),
pianoView(2),
hasACED(false) {
// value keys
valueKeys[SDLK_0]=0;

View File

@ -715,6 +715,27 @@ struct OperationMask {
effectVal(true) {}
};
struct TouchPoint {
// an ID of -1 represents the mouse cursor.
int id;
float x, y, z;
TouchPoint():
id(-1),
x(0.0f),
y(0.0f),
z(1.0f) {}
TouchPoint(float xp, float yp):
id(-1),
x(xp),
y(yp),
z(1.0f) {}
TouchPoint(int ident, float xp, float yp, float pressure=1.0f):
id(ident),
x(xp),
y(yp),
z(pressure) {}
};
struct FurnaceGUISysDef {
const char* name;
std::vector<int> definition;
@ -1084,6 +1105,12 @@ class FurnaceGUI {
// SDL_Keycode,int
std::map<int,int> valueKeys;
// currently active touch points
std::vector<TouchPoint> activePoints;
// one frame points
std::vector<TouchPoint> pressedPoints;
std::vector<TouchPoint> releasedPoints;
int arpMacroScroll;
int pitchMacroScroll;
@ -1186,10 +1213,11 @@ class FurnaceGUI {
bool followLog;
// piano
int pianoOctaves;
bool pianoOptions;
int pianoOctaves, pianoOctavesEdit;
bool pianoOptions, pianoSharePosition;
float pianoKeyHit[180];
int pianoOffset;
int pianoOffset, pianoOffsetEdit;
int pianoView;
// TX81Z
bool hasACED;
@ -1271,6 +1299,7 @@ class FurnaceGUI {
void syncSettings();
void commitSettings();
void processDrags(int dragX, int dragY);
void processPoint(SDL_Event& ev);
void startSelection(int xCoarse, int xFine, int y, bool fullRow=false);
void updateSelection(int xCoarse, int xFine, int y, bool fullRow=false);

View File

@ -21,6 +21,8 @@
#include "guiConst.h"
#include "imgui.h"
#include "imgui_internal.h"
#include <fmt/printf.h>
#include "IconsFontAwesome4.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
@ -34,6 +36,10 @@ const int bottomKeyNotes[7]={
0, 2, 4, 5, 7, 9, 11
};
const bool isTopKey[12]={
false, true, false, true, false, false, true, false, true, false, true, false
};
// TODO: actually implement a piano!
void FurnaceGUI::drawPiano() {
if (nextWindow==GUI_WINDOW_PIANO) {
@ -44,6 +50,9 @@ void FurnaceGUI::drawPiano() {
if (!pianoOpen) return;
if (ImGui::Begin("Piano",&pianoOpen,(pianoOptions)?0:ImGuiWindowFlags_NoTitleBar)) {
if (ImGui::BeginTable("PianoLayout",pianoOptions?2:1,ImGuiTableFlags_BordersInnerV)) {
int& off=(e->isPlaying() || pianoSharePosition)?pianoOffset:pianoOffsetEdit;
int& oct=(e->isPlaying() || pianoSharePosition)?pianoOctaves:pianoOctavesEdit;
bool view=(pianoView==2)?(!e->isPlaying()):pianoView;
if (pianoOptions) {
ImGui::TableSetupColumn("c0",ImGuiTableColumnFlags_WidthFixed);
}
@ -52,7 +61,42 @@ void FurnaceGUI::drawPiano() {
ImGui::TableNextRow();
if (pianoOptions) {
ImGui::TableNextColumn();
ImGui::Button("Options");
if (ImGui::Button(ICON_FA_ARROW_LEFT "##PianoLeft")) {
off--;
if (off<0) off=0;
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_ARROW_RIGHT "##PianoRight")) {
off++;
if ((off+oct)>14) off=15-oct;
}
ImGui::SameLine();
ImGui::Button(ICON_FA_ELLIPSIS_V "##PianoOptions");
if (ImGui::BeginPopupContextItem("PianoOptions",ImGuiPopupFlags_MouseButtonLeft)) {
ImGui::Text("Key layout:");
if (ImGui::RadioButton("Automatic",pianoView==2)) {
pianoView=2;
}
if (ImGui::RadioButton("Standard",pianoView==0)) {
pianoView=0;
}
if (ImGui::RadioButton("Continuous",pianoView==1)) {
pianoView=1;
}
ImGui::Checkbox("Share play/edit offset/range",&pianoSharePosition);
ImGui::EndPopup();
}
if (ImGui::Button(ICON_FA_MINUS "##PianoOctaveDown")) {
oct--;
if (oct<1) oct=1;
}
ImGui::SameLine();
if (ImGui::Button(ICON_FA_PLUS "##PianoOctaveUp")) {
oct++;
if (oct>15) oct=15;
if ((off+oct)>14) off=15-oct;
}
}
ImGui::TableNextColumn();
@ -70,36 +114,67 @@ void FurnaceGUI::drawPiano() {
// 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<bottomNotes; i++) {
int note=bottomKeyNotes[i%7]+12*((i/7)+pianoOffset);
if (note<0) continue;
if (note>=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<pianoOctaves; i++) {
ImVec2 op0=ImLerp(rect.Min,rect.Max,ImVec2((float)i/pianoOctaves,0.0f));
ImVec2 op1=ImLerp(rect.Min,rect.Max,ImVec2((float)(i+1)/pianoOctaves,1.0f));
for (int j=0; j<5; j++) {
int note=topKeyNotes[j]+12*(i+pianoOffset);
if (view) {
int notes=oct*12;
for (int i=0; i<notes; i++) {
int note=i+12*off;
if (note<0) continue;
if (note>=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;
ImVec4 color=isTopKey[i%12]?ImVec4(pkh,pkh,pkh,1.0f):ImVec4(1.0f-pkh,1.0f-pkh,1.0f-pkh,1.0f);
ImVec2 p0=ImLerp(rect.Min,rect.Max,ImVec2((float)i/notes,0.0f));
ImVec2 p1=ImLerp(rect.Min,rect.Max,ImVec2((float)(i+1)/notes,1.0f));
p1.x-=dpiScale;
p1.y-=dpiScale;
dl->AddRectFilled(p0,p1,ImGui::ColorConvertFloat4ToU32(color));
if ((i%12)==0) {
String label=fmt::sprintf("%d",(note-60)/12);
ImVec2 pText=ImLerp(p0,p1,ImVec2(0.5f,1.0f));
ImVec2 labelSize=ImGui::CalcTextSize(label.c_str());
pText.x-=labelSize.x*0.5f;
pText.y-=labelSize.y+ImGui::GetStyle().ItemSpacing.y;
dl->AddText(pText,0xff404040,label.c_str());
}
}
} else {
int bottomNotes=7*oct;
for (int i=0; i<bottomNotes; i++) {
int note=bottomKeyNotes[i%7]+12*((i/7)+off);
if (note<0) continue;
if (note>=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));
if ((i%7)==0) {
String label=fmt::sprintf("%d",(note-60)/12);
ImVec2 pText=ImLerp(p0,p1,ImVec2(0.5f,1.0f));
ImVec2 labelSize=ImGui::CalcTextSize(label.c_str());
pText.x-=labelSize.x*0.5f;
pText.y-=labelSize.y+ImGui::GetStyle().ItemSpacing.y;
dl->AddText(pText,0xff404040,label.c_str());
}
}
for (int i=0; i<oct; i++) {
ImVec2 op0=ImLerp(rect.Min,rect.Max,ImVec2((float)i/oct,0.0f));
ImVec2 op1=ImLerp(rect.Min,rect.Max,ImVec2((float)(i+1)/oct,1.0f));
for (int j=0; j<5; j++) {
int note=topKeyNotes[j]+12*(i+off);
if (note<0) continue;
if (note>=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));
}
}
}