Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/legion_v2' into atelier-prefab
Browse files Browse the repository at this point in the history
  • Loading branch information
ezpuzz committed Dec 24, 2020
2 parents c1c06a8 + 7c9dc38 commit 104fdca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
1 change: 1 addition & 0 deletions amethyst_ui/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ where
builder: &mut DispatcherBuilder,
) -> Result<(), Error> {
resources.insert(EventChannel::<UiButtonAction>::new());
resources.insert(EventChannel::<UiEvent>::new());
resources.insert(Widgets::<UiLabel, W>::new());
resources.insert(CachedSelectionOrderResource::default());

Expand Down
39 changes: 18 additions & 21 deletions amethyst_ui/src/text_editing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ use amethyst_core::{
ecs::*,
shrev::{EventChannel, ReaderId},
};
use amethyst_input::{InputHandler, KeyboardModifiersState};
use copypasta::{ClipboardContext, ClipboardProvider};
use log::error;
use unicode_normalization::{char::is_combining_mark, UnicodeNormalization};
use unicode_segmentation::UnicodeSegmentation;
use winit::event::{
ElementState, Event, KeyboardInput, ModifiersState, VirtualKeyCode, WindowEvent,
};
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};

use crate::{LineMode, Selected, TextEditing, UiEvent, UiEventType, UiText};

/// System managing the keyboard inputs for the editable text fields.
/// ## Features
/// * Adds and removes text.
Expand All @@ -38,9 +36,10 @@ impl System<'static> for TextEditingInputSystem {
SystemBuilder::new("TextEditingInputSystem")
.read_resource::<EventChannel<Event<'static, ()>>>()
.write_resource::<EventChannel<UiEvent>>()
.read_resource::<InputHandler>()
.with_query(<&mut UiText>::query())
.with_query(<(Entity, &mut UiText, &mut TextEditing, &Selected)>::query())
.build(move |_commands, world, (events, ui_events),
.build(move |_commands, world, (events, ui_events, inputs),
(ui_texts_query, selected_ui_texts_query)| {

ui_texts_query.for_each_mut(world, |mut text| {
Expand Down Expand Up @@ -88,16 +87,14 @@ impl System<'static> for TextEditingInputSystem {
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(v_keycode),

modifiers,
..
},
..
},
..
} => match v_keycode {
VirtualKeyCode::Home | VirtualKeyCode::Up => {
focused_edit.highlight_vector = if modifiers.shift() {
focused_edit.highlight_vector = if inputs.modifiers.shift() {
focused_edit.cursor_position
} else {
0
Expand All @@ -107,7 +104,7 @@ impl System<'static> for TextEditingInputSystem {
}
VirtualKeyCode::End | VirtualKeyCode::Down => {
let glyph_len = focused_text.text.graphemes(true).count() as isize;
focused_edit.highlight_vector = if modifiers.shift() {
focused_edit.highlight_vector = if inputs.modifiers.shift() {
focused_edit.cursor_position - glyph_len
} else {
0
Expand Down Expand Up @@ -146,9 +143,9 @@ impl System<'static> for TextEditingInputSystem {
}
}
VirtualKeyCode::Left => {
if focused_edit.highlight_vector == 0 || modifiers.shift() {
if focused_edit.highlight_vector == 0 || inputs.modifiers.shift() {
if focused_edit.cursor_position > 0 {
let delta = if ctrl_or_cmd(modifiers) {
let delta = if ctrl_or_cmd(&inputs.modifiers) {
let mut graphemes = 0;
for word in focused_text.text.split_word_bounds() {
let word_graphemes =
Expand All @@ -165,7 +162,7 @@ impl System<'static> for TextEditingInputSystem {
1
};
focused_edit.cursor_position -= delta;
if modifiers.shift() {
if inputs.modifiers.shift() {
focused_edit.highlight_vector += delta;
}
focused_edit.cursor_blink_timer = 0.0;
Expand All @@ -178,10 +175,10 @@ impl System<'static> for TextEditingInputSystem {
}
}
VirtualKeyCode::Right => {
if focused_edit.highlight_vector == 0 || modifiers.shift() {
if focused_edit.highlight_vector == 0 || inputs.modifiers.shift() {
let glyph_len = focused_text.text.graphemes(true).count();
if (focused_edit.cursor_position as usize) < glyph_len {
let delta = if ctrl_or_cmd(modifiers) {
let delta = if ctrl_or_cmd(&inputs.modifiers) {
let mut graphemes = 0;
for word in focused_text.text.split_word_bounds() {
graphemes += word.graphemes(true).count() as isize;
Expand All @@ -194,7 +191,7 @@ impl System<'static> for TextEditingInputSystem {
1
};
focused_edit.cursor_position += delta;
if modifiers.shift() {
if inputs.modifiers.shift() {
focused_edit.highlight_vector -= delta;
}
focused_edit.cursor_blink_timer = 0.0;
Expand All @@ -207,14 +204,14 @@ impl System<'static> for TextEditingInputSystem {
}
}
VirtualKeyCode::A => {
if ctrl_or_cmd(modifiers) {
if ctrl_or_cmd(&inputs.modifiers) {
let glyph_len = focused_text.text.graphemes(true).count() as isize;
focused_edit.cursor_position = glyph_len;
focused_edit.highlight_vector = -glyph_len;
}
}
VirtualKeyCode::X => {
if ctrl_or_cmd(modifiers) {
if ctrl_or_cmd(&inputs.modifiers) {
let new_clip = extract_highlighted(focused_edit, focused_text);
if !new_clip.is_empty() {
match ClipboardContext::new().and_then(
Expand All @@ -233,7 +230,7 @@ impl System<'static> for TextEditingInputSystem {
}
}
VirtualKeyCode::C => {
if ctrl_or_cmd(modifiers) {
if ctrl_or_cmd(&inputs.modifiers) {
let new_clip = read_highlighted(focused_edit, focused_text);
if !new_clip.is_empty() {
if let Err(e) = ClipboardContext::new().and_then(
Expand All @@ -247,7 +244,7 @@ impl System<'static> for TextEditingInputSystem {
}
}
VirtualKeyCode::V => {
if ctrl_or_cmd(modifiers) {
if ctrl_or_cmd(&inputs.modifiers) {
delete_highlighted(focused_edit, focused_text);

match ClipboardContext::new()
Expand Down Expand Up @@ -289,7 +286,7 @@ impl System<'static> for TextEditingInputSystem {
));
}
LineMode::Wrap => {
if modifiers.shift() {
if inputs.modifiers.shift() {
if focused_text.text.graphemes(true).count()
< focused_edit.max_length
{
Expand Down Expand Up @@ -330,7 +327,7 @@ impl System<'static> for TextEditingInputSystem {
}

/// Returns if the command key is down on OSX, and the CTRL key for everything else.
fn ctrl_or_cmd(modifiers: ModifiersState) -> bool {
fn ctrl_or_cmd(modifiers: &KeyboardModifiersState) -> bool {
(cfg!(target_os = "macos") && modifiers.logo())
|| (cfg!(not(target_os = "macos")) && modifiers.ctrl())
}
Expand Down

0 comments on commit 104fdca

Please sign in to comment.