aboutsummaryrefslogtreecommitdiff
path: root/src/Chatbar.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-04-22 05:58:44 +0200
committerdec05eba <dec05eba@protonmail.com>2018-04-22 05:59:18 +0200
commit1e0e68f9cda51c881b32a54d9eece71c1428f7ac (patch)
treeb8faa1d971c245e3fcf046aa1d2daa1fa601e0f9 /src/Chatbar.cpp
parent424b02609fa34175a4e2aadb95e68b3c9c8dc93c (diff)
Add video and gif support
Gif streams from url. Todo: Add play controls to video
Diffstat (limited to 'src/Chatbar.cpp')
-rw-r--r--src/Chatbar.cpp76
1 files changed, 72 insertions, 4 deletions
diff --git a/src/Chatbar.cpp b/src/Chatbar.cpp
index 14edba7..bdfc75d 100644
--- a/src/Chatbar.cpp
+++ b/src/Chatbar.cpp
@@ -1,7 +1,9 @@
#include "../include/Chatbar.hpp"
#include "../include/ResourceCache.hpp"
#include "../include/Settings.hpp"
+#include "../include/Channel.hpp"
#include <cmath>
+#include <cstring>
using namespace std;
@@ -10,10 +12,13 @@ namespace dchat
const float FONT_SIZE = 24;
const float BOX_PADDING_X = 15.0f;
const float BOX_PADDING_Y = 5.0f;
+ const int BLINK_TIME_VISIBLE_MS = 500;
+ const int BLINK_TIME_INVISIBLE_MS = 500;
Chatbar::Chatbar() :
text("", ResourceCache::getFont("fonts/Roboto-Regular.ttf"), FONT_SIZE * Settings::getScaling()),
- caretIndex(0)
+ caretIndex(0),
+ focused(true)
{
text.setFillColor(sf::Color(240, 240, 240));
background.setFillColor(sf::Color(60, 60, 60));
@@ -26,6 +31,7 @@ namespace dchat
text.setString(str);
++caretIndex;
caretOffset = text.findCharacterPos(caretIndex) - text.getPosition();
+ blinkTimer.restart();
}
const sf::String& Chatbar::getString() const
@@ -43,6 +49,7 @@ namespace dchat
--caretIndex;
caretOffset = text.findCharacterPos(caretIndex) - text.getPosition();
}
+ blinkTimer.restart();
}
void Chatbar::removeNextChar()
@@ -53,6 +60,7 @@ namespace dchat
str.erase(caretIndex);
text.setString(str);
}
+ blinkTimer.restart();
}
void Chatbar::clear()
@@ -61,6 +69,7 @@ namespace dchat
caretIndex = 0;
caretOffset.x = 0.0f;
caretOffset.y = 0.0f;
+ blinkTimer.restart();
}
void Chatbar::moveCaretLeft()
@@ -68,12 +77,64 @@ namespace dchat
caretIndex = max(0, caretIndex - 1);
// TODO: Use glyph size to optimize this, no need to iterate all glyphs
caretOffset = text.findCharacterPos(caretIndex) - text.getPosition();
+ blinkTimer.restart();
}
void Chatbar::moveCaretRight()
{
caretIndex = min((int)text.getString().getSize(), caretIndex + 1);
caretOffset = text.findCharacterPos(caretIndex) - text.getPosition();
+ blinkTimer.restart();
+ }
+
+ bool Chatbar::isFocused() const
+ {
+ return focused;
+ }
+
+ void Chatbar::processEvent(const sf::Event &event, Channel *channel)
+ {
+ if(!focused) return;
+
+ if(event.type == sf::Event::TextEntered)
+ {
+ if(event.text.unicode == 8) // backspace
+ removePreviousChar();
+ else if(event.text.unicode == 13) // enter
+ {
+ if(!getString().isEmpty())
+ {
+ if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::Key::RShift))
+ {
+ addChar('\n');
+ }
+ else
+ {
+ auto chatbarMsgUtf8 = getString().toUtf8();
+ string msg;
+ msg.resize(chatbarMsgUtf8.size());
+ memcpy(&msg[0], chatbarMsgUtf8.data(), chatbarMsgUtf8.size());
+ channel->getMessageBoard().addMessage(Message::buildFromString(channel->getLocalUser(), msg));
+ clear();
+ }
+ }
+ }
+ else if(event.text.unicode == 127) // delete
+ {
+ removeNextChar();
+ }
+ else
+ {
+ addChar(event.text.unicode);
+ }
+ }
+ else if(event.type == sf::Event::KeyPressed)
+ {
+ if(event.key.code == sf::Keyboard::Left)
+ moveCaretLeft();
+ else if(event.key.code == sf::Keyboard::Right)
+ moveCaretRight();
+ }
}
void Chatbar::draw(sf::RenderWindow &window)
@@ -89,8 +150,15 @@ namespace dchat
window.draw(background);
window.draw(text);
- sf::RectangleShape caretShape(sf::Vector2f(2.0f, backgroundSize.y - BOX_PADDING_Y * 2.0f));
- caretShape.setPosition(floor(text.getPosition().x + caretOffset.x), backgroundPos.y + BOX_PADDING_Y);
- window.draw(caretShape);
+ int blinkElapsedTime = blinkTimer.getElapsedTime().asMilliseconds();
+ if(focused && blinkElapsedTime <= BLINK_TIME_VISIBLE_MS)
+ {
+ sf::RectangleShape caretShape(sf::Vector2f(2.0f, backgroundSize.y - BOX_PADDING_Y * 2.0f));
+ caretShape.setPosition(floor(text.getPosition().x + caretOffset.x), (caretOffset.y + backgroundPos.y + BOX_PADDING_Y));
+ window.draw(caretShape);
+ }
+
+ if(blinkElapsedTime > BLINK_TIME_VISIBLE_MS + BLINK_TIME_INVISIBLE_MS)
+ blinkTimer.restart();
}
}