OpenMiner/source/client/states/ChatState.cpp

124 lines
3.8 KiB
C++
Raw Normal View History

2020-02-20 15:48:15 +09:00
/*
* =====================================================================================
*
* OpenMiner
*
2020-02-20 15:48:15 +09:00
* 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.
2020-02-20 15:48:15 +09:00
*
* OpenMiner is free software; you can redistribute it and/or
2020-02-20 15:48:15 +09:00
* 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,
2020-02-20 15:48:15 +09:00
* 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.,
2020-02-20 15:48:15 +09:00
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#include <gk/core/ApplicationStateStack.hpp>
#include <gk/core/Mouse.hpp>
#include "Chat.hpp"
2020-02-20 15:48:15 +09:00
#include "ChatState.hpp"
2020-02-21 17:25:56 +09:00
#include "ClientCommandHandler.hpp"
#include "Config.hpp"
2020-02-20 15:48:15 +09:00
ChatState::ChatState(ClientCommandHandler &clientCommandHandler, Chat &chat, bool addSlash, gk::ApplicationState *parent)
: InterfaceState(parent), m_clientCommandHandler(clientCommandHandler), m_chat(chat)
2020-02-21 17:25:56 +09:00
{
2020-02-20 15:48:15 +09:00
gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true);
gk::Mouse::resetToWindowCenter();
m_drawBackground = false;
2020-02-20 15:48:15 +09:00
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);
2020-02-20 15:48:15 +09:00
}
void ChatState::updateTextInputGeometry() {
m_textInput.setPosition(4, Config::screenHeight - 12 * Config::guiScale);
m_textInput.setBackgroundSize(Config::screenWidth / Config::guiScale - 4, 10);
}
2020-05-11 17:30:54 +02:00
void ChatState::onEvent(const sf::Event &event) {
2020-02-20 15:48:15 +09:00
InterfaceState::onEvent(event);
m_textInput.onEvent(event);
2020-05-11 17:30:54 +02:00
if (event.type == sf::Event::Resized) {
updateTextInputGeometry();
}
2020-05-11 17:30:54 +02:00
if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Escape) {
2020-02-20 15:48:15 +09:00
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);
gk::Mouse::resetToWindowCenter();
m_chat.setMessageVisibility(false);
if (!m_stateStack->empty())
m_stateStack->pop();
2020-02-20 15:48:15 +09:00
}
2020-02-21 17:25:56 +09:00
2020-05-11 17:30:54 +02:00
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();
2020-02-21 17:25:56 +09:00
m_chat.setMessageVisibility(false);
if (!m_stateStack->empty())
m_stateStack->pop();
2020-02-21 17:25:56 +09:00
}
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));
}
}
2020-02-20 15:48:15 +09:00
}
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);
}
}