|
@@ -7,15 +7,17 @@ use ggez::timer::check_update_time;
|
7
|
7
|
use ggez::{event, graphics, input, Context, GameResult};
|
8
|
8
|
|
9
|
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
|
13
|
use crate::physics::{util, MetaEvent, PhysicEvent};
|
12
|
14
|
use crate::scene::item::{ItemState, SceneItem, SceneItemType};
|
13
|
15
|
use crate::ui::scene_item_menu::SceneItemMenuItem;
|
14
|
16
|
use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
|
15
|
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
|
22
|
use ggez::input::keyboard::KeyCode;
|
21
|
23
|
|
|
@@ -24,7 +26,7 @@ pub struct MainState {
|
24
|
26
|
frame_i: u32,
|
25
|
27
|
|
26
|
28
|
// display
|
27
|
|
- display_offset: Point2,
|
|
29
|
+ display_offset: Offset,
|
28
|
30
|
sprite_sheet_batch: graphics::spritebatch::SpriteBatch,
|
29
|
31
|
map_batch: graphics::spritebatch::SpriteBatch,
|
30
|
32
|
ui_batch: graphics::spritebatch::SpriteBatch,
|
|
@@ -37,12 +39,12 @@ pub struct MainState {
|
37
|
39
|
physics_events: Vec<PhysicEvent>,
|
38
|
40
|
|
39
|
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
|
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
|
48
|
scene_item_prepare_order: Option<SceneItemPrepareOrder>,
|
47
|
49
|
}
|
48
|
50
|
|
|
@@ -66,7 +68,7 @@ impl MainState {
|
66
|
68
|
|
67
|
69
|
scene_items.push(SceneItem::new(
|
68
|
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
|
72
|
ItemState::new(current_behavior),
|
71
|
73
|
));
|
72
|
74
|
}
|
|
@@ -74,7 +76,7 @@ impl MainState {
|
74
|
76
|
|
75
|
77
|
let mut main_state = MainState {
|
76
|
78
|
frame_i: 0,
|
77
|
|
- display_offset: Point2::new(0.0, 0.0),
|
|
79
|
+ display_offset: Offset::new(0.0, 0.0),
|
78
|
80
|
sprite_sheet_batch,
|
79
|
81
|
map_batch,
|
80
|
82
|
ui_batch,
|
|
@@ -83,7 +85,7 @@ impl MainState {
|
83
|
85
|
physics_events: vec![],
|
84
|
86
|
left_click_down: None,
|
85
|
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
|
89
|
user_events: vec![],
|
88
|
90
|
selected_scene_items: vec![],
|
89
|
91
|
scene_item_menu: None,
|
|
@@ -91,7 +93,7 @@ impl MainState {
|
91
|
93
|
};
|
92
|
94
|
|
93
|
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
|
97
|
main_state
|
96
|
98
|
.scene_items_by_grid_position
|
97
|
99
|
.entry(grid_position)
|
|
@@ -125,14 +127,12 @@ impl MainState {
|
125
|
127
|
|
126
|
128
|
while let Some(user_event) = self.user_events.pop() {
|
127
|
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
|
133
|
self.selected_scene_items.drain(..);
|
134
|
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
|
137
|
self.selected_scene_items.push(scene_item_usize);
|
138
|
138
|
}
|
|
@@ -143,20 +143,22 @@ impl MainState {
|
143
|
143
|
}
|
144
|
144
|
|
145
|
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
|
149
|
let menu_sprite_info = UiSpriteInfo::from_type(UiItem::SceneItemMenu);
|
148
|
150
|
let scene_item = self
|
149
|
151
|
.scene_items
|
150
|
152
|
.get(scene_item_usize)
|
151
|
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
|
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
|
162
|
scene_item,
|
161
|
163
|
) {
|
162
|
164
|
match menu_item {
|
|
@@ -172,29 +174,26 @@ impl MainState {
|
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
|
181
|
self.selected_scene_items.drain(..);
|
183
|
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
|
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
|
191
|
if self.selected_scene_items.contains(&scene_item_usize) {
|
192
|
192
|
let scene_item = self
|
193
|
193
|
.scene_items
|
194
|
194
|
.get(scene_item_usize)
|
195
|
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,7 +210,7 @@ impl MainState {
|
211
|
210
|
// TODO ici il faut calculer le déplacement réél (en fonction des ticks, etc ...)
|
212
|
211
|
scene_item.position.x += 1.0;
|
213
|
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,21 +272,14 @@ impl MainState {
|
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
|
276
|
// TODO: if found multiple: select nearest
|
285
|
277
|
for (i, scene_item) in self.scene_items.iter().enumerate() {
|
286
|
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
|
284
|
return Some(i);
|
293
|
285
|
}
|
|
@@ -296,7 +288,7 @@ impl MainState {
|
296
|
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
|
292
|
let mut selection = vec![];
|
301
|
293
|
|
302
|
294
|
for (i, scene_item) in self.scene_items.iter().enumerate() {
|
|
@@ -408,18 +400,26 @@ impl event::EventHandler for MainState {
|
408
|
400
|
|
409
|
401
|
scene_mesh_builder.circle(
|
410
|
402
|
DrawMode::fill(),
|
411
|
|
- left_click_down,
|
|
403
|
+ window_point_from_scene_point(&left_click_down, &self.display_offset),
|
412
|
404
|
2.0,
|
413
|
405
|
2.0,
|
414
|
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
|
419
|
self.ui_batch.add(
|
420
|
420
|
UiSpriteInfo::from_type(UiItem::SceneItemMenu)
|
421
|
421
|
.as_draw_param()
|
422
|
|
- .dest(position),
|
|
422
|
+ .dest(scene_point),
|
423
|
423
|
);
|
424
|
424
|
}
|
425
|
425
|
|
|
@@ -431,7 +431,13 @@ impl event::EventHandler for MainState {
|
431
|
431
|
.get(*scene_item_usize)
|
432
|
432
|
.expect(SCENE_ITEMS_CHANGE_ERR_MSG);
|
433
|
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
|
441
|
2.0,
|
436
|
442
|
graphics::WHITE,
|
437
|
443
|
)?;
|
|
@@ -442,33 +448,41 @@ impl event::EventHandler for MainState {
|
442
|
448
|
self.map_batch.add(
|
443
|
449
|
graphics::DrawParam::new()
|
444
|
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
|
454
|
let scene_mesh = scene_mesh_builder.build(ctx)?;
|
449
|
455
|
graphics::draw(
|
450
|
456
|
ctx,
|
451
|
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
|
463
|
graphics::draw(
|
456
|
464
|
ctx,
|
457
|
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
|
471
|
graphics::draw(
|
462
|
472
|
ctx,
|
463
|
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
|
479
|
graphics::draw(
|
468
|
480
|
ctx,
|
469
|
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
|
488
|
self.sprite_sheet_batch.clear();
|
|
@@ -483,10 +497,10 @@ impl event::EventHandler for MainState {
|
483
|
497
|
fn mouse_button_down_event(&mut self, _ctx: &mut Context, button: MouseButton, x: f32, y: f32) {
|
484
|
498
|
match button {
|
485
|
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
|
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
|
505
|
MouseButton::Middle => {}
|
492
|
506
|
MouseButton::Other(_) => {}
|
|
@@ -497,14 +511,14 @@ impl event::EventHandler for MainState {
|
497
|
511
|
match button {
|
498
|
512
|
MouseButton::Left => {
|
499
|
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
|
515
|
self.user_events.push(UserEvent::Click(left_click_down));
|
502
|
516
|
} else {
|
503
|
|
- let from = Point2::new(
|
|
517
|
+ let from = WindowPoint::new(
|
504
|
518
|
cmp::min(left_click_down.x as i32, x as i32) as f32,
|
505
|
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
|
522
|
cmp::max(left_click_down.x as i32, x as i32) as f32,
|
509
|
523
|
cmp::max(left_click_down.y as i32, y as i32) as f32,
|
510
|
524
|
);
|
|
@@ -525,6 +539,6 @@ impl event::EventHandler for MainState {
|
525
|
539
|
}
|
526
|
540
|
|
527
|
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
|
}
|