commit 2f8b04a3
Process text in the console. Not drawn yet.
Changed files
| M | src/saland/Game.cpp before |
| M | src/saland/console/Console.cpp before |
| M | src/saland/console/Console.hpp before |
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp
index 196599e..eabf009 100644
--- a/src/saland/Game.cpp
+++ b/src/saland/Game.cpp
@@ -276,22 +276,27 @@ static bool Intersect(const Placeable& p1, const Placeable& p2) {
}
void Game::Update() {
+ if (data->c) {
+ data->c->Update();
+ }
Uint32 nowTime = SDL_GetTicks();
Uint32 deltaTime = nowTime - data->lastUpdate;
const Uint8 *state = SDL_GetKeyboardState(NULL);
float deltaX = 0.0f;
float deltaY = 0.0f;
- if (state[SDL_SCANCODE_DOWN]) {
- deltaY += 1.0f;
- }
- if (state[SDL_SCANCODE_UP]) {
- deltaY -= 1.0f;
- }
- if (state[SDL_SCANCODE_RIGHT]) {
- deltaX += 1.0f;
- }
- if (state[SDL_SCANCODE_LEFT]) {
- deltaX -= 1.0f;
+ if (!data->c) {
+ if (state[SDL_SCANCODE_DOWN]) {
+ deltaY += 1.0f;
+ }
+ if (state[SDL_SCANCODE_UP]) {
+ deltaY -= 1.0f;
+ }
+ if (state[SDL_SCANCODE_RIGHT]) {
+ deltaX += 1.0f;
+ }
+ if (state[SDL_SCANCODE_LEFT]) {
+ deltaX -= 1.0f;
+ }
}
data->human->moveX = deltaX;
data->human->moveY = deltaY;
@@ -353,7 +358,7 @@ void Game::Update() {
if (data->c && !data->c->IsActive()) {
data->c = nullptr;
}
- if (SDL_GetMouseState(nullptr,nullptr) & 1) {
+ if (SDL_GetMouseState(nullptr,nullptr) & 1 && !data->c) {
if (data->human->castTimeRemaining == 0) {
data->human->castTimeRemaining = data->human->castTime;
std::shared_ptr<Projectile> projectile = std::make_shared<Projectile>();
diff --git a/src/saland/console/Console.cpp b/src/saland/console/Console.cpp
index 3b04b6c..3bb2b1d 100644
--- a/src/saland/console/Console.cpp
+++ b/src/saland/console/Console.cpp
@@ -26,9 +26,12 @@ https://github.com/sago007/saland
#include "../globals.hpp"
Console::Console() {
+ editPosition = editLine.begin();
+ SDL_StartTextInput();
}
Console::~Console() {
+ SDL_StopTextInput();
}
bool Console::IsActive() {
@@ -39,12 +42,70 @@ void Console::Draw(SDL_Renderer* target) {
DrawRectYellow(target, sideBoarder, 10, globalData.ysize/2, globalData.xsize - sideBoarder*2);
}
+void Console::putchar(const std::string& thing) {
+ {
+ int oldPostition = utf8::distance(editLine.begin(), editPosition);
+ int lengthOfInsertString = utf8::distance(thing.begin(), thing.end());
+ editLine.insert(editPosition, thing.begin(), thing.end());
+ editPosition = editLine.begin(); //Inserting may destroy our old iterator
+ utf8::advance(editPosition, oldPostition + lengthOfInsertString, editLine.end());
+ }
+}
+
+void Console::removeChar() {
+ if (editPosition < editLine.end()) {
+ std::string::iterator endChar= editPosition;
+ utf8::advance(endChar, 1, editLine.end());
+ editLine.erase(editPosition, endChar);
+ }
+}
+
+bool Console::ReadKey(SDL_Keycode keyPressed) {
+ if (keyPressed == SDLK_DELETE) {
+ if ((editLine.length()>0)&& (editPosition<editLine.end())) {
+ removeChar();
+ }
+ return true;
+ }
+ if (keyPressed == SDLK_BACKSPACE) {
+ if (editPosition>editLine.begin()) {
+ utf8::prior(editPosition, editLine.begin());
+ removeChar();
+ return true;
+ }
+ return false;
+ }
+ if (keyPressed == SDLK_HOME) {
+ editPosition = editLine.begin();
+ return true;
+ }
+ if (keyPressed == SDLK_END) {
+ editPosition=editLine.end();
+ return true;
+ }
+ if ((keyPressed == SDLK_LEFT) && (editPosition>editLine.begin())) {
+ utf8::prior(editPosition, editLine.begin());
+ return true;
+ }
+ if ((keyPressed == SDLK_RIGHT) && (editPosition<editLine.end())) {
+ utf8::next(editPosition, editLine.end());
+ return true;
+ }
+ return true;
+}
+
void Console::ProcessInput(const SDL_Event& event, bool &processed) {
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_ESCAPE && event.key.keysym.mod & KMOD_LSHIFT) {
active = false;
processed = true;
}
+ else {
+ processed = ReadKey(event.key.keysym.sym);
+ }
+ }
+ if (event.type == SDL_TEXTINPUT) {
+ putchar(event.text.text);
}
}
diff --git a/src/saland/console/Console.hpp b/src/saland/console/Console.hpp
index dd07e07..9513ed7 100644
--- a/src/saland/console/Console.hpp
+++ b/src/saland/console/Console.hpp
@@ -25,6 +25,8 @@ https://github.com/sago007/saland
#define CONSOLE_HPP
#include "../../sago/GameStateInterface.hpp"
+#include <string>
+#include "utf8.h"
class Console : public sago::GameStateInterface {
public:
@@ -37,7 +39,12 @@ public:
Console& operator=(const Console&) = delete;
virtual ~Console();
private:
+ void putchar(const std::string& thing);
+ bool ReadKey(SDL_Keycode keyPressed);
+ void removeChar();
bool active = true;
+ std::string editLine;
+ std::string::iterator editPosition;
};
#endif /* CONSOLE_HPP */