File: WindowHandler.h

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (108 lines) | stat: -rw-r--r-- 3,358 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
/*
 * WindowHandler.h, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#pragma once

class IShowActivatable;

class WindowHandler
{
	/// list of windows. front = bottom-most (background), back = top-most (foreground)
	/// (includes adventure map, window windows, all kind of active dialogs, and so on)
	std::vector<std::shared_ptr<IShowActivatable>> windowsStack;

	/// Temporary list of recently popped windows
	std::vector<std::shared_ptr<IShowActivatable>> disposed;

	bool totalRedrawRequested = false;

	/// returns top windows
	std::shared_ptr<IShowActivatable> topWindowImpl() const;

	/// forces total redraw (using showAll), sets a flag, method gets called at the end of the rendering
	void totalRedrawImpl();

	/// update only top windows and draw background from buffer, sets a flag, method gets called at the end of the rendering
	void simpleRedrawImpl();

public:
	/// forces total redraw (using showAll), sets a flag, method gets called at the end of the rendering
	void totalRedraw();

	/// update only top windows and draw background from buffer, sets a flag, method gets called at the end of the rendering
	void simpleRedraw();

	/// called whenever user selects different resolution, requiring to center/resize all windows
	void onScreenResize();

	/// deactivate old top windows, activates this one and pushes to the top
	void pushWindow(std::shared_ptr<IShowActivatable> newInt);

	/// creates window of class T and pushes it to the top
	template <typename T, typename ... Args>
	void createAndPushWindow(Args && ... args);

	/// pops one or more windows - deactivates top, deletes and removes given number of windows, activates new front
	void popWindows(int howMany);

	/// returns true if current top window is a right-click popup
	bool isTopWindowPopup() const;

	/// removes given windows from the top and activates next
	void popWindow(std::shared_ptr<IShowActivatable> top);

	/// returns true if selected interface is on top
	bool isTopWindow(std::shared_ptr<IShowActivatable> window) const;
	bool isTopWindow(IShowActivatable * window) const;

	/// returns top window if it matches requested class
	template <typename T>
	std::shared_ptr<T> topWindow() const;

	/// should be called after frame has been rendered to screen
	void onFrameRendered();

	/// returns current number of windows in the stack
	size_t count() const;

	/// erases all currently existing windows from the stack
	void clear();

	/// returns all existing windows of selected type
	template <typename T>
	std::vector<std::shared_ptr<T>> findWindows() const;
};

template <typename T, typename ... Args>
void WindowHandler::createAndPushWindow(Args && ... args)
{
	auto newWindow = std::make_shared<T>(std::forward<Args>(args)...);
	pushWindow(newWindow);
}

template <typename T>
std::vector<std::shared_ptr<T>> WindowHandler::findWindows() const
{
	std::vector<std::shared_ptr<T>> result;

	for(const auto & window : windowsStack)
	{
		std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(window);

		if (casted)
			result.push_back(casted);
	}
	return result;
}

template <typename T>
std::shared_ptr<T> WindowHandler::topWindow() const
{
	return std::dynamic_pointer_cast<T>(topWindowImpl());
}