rustfmt and fixed not respecting kana preferences in ktr mode

This commit is contained in:
leca 2024-03-26 14:11:46 +03:00
parent cb7ce267f9
commit 7ad2298676
3 changed files with 113 additions and 87 deletions

View File

@ -12,13 +12,17 @@ pub enum Kanas {
Katakana, Katakana,
} }
impl std::fmt::Display for Kanas{ impl std::fmt::Display for Kanas {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self { write!(
Kanas::Romaji => "Romaji", f,
Kanas::Hiragana => "Hiragana", "{}",
Kanas::Katakana => "Katakana" match self {
}) Kanas::Romaji => "Romaji",
Kanas::Hiragana => "Hiragana",
Kanas::Katakana => "Katakana",
}
)
} }
} }
@ -30,27 +34,31 @@ pub struct Hieroglyph {
pub trait Useful { pub trait Useful {
// fn get_syllable (&self) -> String; // fn get_syllable (&self) -> String;
fn get_kana (&self) -> Kanas; fn get_kana(&self) -> Kanas;
} }
impl Useful for Hieroglyph { impl Useful for Hieroglyph {
// fn get_syllable (&self) -> String { // fn get_syllable (&self) -> String {
// self.syllable.to_string() // self.syllable.to_string()
// } // }
fn get_kana (&self) -> Kanas { fn get_kana(&self) -> Kanas {
for row in ROMAJI { for row in ROMAJI {
if row.contains(&(self.syllable).as_str()) { return Kanas::Romaji; } if row.contains(&(self.syllable).as_str()) {
return Kanas::Romaji;
}
} }
for row in HIRAGANA { for row in HIRAGANA {
if row.contains(&(self.syllable).as_str()) { return Kanas::Hiragana; } if row.contains(&(self.syllable).as_str()) {
return Kanas::Hiragana;
}
} }
for row in KATAKANA { for row in KATAKANA {
if row.contains(&(self.syllable).as_str()) { return Kanas::Katakana; } if row.contains(&(self.syllable).as_str()) {
return Kanas::Katakana;
}
} }
Kanas::Romaji // /shrug Kanas::Romaji // /shrug
} }
} }
impl std::fmt::Display for Hieroglyph { impl std::fmt::Display for Hieroglyph {

View File

@ -7,10 +7,10 @@ pub fn build(text: &str) -> (Box, Switch, Label) {
.build(); .build();
let label: Label = Label::builder() let label: Label = Label::builder()
.valign(gtk::Align::End) .valign(gtk::Align::End)
.halign(gtk::Align::BaselineCenter) .halign(gtk::Align::BaselineCenter)
.label(text) .label(text)
.build(); .build();
let container = Box::builder() let container = Box::builder()
.orientation(gtk::Orientation::Horizontal) .orientation(gtk::Orientation::Horizontal)

View File

@ -1,13 +1,13 @@
mod game; mod game;
mod labled_switch; mod labled_switch;
use crate::game::*;
use gio::Settings; use gio::Settings;
use gtk::{gio, prelude::*, Button, Entry, Label};
use gtk::{glib, glib::clone, Application, ApplicationWindow, Box};
use rand::prelude::*;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::ops::Deref; use std::ops::Deref;
use gtk::{gio, prelude::*, Button, Entry, Label};
use gtk::{glib::clone, glib, Application, ApplicationWindow, Box};
use crate::game::*;
use rand::prelude::*;
const APP_ID: &str = "org.foxarmy.learn-hieroglyph"; const APP_ID: &str = "org.foxarmy.learn-hieroglyph";
@ -49,9 +49,7 @@ fn build_menu(w: &RefCell<ApplicationWindow>) -> Box {
.halign(gtk::Align::Center) .halign(gtk::Align::Center)
.build(); .build();
let start_button: Button = Button::builder() let start_button: Button = Button::builder().label("Start!").build();
.label("Start!")
.build();
let mut settings = Cell::new(Settings::new(APP_ID)); let mut settings = Cell::new(Settings::new(APP_ID));
@ -75,8 +73,10 @@ fn build_menu(w: &RefCell<ApplicationWindow>) -> Box {
let s = settings.get_mut(); let s = settings.get_mut();
s.bind("is-ktr-enabled", &ktr_switch.1, "active").build(); s.bind("is-ktr-enabled", &ktr_switch.1, "active").build();
s.bind("is-rtk-enabled", &rtk_switch.1, "active").build(); s.bind("is-rtk-enabled", &rtk_switch.1, "active").build();
s.bind("is-hiragana-enabled", &hiragana_switch.1, "active").build(); s.bind("is-hiragana-enabled", &hiragana_switch.1, "active")
s.bind("is-katakana-enabled", &katakana_switch.1, "active").build(); .build();
s.bind("is-katakana-enabled", &katakana_switch.1, "active")
.build();
settings_container.append(&gamemode_settings_container); settings_container.append(&gamemode_settings_container);
settings_container.append(&hiragana_setting_container); settings_container.append(&hiragana_setting_container);
@ -89,29 +89,29 @@ fn build_menu(w: &RefCell<ApplicationWindow>) -> Box {
menu menu
} }
fn random_kana(hiragana: bool, katakana: bool) -> Hieroglyph{ fn random_kana(hiragana: bool, katakana: bool) -> Hieroglyph {
if hiragana && katakana { if hiragana && katakana {
generate_random_hieroglyph( generate_random_hieroglyph(if rand::thread_rng().gen_bool(0.5) {
if rand::thread_rng().gen_bool(0.5) { Kanas::Katakana
Kanas::Katakana } else {
} else { Kanas::Hiragana
Kanas::Hiragana })
}
)
} else { } else {
generate_random_hieroglyph( generate_random_hieroglyph(if hiragana {
if hiragana { Kanas::Hiragana
Kanas::Hiragana } else {
} else { Kanas::Katakana
Kanas::Katakana })
}
)
} }
} }
fn ask(ktr: bool, rtk: bool, hiragana: bool, katakana: bool) -> Hieroglyph { fn ask(ktr: bool, rtk: bool, hiragana: bool, katakana: bool) -> Hieroglyph {
if ktr && rtk { if ktr && rtk {
return if rand::thread_rng().gen_bool(0.5) { random_kana(hiragana, katakana) } else { generate_random_hieroglyph(Kanas::Romaji) }; return if rand::thread_rng().gen_bool(0.5) {
random_kana(hiragana, katakana)
} else {
generate_random_hieroglyph(Kanas::Romaji)
};
} else if ktr { } else if ktr {
return random_kana(hiragana, katakana); return random_kana(hiragana, katakana);
} else { } else {
@ -119,45 +119,43 @@ fn ask(ktr: bool, rtk: bool, hiragana: bool, katakana: bool) -> Hieroglyph {
} }
} }
fn generate_question_text(question: & Hieroglyph, exact_kana: &Kanas) -> String { fn generate_question_text(question: &Hieroglyph, exact_kana: &Kanas) -> String {
return String::from(match question.get_kana() { return String::from(match question.get_kana() {
Kanas::Romaji => { Kanas::Romaji => match exact_kana {
match exact_kana { Kanas::Hiragana => {
Kanas::Hiragana => { format!("Name corresponding hiragana for {}", question.to_string())
format!("Name corresponding hiragana for {}", question.to_string()) }
}, Kanas::Katakana => {
Kanas::Katakana => { format!("Name corresponding katakana for {}", question.to_string())
format!("Name corresponding katakana for {}", question.to_string()) }
}, _ => {
_ => { panic!("HOW DID YOU GET HERE!?");
panic!("HOW DID YOU GET HERE!?");
}
} }
}, },
Kanas::Hiragana => { Kanas::Hiragana => {
format!("Name corresponding romaji for hiragana {}", question.to_string()) format!(
}, "Name corresponding romaji for hiragana {}",
question.to_string()
)
}
Kanas::Katakana => { Kanas::Katakana => {
format!("Name corresponding romaji for katakana {}", question.to_string()) format!(
"Name corresponding romaji for katakana {}",
question.to_string()
)
} }
}); });
} }
fn build_game(_w: &RefCell<ApplicationWindow>) -> Box { fn build_game(_w: &RefCell<ApplicationWindow>) -> Box {
let game: Box = Box::builder() let game: Box = Box::builder()
.orientation(gtk::Orientation::Vertical) .orientation(gtk::Orientation::Vertical)
.build();
let mut question_label: RefCell<Label> = RefCell::new(Label::builder()
.label("placeholder")
.build());
let entry: Entry = Entry::builder()
.placeholder_text("Answer")
.build(); .build();
let mut counter_label: RefCell<Label> = RefCell::new(Label::builder() let mut question_label: RefCell<Label> =
.label("Correct|incorrect: 0|0") RefCell::new(Label::builder().label("placeholder").build());
.build() let entry: Entry = Entry::builder().placeholder_text("Answer").build();
); let mut counter_label: RefCell<Label> =
RefCell::new(Label::builder().label("Correct|incorrect: 0|0").build());
let stats: RefCell<(u32, u32)> = RefCell::new((0, 0)); let stats: RefCell<(u32, u32)> = RefCell::new((0, 0));
let settings: Settings = Settings::new(APP_ID); let settings: Settings = Settings::new(APP_ID);
@ -167,30 +165,48 @@ fn build_game(_w: &RefCell<ApplicationWindow>) -> Box {
let katakana: bool = settings.boolean("is-katakana-enabled"); let katakana: bool = settings.boolean("is-katakana-enabled");
let question: RefCell<Hieroglyph> = RefCell::new(ask(ktr, rtk, hiragana, katakana)); let question: RefCell<Hieroglyph> = RefCell::new(ask(ktr, rtk, hiragana, katakana));
let exact_kana: RefCell<Kanas> = RefCell::new(if rand::thread_rng().gen_bool(0.5) { Kanas::Hiragana } else { Kanas:: Katakana }); let exact_kana: RefCell<Kanas> = RefCell::new(if hiragana && katakana {
if rand::thread_rng().gen_bool(0.5) {
Kanas::Hiragana
} else {
Kanas::Katakana
}
} else if hiragana {
Kanas::Hiragana
} else {
Kanas::Katakana
});
let correct_answer: RefCell<Hieroglyph> = RefCell::new(get_kana_pair_for_hieroglyph( let correct_answer: RefCell<Hieroglyph> = RefCell::new(get_kana_pair_for_hieroglyph(
match question.borrow().get_kana() { match question.borrow().get_kana() {
Kanas::Romaji => match exact_kana.borrow().deref() { Kanas::Romaji => match exact_kana.borrow().deref() {
Kanas::Hiragana => &Kanas::Hiragana, Kanas::Hiragana => &Kanas::Hiragana,
Kanas::Katakana => &Kanas::Katakana, Kanas::Katakana => &Kanas::Katakana,
_ => panic!("HOW DID YOU GET HERE?!") _ => panic!("HOW DID YOU GET HERE?!"),
}, },
_ => &Kanas::Romaji _ => &Kanas::Romaji,
}, },
&question.borrow() &question.borrow(),
)); ));
let binding = generate_question_text(&question.borrow(), exact_kana.borrow().deref()); let binding = generate_question_text(&question.borrow(), exact_kana.borrow().deref());
let text: &str = &(binding.as_str()); let text: &str = &(binding.as_str());
question_label.borrow_mut().set_label(text); question_label.borrow_mut().set_label(text);
let iteration = clone!( @strong question_label, @strong counter_label, @strong stats, @strong exact_kana => move |entry: &Entry| { let iteration = clone!( @strong question_label, @strong counter_label, @strong stats, @strong exact_kana => move |entry: &Entry| {
let answer: &String = &entry.text().to_string(); let answer: &String = &entry.text().to_string();
println!("{} <-> {}? = {}", answer, correct_answer.borrow().to_string(), *answer == correct_answer.borrow().to_string()); println!("{} <-> {}? = {}", answer, correct_answer.borrow().to_string(), *answer == correct_answer.borrow().to_string());
*stats.borrow_mut() = if *answer == correct_answer.borrow().to_string() { (stats.borrow().0 + 1, stats.borrow().1) } else {(stats.borrow().0, stats.borrow().1 + 1)}; *stats.borrow_mut() = if *answer == correct_answer.borrow().to_string() { (stats.borrow().0 + 1, stats.borrow().1) } else {(stats.borrow().0, stats.borrow().1 + 1)};
counter_label.borrow_mut().set_label(format!("Correct|Incorrect: {}|{}", stats.borrow().0, stats.borrow().1).as_str()); counter_label.borrow_mut().set_label(format!("Correct|Incorrect: {}|{}", stats.borrow().0, stats.borrow().1).as_str());
*question.borrow_mut() = ask(ktr, rtk, hiragana, katakana); *question.borrow_mut() = ask(ktr, rtk, hiragana, katakana);
*exact_kana.borrow_mut() =
if hiragana && katakana {
if rand::thread_rng().gen_bool(0.5) {Kanas::Hiragana} else {Kanas::Katakana}
} else if hiragana {
Kanas::Hiragana
} else {
Kanas::Katakana
};
*correct_answer.borrow_mut() = get_kana_pair_for_hieroglyph( *correct_answer.borrow_mut() = get_kana_pair_for_hieroglyph(
match question.borrow().get_kana() { match question.borrow().get_kana() {
Kanas::Romaji => match exact_kana.borrow().deref() { Kanas::Romaji => match exact_kana.borrow().deref() {
@ -218,12 +234,14 @@ fn build_game(_w: &RefCell<ApplicationWindow>) -> Box {
} }
fn build_ui(app: &Application) { fn build_ui(app: &Application) {
let mut window: RefCell<ApplicationWindow> = RefCell::new(ApplicationWindow::builder() let mut window: RefCell<ApplicationWindow> = RefCell::new(
.application(app) ApplicationWindow::builder()
.title("Test") .application(app)
.default_height(920) .title("Test")
.default_width(480) .default_height(920)
.build()); .default_width(480)
.build(),
);
let menu = build_menu(&window); let menu = build_menu(&window);