File: gridlayout.cpp

package info (click to toggle)
warzone2100 4.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,348 kB
  • sloc: cpp: 675,711; ansic: 387,204; javascript: 75,107; python: 16,628; php: 4,294; sh: 3,941; makefile: 2,330; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (166 lines) | stat: -rw-r--r-- 4,057 bytes parent folder | download | duplicates (2)
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
/*
	This file is part of Warzone 2100.
	Copyright (C) 2021  Warzone 2100 Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "gridlayout.h"
#include "widgbase.h"
#include "lib/ivis_opengl/pieblitfunc.h"
#include <algorithm>
#include <nonstd/optional.hpp>
#include <map>
#include <set>

GridLayout::GridLayout(): WIDGET()
{
}

void GridLayout::place(grid_allocation::slot column, grid_allocation::slot row, std::shared_ptr<WIDGET> widget)
{
	attach(widget);
	placements.emplace_back(GridLayout::Placement {column, row, widget});
	invalidateAllocation();
}

int32_t GridLayout::idealWidth()
{
	return getColumnAllocator().get_minimum_size();
}

int32_t GridLayout::idealHeight()
{
	return getRowAllocator().get_minimum_size();
}

nonstd::optional<std::vector<uint32_t>> GridLayout::getScrollSnapOffsets()
{
	updateLayout();
	return scrollSnapOffsets;
}

void GridLayout::displayRecursive(WidgetGraphicsContext const &context)
{
	updateLayout();
	auto overrideContext = context
		.setAllowChildDisplayRecursiveIfSelfClipped(true);
	WIDGET::displayRecursive(overrideContext);
}

void GridLayout::geometryChanged()
{
	invalidateLayout();
}

void GridLayout::updateLayout()
{
	if (!layoutDirty)
	{
		return;
	}

	layoutDirty = false;

	columnOffsets = getColumnAllocator().calculate_offsets(width());
	rowOffsets = getRowAllocator().calculate_offsets(height());
	scrollSnapOffsets.resize(rowOffsets.size());
	size_t i = 0;
	for (auto offset: rowOffsets)
	{
		scrollSnapOffsets[i++] = offset.second;
	}

	for (auto &placement: placements)
	{
		auto left = columnOffsets[placement.column.start];
		auto right = columnOffsets[placement.column.start + placement.column.span];
		auto top = rowOffsets[placement.row.start];
		auto bottom = rowOffsets[placement.row.start + placement.row.span];
		placement.widget->setGeometry(left, top, right - left, bottom - top);
	}
}

const std::map<uint32_t, int32_t> &GridLayout::getColumnOffsets()
{
	updateLayout();
	return columnOffsets;
}

const std::map<uint32_t, int32_t> &GridLayout::getRowOffsets()
{
	updateLayout();
	return rowOffsets;
}

void GridLayout::invalidateLayout()
{
	layoutDirty = true;
}

void GridLayout::invalidateAllocation()
{
	columnAllocation.reset();
	rowAllocation.reset();
	invalidateLayout();
}

std::vector<grid_allocation::item> GridLayout::getAllocationItems(PlacementTransformer placementTransformer)
{
	auto items = std::vector<grid_allocation::item>();
	for (const auto &placement: placements)
	{
		items.push_back(placementTransformer(placement));
	}

	return items;
}

grid_allocation::item GridLayout::placementColumn(GridLayout::Placement placement)
{
	return {placement.column, placement.widget->idealWidth()};
}

grid_allocation::item GridLayout::placementRow(GridLayout::Placement placement)
{
	return {placement.row, placement.widget->idealHeight()};
}

GridLayout::Placement::Placement(grid_allocation::slot column, grid_allocation::slot row, std::shared_ptr<WIDGET> widget)
	: column(column)
	, row(row)
	, widget(widget)
{
}

grid_allocation::allocator &GridLayout::getColumnAllocator()
{
	if (!columnAllocation)
	{
		columnAllocation.emplace(getAllocationItems(GridLayout::placementColumn));
	}

	return columnAllocation.value();
}

grid_allocation::allocator &GridLayout::getRowAllocator()
{
	if (!rowAllocation)
	{
		rowAllocation.emplace(getAllocationItems(GridLayout::placementRow));
	}

	return rowAllocation.value();
}