@@ -11,13 +11,10 @@ use std::time::Duration;
11
11
// use std::io::{self, BufRead};
12
12
// use std::path::Path;
13
13
14
- // TODO: add file editing:
15
- // changing individual characters
14
+ // TODO:
16
15
// changing grid size
17
-
18
- // TODO: split stuff into several files
19
-
20
- // TODO: make it so turning around on a @ actually works
16
+ // make pasting with newlines functional
17
+ // split stuff into several files
21
18
22
19
static PRESETS : phf:: Map < & ' static str , & ' static str > = phf_map ! {
23
20
"hello world 1" =>r#"
@@ -190,7 +187,7 @@ struct BefreakState {
190
187
pub struct AppState {
191
188
befreak_state : BefreakState ,
192
189
speed : f32 ,
193
- // cursor_position: (usize, usize),
190
+ cursor_position : ( usize , usize ) ,
194
191
paused : bool ,
195
192
previous_instant : Instant ,
196
193
extra : bool ,
@@ -207,7 +204,7 @@ impl AppState {
207
204
befreak_state : BefreakState :: new_empty ( ) ,
208
205
//state: State::new_load_file("primes1"),
209
206
text_channel : channel ( ) ,
210
- // cursor_position: (0, 0), //TODO: use this for editing
207
+ cursor_position : ( 0 , 0 ) ,
211
208
previous_instant : Instant :: now ( ) ,
212
209
paused : true ,
213
210
extra : false ,
@@ -316,6 +313,59 @@ impl eframe::App for AppState {
316
313
} ) ;
317
314
318
315
egui:: CentralPanel :: default ( ) . show ( ctx, |ui| {
316
+ let mut direction = None ;
317
+ if ui. input ( |e| e. key_pressed ( egui:: Key :: ArrowDown ) ) {
318
+ direction = Some ( Direction :: South ) ;
319
+ } else if ui. input ( |e| e. key_pressed ( egui:: Key :: ArrowUp ) ) {
320
+ direction = Some ( Direction :: North ) ;
321
+ } else if ui. input ( |e| e. key_pressed ( egui:: Key :: ArrowLeft ) ) {
322
+ direction = Some ( Direction :: West ) ;
323
+ } else if ui. input ( |e| e. key_pressed ( egui:: Key :: ArrowRight ) ) {
324
+ direction = Some ( Direction :: East ) ;
325
+ } else {
326
+ ui. input ( |e| {
327
+ for event in e. filtered_events ( & egui:: EventFilter {
328
+ tab : true ,
329
+ escape : false ,
330
+ horizontal_arrows : true ,
331
+ vertical_arrows : true ,
332
+ } ) {
333
+ match event {
334
+ egui:: Event :: Text ( text) | egui:: Event :: Paste ( text) => {
335
+ for char in text. chars ( ) {
336
+ let _ = self . befreak_state . code . set (
337
+ self . cursor_position . 1 ,
338
+ self . cursor_position . 0 ,
339
+ char,
340
+ ) ;
341
+ if self . cursor_position
342
+ == (
343
+ self . befreak_state . code . row_len ( ) - 1 ,
344
+ self . befreak_state . code . column_len ( ) - 1 ,
345
+ )
346
+ {
347
+ self . cursor_position = ( 0 , 0 ) ;
348
+ } else if self . cursor_position . 0
349
+ >= self . befreak_state . code . row_len ( ) - 1
350
+ {
351
+ self . cursor_position = ( 0 , self . cursor_position . 1 + 1 ) ;
352
+ } else {
353
+ self . cursor_position =
354
+ ( self . cursor_position . 0 + 1 , self . cursor_position . 1 ) ;
355
+ }
356
+ }
357
+ }
358
+ _ => ( ) ,
359
+ }
360
+ }
361
+ } ) ;
362
+ }
363
+
364
+ if let Some ( direction) = direction {
365
+ self . cursor_position = self
366
+ . befreak_state
367
+ . move_location ( self . cursor_position , direction)
368
+ }
319
369
let time_per_step = Duration :: from_millis ( ( 500.0 - 49.0 * self . speed ) as u64 ) ;
320
370
let elapsed = self . previous_instant . elapsed ( ) ;
321
371
if !self . paused {
@@ -450,8 +500,7 @@ impl eframe::App for AppState {
450
500
ui. separator ( ) ;
451
501
}
452
502
453
- //TODO: different colours depending on state?
454
- let cursor_color = match self . befreak_state {
503
+ let position_color = match self . befreak_state {
455
504
BefreakState {
456
505
inverse_mode : true ,
457
506
string_mode : true ,
@@ -480,8 +529,12 @@ impl eframe::App for AppState {
480
529
ui. spacing_mut ( ) . item_spacing . x = 0.0 ;
481
530
for ( index_y, row) in self . befreak_state . code . rows_iter ( ) . enumerate ( ) {
482
531
for ( index_x, c) in row. enumerate ( ) {
483
- if self . befreak_state . location == ( index_x, index_y) {
484
- ui. label ( egui:: RichText :: new ( * c) . background_color ( cursor_color) ) ;
532
+ if self . cursor_position == ( index_x, index_y) {
533
+ ui. label (
534
+ egui:: RichText :: new ( * c) . background_color ( egui:: Color32 :: GRAY ) ,
535
+ ) ;
536
+ } else if self . befreak_state . location == ( index_x, index_y) {
537
+ ui. label ( egui:: RichText :: new ( * c) . background_color ( position_color) ) ;
485
538
} else {
486
539
ui. label ( c. to_string ( ) ) ;
487
540
}
@@ -645,7 +698,6 @@ impl BefreakState {
645
698
}
646
699
647
700
fn get_instruction ( & self , location : ( usize , usize ) ) -> Result < & char , BefreakError > {
648
- // TODO: make movement wrap around, as that appears to be the intended behaviour
649
701
self . code
650
702
. get ( location. 1 , location. 0 )
651
703
. ok_or ( BefreakError :: InvalidPosition )
@@ -683,18 +735,39 @@ impl BefreakState {
683
735
Ok ( ( ) )
684
736
}
685
737
686
- fn move_location ( & mut self ) {
687
- let Self {
688
- location,
689
- direction,
690
- ..
691
- } = self ;
692
- self . location = match direction {
693
- Direction :: North => ( location. 0 , location. 1 - 1 ) ,
694
- Direction :: South => ( location. 0 , location. 1 + 1 ) ,
695
- Direction :: East => ( location. 0 + 1 , location. 1 ) ,
696
- Direction :: West => ( location. 0 - 1 , location. 1 ) ,
738
+ fn move_location ( & self , location : ( usize , usize ) , direction : Direction ) -> ( usize , usize ) {
739
+ let loc;
740
+ match direction {
741
+ Direction :: North => {
742
+ if location. 1 == 0 {
743
+ loc = ( location. 0 , self . code . column_len ( ) - 1 ) ;
744
+ } else {
745
+ loc = ( location. 0 , location. 1 - 1 ) ;
746
+ }
747
+ }
748
+ Direction :: South => {
749
+ if location. 1 + 1 >= self . code . column_len ( ) {
750
+ loc = ( location. 0 , 0 ) ;
751
+ } else {
752
+ loc = ( location. 0 , location. 1 + 1 ) ;
753
+ }
754
+ }
755
+ Direction :: West => {
756
+ if location. 0 == 0 {
757
+ loc = ( self . code . row_len ( ) - 1 , location. 1 ) ;
758
+ } else {
759
+ loc = ( location. 0 - 1 , location. 1 ) ;
760
+ }
761
+ }
762
+ Direction :: East => {
763
+ if location. 0 + 1 >= self . code . row_len ( ) {
764
+ loc = ( 0 , location. 1 ) ;
765
+ } else {
766
+ loc = ( location. 0 + 1 , location. 1 ) ;
767
+ }
768
+ }
697
769
} ;
770
+ loc
698
771
}
699
772
700
773
// TODO: this is a shit name. rename it
@@ -735,7 +808,7 @@ impl BefreakState {
735
808
fn step ( & mut self ) -> Result < ( ) , BefreakError > {
736
809
// http://tunes.org/~iepos/befreak.html#reference
737
810
738
- self . move_location ( ) ;
811
+ self . location = self . move_location ( self . location , self . direction ) ;
739
812
740
813
if self . direction_reversed {
741
814
self . step -= 1 ;
0 commit comments