Skip to content

Commit 2ed595f

Browse files
committed
Add text editing and location wrap-around
1 parent f131ff9 commit 2ed595f

File tree

1 file changed

+98
-25
lines changed

1 file changed

+98
-25
lines changed

src/app.rs

+98-25
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ use std::time::Duration;
1111
// use std::io::{self, BufRead};
1212
// use std::path::Path;
1313

14-
// TODO: add file editing:
15-
// changing individual characters
14+
// TODO:
1615
// 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
2118

2219
static PRESETS: phf::Map<&'static str, &'static str> = phf_map! {
2320
"hello world 1" =>r#"
@@ -190,7 +187,7 @@ struct BefreakState {
190187
pub struct AppState {
191188
befreak_state: BefreakState,
192189
speed: f32,
193-
//cursor_position: (usize, usize),
190+
cursor_position: (usize, usize),
194191
paused: bool,
195192
previous_instant: Instant,
196193
extra: bool,
@@ -207,7 +204,7 @@ impl AppState {
207204
befreak_state: BefreakState::new_empty(),
208205
//state: State::new_load_file("primes1"),
209206
text_channel: channel(),
210-
//cursor_position: (0, 0), //TODO: use this for editing
207+
cursor_position: (0, 0),
211208
previous_instant: Instant::now(),
212209
paused: true,
213210
extra: false,
@@ -316,6 +313,59 @@ impl eframe::App for AppState {
316313
});
317314

318315
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+
}
319369
let time_per_step = Duration::from_millis((500.0 - 49.0 * self.speed) as u64);
320370
let elapsed = self.previous_instant.elapsed();
321371
if !self.paused {
@@ -450,8 +500,7 @@ impl eframe::App for AppState {
450500
ui.separator();
451501
}
452502

453-
//TODO: different colours depending on state?
454-
let cursor_color = match self.befreak_state {
503+
let position_color = match self.befreak_state {
455504
BefreakState {
456505
inverse_mode: true,
457506
string_mode: true,
@@ -480,8 +529,12 @@ impl eframe::App for AppState {
480529
ui.spacing_mut().item_spacing.x = 0.0;
481530
for (index_y, row) in self.befreak_state.code.rows_iter().enumerate() {
482531
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));
485538
} else {
486539
ui.label(c.to_string());
487540
}
@@ -645,7 +698,6 @@ impl BefreakState {
645698
}
646699

647700
fn get_instruction(&self, location: (usize, usize)) -> Result<&char, BefreakError> {
648-
// TODO: make movement wrap around, as that appears to be the intended behaviour
649701
self.code
650702
.get(location.1, location.0)
651703
.ok_or(BefreakError::InvalidPosition)
@@ -683,18 +735,39 @@ impl BefreakState {
683735
Ok(())
684736
}
685737

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+
}
697769
};
770+
loc
698771
}
699772

700773
// TODO: this is a shit name. rename it
@@ -735,7 +808,7 @@ impl BefreakState {
735808
fn step(&mut self) -> Result<(), BefreakError> {
736809
// http://tunes.org/~iepos/befreak.html#reference
737810

738-
self.move_location();
811+
self.location = self.move_location(self.location, self.direction);
739812

740813
if self.direction_reversed {
741814
self.step -= 1;

0 commit comments

Comments
 (0)