git repos / blockattack-game

commit c634a901

Poul Sander · 2020-05-24 19:37
c634a90125054c90246eeb620fb70d24b2a3cf49 patch · browse files
parent afe1bac4e3f0c79f2f73cedfbeff19877daf3212

Now possible to enter a name entierly with the mouse. Left, Right and Backspace has been crated using some reserved chars.

Changed files

M source/code/DialogBox.cpp before
M source/code/DialogBox.hpp before
M source/code/ReadKeyboard.cpp before
M source/code/ReadKeyboard.h before
diff --git a/source/code/DialogBox.cpp b/source/code/DialogBox.cpp index 8ef8556..b84974f 100644 --- a/source/code/DialogBox.cpp +++ b/source/code/DialogBox.cpp
@@ -134,7 +134,6 @@ static bool insideRect (int x, int y, int height, int width) {
// For use with the mouse/gamepad keyboard
int keyboardRowLimit = 20;
-
void DialogBox::Draw(SDL_Renderer* target) {
DrawBackground(globalData.screen);
this->x = globalData.xsize/2-300;
@@ -210,8 +209,20 @@ void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
auto topx = globalData.xsize/2-400+(i%keyboardRowLimit)*40-5;
auto topy = globalData.ysize/2+150+(i/keyboardRowLimit)*40-5;
if (insideRect(topx, topy, 30, 30)) {
- std::cout << f.GetText() << " pressed\n";
- rk->putchar(f.GetText());
+ std::string insertChar = f.GetText();
+ std::cout << insertChar << " pressed\n";
+ if (insertChar == backspace) {
+ rk->emulateBackspace();
+ }
+ else if (insertChar == leftChar) {
+ rk->cursorLeft();
+ }
+ else if (insertChar == rightChar) {
+ rk->cursorRight();
+ }
+ else {
+ rk->putchar(f.GetText());
+ }
}
}
}
diff --git a/source/code/DialogBox.hpp b/source/code/DialogBox.hpp index 2561541..7fcff21 100644 --- a/source/code/DialogBox.hpp +++ b/source/code/DialogBox.hpp
@@ -61,7 +61,10 @@ private:
sago::SagoTextField cursorLabel;
std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
- ".,:!?+_^@#%&=*";
+ ".,:!?+_^@#%&=*" "‹›«";
+ const std::string leftChar = "‹";
+ const std::string rightChar = "›";
+ const std::string backspace = "«";
std::vector<std::string> gamePadChars;
std::vector<sago::SagoTextField> gamePadCharFields;
int selectedChar = 0;
diff --git a/source/code/ReadKeyboard.cpp b/source/code/ReadKeyboard.cpp index ee806f5..d88c529 100644 --- a/source/code/ReadKeyboard.cpp +++ b/source/code/ReadKeyboard.cpp
@@ -71,6 +71,30 @@ bool ReadKeyboard::ReadKey(const SDL_Event& key) {
return ReadKey(key.key.keysym.sym);
}
+bool ReadKeyboard::cursorLeft() {
+ if (position>text_string.begin()) {
+ utf8::prior(position, text_string.begin());
+ return true;
+ }
+ return false;
+}
+bool ReadKeyboard::cursorRight() {
+ if (position<text_string.end()) {
+ utf8::next(position, text_string.end());
+ return true;
+ }
+ return false;
+}
+
+bool ReadKeyboard::emulateBackspace() {
+ if (position>text_string.begin()) {
+ utf8::prior(position, text_string.begin());
+ ReadKeyboard::removeChar();
+ return true;
+ }
+ return false;
+}
+
bool ReadKeyboard::ReadKey(SDL_Keycode keyPressed) {
if (keyPressed == SDLK_DELETE) {
if ((text_string.length()>0)&& (position<text_string.end())) {
@@ -79,12 +103,7 @@ bool ReadKeyboard::ReadKey(SDL_Keycode keyPressed) {
return true;
}
if (keyPressed == SDLK_BACKSPACE) {
- if (position>text_string.begin()) {
- utf8::prior(position, text_string.begin());
- ReadKeyboard::removeChar();
- return true;
- }
- return false;
+ return emulateBackspace();
}
if (keyPressed == SDLK_HOME) {
position = text_string.begin();
@@ -94,12 +113,12 @@ bool ReadKeyboard::ReadKey(SDL_Keycode keyPressed) {
position=text_string.end();
return true;
}
- if ((keyPressed == SDLK_LEFT) && (position>text_string.begin())) {
- utf8::prior(position, text_string.begin());
+ if (keyPressed == SDLK_LEFT) {
+ cursorLeft();
return true;
}
- if ((keyPressed == SDLK_RIGHT) && (position<text_string.end())) {
- utf8::next(position, text_string.end());
+ if ((keyPressed == SDLK_RIGHT)) {
+ cursorRight();
return true;
}
return true;
diff --git a/source/code/ReadKeyboard.h b/source/code/ReadKeyboard.h index 9705ea1..db8f508 100644 --- a/source/code/ReadKeyboard.h +++ b/source/code/ReadKeyboard.h
@@ -44,6 +44,9 @@ public:
ReadKeyboard(const char*);
int CharsBeforeCursor(); //Where should the cursor be placed?
void putchar(const std::string& );
+ bool cursorLeft();
+ bool cursorRight();
+ bool emulateBackspace();
bool ReadKey(const SDL_Event&);
bool ReadKey(SDL_Keycode); //true if key accepted
const std::string& GetString(void) const;