File: CComponentHolder.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 (314 lines) | stat: -rw-r--r-- 8,515 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * CComponentHolder.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 "CComponentHolder.h"

#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"

#include "CComponent.h"
#include "Images.h"

#include "../render/Canvas.h"
#include "../render/Colors.h"
#include "../render/IRenderHandler.h"
#include "../CPlayerInterface.h"
#include "../CGameInfo.h"

#include "../../CCallback.h"
#include "../../lib/texts/CGeneralTextHandler.h"
#include "../../lib/ArtifactUtils.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/networkPacks/ArtifactLocation.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/CSkillHandler.h"

CComponentHolder::CComponentHolder(const Rect & area, const Point & selectionOversize)
	: SelectableSlot(area, selectionOversize)
{
	setClickPressedCallback([this](const CComponentHolder &, const Point & cursorPosition)
		{
			if(text.size())
				LRClickableAreaWTextComp::clickPressed(cursorPosition);
		});
	setShowPopupCallback([this](const CComponentHolder &, const Point & cursorPosition)
		{
			if(text.size())
				LRClickableAreaWTextComp::showPopupWindow(cursorPosition);
		});
}

void CComponentHolder::setClickPressedCallback(const ClickFunctor & callback)
{
	clickPressedCallback = callback;
}

void CComponentHolder::setShowPopupCallback(const ClickFunctor & callback)
{
	showPopupCallback = callback;
}

void CComponentHolder::setGestureCallback(const ClickFunctor & callback)
{
	gestureCallback = callback;
}

void CComponentHolder::clickPressed(const Point & cursorPosition)
{
	if(clickPressedCallback)
		clickPressedCallback(*this, cursorPosition);
}

void CComponentHolder::showPopupWindow(const Point & cursorPosition)
{
	if(showPopupCallback)
		showPopupCallback(*this, cursorPosition);
}

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

	if(gestureCallback)
		gestureCallback(*this, initialPosition);
}

CArtPlace::CArtPlace(Point position, const ArtifactID & artId, const SpellID & spellId)
	: CComponentHolder(Rect(position, Point(44, 44)), Point(1, 1))
	, locked(false)
	, imageIndex(0)
{
	OBJECT_CONSTRUCTION;

	image = std::make_shared<CAnimImage>(AnimationPath::builtin("artifact"), 0);
	setArtifact(artId, spellId);
	moveSelectionForeground();
}

void CArtPlace::setArtifact(const SpellID & newSpellId)
{
	setArtifact(ArtifactID::SPELL_SCROLL, newSpellId);
}

void CArtPlace::setArtifact(const ArtifactID & newArtId, const SpellID & newSpellId)
{
	artId = newArtId;
	if(artId == ArtifactID::NONE)
	{
		image->disable();
		text.clear();
		lockSlot(false);
		return;
	}

	const auto artType = artId.toArtifact();
	imageIndex = artType->getIconIndex();
	if(artId == ArtifactID::SPELL_SCROLL)
	{
		spellId = newSpellId;
		assert(spellId.num > 0);

		if(settings["general"]["enableUiEnhancements"].Bool())
		{
			imageIndex = spellId.num;
			if(component.type != ComponentType::SPELL_SCROLL)
			{
				image->setScale(Point(pos.w, 34));
				image->setAnimationPath(AnimationPath::builtin("spellscr"), imageIndex);
				image->moveTo(Point(pos.x, pos.y + 4));
			}
		}
		// Add spell component info (used to provide a pic in r-click popup)
		component.type = ComponentType::SPELL_SCROLL;
		component.subType = spellId;
	}
	else
	{
		if(settings["general"]["enableUiEnhancements"].Bool() && component.type != ComponentType::ARTIFACT)
		{
			image->setScale(Point());
			image->setAnimationPath(AnimationPath::builtin("artifact"), imageIndex);
			image->moveTo(Point(pos.x, pos.y));
		}
		component.type = ComponentType::ARTIFACT;
		component.subType = artId;
	}
	image->enable();
	lockSlot(locked);

	text = artType->getDescriptionTranslated();
	if(artType->isScroll())
		ArtifactUtils::insertScrrollSpellName(text, spellId);
}

ArtifactID CArtPlace::getArtifactId() const
{
	return artId;
}

CCommanderArtPlace::CCommanderArtPlace(Point position, const CGHeroInstance * commanderOwner, ArtifactPosition artSlot,
	const ArtifactID & artId, const SpellID & spellId)
	: CArtPlace(position, artId, spellId),
	commanderOwner(commanderOwner),
	commanderSlotID(artSlot.num)
{
}

