Skip to content

Commit

Permalink
feat(UI): add ability to change settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinatorul committed Aug 29, 2015
1 parent e135d1f commit 12c8ead
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 75 deletions.
12 changes: 12 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub enum ParamType {
Width,
Height,
Mines
}

pub enum MoveDestination {
Up,
Down,
Left,
Right
}
57 changes: 45 additions & 12 deletions src/field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rand;
use rand::Rng;
use std::collections::VecDeque;
use common::{ParamType, MoveDestination};
use piston_window::*;

pub enum Content {
Expand All @@ -9,13 +10,6 @@ pub enum Content {
None
}

pub enum MoveDestination {
Up,
Down,
Left,
Right
}

struct Cell {
content: Content,
revealed: bool
Expand Down Expand Up @@ -58,14 +52,22 @@ impl Field {
nubmers_total: 0,
nubmers_opened: 0
};
for _i in 0..field.size {
field.cells.push(Cell{content: Content::None,
revealed: false});
}
field.reinit_vec();
field.fill();
field
}

fn reinit_vec(&mut self) {
self.cells.clear();
self.size = self.width*self.height;
for _i in 0..self.size {
self.cells.push(Cell{content: Content::None,
revealed: false});
}
self.selected_x = self.width/2;
self.selected_y = self.height/2;
}

fn fill(&mut self) {
self.clear();
for _i in 0..self.mines {
Expand Down Expand Up @@ -273,7 +275,7 @@ impl Field {
],
context.transform,
graphics);
for i in 0..self.get_width()+1 {
for i in 0..self.get_width() + 1 {
line::Line::new([0.5, 0.5, 0.5, 1.0], 1.0)
.draw([
(field_rect[0] + i*cell_w) as f64,
Expand All @@ -284,6 +286,8 @@ impl Field {
&context.draw_state,
context.transform,
graphics);
}
for i in 0..self.get_height() + 1 {
line::Line::new([0.5, 0.5, 0.5, 1.0], 1.0)
.draw([
field_rect[0] as f64,
Expand Down Expand Up @@ -329,4 +333,33 @@ impl Field {
pub fn is_victory(&self) -> bool {
self.nubmers_total == self.nubmers_opened
}

pub fn reinit_field(&mut self, num: u32, param: ParamType) {
let mut restart_neded = false;
match param {
ParamType::Height => {
if self.height != num {
self.height = num;
self.reinit_vec();
restart_neded = true;
}
}
ParamType::Width => {
if self.width != num {
self.width = num;
self.reinit_vec();
restart_neded = true;
}
}
ParamType::Mines => {
if self.mines != num {
self.mines = num;
restart_neded = true;
}
}
}
if restart_neded {
self.restart();
}
}
}
132 changes: 93 additions & 39 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use field::{Field, Content, MoveDestination};
use ui::{UI, BlockType};
use field::{Field, Content};
use ui::UI;
use piston_window::*;
use common::{ParamType, MoveDestination};

pub struct Game<'a> {
field: Field,
Expand All @@ -9,7 +10,8 @@ pub struct Game<'a> {
mouse_x: f64,
mouse_y: f64,
game_ended: bool,
panel_width: u32
panel_width: u32,
in_ui: bool
}

impl<'a> Game<'a> {
Expand All @@ -21,7 +23,8 @@ impl<'a> Game<'a> {
mouse_x: 0.0,
mouse_y: 0.0,
game_ended: false,
panel_width: 350
panel_width: 350,
in_ui: false
}
}

Expand Down Expand Up @@ -52,42 +55,93 @@ impl<'a> Game<'a> {
}

pub fn proc_key(&mut self, button: Button, window: &PistonWindow) {
match button {
Button::Keyboard(key) => {
match key {
Key::R => self.restart(),
Key::Up => self.field.move_selection(MoveDestination::Up),
Key::Down => self.field.move_selection(MoveDestination::Down),
Key::Left => self.field.move_selection(MoveDestination::Left),
Key::Right => self.field.move_selection(MoveDestination::Right),
Key::Space => {
let ind = self.field.get_selected_ind();
self.open_cell(ind);
},
Key::H => self.ui.select(BlockType::Height),
Key::M => self.ui.select(BlockType::Mines),
Key::W => self.ui.select(BlockType::Width),
_ => println!("{:?}", key)
if self.in_ui {
match button {
Button::Keyboard(key) => {
match key {
Key::H => {
match self.ui.proc_key(ParamType::Height) {
Some(h) => {
self.in_ui = false;
self.field.reinit_field(h, ParamType::Height);
}
_ => {}
}
},
Key::M => {
match self.ui.proc_key(ParamType::Mines) {
Some(m) => {
self.in_ui = false;
self.field.reinit_field(m, ParamType::Mines);
}
_ => {}
}
},
Key::W => {
match self.ui.proc_key(ParamType::Width) {
Some(w) => {
self.in_ui = false;
self.field.reinit_field(w, ParamType::Width);
}
_ => {}
}
},
Key::Up => self.ui.change_selected(MoveDestination::Up),
Key::Down => self.ui.change_selected(MoveDestination::Down),
Key::Left => self.ui.change_selected(MoveDestination::Left),
Key::Right => self.ui.change_selected(MoveDestination::Right),
_ => {}
}
}
},
Button::Mouse(btn) => {
match btn {
MouseButton::Left => {
let field_rect = self.get_field_rect(window);
let cell_w = field_rect[2] / self.field.get_width();
let cell_h = field_rect[3] / self.field.get_height();
let mouse_x = self.mouse_x.floor() as u32;
let mouse_y = self.mouse_y.floor() as u32;
if (mouse_x < field_rect[0]) || (mouse_x > field_rect[0] + field_rect[2]) ||
(mouse_y < field_rect[1]) || (mouse_y > field_rect[1] + field_rect[3]) {
return;
}
let x = (mouse_x - field_rect[0]) / cell_w;
let y = (mouse_y - field_rect[1]) / cell_h;
let w = self.field.get_width();
self.open_cell(x + y*w);
},
_ => println!("{:?}", btn)
_ => {}
}
} else {
match button {
Button::Keyboard(key) => {
match key {
Key::R => self.restart(),
Key::Up => self.field.move_selection(MoveDestination::Up),
Key::Down => self.field.move_selection(MoveDestination::Down),
Key::Left => self.field.move_selection(MoveDestination::Left),
Key::Right => self.field.move_selection(MoveDestination::Right),
Key::Space => {
let ind = self.field.get_selected_ind();
self.open_cell(ind);
},
Key::H => {
self.ui.proc_key(ParamType::Height);
self.in_ui = true;
},
Key::M =>{
self.ui.proc_key(ParamType::Mines);
self.in_ui = true;
},
Key::W => {
self.ui.proc_key(ParamType::Width);
self.in_ui = true;
},
_ => println!("{:?}", key)
}
},
Button::Mouse(btn) => {
match btn {
MouseButton::Left => {
let field_rect = self.get_field_rect(window);
let cell_w = field_rect[2] / self.field.get_width();
let cell_h = field_rect[3] / self.field.get_height();
let mouse_x = self.mouse_x.floor() as u32;
let mouse_y = self.mouse_y.floor() as u32;
if (mouse_x < field_rect[0]) || (mouse_x > field_rect[0] + field_rect[2]) ||
(mouse_y < field_rect[1]) || (mouse_y > field_rect[1] + field_rect[3]) {
return;
}
let x = (mouse_x - field_rect[0]) / cell_w;
let y = (mouse_y - field_rect[1]) / cell_h;
let w = self.field.get_width();
self.open_cell(x + y*w);
},
_ => println!("{:?}", btn)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern crate find_folder;
mod game;
mod field;
mod ui;
mod common;

use piston_window::*;
use clap::{Arg, App};
Expand Down
Loading

0 comments on commit 12c8ead

Please sign in to comment.