Browse Source

Fix event bugs

tags/v1.0.0
Andrew Belt 7 years ago
parent
commit
467dd78e57
4 changed files with 69 additions and 31 deletions
  1. +9
    -6
      include/WidgetState.hpp
  2. +54
    -20
      src/WidgetState.cpp
  3. +4
    -0
      src/app/RackWidget.cpp
  4. +2
    -5
      src/widgets/Widget.cpp

+ 9
- 6
include/WidgetState.hpp View File

@@ -7,13 +7,14 @@ namespace rack {


struct WidgetState {
Widget *rootWidget;
Widget *hoveredWidget;
Widget *draggedWidget;
Widget *dragHoveredWidget;
Widget *selectedWidget;
Widget *rootWidget = NULL;
Widget *hoveredWidget = NULL;
Widget *draggedWidget = NULL;
Widget *dragHoveredWidget = NULL;
Widget *selectedWidget = NULL;
/** For middle-click dragging */
Widget *scrollWidget = NULL;

WidgetState();
void handleButton(math::Vec pos, int button, int action, int mods);
void handleHover(math::Vec pos, math::Vec mouseDelta);
void handleLeave();
@@ -21,6 +22,8 @@ struct WidgetState {
void handleText(math::Vec pos, int codepoint);
void handleKey(math::Vec pos, int key, int scancode, int action, int mods);
void handleDrop(math::Vec pos, std::vector<std::string> paths);
/** Prepares a widget for deletion */
void finalizeWidget(Widget *w);
};




+ 54
- 20
src/WidgetState.cpp View File

@@ -4,15 +4,6 @@
namespace rack {


WidgetState::WidgetState() {
rootWidget = NULL;
hoveredWidget = NULL;
draggedWidget = NULL;
dragHoveredWidget = NULL;
selectedWidget = NULL;
}


void WidgetState::handleButton(math::Vec pos, int button, int action, int mods) {
// Button event
event::Button eButton;
@@ -23,18 +14,20 @@ void WidgetState::handleButton(math::Vec pos, int button, int action, int mods)
rootWidget->handleEvent(eButton);
Widget *clickedWidget = eButton.target;

if (button == GLFW_MOUSE_BUTTON_LEFT && clickedWidget) {
if (button == GLFW_MOUSE_BUTTON_LEFT) {
// Drag events
if (action == GLFW_PRESS && !draggedWidget) {
if (action == GLFW_PRESS && !draggedWidget && clickedWidget) {
event::DragStart eDragStart;
clickedWidget->handleEvent(eDragStart);
draggedWidget = eDragStart.target;
draggedWidget = clickedWidget;
}

if (action == GLFW_RELEASE && draggedWidget) {
event::DragDrop eDragDrop;
eDragDrop.origin = draggedWidget;
clickedWidget->handleEvent(eDragDrop);
if (clickedWidget) {
event::DragDrop eDragDrop;
eDragDrop.origin = draggedWidget;
clickedWidget->handleEvent(eDragDrop);
}

event::DragEnd eDragEnd;
draggedWidget->handleEvent(eDragEnd);
@@ -56,21 +49,53 @@ void WidgetState::handleButton(math::Vec pos, int button, int action, int mods)
}
}
}

if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
if (action == GLFW_PRESS) {
scrollWidget = clickedWidget;
}
if (action == GLFW_RELEASE) {
scrollWidget = NULL;
}
}
}


void WidgetState::handleHover(math::Vec pos, math::Vec mouseDelta) {
// Drag events
if (draggedWidget) {
event::DragMove eDragMove;
eDragMove.mouseDelta = mouseDelta;
draggedWidget->handleEvent(eDragMove);
return;
}

// if (scrollWidget) {
// event::HoverScroll eHoverScroll;
// eHoverScroll.pos = pos;
// eHoverScroll.scrollDelta = scrollDelta;
// rootWidget->handleEvent(eHoverScroll);
// }

// Hover event
event::Hover eHover;
eHover.pos = pos;
eHover.mouseDelta = mouseDelta;
rootWidget->handleEvent(eHover);
Widget *newHoveredWidget = eHover.target;

// Drag events
if (draggedWidget) {
event::DragMove eDragMove;
eDragMove.mouseDelta = mouseDelta;
draggedWidget->handleEvent(eDragMove);
if (newHoveredWidget != hoveredWidget) {
if (hoveredWidget) {
event::Leave eLeave;
hoveredWidget->handleEvent(eLeave);
}

hoveredWidget = newHoveredWidget;

if (hoveredWidget) {
event::Enter eEnter;
hoveredWidget->handleEvent(eEnter);
}
}
}

@@ -137,6 +162,15 @@ void WidgetState::handleKey(math::Vec pos, int key, int scancode, int action, in
rootWidget->handleEvent(eHoverKey);
}

void WidgetState::finalizeWidget(Widget *w) {
if (hoveredWidget == w) hoveredWidget = NULL;
if (draggedWidget == w) draggedWidget = NULL;
if (dragHoveredWidget == w) dragHoveredWidget = NULL;
if (selectedWidget == w) selectedWidget = NULL;
if (scrollWidget == w) scrollWidget = NULL;
}



// TODO Move this elsewhere
WidgetState *gWidgetState = NULL;


+ 4
- 0
src/app/RackWidget.cpp View File

@@ -503,9 +503,13 @@ void RackWidget::on(event::Hover &e) {
}

void RackWidget::on(event::Button &e) {
DEBUG("1");
OpaqueWidget::on(e);
DEBUG("2");
if (e.target == this) {
DEBUG("3");
if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_RIGHT) {
DEBUG("4");
appModuleBrowserCreate();
}
}


+ 2
- 5
src/widgets/Widget.cpp View File

@@ -9,11 +9,7 @@ namespace rack {
Widget::~Widget() {
// You should only delete orphaned widgets
assert(!parent);
// Stop dragging and hovering this widget
if (gWidgetState->hoveredWidget == this) gWidgetState->hoveredWidget = NULL;
if (gWidgetState->draggedWidget == this) gWidgetState->draggedWidget = NULL;
if (gWidgetState->hoveredWidget == this) gWidgetState->hoveredWidget = NULL;
if (gWidgetState->selectedWidget == this) gWidgetState->selectedWidget = NULL;
gWidgetState->finalizeWidget(this);
clearChildren();
}

@@ -81,6 +77,7 @@ void Widget::step() {
// Delete children if a delete is requested
if (child->requestedDelete) {
it = children.erase(it);
child->parent = NULL;
delete child;
continue;
}


Loading…
Cancel
Save