update ui and some small changes
This commit is contained in:
parent
b77dea3cd1
commit
1ad28db03c
|
@ -0,0 +1,21 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<schemalist>
|
||||||
|
<schema id="org.foxarmy.learn-hieroglyph" path="/org/foxarmy/learn-hieroglyph/">
|
||||||
|
<key name="is-rtk-enabled" type="b">
|
||||||
|
<default>false</default>
|
||||||
|
<summary>Enable romaji to kana</summary>
|
||||||
|
</key>
|
||||||
|
<key name="is-ktr-enabled" type="b">
|
||||||
|
<default>true</default>
|
||||||
|
<summary>Enable kana to romaji</summary>
|
||||||
|
</key>
|
||||||
|
<key name="is-hiragana-enabled" type="b">
|
||||||
|
<default>true</default>
|
||||||
|
<summary>Enable hiragana</summary>
|
||||||
|
</key>
|
||||||
|
<key name="is-katakana-enabled" type="b">
|
||||||
|
<default>false</default>
|
||||||
|
<summary>Enable katakana</summary>
|
||||||
|
</key>
|
||||||
|
</schema>
|
||||||
|
</schemalist>
|
25
src/game.rs
25
src/game.rs
|
@ -18,6 +18,31 @@ pub struct Hieroglyph {
|
||||||
column: usize,
|
column: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()) { return Kanas::Romaji; }
|
||||||
|
}
|
||||||
|
for row in HIRAGANA {
|
||||||
|
if row.contains(&(self.syllable).as_str()) { return Kanas::Hiragana; }
|
||||||
|
}
|
||||||
|
for row in KATAKANA {
|
||||||
|
if row.contains(&(self.syllable).as_str()) { return Kanas::Katakana; }
|
||||||
|
}
|
||||||
|
Kanas::Romaji // /shrug
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Hieroglyph {
|
impl std::fmt::Display for Hieroglyph {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "{}", self.syllable)
|
write!(f, "{}", self.syllable)
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
use gtk::{prelude::*, Box, Label, Switch};
|
||||||
|
|
||||||
|
pub fn build(text: &str) -> (Box, Switch, Label) {
|
||||||
|
let switch: Switch = Switch::builder()
|
||||||
|
.valign(gtk::Align::Start)
|
||||||
|
.halign(gtk::Align::Center)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let label: Label = Label::builder()
|
||||||
|
.valign(gtk::Align::End)
|
||||||
|
.halign(gtk::Align::BaselineCenter)
|
||||||
|
.label(text)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let container = Box::builder()
|
||||||
|
.orientation(gtk::Orientation::Horizontal)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
container.append(&switch);
|
||||||
|
container.append(&label);
|
||||||
|
|
||||||
|
(container, switch, label)
|
||||||
|
}
|
201
src/main.rs
201
src/main.rs
|
@ -1,13 +1,15 @@
|
||||||
mod game;
|
mod game;
|
||||||
|
mod labled_switch;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use gio::Settings;
|
||||||
|
use std::cell::{Cell, RefCell};
|
||||||
use gtk::glib::clone;
|
use gtk::glib::{clone, closure_local};
|
||||||
use gtk::{prelude::*, EditableLabel, Label, Switch};
|
use gtk::{gio, prelude::*, Button, Entry, Label};
|
||||||
use gtk::{glib, Application, ApplicationWindow, Button, Box};
|
use gtk::{glib, Application, ApplicationWindow, Box};
|
||||||
use crate::game::*;
|
use crate::game::*;
|
||||||
|
use rand::prelude::*;
|
||||||
|
|
||||||
const APP_ID: &str = "org.foxarmy.learn-hieroglyphs";
|
const APP_ID: &str = "org.foxarmy.learn-hieroglyph";
|
||||||
|
|
||||||
fn main() -> glib::ExitCode {
|
fn main() -> glib::ExitCode {
|
||||||
let app = Application::builder().application_id(APP_ID).build();
|
let app = Application::builder().application_id(APP_ID).build();
|
||||||
|
@ -24,97 +26,166 @@ fn main() -> glib::ExitCode {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_menu(w: &RefCell<ApplicationWindow>) -> Box {
|
fn build_menu(w: &RefCell<ApplicationWindow>) -> Box {
|
||||||
let romaji_to_kana = Button::builder()
|
|
||||||
.label("a->あ")
|
|
||||||
.margin_top(12)
|
|
||||||
.margin_end(48)
|
|
||||||
.build();
|
|
||||||
let kana_to_romaji = Button::builder()
|
|
||||||
.label("あ->a")
|
|
||||||
.margin_top(12)
|
|
||||||
.margin_end(12)
|
|
||||||
.margin_start(48)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let hiragana_switch: Switch = Switch::builder()
|
|
||||||
.name("enable hiragana")
|
|
||||||
.valign(gtk::Align::End)
|
|
||||||
.build();
|
|
||||||
let hiragana_label: Label = Label::builder()
|
|
||||||
.label("Enable hiragana")
|
|
||||||
.margin_start(12)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let katakana_switch: Switch = Switch::builder()
|
|
||||||
.name("enable katakana")
|
|
||||||
.build();
|
|
||||||
let katakana_label: Label = Label::builder()
|
|
||||||
.label("Enable katakana")
|
|
||||||
.margin_start(12)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let menu: Box = Box::builder()
|
let menu: Box = Box::builder()
|
||||||
.orientation(gtk::Orientation::Vertical)
|
.orientation(gtk::Orientation::Vertical)
|
||||||
.halign(gtk::Align::Center)
|
.halign(gtk::Align::Center)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let kanas_buttons: Box = Box::builder()
|
let settings_container: Box = Box::builder()
|
||||||
.orientation(gtk::Orientation::Horizontal)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let kanas_settings: Box = Box::builder()
|
|
||||||
.orientation(gtk::Orientation::Vertical)
|
.orientation(gtk::Orientation::Vertical)
|
||||||
.valign(gtk::Align::Center)
|
.valign(gtk::Align::Start)
|
||||||
.halign(gtk::Align::Center)
|
.halign(gtk::Align::Start)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let hiragana_setting: Box = Box::builder()
|
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)
|
.orientation(gtk::Orientation::Horizontal)
|
||||||
.valign(gtk::Align::Center)
|
.valign(gtk::Align::Center)
|
||||||
.halign(gtk::Align::Center)
|
.halign(gtk::Align::Center)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let katakana_setting: Box = Box::builder()
|
let katakana_setting_container: Box = Box::builder()
|
||||||
.orientation(gtk::Orientation::Horizontal)
|
.orientation(gtk::Orientation::Horizontal)
|
||||||
.valign(gtk::Align::Center)
|
.valign(gtk::Align::Center)
|
||||||
.halign(gtk::Align::Center)
|
.halign(gtk::Align::Center)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
kanas_buttons.append(&romaji_to_kana);
|
let start_button: Button = Button::builder()
|
||||||
kanas_buttons.append(&kana_to_romaji);
|
.label("Start!")
|
||||||
|
.build();
|
||||||
|
|
||||||
hiragana_setting.append(&hiragana_switch);
|
let mut settings = Cell::new(Settings::new(APP_ID));
|
||||||
hiragana_setting.append(&hiragana_label);
|
|
||||||
katakana_setting.append(&katakana_switch);
|
|
||||||
katakana_setting.append(&katakana_label);
|
|
||||||
|
|
||||||
kanas_settings.append(&hiragana_setting);
|
start_button.connect_clicked(clone!( @strong w => move |_| {
|
||||||
kanas_settings.append(&katakana_setting);
|
w.borrow_mut().set_child(Some(&build_game(&w)))
|
||||||
|
|
||||||
menu.append(&kanas_buttons);
|
|
||||||
menu.append(&kanas_settings);
|
|
||||||
kana_to_romaji.connect_clicked(clone!( @strong w => move |_button: &Button| {
|
|
||||||
w.borrow_mut().set_child(Some(&build_game(&w)));
|
|
||||||
}));
|
|
||||||
romaji_to_kana.connect_clicked(clone!( @strong w => move |_button: &Button| {
|
|
||||||
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
|
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 {
|
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();
|
.build();
|
||||||
let question_label: Label = Label::builder()
|
let mut question_label: RefCell<Label> = RefCell::new(Label::builder()
|
||||||
.label("placeholder")
|
.label("placeholder")
|
||||||
.build();
|
.build());
|
||||||
let entry: EditableLabel = EditableLabel::builder()
|
let entry: Entry = Entry::builder()
|
||||||
.text("test")
|
.placeholder_text("Answer")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
game.append(&question_label);
|
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());
|
||||||
game.append(&entry);
|
game.append(&entry);
|
||||||
|
|
||||||
game
|
game
|
||||||
|
|
Loading…
Reference in New Issue