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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use bytes::Bytes;
use std::{fmt, rc::Rc};
use crate::engine::d2::{
asset::Asset,
display::{Graphics, SubTexture, Texture},
util::{Disposable, Value},
};
use super::{BasicAsset, TextureRoot};
#[derive(Default, Clone, Debug)]
pub struct BasicTexture<R: TextureRoot + fmt::Debug> {
// DV
pub inner: BasicAsset<BasicTexture<R>>,
pub graphics: Option<Rc<dyn Graphics>>,
// pub parent: Option<Box<dyn Texture>>,
pub root: R,
// The global position of this texture from the root
pub root_x: i32,
pub root_y: i32,
parent: Option<Box<BasicTexture<R>>>,
// The texture bounds
x: i32,
y: i32,
width: i32,
height: i32,
}
impl<R: TextureRoot + fmt::Debug> AsRef<BasicAsset<BasicTexture<R>>> for BasicTexture<R> {
fn as_ref(&self) -> &BasicAsset<BasicTexture<R>> {
&self.inner
}
}
impl<R: TextureRoot + fmt::Debug> BasicTexture<R> {
fn new(root: R, width: i32, height: i32) -> Self {
Self {
inner: BasicAsset::new(),
root,
width,
height,
graphics: None,
parent: None,
root_x: 0,
root_y: 0,
x: 0,
y: 0,
}
}
// override
fn copy_from(&self, other: BasicTexture<R>) {
// self.inner.disposed = false;
// self.inner.copy_from(other.root);
// self.width = other.width;
// self.height = other.height;
// // We don't support mutability on the position, since it invalidates the rootX/Y of child
// // subTextures. This assert should never fail with the asset reloader.
// assert!(self.root_x == other.root_x && self.root_y == other.root_y && self.x == other.x && self.y == other.y);
unimplemented!()
}
// override
fn on_disposed(&mut self) {
// Since subtextures shouldn't be able to accidently dispose their parents and siblings,
// only dispose the root if this is the top-most subtexture.
if self.parent.is_none() {
self.root.dispose();
}
}
}
impl<R: TextureRoot + fmt::Debug> SubTexture for BasicTexture<R> {
#[inline]
fn parent(&self) -> Option<Box<dyn Texture>> {
// self.parent
unimplemented!()
}
#[inline]
fn x(&self) -> i32 {
self.x
}
#[inline]
fn y(&self) -> i32 {
self.y
}
}
impl<R: TextureRoot + fmt::Debug> Texture for BasicTexture<R> {
// FIXME -> Different textures that share the same root also share the same Graphics. This
// is an unfortunate limitation imposed by Canvas2D only allowing one context per canvas. We'll
// need to document this caveat somewhere, or change this API to workaround.
fn graphics(&self) -> Box<dyn Graphics> {
self.root.graphics()
}
#[inline]
fn height(&self) -> i32 {
self.height
}
#[inline]
fn width(&self) -> i32 {
self.width
}
fn read_pixels(&self, x: i32, y: i32, width: i32, height: i32) -> Bytes {
self.root
.read_pixels(self.root_x + x, self.root_y + y, width, height)
}
// tilesHigh: i32 = 1
fn split(&self, tiles_wide: i32, tiles_high: i32) -> Vec<Rc<dyn SubTexture>> {
let mut tiles = Vec::new();
let tile_width = (self.width / tiles_wide) as i32;
let tile_height = (self.height / tiles_high) as i32;
for y in 0..tiles_high {
for x in 0..tiles_wide {
tiles.push(self.sub_texture(
x * tile_width,
y * tile_height,
tile_width,
tile_height,
));
}
}
tiles
}
fn sub_texture(&self, x: i32, y: i32, width: i32, height: i32) -> Rc<dyn SubTexture> {
// let sub: BasicTexture<R> = self.root.create_texture(width, height);
// sub.parent = self;
// sub.x = x;
// sub.y = y;
// sub.root_x = self.root_x + x;
// sub.root_y = self.root_y + y;
// sub
unimplemented!()
}
fn write_pixels(&self, pixels: Bytes, x: i32, y: i32, source_w: i32, source_h: i32) {
self.root
.write_pixels(pixels, self.root_x + x, self.root_y + y, source_w, source_h);
}
}
impl<R: TextureRoot + fmt::Debug> Asset for BasicTexture<R> {
fn reload_count(&self) -> usize {
// Delegate to the root, so that root reloads get propogated to all subtextures
// self.root.get_reload_count()
unimplemented!()
}
}
impl<R: TextureRoot + fmt::Debug> Disposable for BasicTexture<R> {
fn dispose(&self) {
// FIXME:
}
}