structure
This commit is contained in:
parent
18bfb27f4e
commit
c529578069
|
@ -7,4 +7,5 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] }
|
||||
|
||||
rand = "0.8.5"
|
||||
|
|
|
@ -33,14 +33,10 @@ pub struct Hieroglyph {
|
|||
}
|
||||
|
||||
pub trait Useful {
|
||||
// fn get_syllable (&self) -> String;
|
||||
fn get_kana(&self) -> Kanas;
|
||||
}
|
||||
|
||||
impl Useful for Hieroglyph {
|
||||
// fn get_syllable (&self) -> String {
|
||||
// self.syllable.to_string()
|
||||
// }
|
||||
fn get_kana(&self) -> Kanas {
|
||||
for row in ROMAJI {
|
||||
if row.contains(&(self.syllable).as_str()) {
|
||||
|
|
243
src/main.rs
243
src/main.rs
|
@ -1,13 +1,11 @@
|
|||
mod game;
|
||||
mod labled_switch;
|
||||
mod ui;
|
||||
|
||||
use crate::game::*;
|
||||
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::ops::Deref;
|
||||
use crate::ui::build_ui;
|
||||
|
||||
use gtk::prelude::*;
|
||||
use gtk::{glib, Application};
|
||||
|
||||
const APP_ID: &str = "org.foxarmy.learn-hieroglyph";
|
||||
|
||||
|
@ -18,234 +16,3 @@ fn main() -> glib::ExitCode {
|
|||
|
||||
app.run()
|
||||
}
|
||||
|
||||
fn build_menu(w: &RefCell<ApplicationWindow>) -> Box {
|
||||
let menu: Box = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.halign(gtk::Align::Center)
|
||||
.build();
|
||||
|
||||
let settings_container: Box = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.valign(gtk::Align::Start)
|
||||
.halign(gtk::Align::Start)
|
||||
.build();
|
||||
|
||||
let gamemode_settings_container: Box = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.valign(gtk::Align::Start)
|
||||
.halign(gtk::Align::Start)
|
||||
.build();
|
||||
|
||||
let hiragana_setting_container: Box = Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.valign(gtk::Align::Center)
|
||||
.halign(gtk::Align::Center)
|
||||
.build();
|
||||
|
||||
let katakana_setting_container: Box = Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.valign(gtk::Align::Center)
|
||||
.halign(gtk::Align::Center)
|
||||
.build();
|
||||
|
||||
let start_button: Button = Button::builder().label("Start!").build();
|
||||
|
||||
let mut settings = Cell::new(Settings::new(APP_ID));
|
||||
|
||||
start_button.connect_clicked(clone!( @strong w => move |_| {
|
||||
w.borrow_mut().set_child(Some(&build_game(&w)))
|
||||
}));
|
||||
//////////////////kana to romaji setting////////////////////////////
|
||||
let ktr_switch = labled_switch::build("enable あ->a");
|
||||
gamemode_settings_container.append(&ktr_switch.0);
|
||||
|
||||
////////////////romaji to kana setting///////////////////////////////
|
||||
let rtk_switch = labled_switch::build("enable a->あ");
|
||||
gamemode_settings_container.append(&rtk_switch.0);
|
||||
////////////////hiragana enable setting/////////////////////////////
|
||||
let hiragana_switch = labled_switch::build("enable hiragana");
|
||||
hiragana_setting_container.append(&hiragana_switch.0);
|
||||
////////////////katakana enable setting/////////////////////////////
|
||||
let katakana_switch = labled_switch::build("enable katakana");
|
||||
katakana_setting_container.append(&katakana_switch.0);
|
||||
|
||||
let s = settings.get_mut();
|
||||
s.bind("is-ktr-enabled", &ktr_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-katakana-enabled", &katakana_switch.1, "active")
|
||||
.build();
|
||||
|
||||
settings_container.append(&gamemode_settings_container);
|
||||
settings_container.append(&hiragana_setting_container);
|
||||
settings_container.append(&katakana_setting_container);
|
||||
|
||||
menu.append(&settings_container);
|
||||
|
||||
menu.append(&start_button);
|
||||
|
||||
menu
|
||||
}
|
||||
|
||||
fn random_kana(hiragana: bool, katakana: bool) -> Hieroglyph {
|
||||
if hiragana && katakana {
|
||||
generate_random_hieroglyph(if rand::thread_rng().gen_bool(0.5) {
|
||||
Kanas::Katakana
|
||||
} else {
|
||||
Kanas::Hiragana
|
||||
})
|
||||
} else {
|
||||
generate_random_hieroglyph(if hiragana {
|
||||
Kanas::Hiragana
|
||||
} else {
|
||||
Kanas::Katakana
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn ask(ktr: bool, rtk: bool, hiragana: bool, katakana: bool) -> Hieroglyph {
|
||||
if ktr && rtk {
|
||||
return if rand::thread_rng().gen_bool(0.5) {
|
||||
random_kana(hiragana, katakana)
|
||||
} else {
|
||||
generate_random_hieroglyph(Kanas::Romaji)
|
||||
};
|
||||
} else if ktr {
|
||||
return random_kana(hiragana, katakana);
|
||||
} else {
|
||||
return generate_random_hieroglyph(Kanas::Romaji);
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_question_text(question: &Hieroglyph, exact_kana: &Kanas) -> String {
|
||||
return String::from(match question.get_kana() {
|
||||
Kanas::Romaji => match exact_kana {
|
||||
Kanas::Hiragana => {
|
||||
format!("Name corresponding hiragana for {}", question.to_string())
|
||||
}
|
||||
Kanas::Katakana => {
|
||||
format!("Name corresponding katakana for {}", question.to_string())
|
||||
}
|
||||
_ => {
|
||||
panic!("HOW DID YOU GET HERE!?");
|
||||
}
|
||||
},
|
||||
Kanas::Hiragana => {
|
||||
format!(
|
||||
"Name corresponding romaji for hiragana {}",
|
||||
question.to_string()
|
||||
)
|
||||
}
|
||||
Kanas::Katakana => {
|
||||
format!(
|
||||
"Name corresponding romaji for katakana {}",
|
||||
question.to_string()
|
||||
)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn build_game(_w: &RefCell<ApplicationWindow>) -> Box {
|
||||
let game: Box = Box::builder()
|
||||
.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();
|
||||
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 settings: Settings = Settings::new(APP_ID);
|
||||
|
||||
let ktr: bool = settings.boolean("is-ktr-enabled");
|
||||
let rtk: bool = settings.boolean("is-rtk-enabled");
|
||||
let hiragana: bool = settings.boolean("is-hiragana-enabled");
|
||||
let katakana: bool = settings.boolean("is-katakana-enabled");
|
||||
|
||||
let question: RefCell<Hieroglyph> = RefCell::new(ask(ktr, rtk, hiragana, 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(
|
||||
match question.borrow().get_kana() {
|
||||
Kanas::Romaji => match exact_kana.borrow().deref() {
|
||||
Kanas::Hiragana => &Kanas::Hiragana,
|
||||
Kanas::Katakana => &Kanas::Katakana,
|
||||
_ => panic!("HOW DID YOU GET HERE?!"),
|
||||
},
|
||||
_ => &Kanas::Romaji,
|
||||
},
|
||||
&question.borrow(),
|
||||
));
|
||||
|
||||
let binding = generate_question_text(&question.borrow(), exact_kana.borrow().deref());
|
||||
let text: &str = &(binding.as_str());
|
||||
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 answer: &String = &entry.text().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)};
|
||||
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);
|
||||
*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(
|
||||
match question.borrow().get_kana() {
|
||||
Kanas::Romaji => match exact_kana.borrow().deref() {
|
||||
Kanas::Hiragana => &Kanas::Hiragana,
|
||||
Kanas::Katakana => &Kanas::Katakana,
|
||||
_ => panic!("HOW DID YOU GET HERE?!")
|
||||
},
|
||||
_ => &Kanas::Romaji
|
||||
},
|
||||
&question.borrow()
|
||||
);
|
||||
let binding = generate_question_text(&question.borrow(), exact_kana.borrow().deref());
|
||||
let text: &str = &(binding.as_str());
|
||||
question_label.borrow_mut().set_label(text);
|
||||
entry.set_text("");
|
||||
});
|
||||
|
||||
entry.connect_activate(iteration);
|
||||
|
||||
game.append(question_label.get_mut());
|
||||
game.append(&entry);
|
||||
game.append(counter_label.get_mut());
|
||||
|
||||
game
|
||||
}
|
||||
|
||||
fn build_ui(app: &Application) {
|
||||
let mut window: RefCell<ApplicationWindow> = RefCell::new(
|
||||
ApplicationWindow::builder()
|
||||
.application(app)
|
||||
.title("Test")
|
||||
.default_height(920)
|
||||
.default_width(480)
|
||||
.build(),
|
||||
);
|
||||
|
||||
let menu = build_menu(&window);
|
||||
|
||||
window.get_mut().set_child(Some(&menu));
|
||||
|
||||
window.get_mut().present();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,154 @@
|
|||
use crate::game::*;
|
||||
|
||||
use crate::APP_ID;
|
||||
|
||||
use gio::Settings;
|
||||
use gtk::{gio, prelude::*, Entry, Label};
|
||||
use gtk::{glib::clone, ApplicationWindow, Box};
|
||||
use rand::prelude::*;
|
||||
use std::cell::RefCell;
|
||||
use std::ops::Deref;
|
||||
|
||||
fn random_kana(hiragana: bool, katakana: bool) -> Hieroglyph {
|
||||
if hiragana && katakana {
|
||||
generate_random_hieroglyph(if rand::thread_rng().gen_bool(0.5) {
|
||||
Kanas::Katakana
|
||||
} else {
|
||||
Kanas::Hiragana
|
||||
})
|
||||
} else {
|
||||
generate_random_hieroglyph(if hiragana {
|
||||
Kanas::Hiragana
|
||||
} else {
|
||||
Kanas::Katakana
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn ask(ktr: bool, rtk: bool, hiragana: bool, katakana: bool) -> Hieroglyph {
|
||||
if ktr && rtk {
|
||||
return if rand::thread_rng().gen_bool(0.5) {
|
||||
random_kana(hiragana, katakana)
|
||||
} else {
|
||||
generate_random_hieroglyph(Kanas::Romaji)
|
||||
};
|
||||
} else if ktr {
|
||||
return random_kana(hiragana, katakana);
|
||||
} else {
|
||||
return generate_random_hieroglyph(Kanas::Romaji);
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_question_text(question: &Hieroglyph, exact_kana: &Kanas) -> String {
|
||||
return String::from(match question.get_kana() {
|
||||
Kanas::Romaji => match exact_kana {
|
||||
Kanas::Hiragana => {
|
||||
format!("Name corresponding hiragana for {}", question.to_string())
|
||||
}
|
||||
Kanas::Katakana => {
|
||||
format!("Name corresponding katakana for {}", question.to_string())
|
||||
}
|
||||
_ => {
|
||||
panic!("HOW DID YOU GET HERE!?");
|
||||
}
|
||||
},
|
||||
Kanas::Hiragana => {
|
||||
format!(
|
||||
"Name corresponding romaji for hiragana {}",
|
||||
question.to_string()
|
||||
)
|
||||
}
|
||||
Kanas::Katakana => {
|
||||
format!(
|
||||
"Name corresponding romaji for katakana {}",
|
||||
question.to_string()
|
||||
)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn build_game(_w: &RefCell<ApplicationWindow>) -> Box {
|
||||
let game: Box = Box::builder()
|
||||
.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();
|
||||
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 settings: Settings = Settings::new(APP_ID);
|
||||
|
||||
let ktr: bool = settings.boolean("is-ktr-enabled");
|
||||
let rtk: bool = settings.boolean("is-rtk-enabled");
|
||||
let hiragana: bool = settings.boolean("is-hiragana-enabled");
|
||||
let katakana: bool = settings.boolean("is-katakana-enabled");
|
||||
|
||||
let question: RefCell<Hieroglyph> = RefCell::new(ask(ktr, rtk, hiragana, 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(
|
||||
match question.borrow().get_kana() {
|
||||
Kanas::Romaji => match exact_kana.borrow().deref() {
|
||||
Kanas::Hiragana => &Kanas::Hiragana,
|
||||
Kanas::Katakana => &Kanas::Katakana,
|
||||
_ => panic!("HOW DID YOU GET HERE?!"),
|
||||
},
|
||||
_ => &Kanas::Romaji,
|
||||
},
|
||||
&question.borrow(),
|
||||
));
|
||||
|
||||
let binding = generate_question_text(&question.borrow(), exact_kana.borrow().deref());
|
||||
let text: &str = &(binding.as_str());
|
||||
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 answer: &String = &entry.text().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)};
|
||||
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);
|
||||
*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(
|
||||
match question.borrow().get_kana() {
|
||||
Kanas::Romaji => match exact_kana.borrow().deref() {
|
||||
Kanas::Hiragana => &Kanas::Hiragana,
|
||||
Kanas::Katakana => &Kanas::Katakana,
|
||||
_ => panic!("HOW DID YOU GET HERE?!")
|
||||
},
|
||||
_ => &Kanas::Romaji
|
||||
},
|
||||
&question.borrow()
|
||||
);
|
||||
let binding = generate_question_text(&question.borrow(), exact_kana.borrow().deref());
|
||||
let text: &str = &(binding.as_str());
|
||||
question_label.borrow_mut().set_label(text);
|
||||
entry.set_text("");
|
||||
});
|
||||
|
||||
entry.connect_activate(iteration);
|
||||
|
||||
game.append(question_label.get_mut());
|
||||
game.append(&entry);
|
||||
game.append(counter_label.get_mut());
|
||||
|
||||
game
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
use crate::ui::guessing::build_game;
|
||||
|
||||
use crate::APP_ID;
|
||||
|
||||
use gio::Settings;
|
||||
use gtk::{gio, prelude::*, Button};
|
||||
use gtk::{glib::clone, ApplicationWindow, Box};
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
use crate::labled_switch;
|
||||
|
||||
pub fn build_menu(w: &RefCell<ApplicationWindow>) -> Box {
|
||||
let menu: Box = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.halign(gtk::Align::Center)
|
||||
.build();
|
||||
|
||||
let settings_container: Box = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.valign(gtk::Align::Start)
|
||||
.halign(gtk::Align::Start)
|
||||
.build();
|
||||
|
||||
let gamemode_settings_container: Box = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.valign(gtk::Align::Start)
|
||||
.halign(gtk::Align::Start)
|
||||
.build();
|
||||
|
||||
let hiragana_setting_container: Box = Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.valign(gtk::Align::Center)
|
||||
.halign(gtk::Align::Center)
|
||||
.build();
|
||||
|
||||
let katakana_setting_container: Box = Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.valign(gtk::Align::Center)
|
||||
.halign(gtk::Align::Center)
|
||||
.build();
|
||||
|
||||
let start_button: Button = Button::builder().label("Start!").build();
|
||||
|
||||
let mut settings = Cell::new(Settings::new(APP_ID));
|
||||
|
||||
start_button.connect_clicked(clone!( @strong w => move |_| {
|
||||
w.borrow_mut().set_child(Some(&build_game(&w)))
|
||||
}));
|
||||
//////////////////kana to romaji setting////////////////////////////
|
||||
let ktr_switch = labled_switch::build("enable あ->a");
|
||||
gamemode_settings_container.append(&ktr_switch.0);
|
||||
|
||||
////////////////romaji to kana setting///////////////////////////////
|
||||
let rtk_switch = labled_switch::build("enable a->あ");
|
||||
gamemode_settings_container.append(&rtk_switch.0);
|
||||
////////////////hiragana enable setting/////////////////////////////
|
||||
let hiragana_switch = labled_switch::build("enable hiragana");
|
||||
hiragana_setting_container.append(&hiragana_switch.0);
|
||||
////////////////katakana enable setting/////////////////////////////
|
||||
let katakana_switch = labled_switch::build("enable katakana");
|
||||
katakana_setting_container.append(&katakana_switch.0);
|
||||
|
||||
let s = settings.get_mut();
|
||||
s.bind("is-ktr-enabled", &ktr_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-katakana-enabled", &katakana_switch.1, "active")
|
||||
.build();
|
||||
|
||||
settings_container.append(&gamemode_settings_container);
|
||||
settings_container.append(&hiragana_setting_container);
|
||||
settings_container.append(&katakana_setting_container);
|
||||
|
||||
menu.append(&settings_container);
|
||||
|
||||
menu.append(&start_button);
|
||||
|
||||
menu
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
mod guessing;
|
||||
mod menu;
|
||||
|
||||
use crate::ui::menu::build_menu;
|
||||
|
||||
use gtk::prelude::*;
|
||||
use gtk::{Application, ApplicationWindow};
|
||||
use std::cell::RefCell;
|
||||
|
||||
pub fn build_ui(app: &Application) {
|
||||
let mut window: RefCell<ApplicationWindow> = RefCell::new(
|
||||
ApplicationWindow::builder()
|
||||
.application(app)
|
||||
.title("Test")
|
||||
.build(),
|
||||
);
|
||||
|
||||
let menu = build_menu(&window);
|
||||
|
||||
window.get_mut().set_child(Some(&menu));
|
||||
|
||||
window.get_mut().present();
|
||||
}
|
Loading…
Reference in New Issue