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-02-22 01:44:00 +09:00
|
|
|
updateTextInputGeometry();
|
|
|
|
|
2020-02-22 03:44:12 +09:00
|
|
|
if (addSlash)
|
|
|
|
m_textInput.setText("/");
|
|
|
|
|
2020-02-22 00:31:11 +09:00
|
|
|
m_chat.setMessageVisibility(true);
|
2020-02-20 15:48:15 +09:00
|
|
|
}
|
|
|
|
|
2020-02-21 15:51:57 +09:00
|
|
|
void ChatState::updateTextInputGeometry() {
|
2020-02-22 01:44:00 +09:00
|
|
|
m_textInput.setPosition(4, Config::screenHeight - 12 * Config::guiScale);
|
2020-02-21 15:51:57 +09:00
|
|
|
m_textInput.setBackgroundSize(Config::screenWidth / Config::guiScale - 4, 10);
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:48:15 +09:00
|
|
|
void ChatState::onEvent(const SDL_Event &event) {
|
|
|
|
InterfaceState::onEvent(event);
|
|
|
|
|
|
|
|
m_textInput.onEvent(event);
|
|
|
|
|
2020-02-21 15:51:57 +09:00
|
|
|
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
|
|
|
|
updateTextInputGeometry();
|
|
|
|
}
|
|
|
|
|
2020-02-20 15:48:15 +09:00
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
|
|
|
|
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-20 15:48:15 +09:00
|
|
|
m_stateStack->pop();
|
|
|
|
}
|
2020-02-21 17:25:56 +09:00
|
|
|
|
|
|
|
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RETURN) {
|
2020-02-21 17:50:45 +09:00
|
|
|
if (!m_textInput.text().empty())
|
|
|
|
m_clientCommandHandler.sendChatMessage(m_textInput.text());
|
|
|
|
|
|
|
|
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-21 17:25:56 +09:00
|
|
|
m_stateStack->pop();
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|