Browse Source

Render rack rails with rescaling

tags/v0.5.0
Andrew Belt 7 years ago
parent
commit
684c783d41
9 changed files with 100 additions and 40 deletions
  1. +19
    -1
      include/math.hpp
  2. +16
    -11
      include/widgets.hpp
  3. +1
    -1
      src/app/RackScene.cpp
  4. +21
    -14
      src/app/RackWidget.cpp
  5. +1
    -1
      src/app/Toolbar.cpp
  6. +8
    -11
      src/widgets/FramebufferWidget.cpp
  7. +1
    -1
      src/widgets/Menu.cpp
  8. +19
    -0
      src/widgets/Widget.cpp
  9. +14
    -0
      src/widgets/ZoomWidget.cpp

+ 19
- 1
include/math.hpp View File

@@ -197,9 +197,15 @@ struct Vec {
Vec ceil() {
return Vec(ceilf(x), ceilf(y));
}
bool isEqual(Vec b) {
return x == b.x && y == b.y;
}
bool isZero() {
return x == 0.0 && y == 0.0;
}
bool isFinite() {
return isfinite(x) && isfinite(y);
}
Vec clamp(Rect bound);
};

@@ -229,6 +235,9 @@ struct Rect {
return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
&& (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
}
bool isEqual(Rect r) {
return pos.isEqual(r.pos) && size.isEqual(r.size);
}
Vec getCenter() {
return pos.plus(size.mult(0.5));
}
@@ -241,8 +250,17 @@ struct Rect {
Vec getBottomRight() {
return pos.plus(size);
}
/** Clamps the position to fix inside a bounding box */
/** Clamps the edges of the rectangle to fit within a bound */
Rect clamp(Rect bound) {
Rect r;
r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
r.size.x = clampf(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
r.size.y = clampf(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y;
return r;
}
/** Nudges the position to fix inside a bounding box */
Rect nudge(Rect bound) {
Rect r;
r.size = size;
r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);


+ 16
- 11
include/widgets.hpp View File

@@ -58,6 +58,8 @@ struct Widget {

Vec getAbsolutePos();
Rect getChildrenBoundingBox();
/** Returns a subset of the given Rect bounded by the box of this widget and all ancestors */
virtual Rect getViewport(Rect r);

template <class T>
T *getAncestorOfType() {
@@ -131,6 +133,7 @@ struct Widget {

virtual void onAction() {}
virtual void onChange() {}
virtual void onZoom();
};

struct TransformWidget : Widget {
@@ -144,6 +147,17 @@ struct TransformWidget : Widget {
void draw(NVGcontext *vg) override;
};

struct ZoomWidget : Widget {
float zoom = 1.0;
Rect getViewport(Rect r) override;
void setZoom(float zoom);
void draw(NVGcontext *vg) override;
Widget *onMouseDown(Vec pos, int button) override;
Widget *onMouseUp(Vec pos, int button) override;
Widget *onMouseMove(Vec pos, Vec mouseRel) override;
Widget *onHoverKey(Vec pos, int key) override;
Widget *onScroll(Vec pos, Vec scrollRel) override;
};

////////////////////
// Trait widgets
@@ -214,7 +228,7 @@ When `dirty` is true, its children will be re-rendered on the next call to step(
Events are not passed to the underlying scene.
*/
struct FramebufferWidget : virtual Widget {
/** Set this to true to re-render the children to the framebuffer in the next step() override */
/** Set this to true to re-render the children to the framebuffer the next time it is drawn */
bool dirty = true;
/** The root object in the framebuffer scene
The FramebufferWidget owns the pointer
@@ -226,6 +240,7 @@ struct FramebufferWidget : virtual Widget {
~FramebufferWidget();
void draw(NVGcontext *vg) override;
int getImageHandle();
void onZoom() override;
};

struct QuantityWidget : virtual Widget {
@@ -378,16 +393,6 @@ struct ScrollWidget : OpaqueWidget {
bool onScrollOpaque(Vec scrollRel) override;
};

struct ZoomWidget : Widget {
float zoom = 1.0;
void draw(NVGcontext *vg) override;
Widget *onMouseDown(Vec pos, int button) override;
Widget *onMouseUp(Vec pos, int button) override;
Widget *onMouseMove(Vec pos, Vec mouseRel) override;
Widget *onHoverKey(Vec pos, int key) override;
Widget *onScroll(Vec pos, Vec scrollRel) override;
};

struct TextField : OpaqueWidget {
std::string text;
std::string placeholder;


+ 1
- 1
src/app/RackScene.cpp View File

@@ -63,7 +63,7 @@ void RackScene::step() {
.div(zoomWidget->zoom);

// Set zoom from the toolbar's zoom slider
zoomWidget->zoom = gToolbar->zoomSlider->value / 100.0;
zoomWidget->setZoom(gToolbar->zoomSlider->value / 100.0);

Scene::step();



+ 21
- 14
src/app/RackWidget.cpp View File

@@ -16,10 +16,12 @@ namespace rack {

RackWidget::RackWidget() {
rails = new FramebufferWidget();
RackRail *rail = new RackRail();
rail->box.size = Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
rails->addChild(rail);
rails->box.size = rail->box.size;
{
RackRail *rail = new RackRail();
rail->box.size = Vec();
rails->addChild(rail);
}
addChild(rails);

moduleContainer = new Widget();
addChild(moduleContainer);
@@ -29,7 +31,6 @@ RackWidget::RackWidget() {
}

RackWidget::~RackWidget() {
delete rails;
}

void RackWidget::clear() {
@@ -333,13 +334,26 @@ bool RackWidget::requestModuleBoxNearest(ModuleWidget *m, Rect box) {
}

void RackWidget::step() {
rails->step();

// Expand size to fit modules
Vec moduleSize = moduleContainer->getChildrenBoundingBox().getBottomRight();
// We assume that the size is reset by a parent before calling step(). Otherwise it will grow unbounded.
box.size = box.size.max(moduleSize);

// Adjust size and position of rails
Widget *rail = rails->children.front();
Rect bound = getViewport(Rect(Vec(), box.size));
// bound.pos = bound.pos.plus(Vec(100, 100));
// bound.size = bound.size.minus(Vec(200, 200));
rails->box = bound;
Vec grid = Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
Vec gridPos = bound.pos.div(grid).floor().mult(grid);
bound.pos = gridPos.minus(bound.pos);
bound.size = bound.size.minus(bound.pos);
if (!bound.isEqual(rail->box)) {
rails->dirty = true;
}
rail->box = bound;

// Autosave every 15 seconds
if (gGuiFrame % (60*15) == 0) {
savePatch(assetLocal("autosave.vcv"));
@@ -350,13 +364,6 @@ void RackWidget::step() {
}

void RackWidget::draw(NVGcontext *vg) {
// Draw rails
nvgBeginPath(vg);
nvgRect(vg, 0.0, 0.0, box.size.x, box.size.y);
NVGpaint paint = nvgImagePattern(vg, rails->box.pos.x, rails->box.pos.y, rails->box.size.x, rails->box.size.y, 0.0, rails->getImageHandle(), 1.0);
nvgFillPaint(vg, paint);
nvgFill(vg);

Widget::draw(vg);
}



+ 1
- 1
src/app/Toolbar.cpp View File

@@ -153,7 +153,7 @@ Toolbar::Toolbar() {
zoomSlider->box.size.x = 150;
zoomSlider->label = "Zoom";
zoomSlider->unit = "%";
zoomSlider->setLimits(25.0, 200.0);
zoomSlider->setLimits(33.33, 200.0);
zoomSlider->setDefaultValue(100.0);
addChild(zoomSlider);
xPos += zoomSlider->box.size.x;


+ 8
- 11
src/widgets/FramebufferWidget.cpp View File

@@ -18,7 +18,6 @@ static const float oversample = 2.0;
struct FramebufferWidget::Internal {
NVGLUframebuffer *fb = NULL;
Rect box;
Vec lastS;

~Internal() {
setFramebuffer(NULL);
@@ -47,20 +46,16 @@ void FramebufferWidget::draw(NVGcontext *vg) {
// Get world transform
float xform[6];
nvgCurrentTransform(vg, xform);
// Skew is not supported
// Skew and rotate is not supported
assert(fabsf(xform[1]) < 1e-6);
assert(fabsf(xform[2]) < 1e-6);
Vec s = Vec(xform[0], xform[3]);
Vec b = Vec(xform[4], xform[5]);

// Check if scale has changed
if (s.x != internal->lastS.x || s.y != internal->lastS.y) {
dirty = true;
}
internal->lastS = s;

// Render to framebuffer
if (dirty) {
dirty = false;

internal->box.pos = Vec(0, 0);
internal->box.size = box.size.mult(s).ceil().plus(Vec(1, 1));
Vec fbSize = internal->box.size.mult(gPixelRatio * oversample);
@@ -73,7 +68,7 @@ void FramebufferWidget::draw(NVGcontext *vg) {
// Delete old one first to free up GPU memory
internal->setFramebuffer(NULL);
// Create a framebuffer from the main nanovg context. We will draw to this in the secondary nanovg context.
NVGLUframebuffer *fb = nvgluCreateFramebuffer(gVg, fbSize.x, fbSize.y, NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
NVGLUframebuffer *fb = nvgluCreateFramebuffer(gVg, fbSize.x, fbSize.y, 0);
if (!fb)
return;
internal->setFramebuffer(fb);
@@ -94,8 +89,6 @@ void FramebufferWidget::draw(NVGcontext *vg) {

nvgEndFrame(gFramebufferVg);
nvgluBindFramebuffer(NULL);

dirty = false;
}

if (!internal->fb) {
@@ -128,5 +121,9 @@ int FramebufferWidget::getImageHandle() {
return internal->fb->image;
}

void FramebufferWidget::onZoom() {
dirty = true;
}


} // namespace rack

+ 1
- 1
src/widgets/Menu.cpp View File

@@ -30,7 +30,7 @@ void Menu::setChildMenu(Menu *menu) {
void Menu::step() {
// Try to fit into the parent's box
if (parent)
box = box.clamp(Rect(Vec(0, 0), parent->box.size));
box = box.nudge(Rect(Vec(0, 0), parent->box.size));

Widget::step();



+ 19
- 0
src/widgets/Widget.cpp View File

@@ -40,6 +40,18 @@ Rect Widget::getChildrenBoundingBox() {
return Rect(topLeft, bottomRight.minus(topLeft));
}

Rect Widget::getViewport(Rect r) {
Rect bound;
if (parent) {
bound = parent->getViewport(box);
}
else {
bound = box;
}
bound.pos = bound.pos.minus(box.pos);
return r.clamp(bound);
}

void Widget::addChild(Widget *widget) {
assert(!widget->parent);
widget->parent = this;
@@ -172,4 +184,11 @@ Widget *Widget::onScroll(Vec pos, Vec scrollRel) {
return NULL;
}

void Widget::onZoom() {
for (auto it = children.rbegin(); it != children.rend(); it++) {
Widget *child = *it;
child->onZoom();
}
}

} // namespace rack

+ 14
- 0
src/widgets/ZoomWidget.cpp View File

@@ -3,6 +3,20 @@

namespace rack {

Rect ZoomWidget::getViewport(Rect r) {
r.pos = r.pos.mult(zoom);
r.size = r.size.mult(zoom);
r = Widget::getViewport(r);
r.pos = r.pos.div(zoom);
r.size = r.size.div(zoom);
return r;
}

void ZoomWidget::setZoom(float zoom) {
if (zoom != this->zoom)
onZoom();
this->zoom = zoom;
}

void ZoomWidget::draw(NVGcontext *vg) {
nvgScale(vg, zoom, zoom);


Loading…
Cancel
Save