gui.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. use rltk::{ RGB, Rltk, VirtualKeyCode};
  2. use specs::prelude::*;
  3. use super::{CombatStats, Player, GameLog,
  4. MAPHEIGHT, Map, Name,
  5. Position, Point, InBackpack,
  6. State};
  7. const GUI_HEIGHT: usize = 50 - MAPHEIGHT - 1;
  8. pub fn draw_ui(ecs: &World, ctx : &mut Rltk) {
  9. ctx.draw_box(0, 38, 79, GUI_HEIGHT, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  10. let combat_stats = ecs.read_storage::<CombatStats>();
  11. let players = ecs.read_storage::<Player>();
  12. for (_player, stats) in (&players, &combat_stats).join() {
  13. let health = format!(" HP: {} / {} ", stats.hp, stats.max_hp);
  14. ctx.print_color(12, 38, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), &health);
  15. ctx.draw_bar_horizontal(28, 38, 51, stats.hp, stats.max_hp, RGB::named(rltk::RED), RGB::named(rltk::BLACK));
  16. }
  17. let log = ecs.fetch::<GameLog>();
  18. let mut y = 40;
  19. for s in log.entries.iter().rev() {
  20. if y < 49 { ctx.print(2, y, s); }
  21. y += 1;
  22. }
  23. // Draw mouse cursor
  24. let mouse_pos = ctx.mouse_pos();
  25. ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::ANTIQUE_WHITE));
  26. draw_tooltips(ecs, ctx);
  27. }
  28. #[derive(PartialEq, Copy, Clone)]
  29. pub enum ItemMenuResult { Cancel, NoResponse, Selected }
  30. pub fn show_inventory(gs : &mut State, ctx : &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
  31. let player_entity = gs.ecs.fetch::<Entity>();
  32. let names = gs.ecs.read_storage::<Name>();
  33. let backpack = gs.ecs.read_storage::<InBackpack>();
  34. let entities = gs.ecs.entities();
  35. let inventory = (&backpack, &names).join().filter(|item| item.0.owner == *player_entity );
  36. let count = inventory.count();
  37. let mut y = (25 - (count / 2)) as i32;
  38. ctx.draw_box(15, y-2, 31, (count+3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  39. ctx.print_color(18, y-2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Inventory");
  40. ctx.print_color(18, y+count as i32+1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel");
  41. let mut equippable : Vec<Entity> = Vec::new();
  42. let mut j = 0;
  43. for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity ) {
  44. ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('));
  45. ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97+j as rltk::FontCharType);
  46. ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'));
  47. ctx.print(21, y, &name.name.to_string());
  48. equippable.push(entity);
  49. y += 1;
  50. j += 1;
  51. }
  52. match ctx.key {
  53. None => (ItemMenuResult::NoResponse, None),
  54. Some(key) => {
  55. match key {
  56. VirtualKeyCode::Escape => { (ItemMenuResult::Cancel, None) }
  57. _ => {
  58. let selection = rltk::letter_to_option(key);
  59. if selection > -1 && selection < count as i32 {
  60. return (ItemMenuResult::Selected, Some(equippable[selection as usize]));
  61. }
  62. (ItemMenuResult::NoResponse, None)
  63. }
  64. }
  65. }
  66. }
  67. }
  68. pub fn drop_item_menu(gs : &mut State, ctx : &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
  69. let player_entity = gs.ecs.fetch::<Entity>();
  70. let names = gs.ecs.read_storage::<Name>();
  71. let backpack = gs.ecs.read_storage::<InBackpack>();
  72. let entities = gs.ecs.entities();
  73. let inventory = (&backpack, &names).join().filter(|item| item.0.owner == *player_entity );
  74. let count = inventory.count();
  75. let mut y = (25 - (count / 2)) as i32;
  76. ctx.draw_box(15, y-2, 31, (count+3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  77. ctx.print_color(18, y-2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Drop Which Item?");
  78. ctx.print_color(18, y+count as i32+1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel");
  79. let mut equippable : Vec<Entity> = Vec::new();
  80. let mut j = 0;
  81. for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity ) {
  82. ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('));
  83. ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97+j as rltk::FontCharType);
  84. ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'));
  85. ctx.print(21, y, &name.name.to_string());
  86. equippable.push(entity);
  87. y += 1;
  88. j += 1;
  89. }
  90. match ctx.key {
  91. None => (ItemMenuResult::NoResponse, None),
  92. Some(key) => {
  93. match key {
  94. VirtualKeyCode::Escape => { (ItemMenuResult::Cancel, None) }
  95. _ => {
  96. let selection = rltk::letter_to_option(key);
  97. if selection > -1 && selection < count as i32 {
  98. return (ItemMenuResult::Selected, Some(equippable[selection as usize]));
  99. }
  100. (ItemMenuResult::NoResponse, None)
  101. }
  102. }
  103. }
  104. }
  105. }
  106. fn draw_tooltips(ecs: &World, ctx : &mut Rltk) {
  107. let map = ecs.fetch::<Map>();
  108. let names = ecs.read_storage::<Name>();
  109. let positions = ecs.read_storage::<Position>();
  110. let mouse_pos = ctx.mouse_pos();
  111. if mouse_pos.0 >= map.width || mouse_pos.1 >= map.height { return; }
  112. let mut tooltip : Vec<String> = Vec::new();
  113. for (name, position) in (&names, &positions).join() {
  114. let idx = map.xy_idx(position.x, position.y);
  115. if position.x == mouse_pos.0 && position.y == mouse_pos.1 && map.visible_tiles[idx] {
  116. tooltip.push(name.name.to_string());
  117. }
  118. }
  119. if !tooltip.is_empty() {
  120. let mut width :i32 = 0;
  121. for s in tooltip.iter() {
  122. if width < s.len() as i32 { width = s.len() as i32; }
  123. }
  124. width += 3;
  125. if mouse_pos.0 > 40 {
  126. let arrow_pos = Point::new(mouse_pos.0 - 2, mouse_pos.1);
  127. let left_x = mouse_pos.0 - width;
  128. let mut y = mouse_pos.1;
  129. for s in tooltip.iter() {
  130. ctx.print_color(left_x, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), s);
  131. let padding = (width - s.len() as i32)-1;
  132. for i in 0..padding {
  133. ctx.print_color(arrow_pos.x - i, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string());
  134. }
  135. y += 1;
  136. }
  137. ctx.print_color(arrow_pos.x, arrow_pos.y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &"->".to_string());
  138. } else {
  139. let arrow_pos = Point::new(mouse_pos.0 + 1, mouse_pos.1);
  140. let left_x = mouse_pos.0 +3;
  141. let mut y = mouse_pos.1;
  142. for s in tooltip.iter() {
  143. ctx.print_color(left_x + 1, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), s);
  144. let padding = (width - s.len() as i32)-1;
  145. for i in 0..padding {
  146. ctx.print_color(arrow_pos.x + 1 + i, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string());
  147. }
  148. y += 1;
  149. }
  150. ctx.print_color(arrow_pos.x, arrow_pos.y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string());
  151. }
  152. }
  153. }