1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use crate::minecraft::resourcepacks::{Resourcepack, ResourcepackType};
use gio::subclass::prelude::*;
use glib::{ObjectExt, ParamFlags, ParamSpec, ParamSpecString, ToValue};
use once_cell::sync::Lazy;
use std::cell::RefCell;
use std::path::PathBuf;
pub const NAME: &str = "name";
pub const PATH: &str = "path";
pub const TYPE: &str = "type";
mod imp {
use super::*;
#[derive(Debug, Default)]
pub struct GResourcepack {
pub name: RefCell<String>,
pub path: RefCell<String>,
pub _type: RefCell<String>,
}
#[glib::object_subclass]
impl ObjectSubclass for GResourcepack {
const NAME: &'static str = "GResourcepack";
type Type = super::GResourcepack;
type ParentType = glib::Object;
}
impl ObjectImpl for GResourcepack {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
vec![
ParamSpecString::new(NAME, "Name", "Name", None, ParamFlags::READWRITE),
ParamSpecString::new(PATH, "Path", "Path", None, ParamFlags::READWRITE),
ParamSpecString::new(TYPE, "Type", "Type", None, ParamFlags::READWRITE),
]
});
PROPERTIES.as_ref()
}
fn set_property(
&self,
_obj: &Self::Type,
_id: usize,
value: &glib::Value,
pspec: &glib::ParamSpec,
) {
match pspec.name() {
NAME => *self.name.borrow_mut() = value.get().unwrap(),
PATH => *self.path.borrow_mut() = value.get().unwrap(),
TYPE => *self._type.borrow_mut() = value.get().unwrap(),
prop => unimplemented!("Property {prop} not a member of GResourcepack"),
}
}
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
match pspec.name() {
NAME => self.name.borrow().to_value(),
PATH => self.path.borrow().to_value(),
TYPE => self._type.borrow().to_value(),
prop => unimplemented!("Property {prop} not a member of GResourcepack"),
}
}
}
}
glib::wrapper! {
pub struct GResourcepack(ObjectSubclass<imp::GResourcepack>);
}
impl GResourcepack {
pub fn set_name(&self, value: String) {
self.set_property(NAME, value);
}
pub fn name(&self) -> String {
self.property(NAME)
}
pub fn set_path(&self, value: PathBuf) {
self.set_property(PATH, value.to_string_lossy().to_string());
}
pub fn path(&self) -> PathBuf {
PathBuf::from(self.property::<String>(PATH))
}
pub fn set_type(&self, value: ResourcepackType) {
self.set_property(TYPE, value.to_string());
}
pub fn _type(&self) -> ResourcepackType {
let value = self.property::<String>(TYPE);
match value.as_str() {
"Resourcepack" => ResourcepackType::Resourcepack,
"Texturepack" => ResourcepackType::Texturepack,
x => unimplemented!("'{x}' is not a valid ResourcepackType"),
}
}
}
impl From<Resourcepack> for GResourcepack {
fn from(resourcepack: Resourcepack) -> Self {
glib::Object::new(&[
(NAME, &resourcepack.name),
(PATH, &resourcepack.path.to_string_lossy().to_string()),
(TYPE, &resourcepack._type.to_string()),
])
.unwrap()
}
}
impl From<GResourcepack> for Resourcepack {
fn from(gresourcepack: GResourcepack) -> Self {
Self {
name: gresourcepack.name(),
path: gresourcepack.path(),
_type: gresourcepack._type(),
}
}
}