Browse Source

Add tooltip to ParamWidget. Make Widget call event::State::finalizeWidget() when removed, not when deleted.

tags/v1.0.0
Andrew Belt 5 years ago
parent
commit
4ef58cbf78
7 changed files with 52 additions and 32 deletions
  1. +5
    -6
      include/app/ParamWidget.hpp
  2. +2
    -0
      include/context.hpp
  3. +2
    -9
      include/ui/Tooltip.hpp
  4. +2
    -2
      include/widgets/Widget.hpp
  5. +0
    -2
      src/app/ModuleBrowser.cpp
  6. +26
    -4
      src/app/ParamWidget.cpp
  7. +15
    -9
      src/widgets/Widget.cpp

+ 5
- 6
include/app/ParamWidget.hpp View File

@@ -1,6 +1,7 @@
#pragma once
#include "app/common.hpp"
#include "widgets/OpaqueWidget.hpp"
#include "ui/Tooltip.hpp"
#include "ui/Quantity.hpp"


@@ -10,17 +11,15 @@ namespace rack {
struct ParamWidget : OpaqueWidget {
Quantity *quantity = NULL;
float dirtyValue = NAN;
Tooltip *tooltip = NULL;

~ParamWidget() {
if (quantity)
delete quantity;
}

~ParamWidget();
void step() override;
/** For legacy patch loading */
void fromJson(json_t *rootJ);
void onButton(const event::Button &e) override;
void onDragMove(const event::DragMove &e) override;
void onEnter(const event::Enter &e) override;
void onLeave(const event::Leave &e) override;
};




+ 2
- 0
include/context.hpp View File

@@ -14,6 +14,7 @@ struct Engine;
struct Window;


/** Contains the application state */
struct Context {
event::State *event = NULL;
Scene *scene = NULL;
@@ -22,6 +23,7 @@ struct Context {
};


/** Returns the global context */
Context *context();




+ 2
- 9
include/ui/Tooltip.hpp View File

@@ -9,15 +9,8 @@ namespace rack {
struct Tooltip : virtual Widget {
std::string text;

void draw(NVGcontext *vg) override {
// Wrap size to contents
box.size.x = bndLabelWidth(vg, -1, text.c_str()) + 10.0;
box.size.y = bndLabelHeight(vg, -1, text.c_str(), INFINITY);

bndTooltipBackground(vg, 0.0, 0.0, box.size.x, box.size.y);
bndMenuLabel(vg, 0.0, 0.0, box.size.x, box.size.y, -1, text.c_str());
Widget::draw(vg);
}
void step() override;
void draw(NVGcontext *vg) override;
};




+ 2
- 2
include/widgets/Widget.hpp View File

@@ -59,11 +59,11 @@ struct Widget {
/** Adds widget to list of children.
Gives ownership of widget to this widget instance.
*/
void addChild(Widget *widget);
void addChild(Widget *child);
/** Removes widget from list of children if it exists.
Does not delete widget but transfers ownership to caller
*/
void removeChild(Widget *widget);
void removeChild(Widget *child);
/** Removes and deletes all children */
void clearChildren();



+ 0
- 2
src/app/ModuleBrowser.cpp View File

@@ -17,8 +17,6 @@ namespace rack {


static std::set<Model*> sFavoriteModels;
static std::string sAuthorFilter;
static std::string sTagFilter;


struct ModuleBox : OpaqueWidget {


+ 26
- 4
src/app/ParamWidget.cpp View File

@@ -1,10 +1,17 @@
#include "app/ParamWidget.hpp"
#include "app/Scene.hpp"
#include "context.hpp"
#include "random.hpp"


namespace rack {


ParamWidget::~ParamWidget() {
if (quantity)
delete quantity;
}

void ParamWidget::step() {
if (quantity) {
float value = quantity->getValue();
@@ -16,6 +23,12 @@ void ParamWidget::step() {
}
}

if (tooltip) {
if (quantity)
tooltip->text = quantity->getString();
tooltip->box.pos = getAbsoluteOffset(box.size);
}

OpaqueWidget::step();
}

@@ -39,10 +52,19 @@ void ParamWidget::onButton(const event::Button &e) {
OpaqueWidget::onButton(e);
}

void ParamWidget::onDragMove(const event::DragMove &e) {
if (quantity) {
DEBUG("%s", quantity->getString().c_str());
}
void ParamWidget::onEnter(const event::Enter &e) {
if (tooltip)
return;
tooltip = new Tooltip;
context()->scene->addChild(tooltip);
}

void ParamWidget::onLeave(const event::Leave &e) {
if (!tooltip)
return;
context()->scene->removeChild(tooltip);
delete tooltip;
tooltip = NULL;
}




+ 15
- 9
src/widgets/Widget.cpp View File

@@ -9,7 +9,6 @@ namespace rack {
Widget::~Widget() {
// You should only delete orphaned widgets
assert(!parent);
context()->event->finalizeWidget(this);
clearChildren();
}

@@ -49,22 +48,28 @@ math::Rect Widget::getViewport(math::Rect r) {
return r.clamp(bound);
}

void Widget::addChild(Widget *widget) {
assert(!widget->parent);
widget->parent = this;
children.push_back(widget);
void Widget::addChild(Widget *child) {
assert(!child->parent);
child->parent = this;
children.push_back(child);
}

void Widget::removeChild(Widget *widget) {
assert(widget->parent == this);
auto it = std::find(children.begin(), children.end(), widget);
void Widget::removeChild(Widget *child) {
// Make sure `this` is the child's parent
assert(child->parent == this);
// Prepare to remove widget from the event state
context()->event->finalizeWidget(child);
// Delete child from children list
auto it = std::find(children.begin(), children.end(), child);
assert(it != children.end());
children.erase(it);
widget->parent = NULL;
// Revoke child's parent
child->parent = NULL;
}

void Widget::clearChildren() {
for (Widget *child : children) {
context()->event->finalizeWidget(child);
child->parent = NULL;
delete child;
}
@@ -76,6 +81,7 @@ void Widget::step() {
Widget *child = *it;
// Delete children if a delete is requested
if (child->requestedDelete) {
context()->event->finalizeWidget(child);
it = children.erase(it);
child->parent = NULL;
delete child;


Loading…
Cancel
Save