diff options
author | Meway <mewaysid92@gmail.com> | 2025-07-08 23:48:59 -0500 |
---|---|---|
committer | Meway <mewaysid92@gmail.com> | 2025-07-08 23:48:59 -0500 |
commit | f36f0bc8fd28d84ae40e5e3eff2c2ba8a1b769b0 (patch) | |
tree | 0847c54d735532666a5ce11e8d68dbbaf0190b6b | |
parent | 3fb53be63095dad2ebcfd52d44f13d20f630e478 (diff) | |
download | mana-npc-choice-wrapping.tar.gz mana-npc-choice-wrapping.tar.bz2 mana-npc-choice-wrapping.tar.xz mana-npc-choice-wrapping.zip |
Wrap long NPC choice text in ListBox using multiline renderingnpc-choice-wrapping
-rw-r--r-- | src/gui/widgets/listbox.cpp | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/src/gui/widgets/listbox.cpp b/src/gui/widgets/listbox.cpp index 112de232..09f0036b 100644 --- a/src/gui/widgets/listbox.cpp +++ b/src/gui/widgets/listbox.cpp @@ -43,27 +43,57 @@ void ListBox::draw(gcn::Graphics *graphics) graphics->setFont(getFont()); - const int height = getRowHeight(); + const int boxWidth = getWidth() - 4; + gcn::Font *font = getFont(); + graphics->setColor(Theme::getThemeColor(Theme::TEXT)); - // Draw filled rectangle around the selected list element - if (mSelected >= 0) - { - auto highlightColor = Theme::getThemeColor(Theme::HIGHLIGHT); - highlightColor.a = gui->getTheme()->getGuiAlpha(); - graphics->setColor(highlightColor); - graphics->fillRectangle(gcn::Rectangle(0, height * mSelected, - getWidth(), height)); - } + int y = 0; - // Draw the list elements - graphics->setColor(Theme::getThemeColor(Theme::TEXT)); - for (int i = 0, y = 0; i < mListModel->getNumberOfElements(); - ++i, y += height) + for (int i = 0; i < mListModel->getNumberOfElements(); ++i) { - graphics->drawText(mListModel->getElementAt(i), 1, y); + std::string text = mListModel->getElementAt(i); + + // Word wrap + std::vector<std::string> lines; + std::istringstream stream(text); + std::string word, currentLine; + + while (stream >> word) + { + std::string testLine = currentLine.empty() ? word : currentLine + " " + word; + if (font->getWidth(testLine) < boxWidth) + { + currentLine = testLine; + } + else + { + lines.push_back(currentLine); + currentLine = word; + } + } + if (!currentLine.empty()) + lines.push_back(currentLine); + + // Highlight if selected + if (mSelected == i) + { + auto highlightColor = Theme::getThemeColor(Theme::HIGHLIGHT); + highlightColor.a = gui->getTheme()->getGuiAlpha(); + graphics->setColor(highlightColor); + graphics->fillRectangle(gcn::Rectangle(0, y, getWidth(), lines.size() * getRowHeight())); + graphics->setColor(Theme::getThemeColor(Theme::TEXT)); // Reset color + } + + // Draw each line + for (const std::string &line : lines) + { + graphics->drawText(line, 1, y); + y += getRowHeight(); + } } } + void ListBox::keyPressed(gcn::KeyEvent& keyEvent) { const gcn::Key key = keyEvent.getKey(); |