Browse Source

Set gFocusedWidget only if Widget::onFocus() returns true, make

ModuleWidget respond to Backspace/Delete only if no widget is focused
tags/v0.4.0
Andrew Belt 7 years ago
parent
commit
2cbe7c39a7
4 changed files with 20 additions and 13 deletions
  1. +2
    -2
      include/widgets.hpp
  2. +10
    -8
      src/app/ModuleWidget.cpp
  3. +6
    -2
      src/gui.cpp
  4. +2
    -1
      src/widgets/TextField.cpp

+ 2
- 2
include/widgets.hpp View File

@@ -112,7 +112,7 @@ struct Widget {
virtual void onMouseEnter() {}
/** Called when another widget begins responding to `onMouseMove` events */
virtual void onMouseLeave() {}
virtual void onFocus() {}
virtual bool onFocus() {return false;}
virtual void onDefocus() {}
virtual bool onFocusText(int codepoint) {return false;}
virtual bool onFocusKey(int key) {return false;}
@@ -409,7 +409,7 @@ struct TextField : OpaqueWidget {
Widget *onMouseDown(Vec pos, int button);
bool onFocusText(int codepoint);
bool onFocusKey(int scancode);
void onFocus();
bool onFocus();
void insertText(std::string newText);
};



+ 10
- 8
src/app/ModuleWidget.cpp View File

@@ -152,14 +152,16 @@ void ModuleWidget::draw(NVGcontext *vg) {
}

Widget *ModuleWidget::onMouseMove(Vec pos, Vec mouseRel) {
// Instead of checking key-down events, delete the module even if key-repeat hasn't fired yet and the cursor is hovering over the widget.
if (glfwGetKey(gWindow, GLFW_KEY_DELETE) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_BACKSPACE) == GLFW_PRESS) {
if (!guiIsModPressed() && !guiIsShiftPressed()) {
gRackWidget->deleteModule(this);
this->finalizeEvents();
delete this;
// Kinda sketchy because events will be passed further down the tree
return NULL;
if (!gFocusedWidget) {
// Instead of checking key-down events, delete the module even if key-repeat hasn't fired yet and the cursor is hovering over the widget.
if (glfwGetKey(gWindow, GLFW_KEY_DELETE) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_BACKSPACE) == GLFW_PRESS) {
if (!guiIsModPressed() && !guiIsShiftPressed()) {
gRackWidget->deleteModule(this);
this->finalizeEvents();
delete this;
// Kinda sketchy because events will be passed further down the tree
return NULL;
}
}
}
return OpaqueWidget::onMouseMove(pos, mouseRel);


+ 6
- 2
src/gui.cpp View File

@@ -59,12 +59,16 @@ void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods) {

if (w != gFocusedWidget) {
if (gFocusedWidget) {
// onDefocus
w->onDefocus();
}
gFocusedWidget = NULL;
if (w) {
w->onFocus();
// onFocus
if (w->onFocus()) {
gFocusedWidget = w;
}
}
gFocusedWidget = w;
}
}
}


+ 2
- 1
src/widgets/TextField.cpp View File

@@ -103,9 +103,10 @@ bool TextField::onFocusKey(int key) {
return true;
}

void TextField::onFocus() {
bool TextField::onFocus() {
begin = 0;
end = text.size();
return true;
}

void TextField::insertText(std::string newText) {


Loading…
Cancel
Save