OpenMiner/source/client/states/ChatState.cpp
2020-06-20 01:24:06 +02:00

124 lines
3.8 KiB
C++

/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#include <gk/core/ApplicationStateStack.hpp>
#include <gk/core/Mouse.hpp>
#include "Chat.hpp"
#include "ChatState.hpp"
#include "ClientCommandHandler.hpp"
#include "Config.hpp"
ChatState::ChatState(ClientCommandHandler &clientCommandHandler, Chat &chat, bool addSlash, gk::ApplicationState *parent)
: InterfaceState(parent), m_clientCommandHandler(clientCommandHandler), m_chat(chat)
{
gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true);
gk::Mouse::resetToWindowCenter();
m_drawBackground = false;
m_textInput.setScale(Config::guiScale, Config::guiScale);
m_textInput.setBackgroundColor(gk::Color{0, 0, 0, 127});
m_textInput.setPadding(1, 1);
updateTextInputGeometry();
if (addSlash)
m_textInput.setString("/");
m_chat.setMessageVisibility(true);
}
void ChatState::updateTextInputGeometry() {
m_textInput.setPosition(4, Config::screenHeight - 12 * Config::guiScale);
m_textInput.setBackgroundSize(Config::screenWidth / Config::guiScale - 4, 10);
}
void ChatState::onEvent(const sf::Event &event) {
InterfaceState::onEvent(event);
m_textInput.onEvent(event);
if (event.type == sf::Event::Resized) {
updateTextInputGeometry();
}
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Escape) {
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);
gk::Mouse::resetToWindowCenter();
m_chat.setMessageVisibility(false);
if (!m_stateStack->empty())
m_stateStack->pop();
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Return) {
if (!m_textInput.string().empty()) {
m_clientCommandHandler.sendChatMessage(m_textInput.string());
m_chat.addHistoryEntry(m_textInput.string());
}
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);
gk::Mouse::resetToWindowCenter();
m_chat.setMessageVisibility(false);
if (!m_stateStack->empty())
m_stateStack->pop();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Up && m_currentHistoryEntry < (int)m_chat.historySize() - 1) {
if (m_currentHistoryEntry == -1)
m_oldEntry = m_textInput.string();
m_textInput.setString(m_chat.getHistoryEntry(++m_currentHistoryEntry));
}
else if (event.key.code == sf::Keyboard::Down && m_currentHistoryEntry >= 0) {
--m_currentHistoryEntry;
if (m_currentHistoryEntry == -1)
m_textInput.setString(m_oldEntry);
else
m_textInput.setString(m_chat.getHistoryEntry(m_currentHistoryEntry));
}
}
}
void ChatState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (m_parent)
target.draw(*m_parent, states);
if (&m_stateStack->top() == this) {
prepareDraw(target, states);
target.draw(m_textInput, states);
}
}