summaryrefslogtreecommitdiff
path: root/src/Frame.cpp
blob: c63aeb411d49c3a04f1f9478a02aa10a443b70ce (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "Frame.hpp"

Frame::Frame(string location, int nRow, int nCol)
{
    hasSuper = FALSE;
    super = NULL;

    int i, j=0;
    string line;
    ifstream file (location);

    if(file.is_open())
    {
        getline(file,line);
        m.push_back(line);
        i=line.length();
        while(getline(file,line))
        {
            m.push_back(line);
            j++;
        }
    }

    w = newwin(j,i,nRow,nCol);
    height = j;
    width = i;
    row = nRow;
    col = nCol; 
}


Frame::Frame(Frame &sw, int rows, int cols, int nRow, int nCol)
{
    hasSuper = TRUE;
    super = sw.getWin();
    w = derwin(super,rows,cols,nRow,nCol);
    height = rows;
    width = cols;
    row = nRow;
    col = nCol;
}

Frame::~Frame()
{
    delwin(w);
}

void Frame::center(Character &ch)
{
    if(hasSuper)
    {
        int rr = row;
        int cc = col;
        int hh, ww;
        int r = ch.getLocation().x - height/2;
        int c = ch.getLocation().y - width/2;

        getmaxyx(super, hh, ww);

        if(c + width >= ww)
        {
            int delta = ww - (c + width);
            cc = c + delta;
        }
        else
            cc = c;
        
        if(r + height >= hh)
        {
            int delta = hh - (r + height);
            rr = r + delta;
        }
        else
            rr = r;
        
        if(r < 0)
            rr = 0;
        
        if(c < 0)
            cc = 0;

        move(rr,cc);
    }
}

void Frame::refresh()
{
    if(hasSuper)
        touchwin(super);
    wrefresh(w);
}

void Frame::move(int nRow, int nCol)
{
    if(hasSuper)
    {
        mvderwin(w,nRow,nCol);
        row = nRow;
        col = nCol;
        refresh();
    }
}

void Frame::fillWindow()
{ 
    if(!colored)
        for(int i = 0; i < width; i++)
            for(int j = 0; j < height; j++)
            {
                if(m[j][i] == 'O' || m[j][i] == '0')
                    wattron(w,COLOR_PAIR(2));
                else
                    wattron(w,COLOR_PAIR(1));
                mvwaddch(w,j,i,m[j][i]);
            }
    
    wattron(w,COLOR_PAIR(3));
    for(int y = 0; y < height; ++y)
    {      
	mvwaddch(w, y, 0, '-');
	mvwaddch(w, y, width - 1, '-');
    }

    for(int x = 0; x < width; ++x) 
    {
	mvwaddch(w, 0, x, '|');
	mvwaddch(w, height - 1, x, '|');
    }
    wattroff(w,COLOR_PAIR(3));
}