Browse Source

normalize point type and fix errors

Bastien Sevajol 3 years ago
parent
commit
e695904137
9 changed files with 148 additions and 110 deletions
  1. 8 8
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 3 1
      src/main.rs
  4. 12 1
      src/physics/mod.rs
  5. 0 11
      src/physics/position.rs
  6. 23 3
      src/physics/util.rs
  7. 8 6
      src/scene/item.rs
  8. 87 73
      src/scene/main.rs
  9. 6 6
      src/ui/mod.rs

+ 8 - 8
Cargo.lock View File

808
 ]
808
 ]
809
 
809
 
810
 [[package]]
810
 [[package]]
811
-name = "ggez_tests"
812
-version = "0.1.0"
813
-dependencies = [
814
- "ggez",
815
- "glam",
816
-]
817
-
818
-[[package]]
819
 name = "gif"
811
 name = "gif"
820
 version = "0.11.2"
812
 version = "0.11.2"
821
 source = "registry+https://github.com/rust-lang/crates.io-index"
813
 source = "registry+https://github.com/rust-lang/crates.io-index"
1697
 checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
1689
 checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
1698
 
1690
 
1699
 [[package]]
1691
 [[package]]
1692
+name = "open_combat"
1693
+version = "0.1.0"
1694
+dependencies = [
1695
+ "ggez",
1696
+ "glam",
1697
+]
1698
+
1699
+[[package]]
1700
 name = "ordered-float"
1700
 name = "ordered-float"
1701
 version = "2.1.1"
1701
 version = "2.1.1"
1702
 source = "registry+https://github.com/rust-lang/crates.io-index"
1702
 source = "registry+https://github.com/rust-lang/crates.io-index"

+ 1 - 1
Cargo.toml View File

1
 [package]
1
 [package]
2
-name = "ggez_tests"
2
+name = "open_combat"
3
 version = "0.1.0"
3
 version = "0.1.0"
4
 authors = ["Sevajol Bastien <contact@bux.fr>"]
4
 authors = ["Sevajol Bastien <contact@bux.fr>"]
5
 edition = "2018"
5
 edition = "2018"

+ 3 - 1
src/main.rs View File

11
 mod scene;
11
 mod scene;
12
 mod ui;
12
 mod ui;
13
 
13
 
14
-// TODO: create a ScenePosition and a WindowPosition to be more explicit
15
 type Point2 = Vec2;
14
 type Point2 = Vec2;
15
+type WindowPoint = Vec2;
16
+type Offset = Vec2;
17
+type ScenePoint = Vec2;
16
 type Vector2 = Vec2;
18
 type Vector2 = Vec2;
17
 
19
 
18
 const TARGET_FPS: u32 = 60; // execute update code 60x per seconds
20
 const TARGET_FPS: u32 = 60; // execute update code 60x per seconds

+ 12 - 1
src/physics/mod.rs View File

1
-pub mod position;
2
 pub mod util;
1
 pub mod util;
3
 
2
 
4
 #[derive(Debug)]
3
 #[derive(Debug)]
10
 pub enum MetaEvent {
9
 pub enum MetaEvent {
11
     FeelExplosion,
10
     FeelExplosion,
12
 }
11
 }
12
+
13
+#[derive(Eq, PartialEq, Hash)]
14
+pub struct GridPosition {
15
+    x: i32,
16
+    y: i32,
17
+}
18
+
19
+impl GridPosition {
20
+    pub fn new(x: i32, y: i32) -> Self {
21
+        Self { x, y }
22
+    }
23
+}

+ 0 - 11
src/physics/position.rs View File

1
-#[derive(Eq, PartialEq, Hash)]
2
-pub struct GridPosition {
3
-    x: i32,
4
-    y: i32,
5
-}
6
-
7
-impl GridPosition {
8
-    pub fn new(x: i32, y: i32) -> Self {
9
-        Self { x, y }
10
-    }
11
-}

+ 23 - 3
src/physics/util.rs View File

