summaryrefslogtreecommitdiff
path: root/src/view.rs
blob: f464da91af876eff7dba28d4375a641316d4e6b1 (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
60
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 location = character.get_location();
        let c = location.0 - self.width/2;
        let r = 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();
    }
}