gui.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. extern crate rltk;
  2. use rltk::{ RGB, Rltk, Console, Point };
  3. extern crate specs;
  4. use specs::prelude::*;
  5. use super::{CombatStats, Player, gamelog::GameLog, Map, Name, Position};
  6. pub fn draw_ui(ecs: &World, ctx : &mut Rltk) {
  7. ctx.draw_box(0, 43, 79, 6, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  8. let combat_stats = ecs.read_storage::<CombatStats>();
  9. let players = ecs.read_storage::<Player>();
  10. for (_player, stats) in (&players, &combat_stats).join() {
  11. let health = format!(" HP: {} / {} ", stats.hp, stats.max_hp);
  12. ctx.print_color(12, 43, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), &health);
  13. ctx.draw_bar_horizontal(28, 43, 51, stats.hp, stats.max_hp, RGB::named(rltk::RED), RGB::named(rltk::BLACK));
  14. }
  15. let log = ecs.fetch::<GameLog>();
  16. let mut y = 44;
  17. for s in log.entries.iter() {
  18. if y < 49 { ctx.print(2, y, &s.to_string()); }
  19. y += 1;
  20. }
  21. // Draw mouse cursor
  22. let mouse_pos = ctx.mouse_pos();
  23. ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::MAGENTA));
  24. draw_tooltips(ecs, ctx);
  25. }
  26. fn draw_tooltips(ecs: &World, ctx : &mut Rltk) {
  27. let map = ecs.fetch::<Map>();
  28. let names = ecs.read_storage::<Name>();
  29. let positions = ecs.read_storage::<Position>();
  30. let mouse_pos = ctx.mouse_pos();
  31. if mouse_pos.0 >= map.width || mouse_pos.1 >= map.height { return; }
  32. let mut tooltip : Vec<String> = Vec::new();
  33. for (name, position) in (&names, &positions).join() {
  34. if position.x == mouse_pos.0 && position.y == mouse_pos.1 {
  35. tooltip.push(name.name.to_string());
  36. }
  37. }
  38. if !tooltip.is_empty() {
  39. let mut width :i32 = 0;
  40. for s in tooltip.iter() {
  41. if width < s.len() as i32 { width = s.len() as i32; }
  42. }
  43. width += 3;
  44. if mouse_pos.0 > 40 {
  45. let arrow_pos = Point::new(mouse_pos.0 - 2, mouse_pos.1);
  46. let left_x = mouse_pos.0 - width;
  47. let mut y = mouse_pos.1;
  48. for s in tooltip.iter() {
  49. ctx.print_color(left_x, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &s.to_string());
  50. let padding = (width - s.len() as i32)-1;
  51. for i in 0..padding {
  52. ctx.print_color(arrow_pos.x - i, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string());
  53. }
  54. y += 1;
  55. }
  56. ctx.print_color(arrow_pos.x, arrow_pos.y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &"->".to_string());
  57. } else {
  58. let arrow_pos = Point::new(mouse_pos.0 + 1, mouse_pos.1);
  59. let left_x = mouse_pos.0 +3;
  60. let mut y = mouse_pos.1;
  61. for s in tooltip.iter() {
  62. ctx.print_color(left_x, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &s.to_string());
  63. let padding = (width - s.len() as i32)-1;
  64. for i in 0..padding {
  65. ctx.print_color(left_x + s.len() as i32 + i, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string());
  66. }
  67. y += 1;
  68. }
  69. ctx.print_color(arrow_pos.x, arrow_pos.y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &"<-".to_string());
  70. }
  71. }
  72. }