1
-use crate::physics::position::GridPosition;
2
-use crate::{Point2, Vector2, GRID_TILE_HEIGHT, GRID_TILE_WIDTH};
1
+use crate::physics::GridPosition;
2
+use crate::{ScenePoint, Vector2, WindowPoint, GRID_TILE_HEIGHT, GRID_TILE_WIDTH};
3
 
3
 
4
 pub fn vec_from_angle(angle: f32) -> Vector2 {
4
 pub fn vec_from_angle(angle: f32) -> Vector2 {
5
     let vx = angle.sin();
5
     let vx = angle.sin();
7
     Vector2::new(vx, vy)
7
     Vector2::new(vx, vy)
8
 }
8
 }
9
 
9
 
10
-pub fn grid_position_from_position(position: &Point2) -> GridPosition {
10
+pub fn grid_position_from_scene_point(position: &ScenePoint) -> GridPosition {
11
     GridPosition::new(
11
     GridPosition::new(
12
         (position.x / GRID_TILE_WIDTH) as i32,
12
         (position.x / GRID_TILE_WIDTH) as i32,
13
         (position.y / GRID_TILE_HEIGHT) as i32,
13
         (position.y / GRID_TILE_HEIGHT) as i32,
14
     )
14
     )
15
 }
15
 }
16
+
17
+pub fn scene_point_from_window_point(
18
+    window_point: &WindowPoint,
19
+    display_offset: &WindowPoint,
20
+) -> ScenePoint {
21
+    ScenePoint::new(
22
+        window_point.x - display_offset.x,
23
+        window_point.y - display_offset.y,
24
+    )
25
+}
26
+
27
+pub fn window_point_from_scene_point(
28
+    scene_point: &ScenePoint,
29
+    display_offset: &WindowPoint,
30
+) -> WindowPoint {
31
+    WindowPoint::new(
32
+        scene_point.x + display_offset.x,
33
+        scene_point.y + display_offset.y,
34
+    )
35
+}

+ 8 - 6
src/scene/item.rs View File

1
 use ggez::graphics;
1
 use ggez::graphics;
2
 
2
 
3
 use crate::behavior::ItemBehavior;
3
 use crate::behavior::ItemBehavior;
4
-use crate::physics::position::GridPosition;
4
+use crate::physics::GridPosition;
5
 use crate::physics::{util, MetaEvent};
5
 use crate::physics::{util, MetaEvent};
6
 use crate::scene::SpriteType;
6
 use crate::scene::SpriteType;
7
-use crate::{Point2, SCENE_ITEMS_SPRITE_SHEET_HEIGHT, SCENE_ITEMS_SPRITE_SHEET_WIDTH};
7
+use crate::{
8
+    Offset, Point2, ScenePoint, SCENE_ITEMS_SPRITE_SHEET_HEIGHT, SCENE_ITEMS_SPRITE_SHEET_WIDTH,
9
+};
8
 
10
 
