git repos / blockattack-game

commit eb4cc88e

Poul Sander · 2020-05-30 20:42
eb4cc88e9b252bb4768539c52e0f639efd52104e patch · browse files
parent c634a90125054c90246eeb620fb70d24b2a3cf49

It is now possible to enter name with a gamepad if it has enogh buttons (need Back and Start).

Changed files

M source/code/DialogBox.cpp before
M source/code/DialogBox.hpp before
diff --git a/source/code/DialogBox.cpp b/source/code/DialogBox.cpp index b84974f..e0039b6 100644 --- a/source/code/DialogBox.cpp +++ b/source/code/DialogBox.cpp
@@ -26,6 +26,7 @@ http://www.blockattack.net
#include "common.h"
#include "ReadKeyboard.h"
#include "utf8.h"
+#include "MenuSystem.h"
static void setButtonFont(const sago::SagoDataHolder* holder, sago::SagoTextField& field, const char* text) {
field.SetHolder(holder);
@@ -166,6 +167,62 @@ void DialogBox::Draw(SDL_Renderer* target) {
}
}
+
+static bool isGamePadStartEvent(const SDL_Event& event) {
+ if (event.type == SDL_CONTROLLERBUTTONDOWN) {
+ if (event.cbutton.button == SDL_CONTROLLER_BUTTON_START ) {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+static bool isGamePadBackEvent(const SDL_Event& event) {
+ if (event.type == SDL_CONTROLLERBUTTONDOWN) {
+ if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK ) {
+ return true;
+ }
+ }
+ return false;
+}
+
+static bool isGamePadLEvent(const SDL_Event& event) {
+ if (event.type == SDL_CONTROLLERBUTTONDOWN) {
+ if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER ) {
+ return true;
+ }
+ }
+ return false;
+}
+
+static bool isGamePadREvent(const SDL_Event& event) {
+ if (event.type == SDL_CONTROLLERBUTTONDOWN) {
+ if (event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ) {
+ return true;
+ }
+ }
+ return false;
+}
+
+
+void DialogBox::virtualKeyboardWriteSelectedChar(ReadKeyboard *rk, const std::string& insertChar) const {
+ 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(insertChar);
+ }
+}
+
+
void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
if (event.type == SDL_TEXTINPUT) {
if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
@@ -188,6 +245,48 @@ void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
}
}
}
+ else {
+ if (isGamePadStartEvent(event)) {
+ name = rk->GetString();
+ updated = true;
+ isActive = false;
+ }
+ if (isGamePadBackEvent(event)) {
+ isActive = false;
+ }
+ if (isConfirmEvent(event)) {
+ const sago::SagoTextField& f = gamePadCharFields.at(selectedChar);
+ const std::string& insertChar = f.GetText();
+ virtualKeyboardWriteSelectedChar(rk.get(), insertChar);
+ }
+ if (isEscapeEvent(event)) {
+ rk->emulateBackspace();
+ }
+ if (isGamePadLEvent(event)) {
+ rk->cursorLeft();
+ }
+ if (isGamePadREvent(event)) {
+ rk->cursorRight();
+ }
+ if (isRightEvent(event)) {
+ ++selectedChar;
+ }
+ if (isLeftEvent(event)) {
+ --selectedChar;
+ }
+ if (isDownEvent(event)) {
+ selectedChar+= keyboardRowLimit;
+ }
+ if (isUpEvent(event)) {
+ selectedChar -= keyboardRowLimit;
+ }
+ if (selectedChar < 0) {
+ selectedChar = 0;
+ }
+ if (selectedChar >= static_cast<int>(gamePadCharFields.size())) {
+ selectedChar = gamePadCharFields.size()-1;
+ }
+ }
processed = true;
if ( !(SDL_GetMouseState(nullptr, nullptr)&SDL_BUTTON(1)) ) {
bMouseUp=true;
@@ -210,19 +309,7 @@ void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
auto topy = globalData.ysize/2+150+(i/keyboardRowLimit)*40-5;
if (insideRect(topx, topy, 30, 30)) {
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());
- }
+ virtualKeyboardWriteSelectedChar(rk.get(), insertChar);
}
}
}
diff --git a/source/code/DialogBox.hpp b/source/code/DialogBox.hpp index 7fcff21..c88e192 100644 --- a/source/code/DialogBox.hpp +++ b/source/code/DialogBox.hpp
@@ -45,6 +45,7 @@ public:
std::string GetName() const;
bool IsUpdated() const;
private:
+ void virtualKeyboardWriteSelectedChar(ReadKeyboard *rk, const std::string& insertChar) const;
bool isActive = true;
bool bMouseUp = false;
std::shared_ptr<ReadKeyboard> rk;