gui.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. extern crate rltk;
  2. use rltk::{ RGB, Rltk, Console, Point, VirtualKeyCode };
  3. extern crate specs;
  4. use specs::prelude::*;
  5. use super::{CombatStats, Player, gamelog::GameLog, Map, Name, Position, State, InBackpack,
  6. Viewshed, RunState};
  7. pub fn draw_ui(ecs: &World, ctx : &mut Rltk) {
  8. ctx.draw_box(0, 43, 79, 6, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  9. let combat_stats = ecs.read_storage::<CombatStats>();
  10. let players = ecs.read_storage::<Player>();
  11. for (_player, stats) in (&players, &combat_stats).join() {
  12. let health = format!(" HP: {} / {} ", stats.hp, stats.max_hp);
  13. ctx.print_color(12, 43, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), &health);
  14. ctx.draw_bar_horizontal(28, 43, 51, stats.hp, stats.max_hp, RGB::named(rltk::RED), RGB::named(rltk::BLACK));
  15. }
  16. let map = ecs.fetch::<Map>();
  17. let depth = format!("Depth: {}", map.depth);
  18. ctx.print_color(2, 43, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), &depth);
  19. let log = ecs.fetch::<GameLog>();
  20. let mut y = 44;
  21. for s in log.entries.iter() {
  22. if y < 49 { ctx.print(2, y, &s.to_string()); }
  23. y += 1;
  24. }
  25. // Draw mouse cursor
  26. let mouse_pos = ctx.mouse_pos();
  27. ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::MAGENTA));
  28. draw_tooltips(ecs, ctx);
  29. }
  30. fn draw_tooltips(ecs: &World, ctx : &mut Rltk) {
  31. let map = ecs.fetch::<Map>();
  32. let names = ecs.read_storage::<Name>();
  33. let positions = ecs.read_storage::<Position>();
  34. let mouse_pos = ctx.mouse_pos();
  35. if mouse_pos.0 >= map.width || mouse_pos.1 >= map.height { return; }
  36. let mut tooltip : Vec<String> = Vec::new();
  37. for (name, position) in (&names, &positions).join() {
  38. if position.x == mouse_pos.0 && position.y == mouse_pos.1 {
  39. tooltip.push(name.name.to_string());
  40. }
  41. }
  42. if !tooltip.is_empty() {
  43. let mut width :i32 = 0;
  44. for s in tooltip.iter() {
  45. if width < s.len() as i32 { width = s.len() as i32; }
  46. }
  47. width += 3;
  48. if mouse_pos.0 > 40 {
  49. let arrow_pos = Point::new(mouse_pos.0 - 2, mouse_pos.1);
  50. let left_x = mouse_pos.0 - width;
  51. let mut y = mouse_pos.1;
  52. for s in tooltip.iter() {
  53. ctx.print_color(left_x, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &s.to_string());
  54. let padding = (width - s.len() as i32)-1;
  55. for i in 0..padding {
  56. ctx.print_color(arrow_pos.x - i, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string());
  57. }
  58. y += 1;
  59. }
  60. ctx.print_color(arrow_pos.x, arrow_pos.y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &"->".to_string());
  61. } else {
  62. let arrow_pos = Point::new(mouse_pos.0 + 1, mouse_pos.1);
  63. let left_x = mouse_pos.0 +3;
  64. let mut y = mouse_pos.1;
  65. for s in tooltip.iter() {
  66. ctx.print_color(left_x, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &s.to_string());
  67. let padding = (width - s.len() as i32)-1;
  68. for i in 0..padding {
  69. ctx.print_color(left_x + s.len() as i32 + i, y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &" ".to_string());
  70. }
  71. y += 1;
  72. }
  73. ctx.print_color(arrow_pos.x, arrow_pos.y, RGB::named(rltk::WHITE), RGB::named(rltk::GREY), &"<-".to_string());
  74. }
  75. }
  76. }
  77. #[derive(PartialEq, Copy, Clone)]
  78. pub enum ItemMenuResult { Cancel, NoResponse, Selected }
  79. pub fn show_inventory(gs : &mut State, ctx : &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
  80. let player_entity = gs.ecs.fetch::<Entity>();
  81. let names = gs.ecs.read_storage::<Name>();
  82. let backpack = gs.ecs.read_storage::<InBackpack>();
  83. let entities = gs.ecs.entities();
  84. let inventory = (&backpack, &names).join().filter(|item| item.0.owner == *player_entity );
  85. let count = inventory.count();
  86. let mut y = (25 - (count / 2)) as i32;
  87. ctx.draw_box(15, y-2, 31, (count+3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  88. ctx.print_color(18, y-2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Inventory");
  89. ctx.print_color(18, y+count as i32+1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel");
  90. let mut equippable : Vec<Entity> = Vec::new();
  91. let mut j = 0;
  92. for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity ) {
  93. ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('));
  94. ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97+j as u8);
  95. ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'));
  96. ctx.print(21, y, &name.name.to_string());
  97. equippable.push(entity);
  98. y += 1;
  99. j += 1;
  100. }
  101. match ctx.key {
  102. None => (ItemMenuResult::NoResponse, None),
  103. Some(key) => {
  104. match key {
  105. VirtualKeyCode::Escape => { (ItemMenuResult::Cancel, None) }
  106. _ => {
  107. let selection = rltk::letter_to_option(key);
  108. if selection > -1 && selection < count as i32 {
  109. return (ItemMenuResult::Selected, Some(equippable[selection as usize]));
  110. }
  111. (ItemMenuResult::NoResponse, None)
  112. }
  113. }
  114. }
  115. }
  116. }
  117. pub fn drop_item_menu(gs : &mut State, ctx : &mut Rltk) -> (ItemMenuResult, Option<Entity>) {
  118. let player_entity = gs.ecs.fetch::<Entity>();
  119. let names = gs.ecs.read_storage::<Name>();
  120. let backpack = gs.ecs.read_storage::<InBackpack>();
  121. let entities = gs.ecs.entities();
  122. let inventory = (&backpack, &names).join().filter(|item| item.0.owner == *player_entity );
  123. let count = inventory.count();
  124. let mut y = (25 - (count / 2)) as i32;
  125. ctx.draw_box(15, y-2, 31, (count+3) as i32, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK));
  126. ctx.print_color(18, y-2, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Drop Which Item?");
  127. ctx.print_color(18, y+count as i32+1, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "ESCAPE to cancel");
  128. let mut equippable : Vec<Entity> = Vec::new();
  129. let mut j = 0;
  130. for (entity, _pack, name) in (&entities, &backpack, &names).join().filter(|item| item.1.owner == *player_entity ) {
  131. ctx.set(17, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437('('));
  132. ctx.set(18, y, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), 97+j as u8);
  133. ctx.set(19, y, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), rltk::to_cp437(')'));
  134. ctx.print(21, y, &name.name.to_string());
  135. equippable.push(entity);
  136. y += 1;
  137. j += 1;
  138. }
  139. match ctx.key {
  140. None => (ItemMenuResult::NoResponse, None),
  141. Some(key) => {
  142. match key {
  143. VirtualKeyCode::Escape => { (ItemMenuResult::Cancel, None) }
  144. _ => {
  145. let selection = rltk::letter_to_option(key);
  146. if selection > -1 && selection < count as i32 {
  147. return (ItemMenuResult::Selected, Some(equippable[selection as usize]));
  148. }
  149. (ItemMenuResult::NoResponse, None)
  150. }
  151. }
  152. }
  153. }
  154. }
  155. pub fn ranged_target(gs : &mut State, ctx : &mut Rltk, range : i32) -> (ItemMenuResult, Option<Point>) {
  156. let player_entity = gs.ecs.fetch::<Entity>();
  157. let player_pos = gs.ecs.fetch::<Point>();
  158. let viewsheds = gs.ecs.read_storage::<Viewshed>();
  159. ctx.print_color(5, 0, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Select Target:");
  160. // Highlight available target cells
  161. let mut available_cells = Vec::new();
  162. let visible = viewsheds.get(*player_entity);
  163. if let Some(visible) = visible {
  164. // We have a viewshed
  165. for idx in visible.visible_tiles.iter() {
  166. let distance = rltk::DistanceAlg::Pythagoras.distance2d(*player_pos, *idx);
  167. if distance <= range as f32 {
  168. ctx.set_bg(idx.x, idx.y, RGB::named(rltk::BLUE));
  169. available_cells.push(idx);
  170. }
  171. }
  172. } else {
  173. return (ItemMenuResult::Cancel, None);
  174. }
  175. // Draw mouse cursor
  176. let mouse_pos = ctx.mouse_pos();
  177. let mut valid_target = false;
  178. for idx in available_cells.iter() { if idx.x == mouse_pos.0 && idx.y == mouse_pos.1 { valid_target = true; } }
  179. if valid_target {
  180. ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::CYAN));
  181. if ctx.left_click {
  182. return (ItemMenuResult::Selected, Some(Point::new(mouse_pos.0, mouse_pos.1)));
  183. }
  184. } else {
  185. ctx.set_bg(mouse_pos.0, mouse_pos.1, RGB::named(rltk::RED));
  186. if ctx.left_click {
  187. return (ItemMenuResult::Cancel, None);
  188. }
  189. }
  190. (ItemMenuResult::NoResponse, None)
  191. }
  192. #[derive(PartialEq, Copy, Clone)]
  193. pub enum MainMenuSelection { NewGame, LoadGame, Quit }
  194. #[derive(PartialEq, Copy, Clone)]
  195. pub enum MainMenuResult { NoSelection{ selected : MainMenuSelection }, Selected{ selected: MainMenuSelection } }
  196. pub fn main_menu(gs : &mut State, ctx : &mut Rltk) -> MainMenuResult {
  197. let save_exists = super::saveload_system::does_save_exist();
  198. let runstate = gs.ecs.fetch::<RunState>();
  199. ctx.print_color_centered(15, RGB::named(rltk::YELLOW), RGB::named(rltk::BLACK), "Rust Roguelike Tutorial");
  200. if let RunState::MainMenu{ menu_selection : selection } = *runstate {
  201. if selection == MainMenuSelection::NewGame {
  202. ctx.print_color_centered(24, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Begin New Game");
  203. } else {
  204. ctx.print_color_centered(24, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Begin New Game");
  205. }
  206. if save_exists {
  207. if selection == MainMenuSelection::LoadGame {
  208. ctx.print_color_centered(25, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Load Game");
  209. } else {
  210. ctx.print_color_centered(25, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Load Game");
  211. }
  212. }
  213. if selection == MainMenuSelection::Quit {
  214. ctx.print_color_centered(26, RGB::named(rltk::MAGENTA), RGB::named(rltk::BLACK), "Quit");
  215. } else {
  216. ctx.print_color_centered(26, RGB::named(rltk::WHITE), RGB::named(rltk::BLACK), "Quit");
  217. }
  218. match ctx.key {
  219. None => return MainMenuResult::NoSelection{ selected: selection },
  220. Some(key) => {
  221. match key {
  222. VirtualKeyCode::Escape => { return MainMenuResult::NoSelection{ selected: MainMenuSelection::Quit } }
  223. VirtualKeyCode::Up => {
  224. let mut newselection;
  225. match selection {
  226. MainMenuSelection::NewGame => newselection = MainMenuSelection::Quit,
  227. MainMenuSelection::LoadGame => newselection = MainMenuSelection::NewGame,
  228. MainMenuSelection::Quit => newselection = MainMenuSelection::LoadGame
  229. }
  230. if newselection == MainMenuSelection::LoadGame && !save_exists {
  231. newselection = MainMenuSelection::NewGame;
  232. }
  233. return MainMenuResult::NoSelection{ selected: newselection }
  234. }
  235. VirtualKeyCode::Down => {
  236. let mut newselection;
  237. match selection {
  238. MainMenuSelection::NewGame => newselection = MainMenuSelection::LoadGame,
  239. MainMenuSelection::LoadGame => newselection = MainMenuSelection::Quit,
  240. MainMenuSelection::Quit => newselection = MainMenuSelection::NewGame
  241. }
  242. if newselection == MainMenuSelection::LoadGame && !save_exists {
  243. newselection = MainMenuSelection::Quit;
  244. }
  245. return MainMenuResult::NoSelection{ selected: newselection }
  246. }
  247. VirtualKeyCode::Return => return MainMenuResult::Selected{ selected : selection },
  248. _ => return MainMenuResult::NoSelection{ selected: selection }
  249. }
  250. }
  251. }
  252. }
  253. MainMenuResult::NoSelection { selected: MainMenuSelection::NewGame }
  254. }