File: RadialMenu.cpp

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 (146 lines) | stat: -rw-r--r-- 3,974 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
/*
 * RadialMenu.cpp, 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
 *
 */
#include "StdInc.h"
#include "RadialMenu.h"

#include "Images.h"
#include "TextControls.h"

#include "../eventsSDL/InputHandler.h"
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
#include "../render/IImage.h"
#include "../CGameInfo.h"

#include "../../lib/texts/CGeneralTextHandler.h"

RadialMenuItem::RadialMenuItem(const std::string & imageName, const std::string & hoverText, const std::function<void()> & callback, bool alternativeLayout)
	: callback(callback)
	, hoverText(hoverText)
{
	OBJECT_CONSTRUCTION;

	inactiveImage = std::make_shared<CPicture>(ImagePath::builtin(alternativeLayout ? "radialMenu/itemInactiveAlt" : "radialMenu/itemInactive"), Point(0, 0));
	selectedImage = std::make_shared<CPicture>(ImagePath::builtin(alternativeLayout ? "radialMenu/itemEmptyAlt" : "radialMenu/itemEmpty"), Point(0, 0));

	iconImage = std::make_shared<CPicture>(ImagePath::builtin("radialMenu/" + imageName), Point(0, 0));

	pos = selectedImage->pos;
	selectedImage->setEnabled(false);
}

void RadialMenuItem::setSelected(bool selected)
{
	selectedImage->setEnabled(selected);
	inactiveImage->setEnabled(!selected);
}

RadialMenu::RadialMenu(const Point & positionToCenter, const std::vector<RadialMenuConfig> & menuConfig, bool alternativeLayout):
	centerPosition(positionToCenter), alternativeLayout(alternativeLayout)
{
	OBJECT_CONSTRUCTION;
	pos += positionToCenter;

	Point itemSize = alternativeLayout ? Point(80, 70) : Point(70, 80);
	moveBy(-itemSize / 2);
	pos.w = itemSize.x;
	pos.h = itemSize.y;

	for (auto const & item : menuConfig)
		addItem(item.itemPosition, item.enabled, item.imageName, item.hoverText, item.callback);

	statusBar = CGStatusBar::create(-80, -100, ImagePath::builtin("radialMenu/statusBar"));

	for(const auto & item : items)
		pos = pos.include(item->pos);
	pos = pos.include(statusBar->pos);

	fitToScreen(10);

	addUsedEvents(GESTURE);
}

void RadialMenu::addItem(const Point & offset, bool enabled, const std::string & path, const std::string & hoverText, const std::function<void()>& callback )
{
	if (!enabled)
		return;

	auto item = std::make_shared<RadialMenuItem>(path, CGI->generaltexth->translate(hoverText), callback, alternativeLayout);

	item->moveBy(offset);

	items.push_back(item);
}

std::shared_ptr<RadialMenuItem> RadialMenu::findNearestItem(const Point & cursorPosition) const
{
	static const int requiredDistanceFromCenter = 45;

	// cursor is inside centeral area -> no selection
	if ((centerPosition - cursorPosition).length() < requiredDistanceFromCenter)
		return nullptr;

	int bestDistance = std::numeric_limits<int>::max();
	std::shared_ptr<RadialMenuItem> bestItem;

	for(const auto & item : items)
	{
		Point vector = item->pos.center() - cursorPosition;

		if (vector.length() < bestDistance)
		{
			bestDistance = vector.length();
			bestItem = item;
		}
	}

	assert(bestItem);
	return bestItem;
}

void RadialMenu::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
{
	auto newSelection = findNearestItem(currentPosition);


	if (newSelection != selectedItem)
	{
		if (selectedItem)
			selectedItem->setSelected(false);

		if (newSelection)
		{
			GH.statusbar()->write(newSelection->hoverText);
			newSelection->setSelected(true);
		}
		else
			GH.statusbar()->clear();

		selectedItem = newSelection;

		GH.windows().totalRedraw();
	}
}

void RadialMenu::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
{
	if (on)
		return;

	auto item = findNearestItem(finalPosition);

	// we need to close this window first so if action spawns a new window it won't be closed instead
	GH.windows().popWindows(1);
	if (item)
	{
		GH.input().hapticFeedback();
		item->callback();
	}
}