diff options
Diffstat (limited to 'plugingui/knob.cc')
-rw-r--r-- | plugingui/knob.cc | 268 |
1 files changed, 133 insertions, 135 deletions
diff --git a/plugingui/knob.cc b/plugingui/knob.cc index 4cac180..ab2eca9 100644 --- a/plugingui/knob.cc +++ b/plugingui/knob.cc @@ -37,169 +37,167 @@ #endif #include <math.h> -GUI::Knob::Knob(Widget *parent) - : GUI::Widget(parent), img_knob(":knob.png") +namespace GUI { + +Knob::Knob(Widget *parent) + : Widget(parent) + , img_knob(":knob.png") { - state = up; + state = up; - val = 0.0; - maximum = 1.0; - minimum = 0.0; - mouse_offset_x = 0; + maximum = 1.0; + minimum = 0.0; - handler = NULL; - ptr = NULL; -} + currentValue = minimum; -void GUI::Knob::setValue(float v) -{ - val = v; - if(handler) handler(ptr); - repaintEvent(NULL); + mouse_offset_x = 0; } -float GUI::Knob::value() +void Knob::setValue(float value) { - return val; + internalSetValue(value); } -void GUI::Knob::registerClickHandler(void (*handler)(void *), void *ptr) +float Knob::value() { - this->handler = handler; - this->ptr = ptr; + return currentValue; } -void GUI::Knob::scrollEvent(ScrollEvent *e) +void Knob::scrollEvent(ScrollEvent* scrollEvent) { - val -= e->delta / 200.0; - if(val < 0) val = 0; - if(val > 1) val = 1; - - if(handler) handler(ptr); - - repaintEvent(NULL); + float value = currentValue - (scrollEvent->delta / 200.0); + internalSetValue(value); } -void GUI::Knob::mouseMoveEvent(MouseMoveEvent *e) +void Knob::mouseMoveEvent(MouseMoveEvent* mouseMoveEvent) { - if(state == down) { - if(mouse_offset_x == (e->x + -1*e->y)) return; + if(state == down) + { + if(mouse_offset_x == (mouseMoveEvent->x + (-1 * mouseMoveEvent->y))) + { + return; + } - float dval = mouse_offset_x - (e->x + -1*e->y); - val -= dval / 300.0; + float dval = + mouse_offset_x - (mouseMoveEvent->x + (-1 * mouseMoveEvent->y)); + float value = currentValue - (dval / 300.0); - if(val < 0) val = 0; - if(val > 1) val = 1; + internalSetValue(value); - if(handler) handler(ptr); - repaintEvent(NULL); - - mouse_offset_x = e->x + -1*e->y; - } + mouse_offset_x = mouseMoveEvent->x + (-1 * mouseMoveEvent->y); + } } -void GUI::Knob::keyEvent(KeyEvent *e) +void Knob::keyEvent(KeyEvent* keyEvent) { - if(e->direction != -1) return; - - switch(e->keycode) { - case GUI::KeyEvent::KEY_UP: - val += 0.01; - break; - case GUI::KeyEvent::KEY_DOWN: - val -= 0.01; - break; - case GUI::KeyEvent::KEY_RIGHT: - val += 0.01; - break; - case GUI::KeyEvent::KEY_LEFT: - val -= 0.01; - break; - case GUI::KeyEvent::KEY_HOME: - val = 0; - break; - case GUI::KeyEvent::KEY_END: - val = 1; - break; - default: - break; - } - - if(val < 0) val = 0; - if(val > 1) val = 1; - - repaintEvent(NULL); + if(keyEvent->direction != Direction::up) + { + return; + } + + float value = currentValue; + switch(keyEvent->keycode) { + case Key::up: + value += 0.01; + break; + case Key::down: + value -= 0.01; + break; + case Key::right: + value += 0.01; + break; + case Key::left: + value -= 0.01; + break; + case Key::home: + value = 0; + break; + case Key::end: + value = 1; + break; + default: + break; + } + + internalSetValue(value); } -void GUI::Knob::buttonEvent(ButtonEvent *e) +void Knob::buttonEvent(ButtonEvent* buttonEvent) { - if(e->direction == 1) { - state = down; - mouse_offset_x = e->x + -1*e->y; - if(handler) handler(ptr); - repaintEvent(NULL); - } - if(e->direction == -1) { - state = up; - mouse_offset_x = e->x + -1*e->y; - repaintEvent(NULL); - clicked(); - if(handler) handler(ptr); - } + if(buttonEvent->direction == Direction::down) + { + state = down; + mouse_offset_x = buttonEvent->x + (-1 * buttonEvent->y); + } + + if(buttonEvent->direction == Direction::up) + { + state = up; + mouse_offset_x = buttonEvent->x + (-1 * buttonEvent->y); + clicked(); + } } -void GUI::Knob::repaintEvent(GUI::RepaintEvent *e) +void Knob::repaintEvent(RepaintEvent* repaintEvent) { - int diameter = (width()>height()?height():width()); - int radius = diameter / 2; - int center_x = width() / 2; - int center_y = height() / 2; - - Painter p(this); - - p.clear(); - p.drawImageStretched(0, 0, &img_knob, diameter, diameter); - - char buf[64]; - sprintf(buf, "%.2f", val * maximum); - Font font; - p.drawText(center_x - font.textWidth(buf) / 2 + 1, - center_y + font.textHeight(buf) / 2 + 1, font, buf); - - double padval = val * 0.8 + 0.1; // Make it start from 20% and stop at 80% - - double from_x = sin((-1 * padval + 1) * 2 * M_PI) * radius * 0.6; - double from_y = cos((-1 * padval + 1) * 2 * M_PI) * radius * 0.6; - - double to_x = sin((-1 * padval + 1) * 2 * M_PI) * radius * 0.8; - double to_y = cos((-1 * padval + 1) * 2 * M_PI) * radius * 0.8; - - // Draw "fat" line by drawing 9 lines with moved start/ending points. - p.setColour(Colour(1, 0, 0, 1)); - for(int _x = -1; _x < 2; _x++) { - for(int _y = -1; _y < 2; _y++) { - p.drawLine(from_x + center_x + _x, - from_y + center_y + _y, - to_x + center_x + _x, - to_y + center_y + _y); - - } - } + int diameter = (width()>height()?height():width()); + int radius = diameter / 2; + int center_x = width() / 2; + int center_y = height() / 2; + + Painter p(*this); + + p.clear(); + p.drawImageStretched(0, 0, img_knob, diameter, diameter); + + char buf[64]; + sprintf(buf, "%.2f", currentValue * maximum); + p.drawText(center_x - font.textWidth(buf) / 2 + 1, + center_y + font.textHeight(buf) / 2 + 1, font, buf); + + // Make it start from 20% and stop at 80% + double padval = currentValue * 0.8 + 0.1; + + double from_x = sin((-1 * padval + 1) * 2 * M_PI) * radius * 0.6; + double from_y = cos((-1 * padval + 1) * 2 * M_PI) * radius * 0.6; + + double to_x = sin((-1 * padval + 1) * 2 * M_PI) * radius * 0.8; + double to_y = cos((-1 * padval + 1) * 2 * M_PI) * radius * 0.8; + + // Draw "fat" line by drawing 9 lines with moved start/ending points. + p.setColour(Colour(1, 0, 0, 1)); + for(int _x = -1; _x < 2; _x++) + { + for(int _y = -1; _y < 2; _y++) + { + p.drawLine(from_x + center_x + _x, + from_y + center_y + _y, + to_x + center_x + _x, + to_y + center_y + _y); + } + } } -#ifdef TEST_KNOB -//Additional dependency files -//deps: -//Required cflags (autoconf vars may be used) -//cflags: -//Required link options (autoconf vars may be used) -//libs: -#include "test.h" - -TEST_BEGIN; - -// TODO: Put some testcode here (see test.h for usable macros). - -TEST_END; +void Knob::internalSetValue(float value) +{ + if(value < minimum) + { + value = minimum; + } + + if(value > maximum) + { + value = maximum; + } + + if(value == currentValue) + { + return; + } + + currentValue = value; + valueChangedNotifier(currentValue); + repaintEvent(nullptr); +} -#endif/*TEST_KNOB*/ +} // GUI:: |