File: cliprect.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 (118 lines) | stat: -rw-r--r-- 3,234 bytes parent folder | download
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
/*
	This file is part of Warzone 2100.
	Copyright (C) 2020  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
*/
/**
 * @file
 * Functions for clip rect.
 *
 * This widget will render only the childrens that are visible, and whose rectangle is contained in the clip rect's rectangle.
 */

#include "cliprect.h"
#include "lib/ivis_opengl/pieblitfunc.h"

void ClipRectWidget::runRecursive(W_CONTEXT *psContext)
{
	W_CONTEXT newContext(psContext);
	newContext.xOffset = psContext->xOffset + x();
	newContext.yOffset = psContext->yOffset + y();
	newContext.mx = psContext->mx - x();
	newContext.my = psContext->my - y() + offset.y;

	WIDGET::runRecursive(&newContext);
}

std::shared_ptr<WIDGET> ClipRectWidget::findMouseTargetRecursive(W_CONTEXT *psContext, WIDGET_KEY key, bool wasPressed)
{
	W_CONTEXT newContext(psContext);
	newContext.xOffset = psContext->xOffset - offset.x;
	newContext.yOffset = psContext->yOffset - offset.y;
	newContext.mx = psContext->mx + offset.x;
	newContext.my = psContext->my + offset.y;

	auto result = WIDGET::findMouseTargetRecursive(&newContext, key, wasPressed);
	if (result != nullptr)
	{
		*psContext = newContext; // bubble-up the matching hit's context
	}
	return result;
}

void ClipRectWidget::displayRecursive(const WidgetGraphicsContext &context)
{
	if (context.clipContains(geometry()))
	{
		display(context.getXOffset(), context.getYOffset());
	}

	auto childrenContext = context
		.translatedBy(x() - offset.x, + y() - offset.y)
		.clippedBy(WzRect(offset.x, offset.y, width(), height()));

	for (auto child : children())
	{
		if (child->visible())
		{
			child->displayRecursive(childrenContext);
		}
	}
}

bool ClipRectWidget::isChildVisible(const std::shared_ptr<WIDGET>& child)
{
	ASSERT_OR_RETURN(false, child->parent() == shared_from_this(), "Not a child of this widget?");
	WidgetGraphicsContext childrenContext = WidgetGraphicsContext()
		.translatedBy(screenPosX(), screenPosY())
		.clippedBy(WzRect(offset.x, offset.y, width(), height()));
	return childrenContext.clipContains(child->geometry());
}

bool ClipRectWidget::setTopOffset(uint16_t value)
{
	if (value == offset.y)
	{
		return false;
	}
	offset.y = value;
	return true;
}

uint16_t ClipRectWidget::getTopOffset()
{
	return offset.y;
}

bool ClipRectWidget::setLeftOffset(uint16_t value)
{
	if (value == offset.x)
	{
		return false;
	}
	offset.x = value;
	return true;
}

int ClipRectWidget::parentRelativeXOffset(int coord) const
{
	return x() - offset.x + coord;
}

int ClipRectWidget::parentRelativeYOffset(int coord) const
{
	return y() - offset.y + coord;
}