Browse Source

Add operator overloads for math::Vec.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
c9df984896
2 changed files with 64 additions and 23 deletions
  1. +52
    -10
      include/math.hpp
  2. +12
    -13
      src/ui/ScrollBar.cpp

+ 52
- 10
include/math.hpp View File

@@ -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.



+ 12
- 13
src/ui/ScrollBar.cpp View File

@@ -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<ScrollWidget*>(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;
}




Loading…
Cancel
Save