9
 pub struct SceneItemSpriteInfo {
11
 pub struct SceneItemSpriteInfo {
10
     pub relative_start_y: f32,
12
     pub relative_start_y: f32,
55
 
57
 
56
 pub struct SceneItem {
58
 pub struct SceneItem {
57
     pub type_: SceneItemType,
59
     pub type_: SceneItemType,
58
-    pub position: Point2,
60
+    pub position: ScenePoint,
59
     pub grid_position: GridPosition,
61
     pub grid_position: GridPosition,
60
     pub state: ItemState,
62
     pub state: ItemState,
61
     pub meta_events: Vec<MetaEvent>,
63
     pub meta_events: Vec<MetaEvent>,
63
 }
65
 }
64
 
66
 
65
 impl SceneItem {
67
 impl SceneItem {
66
-    pub fn new(type_: SceneItemType, position: Point2, state: ItemState) -> Self {
68
+    pub fn new(type_: SceneItemType, position: ScenePoint, state: ItemState) -> Self {
67
         Self {
69
         Self {
68
             type_,
70
             type_,
69
             position: position.clone(),
71
             position: position.clone(),
70
-            grid_position: util::grid_position_from_position(&position.clone()),
72
+            grid_position: util::grid_position_from_scene_point(&position.clone()),
71
             state,
73
             state,
72
             meta_events: vec![],
74
             meta_events: vec![],
73
             current_frame: 0,
75
             current_frame: 0,
96
                 sprite_info.relative_tile_height,
98
                 sprite_info.relative_tile_height,
97
             ))
99
             ))
98
             .rotation(90.0f32.to_radians())
100
             .rotation(90.0f32.to_radians())
99
-            .offset(Point2::new(0.5, 0.5))
101
+            .offset(Offset::new(0.5, 0.5))
100
     }
102
     }
101
 
103
 
102
     pub fn sprite_type(&self) -> SpriteType {
104
     pub fn sprite_type(&self) -> SpriteType {

+ 87 - 73
src/scene/main.rs View File

7
 use ggez::{event, graphics, input, Context, GameResult};
7
 use ggez::{event, graphics, input, Context, GameResult};
8
 
8
 
9
 use crate::behavior::ItemBehavior;
9
 use crate::behavior::ItemBehavior;
10
-use crate::physics::position::GridPosition;
10
+use crate::physics::util::scene_point_from_window_point;
11
+use crate::physics::util::window_point_from_scene_point;
12
+use crate::physics::GridPosition;
11
 use crate::physics::{util, MetaEvent, PhysicEvent};
13
 use crate::physics::{util, MetaEvent, PhysicEvent};
12
 use crate::scene::item::{ItemState, SceneItem, SceneItemType};
14
 use crate::scene::item::{ItemState, SceneItem, SceneItemType};
13
 use crate::ui::scene_item_menu::SceneItemMenuItem;
15
 use crate::ui::scene_item_menu::SceneItemMenuItem;
14
 use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
16
 use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
15
 use crate::{
17
 use crate::{
16
-    Point2, ANIMATE_EACH, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
17
-    DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I, META_EACH, PHYSICS_EACH,
18
-    SCENE_ITEMS_CHANGE_ERR_MSG, SPRITE_EACH, TARGET_FPS,
18
+    Offset, ScenePoint, WindowPoint, ANIMATE_EACH, DEFAULT_SELECTED_SQUARE_SIDE,
19
+    DEFAULT_SELECTED_SQUARE_SIDE_HALF, DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I,
20
+    META_EACH, PHYSICS_EACH, SCENE_ITEMS_CHANGE_ERR_MSG, SPRITE_EACH, TARGET_FPS,
19
 };
21
 };
20
 use ggez::input::keyboard::KeyCode;
22
 use ggez::input::keyboard::KeyCode;
21
 
23
 
24
     frame_i: u32,
26
     frame_i: u32,
25
 
27
 
26
     // display
28
     // display
27
-    display_offset: Point2,
29
+    display_offset: Offset,
28
     sprite_sheet_batch: graphics::spritebatch::SpriteBatch,
30
     sprite_sheet_batch: graphics::spritebatch::SpriteBatch,
29
     map_batch: graphics::spritebatch::SpriteBatch,
31
     map_batch: graphics::spritebatch::SpriteBatch,
30
     ui_batch: graphics::spritebatch::SpriteBatch,
32
     ui_batch: graphics::spritebatch::SpriteBatch,
37
     physics_events: Vec<PhysicEvent>,
39
     physics_events: Vec<PhysicEvent>,
38
 
40
 
39
     // user interactions
41
     // user interactions
40
-    left_click_down: Option<Point2>,
41
-    right_click_down: Option<Point2>,
42
-    current_cursor_position: Point2,
42
+    left_click_down: Option<WindowPoint>,
43
+    right_click_down: Option<WindowPoint>,
44
+    current_cursor_position: WindowPoint,
43
     user_events: Vec<UserEvent>,
45
     user_events: Vec<UserEvent>,
44
-    selected_scene_items: Vec<usize>,         // scene_item usize
45
-    scene_item_menu: Option<(usize, Point2)>, // scene_item usize, display_at
46
+    selected_scene_items: Vec<usize>,             // scene_item usize
47
+    scene_item_menu: Option<(usize, ScenePoint)>, // scene_item usize, display_at
46
     scene_item_prepare_order: Option<SceneItemPrepareOrder>,
48
     scene_item_prepare_order: Option<SceneItemPrepareOrder>,
47
 }
49
 }
48
 
50
 
66
 
68
 
67
                 scene_items.push(SceneItem::new(
69
                 scene_items.push(SceneItem::new(
68
                     SceneItemType::Soldier,
70
                     SceneItemType::Soldier,
69
-                    Point2::new((x as f32 * 24.0) + 100.0, (y as f32 * 24.0) + 100.0),
71
+                    ScenePoint::new((x as f32 * 24.0) + 100.0, (y as f32 * 24.0) + 100.0),
70
                     ItemState::new(current_behavior),
72
                     ItemState::new(current_behavior),
71
                 ));
73
                 ));
72
             }
74
             }
