add support for drawing portions of images in the ui

Former-commit-id: 98a939fcdde6ed202c884cdaae56326e3f90d61d
This commit is contained in:
Imberflur 2019-02-27 19:37:47 -05:00 committed by Imbris
parent a20517684a
commit e35e2fffc1
2 changed files with 16 additions and 12 deletions

View File

@ -10,6 +10,7 @@ use image::{
DynamicImage, DynamicImage,
GenericImageView, GenericImageView,
}; };
use vek::Vec2;
// Local // Local
use super::{ use super::{
@ -109,4 +110,9 @@ impl<P: Pipeline> Texture<P> {
.update_texture::<<ShaderFormat as gfx::format::Formatted>::Surface, ShaderFormat>(&self.tex, None, info, data) .update_texture::<<ShaderFormat as gfx::format::Formatted>::Surface, ShaderFormat>(&self.tex, None, info, data)
.map_err(|err| RenderError::TexUpdateError(err)) .map_err(|err| RenderError::TexUpdateError(err))
} }
/// Get dimensions of the represented image
pub fn get_dimensions(&self) -> Vec2<u16> {
let (w, h, ..) = self.tex.get_info().kind.get_dimensions();
Vec2::new(w, h)
}
} }

View File

@ -167,15 +167,14 @@ impl Ui {
let color = color.unwrap_or(conrod_core::color::WHITE).to_fsa(); let color = color.unwrap_or(conrod_core::color::WHITE).to_fsa();
//let (image_w, image_h) = image_map.get(&image_id).unwrap().1; // Transform the source rectangle into uv coordinates
//let (image_w, image_h) = (image_w as Scalar, image_h as Scalar); let (image_w, image_h) = self.image_map
.get(&image_id)
// Get the sides of the source rectangle as uv coordinates. .expect("Image does not exist in image map")
// .get_dimensions()
// Texture coordinates range: .map(|e| e as f64)
// - left to right: 0.0 to 1.0 .into_tuple();
// - bottom to top: 1.0 to 0.0 let (uv_l, uv_r, uv_t, uv_b) = match source_rect {
/*let (uv_l, uv_r, uv_t, uv_b) = match source_rect {
Some(src_rect) => { Some(src_rect) => {
let (l, r, b, t) = src_rect.l_r_b_t(); let (l, r, b, t) = src_rect.l_r_b_t();
((l / image_w) as f32, ((l / image_w) as f32,
@ -184,10 +183,9 @@ impl Ui {
(t / image_h) as f32) (t / image_h) as f32)
} }
None => (0.0, 1.0, 0.0, 1.0), None => (0.0, 1.0, 0.0, 1.0),
};*/ };
let (uv_l, uv_r, uv_t, uv_b) = (0.0, 1.0, 0.0, 1.0);
let (l, r, b, t) = rect.l_r_b_t();
// Convert from conrod Scalar range to GL range -1.0 to 1.0. // Convert from conrod Scalar range to GL range -1.0 to 1.0.
let (l, r, b, t) = rect.l_r_b_t();
let (l, r, b, t) = ( let (l, r, b, t) = (
(l / ui.win_w * 2.0) as f32, (l / ui.win_w * 2.0) as f32,
(r / ui.win_w * 2.0) as f32, (r / ui.win_w * 2.0) as f32,