From 9cab88b986a168ad2aedba35ac000091b829e8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 18 Aug 2020 04:12:23 +0200 Subject: [PATCH] Draw only visible options in `overlay::Menu` --- graphics/src/overlay/menu.rs | 15 ++++++++++++--- native/src/overlay/menu.rs | 4 +++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index a952f065..f42c5e3c 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -42,6 +42,7 @@ where &mut self, bounds: Rectangle, cursor_position: Point, + viewport: &Rectangle, options: &[T], hovered_option: Option, padding: u16, @@ -52,16 +53,24 @@ where use std::f32; let is_mouse_over = bounds.contains(cursor_position); + let option_height = text_size as usize + padding as usize * 2; let mut primitives = Vec::new(); - for (i, option) in options.iter().enumerate() { + let offset = viewport.y - bounds.y; + let start = (offset / option_height as f32) as usize; + let end = + ((offset + viewport.height) / option_height as f32).ceil() as usize; + + let visible_options = &options[start..end.min(options.len())]; + + for (i, option) in visible_options.iter().enumerate() { + let i = start + i; let is_selected = hovered_option == Some(i); let bounds = Rectangle { x: bounds.x, - y: bounds.y - + ((text_size as usize + padding as usize * 2) * i) as f32, + y: bounds.y + (option_height * i) as f32, width: bounds.width, height: f32::from(text_size + padding * 2), }; diff --git a/native/src/overlay/menu.rs b/native/src/overlay/menu.rs index 29e4e63d..4b392a8e 100644 --- a/native/src/overlay/menu.rs +++ b/native/src/overlay/menu.rs @@ -372,12 +372,13 @@ where _defaults: &Renderer::Defaults, layout: Layout<'_>, cursor_position: Point, - _viewport: &Rectangle, + viewport: &Rectangle, ) -> Renderer::Output { self::Renderer::draw( renderer, layout.bounds(), cursor_position, + viewport, self.options, *self.hovered_option, self.padding, @@ -423,6 +424,7 @@ pub trait Renderer: &mut self, bounds: Rectangle, cursor_position: Point, + viewport: &Rectangle, options: &[T], hovered_option: Option, padding: u16,