diff options
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/console.cc | 1 | ||||
-rw-r--r-- | src/ui/label.cc | 5 | ||||
-rw-r--r-- | src/ui/label.h | 3 | ||||
-rw-r--r-- | src/ui/scrollpane.cc | 62 | ||||
-rw-r--r-- | src/ui/scrollpane.h | 24 |
5 files changed, 74 insertions, 21 deletions
diff --git a/src/ui/console.cc b/src/ui/console.cc index c486c65..6ce3593 100644 --- a/src/ui/console.cc +++ b/src/ui/console.cc @@ -78,6 +78,7 @@ void Console::show() SDL_ShowCursor(SDL_ENABLE); console_scrollpane->set_scroll(0); + console_scrollpane->set_offset(3); history_pos = history.rbegin(); (*history_pos).clear(); diff --git a/src/ui/label.cc b/src/ui/label.cc index 3284d0e..b6f72c7 100644 --- a/src/ui/label.cc +++ b/src/ui/label.cc @@ -30,6 +30,11 @@ void Label::print(const size_t indent) const con_print << aux::pad_left(marker, indent*2) << label() << " \"" << text() << "\"" << std::endl; } +void Label::clear() +{ + label_text.clear(); +} + void Label::set_text(const char *text) { if (text) diff --git a/src/ui/label.h b/src/ui/label.h index 9188dc5..2a38273 100644 --- a/src/ui/label.h +++ b/src/ui/label.h @@ -28,6 +28,9 @@ public: /// set the text alignment void set_alignment(const unsigned int alignment); + + /// clear the label text + void clear(); /// return the text displayed by the label inline const std::string &text() const { diff --git a/src/ui/scrollpane.cc b/src/ui/scrollpane.cc index 38260e0..48391c5 100644 --- a/src/ui/scrollpane.cc +++ b/src/ui/scrollpane.cc @@ -18,6 +18,7 @@ ScrollPane::ScrollPane(Widget *parent, ui::Text &text) : Widget(parent), scrollp set_label("scrollpane"); set_alignment(AlignBottom); scrollpane_scroll = 0; + scrollpane_offset = 1; } ScrollPane::~ScrollPane() @@ -29,7 +30,7 @@ void ScrollPane::set_alignment(const unsigned int alignment) scrollpane_alignment = alignment; } -void ScrollPane::set_scroll(int scroll) +void ScrollPane::set_scroll(const int scroll) { scrollpane_scroll = scroll; @@ -39,22 +40,52 @@ void ScrollPane::set_scroll(int scroll) scrollpane_scroll = 0; } -void ScrollPane::inc_scroll(int scroll) +void ScrollPane::inc_scroll(const int scroll) { scrollpane_scroll += scroll; if (scrollpane_scroll > (int) scrollpane_text.size()) scrollpane_scroll = (int) scrollpane_text.size(); + else if (scrollpane_scroll < 0) + scrollpane_scroll = 0; } -void ScrollPane::dec_scroll(int scroll) +void ScrollPane::dec_scroll(const int scroll) { scrollpane_scroll -= scroll; - if (scrollpane_scroll < 0) + if (scrollpane_scroll > (int) scrollpane_text.size()) + scrollpane_scroll = (int) scrollpane_text.size(); + else if (scrollpane_scroll < 0) scrollpane_scroll = 0; } +void ScrollPane::set_offset(const int offset) +{ + scrollpane_offset = offset; +} + +bool ScrollPane::on_keypress(const int key, const unsigned int modifier) +{ + // number of lines to scroll + int alignmentmodifier =( (alignment() & AlignTop) == AlignTop) ? -1 : 1; + + switch (key) { + + case 512 + SDL_BUTTON_WHEELUP: + inc_scroll(alignmentmodifier * scrollpane_offset); + return true; + break; + + case 512 + SDL_BUTTON_WHEELDOWN: + dec_scroll(alignmentmodifier * scrollpane_offset); + return true; + break; + } + + return false; +} + void ScrollPane::draw() { render::Text::setfont(font()->name().c_str(), font()->width(), font()->height()); @@ -69,12 +100,17 @@ void ScrollPane::draw() scrollpane_scroll = (int) scrollpane_text.size(); else if (scrollpane_scroll < 0) scrollpane_scroll = 0; - - int bottom = (int) scrollpane_text.size() - scrollpane_scroll; + + int bottom = 0; int current_line = 0; - + + if ((alignment() & AlignTop) == AlignTop) { + bottom = text_height + (int) scrollpane_scroll; + } else { + bottom = (int) scrollpane_text.size() - scrollpane_scroll; + } + ui::Text lines; - for (ui::Text::const_iterator it = scrollpane_text.begin(); it != scrollpane_text.end() && current_line < bottom; it++) { if (current_line >= bottom - text_height) { std::string linedata(*it); @@ -169,13 +205,9 @@ void ScrollPane::draw() float y = 0; if ((alignment() & AlignTop) == AlignTop) { - int i = (int) lines.size(); - for (ui::Text::iterator it = lines.begin(); it != lines.end(); it++) { - if (i <= text_height) { - render::Text::draw(gl.x(), gl.y() + y, (*it)); - y += font()->height(); - } - i--; + for (ui::Text::iterator it = lines.begin(); (y + font()->height() < height()) && (it != lines.end()); it++) { + render::Text::draw(gl.x(), gl.y() + y, (*it)); + y += font()->height(); } } else { y = height() - font()->height(); diff --git a/src/ui/scrollpane.h b/src/ui/scrollpane.h index 82c74c9..8fe8395 100644 --- a/src/ui/scrollpane.h +++ b/src/ui/scrollpane.h @@ -26,33 +26,45 @@ public: inline int scroll() const { return scrollpane_scroll; } + + /// current scroll offset + inline int offset() const { + return scrollpane_offset; + } /// text alignment inline unsigned int alignment() const { return scrollpane_alignment; } - /// set text alignment - void set_alignment(const unsigned int alignment); - /* -- mutators --------------------------------------------- */ /// set scroll - void set_scroll(int scroll); + void set_scroll(const int scroll); + + /// set scroll offset + void set_offset(const int offset); /// increase scroll - void inc_scroll(int scroll); + void inc_scroll(const int scroll); /// decrease scroll - void dec_scroll(int scroll); + void dec_scroll(const int scroll); + + /// set text alignment + void set_alignment(const unsigned int alignment); protected: /// draw the scroll pane virtual void draw(); + + /// key event handler provides mouse scrolling + virtual bool on_keypress(const int key, const unsigned int modifier); private: ui::Text &scrollpane_text; int scrollpane_scroll; + int scrollpane_offset; unsigned int scrollpane_alignment; }; |