no new windows

This commit is contained in:
leca 2024-04-10 20:25:02 +03:00
parent d51aaed23d
commit 5fa5c5e3a7
20 changed files with 404 additions and 97 deletions

View File

@ -51,6 +51,11 @@
</child>
</object>
</child>
<child>
<object class="GtkButton" id="back_button">
<property name="label">go back</property>
</object>
</child>
</object>
</child>
</template>

View File

@ -9,11 +9,22 @@
<child>
<object class="CardDisplay" id="card_display"></object>
</child>
<child>
<object class="GtkLabel" id="no_cards_label">
<property name="label">No card found. Please, add some.</property>
<property name="visible">false</property>
</object>
</child>
<child>
<object class="GtkLabel" id="stats_label">
<property name="label">Correct|Incorrect: 0|0</property>
</object>
</child>
<child>
<object class="GtkButton" id="back_button">
<property name="label">go back</property>
</object>
</child>
</object>
</child>
</template>

View File

@ -15,6 +15,11 @@
<property name="label">start game</property>
</object>
</child>
<child>
<object class="GtkButton" id="back_button">
<property name="label">go back</property>
</object>
</child>
</object>
</child>
</template>

View File

@ -20,6 +20,11 @@
<property name="label">Correct|Incorrect: 0|0</property>
</object>
</child>
<child>
<object class="GtkButton" id="back_button">
<property name="label">go back</property>
</object>
</child>
</object>
</child>
</template>

View File

@ -30,6 +30,11 @@
<property name="label">Start!</property>
</object>
</child>
<child>
<object class="GtkButton" id="back_button">
<property name="label">go back</property>
</object>
</child>
</object>
</child>
</template>

View File

@ -1,3 +1,4 @@
use rand::Rng;
const VOWELS: usize = 5;
@ -12,6 +13,12 @@ pub enum Kanas {
Katakana,
}
impl Default for Kanas {
fn default() -> Self {
Kanas::Romaji
}
}
impl std::fmt::Display for Kanas {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
@ -25,7 +32,7 @@ impl std::fmt::Display for Kanas {
)
}
}
#[derive(Default)]
pub struct Hieroglyph {
syllable: String,
row: usize,

View File

@ -1,12 +1,31 @@
mod db;
mod game;
mod ui;
mod widgets;
mod db;
use crate::ui::menu::MenuScene;
use gtk::{gio, glib, Application};
use gtk::{prelude::*, ApplicationWindow};
use gtk::glib::closure_local;
use gtk::prelude::*;
use gtk::{gio, glib, Application, Button};
use ui::cards::edit::MemoryCardsEditScene;
use ui::cards::game::MemoryCardsGameScene;
use ui::cards::new::MemoryCardsNewScene;
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,
// MemoryCardsNewScene = 7,
}
const APP_ID: &str = "org.foxarmy.learn-hieroglyph";
@ -17,13 +36,152 @@ fn main() -> glib::ExitCode {
let app: Application = Application::builder().application_id(APP_ID).build();
app.connect_activate(test_ui);
app.connect_activate(build_ui);
app.run()
}
fn test_ui(app: &Application) {
let window: ApplicationWindow = MenuScene::new(app).into();
window.present();
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
let _memory_cards_new: MemoryCardsNewScene = MemoryCardsNewScene::new(app); //7
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);
}
),
);
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);
}
),
);
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);
}
),
);
guessing_scene.get_back_button().connect_closure(
"clicked",
false,
closure_local!(
@strong app => move |_b: &Button| {
switch_to(&app, AppWindow::GuessingSetupScene);
}
),
);
// memory_cards_edit.get_add_button().connect_closure(
// "clicked",
// false,
// closure_local!(
// @strong app => move |_b: &Button| {
// switch_to(&app, AppWindow::MemoryCardsNewScene);
// hide_all_windows(&app);
// app.window_by_id(7).unwrap().set_visible(true); // to memory card new scene
// }
// ),
// );
menu.present();
}

View File

@ -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>>
}

View File

@ -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()
}
}

View File

@ -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,16 +48,7 @@ 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
}
};
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());
@ -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("");
}));
}

View File

@ -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);
}
};
}
}

View File

@ -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]

View File

@ -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()
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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]

View File

@ -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()
}
}

View File

@ -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()
}
}

View File

@ -30,7 +30,6 @@ impl ObjectSubclass for CardDisplay {
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.set_layout_manager_type::<gtk::BoxLayout>();
// klass.set_layout_manager_type(a);
}
fn instance_init(obj: &InitializingObject<Self>) {
@ -46,8 +45,6 @@ impl ObjectImpl for CardDisplay {
self.obj().set_layout_manager(Some(layout_manager));
self.image.set_width_request(256);
self.image.set_height_request(256);
// self.image.
// self.image.set
}
fn dispose(&self) {

View File

@ -65,7 +65,11 @@ impl CardDisplay {
self.update_file_for_image();
connection.flush_prepared_statement_cache();
connection.cache_flush().expect("Cannot flush cache");
Some((&self.imp().imagepath, &self.imp().hieroglyph))
}
pub fn get_answer_entry(&self) -> &Entry {