Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic IME mapping #204

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use bevy::{
},
prelude::{Entity, EventReader, Query, Resource, Time},
window::{
CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, RequestRedraw, WindowCreated,
WindowFocused,
CursorEntered, CursorLeft, CursorMoved, Ime, ReceivedCharacter, RequestRedraw,
WindowCreated, WindowFocused,
},
};
use std::marker::PhantomData;
Expand All @@ -35,6 +35,7 @@ pub struct InputEvents<'w, 's> {
pub ev_window_focused: EventReader<'w, 's, WindowFocused>,
pub ev_window_created: EventReader<'w, 's, WindowCreated>,
pub ev_touch: EventReader<'w, 's, TouchInput>,
pub ev_ime_input: EventReader<'w, 's, Ime>,
}

impl<'w, 's> InputEvents<'w, 's> {
Expand All @@ -50,6 +51,7 @@ impl<'w, 's> InputEvents<'w, 's> {
self.ev_keyboard_input.iter().last();
self.ev_window_focused.iter().last();
self.ev_window_created.iter().last();
self.ev_ime_input.iter().last();
}
}

Expand Down Expand Up @@ -231,6 +233,42 @@ pub fn process_input_system(
}
}

fn push_ime_event(params: &mut ContextSystemParams, window: &Entity, event: egui::Event) {
params
.contexts
.get_mut(*window)
.unwrap()
.egui_input
.events
.push(event);
}

for ev in input_events.ev_ime_input.iter() {
match ev {
Ime::Preedit {
window,
value,
cursor: _,
} => {
push_ime_event(&mut context_params, window, egui::Event::CompositionStart);
push_ime_event(&mut context_params, window, egui::Event::CompositionUpdate(value.clone()));
},
Ime::Commit { window, value } => push_ime_event(
&mut context_params,
window,
egui::Event::CompositionEnd(value.clone()),
),
Ime::Enabled { window } => {
push_ime_event(&mut context_params, window, egui::Event::CompositionStart)
}
Ime::Disabled { window } => push_ime_event(
&mut context_params,
window,
egui::Event::CompositionEnd("".to_string()),
),
}
}

if let Some(mut focused_input) = context_params
.focused_window
.as_ref()
Expand Down