Skip to content

Commit

Permalink
Added presentation. (P for normal, Ctrl+P for random)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturKovacs committed Aug 28, 2018
1 parent 9e949a6 commit 7939f34
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 60 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ backtrace = "0.3"
serde = "1.0"
serde_derive = "1.0"
rmp-serde = "0.13"
rand = "0.5"

[dependencies.glutin]
version = "0.17"
Expand Down
6 changes: 2 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ fn platform_specific() {
}

#[cfg(unix)]
fn platform_specific() {
}
fn platform_specific() {}

fn main() {
platform_specific();
Expand All @@ -31,5 +30,4 @@ fn main() {
fs::copy(entry.path(), target_resource_path.join(entry.file_name())).unwrap();
}
}

}
}
8 changes: 2 additions & 6 deletions src/bottom_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,8 @@ impl<'callback_ref> BottomPanel<'callback_ref> {
});
}

let help_toggle = ui.create_toggle(
question.clone(),
Vector2::new(32f32, 4f32),
false,
|| {},
);
let help_toggle =
ui.create_toggle(question.clone(), Vector2::new(32f32, 4f32), false, || {});

let theme_toggle = ui.create_toggle(
moon_texture.clone(),
Expand Down
1 change: 0 additions & 1 deletion src/image_cache/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ pub enum LoadResult {
Failed,
}


pub struct ImageLoader {
running: Arc<AtomicBool>,
join_handles: Option<Vec<thread::JoinHandle<()>>>,
Expand Down
35 changes: 24 additions & 11 deletions src/image_cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,31 +358,48 @@ impl ImageCache {
Ok(())
}

pub fn send_load_requests(&mut self) {
use std::collections::btree_map::Entry;

pub fn prefetch_neighbors(&mut self) {
let mut index = self.current_index;

// Send as many load requests so that the estimated total will just fill the cache
let mut estimated_remaining_cap = self.remaining_capacity;

while estimated_remaining_cap > self.curr_est_size as isize {
if self.requested_images >= Self::MAX_PENDING_PREFETCH_REQUESTS {
break;
}
// Send a load request for the closest file not in the cache or outdated
index += 1;
if self.prefetch_at_index(index) {
estimated_remaining_cap -= self.curr_est_size as isize;
} else {
break;
}
}
}

pub fn prefetch_at_index(&mut self, index: usize) -> bool {
use std::collections::btree_map::Entry;

if self.requested_images >= Self::MAX_PENDING_PREFETCH_REQUESTS {
return false;
}

if self.remaining_capacity > self.curr_est_size as isize {
if let Some(file) = self.dir_files.get(index) {
let file_path = file.path();
let file_name = if let Some(file_name) = file_path.file_name() {
file_name.to_owned()
} else {
continue;
return false;
};
match self.texture_cache.entry(file_name) {
Entry::Vacant(entry) => {
if is_file_supported(file_path.as_ref()) {
entry.insert(CachedTexture::LoadRequested);
self.loader.send_load_request(file_path);
estimated_remaining_cap -= self.curr_est_size as isize;
self.requested_images += 1;
return true;
}
}
Entry::Occupied(entry) => {
Expand All @@ -391,19 +408,15 @@ impl ImageCache {
!= file.metadata().unwrap().modified().unwrap()
{
self.loader.send_load_request(file_path);
estimated_remaining_cap -= self.curr_est_size as isize;
self.requested_images += 1;
}
}
return true;
}
}
if self.requested_images >= Self::MAX_PENDING_PREFETCH_REQUESTS {
break;
}
} else {
break;
}
}
false
}

fn change_directory(&mut self, dir_path: PathBuf, filename: OsString) -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern crate serde;
extern crate sys_info;
#[macro_use]
extern crate serde_derive;
extern crate rand;
extern crate rmp_serde;

use std::cell::RefCell;
Expand Down
113 changes: 82 additions & 31 deletions src/picture_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use glium::{Frame, Surface};

use cgmath;
use cgmath::SquareMatrix;
use cgmath::{InnerSpace, Matrix, Matrix4, Vector2, Vector3, Vector4};
use cgmath::{Matrix4, Vector2, Vector3};

use shaders;

Expand Down Expand Up @@ -122,7 +122,10 @@ impl PicturePanel {
img_pos: Vector2::new(0.0, 0.0),
projection_transform: Matrix4::identity(),
bottom,
panel_size: LogicalSize {width: 1.0, height: 1.0},
panel_size: LogicalSize {
width: 1.0,
height: 1.0,
},

file_hover_state: FileHoverState::Idle,

Expand Down Expand Up @@ -167,29 +170,49 @@ impl PicturePanel {
VirtualKeyCode::Left | VirtualKeyCode::A => {
playback_manager.request_load(LoadRequest::LoadPrevious);
}
VirtualKeyCode::Space => {
if playback_manager.playback_state() == PlaybackState::Forward {
playback_manager.pause_playback();
let filename = playback_manager
.current_filename()
.to_str()
.unwrap()
.to_owned();
window.set_title_filename(filename.as_ref());
} else {
VirtualKeyCode::Space => match playback_manager.playback_state() {
PlaybackState::Forward => {
Self::pause_playback(window, playback_manager)
}
PlaybackState::Paused => {
playback_manager.start_playback_forward();
window.set_title_filename("PLAYING");
};
}
window.set_title_filename("Playing");
}
_ => (),
},
VirtualKeyCode::P => if input.modifiers.ctrl {
match playback_manager.playback_state() {
PlaybackState::RandomPresent => {
Self::pause_playback(window, playback_manager)
}
PlaybackState::Paused => {
playback_manager.start_random_presentation();
window.set_title_filename("Presenting In Random Order");
}
_ => (),
}
} else {
match playback_manager.playback_state() {
PlaybackState::Present => {
Self::pause_playback(window, playback_manager)
}
PlaybackState::Paused => {
playback_manager.start_presentation();
window.set_title_filename("Presenting");
}
_ => (),
}
},
VirtualKeyCode::F => {
self.fit_image_to_panel();
}
VirtualKeyCode::Q => {
let texture_width = if let Some(ref texture) = self.image_texture {
Some(texture.width())
} else {
None
};
let texture_width =
if let Some(ref texture) = self.image_texture {
Some(texture.width())
} else {
None
};
if let Some(texture_width) = texture_width {
let panel_center = Vector2::new(
self.panel_size.width as f32 * 0.5,
Expand All @@ -210,8 +233,12 @@ impl PicturePanel {
if self.image_fit {
self.fit_image_to_panel();
} else {
let prev_panel_size = Vector2::new(self.panel_size.width as f32, self.panel_size.height as f32);
let new_panel_size = Vector2::new(new_panel_size.width as f32, new_panel_size.height as f32);
let prev_panel_size = Vector2::new(
self.panel_size.width as f32,
self.panel_size.height as f32,
);
let new_panel_size =
Vector2::new(new_panel_size.width as f32, new_panel_size.height as f32);
let center_offset = (new_panel_size - prev_panel_size) * 0.5f32;
self.img_pos += center_offset;
}
Expand Down Expand Up @@ -262,7 +289,8 @@ impl PicturePanel {
1.0 / (delta.abs() + 1.0)
};

let new_image_display_width = (self.img_display_width as f32 * delta).max(1.0) as u32;
let new_image_display_width =
(self.img_display_width as f32 * delta).max(1.0) as u32;
let last_mouse_pos = self.last_mouse_pos;

self.zoom_image(last_mouse_pos, new_image_display_width);
Expand Down Expand Up @@ -326,15 +354,17 @@ impl PicturePanel {
let image_display_height = image_display_width * img_height_over_width;
let corner_x = (self.img_pos.x - image_display_width * 0.5).floor();
let corner_y = (self.img_pos.y - image_display_height * 0.5).floor();
let transform = Matrix4::from_nonuniform_scale(image_display_width, image_display_height, 1.0);
let transform = Matrix4::from_translation(Vector3::new(corner_x, corner_y, 0.0)) * transform;
let transform =
Matrix4::from_nonuniform_scale(image_display_width, image_display_height, 1.0);
let transform =
Matrix4::from_translation(Vector3::new(corner_x, corner_y, 0.0)) * transform;
// Projection tranform
let transform = self.projection_transform * transform;

let sampler = texture
.sampled()
.wrap_function(glium::uniforms::SamplerWrapFunction::Clamp);
let sampler = if self.get_texel_size(panel_size) >= 4f32 {
let sampler = if self.get_texel_size() >= 4f32 {
sampler.magnify_filter(glium::uniforms::MagnifySamplerFilter::Nearest)
} else {
sampler.magnify_filter(glium::uniforms::MagnifySamplerFilter::Linear)
Expand Down Expand Up @@ -367,10 +397,17 @@ impl PicturePanel {
}

fn update_projection_transform(&mut self) {
self.projection_transform = cgmath::ortho(0.0, self.panel_size.width as f32, self.panel_size.height as f32, 0.0, -1.0, 1.0);
self.projection_transform = cgmath::ortho(
0.0,
self.panel_size.width as f32,
self.panel_size.height as f32,
0.0,
-1.0,
1.0,
);
}

fn get_texel_size(&self, panel_size: LogicalSize) -> f32 {
fn get_texel_size(&self) -> f32 {
if let Some(ref image_texture) = self.image_texture {
let img_w = image_texture.width() as f32;
self.img_display_width as f32 / img_w
Expand All @@ -387,7 +424,8 @@ impl PicturePanel {
}

fn zoom_image(&mut self, anchor: Vector2<f32>, image_display_width: u32) {
self.img_pos = (image_display_width as f32 / self.img_display_width as f32) * (self.img_pos-anchor) + anchor;
self.img_pos = (image_display_width as f32 / self.img_display_width as f32)
* (self.img_pos - anchor) + anchor;
self.img_display_width = image_display_width;
}

Expand All @@ -407,11 +445,24 @@ impl PicturePanel {
} else {
None
};

if let Some(img_display_width) = img_display_width {
self.img_pos = Vector2::new(self.panel_size.width as f32 * 0.5, self.panel_size.height as f32 * 0.5);
self.img_pos = Vector2::new(
self.panel_size.width as f32 * 0.5,
self.panel_size.height as f32 * 0.5,
);
self.img_display_width = img_display_width;
self.image_fit = true;
}
}

fn pause_playback(window: &mut Window, playback_manager: &mut PlaybackManager) {
playback_manager.pause_playback();
let filename = playback_manager
.current_filename()
.to_str()
.unwrap()
.to_owned();
window.set_title_filename(filename.as_ref());
}
}
Loading

0 comments on commit 7939f34

Please sign in to comment.