2020-02-20 15:48:15 +09:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* OpenMiner
|
2020-02-25 01:42:10 +09:00
|
|
|
*
|
2020-02-20 15:48:15 +09:00
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2020-02-25 01:42:10 +09:00
|
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
|
|
*
|
|
|
|
* This file is part of OpenMiner.
|
2020-02-20 15:48:15 +09:00
|
|
|
*
|
2020-02-25 01:42:10 +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.
|
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* 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
|
2020-02-25 01:42:10 +09:00
|
|
|
* 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>
|
|
|
|
|
2020-02-22 00:31:11 +09:00
|
|
|
#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"
|
2020-02-22 00:31:11 +09:00
|
|
|
#include "Config.hpp"
|
2020-02-20 15:48:15 +09:00
|
|
|
|
2020-02-22 03:44:12 +09:00
|
|
|
ChatState::ChatState(ClientCommandHandler &clientCommandHandler, Chat &chat, bool addSlash, gk::ApplicationState *parent)
|
2020-02-22 00:31:11 +09:00
|
|
|
: 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();
|
|
|
|
|
2020-02-20 17:07:45 +09:00
|
|
|
m_drawBackground = false;
|
|
|
|
|
2020-02-20 15:48:15 +09:00
|
|
|
m_textInput.setScale(Config::guiScale, Config::guiScale);
|
2020-02-20 17:07:45 +09:00
|
|
|
m_textInput.setBackgroundColor(gk::Color{0, 0, 0, 127});
|
|
|
|
m_textInput.setPadding(1, 1);
|
2020-02-22 00:31:11 +09:00
|
|
|
|
2020-06-26 12:09:13 +02:00
|
|
|
updateWidgetPosition();
|
2020-02-22 01:44:00 +09:00
|
|
|
|
2020-02-22 03:44:12 +09:00
|
|
|
if (addSlash)
|
2020-06-20 01:24:06 +02:00
|
|
|
m_textInput.setString("/");
|
2020-02-22 03:44:12 +09:00
|
|
|
|
2020-02-22 00:31:11 +09:00
|
|
|
m_chat.setMessageVisibility(true);
|
2020-02-20 15:48:15 +09:00
|
|
|
}
|
|
|
|
|
2020-07-07 19:33:01 +02:00
|
|
|
void ChatState::onEvent(const SDL_Event &event) {
|
2020-02-20 15:48:15 +09:00
|
|
|
InterfaceState::onEvent(event);
|
|
|
|
|
|
|
|
m_textInput.onEvent(event);
|
|
|
|
|
2020-07-07 19:33:01 +02:00
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
|
2020-02-20 15:48:15 +09:00
|
|
|
gk::Mouse::setCursorGrabbed(true);
|
|
|
|
gk::Mouse::setCursorVisible(false);
|
|
|
|
gk::Mouse::resetToWindowCenter();
|
|
|
|
|
2020-02-22 00:31:11 +09:00
|
|
|
m_chat.setMessageVisibility(false);
|
|
|
|
|
2020-02-25 22:11:02 +09:00
|
|
|
if (!m_stateStack->empty())
|
|
|
|
m_stateStack->pop();
|
2020-02-20 15:48:15 +09:00
|
|
|
}
|
2020-02-21 17:25:56 +09:00
|
|
|
|
2020-07-07 19:33:01 +02:00
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN) {
|
2020-06-20 01:24:06 +02:00
|
|
|
if (!m_textInput.string().empty()) {
|
|
|
|
m_clientCommandHandler.sendChatMessage(m_textInput.string());
|
|
|
|
m_chat.addHistoryEntry(m_textInput.string());
|
2020-06-20 01:07:28 +02:00
|
|
|
}
|
2020-02-21 17:50:45 +09:00
|
|
|
|
|
|
|
gk::Mouse::setCursorGrabbed(true);
|
|
|
|
gk::Mouse::setCursorVisible(false);
|
|
|
|
gk::Mouse::resetToWindowCenter();
|
2020-02-21 17:25:56 +09:00
|
|
|
|
2020-02-22 00:31:11 +09:00
|
|
|
m_chat.setMessageVisibility(false);
|
|
|
|
|
2020-02-25 22:11:02 +09:00
|
|
|
if (!m_stateStack->empty())
|
|
|
|
m_stateStack->pop();
|
2020-02-21 17:25:56 +09:00
|
|
|
}
|
2020-06-20 01:07:28 +02:00
|
|
|
|
2020-07-07 19:33:01 +02:00
|
|
|
if (event.type == SDL_KEYDOWN) {
|
|
|
|
if (event.key.keysym.sym == SDLK_UP && m_currentHistoryEntry < (int)m_chat.historySize() - 1) {
|
2020-06-20 01:07:28 +02:00
|
|
|
if (m_currentHistoryEntry == -1)
|
2020-06-20 01:24:06 +02:00
|
|
|
m_oldEntry = m_textInput.string();
|
|
|
|
m_textInput.setString(m_chat.getHistoryEntry(++m_currentHistoryEntry));
|
2020-06-20 01:07:28 +02:00
|
|
|
}
|
2020-07-07 19:33:01 +02:00
|
|
|
else if (event.key.keysym.sym == SDLK_DOWN && m_currentHistoryEntry >= 0) {
|
2020-06-20 01:07:28 +02:00
|
|
|
--m_currentHistoryEntry;
|
|
|
|
if (m_currentHistoryEntry == -1)
|
2020-06-20 01:24:06 +02:00
|
|
|
m_textInput.setString(m_oldEntry);
|
2020-06-20 01:07:28 +02:00
|
|
|
else
|
2020-06-20 01:24:06 +02:00
|
|
|
m_textInput.setString(m_chat.getHistoryEntry(m_currentHistoryEntry));
|
2020-06-20 01:07:28 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-20 15:48:15 +09:00
|
|
|
}
|
|
|
|
|
2020-06-26 12:09:13 +02:00
|
|
|
void ChatState::updateWidgetPosition() {
|
|
|
|
m_textInput.setPosition(4, Config::screenHeight - 12 * Config::guiScale);
|
|
|
|
m_textInput.setBackgroundSize(Config::screenWidth / Config::guiScale - 4, 10);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|