74
 
76
 
75
         let mut main_state = MainState {
77
         let mut main_state = MainState {
76
             frame_i: 0,
78
             frame_i: 0,
77
-            display_offset: Point2::new(0.0, 0.0),
79
+            display_offset: Offset::new(0.0, 0.0),
78
             sprite_sheet_batch,
80
             sprite_sheet_batch,
79
             map_batch,
81
             map_batch,
80
             ui_batch,
82
             ui_batch,
83
             physics_events: vec![],
85
             physics_events: vec![],
84
             left_click_down: None,
86
             left_click_down: None,
85
             right_click_down: None,
87
             right_click_down: None,
86
-            current_cursor_position: Point2::new(0.0, 0.0),
88
+            current_cursor_position: WindowPoint::new(0.0, 0.0),
87
             user_events: vec![],
89
             user_events: vec![],
88
             selected_scene_items: vec![],
90
             selected_scene_items: vec![],
89
             scene_item_menu: None,
91
             scene_item_menu: None,
91
         };
93
         };
92
 
94
 
93
         for (i, scene_item) in main_state.scene_items.iter().enumerate() {
95
         for (i, scene_item) in main_state.scene_items.iter().enumerate() {
94
-            let grid_position = util::grid_position_from_position(&scene_item.position);
96
+            let grid_position = util::grid_position_from_scene_point(&scene_item.position);
95
             main_state
97
             main_state
96
                 .scene_items_by_grid_position
98
                 .scene_items_by_grid_position
97
                 .entry(grid_position)
99
                 .entry(grid_position)
125
 
127
 
126
         while let Some(user_event) = self.user_events.pop() {
128
         while let Some(user_event) = self.user_events.pop() {
127
             match user_event {
129
             match user_event {
128
-                UserEvent::Click(click_position) => {
129
-                    let scene_position = Point2::new(
130
-                        click_position.x - self.display_offset.x,
131
-                        click_position.y - self.display_offset.y,
132
-                    );
130
+                UserEvent::Click(window_click_point) => {
131
+                    let scene_position =
132
+                        scene_point_from_window_point(&window_click_point, &self.display_offset);
133
                     self.selected_scene_items.drain(..);
133
                     self.selected_scene_items.drain(..);
134
                     if let Some(scene_item_usize) =
134
                     if let Some(scene_item_usize) =
135
-                        self.get_first_scene_item_for_position(&scene_position)
135
+                        self.get_first_scene_item_for_scene_point(&scene_position)
136
                     {
136
                     {
137
                         self.selected_scene_items.push(scene_item_usize);
137
                         self.selected_scene_items.push(scene_item_usize);
138
                     }
138
                     }
143
                     }
143
                     }
144
 
144
 
145
                     // FIXME BS NOW: interpreter sur quel element du menu on a click ...
145
                     // FIXME BS NOW: interpreter sur quel element du menu on a click ...
146
-                    if let Some((scene_item_usize, menu_position)) = self.scene_item_menu {
146
+                    if let Some((scene_item_usize, scene_menu_point)) = self.scene_item_menu {
147
+                        let window_menu_point =
148
+                            window_point_from_scene_point(&scene_menu_point, &self.display_offset);
147
                         let menu_sprite_info = UiSpriteInfo::from_type(UiItem::SceneItemMenu);
149
                         let menu_sprite_info = UiSpriteInfo::from_type(UiItem::SceneItemMenu);
148
                         let scene_item = self
150
                         let scene_item = self
149
                             .scene_items
151
                             .scene_items
150
                             .get(scene_item_usize)
152
                             .get(scene_item_usize)
151
                             .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
153
                             .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
152
-                        if click_position.x >= menu_position.x
153
-                            && click_position.x <= menu_position.x + menu_sprite_info.width
154
-                            && click_position.y >= menu_position.y
155
-                            && click_position.y <= menu_position.y + menu_sprite_info.height
154
+                        if window_click_point.x >= window_menu_point.x
155
+                            && window_click_point.x <= window_menu_point.x + menu_sprite_info.width
156
+                            && window_click_point.y >= window_menu_point.y
157
+                            && window_click_point.y <= window_menu_point.y + menu_sprite_info.height
156
                         {
158
                         {
157
                             if let Some(menu_item) = menu_sprite_info.which_item_clicked(
159
                             if let Some(menu_item) = menu_sprite_info.which_item_clicked(
158
-                                menu_position,
159
-                                click_position,
160
+                                window_menu_point,
161
+                                window_click_point,
160
                                 scene_item,
162
                                 scene_item,
161
                             ) {
163
                             ) {
162
                                 match menu_item {
164
                                 match menu_item {
172
                         }
174
                         }
173
                     };
175
                     };
174
                 }
176
                 }
175
-                UserEvent::AreaSelection(from, to) => {
176
-                    let scene_from = Point2::new(
177
-                        from.x - self.display_offset.x,
178
-                        from.y - self.display_offset.y,
179
-                    );
180
-                    let scene_to =
181
-                        Point2::new(to.x - self.display_offset.x, to.y - self.display_offset.y);
177
+                UserEvent::AreaSelection(window_from, window_to) => {
178
+                    let scene_from =
179
+                        scene_point_from_window_point(&window_from, &self.display_offset);
180
+                    let scene_to = scene_point_from_window_point(&window_to, &self.display_offset);
182
                     self.selected_scene_items.drain(..);
181
                     self.selected_scene_items.drain(..);
183
                     self.selected_scene_items
182
                     self.selected_scene_items
184
-                        .extend(self.get_scene_items_for_area(&scene_from, &scene_to));
183
+                        .extend(self.get_scene_items_for_scene_area(&scene_from, &scene_to));
185
                 }
184
                 }
186
-                UserEvent::RightClick(position) => {
187
-                    // FIXME BS NOW: il y a des probleme de position avec le offset !
185
+                UserEvent::RightClick(window_position) => {
186
+                    let scene_point =
187
+                        scene_point_from_window_point(&window_position, &self.display_offset);
188
                     if let Some(scene_item_usize) =
188
                     if let Some(scene_item_usize) =
189
-                        self.get_first_scene_item_for_position(&position)
189
+                        self.get_first_scene_item_for_scene_point(&scene_point)
190
                     {
190
                     {
191
                         if self.selected_scene_items.contains(&scene_item_usize) {
191
                         if self.selected_scene_items.contains(&scene_item_usize) {
192
                             let scene_item = self
192
                             let scene_item = self
193
                                 .scene_items
193
                                 .scene_items
194
                                 .get(scene_item_usize)
194
                                 .get(scene_item_usize)
195
                                 .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
195
                                 .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
196
-                            self.scene_item_menu =
197
-                                Some((scene_item_usize, scene_item.position.clone()))
196
+                            self.scene_item_menu = Some((scene_item_usize, scene_item.position))
198
                         }
197
                         }
199
                     }
198
                     }
200
                 }
199
                 }
211
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
210
                     // TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
212
                     scene_item.position.x += 1.0;
211
                     scene_item.position.x += 1.0;
213
                     scene_item.grid_position =
212
                     scene_item.grid_position =
214
-                        util::grid_position_from_position(&scene_item.position);
213
+                        util::grid_position_from_scene_point(&scene_item.position);
215
                 }
214
                 }
216
                 _ => {}
215
                 _ => {}
217
             }
216
             }
273
         }
272
         }
274
     }
273
     }
