learn-hieroglyphs/src/main.rs

208 lines
6.8 KiB
Rust
Raw Normal View History

2024-03-22 15:59:42 +03:00
mod game;
2024-03-25 05:20:18 +03:00
mod labled_switch;
2024-03-22 15:59:42 +03:00
2024-03-25 05:20:18 +03:00
use gio::Settings;
use std::cell::{Cell, RefCell};
use gtk::glib::{clone, closure_local};
use gtk::{gio, prelude::*, Button, Entry, Label};
use gtk::{glib, Application, ApplicationWindow, Box};
2024-03-22 15:59:42 +03:00
use crate::game::*;
2024-03-25 05:20:18 +03:00
use rand::prelude::*;
2024-03-22 15:59:42 +03:00
2024-03-25 05:20:18 +03:00
const APP_ID: &str = "org.foxarmy.learn-hieroglyph";
2024-03-22 15:59:42 +03:00
fn main() -> glib::ExitCode {
let app = Application::builder().application_id(APP_ID).build();
let syllable = generate_random_hieroglyph(Kanas::Romaji);
let hiragana = get_kana_pair_for_hieroglyph(Kanas::Hiragana, &syllable);
let katakana = get_kana_pair_for_hieroglyph(Kanas::Katakana, &syllable);
println!("{syllable} = {hiragana} = {katakana}");
app.connect_activate(build_ui);
app.run()
}
fn build_menu(w: &RefCell<ApplicationWindow>) -> Box {
let menu: Box = Box::builder()
.orientation(gtk::Orientation::Vertical)
.halign(gtk::Align::Center)
.build();
2024-03-25 05:20:18 +03:00
let settings_container: Box = Box::builder()
.orientation(gtk::Orientation::Vertical)
.valign(gtk::Align::Start)
.halign(gtk::Align::Start)
2024-03-22 15:59:42 +03:00
.build();
2024-03-25 05:20:18 +03:00
let gamemode_settings_container: Box = Box::builder()
2024-03-22 15:59:42 +03:00
.orientation(gtk::Orientation::Vertical)
2024-03-25 05:20:18 +03:00
.valign(gtk::Align::Start)
.halign(gtk::Align::Start)
2024-03-22 15:59:42 +03:00
.build();
2024-03-25 05:20:18 +03:00
let hiragana_setting_container: Box = Box::builder()
2024-03-22 15:59:42 +03:00
.orientation(gtk::Orientation::Horizontal)
.valign(gtk::Align::Center)
.halign(gtk::Align::Center)
.build();
2024-03-25 05:20:18 +03:00
let katakana_setting_container: Box = Box::builder()
2024-03-22 15:59:42 +03:00
.orientation(gtk::Orientation::Horizontal)
.valign(gtk::Align::Center)
.halign(gtk::Align::Center)
.build();
2024-03-25 05:20:18 +03:00
let start_button: Button = Button::builder()
.label("Start!")
.build();
2024-03-22 15:59:42 +03:00
2024-03-25 05:20:18 +03:00
let mut settings = Cell::new(Settings::new(APP_ID));
2024-03-22 15:59:42 +03:00
2024-03-25 05:20:18 +03:00
start_button.connect_clicked(clone!( @strong w => move |_| {
w.borrow_mut().set_child(Some(&build_game(&w)))
2024-03-22 15:59:42 +03:00
}));
2024-03-25 05:20:18 +03:00
//////////////////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);
2024-03-22 15:59:42 +03:00
menu
}
2024-03-25 05:20:18 +03:00
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())
}
});
}
2024-03-22 15:59:42 +03:00
fn build_game(w: &RefCell<ApplicationWindow>) -> Box {
let game: Box = Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
2024-03-25 05:20:18 +03:00
let mut question_label: RefCell<Label> = RefCell::new(Label::builder()
2024-03-22 15:59:42 +03:00
.label("placeholder")
2024-03-25 05:20:18 +03:00
.build());
let entry: Entry = Entry::builder()
.placeholder_text("Answer")
2024-03-22 15:59:42 +03:00
.build();
2024-03-25 05:20:18 +03:00
let settings: Settings = Settings::new(APP_ID);
let ktr: bool = settings.boolean("is-ktr-enabled");
let rtk: bool = settings.boolean("is-rtk-enabled");
let exact_kana: Kanas = if rand::thread_rng().gen_bool(0.5) { Kanas::Hiragana } else { Kanas:: Katakana };
let hiragana: bool = settings.boolean("is-hiragana-enabled");
let katakana: bool = settings.boolean("is-katakana-enabled");
let question: Hieroglyph = ask(ktr, rtk, hiragana, katakana);
let binding = generate_question_text(&question, &exact_kana);
let text: &str = &(binding.as_str());
question_label.get_mut().set_label(text);
entry.connect_activate(clone!( @strong question_label => move |entry: &Entry| {
let question: Hieroglyph = ask(ktr, rtk, hiragana, katakana);
let exact_kana: Kanas = if rand::thread_rng().gen_bool(0.5) { Kanas::Hiragana } else { Kanas:: Katakana };
let binding = generate_question_text(&question, &exact_kana);
let text: &str = &(binding.as_str());
question_label.borrow_mut().set_label(text);
}));
game.append(question_label.get_mut());
2024-03-22 15:59:42 +03:00
game.append(&entry);
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();
2024-03-08 13:51:55 +03:00
}