complete cards/game

This commit is contained in:
2024-04-07 14:08:57 +03:00
parent 24acb4ecd1
commit 857613a01d
10 changed files with 257 additions and 29 deletions

View File

@@ -0,0 +1,52 @@
use std::cell::RefCell;
use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, BoxLayout, CompositeTemplate, Entry, Picture};
#[derive(CompositeTemplate, Properties, Default)]
#[properties(wrapper_type = super::CardDisplay)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/widgets/card_display/template.ui.xml")]
pub struct CardDisplay {
#[template_child]
pub picture: TemplateChild<Picture>,
#[template_child]
pub answer_entry: TemplateChild<Entry>,
#[property(get, set)]
pub imagepath: RefCell<String>,
#[property(get, set)]
pub hieroglyph: RefCell<String>,
}
#[glib::object_subclass]
impl ObjectSubclass for CardDisplay {
const NAME: &'static str = "CardDisplay";
type Type = super::CardDisplay;
type ParentType = gtk::Widget;
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>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for CardDisplay {
fn constructed(&self) {
self.parent_constructed();
let layout_manager: BoxLayout = BoxLayout::new(gtk::Orientation::Vertical);
self.obj().set_layout_manager(Some(layout_manager));
}
fn dispose(&self) {
self.dispose_template();
}
}
impl WidgetImpl for CardDisplay {}

View File

@@ -0,0 +1,72 @@
mod imp;
use std::cell::RefCell;
use glib::Object;
use gtk::{gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Entry, Picture};
use rusqlite::Connection;
use crate::db::{get_db_path, get_images_store_path};
glib::wrapper! {
pub struct CardDisplay(ObjectSubclass<imp::CardDisplay>)
@extends gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
struct Card {
imagename: String,
hieroglyph: String,
}
impl CardDisplay {
pub fn new(image_path: &String, hieroglyph: &String) -> Self {
Object::builder()
.property("imagepath", image_path)
.property("hieroglyph", hieroglyph)
// .property("reading", reading)
.build()
}
pub fn update_file_for_picture(&self) {
let picture_binding: &Picture = self.imp().picture.as_ref();
let path: &str = &*self.imp().imagepath.borrow().clone().to_string();
picture_binding.set_file(Some(&gio::File::for_path(path)));
}
pub fn generate_card(&self) -> (&RefCell<String>, &RefCell<String>) {
let connection = Connection::open(get_db_path()).unwrap();
let mut stmt = connection.prepare("SELECT imagename, hieroglyph FROM cards ORDER BY RANDOM() LIMIT 1").unwrap();
let random_card_iter = stmt.query_map((), |row| {
Ok(
Card {
imagename: row.get(0).unwrap(),
hieroglyph: row.get(1).unwrap()
}
)
}).unwrap();
let mut random_card = None;
for i in random_card_iter {
random_card = Some(i.unwrap());
}
let random_card: Card = random_card.unwrap();
*self.imp().imagepath.borrow_mut() = get_images_store_path() + "/" + random_card.imagename.as_str();
*self.imp().hieroglyph.borrow_mut() = random_card.hieroglyph;
self.update_file_for_picture();
(&self.imp().imagepath, &self.imp().hieroglyph)
}
pub fn get_answer_entry(&self) -> &Entry {
self.imp().answer_entry.as_ref()
}
pub fn get_hieroglyph(&self) -> String {
self.imp().hieroglyph.borrow().clone()
}
}

View File

@@ -1,9 +1,7 @@
use std::cell::RefCell;
use std::sync::OnceLock;
use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::glib::subclass::Signal;
use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, Button, CompositeTemplate, Label, Picture};
@@ -44,19 +42,9 @@ impl ObjectSubclass for CardEntry {
}
#[glib::derived_properties]
impl ObjectImpl for CardEntry {
fn signals() -> &'static [Signal] {
static SIGNALS: OnceLock<Vec<Signal>> = OnceLock::new();
SIGNALS.get_or_init(|| {
vec![Signal::builder("created")
.build()]
})
}
fn constructed(&self) {
self.parent_constructed();
let hieroglyph_label_binding: &Label = self.hieroglyph_label.as_ref();
self.obj()
.bind_property("hieroglyph", hieroglyph_label_binding, "label")