Przeglądaj źródła

light on scene item menu

Bastien Sevajol 3 lat temu
rodzic
commit
0f12075fec
2 zmienionych plików z 58 dodań i 18 usunięć
  1. 5 5
      src/scene/main.rs
  2. 53 13
      src/ui/mod.rs

+ 5 - 5
src/scene/main.rs Wyświetl plik

@@ -329,11 +329,11 @@ impl MainState {
329 329
 
330 330
     fn generate_scene_item_menu_sprites(&mut self) -> GameResult {
331 331
         if let Some((_, scene_point)) = self.scene_item_menu {
332
-            self.ui_batch.add(
333
-                UiSpriteInfo::from_type(UiItem::SceneItemMenu)
334
-                    .as_draw_param()
335
-                    .dest(scene_point),
336
-            );
332
+            for draw_param in UiSpriteInfo::from_type(UiItem::SceneItemMenu)
333
+                .as_draw_params(&scene_point, &self.current_cursor_position)
334
+            {
335
+                self.ui_batch.add(draw_param);
336
+            }
337 337
         }
338 338
 
339 339
         Ok(())

+ 53 - 13
src/ui/mod.rs Wyświetl plik

@@ -3,14 +3,19 @@ use ggez::graphics;
3 3
 use crate::config::{UI_SPRITE_SHEET_HEIGHT, UI_SPRITE_SHEET_WIDTH};
4 4
 use crate::scene::item::SceneItem;
5 5
 use crate::ui::scene_item_menu::SceneItemMenuItem;
6
-use crate::WindowPoint;
6
+use crate::{Offset, ScenePoint, WindowPoint};
7 7
 
8 8
 pub mod scene_item_menu;
9 9
 
10
+const SCENE_ITEM_MENU_WIDTH: f32 = 71.0;
11
+const SCENE_ITEM_MENU_HEIGHT: f32 = 68.0;
12
+const SCENE_ITEM_MENU_ITEM_HEIGHT: f32 = 15.0;
13
+
10 14
 pub enum UiItem {
11 15
     SceneItemMenu,
12 16
 }
13
-
17
+// FIXME BS NOW: Transformer ça en VerticalMenu, où l'on donne les carac des item aussi
18
+// --> ce sera capable de manière generique d'eclairer le bon item et dire quel item affiché
14 19
 pub struct UiSpriteInfo {
15 20
     pub relative_start_x: f32,
16 21
     pub relative_start_y: f32,
@@ -26,23 +31,58 @@ impl UiSpriteInfo {
26 31
             UiItem::SceneItemMenu => Self {
27 32
                 relative_start_x: 0.0,
28 33
                 relative_start_y: 0.0,
29
-                relative_width: 71.0 / UI_SPRITE_SHEET_WIDTH,
30
-                relative_height: 68.0 / UI_SPRITE_SHEET_HEIGHT,
31
-                width: 71.0,
32
-                height: 68.0,
34
+                relative_width: SCENE_ITEM_MENU_WIDTH / UI_SPRITE_SHEET_WIDTH,
35
+                relative_height: SCENE_ITEM_MENU_HEIGHT / UI_SPRITE_SHEET_HEIGHT,
36
+                width: SCENE_ITEM_MENU_WIDTH,
37
+                height: SCENE_ITEM_MENU_HEIGHT,
33 38
             },
34 39
         }
35 40
     }
36 41
 
37
-    pub fn as_draw_param(&self) -> graphics::DrawParam {
38
-        graphics::DrawParam::new().src(graphics::Rect::new(
39
-            self.relative_start_x,
40
-            self.relative_start_y,
41
-            self.relative_width,
42
-            self.relative_height,
43
-        ))
42
+    pub fn as_draw_params(
43
+        &self,
44
+        scene_point: &ScenePoint,
45
+        scene_current_cursor_point: &ScenePoint,
46
+    ) -> Vec<graphics::DrawParam> {
47
+        // FIXME BS NOW: this is a generic struct ! not SceneItemMenu struct
48
+        let mut draw_params = vec![graphics::DrawParam::new()
49
+            .src(graphics::Rect::new(
50
+                self.relative_start_x,
51
+                self.relative_start_y,
52
+                self.relative_width,
53
+                self.relative_height,
54
+            ))
55
+            .dest(*scene_point)];
56
+
57
+        let relative_cursor_position: Offset = Offset::new(
58
+            scene_current_cursor_point.x - scene_point.x,
59
+            scene_current_cursor_point.y - scene_point.y,
60
+        );
61
+
62
+        // Cursor inside menu
63
+        if relative_cursor_position.x >= 0.0
64
+            && relative_cursor_position.x <= SCENE_ITEM_MENU_WIDTH
65
+            && relative_cursor_position.y >= 0.0
66
+            && relative_cursor_position.y <= SCENE_ITEM_MENU_HEIGHT
67
+        {
68
+            let hover_item_i = (relative_cursor_position.y / SCENE_ITEM_MENU_ITEM_HEIGHT) as i32;
69
+            let source = graphics::Rect::new(
70
+                self.relative_width,
71
+                (SCENE_ITEM_MENU_ITEM_HEIGHT / UI_SPRITE_SHEET_HEIGHT) * hover_item_i as f32,
72
+                self.relative_width,
73
+                SCENE_ITEM_MENU_ITEM_HEIGHT / UI_SPRITE_SHEET_HEIGHT,
74
+            );
75
+            let destination = ScenePoint::new(
76
+                scene_point.x,
77
+                scene_point.y + (SCENE_ITEM_MENU_ITEM_HEIGHT * hover_item_i as f32),
78
+            );
79
+            draw_params.push(graphics::DrawParam::new().src(source).dest(destination));
80
+        }
81
+
82
+        draw_params
44 83
     }
45 84
 
85
+    // FIXME BS NOW: this is a generic struct ! not SceneItemMenu struct
46 86
     pub fn which_item_clicked(
47 87
         &self,
48 88
         _window_menu_point: WindowPoint,