From 03e9c9db0ced7a6a69b9ef35b317c91338e7e665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sallai=20J=C3=B3zsef?= Date: Sat, 30 Jul 2022 15:01:22 +0300 Subject: [PATCH] make menus scrollable --- src/builtin/builtin_data/triangles.png | Bin 0 -> 130 bytes src/builtin_fs.rs | 5 +- src/engine_constants/mod.rs | 1 + src/menu/mod.rs | 89 ++++++++++++++++--------- 4 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 src/builtin/builtin_data/triangles.png diff --git a/src/builtin/builtin_data/triangles.png b/src/builtin/builtin_data/triangles.png new file mode 100644 index 0000000000000000000000000000000000000000..6130b54877aec5c8dd0453e5af7f9a19cf10bea9 GIT binary patch literal 130 zcmeAS@N?(olHy`uVBq!ia0vp^B0$W_!3HEylDNj!q#}J9BQ~NJ+F(_~_Z-4Q> zzA{l{MZ`t9=am|&50%+IH*~A~ZYuf~q-s%WyjG$rvO(K%-s}tBo7Tj5xwkaGd8%GA e@uwey0mJW00s={vCMrNf89ZJ6T-G@yGywoO*(wbH literal 0 HcmV?d00001 diff --git a/src/builtin_fs.rs b/src/builtin_fs.rs index 4fc6075..a72ca54 100644 --- a/src/builtin_fs.rs +++ b/src/builtin_fs.rs @@ -108,7 +108,10 @@ impl BuiltinFS { FSNode::File("touch.png", include_bytes!("builtin/touch.png")), FSNode::Directory( "builtin_data", - vec![FSNode::File("buttons.png", include_bytes!("builtin/builtin_data/buttons.png"))], + vec![ + FSNode::File("buttons.png", include_bytes!("builtin/builtin_data/buttons.png")), + FSNode::File("triangles.png", include_bytes!("builtin/builtin_data/triangles.png")), + ], ), FSNode::Directory( "shaders", diff --git a/src/engine_constants/mod.rs b/src/engine_constants/mod.rs index 2b7d301..15a97af 100644 --- a/src/engine_constants/mod.rs +++ b/src/engine_constants/mod.rs @@ -1503,6 +1503,7 @@ impl EngineConstants { "Stage/PrtWhite" => (256, 240), "TextBox" => (244, 144), "Title" => (320, 48), + "triangles" => (20, 5), }, textscript: TextScriptConsts { encoding: TextScriptEncoding::ShiftJIS, diff --git a/src/menu/mod.rs b/src/menu/mod.rs index b699df3..0753f9d 100644 --- a/src/menu/mod.rs +++ b/src/menu/mod.rs @@ -14,6 +14,8 @@ pub mod pause_menu; pub mod save_select_menu; pub mod settings_menu; +const MENU_MIN_PADDING: f32 = 30.0; + #[allow(dead_code)] #[derive(Clone)] pub enum MenuEntry { @@ -199,21 +201,30 @@ impl Menu { let mut rect; let mut rect2; + let selected_y = self.get_selected_entry_y() as f32; + + let mut computed_y = (self.y as f32).max(MENU_MIN_PADDING); + + if (selected_y + MENU_MIN_PADDING) > state.canvas_size.1 - MENU_MIN_PADDING { + computed_y -= (selected_y + MENU_MIN_PADDING) - (state.canvas_size.1 - MENU_MIN_PADDING) + 4.0; + } + + let mut x = self.x as f32; + let mut y = computed_y; + let mut width = self.width; + let mut height = self.height; + rect = state.constants.title.menu_left_top; - batch.add_rect(self.x as f32 - rect.width() as f32, self.y as f32 - rect.height() as f32, &rect); + batch.add_rect(self.x as f32 - rect.width() as f32, y - rect.height() as f32, &rect); rect = state.constants.title.menu_right_top; - batch.add_rect(self.x as f32 + self.width as f32, self.y as f32 - rect.height() as f32, &rect); + batch.add_rect(self.x as f32 + self.width as f32, y - rect.height() as f32, &rect); rect = state.constants.title.menu_left_bottom; - batch.add_rect(self.x as f32 - rect.width() as f32, self.y as f32 + self.height as f32, &rect); + batch.add_rect(self.x as f32 - rect.width() as f32, y + self.height as f32, &rect); rect = state.constants.title.menu_right_bottom; - batch.add_rect(self.x as f32 + self.width as f32, self.y as f32 + self.height as f32, &rect); + batch.add_rect(self.x as f32 + self.width as f32, y + self.height as f32, &rect); rect = state.constants.title.menu_top; rect2 = state.constants.title.menu_bottom; - let mut x = self.x as f32; - let mut y = self.y as f32; - let mut width = self.width; - let mut height = self.height; while width > 0 { rect.right = if width >= rect.width() { @@ -251,7 +262,7 @@ impl Menu { } height = self.height; - y = self.y as f32; + y = computed_y; while height > 0 { rect = state.constants.title.menu_middle; @@ -287,22 +298,6 @@ impl Menu { batch.draw(ctx)?; - let mut entry_y = 0; - - if !self.entries.is_empty() { - let mut sum = 0.0; - - for (id, entry) in &self.entries { - if *id == self.selected { - break; - } - - sum += entry.height(); - } - - entry_y = sum as u16; - } - if self.draw_cursor { if self.custom_cursor.get() { if let Ok(batch) = state.texture_set.get_or_load_batch(ctx, &state.constants, "MenuCursor") { @@ -311,7 +306,7 @@ impl Menu { rect.right = rect.left + 16; rect.bottom = rect.top + 16; - batch.add_rect(self.x as f32, self.y as f32 + 3.0 + entry_y as f32, &rect); + batch.add_rect(self.x as f32, computed_y + 3.0 + selected_y, &rect); batch.draw(ctx)?; } else { @@ -348,17 +343,13 @@ impl Menu { let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, menu_texture)?; - batch.add_rect( - self.x as f32, - self.y as f32 + 4.0 + entry_y as f32, - &character_rect[self.anim_num as usize], - ); + batch.add_rect(self.x as f32, computed_y + 4.0 + selected_y, &character_rect[self.anim_num as usize]); batch.draw(ctx)?; } } - y = self.y as f32 + 8.0; + y = computed_y + 8.0; for (_, entry) in &self.entries { match entry { MenuEntry::Active(name) | MenuEntry::DisabledWhite(name) => { @@ -609,6 +600,20 @@ impl Menu { y += entry.height() as f32; } + if self.height as f32 > state.canvas_size.1 && self.selected != self.entries.last().unwrap().0 { + // draw down triangle + let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, "triangles")?; + batch.add_rect(self.x as f32 + 6.0, state.canvas_size.1 - 10.0, &Rect::new_size(0, 0, 5, 5)); + batch.draw(ctx)?; + } + + if computed_y < 0.0 { + // draw up triangle + let batch = state.texture_set.get_or_load_batch(ctx, &state.constants, "triangles")?; + batch.add_rect(self.x as f32 + 6.0, 7.0, &Rect::new_size(5, 0, 5, 5)); + batch.draw(ctx)?; + } + Ok(()) } @@ -720,4 +725,24 @@ impl Menu { MenuSelectionResult::None } + + fn get_selected_entry_y(&self) -> u16 { + let mut entry_y: u16 = 0; + + if !self.entries.is_empty() { + let mut sum = 0.0; + + for (id, entry) in &self.entries { + if *id == self.selected { + break; + } + + sum += entry.height(); + } + + entry_y = sum as u16; + } + + entry_y + } }