diff --git a/include/math.hpp b/include/math.hpp index 508bc256..4b57805c 100644 --- a/include/math.hpp +++ b/include/math.hpp @@ -190,17 +190,11 @@ struct Vec { Vec() {} Vec(float x, float y) : x(x), y(y) {} - float get(int index) const { - return (index == 0) ? x : y; + float& operator[](int i) { + return (i == 0) ? x : y; } - float& get(int index) { - return (index == 0) ? x : y; - } - void set(int index, float value) { - if (index == 0) - x = value; - else - y = value; + const float& operator[](int i) const { + return (i == 0) ? x : y; } /** Negates the vector. Equivalent to a reflection across the `y = -x` line. @@ -421,6 +415,54 @@ inline Vec Vec::clampSafe(Rect bound) const { } +// Operator overloads for Vec +inline Vec operator+(const Vec& a, const Vec& b) { + return a.plus(b); +} +inline Vec operator-(const Vec& a, const Vec& b) { + return a.minus(b); +} +inline Vec operator*(const Vec& a, const Vec& b) { + return a.mult(b); +} +inline Vec operator*(const Vec& a, const float& b) { + return a.mult(b); +} +inline Vec operator*(const float& a, const Vec& b) { + return b.mult(a); +} +inline Vec operator/(const Vec& a, const Vec& b) { + return a.div(b); +} +inline Vec operator/(const Vec& a, const float& b) { + return a.div(b); +} +inline Vec operator+=(Vec& a, const Vec& b) { + return a = a.plus(b); +} +inline Vec operator-=(Vec& a, const Vec& b) { + return a = a.minus(b); +} +inline Vec operator*=(Vec& a, const Vec& b) { + return a = a.mult(b); +} +inline Vec operator*=(Vec& a, const float& b) { + return a = a.mult(b); +} +inline Vec operator/=(Vec& a, const Vec& b) { + return a = a.div(b); +} +inline Vec operator/=(Vec& a, const float& b) { + return a = a.div(b); +} +inline bool operator==(const Vec& a, const Vec& b) { + return a.isEqual(b); +} +inline bool operator!=(const Vec& a, const Vec& b) { + return !a.isEqual(b); +} + + /** Expands a Vec and Rect into a comma-separated list. Useful for print debugging. diff --git a/src/ui/ScrollBar.cpp b/src/ui/ScrollBar.cpp index d22ad3b1..d3f02d34 100644 --- a/src/ui/ScrollBar.cpp +++ b/src/ui/ScrollBar.cpp @@ -30,8 +30,8 @@ void ScrollBar::draw(const DrawArgs& args) { if (APP->event->getDraggedWidget() == this) state = BND_ACTIVE; - float handleOffset = sw->getHandleOffset().get(vertical); - float handleSize = sw->getHandleSize().get(vertical); + float handleOffset = sw->getHandleOffset()[vertical]; + float handleSize = sw->getHandleSize()[vertical]; bndScrollBar(args.vg, 0.0, 0.0, box.size.x, box.size.y, state, handleOffset, handleSize); } @@ -41,18 +41,17 @@ void ScrollBar::onButton(const event::Button& e) { ScrollWidget* sw = dynamic_cast(parent); assert(sw); - float pos = e.pos.get(vertical); - pos /= box.size.get(vertical); - float handleOffset = sw->getHandleOffset().get(vertical); - float handleSize = sw->getHandleSize().get(vertical); + float pos = e.pos[vertical]; + pos /= box.size[vertical]; + float handleOffset = sw->getHandleOffset()[vertical]; + float handleSize = sw->getHandleSize()[vertical]; float handlePos = math::rescale(handleOffset, 0.f, 1.f, handleSize / 2.f, 1.f - handleSize / 2.f); - math::Rect offsetBound = sw->getContainerOffsetBound(); // Check if user clicked on handle if (std::fabs(pos - handlePos) > handleSize / 2.f) { // Jump to absolute position of the handle float offset = math::rescale(pos, handleSize / 2.f, 1.f - handleSize / 2.f, 0.f, 1.f); - sw->offset.get(vertical) = sw->containerBox.pos.get(vertical) + offset * (sw->containerBox.size.get(vertical) - sw->box.size.get(vertical)); + sw->offset[vertical] = sw->containerBox.pos[vertical] + offset * (sw->containerBox.size[vertical] - sw->box.size[vertical]); } } OpaqueWidget::onButton(e); @@ -72,14 +71,14 @@ void ScrollBar::onDragMove(const event::DragMove& e) { assert(sw); // Move handle absolutely. - float mouseDelta = e.mouseDelta.get(vertical); + float mouseDelta = e.mouseDelta[vertical]; mouseDelta /= getAbsoluteZoom(); - float handleSize = sw->getHandleSize().get(vertical); - float handleBound = (1.f - handleSize) * box.size.get(vertical); - float offsetBound = sw->getContainerOffsetBound().size.get(vertical); + float handleSize = sw->getHandleSize()[vertical]; + float handleBound = (1.f - handleSize) * box.size[vertical]; + float offsetBound = sw->getContainerOffsetBound().size[vertical]; float offsetDelta = mouseDelta * offsetBound / handleBound; - sw->offset.get(vertical) += offsetDelta; + sw->offset[vertical] += offsetDelta; }