summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Layout.cpp
blob: d22d37d4fd86a879c619f64a8afb36b326336afe (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Layout Resource class implementation
*/

#include "MemDebug.h"
#include "Layout.h"

// +--------------------------------------------------------------------+

Layout::Layout()
{ }

Layout::~Layout()
{ }

// +--------------------------------------------------------------------+

bool
Layout::DoLayout(ActiveWindow* panel)
{
    if (!panel || panel->GetChildren().size() < 1)
    return false;

    if (cols.size() < 1 || rows.size() < 1)
    return false;

    std::vector<DWORD>   cell_x;
    std::vector<DWORD>   cell_y;

    ScaleWeights();
    CalcCells(panel->Width(), panel->Height(), cell_x, cell_y);

    ListIter<ActiveWindow> iter = panel->GetChildren();
    while (++iter) {
        ActiveWindow* w = iter.value();
        Rect          c = w->GetCells();
        Rect          r;
        Rect          rp = panel->GetRect();

        if (c.x < 0)                     c.x = 0;
        else if (c.x >= (int)cell_x.size())   c.x = cell_x.size() - 1;
        if (c.y < 0)                     c.y = 0;
        else if (c.y >= (int)cell_y.size())   c.y = cell_y.size() - 1;
        if (c.x+c.w  >= (int)cell_x.size())   c.w = cell_x.size() - c.x - 1;
        if (c.y+c.h  >= (int)cell_y.size())   c.h = cell_y.size() - c.y - 1;

        r.x = cell_x[c.x]     + w->GetCellInsets().left;
        r.y = cell_y[c.y]     + w->GetCellInsets().top;
        r.w = cell_x[c.x+c.w] - w->GetCellInsets().right  - r.x;
        r.h = cell_y[c.y+c.h] - w->GetCellInsets().bottom - r.y;

        r.x += panel->X();
        r.y += panel->Y();

        if (w->GetFixedWidth() && w->GetFixedWidth() < r.w)
        r.w = w->GetFixedWidth();

        if (w->GetFixedHeight() && w->GetFixedHeight() < r.h)
        r.h = w->GetFixedHeight();

        if (w->GetID() == 330 || w->GetID() == 125) {
            int y1 = r.y  + r.h;
            int y2 = rp.y + rp.h;
        }

        if (w->GetHidePartial() && (r.x + r.w > rp.x + rp.w)) {
            w->MoveTo(Rect(0,0,0,0));
        }

        else if (w->GetHidePartial() && (r.y + r.h > rp.y + rp.h)) {
            w->MoveTo(Rect(0,0,0,0));
        }

        else {
            w->MoveTo(r);
        }
    }

    return true;
}

// +--------------------------------------------------------------------+

void
Layout::ScaleWeights()
{
    float total = 0;

    for (auto cwi = col_weights.begin(); cwi != col_weights.end(); ++cwi)
        total += *cwi;

    if (total > 0) {
        for (auto cwi = col_weights.begin(); cwi != col_weights.end(); ++cwi)
            *cwi = *cwi / total;
    }

    total = 0;
    for (auto rwi = row_weights.begin(); rwi != row_weights.end(); ++rwi)
        total += *rwi;

    if (total > 0) {
        for (auto rwi = row_weights.begin(); rwi != row_weights.end(); ++rwi)
            *rwi = *rwi / total;
    }
}

// +--------------------------------------------------------------------+

void
    Layout::CalcCells(DWORD w, DWORD h, std::vector<DWORD>& cell_x, std::vector<DWORD>& cell_y)
{
    DWORD       x     = 0;
    DWORD       y     = 0;
    DWORD       min_x = 0;
    DWORD       min_y = 0;
    DWORD       ext_x = 0;
    DWORD       ext_y = 0;

    for (auto cit = cols.begin(); cit != cols.end(); ++cit)
        min_x += *cit;

    for (auto rit = rows.begin(); rit != rows.end(); ++rit)
        min_y += *rit;

    if (min_x < w)
        ext_x = w - min_x;

    if (min_y < h)
        ext_y = h - min_y;

    cell_x.push_back(x);
    for (auto cit = cols.begin(); cit != cols.end(); ++cit) {
        x += *cit + (DWORD) (ext_x * col_weights[cit - cols.begin()]);
        cell_x.push_back(x);
    }

    cell_y.push_back(y);
    for (auto rit = rows.begin(); rit != rows.end(); ++rit) {
        y += *rit + (DWORD) (ext_y * row_weights[rit - rows.begin()]);
        cell_y.push_back(y);
    }
}

// +--------------------------------------------------------------------+

void
Layout::Clear()
{
    cols.clear();
    rows.clear();

    col_weights.clear();
    row_weights.clear();
}

void
Layout::AddCol(DWORD min_width, float col_factor)
{
    cols.push_back(min_width);
    col_weights.push_back(col_factor);
}

void
Layout::AddRow(DWORD min_height, float row_factor)
{
    rows.push_back(min_height);
    row_weights.push_back(row_factor);
}

void
Layout::SetConstraints(const std::vector<DWORD>& min_x,
const std::vector<DWORD>& min_y,
const std::vector<float>& weight_x,
const std::vector<float>& weight_y)
{
    Clear();

    if (min_x.size() == weight_x.size() && min_y.size() == weight_y.size()) {
        for (auto iter = min_x.begin(); iter != min_x.end(); ++iter)
            cols.push_back(*iter);

        for (auto iter = min_y.begin(); iter != min_y.end(); ++iter)
            rows.push_back(*iter);

        for (auto iter = weight_x.begin(); iter != weight_x.end(); ++iter)
            col_weights.push_back(*iter);

        for (auto iter = weight_y.begin(); iter != weight_y.end(); ++iter)
            row_weights.push_back(*iter);
    }
}

void
Layout::SetConstraints(const std::vector<float>& min_x,
const std::vector<float>& min_y,
const std::vector<float>& weight_x,
const std::vector<float>& weight_y)
{
    Clear();

    if (min_x.size() == weight_x.size() &&
            min_y.size() == weight_y.size()) {

        for (auto iter = min_x.begin(); iter != min_x.end(); ++iter)
            cols.push_back((DWORD) *iter);

        for (auto iter = min_y.begin(); iter != min_y.end(); ++iter)
            rows.push_back((DWORD) *iter);

        for (auto iter = weight_x.begin(); iter != weight_x.end(); ++iter)
            col_weights.push_back(*iter);

        for (auto iter = weight_y.begin(); iter != weight_y.end(); ++iter)
            row_weights.push_back(*iter);
    }
}

void
Layout::SetConstraints(int            ncols,
int            nrows,
const int*     min_x,
const int*     min_y,
const float*   weight_x,
const float*   weight_y)
{
    Clear();

    if (nrows > 0 && ncols > 0) {
        int i = 0;

        for (i = 0; i < ncols; i++) {
            cols.push_back(min_x[i]);
            col_weights.push_back(weight_x[i]);
        }

        for (i = 0; i < nrows; i++) {
            rows.push_back(min_y[i]);
            row_weights.push_back(weight_y[i]);
        }
    }
}