[ChatState|TextInput] Added.

This commit is contained in:
Quentin Bazin 2020-02-20 15:48:15 +09:00
parent 530355cb39
commit 2ecbdec6bb
5 changed files with 216 additions and 0 deletions

View File

@ -0,0 +1,50 @@
/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef TEXTINPUT_HPP_
#define TEXTINPUT_HPP_
#include <gk/core/SDLHeaders.hpp>
#include <gk/graphics/RectangleShape.hpp>
#include "Text.hpp"
class TextInput : public gk::Drawable, public gk::Transformable {
public:
TextInput();
void onEvent(const SDL_Event &event);
private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
Text m_text;
std::string m_content;
u16 m_characterLimit = 0;
char m_cursor = '_';
gk::RectangleShape m_background;
};
#endif // TEXTINPUT_HPP_

View File

@ -0,0 +1,43 @@
/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef CHATSTATE_HPP_
#define CHATSTATE_HPP_
#include "InterfaceState.hpp"
#include "TextInput.hpp"
class ChatState : public InterfaceState {
public:
ChatState(gk::ApplicationState *parent = nullptr);
void onEvent(const SDL_Event &event) override;
void update() override;
private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
TextInput m_textInput;
};
#endif // CHATSTATE_HPP_

View File

@ -0,0 +1,54 @@
/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#include "TextInput.hpp"
TextInput::TextInput() {
m_background.setFillColor(gk::Color::Transparent);
}
void TextInput::onEvent(const SDL_Event &event) {
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_BACKSPACE && !m_content.empty()) {
m_content.erase(m_content.begin() + m_content.length() - 1);
m_text.setText(m_content + m_cursor);
}
if (event.type == SDL_TEXTINPUT) {
std::string text = event.text.text;
for (char c : text) {
if (isprint(c) && (!m_characterLimit || m_content.size() < m_characterLimit)) {
m_content += c;
}
m_text.setText(m_content + m_cursor);
}
}
}
void TextInput::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform *= getTransform();
target.draw(m_text, states);
target.draw(m_background, states);
}

View File

@ -0,0 +1,64 @@
/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
*
* This program 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.
*
* This program 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 this program; 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 "Config.hpp"
#include "ChatState.hpp"
ChatState::ChatState(gk::ApplicationState *parent) : InterfaceState(parent) {
gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true);
gk::Mouse::resetToWindowCenter();
m_textInput.setScale(Config::guiScale, Config::guiScale);
}
void ChatState::onEvent(const SDL_Event &event) {
InterfaceState::onEvent(event);
m_textInput.onEvent(event);
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
gk::Mouse::setCursorGrabbed(true);
gk::Mouse::setCursorVisible(false);
gk::Mouse::resetToWindowCenter();
m_stateStack->pop();
}
}
void ChatState::update() {
}
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);
}
}

View File

@ -32,6 +32,7 @@
#include <gk/gl/OpenGL.hpp> #include <gk/gl/OpenGL.hpp>
#include <gk/resource/ResourceHandler.hpp> #include <gk/resource/ResourceHandler.hpp>
#include "ChatState.hpp"
#include "GameKey.hpp" #include "GameKey.hpp"
#include "GameState.hpp" #include "GameState.hpp"
#include "LuaGUIState.hpp" #include "LuaGUIState.hpp"
@ -78,6 +79,10 @@ void GameState::onEvent(const SDL_Event &event) {
else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) { else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
m_stateStack->push<PauseMenuState>(this); m_stateStack->push<PauseMenuState>(this);
} }
// FIXME: Use GamePad/GameKey instead of a hardcoded keycode
else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_t) {
m_stateStack->push<ChatState>(this);
}
else if (event.type == SDL_WINDOWEVENT) { else if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
// FIXME // FIXME