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 = std::make_shared(); 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)&& (editPositioneditLine.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 +#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 */