no new windows
This commit is contained in:
@@ -28,6 +28,8 @@ pub struct MemoryCardsEditScene {
|
||||
pub cards_container: TemplateChild<Box>,
|
||||
#[template_child]
|
||||
pub cards_scrolled_window: TemplateChild<ScrolledWindow>,
|
||||
#[template_child]
|
||||
pub back_button: TemplateChild<Button>,
|
||||
|
||||
displaying_cards: RefCell<Vec<CardEntry>>
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
mod imp;
|
||||
use glib::Object;
|
||||
use gtk::{gio, glib, Application};
|
||||
use gtk::{gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button};
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct MemoryCardsEditScene(ObjectSubclass<imp::MemoryCardsEditScene>)
|
||||
@@ -13,4 +13,12 @@ impl MemoryCardsEditScene {
|
||||
pub fn new(app: &Application) -> Self {
|
||||
Object::builder().property("application", app).build()
|
||||
}
|
||||
|
||||
pub fn get_add_button(&self) -> &Button {
|
||||
self.imp().add_button.as_ref()
|
||||
}
|
||||
|
||||
pub fn get_back_button(&self) -> &Button {
|
||||
self.imp().back_button.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ use crate::widgets::card_display::*;
|
||||
use glib::subclass::InitializingObject;
|
||||
use gtk::glib::clone;
|
||||
use gtk::subclass::prelude::*;
|
||||
use gtk::{glib, Box, Button, CompositeTemplate, Label};
|
||||
use gtk::{prelude::*, Entry};
|
||||
use gtk::{glib, CompositeTemplate, Label, Box};
|
||||
|
||||
#[derive(CompositeTemplate, Default)]
|
||||
#[template(resource = "/org/foxarmy/learn-hieroglyph/cards/game/ui.xml")]
|
||||
@@ -17,9 +17,13 @@ pub struct MemoryCardsGameScene {
|
||||
pub stats_label: TemplateChild<Label>,
|
||||
#[template_child]
|
||||
pub content: TemplateChild<Box>,
|
||||
#[template_child]
|
||||
pub no_cards_label: TemplateChild<Label>,
|
||||
#[template_child]
|
||||
pub back_button: TemplateChild<Button>,
|
||||
|
||||
pub correct: RefCell<usize>,
|
||||
pub incorrect: RefCell<usize>
|
||||
pub incorrect: RefCell<usize>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
@@ -44,18 +48,9 @@ impl ObjectImpl for MemoryCardsGameScene {
|
||||
let card_display_binding = self.card_display.imp().obj();
|
||||
let self_binding = self.obj();
|
||||
|
||||
match card_display_binding.generate_card() {
|
||||
Some(_) => (),
|
||||
None => {
|
||||
while self.content.first_child() != None {
|
||||
self.content.remove(&self.content.first_child().unwrap());
|
||||
}
|
||||
self.content.append(&Label::builder().label("No cards found. Please, add some.").build());
|
||||
return
|
||||
}
|
||||
};
|
||||
|
||||
card_display_binding.get_answer_entry().connect_activate(clone!(@strong card_display_binding, @strong self_binding => move |e: &Entry| {
|
||||
self_binding.update_card_list();
|
||||
|
||||
card_display_binding.get_answer_entry().connect_activate(clone!(@strong card_display_binding, @strong self_binding => move |e: &Entry| {
|
||||
println!("{}", e.text());
|
||||
println!("{} = {}? -> {}", e.text(), card_display_binding.get_hieroglyph(), e.text().to_string() == *card_display_binding.get_hieroglyph());
|
||||
if e.text() == *card_display_binding.get_hieroglyph() {
|
||||
@@ -64,7 +59,7 @@ impl ObjectImpl for MemoryCardsGameScene {
|
||||
*self_binding.imp().incorrect.borrow_mut() +=1;
|
||||
}
|
||||
self_binding.update_stats();
|
||||
card_display_binding.generate_card();
|
||||
self_binding.update_card_list();
|
||||
e.set_text("");
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
|
||||
mod imp;
|
||||
|
||||
use glib::Object;
|
||||
use gtk::{gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application};
|
||||
use gtk::{
|
||||
gio, glib::{self, subclass::types::ObjectSubclassIsExt}, prelude::*, Application, Button
|
||||
};
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct MemoryCardsGameScene(ObjectSubclass<imp::MemoryCardsGameScene>)
|
||||
@@ -21,15 +22,30 @@ impl MemoryCardsGameScene {
|
||||
// }
|
||||
|
||||
pub fn update_stats(&self) {
|
||||
self.imp().stats_label.set_text(&format!("Correct|Incorrect: {}|{}", self.imp().correct.borrow(), self.imp().incorrect.borrow()));
|
||||
self.imp().stats_label.set_text(&format!(
|
||||
"Correct|Incorrect: {}|{}",
|
||||
self.imp().correct.borrow(),
|
||||
self.imp().incorrect.borrow()
|
||||
));
|
||||
}
|
||||
|
||||
// pub fn get_stats(&self) -> (usize, usize) {
|
||||
// (self.imp().correct, self.imp().incorrect)
|
||||
// }
|
||||
pub fn get_back_button(&self) -> &Button {
|
||||
self.imp().back_button.as_ref()
|
||||
}
|
||||
|
||||
// pub fn set_stats(&self, correct: usize, incorrect: usize) {
|
||||
// self.imp().correct = correct;
|
||||
// self.imp().incorrect = incorrect;
|
||||
// }
|
||||
pub fn update_card_list(&self) {
|
||||
match self.imp().card_display.generate_card() {
|
||||
Some(_) => {
|
||||
self.imp().card_display.set_visible(true);
|
||||
self.imp().stats_label.set_visible(true);
|
||||
self.imp().no_cards_label.set_visible(false);
|
||||
self.imp().card_display.generate_card();
|
||||
},
|
||||
None => {
|
||||
self.imp().card_display.set_visible(false);
|
||||
self.imp().stats_label.set_visible(false);
|
||||
self.imp().no_cards_label.set_visible(true);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ pub struct MemoryCardsSetupScene {
|
||||
pub edit_button: TemplateChild<Button>,
|
||||
#[template_child]
|
||||
pub start_button: TemplateChild<Button>,
|
||||
#[template_child]
|
||||
pub back_button: TemplateChild<Button>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
mod imp;
|
||||
|
||||
use glib::Object;
|
||||
use gtk::{gio, glib, Application};
|
||||
use gtk::{gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button};
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct MemoryCardsSetupScene(ObjectSubclass<imp::MemoryCardsSetupScene>)
|
||||
@@ -14,4 +14,16 @@ impl MemoryCardsSetupScene {
|
||||
pub fn new(app: &Application) -> Self {
|
||||
Object::builder().property("application", app).build()
|
||||
}
|
||||
|
||||
pub fn get_edit_button(&self) -> &Button {
|
||||
self.imp().edit_button.as_ref()
|
||||
}
|
||||
|
||||
pub fn get_start_button(&self) -> &Button {
|
||||
self.imp().start_button.as_ref()
|
||||
}
|
||||
|
||||
pub fn get_back_button(&self) -> &Button {
|
||||
self.imp().back_button.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
use std::cell::RefCell;
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::{game::*, APP_ID};
|
||||
use crate::game::*;
|
||||
|
||||
use glib::subclass::InitializingObject;
|
||||
use gtk::gio::Settings;
|
||||
use gtk::glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk::glib::closure_local;
|
||||
use gtk::subclass::prelude::*;
|
||||
use gtk::{glib, CompositeTemplate, Entry, Label};
|
||||
use gtk::{prelude::*, Button};
|
||||
use rand::Rng;
|
||||
|
||||
#[derive(CompositeTemplate, Default)]
|
||||
@@ -20,6 +19,17 @@ pub struct GuessingScene {
|
||||
pub answer_entry: TemplateChild<Entry>,
|
||||
#[template_child]
|
||||
pub stats_label: TemplateChild<Label>,
|
||||
#[template_child]
|
||||
pub back_button: TemplateChild<Button>,
|
||||
|
||||
pub ktr: RefCell<bool>,
|
||||
pub rtk: RefCell<bool>,
|
||||
pub hiragana: RefCell<bool>,
|
||||
pub katakana: RefCell<bool>,
|
||||
|
||||
pub question: RefCell<Hieroglyph>,
|
||||
pub exact_kana: RefCell<Kanas>,
|
||||
pub correct_answer: RefCell<Hieroglyph>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
@@ -41,83 +51,65 @@ impl ObjectImpl for GuessingScene {
|
||||
fn constructed(&self) {
|
||||
self.parent_constructed();
|
||||
|
||||
let 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());
|
||||
self.question_label.set_label(text);
|
||||
self.obj().init();
|
||||
|
||||
let stats: RefCell<(u32, u32)> = RefCell::new((0, 0));
|
||||
|
||||
let b = self.obj();
|
||||
let self_binding = b.as_ref();
|
||||
let question_label_binding = &self.question_label.get();
|
||||
let answer_entry_binding = &self.answer_entry.get();
|
||||
let stats_label_binding = &self.stats_label.get();
|
||||
|
||||
let iteration = clone!(@strong stats,
|
||||
|
||||
/*
|
||||
|
||||
"Memory management when writing a gtk-rs app can be a bit tricky."
|
||||
(c) https://gtk-rs.org/gtk4-rs/stable/latest/book/g_object_memory_management.html
|
||||
|
||||
*/
|
||||
|
||||
let iteration = closure_local!(@strong self_binding,
|
||||
@strong stats,
|
||||
@strong question_label_binding as question_label,
|
||||
@strong answer_entry_binding as answer_entry,
|
||||
@strong stats_label_binding as stats_label => move |_entry: &Entry| {
|
||||
@strong stats_label_binding as stats_label, => move |_entry: &Entry| {
|
||||
self_binding.read_settings();
|
||||
let answer: &String = &answer_entry.text().to_string();
|
||||
println!("{} <-> {}({})? = {}", answer, correct_answer.borrow().to_string(), question.borrow(), *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)};
|
||||
// //
|
||||
println!("{} <-> {}({})? = {}", answer, self_binding.imp().correct_answer.borrow(), self_binding.imp().question.borrow(), *answer == self_binding.imp().correct_answer.borrow().to_string());
|
||||
*stats.borrow_mut() = if *answer == self_binding.imp().correct_answer.borrow().to_string() { (stats.borrow().0 + 1, stats.borrow().1) } else {(stats.borrow().0, stats.borrow().1 + 1)};
|
||||
stats_label.set_label(format!("Correct|Incorrect: {}|{} ({:.2}%)", stats.borrow().0, stats.borrow().1, 100.0 * f64::from(stats.borrow().0) / f64::from(stats.borrow().0 + stats.borrow().1)).as_str());
|
||||
*question.borrow_mut() = ask(ktr, rtk, hiragana, katakana);
|
||||
*exact_kana.borrow_mut() =
|
||||
if hiragana && katakana {
|
||||
*self_binding.imp().question.borrow_mut() = ask(
|
||||
*self_binding.imp().ktr.borrow(),
|
||||
*self_binding.imp().rtk.borrow(),
|
||||
*self_binding.imp().hiragana.borrow(),
|
||||
*self_binding.imp().katakana.borrow());
|
||||
*self_binding.imp().exact_kana.borrow_mut() =
|
||||
if *self_binding.imp().hiragana.borrow() && *self_binding.imp().katakana.borrow() {
|
||||
if rand::thread_rng().gen_bool(0.5) {Kanas::Hiragana} else {Kanas::Katakana}
|
||||
} else if hiragana {
|
||||
} else if *self_binding.imp().hiragana.borrow() {
|
||||
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() {
|
||||
*self_binding.imp().correct_answer.borrow_mut() = get_kana_pair_for_hieroglyph(
|
||||
match self_binding.imp().question.borrow().get_kana() {
|
||||
Kanas::Romaji => match self_binding.imp().exact_kana.borrow().deref() {
|
||||
Kanas::Hiragana => &Kanas::Hiragana,
|
||||
Kanas::Katakana => &Kanas::Katakana,
|
||||
_ => panic!("HOW DID YOU GET HERE?!")
|
||||
},
|
||||
_ => &Kanas::Romaji
|
||||
},
|
||||
&question.borrow()
|
||||
&self_binding.imp().question.borrow()
|
||||
);
|
||||
let binding = generate_question_text(&question.borrow(), exact_kana.borrow().deref());
|
||||
let binding = generate_question_text(&self_binding.imp().question.borrow(), &self_binding.imp().exact_kana.borrow());
|
||||
let text: &str = &(binding.as_str());
|
||||
question_label.set_label(text);
|
||||
answer_entry.set_text("");
|
||||
});
|
||||
|
||||
self.answer_entry.connect_activate(iteration);
|
||||
answer_entry_binding.connect_closure("activate", false, iteration);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
mod imp;
|
||||
|
||||
use std::ops::Deref;
|
||||
|
||||
use glib::Object;
|
||||
use gtk::{gio, glib, Application};
|
||||
use gtk::{gio::{self, prelude::*}, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button};
|
||||
use gtk::gio::Settings;
|
||||
use rand::Rng;
|
||||
use crate::{game::*, APP_ID};
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct GuessingScene(ObjectSubclass<imp::GuessingScene>)
|
||||
@@ -14,4 +19,63 @@ impl GuessingScene {
|
||||
pub fn new(app: &Application) -> Self {
|
||||
Object::builder().property("application", app).build()
|
||||
}
|
||||
|
||||
pub fn get_back_button(&self) -> &Button {
|
||||
self.imp().back_button.as_ref()
|
||||
}
|
||||
|
||||
pub fn read_settings(&self) {
|
||||
println!("reading settings");
|
||||
let settings = Settings::new(APP_ID);
|
||||
|
||||
println!("before hiragana: {}", *self.imp().hiragana.borrow());
|
||||
println!("before katakana: {}", *self.imp().katakana.borrow());
|
||||
|
||||
*self.imp().ktr.borrow_mut() = settings.boolean("is-ktr-enabled");
|
||||
*self.imp().rtk.borrow_mut() = settings.boolean("is-rtk-enabled");
|
||||
*self.imp().hiragana.borrow_mut() = settings.boolean("is-hiragana-enabled");
|
||||
*self.imp().katakana.borrow_mut() = settings.boolean("is-katakana-enabled");
|
||||
|
||||
println!("after hiragana: {}", *self.imp().hiragana.borrow());
|
||||
println!("after katakana: {}", *self.imp().katakana.borrow());
|
||||
}
|
||||
|
||||
pub fn init(&self) {
|
||||
self.read_settings();
|
||||
|
||||
let ktr = *self.imp().ktr.borrow();
|
||||
let rtk = *self.imp().rtk.borrow();
|
||||
let hiragana = *self.imp().hiragana.borrow();
|
||||
let katakana = *self.imp().katakana.borrow();
|
||||
|
||||
*self.imp().question.borrow_mut() = ask(ktr, rtk, hiragana, katakana);
|
||||
*self.imp().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
|
||||
};
|
||||
|
||||
*self.imp().correct_answer.borrow_mut() = get_kana_pair_for_hieroglyph(
|
||||
match self.imp().question.borrow().get_kana() {
|
||||
Kanas::Romaji => match self.imp().exact_kana.borrow().deref() {
|
||||
Kanas::Hiragana => &Kanas::Hiragana,
|
||||
Kanas::Katakana => &Kanas::Katakana,
|
||||
_ => panic!("HOW DID YOU GET HERE?!"),
|
||||
},
|
||||
_ => &Kanas::Romaji,
|
||||
},
|
||||
&self.imp().question.borrow(),
|
||||
);
|
||||
|
||||
let binding = generate_question_text(&self.imp().question.borrow(), self.imp().exact_kana.borrow().deref());
|
||||
let text: &str = &(binding.as_str());
|
||||
self.imp().question_label.set_label(text);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ pub struct GuessingSetupScene {
|
||||
pub rtk_enable: TemplateChild<LabledSwitch>,
|
||||
#[template_child]
|
||||
pub start_button: TemplateChild<Button>,
|
||||
#[template_child]
|
||||
pub back_button: TemplateChild<Button>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
mod imp;
|
||||
|
||||
use glib::Object;
|
||||
use gtk::{gio, glib, Application};
|
||||
use gtk::{gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button};
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct GuessingSetupScene(ObjectSubclass<imp::GuessingSetupScene>)
|
||||
@@ -14,4 +14,12 @@ impl GuessingSetupScene {
|
||||
pub fn new(app: &Application) -> Self {
|
||||
Object::builder().property("application", app).build()
|
||||
}
|
||||
|
||||
pub fn get_start_button(&self) -> &Button {
|
||||
self.imp().start_button.as_ref()
|
||||
}
|
||||
|
||||
pub fn get_back_button(&self) -> &Button {
|
||||
self.imp().back_button.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
mod imp;
|
||||
|
||||
use glib::Object;
|
||||
use gtk::{gio, glib, Application};
|
||||
use gtk::{gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button};
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct MenuScene(ObjectSubclass<imp::MenuScene>)
|
||||
@@ -14,4 +14,13 @@ impl MenuScene {
|
||||
pub fn new(app: &Application) -> Self {
|
||||
Object::builder().property("application", app).build()
|
||||
}
|
||||
|
||||
pub fn get_guessing_button(&self) -> &Button {
|
||||
self.imp().hiragana_and_katakana.as_ref()
|
||||
}
|
||||
|
||||
pub fn get_memory_cards_button(&self) -> &Button {
|
||||
self.imp().memory_cards.as_ref()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user