File: popover.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 (201 lines) | stat: -rw-r--r-- 6,030 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
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
// SPDX-License-Identifier: GPL-2.0-or-later

/*
	This file is part of Warzone 2100.
	Copyright (C) 2025  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 popover.
 */

#include "popover.h"
#include "widgbase.h"
#include "form.h"
#include "lib/ivis_opengl/pieblitfunc.h"
#include "lib/framework/input.h"

PopoverWidget::PopoverWidget()
{ }

PopoverWidget::~PopoverWidget()
{
	if (overlayScreen.lock())
	{
		close();
	}
}

std::shared_ptr<PopoverWidget> PopoverWidget::makePopover(const std::shared_ptr<WIDGET>& parent, std::shared_ptr<WIDGET> popoverContents, PopoverWidget::Style style, Alignment align, Vector2i positionOffset, const OnPopoverCloseHandlerFunc& onCloseHandler)
{
	class make_shared_enabler: public PopoverWidget { };
	auto result = std::make_shared<make_shared_enabler>();
	result->style = style;
	result->align = align;
	result->positionOffset = positionOffset;
	result->setGeometry(0, 0, popoverContents->width(), popoverContents->height());
	result->popoverContents = popoverContents;
	result->onCloseHandler = onCloseHandler;
	result->attach(popoverContents);
	result->open(parent);
	return result;
}

void PopoverWidget::repositionOnScreenRelativeToParent(const std::shared_ptr<WIDGET>& parent)
{
	int popOverX0 = 0;
	switch (align)
	{
		case Alignment::LeftOfParent:
		{
			// (Ideally, with its left aligned with the left side of the "parent" widget, but ensure visibility on-screen)
			popOverX0 = parent->screenPosX();
			if (popOverX0 + width() > screenWidth)
			{
				popOverX0 = screenWidth - width();
			}
		}
		break;
		case Alignment::RightOfParent:
		{
			// (Ideally, with its right aligned with the right side of the "parent" widget, but ensure left side visibility on-screen)
			int parentX1 = parent->screenPosX() + parent->width();
			popOverX0 = parentX1 - width();
			if (popOverX0 < 0)
			{
				popOverX0 = 0;
			}
		}
		break;
	}
	// (Ideally, with its top aligned with the bottom of the "parent" widget, but ensure full visibility on-screen)
	int popOverY0 = parent->screenPosY() + parent->height();
	if (popOverY0 + height() > screenHeight)
	{
		popOverY0 = screenHeight - height();
	}
	move(popOverX0 + positionOffset.x, popOverY0 + positionOffset.y);
}

void PopoverWidget::geometryChanged()
{
	if (popoverContents)
	{
		popoverContents->setGeometry(0, 0, width(), height());
	}
}

void PopoverWidget::open(const std::shared_ptr<WIDGET>& parent)
{
	if (overlayScreen.lock() != nullptr) { return; }
	std::shared_ptr<PopoverWidget> popoverWidget(std::dynamic_pointer_cast<PopoverWidget>(shared_from_this()));
	std::weak_ptr<WIDGET> weakParent = parent;

	widgScheduleTask([popoverWidget, weakParent]() {
		auto strongParent = weakParent.lock();

		auto lockedScreen = (strongParent) ? strongParent->screenPointer.lock() : nullptr;
		ASSERT(lockedScreen != nullptr, "The parent does not have an associated screen pointer?");

		auto psOverlayScreen = W_SCREEN::make();
		psOverlayScreen->psForm->hide();

		auto newRootFrm = W_FULLSCREENOVERLAY_CLICKFORM::make();
		newRootFrm->displayFunction = displayChildDropShadows;
		std::weak_ptr<W_SCREEN> psWeakOverlayScreen(psOverlayScreen);
		std::weak_ptr<PopoverWidget> pWeakThis(popoverWidget);
		newRootFrm->onClickedFunc = [psWeakOverlayScreen, pWeakThis]() {
			if (auto popoverWidget = pWeakThis.lock())
			{
				popoverWidget->close();
			}
			if (auto psOverlayScreen = psWeakOverlayScreen.lock())
			{
				widgRemoveOverlayScreen(psOverlayScreen);
			}
		};
		newRootFrm->onCancelPressed = newRootFrm->onClickedFunc;
		if (popoverWidget->style == Style::NonInteractive)
		{
			newRootFrm->setTransparentToMouse(true);
			newRootFrm->setCustomHitTest([](const WIDGET *psWidget, int x, int y) -> bool {
				return false; // effectively: make this (and all of its children) unclickable
			});
		}
		psOverlayScreen->psForm->attach(newRootFrm);

		popoverWidget->overlayScreen = psOverlayScreen;
		newRootFrm->attach(popoverWidget);

		popoverWidget->setCalcLayout([weakParent](WIDGET *psWidget) {
			auto psParent = weakParent.lock();
			ASSERT_OR_RETURN(, psParent != nullptr, "parent is null");
			auto psPopoverWidget = std::static_pointer_cast<PopoverWidget>(psWidget->shared_from_this());
			psPopoverWidget->repositionOnScreenRelativeToParent(psParent);
		});

		widgRegisterOverlayScreenOnTopOfScreen(psOverlayScreen, lockedScreen);
	});
}

void PopoverWidget::closeImpl()
{
	auto strongOverlay = overlayScreen.lock();
	if (strongOverlay == nullptr)
	{
		return;
	}
	widgRemoveOverlayScreen(strongOverlay);
	overlayScreen.reset();
	if (onCloseHandler)
	{
		onCloseHandler();
	}
}

void PopoverWidget::close()
{
	// because open() uses widgScheduleTask to actually create the overlay screen,
	// widgScheduleTask must also be used to close it (otherwise, overlayScreen might be accessed before the scheduled open task creates it)
	std::shared_ptr<PopoverWidget> popoverWidget(std::dynamic_pointer_cast<PopoverWidget>(shared_from_this()));
	widgScheduleTask([popoverWidget]() {
		popoverWidget->closeImpl();
	});
}

void PopoverWidget::display(int xOffset, int yOffset)
{
	// currently, no-op
}

int32_t PopoverWidget::idealWidth()
{
	if (!popoverContents)
	{
		return 0;
	}
	return popoverContents->idealWidth();
}

int32_t PopoverWidget::idealHeight()
{
	if (!popoverContents)
	{
		return 0;
	}
	return popoverContents->idealHeight();
}