| 28 | } |
| 29 | |
| 30 | size_t UpdateText(String& text, size_t cursorPos, const TextInputMode mode) |
| 31 | { |
| 32 | const String chars = SIV3D_ENGINE(TextInput)->getChars(); |
| 33 | |
| 34 | if (not chars) |
| 35 | { |
| 36 | return cursorPos; |
| 37 | } |
| 38 | |
| 39 | const bool allowEnter = !!(mode & TextInputMode::AllowEnter); |
| 40 | const bool allowTab = !!(mode & TextInputMode::AllowTab); |
| 41 | const bool allowBackSpace = !!(mode & TextInputMode::AllowBackSpace); |
| 42 | const bool allowDelete = !!(mode & TextInputMode::AllowDelete); |
| 43 | |
| 44 | if (text.size() < cursorPos) |
| 45 | { |
| 46 | cursorPos = text.size(); |
| 47 | } |
| 48 | |
| 49 | for (auto const ch : chars) |
| 50 | { |
| 51 | if (ch == U'\r') |
| 52 | { |
| 53 | if (allowEnter) |
| 54 | { |
| 55 | text.insert(text.begin() + cursorPos, U'\n'); |
| 56 | ++cursorPos; |
| 57 | } |
| 58 | } |
| 59 | else if (ch == U'\b') |
| 60 | { |
| 61 | if (allowBackSpace && 0 < cursorPos) |
| 62 | { |
| 63 | text.erase(text.begin() + cursorPos - 1); |
| 64 | --cursorPos; |
| 65 | } |
| 66 | } |
| 67 | else if (ch == U'\t') |
| 68 | { |
| 69 | if (allowTab) |
| 70 | { |
| 71 | text.insert(text.begin() + cursorPos, U'\t'); |
| 72 | ++cursorPos; |
| 73 | } |
| 74 | } |
| 75 | else if (ch == char32(0x7F)) |
| 76 | { |
| 77 | if (allowDelete && cursorPos < text.size()) |
| 78 | { |
| 79 | text.erase(text.begin() + cursorPos); |
| 80 | } |
| 81 | } |
| 82 | else if (not IsControl(ch)) |
| 83 | { |
| 84 | text.insert(text.begin() + cursorPos, ch); |
| 85 | ++cursorPos; |
| 86 | } |
| 87 | } |