mirror of
https://github.com/griffi-gh/hUI.git
synced 2025-04-16 04:37:21 -05:00
allocate -> add to be consistent with fonts api, minor public api changes
This commit is contained in:
parent
119b9ad077
commit
e43998419a
hui-examples/examples
hui-painter/src
hui/src
|
@ -20,7 +20,7 @@ ui_main!{
|
||||||
"Mom downloader 2000",
|
"Mom downloader 2000",
|
||||||
init: |ui| {
|
init: |ui| {
|
||||||
let font_handle = ui.add_font(include_bytes!("../assets/roboto/Roboto-Regular.ttf"));
|
let font_handle = ui.add_font(include_bytes!("../assets/roboto/Roboto-Regular.ttf"));
|
||||||
ui.push_font(font_handle);
|
ui.push_font_stack(font_handle);
|
||||||
Instant::now()
|
Instant::now()
|
||||||
},
|
},
|
||||||
run: |ui, max_size, instant| {
|
run: |ui, max_size, instant| {
|
||||||
|
|
|
@ -21,7 +21,7 @@ ui_main!(
|
||||||
"hUI: Loading screen demo",
|
"hUI: Loading screen demo",
|
||||||
init: |ui| {
|
init: |ui| {
|
||||||
let font = ui.add_font(include_bytes!("../assets/blink/Blink-ynYZ.otf"));
|
let font = ui.add_font(include_bytes!("../assets/blink/Blink-ynYZ.otf"));
|
||||||
ui.push_font(font);
|
ui.push_font_stack(font);
|
||||||
(std::time::Instant::now(),)
|
(std::time::Instant::now(),)
|
||||||
},
|
},
|
||||||
run: |ui, size, (instant,)| {
|
run: |ui, size, (instant,)| {
|
||||||
|
|
|
@ -23,7 +23,7 @@ ui_main!(
|
||||||
"hUI: Transform API demo",
|
"hUI: Transform API demo",
|
||||||
init: |ui| {
|
init: |ui| {
|
||||||
let font = ui.add_font(include_bytes!("../assets/blink/Blink-ynYZ.otf"));
|
let font = ui.add_font(include_bytes!("../assets/blink/Blink-ynYZ.otf"));
|
||||||
ui.push_font(font);
|
ui.push_font_stack(font);
|
||||||
(std::time::Instant::now(),)
|
(std::time::Instant::now(),)
|
||||||
},
|
},
|
||||||
run: |ui, size, (instant,)| {
|
run: |ui, size, (instant,)| {
|
||||||
|
|
|
@ -25,7 +25,7 @@ ui_main!(
|
||||||
"hUI: vscode demo",
|
"hUI: vscode demo",
|
||||||
init: |ui| {
|
init: |ui| {
|
||||||
let handle = ui.add_font(include_bytes!("../assets/fira/FiraSans-Light.ttf"));
|
let handle = ui.add_font(include_bytes!("../assets/fira/FiraSans-Light.ttf"));
|
||||||
ui.push_font(handle);
|
ui.push_font_stack(handle);
|
||||||
Stuff {
|
Stuff {
|
||||||
vscode_icon: ui.add_image(SourceTextureFormat::RGBA8, include_bytes!("../assets/icons/visual-studio-code-icon_32x32.rgba"), 32),
|
vscode_icon: ui.add_image(SourceTextureFormat::RGBA8, include_bytes!("../assets/icons/visual-studio-code-icon_32x32.rgba"), 32),
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl FontTextureManager {
|
||||||
pub(crate) fn drop_font(&mut self, font: FontHandle, atlas: &mut TextureAtlas) {
|
pub(crate) fn drop_font(&mut self, font: FontHandle, atlas: &mut TextureAtlas) {
|
||||||
let dump = self.partition.remove(&font.0).expect("Font handle is invalid");
|
let dump = self.partition.remove(&font.0).expect("Font handle is invalid");
|
||||||
for (_, item) in dump {
|
for (_, item) in dump {
|
||||||
atlas.deallocate(item.handle);
|
atlas.remove(item.handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ impl FontTextureManager {
|
||||||
|
|
||||||
// Rasterize the font and copy the texture data
|
// Rasterize the font and copy the texture data
|
||||||
let (metrics, data) = font.rasterize_config(config);
|
let (metrics, data) = font.rasterize_config(config);
|
||||||
let handle = atlas.allocate_with_data(SourceTextureFormat::A8, &data, metrics.width);
|
let handle = atlas.add_with_data(SourceTextureFormat::A8, &data, metrics.width);
|
||||||
|
|
||||||
// Create a texture item struct and insert it into the partition
|
// Create a texture item struct and insert it into the partition
|
||||||
let itm = RasterizedGlyphInternal { handle, metrics };
|
let itm = RasterizedGlyphInternal { handle, metrics };
|
||||||
|
|
|
@ -186,7 +186,7 @@ impl TextureAtlas {
|
||||||
let mut this = Self::new_internal(size);
|
let mut this = Self::new_internal(size);
|
||||||
|
|
||||||
// HACK?: ensure 0,0 is a white pixel
|
// HACK?: ensure 0,0 is a white pixel
|
||||||
let h = this.allocate_with_data(SourceTextureFormat::A8, &[255], 1);
|
let h = this.add_with_data(SourceTextureFormat::A8, &[255], 1);
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
h.size == uvec2(1, 1) && h.id == 0,
|
h.size == uvec2(1, 1) && h.id == 0,
|
||||||
"The texture handle was not allocated correctly"
|
"The texture handle was not allocated correctly"
|
||||||
|
@ -284,7 +284,7 @@ impl TextureAtlas {
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// - If any of the dimensions of the texture are zero or exceed `i32::MAX`.
|
/// - If any of the dimensions of the texture are zero or exceed `i32::MAX`.
|
||||||
pub fn allocate(&mut self, size: UVec2) -> TextureHandle {
|
pub fn add_empty(&mut self, size: UVec2) -> TextureHandle {
|
||||||
assert_size(size);
|
assert_size(size);
|
||||||
|
|
||||||
// Check if any deallocated allocations can be reused
|
// Check if any deallocated allocations can be reused
|
||||||
|
@ -344,7 +344,7 @@ impl TextureAtlas {
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// - If the texture handle is invalid for this atlas.
|
/// - If the texture handle is invalid for this atlas.
|
||||||
pub fn deallocate(&mut self, handle: TextureHandle) {
|
pub fn remove(&mut self, handle: TextureHandle) {
|
||||||
// Remove the allocation from the active allocations
|
// Remove the allocation from the active allocations
|
||||||
let allocation = self.allocations
|
let allocation = self.allocations
|
||||||
.remove(&handle.id)
|
.remove(&handle.id)
|
||||||
|
@ -439,7 +439,7 @@ impl TextureAtlas {
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// - If any of the dimensions of the texture are zero or exceed `i32::MAX`.
|
/// - If any of the dimensions of the texture are zero or exceed `i32::MAX`.
|
||||||
/// - The length of the data array is zero or not a multiple of the stride (stride = width * bytes per pixel).
|
/// - The length of the data array is zero or not a multiple of the stride (stride = width * bytes per pixel).
|
||||||
pub fn allocate_with_data(&mut self, format: SourceTextureFormat, data: &[u8], width: usize) -> TextureHandle {
|
pub fn add_with_data(&mut self, format: SourceTextureFormat, data: &[u8], width: usize) -> TextureHandle {
|
||||||
assert!(
|
assert!(
|
||||||
!data.is_empty(),
|
!data.is_empty(),
|
||||||
"texture data must not be empty"
|
"texture data must not be empty"
|
||||||
|
@ -461,7 +461,7 @@ impl TextureAtlas {
|
||||||
assert_size(size);
|
assert_size(size);
|
||||||
|
|
||||||
// Allocate the texture
|
// Allocate the texture
|
||||||
let handle = self.allocate(size);
|
let handle = self.add_empty(size);
|
||||||
|
|
||||||
// Write the data to the texture
|
// Write the data to the texture
|
||||||
self.update(handle, format, data);
|
self.update(handle, format, data);
|
||||||
|
@ -545,16 +545,16 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_texture_atlas_allocate() {
|
fn test_texture_atlas_add_empty() {
|
||||||
let mut atlas = TextureAtlas::new_internal(uvec2(128, 128));
|
let mut atlas = TextureAtlas::new_internal(uvec2(128, 128));
|
||||||
let handle = atlas.allocate(uvec2(32, 32));
|
let handle = atlas.add_empty(uvec2(32, 32));
|
||||||
assert_eq!(handle.size, uvec2(32, 32));
|
assert_eq!(handle.size, uvec2(32, 32));
|
||||||
assert_eq!(atlas.get_uv(handle).unwrap().bottom_right, vec2(32. / 128., 32. / 128.));
|
assert_eq!(atlas.get_uv(handle).unwrap().bottom_right, vec2(32. / 128., 32. / 128.));
|
||||||
assert_eq!(atlas.allocations.len(), 1);
|
assert_eq!(atlas.allocations.len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_texture_atlas_allocate_with_data() {
|
fn test_texture_atlas_add_with_data() {
|
||||||
fn make_data(o: u8)-> Vec<u8> {
|
fn make_data(o: u8)-> Vec<u8> {
|
||||||
let mut data = vec![o; 32 * 32 * 4];
|
let mut data = vec![o; 32 * 32 * 4];
|
||||||
for y in 0..32 {
|
for y in 0..32 {
|
||||||
|
@ -570,7 +570,7 @@ mod tests {
|
||||||
let mut atlas = TextureAtlas::new_internal(uvec2(128, 128));
|
let mut atlas = TextureAtlas::new_internal(uvec2(128, 128));
|
||||||
|
|
||||||
let data = make_data(1);
|
let data = make_data(1);
|
||||||
let handle = atlas.allocate_with_data(SourceTextureFormat::RGBA8, &data, 32);
|
let handle = atlas.add_with_data(SourceTextureFormat::RGBA8, &data, 32);
|
||||||
assert_eq!(handle.size, uvec2(32, 32));
|
assert_eq!(handle.size, uvec2(32, 32));
|
||||||
assert_eq!(atlas.allocations.len(), 1);
|
assert_eq!(atlas.allocations.len(), 1);
|
||||||
let uv = atlas.get_uv(handle).unwrap();
|
let uv = atlas.get_uv(handle).unwrap();
|
||||||
|
@ -580,7 +580,7 @@ mod tests {
|
||||||
assert_eq!(uv.bottom_right, vec2(32.0 / 128.0, 32.0 / 128.0));
|
assert_eq!(uv.bottom_right, vec2(32.0 / 128.0, 32.0 / 128.0));
|
||||||
|
|
||||||
let data = make_data(2);
|
let data = make_data(2);
|
||||||
let handle = atlas.allocate_with_data(SourceTextureFormat::RGBA8, &data, 32);
|
let handle = atlas.add_with_data(SourceTextureFormat::RGBA8, &data, 32);
|
||||||
assert_eq!(handle.size, uvec2(32, 32));
|
assert_eq!(handle.size, uvec2(32, 32));
|
||||||
assert_eq!(atlas.allocations.len(), 2);
|
assert_eq!(atlas.allocations.len(), 2);
|
||||||
let uv = atlas.get_uv(handle).unwrap();
|
let uv = atlas.get_uv(handle).unwrap();
|
||||||
|
@ -633,10 +633,10 @@ mod tests {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_texture_atlas_deallocate() {
|
fn test_texture_atlas_remove() {
|
||||||
let mut atlas = TextureAtlas::new_internal(uvec2(128, 128));
|
let mut atlas = TextureAtlas::new_internal(uvec2(128, 128));
|
||||||
let handle = atlas.allocate(uvec2(32, 32));
|
let handle = atlas.add_empty(uvec2(32, 32));
|
||||||
atlas.deallocate(handle);
|
atlas.remove(handle);
|
||||||
assert_eq!(atlas.allocations.len(), 0);
|
assert_eq!(atlas.allocations.len(), 0);
|
||||||
assert_eq!(atlas.reuse_allocations.len(), 1);
|
assert_eq!(atlas.reuse_allocations.len(), 1);
|
||||||
}
|
}
|
||||||
|
@ -644,7 +644,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_texture_atlas_get_uv() {
|
fn test_texture_atlas_get_uv() {
|
||||||
let mut atlas = TextureAtlas::new_internal(uvec2(128, 128));
|
let mut atlas = TextureAtlas::new_internal(uvec2(128, 128));
|
||||||
let handle = atlas.allocate(uvec2(32, 32));
|
let handle = atlas.add_empty(uvec2(32, 32));
|
||||||
let uv = atlas.get_uv(handle).unwrap();
|
let uv = atlas.get_uv(handle).unwrap();
|
||||||
assert_eq!(uv.top_left, vec2(0.0, 0.0));
|
assert_eq!(uv.top_left, vec2(0.0, 0.0));
|
||||||
assert_eq!(uv.top_right, vec2(32.0 / 128.0, 0.0));
|
assert_eq!(uv.top_right, vec2(32.0 / 128.0, 0.0));
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use hui_painter::{
|
use hui_painter::{
|
||||||
backend::BackendData, paint::command::{PaintCommand, PaintList}, presentation::Presentatation, text::FontHandle, texture::{SourceTextureFormat, TextureHandle}, PainterInstance
|
backend::BackendData, paint::command::{PaintCommand, PaintList}, presentation::Presentatation, text::{FontHandle, FontManager}, texture::{SourceTextureFormat, TextureAtlas, TextureHandle}, PainterInstance
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
element::{MeasureContext, ProcessContext, UiElement},
|
element::{MeasureContext, ProcessContext, UiElement},
|
||||||
|
@ -49,6 +49,41 @@ impl UiInstance {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Returns a reference to the painter instance
|
||||||
|
pub fn painter(&self) -> &PainterInstance {
|
||||||
|
&self.painter
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a mutable reference to the painter instance
|
||||||
|
pub fn painter_mut(&mut self) -> &mut PainterInstance {
|
||||||
|
&mut self.painter
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a reference to the texture atlas
|
||||||
|
///
|
||||||
|
/// Shorthand for:
|
||||||
|
/// ```
|
||||||
|
/// # let mut instance = hui::UiInstance::new();
|
||||||
|
/// instance.painter_mut().textures_mut()
|
||||||
|
/// # ;
|
||||||
|
/// ```
|
||||||
|
pub fn textures_mut(&mut self) -> &mut TextureAtlas {
|
||||||
|
self.painter.textures_mut()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a reference to the font manager
|
||||||
|
///
|
||||||
|
/// Shorthand for:
|
||||||
|
/// ```
|
||||||
|
/// # let mut instance = hui::UiInstance::new();
|
||||||
|
/// instance.painter_mut().fonts_mut()
|
||||||
|
/// # ;
|
||||||
|
/// ```
|
||||||
|
pub fn fonts_mut(&mut self) -> &mut FontManager {
|
||||||
|
self.painter.fonts_mut()
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse and add a font from a raw byte slice to the UI\
|
/// Parse and add a font from a raw byte slice to the UI\
|
||||||
/// TrueType (`.ttf`/`.ttc`) and OpenType (`.otf`) fonts are supported\
|
/// TrueType (`.ttf`/`.ttc`) and OpenType (`.otf`) fonts are supported\
|
||||||
///
|
///
|
||||||
|
@ -56,7 +91,7 @@ impl UiInstance {
|
||||||
///
|
///
|
||||||
/// ## Panics:
|
/// ## Panics:
|
||||||
/// If the font data is invalid or corrupt
|
/// If the font data is invalid or corrupt
|
||||||
#[deprecated(note = "use painter.fonts_mut().add instead")]
|
#[deprecated(since = "0.1.0-alpha.7", note = "use instance.fonts_mut().add(...) instead")]
|
||||||
pub fn add_font(&mut self, font: &[u8]) -> FontHandle {
|
pub fn add_font(&mut self, font: &[u8]) -> FontHandle {
|
||||||
self.painter.fonts_mut().add(font)
|
self.painter.fonts_mut().add(font)
|
||||||
}
|
}
|
||||||
|
@ -67,10 +102,10 @@ impl UiInstance {
|
||||||
/// Returns an image handle ([`ImageHandle`])\
|
/// Returns an image handle ([`ImageHandle`])\
|
||||||
/// This handle can be used to reference the texture in draw commands\
|
/// This handle can be used to reference the texture in draw commands\
|
||||||
/// It's a light reference and can be cloned/copied freely, but will not be cleaned up even when dropped
|
/// It's a light reference and can be cloned/copied freely, but will not be cleaned up even when dropped
|
||||||
#[deprecated(note = "use painter.textures_mut().atlas_mut().allocate_with_data instead")]
|
#[deprecated(since = "0.1.0-alpha.7", note = "use instance.textures_mut().add_with_data(...) instead")]
|
||||||
pub fn add_image(&mut self, format: SourceTextureFormat, data: &[u8], width: usize) -> TextureHandle {
|
pub fn add_image(&mut self, format: SourceTextureFormat, data: &[u8], width: usize) -> TextureHandle {
|
||||||
// self.atlas().add(width, data, format)
|
// self.atlas().add(width, data, format)
|
||||||
self.painter.textures_mut().allocate_with_data(format, data, width)
|
self.painter.textures_mut().add_with_data(format, data, width)
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO better error handling
|
//TODO better error handling
|
||||||
|
@ -120,7 +155,7 @@ impl UiInstance {
|
||||||
///
|
///
|
||||||
/// This function is useful for replacing the default font, use sparingly\
|
/// This function is useful for replacing the default font, use sparingly\
|
||||||
/// (This library attempts to be stateless, however passing the font to every text element is not very practical)
|
/// (This library attempts to be stateless, however passing the font to every text element is not very practical)
|
||||||
pub fn push_font(&mut self, font: FontHandle) {
|
pub fn push_font_stack(&mut self, font: FontHandle) {
|
||||||
self.font_stack.push(font);
|
self.font_stack.push(font);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,11 +163,11 @@ impl UiInstance {
|
||||||
///
|
///
|
||||||
/// ## Panics:
|
/// ## Panics:
|
||||||
/// If the font stack is empty
|
/// If the font stack is empty
|
||||||
pub fn pop_font(&mut self) {
|
pub fn pop_font_stack(&mut self) {
|
||||||
self.font_stack.pop();
|
self.font_stack.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the current default font
|
/// Get the current default font from the font stack
|
||||||
pub fn current_font(&self) -> Option<FontHandle> {
|
pub fn current_font(&self) -> Option<FontHandle> {
|
||||||
self.font_stack.current()
|
self.font_stack.current()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue