|
@@ -3,10 +3,16 @@ use std::collections::HashMap;
|
3
|
3
|
|
4
|
4
|
use ggez::event::MouseButton;
|
5
|
5
|
use ggez::graphics::{DrawMode, MeshBuilder, StrokeOptions};
|
|
6
|
+use ggez::input::keyboard::KeyCode;
|
6
|
7
|
use ggez::timer::check_update_time;
|
7
|
8
|
use ggez::{event, graphics, input, Context, GameResult};
|
8
|
9
|
|
9
|
10
|
use crate::behavior::ItemBehavior;
|
|
11
|
+use crate::config::{
|
|
12
|
+ ANIMATE_EACH, DEBUG, DEFAULT_SELECTED_SQUARE_SIDE, DEFAULT_SELECTED_SQUARE_SIDE_HALF,
|
|
13
|
+ DISPLAY_OFFSET_BY, DISPLAY_OFFSET_BY_SPEED, MAX_FRAME_I, META_EACH, PHYSICS_EACH,
|
|
14
|
+ SCENE_ITEMS_CHANGE_ERR_MSG, SPRITE_EACH, TARGET_FPS,
|
|
15
|
+};
|
10
|
16
|
use crate::physics::util::scene_point_from_window_point;
|
11
|
17
|
use crate::physics::util::window_point_from_scene_point;
|
12
|
18
|
use crate::physics::GridPosition;
|
|
@@ -14,12 +20,7 @@ use crate::physics::{util, MetaEvent, PhysicEvent};
|
14
|
20
|
use crate::scene::item::{ItemState, SceneItem, SceneItemType};
|
15
|
21
|
use crate::ui::scene_item_menu::SceneItemMenuItem;
|
16
|
22
|
use crate::ui::{SceneItemPrepareOrder, UiItem, UiSpriteInfo, UserEvent};
|
17
|
|
-use crate::{
|
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,
|
21
|
|
-};
|
22
|
|
-use ggez::input::keyboard::KeyCode;
|
|
23
|
+use crate::{Offset, ScenePoint, WindowPoint};
|
23
|
24
|
|
24
|
25
|
pub struct MainState {
|
25
|
26
|
// time
|
|
@@ -315,6 +316,83 @@ impl MainState {
|
315
|
316
|
|
316
|
317
|
selection
|
317
|
318
|
}
|
|
319
|
+
|
|
320
|
+ fn build_scene_items_draw_params(&self) -> Vec<graphics::DrawParam> {
|
|
321
|
+ let mut draw_params = vec![];
|
|
322
|
+
|
|
323
|
+ for scene_item in self.scene_items.iter() {
|
|
324
|
+ draw_params.push(
|
|
325
|
+ scene_item
|
|
326
|
+ .as_draw_param(scene_item.current_frame as f32)
|
|
327
|
+ .dest(scene_item.position.clone()),
|
|
328
|
+ )
|
|
329
|
+ }
|
|
330
|
+
|
|
331
|
+ draw_params
|
|
332
|
+ }
|
|
333
|
+
|
|
334
|
+ fn update_mesh_builder_with_scene_items_positions(
|
|
335
|
+ &self,
|
|
336
|
+ mut mesh_builder: MeshBuilder,
|
|
337
|
+ ) -> GameResult<MeshBuilder> {
|
|
338
|
+ for scene_item in self.scene_items.iter() {
|
|
339
|
+ mesh_builder.circle(
|
|
340
|
+ DrawMode::fill(),
|
|
341
|
+ scene_item.position.clone(),
|
|
342
|
+ 2.0,
|
|
343
|
+ 2.0,
|
|
344
|
+ graphics::WHITE,
|
|
345
|
+ )?;
|
|
346
|
+ }
|
|
347
|
+
|
|
348
|
+ GameResult::Ok(mesh_builder)
|
|
349
|
+ }
|
|
350
|
+
|
|
351
|
+ fn update_mesh_builder_with_selected_items(
|
|
352
|
+ &self,
|
|
353
|
+ mut mesh_builder: MeshBuilder,
|
|
354
|
+ ) -> GameResult<MeshBuilder> {
|
|
355
|
+ for i in &self.selected_scene_items {
|
|
356
|
+ let selected_scene_item = self.scene_items.get(*i).expect(SCENE_ITEMS_CHANGE_ERR_MSG);
|
|
357
|
+ mesh_builder.rectangle(
|
|
358
|
+ DrawMode::Stroke(StrokeOptions::default()),
|
|
359
|
+ graphics::Rect::new(
|
|
360
|
+ selected_scene_item.position.x - DEFAULT_SELECTED_SQUARE_SIDE_HALF,
|
|
361
|
+ selected_scene_item.position.y - DEFAULT_SELECTED_SQUARE_SIDE_HALF,
|
|
362
|
+ DEFAULT_SELECTED_SQUARE_SIDE,
|
|
363
|
+ DEFAULT_SELECTED_SQUARE_SIDE,
|
|
364
|
+ ),
|
|
365
|
+ graphics::GREEN,
|
|
366
|
+ )?;
|
|
367
|
+ }
|
|
368
|
+
|
|
369
|
+ GameResult::Ok(mesh_builder)
|
|
370
|
+ }
|
|
371
|
+
|
|
372
|
+ fn update_mesh_builder_with_selection_area(
|
|
373
|
+ &self,
|
|
374
|
+ mut mesh_builder: MeshBuilder,
|
|
375
|
+ window_left_click_down_point: WindowPoint,
|
|
376
|
+ ) -> GameResult<MeshBuilder> {
|
|
377
|
+ let scene_left_click_down_point =
|
|
378
|
+ scene_point_from_window_point(&window_left_click_down_point, &self.display_offset);
|
|
379
|
+ let scene_current_cursor_position =
|
|
380
|
+ scene_point_from_window_point(&self.current_cursor_position, &self.display_offset);
|
|
381
|
+ if scene_left_click_down_point != scene_current_cursor_position {
|
|
382
|
+ mesh_builder.rectangle(
|
|
383
|
+ DrawMode::stroke(1.0),
|
|
384
|
+ graphics::Rect::new(
|
|
385
|
+ scene_left_click_down_point.x,
|
|
386
|
+ scene_left_click_down_point.y,
|
|
387
|
+ scene_current_cursor_position.x - scene_left_click_down_point.x,
|
|
388
|
+ scene_current_cursor_position.y - scene_left_click_down_point.y,
|
|
389
|
+ ),
|
|
390
|
+ graphics::GREEN,
|
|
391
|
+ )?;
|
|
392
|
+ }
|
|
393
|
+
|
|
394
|
+ GameResult::Ok(mesh_builder)
|
|
395
|
+ }
|
318
|
396
|
}
|
319
|
397
|
|
320
|
398
|
impl event::EventHandler for MainState {
|
|
@@ -364,63 +442,38 @@ impl event::EventHandler for MainState {
|
364
|
442
|
|
365
|
443
|
fn draw(&mut self, ctx: &mut Context) -> GameResult {
|
366
|
444
|
graphics::clear(ctx, graphics::BLACK);
|
367
|
|
-
|
368
|
445
|
let mut scene_mesh_builder = MeshBuilder::new();
|
369
|
446
|
|
370
|
|
- for scene_item in self.scene_items.iter() {
|
371
|
|
- self.sprite_sheet_batch.add(
|
372
|
|
- scene_item
|
373
|
|
- .as_draw_param(scene_item.current_frame as f32)
|
374
|
|
- .dest(scene_item.position.clone()),
|
375
|
|
- );
|
376
|
|
- scene_mesh_builder.circle(
|
377
|
|
- DrawMode::fill(),
|
378
|
|
- scene_item.position.clone(),
|
379
|
|
- 2.0,
|
380
|
|
- 2.0,
|
381
|
|
- graphics::WHITE,
|
382
|
|
- )?;
|
|
447
|
+ for sprite in self.build_scene_items_draw_params() {
|
|
448
|
+ self.sprite_sheet_batch.add(sprite);
|
383
|
449
|
}
|
384
|
450
|
|
385
|
|
- for i in &self.selected_scene_items {
|
386
|
|
- let selected_scene_item = self.scene_items.get(*i).expect(SCENE_ITEMS_CHANGE_ERR_MSG);
|
387
|
|
- scene_mesh_builder.rectangle(
|
388
|
|
- DrawMode::Stroke(StrokeOptions::default()),
|
389
|
|
- graphics::Rect::new(
|
390
|
|
- selected_scene_item.position.x - DEFAULT_SELECTED_SQUARE_SIDE_HALF,
|
391
|
|
- selected_scene_item.position.y - DEFAULT_SELECTED_SQUARE_SIDE_HALF,
|
392
|
|
- DEFAULT_SELECTED_SQUARE_SIDE,
|
393
|
|
- DEFAULT_SELECTED_SQUARE_SIDE,
|
394
|
|
- ),
|
395
|
|
- graphics::GREEN,
|
396
|
|
- )?;
|
|
451
|
+ if DEBUG {
|
|
452
|
+ scene_mesh_builder =
|
|
453
|
+ self.update_mesh_builder_with_scene_items_positions(scene_mesh_builder)?;
|
397
|
454
|
}
|
398
|
455
|
|
|
456
|
+ scene_mesh_builder = self.update_mesh_builder_with_selected_items(scene_mesh_builder)?;
|
|
457
|
+
|
399
|
458
|
if let Some(window_left_click_down_point) = self.left_click_down {
|
400
|
|
- let scene_left_click_down_point =
|
401
|
|
- scene_point_from_window_point(&window_left_click_down_point, &self.display_offset);
|
402
|
|
- let scene_current_cursor_position =
|
403
|
|
- scene_point_from_window_point(&self.current_cursor_position, &self.display_offset);
|
404
|
|
- if scene_left_click_down_point != scene_current_cursor_position {
|
405
|
|
- scene_mesh_builder.rectangle(
|
|
459
|
+ scene_mesh_builder = self.update_mesh_builder_with_selection_area(
|
|
460
|
+ scene_mesh_builder,
|
|
461
|
+ window_left_click_down_point,
|
|
462
|
+ )?;
|
|
463
|
+
|
|
464
|
+ if DEBUG {
|
|
465
|
+ let scene_left_click_down_point = scene_point_from_window_point(
|
|
466
|
+ &window_left_click_down_point,
|
|
467
|
+ &self.display_offset,
|
|
468
|
+ );
|
|
469
|
+ scene_mesh_builder.circle(
|
406
|
470
|
DrawMode::fill(),
|
407
|
|
- graphics::Rect::new(
|
408
|
|
- scene_left_click_down_point.x,
|
409
|
|
- scene_left_click_down_point.y,
|
410
|
|
- scene_current_cursor_position.x - scene_left_click_down_point.x,
|
411
|
|
- scene_current_cursor_position.y - scene_left_click_down_point.y,
|
412
|
|
- ),
|
413
|
|
- graphics::GREEN,
|
|
471
|
+ scene_left_click_down_point,
|
|
472
|
+ 2.0,
|
|
473
|
+ 2.0,
|
|
474
|
+ graphics::YELLOW,
|
414
|
475
|
)?;
|
415
|
476
|
}
|
416
|
|
-
|
417
|
|
- scene_mesh_builder.circle(
|
418
|
|
- DrawMode::fill(),
|
419
|
|
- scene_left_click_down_point,
|
420
|
|
- 2.0,
|
421
|
|
- 2.0,
|
422
|
|
- graphics::YELLOW,
|
423
|
|
- )?;
|
424
|
477
|
}
|
425
|
478
|
|
426
|
479
|
scene_mesh_builder.circle(
|