mirror of
https://github.com/J-D-K/JKSV.git
synced 2026-09-08 02:25:17 -05:00
Add Dictionary back to keyboard.
This commit is contained in:
62
source/keyboard/keyboard.cpp
Normal file
62
source/keyboard/keyboard.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include "keyboard/keyboard.hpp"
|
||||
|
||||
#include "error.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
bool keyboard::get_input(SwkbdType keyboardType,
|
||||
std::string_view defaultText,
|
||||
std::string_view header,
|
||||
char *stringOut,
|
||||
size_t stringLength,
|
||||
std::optional<std::reference_wrapper<keyboard::Dictionary>> dictionary)
|
||||
{
|
||||
// Swkbd config.
|
||||
SwkbdConfig keyboard{};
|
||||
|
||||
// Cache instead of repeated calls.
|
||||
const bool hasDictionary = dictionary.has_value();
|
||||
|
||||
// This changes whether or not we're passed a dictionary here.
|
||||
bool createError{};
|
||||
if (hasDictionary)
|
||||
{
|
||||
// Getting this out of the optional looks confusing...
|
||||
const size_t wordCount = dictionary->get().get_count();
|
||||
createError = error::libnx(swkbdCreate(&keyboard, wordCount));
|
||||
}
|
||||
else { createError = error::libnx(swkbdCreate(&keyboard, 0)); }
|
||||
|
||||
// Just return false if we can't init the keyboard.
|
||||
if (createError) { return false; }
|
||||
|
||||
// Standard swkbd init.
|
||||
swkbdConfigSetBlurBackground(&keyboard, true);
|
||||
swkbdConfigSetInitialText(&keyboard, defaultText.data());
|
||||
swkbdConfigSetHeaderText(&keyboard, header.data());
|
||||
swkbdConfigSetGuideText(&keyboard, header.data());
|
||||
swkbdConfigSetType(&keyboard, keyboardType);
|
||||
swkbdConfigSetStringLenMax(&keyboard, stringLength);
|
||||
swkbdConfigSetKeySetDisableBitmask(&keyboard, SwkbdKeyDisableBitmask_Backslash);
|
||||
|
||||
// Add the dictionary if one was passed.
|
||||
if (hasDictionary)
|
||||
{
|
||||
// These are easier to read and work with.
|
||||
const SwkbdDictWord *words = reinterpret_cast<const SwkbdDictWord *>(dictionary->get().get_words());
|
||||
const size_t wordCount = dictionary->get().get_count();
|
||||
|
||||
swkbdConfigSetDictionary(&keyboard, words, wordCount);
|
||||
}
|
||||
|
||||
const bool swkbdError = error::libnx(swkbdShow(&keyboard, stringOut, stringLength));
|
||||
const bool empty = std::char_traits<char>::length(stringOut) == 0;
|
||||
if (swkbdError || empty)
|
||||
{
|
||||
swkbdClose(&keyboard);
|
||||
return false;
|
||||
}
|
||||
|
||||
swkbdClose(&keyboard);
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user