File: ComboBox.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 (184 lines) | stat: -rw-r--r-- 4,415 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
/*
 * ComboBox.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 "ComboBox.h"

#include "Slider.h"
#include "Images.h"
#include "TextControls.h"
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"

ComboBox::DropDown::Item::Item(const JsonNode & config, ComboBox::DropDown & _dropDown, Point position)
	: InterfaceObjectConfigurable(LCLICK | HOVER, position),
	dropDown(_dropDown)
{
	build(config);
	
	if(auto w = widget<CIntObject>("hoverImage"))
	{
		pos.w = w->pos.w;
		pos.h = w->pos.h;
		w->disable();
	}
	setRedrawParent(true);
}

void ComboBox::DropDown::Item::updateItem(int idx, const void * _item)
{
	item = _item;
	if(auto w = widget<CLabel>("labelName"))
	{
		if(dropDown.comboBox.getItemText)
			w->setText(dropDown.comboBox.getItemText(idx, item));
	}
}

void ComboBox::DropDown::Item::hover(bool on)
{
	auto h = widget<CIntObject>("hoverImage");
	auto w = widget<CLabel>("labelName");
	if(h && w)
	{
		if(w->getText().empty() || on == false)
			h->disable();
		else
			h->enable();
	}
	redraw();
}

void ComboBox::DropDown::Item::clickPressed(const Point & cursorPosition)
{
	if(isHovered())
		dropDown.setItem(item);
}

void ComboBox::DropDown::Item::clickReleased(const Point & cursorPosition)
{
	dropDown.clickPressed(cursorPosition);
	dropDown.clickReleased(cursorPosition);
}

ComboBox::DropDown::DropDown(const JsonNode & config, ComboBox & _comboBox, Point dropDownPosition):
	InterfaceObjectConfigurable(LCLICK | HOVER),
	comboBox(_comboBox)
{
	REGISTER_BUILDER("item", &ComboBox::DropDown::buildItem);
	
	if(comboBox.onConstructItems)
		comboBox.onConstructItems(curItems);
	
	addCallback("sliderMove", std::bind(&ComboBox::DropDown::sliderMove, this, std::placeholders::_1));
	
	pos = comboBox.pos + dropDownPosition;
	
	build(config);
	
	if(auto w = widget<CSlider>("slider"))
	{
		w->setAmount(curItems.size());
	}

	//FIXME: this should be done by InterfaceObjectConfigurable, but might have side-effects
	pos = children.front()->pos;
	for (auto const & child : children)
		pos = pos.include(child->pos);
	
	updateListItems();
}

std::shared_ptr<ComboBox::DropDown::Item> ComboBox::DropDown::buildItem(const JsonNode & config)
{
	auto position = readPosition(config["position"]);
	items.push_back(std::make_shared<Item>(config, *this, position));
	return items.back();
}

void ComboBox::DropDown::sliderMove(int slidPos)
{
	auto w = widget<CSlider>("slider");
	if(!w)
		return; // ignore spurious call when slider is being created
	updateListItems();
	redraw();
}

bool ComboBox::DropDown::receiveEvent(const Point & position, int eventType) const
{
	if (eventType == LCLICK)
		return true; // we want drop box to close when clicking outside drop box borders

	return CIntObject::receiveEvent(position, eventType);
}

void ComboBox::DropDown::clickPressed(const Point & cursorPosition)
{
	if (!pos.isInside(cursorPosition))
	{
		assert(GH.windows().isTopWindow(this));
		GH.windows().popWindows(1);
	}
}

void ComboBox::DropDown::updateListItems()
{
	int elemIdx = 0;

	if(auto w = widget<CSlider>("slider"))
		elemIdx = w->getValue();

	for(auto item : items)
	{
		if(elemIdx < curItems.size())
		{
			item->updateItem(elemIdx, curItems[elemIdx]);
			elemIdx++;
		}
		else
		{
			item->updateItem(elemIdx);
		}
	}
}

void ComboBox::DropDown::setItem(const void * item)
{
	comboBox.setItem(item);
	
	assert(GH.windows().isTopWindow(this));
	GH.windows().popWindows(1);
}

ComboBox::ComboBox(Point position, const AnimationPath & defName, const std::pair<std::string, std::string> & help, const JsonNode & dropDownDescriptor, Point dropDownPosition, EShortcut key, bool playerColoredButton):
	CButton(position, defName, help, 0, key, playerColoredButton)
{
	addCallback([this, dropDownDescriptor, dropDownPosition]()
	{
		GH.windows().createAndPushWindow<ComboBox::DropDown>(dropDownDescriptor, *this, dropDownPosition);
	});
}

void ComboBox::setItem(const void * item)
{
	auto w = std::dynamic_pointer_cast<CLabel>(getOverlay());
	if( w && getItemText)
		setTextOverlay(getItemText(0, item), w->font, w->color);
	
	if(onSetItem)
		onSetItem(item);
}

void ComboBox::setItem(int id)
{
	std::vector<const void *> tempItems;
	onConstructItems(tempItems);
	setItem(tempItems.at(id));
}