void CCommanderArtPlace::returnArtToHeroCallback()
{
	ArtifactPosition artifactPos = commanderSlotID;
	ArtifactPosition freeSlot = ArtifactUtils::getArtBackpackPosition(commanderOwner, getArtifactId());
	if(freeSlot == ArtifactPosition::PRE_FIRST)
	{
		LOCPLINT->showInfoDialog(CGI->generaltexth->translate("core.genrltxt.152"));
	}
	else
	{
		ArtifactLocation src(commanderOwner->id, artifactPos);
		src.creature = SlotID::COMMANDER_SLOT_PLACEHOLDER;
		ArtifactLocation dst(commanderOwner->id, freeSlot);

		if(getArtifactId().toArtifact()->canBePutAt(commanderOwner, freeSlot, true))
		{
			LOCPLINT->cb->swapArtifacts(src, dst);
			setArtifact(ArtifactID(ArtifactID::NONE));
			parent->redraw();
		}
	}
}

void CCommanderArtPlace::clickPressed(const Point & cursorPosition)
{
	if(getArtifactId() != ArtifactID::NONE && text.size())
		LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.commanderWindow.artifactMessage"), [this]() { returnArtToHeroCallback(); }, []() {});
}

void CCommanderArtPlace::showPopupWindow(const Point & cursorPosition)
{
	if(getArtifactId() != ArtifactID::NONE && text.size())
		CArtPlace::showPopupWindow(cursorPosition);
}

void CArtPlace::lockSlot(bool on)
{
	locked = on;
	if(on)
	{
		image->setFrame(ArtifactID::ART_LOCK);
		hoverText = CGI->generaltexth->allTexts[507];
	}
	else if(artId != ArtifactID::NONE)
	{
		image->setFrame(imageIndex);
		auto hoverText = MetaString::createFromRawString(CGI->generaltexth->heroscrn[1]);
		hoverText.replaceName(artId);
		this->hoverText = hoverText.toString();
	}
	else
	{
		hoverText = CGI->generaltexth->allTexts[507];
	}
}

bool CArtPlace::isLocked() const
{
	return locked;
}

void CArtPlace::addCombinedArtInfo(const std::map<const ArtifactID, std::vector<ArtifactID>> & arts)
{
	for(auto [combinedId, availableArts] : arts)
	{
		const auto combinedArt = combinedId.toArtifact();
		MetaString info;
		info.appendEOL();
		info.appendEOL();
		info.appendRawString("{");
		info.appendName(combinedArt->getId());
		info.appendRawString("}");
		info.appendRawString(" (%d/%d)");
		info.replaceNumber(availableArts.size());
		info.replaceNumber(combinedArt->getConstituents().size());
		for(const auto part : combinedArt->getConstituents())
		{
			const auto found = std::find_if(availableArts.begin(), availableArts.end(), [part](const auto & availablePart) -> bool
				{
					return availablePart == part->getId() ? true : false;
				});

			info.appendEOL();
			if(found < availableArts.end())
			{
				info.appendName(part->getId());
				availableArts.erase(found);
			}
			else
			{
				info.appendRawString("{#A9A9A9|");
				info.appendName(part->getId());
				info.appendRawString("}");
			}
		}
		text += info.toString();
	}
}

CSecSkillPlace::CSecSkillPlace(const Point & position, const ImageSize & imageSize, const SecondarySkill & newSkillId, const uint8_t level)
	: CComponentHolder(Rect(position, Point()), Point())
{
	OBJECT_CONSTRUCTION;

	auto imagePath = AnimationPath::builtin("SECSK82");
	if(imageSize == ImageSize::MEDIUM)
		imagePath = AnimationPath::builtin("SECSKILL");
	if(imageSize == ImageSize::SMALL)
		imagePath = AnimationPath::builtin("SECSK32");

	image = std::make_shared<CAnimImage>(imagePath, 0);
	component.type = ComponentType::SEC_SKILL;
	pos.w = image->pos.w;
	pos.h = image->pos.h;
	setSkill(newSkillId, level);
}

void CSecSkillPlace::setSkill(const SecondarySkill & newSkillId, const uint8_t level)
{
	skillId = newSkillId;
	component.subType = newSkillId;
	setLevel(level);
}

void CSecSkillPlace::setLevel(const uint8_t level)
{
	// 0 - none
	// 1 - base
	// 2 - advanced
	// 3 - expert
	assert(level <= 3);
	if(skillId != SecondarySkill::NONE && level > 0)
	{
		const auto secSkill = skillId.toSkill();
		image->setFrame(secSkill->getIconIndex(level - 1));
		image->enable();
		auto hoverText = MetaString::createFromRawString(CGI->generaltexth->heroscrn[21]);
		hoverText.replaceRawString(CGI->generaltexth->levels[level - 1]);
		hoverText.replaceTextID(secSkill->getNameTextID());
		this->hoverText = hoverText.toString();
		component.value = level;
		text = secSkill->getDescriptionTranslated(level);
	}
	else
	{
		image->disable();
		hoverText.clear();
		text.clear();
	}
}