Skip to content

Commit

Permalink
Added an exercise book explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
margual56 committed Mar 14, 2023
1 parent 2eb213f commit e443a80
Show file tree
Hide file tree
Showing 10 changed files with 915 additions and 435 deletions.
1,198 changes: 772 additions & 426 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ license = "GPL-2.0"
clap = { version = "^4.1", features = ["derive"] }
pest = "^2.4"
pest_derive = "^2.4"
eframe = "^0.20"
eframe = "^0.21.3"
rfd = "^0.11"
egui_extras = "0.20.0"
egui_extras = {version = "^0.21.0", features = ["image"] }
image = { version = "^0.24", default-features = false, features = ["png"] }
webbrowser = "0.8.3" # There is a vulnerability in version 0.8.5
internationalization = "0.0.3"
log = "0.4.17"
Expand Down
11 changes: 11 additions & 0 deletions assets/ui/exercise1/code.tm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// a + b

{11111011};

I = {q0};
F = {q2};

(q0, 1, 0, R, q1);

(q1, 1, 1, R, q1);
(q1, 0, 0, R, q2);
Binary file added assets/ui/exercise1/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions assets/ui/exercise2/code.tm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
///Number x2

{1111};

I = {q0};
F = {q1};

(q0, 1, 0, R, q1);

(q1, 1, 0, R, q2);
(q1, 0, 0, H, q1);

(q2, 1, 1, R, q2);
(q2, 0, 0, R, q3);

(q3, 1, 1, R, q3);
(q3, 0, 1, L, q4);

(q4, 1, 1, L, q4);
(q4, 0, 0, L, q5);

(q5, 1, 1, L, q5);
(q5, 0, 1, R, q1);

Binary file added assets/ui/exercise2/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn run_machine_gui(file: PathBuf) {
),
options,
Box::new(|cc| Box::new(MyApp::new(tm, cc))),
);
).unwrap();
}

#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -194,7 +194,7 @@ fn handle_error(e: pest::error::Error<Rule>, file: PathBuf) {
),
options,
Box::new(|cc| Box::new(ErrorWindow::new(e, file, cc))),
);
).unwrap();
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down
26 changes: 21 additions & 5 deletions src/window.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::turing::{Rule, TuringOutput};
use crate::windows::{AboutWindow, DebugWindow, InfiniteLoopWindow, SecondaryWindow};
use crate::windows::{AboutWindow, DebugWindow, InfiniteLoopWindow, SecondaryWindow, BookWindow};
use crate::{turing::TuringMachine, TuringWidget};
use eframe;
use eframe::egui::{self, Id, RichText, Ui};
use eframe::epaint::Color32;
use internationalization::t;
use log::warn;
//use egui_extras::{Column, TableBuilder};

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Language {
Expand All @@ -21,6 +20,7 @@ pub struct MyApp {
about_window: Option<Box<AboutWindow>>,
debug_window: Option<Box<DebugWindow>>,
infinite_loop_window: Option<Box<InfiniteLoopWindow>>,
book_window: Option<Box<BookWindow>>,
lang: Language,
}

Expand All @@ -40,7 +40,8 @@ impl MyApp {
about_window: None,
debug_window: None,
infinite_loop_window: None,
lang: Language::English,
book_window: None,
lang: Language::English
}
}

Expand Down Expand Up @@ -132,7 +133,7 @@ impl MyApp {
ui.button(t!("lbl.machine.step", lang))
})
.clicked()
|| ui.input().key_pressed(egui::Key::ArrowRight)
|| ui.input(|i| i.key_pressed(egui::Key::ArrowRight))
|| !self.tm.paused)
&& !editor_focused
{
Expand Down Expand Up @@ -196,6 +197,17 @@ impl eframe::App for MyApp {
}
}

if let Some(book) = self.book_window.as_mut() {
let (active, code) = book.show(ctx);

if let Some(c) = code {
self.restart(&c);
self.book_window = None;
}else if !active {
self.book_window = None;
}
}

egui::TopBottomPanel::top("header")
.default_height(35.0)
.show(ctx, |ui| {
Expand All @@ -217,6 +229,10 @@ impl eframe::App for MyApp {
}
});

if ui.button("Exercises").clicked() {
self.book_window = Some(Box::new(BookWindow::new(&self.get_lang())));
}

ui.menu_button(t!("menu.language", lang), |ui| {
ui.radio_value(&mut self.lang, Language::English, t!("lang.en", lang));
ui.radio_value(&mut self.lang, Language::Spanish, t!("lang.es", lang));
Expand Down Expand Up @@ -390,7 +406,7 @@ impl eframe::App for MyApp {
}
let b = ui.button(text);
//b.ctx.set_style(style);
if (b.clicked() || ui.input().key_pressed(egui::Key::Space))
if (b.clicked() || ui.input(|i| i.key_pressed(egui::Key::Space)))
&& !editor_focused
{
if self.tm.finished() {
Expand Down
80 changes: 80 additions & 0 deletions src/windows/book.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use eframe::{egui, epaint::Vec2};
use egui_extras::RetainedImage;
use internationalization::t;

struct Exercise {
image: RetainedImage,
code: String,
}

impl Exercise {
pub fn new(title: &str, img: &[u8], code: String) -> Self {
Self {
image: RetainedImage::from_image_bytes(
title,
img
).unwrap(),
code: String::from(code),
}
}
}


pub struct BookWindow {
lang: String,
exercises: Vec<Exercise>,
selected: usize
}

impl BookWindow {
pub fn new(
lang: &str,
) -> Self {
let exercises = vec![
Exercise::new("Exercise 1", include_bytes!("../../assets/ui/exercise1/cover.png"), String::from_utf8(include_bytes!("../../assets/ui/exercise1/code.tm").to_vec()).unwrap()),
Exercise::new("Exercise 2", include_bytes!("../../assets/ui/exercise2/cover.png"), String::from_utf8(include_bytes!("../../assets/ui/exercise2/code.tm").to_vec()).unwrap()),
];

Self {
lang: String::from(lang),
exercises,
selected: 0,
}
}

pub fn set_lang(&mut self, lang: &str) {
self.lang = lang.to_string();
}

pub fn show(&mut self, ctx: &egui::Context) -> (bool, Option<String>) {
let mut active = true;
let mut code = None;

egui::Window::new(t!("title.debug", self.lang))
.resizable(false)
.open(&mut active)
.show(ctx, |ui| {
ui.vertical_centered_justified(|ui| {
self.exercises[self.selected].image.show_max_size(ui, Vec2::new(600.0, 500.0));

ui.horizontal(|ui| {
if ui.add_enabled(self.selected > 0, egui::Button::new("Previous")).clicked() {
self.selected -= 1;
}

ui.add_space(ui.available_width()-50.0);

if ui.add_enabled(self.selected < self.exercises.len()-1, egui::Button::new("Next")).clicked() {
self.selected += 1;
}
});

if ui.button("Use this exercise").clicked() {
code = Some(self.exercises[self.selected].code.clone());
}
});
});

(active, code)
}
}
2 changes: 2 additions & 0 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ mod about_window;
mod debug_window;
mod error_window;
mod infinite_loop_window;
mod book;

pub use about_window::AboutWindow;
pub use debug_window::DebugWindow;
pub use error_window::ErrorWindow;
pub use infinite_loop_window::InfiniteLoopWindow;
pub use book::BookWindow;

pub trait SecondaryWindow {
fn set_lang(&mut self, lang: &str);
Expand Down

0 comments on commit e443a80

Please sign in to comment.