learn-hieroglyphs/src/main.rs

194 lines
5.5 KiB
Rust
Raw Normal View History

2024-04-12 09:33:22 +03:00
/*
TODOS:
1. remake db scheme for https://raw.githubusercontent.com/schneems/united-dictionary/master/public/languages/japanese/japanese-english.txt
2. parse it
3. add support for it
4. With incorrect answerd card, it shows hieroglyp, reading ant its translation
*/
2024-04-10 20:25:02 +03:00
mod db;
2024-03-22 15:59:42 +03:00
mod game;
2024-03-27 11:27:15 +03:00
mod ui;
mod widgets;
2024-04-14 09:15:26 +03:00
mod dictionary;
2024-03-22 15:59:42 +03:00
2024-04-01 15:42:50 +03:00
use crate::ui::menu::MenuScene;
2024-03-27 11:27:15 +03:00
2024-04-10 20:25:02 +03:00
use gtk::glib::closure_local;
use gtk::{gio, glib, Application, Button};
2024-04-12 09:33:22 +03:00
use gtk::{prelude::*, Window};
2024-04-10 20:25:02 +03:00
use ui::cards::edit::MemoryCardsEditScene;
use ui::cards::game::MemoryCardsGameScene;
use ui::cards::setup::MemoryCardsSetupScene;
use ui::guessing::game::GuessingScene;
use ui::guessing::setup::GuessingSetupScene;
enum AppWindow {
MenuScene = 1,
GuessingSetupScene = 2,
GuessingScene = 3,
MemoryCardsSetupScene = 4,
MemoryCardsEditScene = 5,
MemoryCardsGameScene = 6,
}
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-04-01 23:46:13 +03:00
2024-03-22 15:59:42 +03:00
fn main() -> glib::ExitCode {
2024-04-02 15:32:43 +03:00
gio::resources_register_include!("compiled.gresource").expect("Cannot include gresources");
2024-04-01 15:42:50 +03:00
2024-04-14 09:15:26 +03:00
db::init(); // Create database if not exists, create program's home if not exists, etc...
2024-04-05 15:01:24 +03:00
2024-04-14 09:15:26 +03:00
dictionary::import(&String::from("./reparsed.txt")); //
2024-04-13 23:13:25 +03:00
2024-04-01 15:42:50 +03:00
let app: Application = Application::builder().application_id(APP_ID).build();
2024-04-10 20:25:02 +03:00
app.connect_activate(build_ui);
2024-03-22 15:59:42 +03:00
app.run()
}
2024-04-01 15:42:50 +03:00
2024-04-10 20:25:02 +03:00
fn hide_all_windows(app: &Application) {
for window in app.windows() {
window.set_visible(false);
}
}
fn switch_to(app: &Application, win: AppWindow) {
hide_all_windows(app);
app.window_by_id(win as u32).unwrap().set_visible(true);
}
fn build_ui(app: &Application) {
let menu: MenuScene = MenuScene::new(app); // 1
let guessing_setup: GuessingSetupScene = GuessingSetupScene::new(app); // 2
let guessing_scene: GuessingScene = GuessingScene::new(app); // 3
let memory_cards_setup: MemoryCardsSetupScene = MemoryCardsSetupScene::new(app); // 4
let memory_cards_edit: MemoryCardsEditScene = MemoryCardsEditScene::new(app); // 5
let memory_cards_game: MemoryCardsGameScene = MemoryCardsGameScene::new(app); // 6
menu.get_guessing_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app => move |_b: &Button| {
switch_to(&app, AppWindow::GuessingSetupScene);
}
),
);
menu.get_memory_cards_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app => move |_b: &Button| {
switch_to(&app, AppWindow::MemoryCardsSetupScene);
}
),
);
2024-04-12 09:33:22 +03:00
2024-04-10 20:25:02 +03:00
guessing_setup.get_start_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app, @strong guessing_scene => move |_b: &Button| {
switch_to(&app, AppWindow::GuessingScene);
guessing_scene.read_settings();
guessing_scene.init();
}
),
);
guessing_setup.get_back_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app => move |_b: &Button| {
switch_to(&app, AppWindow::MenuScene);
}
),
);
2024-04-12 09:33:22 +03:00
2024-04-11 15:19:35 +03:00
guessing_scene.get_back_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app => move |_b: &Button| {
switch_to(&app, AppWindow::GuessingSetupScene);
}
),
);
2024-04-12 09:33:22 +03:00
2024-04-10 20:25:02 +03:00
memory_cards_setup.get_edit_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app => move |_b: &Button| {
switch_to(&app, AppWindow::MemoryCardsEditScene);
}
),
);
memory_cards_setup.get_back_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app => move |_b: &Button| {
switch_to(&app, AppWindow::MenuScene);
}
),
);
memory_cards_setup.get_start_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app, @strong memory_cards_game => move |_b: &Button| {
switch_to(&app, AppWindow::MemoryCardsGameScene);
memory_cards_game.update_card_list();
}
),
);
memory_cards_edit.get_back_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app => move |_b: &Button| {
switch_to(&app, AppWindow::MemoryCardsSetupScene);
}
),
);
memory_cards_game.get_back_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app => move |_b: &Button| {
switch_to(&app, AppWindow::MemoryCardsSetupScene);
}
),
);
2024-04-11 15:19:35 +03:00
connect_close_requrest(app, menu.upcast_ref::<Window>());
connect_close_requrest(app, guessing_setup.upcast_ref::<Window>());
connect_close_requrest(app, guessing_scene.upcast_ref::<Window>());
connect_close_requrest(app, memory_cards_setup.upcast_ref::<Window>());
connect_close_requrest(app, memory_cards_edit.upcast_ref::<Window>());
connect_close_requrest(app, memory_cards_game.upcast_ref::<Window>());
2024-04-01 15:42:50 +03:00
2024-04-10 20:25:02 +03:00
menu.present();
2024-04-01 15:42:50 +03:00
}
2024-04-11 15:19:35 +03:00
fn connect_close_requrest<W: IsA<Window>>(app: &Application, w: &W) {
2024-04-12 09:33:22 +03:00
w.connect_closure(
"close-request",
false,
closure_local!(@strong app => move |_w: &Window| {
app.quit();
true
}),
);
2024-04-13 23:13:25 +03:00
}