@@ -2,108 +2,59 @@ use bevy::{asset::AsAssetId, input::keyboard::Key};
22
33use crate :: prelude:: * ;
44
5- pub fn delete_char (
6- input : In < ConsoleActionSystemInput > ,
7- mut console_q : Query < ( & mut ConsoleInputText , & mut ConsoleBuffer ) > ,
8- ) {
9- if let Ok ( ( mut input, mut buffer) ) = console_q. get_mut ( input. console_id ) {
10- let len = buffer. pop_front ( ) . map ( |c| c. len_utf8 ( ) ) . unwrap_or_default ( ) ;
11- input. move_cursor ( -( len as isize ) ) ;
12- } else {
13- error ! (
14- "Could not delete char from console with id {}" ,
15- input. console_id
16- ) ;
17- }
5+ pub fn delete_char ( input : In < ConsoleActionSystemInput > , mut commands : Commands ) {
6+ commands. write_message ( ConsoleBufferMsg :: new (
7+ input. console_id ,
8+ ConsoleBufferMsgKind :: RemoveCharacter ,
9+ ) ) ;
1810}
19- pub fn delete_word (
20- input : In < ConsoleActionSystemInput > ,
21- mut console_q : Query < & mut ConsoleInputText > ,
22- ) {
23- if let Ok ( mut input) = console_q. get_mut ( input. console_id ) {
24- // TODO
25- // let last_ws = input.text.rfind(char::is_whitespace).unwrap_or_default();
26- // input.text.truncate(last_ws);
27- // input.set_cursor(last_ws)
28- } else {
29- error ! (
30- "Could not delete word from console with id {}" ,
31- input. console_id
32- ) ;
33- }
11+ pub fn delete_word ( input : In < ConsoleActionSystemInput > , mut commands : Commands ) {
12+ commands. write_message ( ConsoleBufferMsg :: new (
13+ input. console_id ,
14+ ConsoleBufferMsgKind :: RemoveWord ,
15+ ) ) ;
3416}
35- pub fn write_char (
36- input : In < ConsoleActionSystemInput > ,
37- mut console_q : Query < & mut ConsoleInputText > ,
38- mut commands : Commands ,
39- ) {
40- if let Ok ( mut input_text) = console_q. get_mut ( input. console_id ) {
41- for key in input. matched_logical_keys ( ) {
42- match key {
43- Key :: Character ( c) => {
44- commands. write_message ( ConsoleWriteMsg {
45- message : c. to_string ( ) ,
46- console_id : input. console_id ,
47- pos : input_text. anchor + input_text. cursor ( ) ,
48- } ) ;
49- input_text. move_cursor ( c. len ( ) as isize ) ;
50- }
51- Key :: Space => {
52- commands. write_message ( ConsoleWriteMsg {
53- message : " " . to_string ( ) ,
54- console_id : input. console_id ,
55- pos : input_text. anchor + input_text. cursor ( ) ,
56- } ) ;
57- input_text. move_cursor ( 1 ) ;
58- }
59- Key :: Enter => {
60- commands. write_message ( ConsoleWriteMsg {
61- message : "\n " . to_string ( ) ,
62- console_id : input. console_id ,
63- pos : input_text. anchor + input_text. cursor ( ) ,
64- } ) ;
65- input_text. move_cursor ( 1 ) ;
66- }
67- _ => { }
17+ pub fn write_char ( input : In < ConsoleActionSystemInput > , mut commands : Commands ) {
18+ for key in input. matched_logical_keys ( ) {
19+ match key {
20+ Key :: Character ( c) => {
21+ commands. write_message ( ConsoleBufferMsg :: write ( input. console_id , c. to_string ( ) ) ) ;
22+ }
23+ Key :: Space => {
24+ commands. write_message ( ConsoleBufferMsg :: write ( input. console_id , " " . to_string ( ) ) ) ;
25+ }
26+ Key :: Enter => {
27+ commands. write_message ( ConsoleBufferMsg :: write ( input. console_id , "\n " . to_string ( ) ) ) ;
6828 }
29+ _ => { }
6930 }
70- commands. write_message ( ConsoleViewMsg :: jump_to_bottom ( input. console_id ) ) ;
71- } else {
72- error ! (
73- "Could not write char to console with id {}" ,
74- input. console_id
75- ) ;
7631 }
32+ commands. write_message ( ConsoleViewMsg :: jump_to_bottom ( input. console_id ) ) ;
7733}
7834
7935pub fn submit (
8036 input : In < ConsoleActionSystemInput > ,
81- mut query : Query < (
82- & mut ConsoleInputText ,
83- & ConsoleAssetHandle < ConsoleHistory > ,
84- & ConsoleBuffer ,
85- ) > ,
37+ mut query : Query < ( & ConsoleBuffer , & ConsoleAssetHandle < ConsoleHistory > ) > ,
8638 mut assets : ResMut < Assets < ConsoleHistory > > ,
8739 mut commands : Commands ,
8840) {
89- if let Ok ( ( mut input_text , history_handle, buffer ) ) = query. get_mut ( input. console_id ) {
41+ if let Ok ( ( buffer , history_handle) ) = query. get_mut ( input. console_id ) {
9042 let history = assets. get_mut ( history_handle. as_asset_id ( ) ) ;
9143 if history. is_none ( ) {
9244 error ! ( "Failed to get console history!" ) ;
9345 return ;
9446 }
9547 let history = history. unwrap ( ) ;
96- let command = buffer. range ( input_text. anchor , buffer. len ( ) - input_text. anchor ) ;
97- if let Some ( event) = SubmitEvent :: new ( input. console_id , command. clone ( ) ) {
48+ let command = buffer. user_input ( ) ;
49+ history. push ( command. clone ( ) ) ;
50+ if let Some ( event) = SubmitEvent :: new ( input. console_id , command) {
9851 commands. trigger ( event) ;
9952 } else {
100- commands. trigger ( ConsolePrintln :: new (
53+ commands. write_message ( ConsoleBufferMsg :: println (
10154 input. console_id ,
10255 "Invalid shell expression" . to_string ( ) ,
10356 ) ) ;
10457 }
105- input_text. set_cursor ( 0 ) ;
106- history. push ( command) ;
10758 } else {
10859 error ! ( "Could not submit from console with id {}" , input. console_id) ;
10960 }
@@ -115,19 +66,11 @@ fn on_scroll(input: In<ConsoleActionSystemInput>, mut commands: Commands) {
11566}
11667
11768fn clear ( input : In < ConsoleActionSystemInput > , mut commands : Commands ) {
118- commands. run_system_cached_with ( clear_buffer, input. console_id ) ;
69+ commands. write_message ( ConsoleBufferMsg :: clear_buffer ( input. console_id ) ) ;
11970}
12071
121- fn clear_input (
122- input : In < ConsoleActionSystemInput > ,
123- mut query : Query < ( & mut ConsoleInputText , & mut ConsoleBuffer ) > ,
124- ) {
125- // TODO: Don't edit buffer directly! Use a message
126- let ( mut input, mut buffer) = query. get_mut ( input. console_id ) . unwrap ( ) ;
127- for _ in 0 ..( buffer. len ( ) - input. anchor ) {
128- buffer. pop_front ( ) ;
129- }
130- input. set_cursor ( 0 ) ;
72+ fn clear_input ( input : In < ConsoleActionSystemInput > , mut commands : Commands ) {
73+ commands. write_message ( ConsoleBufferMsg :: clear_input ( input. console_id ) ) ;
13174}
13275
13376pub fn scroll_line ( input : In < ConsoleActionSystemInput > , mut commands : Commands ) {
0 commit comments