Skip to content

Commit

Permalink
Curent non wip: drawing zoom image with notan.
Browse files Browse the repository at this point in the history
  • Loading branch information
B-LechCode committed Nov 4, 2024
1 parent 4d7ab70 commit 480d8b9
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 38 deletions.
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,8 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
// }
let mut bbox_tl: egui::Pos2 = Default::default();
let mut bbox_br: egui::Pos2 = Default::default();
let mut uv_center: (f64,f64) = Default::default();
let mut uv_size: (f64,f64) = Default::default();
let egui_output = plugins.egui(|ctx| {
state.toasts.show(ctx);
if let Some(id) = state.filebrowser_id.take() {
Expand Down Expand Up @@ -1090,8 +1092,7 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
&& !state.settings_enabled
&& !state.persistent_settings.zen_mode
{
(bbox_tl, bbox_br) = info_ui(ctx, state, gfx);

(bbox_tl, bbox_br, uv_size) = info_ui(ctx, state, gfx);
}

state.pointer_over_ui = ctx.is_pointer_over_area();
Expand Down Expand Up @@ -1211,7 +1212,8 @@ fn drawe(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut O
bbox_tl.y,
bbox_br.x-bbox_tl.x,
(state.cursor_relative.x, state.cursor_relative.y),
);
uv_size
);
}

// Draw a brush preview when paint mode is on
Expand Down
246 changes: 241 additions & 5 deletions src/texture_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use notan::draw::*;
use notan::egui::EguiRegisterTexture;
use notan::egui::SizedTexture;
use notan::prelude::{BlendMode, Graphics, ShaderSource, Texture, TextureFilter};

use notan::{
egui::{self, *}};
pub struct TexWrap {
texture_array: Vec<TexturePair>,
pub col_count: u32,
Expand Down Expand Up @@ -370,7 +371,6 @@ impl TexWrap {

pub fn draw_textures(
&self,

draw: &mut Draw,
translation_x: f32,
translation_y: f32,
Expand Down Expand Up @@ -403,20 +403,256 @@ impl TexWrap {
width: f32,
// xy, size
center: (f32, f32),
texture_relsz: (f64, f64)
) {
self.begin_draw(draw);
let size = self.size();
let width_tex = (texture_relsz.0*size.0 as f64) as i32;
let xy_size = (
(width_tex) as i32,
(width_tex) as i32,
);

let xy_center = (
(translation_x) as i32,
(translation_y) as i32,
); //(16384,1024);//

let sc1 = (
2.0 * xy_size.0 as f64 / width as f64,
2.0 * xy_size.1 as f64 / width as f64,
);

//coordinates of the image-view
let mut bbox_tl = egui::pos2(f32::MAX, f32::MAX);
let mut bbox_br = egui::pos2(f32::MIN, f32::MIN);

//Ui position to start at
let base_ui_curs = nalgebra::Vector2::new(translation_x as f64, translation_y as f64);
let mut curr_ui_curs = base_ui_curs;
//our start position

//Loop control variables, start end end coordinates of interest
let x_coordinate_end = (xy_center.0 + xy_size.0) as i32;
let mut y_coordinate = xy_center.1 - xy_size.1;
let y_coordinate_end = (xy_center.1 + xy_size.1) as i32;

while y_coordinate <= y_coordinate_end {
let mut y_coordinate_increment = 0; //increment for y coordinate after x loop
let mut x_coordinate = xy_center.0 - xy_size.0;
curr_ui_curs.x = base_ui_curs.x;
let mut last_display_size_y: f64 = 0.0;
while x_coordinate <= x_coordinate_end {
//get texture tile
let curr_tex_response =
self.get_texture_at_xy(x_coordinate as i32, y_coordinate as i32);

//increment coordinates by usable width/height
y_coordinate_increment = curr_tex_response.offset_height;
x_coordinate += curr_tex_response.offset_width;

//Handling last texture in a row or col
let mut curr_tex_end = nalgebra::Vector2::new(
i32::min(curr_tex_response.x_tex_right_global, x_coordinate_end),
i32::min(curr_tex_response.y_tex_bottom_global, y_coordinate_end),
);

//Handling positive coordinate overflow
if curr_tex_response.x_tex_right_global as f32 >= self.width() - 1.0f32 {
x_coordinate = x_coordinate_end + 1;
curr_tex_end.x += (x_coordinate_end - curr_tex_response.x_tex_right_global).max(0);
}

if curr_tex_response.y_tex_bottom_global as f32 >= self.height() - 1.0f32 {
y_coordinate_increment = y_coordinate_end - y_coordinate + 1;
curr_tex_end.y += (y_coordinate_end - curr_tex_response.y_tex_bottom_global).max(0);
}

//Usable tile size, depending on offsets
let tile_size = nalgebra::Vector2::new(
curr_tex_end.x
- curr_tex_response.x_offset_texture
- curr_tex_response.x_tex_left_global
+ 1,
curr_tex_end.y
- curr_tex_response.y_offset_texture
- curr_tex_response.y_tex_top_global
+ 1,
);

//Display size - tile size scaled
let display_size =
nalgebra::Vector2::new(tile_size.x as f64 / sc1.0, tile_size.y as f64 / sc1.1);

//Texture display range
let uv_start = nalgebra::Vector2::new(
curr_tex_response.x_offset_texture as f64 / curr_tex_response.texture_width as f64,
curr_tex_response.y_offset_texture as f64 / curr_tex_response.texture_height as f64,
);

let uv_end = nalgebra::Vector2::new(
(curr_tex_end.x - curr_tex_response.x_tex_left_global + 1) as f64
/ curr_tex_response.texture_width as f64,
(curr_tex_end.y - curr_tex_response.y_tex_top_global + 1) as f64
/ curr_tex_response.texture_height as f64,
);

//let tex_id2 = gfx.egui_register_texture(curr_tex_response.texture);
let draw_tl_32 = Pos2::new(curr_ui_curs.x as f32, curr_ui_curs.y as f32);
let draw_br_32 = Pos2::new(
(curr_ui_curs.x + display_size.x) as f32,
(curr_ui_curs.y + display_size.y) as f32,
);
let r_ret = egui::Rect::from_min_max(draw_tl_32, draw_br_32);


draw.image(&curr_tex_response.texture.texture)
.blend_mode(BlendMode::NORMAL)
.size(display_size.x as f32, display_size.y as f32)
.crop((curr_tex_response.x_offset_texture as f32, curr_tex_response.y_offset_texture as f32), ((tile_size.x) as f32, (tile_size.y) as f32))
// .crop((size.0 * 0.5, size.1 * 0.5), (200., 200.))
.translate(curr_ui_curs.x as f32, curr_ui_curs.y as f32);


/*egui::Image::new(curr_tex_response.texture.texture_egui)
.maintain_aspect_ratio(false)
.fit_to_exact_size(egui::Vec2::new(
display_size.x as f32,
display_size.y as f32,
))
.uv(egui::Rect::from_x_y_ranges(
uv_start.x as f32..=uv_end.x as f32,
uv_start.y as f32..=uv_end.y as f32,
))
.paint_at(ui, r_ret);*/

//Update display cursor
curr_ui_curs.x += display_size.x;
last_display_size_y = display_size.y;

//Update coordinates for preview rectangle
bbox_tl.x = bbox_tl.x.min(r_ret.left());
bbox_tl.y = bbox_tl.y.min(r_ret.top());
bbox_br.x = bbox_br.x.max(r_ret.right());
bbox_br.y = bbox_br.y.max(r_ret.bottom());
}
//Update y coordinates
y_coordinate += y_coordinate_increment;
curr_ui_curs.y += last_display_size_y;
}


























/*let size = self.size();
let width_tex = (texture_relsz.0*size.0 as f64) as i32;
let scale_disp = width/width_tex as f32;
let textures_start = (center.0 as i32-width_tex/2, center.1 as i32-width_tex/2);
//TODO: Looped render
//Loop control variables, start end end coordinates of interest
let x_coordinate_end = (textures_start.0 + width_tex) as i32;
let mut y_coordinate = textures_start.1 as i32;
let y_coordinate_end = (textures_start.1 + width_tex) as i32;
let base_ui_curs = nalgebra::Vector2::new(translation_x, translation_y);
let mut curr_ui_curs = base_ui_curs;
while y_coordinate <= y_coordinate_end {
let mut y_coordinate_increment = 0; //increment for y coordinate after x loop
let mut x_coordinate = textures_start.0;
curr_ui_curs.x = base_ui_curs.x;
let mut last_display_size_y: f64 = 0.0;
while x_coordinate <= x_coordinate_end {
//get texture tile
let curr_tex_response =
self.get_texture_at_xy(x_coordinate as i32, y_coordinate as i32);
//increment coordinates by usable width/height
y_coordinate_increment = curr_tex_response.offset_height;
x_coordinate += curr_tex_response.offset_width;
//Handling last texture in a row or col
let mut curr_tex_end = nalgebra::Vector2::new(
i32::min(curr_tex_response.x_tex_right_global, x_coordinate_end),
i32::min(curr_tex_response.y_tex_bottom_global, y_coordinate_end),
);
//Handling positive coordinate overflow
if curr_tex_response.x_tex_right_global as f32 >= self.width() - 1.0f32 {
x_coordinate = x_coordinate_end + 1;
curr_tex_end.x += (x_coordinate_end - curr_tex_response.x_tex_right_global).max(0);
}
if curr_tex_response.y_tex_bottom_global as f32 >= self.height() - 1.0f32 {
y_coordinate_increment = y_coordinate_end - y_coordinate + 1;
curr_tex_end.y += (y_coordinate_end - curr_tex_response.y_tex_bottom_global).max(0);
}
//Usable tile size, depending on offsets
let tile_size = nalgebra::Vector2::new(
curr_tex_end.x
- curr_tex_response.x_offset_texture
- curr_tex_response.x_tex_left_global
+ 1,
curr_tex_end.y
- curr_tex_response.y_offset_texture
- curr_tex_response.y_tex_top_global
+ 1,
);
draw.image(&curr_tex_response.texture.texture)
.blend_mode(BlendMode::NORMAL)
.size(width, width)
.crop((x_coordinate as f32, y_coordinate as f32), ((x_coordinate-curr_tex_end.x) as f32, (y_coordinate-curr_tex_end.y) as f32))
// .crop((size.0 * 0.5, size.1 * 0.5), (200., 200.))
.translate(translation_x as f32, translation_y as f32);
curr_ui_curs.x = (curr_tex_end.x-x_coordinate) as f32*scale_disp as f32;
last_display_size_y = (curr_tex_end.y-y_coordinate) as f64*scale_disp as f64;
}
//Update y coordinates
y_coordinate += y_coordinate_increment;
curr_ui_curs.y += last_display_size_y as f32;
}
let mut tex_idx = 0;
for _row_idx in 0..self.row_count {
/*for _row_idx in 0..self.row_count {
for _col_idx in 0..self.col_count {
draw.image(&self.texture_array[tex_idx].texture)
.blend_mode(BlendMode::NORMAL)
.size(width, width)
.crop(center, (200., 200.))
.crop(textures_start, (width_tex, width_tex))
// .crop((size.0 * 0.5, size.1 * 0.5), (200., 200.))
.translate(translation_x as f32, translation_y as f32);
tex_idx += 1;
}
}
}*/*/
self.end_draw(draw);
}

pub fn unregister_textures(&mut self, gfx: &mut Graphics) {
Expand Down
38 changes: 8 additions & 30 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,12 @@ pub fn image_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) {
}


pub fn info_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) -> (Pos2, Pos2) {
pub fn info_ui(ctx: &Context, state: &mut OculanteState, _gfx: &mut Graphics) -> (Pos2, Pos2, (f64, f64)) {
let mut color_type = ColorType::Rgba8;
let mut bbox_tl: Pos2 = Default::default();
let mut bbox_br: Pos2 = Default::default();
let mut uv_center: (f64, f64)= Default::default();
let mut uv_size: (f64, f64)= Default::default();

if let Some(img) = &state.current_image {
color_type = img.color();
Expand Down Expand Up @@ -545,7 +547,7 @@ pub fn info_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) ->
if let Some(texture) = &state.current_texture.get() {
let desired_width = PANEL_WIDTH as f64 - PANEL_WIDGET_OFFSET as f64;
let scale = (desired_width / 8.) / texture.size().0 as f64;
let uv_center = (
uv_center = (
state.cursor_relative.x as f64 / state.image_geometry.dimensions.0 as f64,
(state.cursor_relative.y as f64 / state.image_geometry.dimensions.1 as f64),
);
Expand Down Expand Up @@ -611,9 +613,8 @@ pub fn info_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) ->
});

// make sure aspect ratio is compensated for the square preview
//let ratio = texture.size().0 as f64 / texture.size().1 as f64;
//let uv_size = (scale, scale * ratio);

let ratio = texture.size().0 as f64 / texture.size().1 as f64;
uv_size = (scale, scale * ratio);
ui.add_space(10.);

let preview_rect = egui::Rect::from_min_size(ui.cursor().left_top(), egui::Vec2::splat(desired_width as f32));
Expand All @@ -623,30 +624,7 @@ pub fn info_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) ->

bbox_tl = preview_rect.left_top();
bbox_br = preview_rect.right_bottom();
/*if texture.texture_count==1 {
let texture_resonse = texture.get_texture_at_xy(0,0);
/*let preview_rect = ui
.add(
egui::Image::new(texture_resonse.texture.texture_egui)
.maintain_aspect_ratio(false)
.fit_to_exact_size(egui::Vec2::splat(desired_width as f32))
.rounding(ROUNDING)
.uv(egui::Rect::from_x_y_ranges(
(uv_center.0 - uv_size.0) as f32..=(uv_center.0 + uv_size.0) as f32,
(uv_center.1 - uv_size.1) as f32..=(uv_center.1 + uv_size.1) as f32,
)),
)
.rect;*/
bbox_tl = preview_rect.left_top();
bbox_br = preview_rect.right_bottom();
}
else {
(bbox_tl, bbox_br) = render_info_image_tiled(ui, uv_center,uv_size, desired_width, texture);
}*/


let bg_color = Color32::BLACK.linear_multiply(0.5);
let preview_rect = egui::Rect::from_min_max(bbox_tl, bbox_br);
ui.advance_cursor_after_rect(preview_rect);
Expand Down Expand Up @@ -778,7 +756,7 @@ pub fn info_ui(ctx: &Context, state: &mut OculanteState, gfx: &mut Graphics) ->

});
});
return (bbox_tl, bbox_br);
return (bbox_tl, bbox_br, uv_size);
}

fn render_info_image_tiled(
Expand Down

0 comments on commit 480d8b9

Please sign in to comment.