diff options
-rw-r--r-- | plugingui/checkbox.cc | 119 | ||||
-rw-r--r-- | plugingui/checkbox.h | 36 | ||||
-rw-r--r-- | plugingui/knob.cc | 223 | ||||
-rw-r--r-- | plugingui/knob.h | 51 | ||||
-rw-r--r-- | plugingui/label.cc | 16 | ||||
-rw-r--r-- | plugingui/label.h | 10 | ||||
-rw-r--r-- | plugingui/widget.cc | 175 | ||||
-rw-r--r-- | plugingui/widget.h | 72 |
8 files changed, 384 insertions, 318 deletions
diff --git a/plugingui/checkbox.cc b/plugingui/checkbox.cc index 6f95287..54e0787 100644 --- a/plugingui/checkbox.cc +++ b/plugingui/checkbox.cc @@ -33,83 +33,106 @@ namespace GUI { CheckBox::CheckBox(Widget *parent) - : Widget(parent) - , bg_on(":switch_back_on.png") - , bg_off(":switch_back_off.png") - , knob(":switch_front.png") - , state(false) - , middle(false) + : Widget(parent) + , bg_on(":switch_back_on.png") + , bg_off(":switch_back_off.png") + , knob(":switch_front.png") + , state(false) + , middle(false) { } void CheckBox::buttonEvent(ButtonEvent *e) { - if(e->direction == -1 || e->doubleclick) { - middle = false; - internalSetChecked(!state); - } else { - middle = true; - } - - repaintEvent(NULL); + if(e->direction == -1 || e->doubleclick) + { + middle = false; + internalSetChecked(!state); + } + else + { + middle = true; + } + + repaintEvent(nullptr); } void CheckBox::setText(std::string text) { - _text = text; - repaintEvent(NULL); + _text = text; + repaintEvent(nullptr); } void CheckBox::keyEvent(KeyEvent *e) { - if(e->keycode == KeyEvent::KEY_CHARACTER && e->text == " ") { - if(e->direction == -1) { - middle = false; - internalSetChecked(!state); - } else { - middle = true; - } - - repaintEvent(NULL); - } + if(e->keycode == KeyEvent::KEY_CHARACTER && e->text == " ") + { + if(e->direction == -1) + { + middle = false; + internalSetChecked(!state); + } + else + { + middle = true; + } + + repaintEvent(nullptr); + } } void CheckBox::repaintEvent(RepaintEvent *e) { - Painter p(this); - - p.clear(); - - if(state) { - p.drawImage(0, (knob.height() - bg_on.height()) / 2, &bg_on); - if(middle) p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, &knob); - else p.drawImage(bg_on.width() - 40 + 2, 0, &knob); - } else { - p.drawImage(0, (knob.height() - bg_off.height()) / 2, &bg_off); - if(middle) p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, &knob); - else p.drawImage(0, 0, &knob); - } + Painter p(this); + + p.clear(); + + if(state) + { + p.drawImage(0, (knob.height() - bg_on.height()) / 2, &bg_on); + if(middle) + { + p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, &knob); + } + else + { + p.drawImage(bg_on.width() - 40 + 2, 0, &knob); + } + } + else + { + p.drawImage(0, (knob.height() - bg_off.height()) / 2, &bg_off); + if(middle) + { + p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, &knob); + } + else + { + p.drawImage(0, 0, &knob); + } + } } bool CheckBox::checked() { - return state; + return state; } void CheckBox::setChecked(bool c) { - internalSetChecked(c); + internalSetChecked(c); } void CheckBox::internalSetChecked(bool checked) { - if(checked == state) { - return; - } - - state = checked; - stateChangedNotifier(state); - repaintEvent(NULL); + if(checked == state) + { + return; + } + + state = checked; + stateChangedNotifier(state); + repaintEvent(nullptr); } } // GUI:: diff --git a/plugingui/checkbox.h b/plugingui/checkbox.h index 8dd3821..4cfeef0 100644 --- a/plugingui/checkbox.h +++ b/plugingui/checkbox.h @@ -34,36 +34,36 @@ namespace GUI { class CheckBox : public Widget { public: - CheckBox(Widget *parent); + CheckBox(Widget *parent); - void setText(std::string text); + void setText(std::string text); - bool isFocusable() override { return true; } + bool isFocusable() override { return true; } - bool checked(); - void setChecked(bool checked); + bool checked(); + void setChecked(bool checked); - Notifier<bool> stateChangedNotifier; + Notifier<bool> stateChangedNotifier; protected: - virtual void clicked() {} + virtual void clicked() {} - // From Widget: - virtual void repaintEvent(RepaintEvent *e) override; - virtual void buttonEvent(ButtonEvent *e) override; - virtual void keyEvent(KeyEvent *e) override; + // From Widget: + virtual void repaintEvent(RepaintEvent *e) override; + virtual void buttonEvent(ButtonEvent *e) override; + virtual void keyEvent(KeyEvent *e) override; private: - void internalSetChecked(bool checked); + void internalSetChecked(bool checked); - Image bg_on; - Image bg_off; - Image knob; + Image bg_on; + Image bg_off; + Image knob; - bool state; - bool middle; + bool state; + bool middle; - std::string _text; + std::string _text; }; } // GUI:: diff --git a/plugingui/knob.cc b/plugingui/knob.cc index f907004..765d401 100644 --- a/plugingui/knob.cc +++ b/plugingui/knob.cc @@ -40,149 +40,164 @@ namespace GUI { Knob::Knob(Widget *parent) - : Widget(parent) - , img_knob(":knob.png") + : Widget(parent) + , img_knob(":knob.png") { - state = up; + state = up; - maximum = 1.0; - minimum = 0.0; + maximum = 1.0; + minimum = 0.0; - currentValue = minimum; + currentValue = minimum; - mouse_offset_x = 0; + mouse_offset_x = 0; } void Knob::setValue(float value) { - internalSetValue(value); + internalSetValue(value); } float Knob::value() { - return currentValue; + return currentValue; } void Knob::scrollEvent(ScrollEvent *e) { - float value = currentValue - (e->delta / 200.0); - internalSetValue(value); + float value = currentValue - (e->delta / 200.0); + internalSetValue(value); } void Knob::mouseMoveEvent(MouseMoveEvent *e) { - if(state == down) { - if(mouse_offset_x == (e->x + -1 * e->y)) { - return; - } + if(state == down) + { + if(mouse_offset_x == (e->x + -1 * e->y)) + { + return; + } - float dval = mouse_offset_x - (e->x + -1 * e->y); - float value = currentValue - (dval / 300.0); + float dval = mouse_offset_x - (e->x + -1 * e->y); + float value = currentValue - (dval / 300.0); - internalSetValue(value); + internalSetValue(value); - mouse_offset_x = e->x + -1 * e->y; - } + mouse_offset_x = e->x + -1 * e->y; + } } void Knob::keyEvent(KeyEvent *e) { - if(e->direction != -1) { - return; - } - - float value = currentValue; - switch(e->keycode) { - case KeyEvent::KEY_UP: - value += 0.01; - break; - case KeyEvent::KEY_DOWN: - value -= 0.01; - break; - case KeyEvent::KEY_RIGHT: - value += 0.01; - break; - case KeyEvent::KEY_LEFT: - value -= 0.01; - break; - case KeyEvent::KEY_HOME: - value = 0; - break; - case KeyEvent::KEY_END: - value = 1; - break; - default: - break; - } - - internalSetValue(value); + if(e->direction != -1) + { + return; + } + + float value = currentValue; + switch(e->keycode) { + case KeyEvent::KEY_UP: + value += 0.01; + break; + case KeyEvent::KEY_DOWN: + value -= 0.01; + break; + case KeyEvent::KEY_RIGHT: + value += 0.01; + break; + case KeyEvent::KEY_LEFT: + value -= 0.01; + break; + case KeyEvent::KEY_HOME: + value = 0; + break; + case KeyEvent::KEY_END: + value = 1; + break; + default: + break; + } + + internalSetValue(value); } void Knob::buttonEvent(ButtonEvent *e) { - if(e->direction == 1) { - state = down; - mouse_offset_x = e->x + -1*e->y; - } - if(e->direction == -1) { - state = up; - mouse_offset_x = e->x + -1*e->y; - clicked(); - } + if(e->direction == 1) + { + state = down; + mouse_offset_x = e->x + -1*e->y; + } + + if(e->direction == -1) + { + state = up; + mouse_offset_x = e->x + -1*e->y; + clicked(); + } } void Knob::repaintEvent(RepaintEvent *e) { - 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); - Font font; - 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); - - } - } + 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); + Font font; + 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); + } + } } void Knob::internalSetValue(float value) { - if(value < minimum) value = minimum; - if(value > maximum) value = maximum; - - if(value == currentValue) { - return; - } - - currentValue = value; - valueChangedNotifier(currentValue); - repaintEvent(NULL); + if(value < minimum) + { + value = minimum; + } + + if(value > maximum) + { + value = maximum; + } + + if(value == currentValue) + { + return; + } + + currentValue = value; + valueChangedNotifier(currentValue); + repaintEvent(nullptr); } } // GUI:: diff --git a/plugingui/knob.h b/plugingui/knob.h index da03710..a0a093f 100644 --- a/plugingui/knob.h +++ b/plugingui/knob.h @@ -36,45 +36,44 @@ namespace GUI { class Knob : public Widget { public: - Knob(Widget *parent); + Knob(Widget *parent); - bool catchMouse() override { return true; } - bool isFocusable() override { return true; } + bool catchMouse() override { return true; } + bool isFocusable() override { return true; } - void setValue(float value); - float value(); + void setValue(float value); + float value(); - Notifier<float> valueChangedNotifier; // (float newValue) + Notifier<float> valueChangedNotifier; // (float newValue) protected: - virtual void clicked() {} + virtual void clicked() {} - // From Widget: - virtual void repaintEvent(RepaintEvent *e) override; - virtual void buttonEvent(ButtonEvent *e) override; - virtual void mouseMoveEvent(MouseMoveEvent *e) override; - virtual void scrollEvent(ScrollEvent *e) override; - virtual void keyEvent(KeyEvent *e) override; + // From Widget: + virtual void repaintEvent(RepaintEvent *e) override; + virtual void buttonEvent(ButtonEvent *e) override; + virtual void mouseMoveEvent(MouseMoveEvent *e) override; + virtual void scrollEvent(ScrollEvent *e) override; + virtual void keyEvent(KeyEvent *e) override; private: - //! Sets the internal value and sends out the changed notification. - void internalSetValue(float value); + //! Sets the internal value and sends out the changed notification. + void internalSetValue(float value); - typedef enum { - up, - down - } state_t; + typedef enum { + up, + down + } state_t; - state_t state; + state_t state; - float currentValue; - float maximum; - float minimum; + float currentValue; + float maximum; + float minimum; - Image img_knob; + Image img_knob; - int mouse_offset_x; + int mouse_offset_x; }; } // GUI:: - diff --git a/plugingui/label.cc b/plugingui/label.cc index be37d82..1b2ec14 100644 --- a/plugingui/label.cc +++ b/plugingui/label.cc @@ -32,26 +32,26 @@ namespace GUI { Label::Label(GUI::Widget *parent) - : Widget(parent) + : Widget(parent) { } void Label::setText(std::string text) { - _text = text; - repaintEvent(NULL); + _text = text; + repaintEvent(nullptr); } void Label::repaintEvent(GUI::RepaintEvent *e) { - Painter p(this); + Painter p(this); - p.clear(); + p.clear(); - p.setColour(Colour(1)); + p.setColour(Colour(1)); - Font font(":fontemboss.png"); - p.drawText(10, (height() + font.textHeight()) / 2, font, _text, true); + Font font(":fontemboss.png"); + p.drawText(10, (height() + font.textHeight()) / 2, font, _text, true); } } // GUI:: diff --git a/plugingui/label.h b/plugingui/label.h index 707cdb1..644d4b7 100644 --- a/plugingui/label.h +++ b/plugingui/label.h @@ -36,16 +36,16 @@ class RepaintEvent; class Label : public Widget { public: - Label(Widget *parent); + Label(Widget *parent); - void setText(std::string text); + void setText(std::string text); protected: - // From Widget: - virtual void repaintEvent(RepaintEvent *e) override; + // From Widget: + virtual void repaintEvent(RepaintEvent *e) override; private: - std::string _text; + std::string _text; }; } // GUI:: diff --git a/plugingui/widget.cc b/plugingui/widget.cc index 0919e22..65d269b 100644 --- a/plugingui/widget.cc +++ b/plugingui/widget.cc @@ -34,74 +34,84 @@ namespace GUI { Widget::Widget(Widget *parent) - : pixbuf(1, 1) + : pixbuf(1, 1) { - _width = _height = 10; + _width = _height = 10; - this->parent = parent; - if(parent) { - parent->addChild(this); - _window = parent->window(); - } - _width = _height = 0; - _visible = true; + this->parent = parent; + if(parent) + { + parent->addChild(this); + _window = parent->window(); + } + _width = _height = 0; + _visible = true; } Widget::~Widget() { - if(parent) parent->removeChild(this); + if(parent) + { + parent->removeChild(this); + } } void Widget::show() { - setVisible(true); + setVisible(true); } void Widget::hide() { - setVisible(false); + setVisible(false); } void Widget::setVisible(bool v) { - _visible = v; - repaintEvent(NULL); + _visible = v; + repaintEvent(nullptr); } bool Widget::visible() { - return _visible; + return _visible; } void Widget::addChild(Widget *widget) { - children.push_back(widget); + children.push_back(widget); } void Widget::removeChild(Widget *widget) { - std::vector<Widget *>::iterator i = children.begin(); - while(i != children.end()) { - if(*i == widget) { - children.erase(i); - return; - } - i++; - } + std::vector<Widget *>::iterator i = children.begin(); + while(i != children.end()) + { + if(*i == widget) + { + children.erase(i); + return; + } + i++; + } } void Widget::resize(int width, int height) { - if(width < 1 || height < 1) return; - _width = width; - _height = height; - pixbuf.realloc(width, height); + if(width < 1 || height < 1) + { + return; + } + + _width = width; + _height = height; + pixbuf.realloc(width, height); } void Widget::move(size_t x, size_t y) { - _x = x; - _y = y; + _x = x; + _y = y; } size_t Widget::x() { return _x; } @@ -111,79 +121,98 @@ size_t Widget::height() { return _height; } size_t Widget::windowX() { - size_t window_x = x(); - if(parent) window_x += parent->windowX(); - return window_x; + size_t window_x = x(); + if(parent) + { + window_x += parent->windowX(); + } + + return window_x; } size_t Widget::windowY() { - size_t window_y = y(); - if(parent) window_y += parent->windowY(); - return window_y; + size_t window_y = y(); + if(parent) + { + window_y += parent->windowY(); + } + + return window_y; } Widget *Widget::find(size_t x, size_t y) { - std::vector<Widget*>::reverse_iterator i = children.rbegin(); - while(i != children.rend()) { - Widget *w = *i; - if(w->visible()) { - if(w->x() <= x && (w->x() + w->width()) >= x && - w->y() <= y && w->y() + w->height() >= y) - return w->find(x - w->x(), y - w->y()); - } - i++; - } - - if(x > width() || x < 0 || y > height() || y < 0) return NULL; - return this; + std::vector<Widget*>::reverse_iterator i = children.rbegin(); + while(i != children.rend()) + { + Widget *w = *i; + if(w->visible()) + { + if(w->x() <= x && (w->x() + w->width()) >= x && + w->y() <= y && w->y() + w->height() >= y) + { + return w->find(x - w->x(), y - w->y()); + } + } + i++; + } + + if(x > width() || x < 0 || y > height() || y < 0) + { + return nullptr; + } + + return this; } Window *Widget::window() { - return _window; + return _window; } void Widget::repaint_r(RepaintEvent *e) { - Painter p(this); // make sure pixbuf refcount is incremented. + Painter p(this); // make sure pixbuf refcount is incremented. - repaintEvent(e); + repaintEvent(e); - std::vector<Widget*>::iterator i = children.begin(); - while(i != children.end()) { - Widget *w = *i; - w->repaint_r(e); - i++; - } + std::vector<Widget*>::iterator i = children.begin(); + while(i != children.end()) + { + Widget *w = *i; + w->repaint_r(e); + i++; + } } std::vector<PixelBufferAlpha *> Widget::getPixelBuffers() { - std::vector<PixelBufferAlpha *> pbs; + std::vector<PixelBufferAlpha *> pbs; - pixbuf.x = windowX(); - pixbuf.y = windowY(); + pixbuf.x = windowX(); + pixbuf.y = windowY(); - pbs.push_back(&pixbuf); + pbs.push_back(&pixbuf); - std::vector<Widget*>::iterator i = children.begin(); - while(i != children.end()) { - Widget *w = *i; - if(w->visible()) { - std::vector<PixelBufferAlpha *> pbs0 = w->getPixelBuffers(); - pbs.insert(pbs.end(), pbs0.begin(), pbs0.end()); - } - i++; - } + std::vector<Widget*>::iterator i = children.begin(); + while(i != children.end()) + { + Widget *w = *i; + if(w->visible()) + { + std::vector<PixelBufferAlpha *> pbs0 = w->getPixelBuffers(); + pbs.insert(pbs.end(), pbs0.begin(), pbs0.end()); + } + i++; + } - return pbs; + return pbs; } bool Widget::hasKeyboardFocus() { - return window()->keyboardFocus() == this; + return window()->keyboardFocus() == this; } } // GUI:: diff --git a/plugingui/widget.h b/plugingui/widget.h index 13725f0..10b221a 100644 --- a/plugingui/widget.h +++ b/plugingui/widget.h @@ -38,60 +38,60 @@ class Window; class Widget : public Listener { public: - Widget(Widget *parent); - virtual ~Widget(); + Widget(Widget *parent); + virtual ~Widget(); - virtual void show(); - virtual void hide(); + virtual void show(); + virtual void hide(); - virtual void resize(int width, int height); - virtual void move(size_t x, size_t y); + virtual void resize(int width, int height); + virtual void move(size_t x, size_t y); - virtual size_t x(); - virtual size_t y(); - virtual size_t windowX(); - virtual size_t windowY(); - virtual size_t width(); - virtual size_t height(); + virtual size_t x(); + virtual size_t y(); + virtual size_t windowX(); + virtual size_t windowY(); + virtual size_t width(); + virtual size_t height(); - virtual bool isFocusable() { return false; } - virtual bool catchMouse() { return false; } + virtual bool isFocusable() { return false; } + virtual bool catchMouse() { return false; } - void addChild(Widget *widget); - void removeChild(Widget *widget); + void addChild(Widget *widget); + void removeChild(Widget *widget); - virtual void repaintEvent(RepaintEvent *e) {} - virtual void mouseMoveEvent(MouseMoveEvent *e) {} - virtual void buttonEvent(ButtonEvent *e) {} - virtual void scrollEvent(ScrollEvent *e) {} - virtual void keyEvent(KeyEvent *e) {} + virtual void repaintEvent(RepaintEvent *e) {} + virtual void mouseMoveEvent(MouseMoveEvent *e) {} + virtual void buttonEvent(ButtonEvent *e) {} + virtual void scrollEvent(ScrollEvent *e) {} + virtual void keyEvent(KeyEvent *e) {} - virtual void mouseLeaveEvent() {} - virtual void mouseEnterEvent() {} + virtual void mouseLeaveEvent() {} + virtual void mouseEnterEvent() {} - Widget *find(size_t x, size_t y); + Widget *find(size_t x, size_t y); - virtual Window *window(); + virtual Window *window(); - void repaint_r(RepaintEvent *e); + void repaint_r(RepaintEvent *e); - PixelBufferAlpha pixbuf; - std::vector<PixelBufferAlpha *> getPixelBuffers(); + PixelBufferAlpha pixbuf; + std::vector<PixelBufferAlpha *> getPixelBuffers(); - bool hasKeyboardFocus(); + bool hasKeyboardFocus(); - Widget *parent; + Widget *parent; - bool visible(); - void setVisible(bool visible); + bool visible(); + void setVisible(bool visible); protected: - std::vector<Widget*> children; - Window *_window; - size_t _x, _y, _width, _height; + std::vector<Widget*> children; + Window *_window; + size_t _x, _y, _width, _height; private: - bool _visible; + bool _visible; }; } // GUI:: |