cards/new, cards/edit and some improvements

This commit is contained in:
leca 2024-04-05 12:42:02 +03:00
parent 005da0d8c9
commit 5d65c0b93a
22 changed files with 470 additions and 38 deletions

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="MemoryCardsEditScene" parent="GtkApplicationWindow">
<property name="title">Memory cards edit</property>
<property name="height-request">1000</property>
<property name="width-request">1000</property>
<child>
<object class="GtkBox" id="content">
<property name="orientation">vertical</property>
<property name="valign">GTK_ALIGN_FILL</property>
<property name="halign">GTK_ALIGN_FILL</property>
<child>
<object class="GtkBox" id="search_bar">
<property name="orientation">horizontal</property>
<property name="valign">GTK_ALIGN_START</property>
<property name="halign">GTK_ALIGN_FILL</property>
<child>
<object class="GtkSearchEntry" id="search_entry">
<property name="placeholder-text">Find a card</property>
<property name="halign">GTK_ALIGN_FILL</property>
<property name="valign">GTK_ALIGN_FILL</property>
<property name="hexpand">true</property>
</object>
</child>
<child>
<object class="GtkButton" id="add_button">
<property name="label">+</property>
<property name="halign">GTK_ALIGN_END</property>
<property name="valign">GTK_ALIGN_FILL</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkScrolledWindow" id="cards">
<property name="halign">GTK_ALIGN_FILL</property>
<property name="valign">GTK_ALIGN_FILL</property>
<property name="vexpand">true</property>
<child>
<object class="CardEntry" id="first">
<property name="hieroglyph">ka</property>
<property name="translation">tr</property>
</object>
</child>
</object>
</child>
</object>
</child>
</template>
</interface>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="MemoryCardsSetupScene" parent="GtkApplicationWindow">
<property name="title">Memory cards setup</property>
<child>
<object class="GtkBox" id="content">
<property name="orientation">horizontal</property>
<child>
<object class="GtkButton" id="edit_button">
<property name="label">Edit cards</property>
</object>
</child>
<child>
<object class="GtkButton" id="start_button">
<property name="label">start game</property>
</object>
</child>
</object>
</child>
</template>
</interface>

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="MemoryCardsNewScene" parent="GtkApplicationWindow">
<property name="title">New memory card</property>
<property name="height-request">512</property>
<property name="width-request">512</property>
<child>
<object class="GtkBox" id="content">
<property name="orientation">vertical</property>
<property name="halign">GTK_ALIGN_CENTER</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<child>
<object class="GtkImage" id="image">
<property name="halign">GTK_ALIGN_CENTER</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="file">/home/leca/Downloads/2648 - SoyBooru.png</property>
</object>
</child>
<child>
<object class="GtkButton" id="file_choose_button">
<property name="halign">GTK_ALIGN_CENTER</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="label">Choose file</property>
</object>
</child>
<child>
<object class="GtkEntry" id="heiroglyph_entry">
<property name="halign">GTK_ALIGN_CENTER</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="placeholder-text">Hieroglyph</property>
</object>
</child>
<child>
<object class="GtkEntry" id="translation_entry">
<property name="halign">GTK_ALIGN_CENTER</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="placeholder-text">Translation</property>
</object>
</child>
<child>
<object class="GtkButton" id="done_button">
<property name="halign">GTK_ALIGN_CENTER</property>
<property name="valign">GTK_ALIGN_CENTER</property>
<property name="label">Done</property>
</object>
</child>
</object>
</child>
</template>
</interface>

View File

@ -7,12 +7,12 @@
<property name="orientation">horizontal</property>
<child>
<object class="GtkButton" id="edit_button">
<property name="label">test</property>
<property name="label">Edit cards</property>
</object>
</child>
<child>
<object class="GtkButton" id="start_button">
<property name="label">test2</property>
<property name="label">start game</property>
</object>
</child>
</object>

View File

@ -8,11 +8,17 @@
<child>
<object class="GtkButton" id="hiragana_and_katakana">
<property name="label">Practice hiragana and katakana</property>
<style>
<class name="hiragana_and_katakana" />
</style>
</object>
</child>
<child>
<object class="GtkButton" id="memory_cards">
<property name="label">Practice vocabulary with memory cards</property>
<style>
<class name="memory_cards"/>
</style>
</object>
</child>
</object>

View File

