diff --git a/examples/keyboard/english-us.json b/examples/keyboard/english-us.json index 3786499..eb5f450 100644 --- a/examples/keyboard/english-us.json +++ b/examples/keyboard/english-us.json @@ -47,7 +47,7 @@ "l": [216, 44, 20, 20], ";": [238, 44, 20, 20], "'": [260, 44, 20, 20], - "Enter": [282, 44, 38, 20, {"action": "newline"}], + "Enter": [282, 44, 38, 20, {"action": "enter"}], "Shift": [0, 66, 49, 20, {"mode": "shift", "key": "Y"}], "z": [51, 66, 20, 20], "x": [73, 66, 20, 20], @@ -113,7 +113,7 @@ "L": [216, 44, 20, 20], ":": [238, 44, 20, 20], "\"": [260, 44, 20, 20], - "Enter": [282, 44, 38, 20, {"action": "newline"}], + "Enter": [282, 44, 38, 20, {"action": "enter"}], "Shift": [0, 66, 49, 20, {"value": "", "active": true, "key": "Y"}], "Z": [51, 66, 20, 20], "X": [73, 66, 20, 20], @@ -178,7 +178,7 @@ "L": [216, 44, 20, 20], ";": [238, 44, 20, 20], "'": [260, 44, 20, 20], - "Enter": [282, 44, 38, 20, {"action": "newline"}], + "Enter": [282, 44, 38, 20, {"action": "enter"}], "Shift": [0, 66, 49, 20, {"mode": "shift", "key": "Y"}], "Z": [51, 66, 20, 20], "X": [73, 66, 20, 20], diff --git a/include/keyboard.hpp b/include/keyboard.hpp index ee92a79..4aaeff3 100644 --- a/include/keyboard.hpp +++ b/include/keyboard.hpp @@ -34,6 +34,13 @@ #include class UCKeyboard { +public: + enum class Status { + Active, + DoneEnter, + DoneExit + }; + private: struct Key { enum class Property : uint8_t { Invalid, Action, Mode, Value }; @@ -63,7 +70,7 @@ private: std::string CurrentString = ""; int Cursor = 0; uint MaxSize = 0; - bool IsDone = false; + Status CurrentStatus = Status::Active; uint8_t GetCharSize(void) const; uint8_t GetPrevCharSize(void) const; @@ -109,9 +116,9 @@ public: std::string String(void) const { return CurrentString; }; /** - * @brief Gets if the user is done inputting. + * @brief Gets the status of the keyboard, use to check if the user has finished. */ - bool Done(void) const { return IsDone; }; + Status GetStatus(void) const { return CurrentStatus; }; /** * @brief Gets a string from user input. diff --git a/source/font.cpp b/source/font.cpp index fd6183c..f9f45c1 100644 --- a/source/font.cpp +++ b/source/font.cpp @@ -209,6 +209,7 @@ ITCM_CODE void Font::print(std::u16string_view text, int x, int y, Alignment ali y += tileHeight; } + SCALE_3DS(x); x += ((sprite ? sprite->width() : 256) - (calcWidth(text) * scaleX)) / 2; break; } diff --git a/source/gui.cpp b/source/gui.cpp index dcde84c..e7c18a4 100644 --- a/source/gui.cpp +++ b/source/gui.cpp @@ -113,11 +113,7 @@ bool Gui::reinit(const std::vector &FontPaths) { } void Gui::DrawStringCentered(int x, int y, float size, u8 color, const std::string &Text, int maxWidth, int maxHeight, Font *fnt, int flags) { -#ifdef UNIVCORE_3DS_SIZE Gui::DrawString(x, y, size, color, Text, maxWidth, maxHeight, fnt, flags | C2D_AlignCenter); -#else - Gui::DrawString(x, y, size, color, Text, maxWidth, maxHeight, fnt, flags | C2D_AlignCenter); -#endif } void Gui::DrawString(int x, int y, float size, u8 color, const std::string &Text, int maxWidth, int maxHeight, Font *fnt, int flags) { diff --git a/source/keyboard.cpp b/source/keyboard.cpp index ed0559e..0e06c59 100644 --- a/source/keyboard.cpp +++ b/source/keyboard.cpp @@ -130,7 +130,7 @@ uint8_t UCKeyboard::GetCharSize(void) const { Str++; } while ((*Str & 0xC0) == 0x80); - return Str - this->CurrentString.c_str(); + return Str - (this->CurrentString.c_str() + Cursor); } uint8_t UCKeyboard::GetPrevCharSize(void) const { @@ -139,7 +139,7 @@ uint8_t UCKeyboard::GetPrevCharSize(void) const { Str--; } while ((*Str & 0xC0) == 0x80); - return this->CurrentString.c_str() - Str; + return this->CurrentString.c_str() + Cursor - Str; } void UCKeyboard::Draw(uint32_t Held, uint32_t Repeat, touchPosition T) const { @@ -147,18 +147,24 @@ void UCKeyboard::Draw(uint32_t Held, uint32_t Repeat, touchPosition T) const { /* A sub menu or so? */ if (!this->Loaded) { - Gui::Draw_Rect(48, 0, 320, 20, this->BarColor); - Gui::Draw_Rect(48, 20, 320, 1, this->OutlineColor); + Gui::Draw_Rect(40, 0, 320, 25, this->BarColor); + Gui::Draw_Rect(40, 25, 320, 1, this->OutlineColor); Gui::DrawStringCentered(24, 2, 1.0f, this->TextColor, "Invalid keyboard layout", 310); } else { - Gui::Draw_Rect(0, 0, 320, 240, this->BgColor); - Gui::DrawStringCentered(24, 2, 1.0f, this->TextColor, this->CurrentString, 310); + Gui::Draw_Rect(0, 0, 280, 25, this->BarColor); + Gui::Draw_Rect(0, 25, 280, 1, this->OutlineColor); + Gui::Draw_Rect(0, 26, 320, 240 - 26, this->BgColor); + Gui::DrawStringCentered(0, 2, 1.0f, this->TextColor, this->CurrentString, 310); if (this->Kbd.contains(this->CurrentMode.back())) { for (const auto &Key : this->Kbd.at(this->CurrentMode.back()).Keys) { Gui::Draw_Rect(Key.Pos.x, Key.Pos.y, Key.Pos.w, Key.Pos.h, (Key.Active || Key.Pos.Touched(T) || Held & Key.Button) ? this->KeyColorPressed : this->KeyColor); +#ifdef UNIVCORE_3DS_SIZE Gui::DrawStringCentered(Key.Pos.x + (Key.Pos.w / 2) - 160, Key.Pos.y + (Key.Pos.h / 10), 1.0f, this->TextColor, Key.Label); +#else + Gui::DrawStringCentered(Key.Pos.x + (Key.Pos.w / 2) - 128, Key.Pos.y + (Key.Pos.h / 10), 1.0f, this->TextColor, Key.Label); +#endif }; } else { @@ -214,49 +220,47 @@ void UCKeyboard::HandleKeyPress(const Key &Key) { if (Value == "backspace") { if (this->Cursor > 0) { this->Cursor -= GetPrevCharSize(); - this->CurrentString = this->CurrentString.substr(0, this->Cursor) + this->CurrentString.substr(this->Cursor + GetCharSize(), this->CurrentString.size()); + this->CurrentString = this->CurrentString.substr(0, this->Cursor) + this->CurrentString.substr(this->Cursor + GetCharSize()); }; + } else if (Value == "delete") { - // TextEditor::Remove(); - + if (this->Cursor + GetCharSize() < (int)this->CurrentString.size()) + this->CurrentString = this->CurrentString.substr(0, this->Cursor) + this->CurrentString.substr(this->Cursor + GetCharSize()); + } else if (Value == "up") { - // TextEditor::CursorUp(); + this->Cursor = 0; } else if (Value == "down") { - // TextEditor::CursorDown(); + this->Cursor = this->CurrentString.size(); + this->Cursor -= GetPrevCharSize(); } else if (Value == "left") { - // TextEditor::CursorLeft(); + this->Cursor -= GetPrevCharSize(); } else if (Value == "right") { - // TextEditor::CursorRight(); + this->Cursor += GetCharSize(); } else if (Value == "dakuten" || Value == "handakuten") { - // bool Handakuten = Value == "handakuten"; + bool Handakuten = Value == "handakuten"; - // if (TextEditor::CursorPos > 0) { - // TextEditor::CursorLeft(); - // const std::string Char = UniversalEdit::UE->CurrentFile->GetCharacter(TextEditor::CurrentLine, TextEditor::CursorPos); - // const std::string Out = TextUtils::Dakutenify(Char, Handakuten); + if (this->Cursor > 0) { + this->Cursor -= GetPrevCharSize(); + const std::string Char = this->CurrentString.substr(this->Cursor, GetCharSize()); + const std::string Out = TextUtils::Dakutenify(Char, Handakuten); - // UniversalEdit::UE->CurrentFile->EraseContent(TextEditor::CurrentLine, TextEditor::CursorPos, Char.size()); - // if (UniversalEdit::UE->CurrentFile->InsertContent(TextEditor::CurrentLine, TextEditor::CursorPos, Out)) { - // TextEditor::CursorRight(); + this->CurrentString = this->CurrentString.substr(0, this->Cursor) + Out + this->CurrentString.substr(this->Cursor + GetCharSize()); + this->Cursor += Out.size(); - // if (Out.size() > Char.size()) TextEditor::CursorRight(); - // }; + } else { + this->CurrentString.insert(this->Cursor, Handakuten ? "゜" : "゛"); + this->Cursor += 3; + }; - // } else { - // if (UniversalEdit::UE->CurrentFile->InsertContent(TextEditor::CurrentLine, TextEditor::CursorPos, Handakuten ? "゜" : "゛")) { - // TextEditor::CursorPos += 3; - // }; - // }; - - } else if (Value == "newline") { - // TextEditor::InsertLine(); + } else if (Value == "enter") { + this->CurrentStatus = Status::DoneEnter; } else if (Value == "exit") { - IsDone = true; + this->CurrentStatus = Status::DoneExit; } else if (Value == "layout") { this->SwitchLayout(); @@ -277,7 +281,7 @@ void UCKeyboard::HandleKeyPress(const Key &Key) { /* Output a value that's not the label. */ case Key::Property::Value: if(this->CurrentString.size() + Value.size() < this->MaxSize) { - this->CurrentString += Value; + this->CurrentString.insert(this->Cursor, Value); this->Cursor += Value.size(); }; break; @@ -291,8 +295,7 @@ void UCKeyboard::HandleKeyPress(const Key &Key) { nocashMessage(Key.Label.c_str()); /* Otherwise, just output the label */ if(this->CurrentString.size() + Key.Label.size() < this->MaxSize) { - nocashMessage("adding"); - this->CurrentString += Key.Label; + this->CurrentString.insert(this->Cursor, Key.Label); this->Cursor += Key.Label.size(); }; }; @@ -303,9 +306,9 @@ std::string UCKeyboard::GetString(uint MaxSize, const std::string &Hint) { u32 Held = 0, Repeat = 0; touchPosition T; - while(1) { - this->Draw(Held, Repeat, T); + while(this->CurrentStatus == Status::Active) { this->Handler(Held, Repeat, T); + this->Draw(Held, Repeat, T); do { swiWaitForVBlank(); @@ -313,8 +316,10 @@ std::string UCKeyboard::GetString(uint MaxSize, const std::string &Hint) { Held = keysHeld(); Repeat = keysDownRepeat(); touchRead(&T); - SCALE_3DS(T.px); - SCALE_3DS(T.py); +#ifdef UNIVCORE_3DS_SIZE + T.px = T.px * 5 / 4; + T.py = T.py * 5 / 4; +#endif } while (!Held); if (Held & KEY_START) { @@ -322,5 +327,5 @@ std::string UCKeyboard::GetString(uint MaxSize, const std::string &Hint) { }; }; - return this->CurrentString; + return this->CurrentStatus == Status::DoneExit ? "" : this->CurrentString; };