diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2015-10-31 14:44:23 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2015-10-31 14:44:23 +0100 |
commit | 5d76d943eca9734f7df2dc351871815385c571b3 (patch) | |
tree | 6f395e870283b43702e75e320dd53ed316e22db9 | |
parent | bc931baa7a31857b971b9ac6543b2af20cdd1bf1 (diff) |
Font refactoring. Some rendering optimizations on ScrollBar, LineEdit and ListBoxBasic.
-rw-r--r-- | plugingui/font.cc | 159 | ||||
-rw-r--r-- | plugingui/font.h | 41 | ||||
-rw-r--r-- | plugingui/lineedit.cc | 32 | ||||
-rw-r--r-- | plugingui/listboxbasic.cc | 19 | ||||
-rw-r--r-- | plugingui/scrollbar.cc | 10 |
5 files changed, 133 insertions, 128 deletions
diff --git a/plugingui/font.cc b/plugingui/font.cc index 828286c..79e3ab9 100644 --- a/plugingui/font.cc +++ b/plugingui/font.cc @@ -26,106 +26,95 @@ */ #include "font.h" -#ifdef X11 -#include <X11/Xlib.h> -#endif/*X11*/ +namespace GUI { -GUI::Font::Font(std::string fontfile) - : img_font(fontfile) +Font::Font(const std::string& fontfile) + : img_font(fontfile) { - int px = 0; - int c; - for(c = 0; c < 255 && px < (int)img_font.width(); c++) { - character_offset[c] = px + 1; - character_pre_bias[c] = 0; - character_post_bias[c] = 0; - if(c > 0) { - character_width[c - 1] = - character_offset[c] - character_offset[c - 1] - 1; - } - px++; - GUI::Colour c; - while(px < (int)img_font.width()) { - c = img_font.getPixel(px, 0); - // Find next purple pixel in top row: - if(c.red == 1 && c.green == 0 && c.blue == 1 && c.alpha == 1) break; - px++; - } - } - c++; - character_width[c - 1] = character_offset[c] - character_offset[c - 1] - 1; -} - -void GUI::Font::setFace(std::string face) -{ - face = face; -} + size_t px = 0; + size_t c; -std::string GUI::Font::face() -{ - return "";//face; -} + for(c = 0; c < characters.size() && px < img_font.width(); ++c) + { + auto& character = characters[c]; + character.offset = px + 1; + character.pre_bias = 0; + character.post_bias = 0; -void GUI::Font::setSize(size_t points) -{ - points = points; -} + if(c > 0) + { + characters[c - 1].width = + character.offset - characters[c - 1].offset - 1; + } -size_t GUI::Font::size() -{ - return 0; + ++px; + + Colour pixel; + while(px < img_font.width()) + { + pixel = img_font.getPixel(px, 0); + + // Find next purple pixel in top row: + if((pixel.red == 1) && (pixel.green == 0) && + (pixel.blue == 1) && (pixel.alpha == 1)) + { + break; + } + + ++px; + } + + characters[c] = character; + } + + ++c; + + characters[c - 1].width = characters[c].offset - characters[c - 1].offset - 1; } -size_t GUI::Font::textWidth(std::string text) +size_t Font::textWidth(const std::string& text) { - size_t len = 0; + size_t len = 0; - for(size_t i = 0; i < text.length(); i++) { - unsigned int cha = text[i]; - len += character_width[(char)cha] + 1 + character_post_bias[(char)cha]; - } + for(size_t i = 0; i < text.length(); i++) + { + unsigned int cha = text[i]; + auto& character = characters[cha]; + len += character.width + 1 + character.post_bias; + } - return len; + return len; } -size_t GUI::Font::textHeight(std::string text) +size_t Font::textHeight(const std::string& text) { - return img_font.height(); + return img_font.height(); } -GUI::PixelBufferAlpha *GUI::Font::render(std::string text) +PixelBufferAlpha *Font::render(const std::string& text) { - int border = 1; - PixelBufferAlpha *pb = - new PixelBufferAlpha(textWidth(text), textHeight(text)); - - int x_offset = 0; - for(size_t i = 0; i < text.length(); i++) { - unsigned int cha = text[i]; - for(size_t x = 0; x < character_width[(char)cha]; x++) { - for(size_t y = 0; y < img_font.height(); y++) { - Colour c = img_font.getPixel(x + (character_offset[(char)cha]), y); - pb->setPixel(x + x_offset + character_pre_bias[(char)cha], y, - c.red * 255, c.green * 255, c.blue * 255, c.alpha * 255); - } - } - x_offset += character_width[(char)cha] + border + - character_post_bias[(char)cha]; - } - - return pb; + int border = 1; + PixelBufferAlpha *pb = + new PixelBufferAlpha(textWidth(text), textHeight(text)); + + int x_offset = 0; + for(size_t i = 0; i < text.length(); ++i) + { + unsigned int cha = text[i]; + auto& character = characters[cha]; + for(size_t x = 0; x < character.width; ++x) + { + for(size_t y = 0; y < img_font.height(); ++y) + { + Colour c = img_font.getPixel(x + character.offset, y); + pb->setPixel(x + x_offset + character.pre_bias, y, + c.red * 255, c.green * 255, c.blue * 255, c.alpha * 255); + } + } + x_offset += character.width + border + character.post_bias; + } + + return pb; } -#ifdef TEST_FONT -//deps: -//cflags: -//libs: -#include "test.h" - -TEST_BEGIN; - -// TODO: Put some testcode here (see test.h for usable macros). - -TEST_END; - -#endif/*TEST_FONT*/ +} // GUI:: diff --git a/plugingui/font.h b/plugingui/font.h index b47fbde..c7fe67e 100644 --- a/plugingui/font.h +++ b/plugingui/font.h @@ -24,11 +24,10 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_FONT_H__ -#define __DRUMGIZMO_FONT_H__ +#pragma once #include <string> -#include <map> +#include <array> #include "pixelbuffer.h" #include "image.h" @@ -37,27 +36,29 @@ namespace GUI { class Font { public: - Font(std::string fontfile = ":font.png"); - - void setFace(std::string face); - std::string face(); + Font(const std::string& fontfile = ":font.png"); - void setSize(size_t points); - size_t size(); + size_t textWidth(const std::string& text); + size_t textHeight(const std::string& text = ""); - size_t textWidth(std::string text); - size_t textHeight(std::string text = ""); - - PixelBufferAlpha *render(std::string text); + PixelBufferAlpha *render(const std::string& text); private: - Image img_font; + Image img_font; - std::map<char, int> character_offset; - std::map<char, size_t> character_width; - std::map<char, int> character_pre_bias; - std::map<char, int> character_post_bias; -}; + struct Character { + int offset = 0; + size_t width = 0; + int pre_bias = 0; + int post_bias = 0; + }; + std::array<Character, 255> characters; + +// std::map<char, int> character_offset; +// std::map<char, size_t> character_width; +// std::map<char, int> character_pre_bias; +// std::map<char, int> character_post_bias; }; -#endif/*__DRUMGIZMO_FONT_H__*/ + +} // GUI:: diff --git a/plugingui/lineedit.cc b/plugingui/lineedit.cc index 9c2420c..12969ba 100644 --- a/plugingui/lineedit.cc +++ b/plugingui/lineedit.cc @@ -124,16 +124,33 @@ void LineEdit::keyEvent(KeyEvent *keyEvent) { switch(keyEvent->keycode) { case KeyEvent::KeyLeft: - if(pos) + if(pos == 0) { - pos--; + return; } + + pos--; + if(offsetPos >= pos) { walkstate = WalkLeft; } break; + case KeyEvent::KeyRight: + if(pos == _text.length()) + { + return; + } + + pos++; + + if((pos < _text.length()) && ((offsetPos + visibleText.length()) <= pos)) + { + walkstate = WalkRight; + } + break; + case KeyEvent::KeyHome: pos = 0; visibleText = _text; @@ -146,17 +163,6 @@ void LineEdit::keyEvent(KeyEvent *keyEvent) offsetPos = 0; break; - case KeyEvent::KeyRight: - if(pos < _text.length()) - { - pos++; - } - if((pos < _text.length()) && ((offsetPos + visibleText.length()) <= pos)) - { - walkstate = WalkRight; - } - break; - case KeyEvent::KeyDelete: if(pos < _text.length()) { diff --git a/plugingui/listboxbasic.cc b/plugingui/listboxbasic.cc index bf75684..fa1601f 100644 --- a/plugingui/listboxbasic.cc +++ b/plugingui/listboxbasic.cc @@ -91,6 +91,7 @@ void ListBoxBasic::addItems(std::vector<ListBoxBasic::Item> &newItems) int numitems = height() / (font.textHeight() + padding); scroll.setRange(numitems); scroll.setMaximum(items.size()); + repaintEvent(nullptr); } void ListBoxBasic::clear() @@ -163,7 +164,7 @@ void ListBoxBasic::repaintEvent(RepaintEvent *e) for(int idx = skip; (idx < (int)items.size()) && (idx < (skip + numitems)); idx++) { - ListBoxBasic::Item *i = &items[idx]; + auto& item = items[idx]; if(idx == selected) { p.setColour(Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 0.5)); @@ -183,7 +184,7 @@ void ListBoxBasic::repaintEvent(RepaintEvent *e) p.setColour(Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); - p.drawText(2, yoffset + font.textHeight(), font, i->name); + p.drawText(2, yoffset + font.textHeight(), font, item.name); yoffset += font.textHeight() + padding; } } @@ -202,12 +203,13 @@ void ListBoxBasic::keyEvent(KeyEvent *e) switch(e->keycode) { case KeyEvent::KeyUp: - marked--; - if(marked < 0) + if(marked == 0) { - marked = 0; + return; } + marked--; + if(marked < scroll.value()) { scroll.setValue(marked); @@ -219,12 +221,13 @@ void ListBoxBasic::keyEvent(KeyEvent *e) // Number of items that can be displayed at a time. int numitems = height() / (font.textHeight() + padding); - marked++; - if(marked > ((int)items.size() - 1)) + if(marked == ((int)items.size() - 1)) { - marked = (int)items.size() - 1; + return; } + marked++; + if(marked > (scroll.value() + numitems - 1)) { scroll.setValue(marked - numitems + 1); diff --git a/plugingui/scrollbar.cc b/plugingui/scrollbar.cc index 0b10972..3107987 100644 --- a/plugingui/scrollbar.cc +++ b/plugingui/scrollbar.cc @@ -66,9 +66,15 @@ int GUI::ScrollBar::maximum() void GUI::ScrollBar::setValue(int value) { + if(value > max - ran) value = max - ran; + if(value < 0) value = 0; + + if(val == value) + { + return; + } + val = value; - if(val > max - ran) val = max - ran; - if(val < 0) val = 0; if(handler) handler(ptr); |