summaryrefslogtreecommitdiff
path: root/src/view.rs
blob: d62454bcd5d9bf031b61a7c5c66c47ecc5f8a10b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
extern crate pancurses;

use character::Character;

pub struct View {
	width   : i32,
	height  : i32,
    row     : i32,
    col     : i32,
	window  : pancurses::Window,
}

impl View {
    pub fn new((x,y) : (i32, i32), map_window : &pancurses::Window) -> View {
        let window = map_window.derwin(x,y,0,0).expect("Cannot create derwin.");
        View {
            width   : x,
            height  : y,
            row     : 0,
            col     : 0, 
            window  : window,
        }
    }

    pub fn center(&mut self, character : Character, map_window : &pancurses::Window) {
        let c = character.location.0 - self.width/2;
        let r = character.location.1 - self.height/2;

        let (hh, ww) = map_window.get_max_yx();
        
        if c + self.width >= ww {
            let delta = ww - (c + self.width);
            self.col = c + delta;
        }
        else {
            self.col = c;
        }

        if r + self.height >= hh {
            let delta = hh - (r + self.height);
            self.row = r + delta;
        }
        else {
            self.row = r;
        }

        if r < 0 {
            self.row = 0;
        }

        if c < 0 {
            self.col = 0;
        }
        
        self.window.mvderwin(self.col, self.row);
        map_window.touch();
        self.window.refresh();
    }
}