diff options
Diffstat (limited to 'src/Text.cpp')
-rw-r--r-- | src/Text.cpp | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/src/Text.cpp b/src/Text.cpp index be147db..9ea4847 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -2,6 +2,7 @@ #include "../include/Cache.hpp" #include "../include/Gif.hpp" #include "../include/WebPagePreview.hpp" +#include "../include/ColorScheme.hpp" #include <SFML/Graphics/RectangleShape.hpp> #include <cmath> @@ -27,7 +28,6 @@ namespace dchat plainText(false), editable(false), caretMoveDirection(CaretMoveDirection::NONE), - totalHeight(0.0f), caretIndex(0) { @@ -46,7 +46,6 @@ namespace dchat plainText(_plainText), editable(false), caretMoveDirection(CaretMoveDirection::NONE), - totalHeight(0.0f), lineSpacing(0.0f), caretIndex(0) { @@ -126,7 +125,7 @@ namespace dchat float Text::getHeight() const { - return totalHeight; + return boundingBox.height; } size_t stringSplitUrl(const StringViewUtf32 textElementStr, const sf::String &urlStr, size_t offset, std::vector<TextElement> &newTextElements) @@ -282,6 +281,8 @@ namespace dchat float hspace = font->getGlyph(' ', characterSize, false).advance; float vspace = font->getLineSpacing(characterSize); + boundingBox = sf::FloatRect(); + sf::Vector2f glyphPos; sf::Uint32 prevCodePoint = 0; size_t lastSpacingWordWrapIndex = -1; @@ -335,6 +336,7 @@ namespace dchat glyphPos.y += vspace + lineSpacing; } + boundingBox.width = std::max(boundingBox.width, glyphPos.x); continue; } @@ -451,7 +453,14 @@ namespace dchat glyphPos.y += floor(vspace * IMAGE_HEIGHT_SCALE) + lineSpacing; } } - totalHeight = glyphPos.y + vspace + lineSpacing; + + boundingBox.height = glyphPos.y + vspace + lineSpacing; + usize numVertices = vertices.getVertexCount(); + for(usize i = 0; i < numVertices; i += 4) + { + const sf::Vertex &bottomRight = vertices[i + 2]; + boundingBox.width = std::max(boundingBox.width, bottomRight.position.x); + } } void Text::updateCaret() @@ -616,7 +625,9 @@ namespace dchat void Text::processEvent(const sf::Event &event) { - if(!editable) return; + if(!editable || textElements.size() == 0) return; + + bool caretAtEnd = textElements[0].text.size == 0 || caretIndex == textElements[0].text.size; if(event.type == sf::Event::KeyPressed) { @@ -625,7 +636,7 @@ namespace dchat --caretIndex; dirtyCaret = true; } - else if(event.key.code == sf::Keyboard::Right && !isCaretAtEnd()) + else if(event.key.code == sf::Keyboard::Right && !caretAtEnd) { ++caretIndex; dirtyCaret = true; @@ -638,7 +649,7 @@ namespace dchat --caretIndex; dirtyCaret = true; } - else if(event.key.code == sf::Keyboard::Delete && !isCaretAtEnd()) + else if(event.key.code == sf::Keyboard::Delete && !caretAtEnd) { auto strBefore = str.substring(0, caretIndex); auto strAfter = str.substring(caretIndex + 1); @@ -666,7 +677,7 @@ namespace dchat if(event.text.unicode == 8 || event.text.unicode == 127) // backspace, del return; - if(isCaretAtEnd()) + if(caretAtEnd) str += event.text.unicode; else { @@ -715,7 +726,15 @@ namespace dchat //sf::FloatRect textRect(pos.x, pos.y, maxWidth, ) //colRect.contains() //if(pos.x + maxWidth <= 0.0f || pos.x >= maxWidth || pos.y + totalHeight <= 0.0f || pos.y >= target.getSize().y) return; - if(pos.y + totalHeight <= 0.0f || pos.y >= target.getSize().y) return; + if(pos.y + getHeight() <= 0.0f || pos.y >= target.getSize().y) return; + + if(editable) + { + sf::RectangleShape editBox(sf::Vector2f(std::max(maxWidth, boundingBox.width), boundingBox.height)); + editBox.setPosition(pos.x, pos.y - floor(vspace)); + editBox.setFillColor(ColorScheme::getBackgroundColor() + sf::Color(10, 10, 10)); + target.draw(editBox); + } states.transform.translate(pos); states.texture = &font->getTexture(characterSize); |