mirror of
https://github.com/SpaceManiac/SpacemanDMM.git
synced 2025-12-23 05:36:47 +00:00
Move common icon rect logic into dreammaker crate
This commit is contained in:
parent
df2a9552cc
commit
dfa7a1b25e
3 changed files with 27 additions and 27 deletions
|
|
@ -261,6 +261,28 @@ impl Metadata {
|
|||
pub fn from_str(data: &str) -> Metadata {
|
||||
parse_metadata(data)
|
||||
}
|
||||
|
||||
pub fn rect_of(&self, bitmap_width: u32, icon_state: &str, dir: Dir, frame: u32) -> Option<(u32, u32, u32, u32)> {
|
||||
if self.states.is_empty() {
|
||||
return Some((0, 0, self.width, self.height));
|
||||
}
|
||||
let state_index = match self.state_names.get(icon_state) {
|
||||
Some(&i) => i,
|
||||
None if icon_state == "" => 0,
|
||||
None => return None,
|
||||
};
|
||||
let state = &self.states[state_index];
|
||||
let icon_index = state.index_of_frame(dir, frame);
|
||||
|
||||
let icon_count = bitmap_width / self.width;
|
||||
let (icon_x, icon_y) = (icon_index % icon_count, icon_index / icon_count);
|
||||
Some((
|
||||
icon_x * self.width,
|
||||
icon_y * self.height,
|
||||
self.width,
|
||||
self.height,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl State {
|
||||
|
|
@ -284,6 +306,7 @@ impl State {
|
|||
self.offset as u32 + dir_idx
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn index_of_frame(&self, dir: Dir, frame: u32) -> u32 {
|
||||
self.index_of_dir(dir) + frame * self.dirs.len() as u32
|
||||
}
|
||||
|
|
|
|||
|
|
@ -138,24 +138,9 @@ impl IconFile {
|
|||
])
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn rect_of(&self, icon_state: &str, dir: Dir) -> Option<Rect> {
|
||||
if self.metadata.states.is_empty() {
|
||||
return Some((0, 0, self.metadata.width, self.metadata.height));
|
||||
}
|
||||
let state_index = match self.metadata.state_names.get(icon_state) {
|
||||
Some(&i) => i,
|
||||
None => return None,
|
||||
};
|
||||
let state = &self.metadata.states[state_index];
|
||||
let icon_index = state.index_of_dir(dir);
|
||||
let icon_count = self.width / self.metadata.width;
|
||||
let (icon_x, icon_y) = (icon_index % icon_count, icon_index / icon_count);
|
||||
Some((
|
||||
icon_x * self.metadata.width,
|
||||
icon_y * self.metadata.height,
|
||||
self.metadata.width,
|
||||
self.metadata.height,
|
||||
))
|
||||
self.metadata.rect_of(self.width, icon_state, dir, 0)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,17 +32,9 @@ impl IconFile {
|
|||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn rect_of(&self, icon_state: &str, dir: Dir) -> Option<Rect> {
|
||||
if self.metadata.states.is_empty() {
|
||||
return Some((0, 0, self.image.width, self.image.height))
|
||||
}
|
||||
let state_index = match self.metadata.state_names.get(icon_state) {
|
||||
Some(&i) => i,
|
||||
None if icon_state == "" => 0,
|
||||
None => return None,
|
||||
};
|
||||
let index = self.metadata.states[state_index].index_of_dir(dir);
|
||||
Some(self.rect_of_index(index))
|
||||
self.metadata.rect_of(self.image.width, icon_state, dir, 0)
|
||||
}
|
||||
|
||||
pub fn rect_of_index(&self, icon_index: u32) -> Rect {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue