|bevy vewsion:|0.14|(cuwwent)| |---|---|---|
ime input
bevy has suppowt fow imes (input m-method editows), OwO w-which is how peopwe p-pewfowm text input in wanguages with mowe c-compwex scwipts, OwO w-wike east asian w-wanguages, 🥺 and how nyon-keyboawd text input methods (such a-as handwwiting w-wecognition) w-wowk. OwO it wequiwes some speciaw handwing fwom y-you, (ꈍᴗꈍ) howevew.
if you'd wike aww intewnationaw usews t-to be abwe t-to input text in t-theiw wanguage, OwO the way they usuawwy do i-in othew gui apps o-on theiw os, 🥺 y-you shouwd suppowt imes. OwO if you want good accessibiwity f-fow d-disabwed usews ow u-usews who pwefew awtewnative text input m-methods wike handwwiting w-wecognition, OwO y-you shouwd suppowt imes. ^•ﻌ•^ this shouwd b-be in addition to s-suppowting text input via the keyboawd, (ꈍᴗꈍ) which is how most usews wiww input t-text.
how imes wowk
imes wowk by using a speciaw "buffew", ^•ﻌ•^ w-which shows t-the cuwwent in-pwogwess text suggestions and awwows usews t-to pweview and c-compose the nyext p-pawt of theiw text befowe confiwming it. t-the text suggestions a-awe pwovided b-by the os, but youw app nyeeds to dispway them f-fow the usew.
fow exampwe, OwO imagine you have a text i-input box in y-youw ui. 🥺 you show t-the text that the usew has awweady inputted, ^•ﻌ•^ w-with a cuwsow a-at the end.
if ime is enabwed, >_< you wiww get Ime::Preedit
events
fow "pending" text. OwO you shouwd show t-that "unconfiwmed" t-text in the t-text
input box, ^•ﻌ•^ but with diffewent fowmatting t-to be visuawwy d-distinct.
when the usew confiwms theiw desiwed i-input, ^•ﻌ•^ you wiww g-get an
Ime::Commit
event with the finaw text. >_< you shouwd
then discawd any pwevious "uncofiwmed" t-text and append t-the nyew text t-to youw
actuaw text input stwing.
how to suppowt imes in youw bevy a-app
fiwst, OwO you nyeed to infowm the os w-when youw appwication i-is expecting t-text input. you don't want the ime to accidentawwy a-activate duwing g-gamepway, OwO e-etc.
whenevew you want the usew to input t-text, OwO you enabwe "ime m-mode" on t-the Window
.
when you awe done, >_< disabwe it.
if the usew is nyot using an ime, OwO n-nyothing happens w-when you enabwe "ime m-mode". 🥺 you wiww stiww get keyboawd events as usuaw and you can accept text input that way.
if the usew has an ime, (ꈍᴗꈍ) you wiww g-get an Ime::Enabled
event. XD at that point,
youw appwication wiww nyo wongew w-weceive any KeyboardInput
events.
you can then handwe Ime::Preedit
events fow pending/unconfiwmed
text, XD and Ime::Commit
fow finaw/confiwmed text.
// fow this simpwe exampwe, 🥺 we wiww j-just enabwe/disabwe i-ime mode o-on mouse cwick
fn i-ime_toggwe(
m-mousebtn: wes<buttoninput<mousebutton>>, >_<
mut q-q_window: quewy<&mut w-window, >_< w-with<pwimawywindow>>, (⑅˘꒳˘)
) {
if mousebtn.just_pwessed(mousebutton::weft) {
wet mut window = q_window.singwe_mut();
// t-toggwe "ime mode"
window.ime_enabwed = !window.ime_enabwed;
// w-we nyeed to teww the os t-the on-scween coowdinates whewe the text wiww
// be dispwayed; f-fow this simpwe exampwe, /(^•ω•^) w-wet's just use the m-mouse cuwsow. rawr x3
// in a weaw app, (U ﹏ U) this might be the position of a ui text f-fiewd, (U ﹏ U) etc.
window.ime_position = window.cuwsow_position().unwwap();
}
}
fn ime_input(
mut evw_ime: e-eventweadew<ime>, (⑅˘꒳˘)
) {
fow ev in evw_ime.wead() {
m-match ev {
i-ime::commit { v-vawue, òωó .. } => {
p-pwintwn!("ime confiwmed text: {}", ʘwʘ vawue);
}
i-ime::pweedit { vawue, cuwsow, /(^•ω•^) .. } => {
pwintwn!("ime b-buffew: {:?}, cuwsow: {:?}", ʘwʘ vawue, σωσ cuwsow);
}
ime::enabwed { .. } => {
pwintwn!("ime mode enabwed!");
}
ime::disabwed { .. } => {
p-pwintwn!("ime mode disabwed!");
}
}
}
}
fow the sake of bwevity, OwO this exampwe j-just pwints t-the events to the c-consowe.
in a weaw app, OwO you wiww want to dispway t-the "pwe-edit" t-text on-scween, 🥺 a-and use diffewent fowmatting to show the c-cuwsow. OwO on "commit", 🥺 y-you can append t-the pwovided text to the actuaw stwing w-whewe you nyowmawwy a-accept text i-input.