275
 
274
 
276
-    fn position_with_display_offset(&self, position: &Point2) -> Point2 {
277
-        Point2::new(
278
-            position.x + self.display_offset.x,
279
-            position.y + self.display_offset.y,
280
-        )
281
-    }
282
-
283
-    fn get_first_scene_item_for_position(&self, position: &Point2) -> Option<usize> {
275
+    fn get_first_scene_item_for_scene_point(&self, scene_position: &ScenePoint) -> Option<usize> {
284
         // TODO: if found multiple: select nearest
276
         // TODO: if found multiple: select nearest
285
         for (i, scene_item) in self.scene_items.iter().enumerate() {
277
         for (i, scene_item) in self.scene_items.iter().enumerate() {
286
             let sprite_info = scene_item.sprite_info();
278
             let sprite_info = scene_item.sprite_info();
287
-            if scene_item.position.x >= position.x - sprite_info.tile_width
288
-                && scene_item.position.x <= position.x + sprite_info.tile_width
289
-                && scene_item.position.y >= position.y - sprite_info.tile_height
290
-                && scene_item.position.y <= position.y + sprite_info.tile_height
279
+            if scene_item.position.x >= scene_position.x - sprite_info.tile_width
280
+                && scene_item.position.x <= scene_position.x + sprite_info.tile_width
281
+                && scene_item.position.y >= scene_position.y - sprite_info.tile_height
282
+                && scene_item.position.y <= scene_position.y + sprite_info.tile_height
291
             {
283
             {
292
                 return Some(i);
284
                 return Some(i);
293
             }
285
             }
296
         None
288
         None
297
     }
289
     }
298
 
290
 
299
-    fn get_scene_items_for_area(&self, from: &Point2, to: &Point2) -> Vec<usize> {
291
+    fn get_scene_items_for_scene_area(&self, from: &ScenePoint, to: &ScenePoint) -> Vec<usize> {
300
         let mut selection = vec![];
292
         let mut selection = vec![];
301
 
293
 
302
         for (i, scene_item) in self.scene_items.iter().enumerate() {
294
         for (i, scene_item) in self.scene_items.iter().enumerate() {
408
 
400
 
409
             scene_mesh_builder.circle(
401
             scene_mesh_builder.circle(
410
                 DrawMode::fill(),
402
                 DrawMode::fill(),
411
-                left_click_down,
403
+                window_point_from_scene_point(&left_click_down, &self.display_offset),
412
                 2.0,
404
                 2.0,
413
                 2.0,
405
                 2.0,
414
                 graphics::YELLOW,
406
                 graphics::YELLOW,
415
             )?;
407
             )?;
416
         }
408
         }
417
 
409
 
418
-        if let Some((_, position)) = self.scene_item_menu {
410
+        scene_mesh_builder.circle(
411
+            DrawMode::fill(),
412
+            scene_point_from_window_point(&self.current_cursor_position, &self.display_offset),
413
+            2.0,
414
+            2.0,
415
+            graphics::BLUE,
416
+        )?;
417
+
418
+        if let Some((_, scene_point)) = self.scene_item_menu {
419
             self.ui_batch.add(
419
             self.ui_batch.add(
420
                 UiSpriteInfo::from_type(UiItem::SceneItemMenu)
420
                 UiSpriteInfo::from_type(UiItem::SceneItemMenu)
421
                     .as_draw_param()
421
                     .as_draw_param()
422
-                    .dest(position),
422
+                    .dest(scene_point),
423
             );
423
             );
424
         }
424
         }
425
 
425
 
431
                         .get(*scene_item_usize)
431
                         .get(*scene_item_usize)
432
                         .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
432
                         .expect(SCENE_ITEMS_CHANGE_ERR_MSG);
433
                     scene_mesh_builder.line(
433
                     scene_mesh_builder.line(
434
-                        &vec![scene_item.position.clone(), self.current_cursor_position],
434
+                        &vec![
435
+                            scene_item.position.clone(),
436
+                            scene_point_from_window_point(
437
+                                &self.current_cursor_position,
438
+                                &self.display_offset,
439
+                            ),
440
+                        ],
435
                         2.0,
441
                         2.0,
436
                         graphics::WHITE,
442
                         graphics::WHITE,
437
                     )?;
443
                     )?;
442
         self.map_batch.add(
448
         self.map_batch.add(
443
             graphics::DrawParam::new()
449
             graphics::DrawParam::new()
444
                 .src(graphics::Rect::new(0.0, 0.0, 1.0, 1.0))
450
                 .src(graphics::Rect::new(0.0, 0.0, 1.0, 1.0))
445
-                .dest(Point2::new(0.0, 0.0)),
451
+                .dest(ScenePoint::new(0.0, 0.0)),
446
         );
452
         );
447
 
453
 
448
         let scene_mesh = scene_mesh_builder.build(ctx)?;
454
         let scene_mesh = scene_mesh_builder.build(ctx)?;
449
         graphics::draw(
455
         graphics::draw(
450
             ctx,
456
             ctx,
451
             &self.map_batch,
457
             &self.map_batch,
452
-            graphics::DrawParam::new()
453
-                .dest(self.position_with_display_offset(&Point2::new(0.0, 0.0))),
458
+            graphics::DrawParam::new().dest(window_point_from_scene_point(
459
+                &ScenePoint::new(0.0, 0.0),
460
+                &self.display_offset,
461
+            )),
454
         )?;
462
         )?;
455
         graphics::draw(
463
         graphics::draw(
456
             ctx,
464
             ctx,
457
             &self.sprite_sheet_batch,
465
             &self.sprite_sheet_batch,
458
-            graphics::DrawParam::new()
459
-                .dest(self.position_with_display_offset(&Point2::new(0.0, 0.0))),
466
+            graphics::DrawParam::new().dest(window_point_from_scene_point(
467
+                &ScenePoint::new(0.0, 0.0),
468
+                &self.display_offset,
469
+            )),
460
         )?;
470
         )?;
461
         graphics::draw(
471
         graphics::draw(
462
             ctx,
472
             ctx,
463
             &scene_mesh,
473
             &scene_mesh,
464
-            graphics::DrawParam::new()
465
-                .dest(self.position_with_display_offset(&Point2::new(0.0, 0.0))),
474
+            graphics::DrawParam::new().dest(window_point_from_scene_point(
475
+                &ScenePoint::new(0.0, 0.0),
476
+                &self.display_offset,
477
+            )),
466
         )?;
478
         )?;
467
         graphics::draw(
479
         graphics::draw(
468
             ctx,
480
             ctx,
469
             &self.ui_batch,
481
             &self.ui_batch,
470
-            graphics::DrawParam::new()
471
-                .dest(self.position_with_display_offset(&Point2::new(0.0, 0.0))),
482
+            graphics::DrawParam::new().dest(window_point_from_scene_point(
483
+                &ScenePoint::new(0.0, 0.0),
484
+                &self.display_offset,
485
+            )),
472
         )?;
486
         )?;
473
 
487
 
474
         self.sprite_sheet_batch.clear();
488
         self.sprite_sheet_batch.clear();
483
     fn mouse_button_down_event(&mut self, _ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
497
     fn mouse_button_down_event(&mut self, _ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
484
         match button {
498
         match button {
485
             MouseButton::Left => {
499
             MouseButton::Left => {
486
-                self.left_click_down = Some(Point2::new(x, y));
500
+                self.left_click_down = Some(WindowPoint::new(x, y));
487
             }
501
             }
488
             MouseButton::Right => {
502
             MouseButton::Right => {
489
-                self.right_click_down = Some(Point2::new(x, y));
503
+                self.right_click_down = Some(WindowPoint::new(x, y));
490
             }
504
             }
491
             MouseButton::Middle => {}
505
             MouseButton::Middle => {}
492
             MouseButton::Other(_) => {}
506
             MouseButton::Other(_) => {}
497
         match button {
511
         match button {
498
             MouseButton::Left => {
512
             MouseButton::Left => {
499
                 if let Some(left_click_down) = self.left_click_down {
513
                 if let Some(left_click_down) = self.left_click_down {
500
-                    if left_click_down == Point2::new(x, y) {
514
+                    if left_click_down == WindowPoint::new(x, y) {
501
                         self.user_events.push(UserEvent::Click(left_click_down));
515
                         self.user_events.push(UserEvent::Click(left_click_down));
502
                     } else {
516
                     } else {
503
-                        let from = Point2::new(
517
+                        let from = WindowPoint::new(
504
                             cmp::min(left_click_down.x as i32, x as i32) as f32,
518
                             cmp::min(left_click_down.x as i32, x as i32) as f32,
505
                             cmp::min(left_click_down.y as i32, y as i32) as f32,
519
                             cmp::min(left_click_down.y as i32, y as i32) as f32,
506
                         );
520
                         );
507
-                        let to = Point2::new(
521
+                        let to = WindowPoint::new(
508
                             cmp::max(left_click_down.x as i32, x as i32) as f32,
522
                             cmp::max(left_click_down.x as i32, x as i32) as f32,
509
                             cmp::max(left_click_down.y as i32, y as i32) as f32,
523
                             cmp::max(left_click_down.y as i32, y as i32) as f32,
510
                         );
524
                         );
525
     }
539
     }
526
 
540
 
527
     fn mouse_motion_event(&mut self, _ctx: &mut Context, x: f32, y: f32, _dx: f32, _dy: f32) {
541
     fn mouse_motion_event(&mut self, _ctx: &mut Context, x: f32, y: f32, _dx: f32, _dy: f32) {
528
-        self.current_cursor_position = Point2::new(x, y);
542
+        self.current_cursor_position = WindowPoint::new(x, y);
529
     }
543
     }
530
 }
544
 }

+ 6 - 6
src/ui/mod.rs View File

2
 
2
 
3
 use crate::scene::item::SceneItem;
3
 use crate::scene::item::SceneItem;
4
 use crate::ui::scene_item_menu::SceneItemMenuItem;
4
 use crate::ui::scene_item_menu::SceneItemMenuItem;
5
-use crate::{Point2, UI_SPRITE_SHEET_HEIGHT, UI_SPRITE_SHEET_WIDTH};
5
+use crate::{Point2, WindowPoint, UI_SPRITE_SHEET_HEIGHT, UI_SPRITE_SHEET_WIDTH};
6
 
6
 
7
 pub mod scene_item_menu;
7
 pub mod scene_item_menu;
8
 
8
 
44
 
44
 
45
     pub fn which_item_clicked(
45
     pub fn which_item_clicked(
46
         &self,
46
         &self,
47
-        menu_position: Point2,
48
-        click_position: Point2,
47
+        window_menu_point: WindowPoint,
48
+        window_click_point: WindowPoint,
49
         scene_item: &SceneItem,
49
         scene_item: &SceneItem,
50
     ) -> Option<SceneItemMenuItem> {
50
     ) -> Option<SceneItemMenuItem> {
51
         Some(SceneItemMenuItem::Move)
51
         Some(SceneItemMenuItem::Move)
54
 
54
 
55
 #[derive(Debug)]
55
 #[derive(Debug)]
56
 pub enum UserEvent {
56
 pub enum UserEvent {
57
-    Click(Point2),                 // Window coordinates
58
-    RightClick(Point2),            // Window coordinates
59
-    AreaSelection(Point2, Point2), // Window coordinates
57
+    Click(WindowPoint),                      // Window coordinates
58
+    RightClick(WindowPoint),                 // Window coordinates
59
+    AreaSelection(WindowPoint, WindowPoint), // Window coordinates
60
 }
60
 }
61
 
61
 
62
 pub enum SceneItemPrepareOrder {
62
 pub enum SceneItemPrepareOrder {