@ -3,12 +3,17 @@
<gresource prefix="/org/foxarmy/learn-hieroglyph/">
<file compressed="true" preprocess="xml-stripblanks">menu/ui.xml</file>
<file compressed="true" preprocess="xml-stripblanks">cards/edit/ui.xml</file>
<file compressed="true" preprocess="xml-stripblanks">cards/setup/ui.xml</file>
<file compressed="true" preprocess="xml-stripblanks">cards/new/ui.xml</file>
<file compressed="true" preprocess="xml-stripblanks">guessing/game/ui.xml</file>
<file compressed="true" preprocess="xml-stripblanks">guessing/setup/ui.xml</file>
<!-- <file compressed="true" preprocess="xml-stripblanks">cards/game/ui.xml</file> -->
<file compressed="true" preprocess="xml-stripblanks">cards/setup/ui.xml</file>
<file compressed="true" preprocess="xml-stripblanks">widgets/labled_switch/template.ui.xml</file>
<file compressed="true" preprocess="xml-stripblanks">widgets/card_entry/template.ui.xml</file>
</gresource>
</gresources>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="CardEntry" parent="GtkWidget">
<property name="image_hash"></property>
<property name="hieroglyph"></property>
<property name="translation"></property>
<child>
<object class="GtkPicture" id="picture">
<property name="halign">GTK_ALIGN_CENTER</property>
<property name="valign">GTK_ALIGN_CENTER</property>
</object>
</child>
<child>
<object class="GtkLabel" id="hieroglyph_label">
<property name="halign">GTK_ALIGN_START</property>
<property name="valign">GTK_ALIGN_START</property>
</object>
</child>
<child>
<object class="GtkLabel" id="translation_label">
<property name="halign">GTK_ALIGN_START</property>
<property name="valign">GTK_ALIGN_START</property>
</object>
</child>
<child>
<object class="GtkButton" id="edit_button">
<property name="halign">GTK_ALIGN_END</property>
<property name="valign">GTK_ALIGN_END</property>
</object>
</child>
</template>
</interface>

View File

@ -1,6 +1,6 @@
mod game;
mod labled_switch;
mod ui;
mod widgets;
use crate::ui::menu::MenuScene;

View File

@ -1 +1,80 @@
use std::rc::Rc;
use glib::subclass::InitializingObject;
use gtk::glib::object::ObjectExt;
use gtk::glib::{clone, closure_local};
use gtk::subclass::prelude::*;
use gtk::{glib, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window};
use gtk::{prelude::*, FileDialog};
use crate::ui::cards::new::*;
use crate::widgets::card_entry::CardEntry;
#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/cards/edit/ui.xml")]
pub struct MemoryCardsEditScene {
#[template_child]
pub search_entry: TemplateChild<SearchEntry>,
#[template_child]
pub add_button: TemplateChild<Button>,
#[template_child]
pub first: TemplateChild<CardEntry>,
#[template_child]
pub cards: TemplateChild<ScrolledWindow>,
}
#[glib::object_subclass]
impl ObjectSubclass for MemoryCardsEditScene {
const NAME: &'static str = "MemoryCardsEditScene";
type Type = super::MemoryCardsEditScene;
type ParentType = gtk::ApplicationWindow;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for MemoryCardsEditScene {
fn constructed(&self) {
self.parent_constructed();
let binding = self.obj();
self.add_button.connect_closure("clicked",
false,
closure_local!(@strong binding => move |_b: &Button| {
let new_win = Rc::new(MemoryCardsNewScene::new(&binding.application().unwrap()));
new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win => move |_| {
let path = Rc::new(String::from(""));
gtk::glib::MainContext::default().spawn_local(file_choose_dialog(Rc::clone(&new_win), Rc::clone(&path)));
println!("{}", path); // this is empty cuz it's not waiting for user
}));
new_win.present();
}));
let c: &ScrolledWindow = self.cards.as_ref();
c.set_child(glib::bitflags::__private::core::option::Option::Some(
&CardEntry::new(&"a".to_string(), &"a".to_string()),
));
}
}
async fn file_choose_dialog<W: IsA<gtk::Window>>(window: Rc<W>, mut _p: Rc<String>) {
let dialog: FileDialog = gtk::FileDialog::builder().build();
let answer = dialog.open_future(Some(&*window)).await;
let path = answer.unwrap().path().unwrap();
let path = path.as_path().to_str();
// _p = Rc::new(path.into_os_string().into_string().unwrap());
let w: &MemoryCardsNewScene = Into::<&Window>::into(window.upcast_ref()).downcast_ref().unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene
w.get_image_widget().set_file(path);
}
impl WidgetImpl for MemoryCardsEditScene {}
impl WindowImpl for MemoryCardsEditScene {}
impl ApplicationWindowImpl for MemoryCardsEditScene {}

View File

