gui.rs 8.8 KB

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