@ -1 +1,16 @@
mod imp;
use glib::Object;
use gtk::{gio, glib, Application};
glib::wrapper! {
pub struct MemoryCardsEditScene(ObjectSubclass<imp::MemoryCardsEditScene>)
@extends gtk::ApplicationWindow, gtk::Window, gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
impl MemoryCardsEditScene {
pub fn new(app: &Application) -> Self {
Object::builder().property("application", app).build()
}
}

View File

@ -1,3 +1,4 @@
pub mod edit;
pub mod game;
pub mod setup;
pub mod new;

46
src/ui/cards/new/imp.rs Normal file
View File

@ -0,0 +1,46 @@
use glib::subclass::InitializingObject;
use gtk::subclass::prelude::*;
use gtk::{glib, Button, CompositeTemplate, Entry, Image};
#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/cards/new/ui.xml")]
pub struct MemoryCardsNewScene {
#[template_child]
pub image: TemplateChild<Image>,
#[template_child]
pub file_choose_button: TemplateChild<Button>,
#[template_child]
pub heiroglyph_entry: TemplateChild<Entry>,
#[template_child]
pub translation_entry: TemplateChild<Entry>,
#[template_child]
pub done_button: TemplateChild<Button>,
}
#[glib::object_subclass]
impl ObjectSubclass for MemoryCardsNewScene {
const NAME: &'static str = "MemoryCardsNewScene";
type Type = super::MemoryCardsNewScene;
type ParentType = gtk::ApplicationWindow;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for MemoryCardsNewScene {
fn constructed(&self) {
self.parent_constructed();
}
}
impl WidgetImpl for MemoryCardsNewScene {}
impl WindowImpl for MemoryCardsNewScene {}
impl ApplicationWindowImpl for MemoryCardsNewScene {}

27
src/ui/cards/new/mod.rs Normal file
View File

@ -0,0 +1,27 @@
mod imp;
use glib::Object;
use gtk::{
gio, glib::{self, subclass::types::ObjectSubclassIsExt}, Application, Button, Image
};
glib::wrapper! {
pub struct MemoryCardsNewScene(ObjectSubclass<imp::MemoryCardsNewScene>)
@extends gtk::ApplicationWindow, gtk::Window, gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
impl MemoryCardsNewScene {
pub fn new(app: &Application) -> Self {
Object::builder().property("application", app).build()
}
pub fn get_file_choose_button(&self) -> &Button {
self.imp().file_choose_button.as_ref()
}
pub fn get_image_widget(&self) -> &Image {
self.imp().image.as_ref()
}
}

View File

@ -1,9 +1,12 @@
use crate::ui::cards::edit::*;
use glib::subclass::InitializingObject;
use gtk::glib::closure_local;
use gtk::subclass::prelude::*;
use gtk::{glib, Button, CompositeTemplate};
use gtk::{glib, prelude::*, Button, CompositeTemplate};
#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/cards/settings/ui.xml")]
#[template(resource = "/org/foxarmy/learn-hieroglyph/cards/setup/ui.xml")]
pub struct MemoryCardsSetupScene {
#[template_child]
pub edit_button: TemplateChild<Button>,
@ -29,6 +32,16 @@ impl ObjectSubclass for MemoryCardsSetupScene {
impl ObjectImpl for MemoryCardsSetupScene {
fn constructed(&self) {
self.parent_constructed();
let binding = self.obj();
self.edit_button.connect_closure("clicked",
false,
closure_local!(@strong binding => move |_b: &Button| {
let new_win: MemoryCardsEditScene = MemoryCardsEditScene::new(&binding.application().unwrap());
new_win.present();
})
);
}
}

View File

@ -1,4 +1,4 @@
use crate::labled_switch::LabledSwitch;
use crate::widgets::labled_switch::LabledSwitch;
use crate::ui::guessing::game::GuessingScene;
use crate::APP_ID;

View File

View File

@ -0,0 +1,67 @@
use std::cell::RefCell;
use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, Button, CompositeTemplate, Label, Picture};
#[derive(CompositeTemplate, Properties, Default)]
#[properties(wrapper_type = super::CardEntry)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/widgets/card_entry/template.ui.xml")]
pub struct CardEntry {
#[template_child]
pub picture: TemplateChild<Picture>,
#[template_child]
pub hieroglyph_label: TemplateChild<Label>,
#[template_child]
pub translation_label: TemplateChild<Label>,
#[template_child]
pub edit_button: TemplateChild<Button>,
#[property(get, set)]
image_hash: RefCell<String>,
#[property(get, set)]
hieroglyph: RefCell<String>,
#[property(get, set)]
translation: RefCell<String>,
}
#[glib::object_subclass]
impl ObjectSubclass for CardEntry {
const NAME: &'static str = "CardEntry";
type Type = super::CardEntry;
type ParentType = gtk::Widget;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.set_layout_manager_type::<gtk::BoxLayout>();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for CardEntry {
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")
.sync_create()
.build();
let translation_label_binding: &Label = self.translation_label.as_ref();
self.obj()
.bind_property("translation", translation_label_binding, "label")
.sync_create()
.build();
}
fn dispose(&self) {
self.dispose_template();
}
}
impl WidgetImpl for CardEntry {}

View File

@ -0,0 +1,17 @@
mod imp;
use glib::Object;
use gtk::{gio, glib};
glib::wrapper! {
pub struct CardEntry(ObjectSubclass<imp::CardEntry>)
@extends gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
impl CardEntry {
pub fn new(hieroglyph: &String, translation: &String) -> Self {
Object::builder().property("hieroglyph", hieroglyph).property("translation", translation).build()
}
}

3
src/widgets/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod labled_switch;
pub mod card_entry;
pub mod card_display;