File: ReplyMenu.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (148 lines) | stat: -rw-r--r-- 4,763 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * This code is based on the CRAB engine
 *
 * Copyright (c) Arvind Raja Yadav
 *
 * Licensed under MIT
 *
 */

#include "crab/XMLDoc.h"
#include "crab/ui/ReplyMenu.h"

namespace Crab {

using namespace pyrodactyl::ui;
using namespace pyrodactyl::text;
using namespace pyrodactyl::image;
using namespace pyrodactyl::input;
using namespace pyrodactyl::music;
using namespace pyrodactyl::event;
using namespace pyrodactyl::people;

void ReplyMenu::load(const Common::Path &filename) {
	XMLDoc conf(filename);
	if (conf.ready()) {
		rapidxml::xml_node<char> *node = conf.doc()->first_node("conversation");
		if (nodeValid(node)) {
			if (nodeValid("tone", node))
				_tone.load(node->first_node("tone"));

			if (nodeValid("reply", node)) {
				rapidxml::xml_node<char> *replynode = node->first_node("reply");
				Menu<ReplyButton>::load(replynode->first_node("menu"));
				_tone._value.resize(_element.size());

				_bg.load(replynode->first_node("bg"));
				loadNum(_spacing, "spacing", replynode);
			}
		}
	}
}

int ReplyMenu::handleEvents(Info &info, ConversationData &dat, const Common::String &curId, PersonHandler &oh, const Common::Event &event) {
	// After that, check if the user has clicked on any reply option
	int choice = Menu<ReplyButton>::handleEvents(event);
	if (choice >= 0 && (uint)choice < dat._reply.size()) {
		bool playSound = false;

		// Loop through any opinion changes required
		for (auto &i : dat._reply[_element[choice]._index]._change) {
			if (i._id == curId) {
				// This is a special case because we also need to update the opinion bars
				oh.opinionChange(info, i._id, OPI_LIKE, i._val[OPI_LIKE]);
				oh.opinionChange(info, i._id, OPI_RESPECT, i._val[OPI_RESPECT]);
				oh.opinionChange(info, i._id, OPI_FEAR, i._val[OPI_FEAR]);
				playSound = true;
			} else {
				info.opinionChange(i._id, OPI_LIKE, i._val[OPI_LIKE]);
				info.opinionChange(i._id, OPI_RESPECT, i._val[OPI_RESPECT]);
				info.opinionChange(i._id, OPI_FEAR, i._val[OPI_FEAR]);
				playSound = true;
			}
		}

		// Right now we play sound randomly
		if (playSound) {
			if (g_engine->getRandomNumber(1))
				info._sound._repDec = true;
			else
				info._sound._repInc = true;
		}

		return dat._reply[_element[choice]._index]._nextid;
	}

	return -1;
}

void ReplyMenu::draw() {
	_bg.draw();
	_tone.draw(_hoverIndex);

	// Draw the reply options
	Menu<ReplyButton>::draw();
}

void ReplyMenu::cache(pyrodactyl::event::Info &info, pyrodactyl::event::ConversationData &dat) {
	// Some replies are locked, which means the other replies move up and take their place -
	// which is why we need two count variables
	uint replyCount = 0, elementCount = 0;

	for (auto i = dat._reply.begin(); i != dat._reply.end() && replyCount < dat._reply.size(); ++i, ++replyCount) {
		if (i->_unlock.evaluate(info)) {
			_element[elementCount]._visible = true;
			_element[elementCount]._index = replyCount;

			_tone._value[elementCount] = dat._reply[replyCount]._tone;

			const InputType type = static_cast<InputType>(IU_REPLY_0 + elementCount);
			Common::String text = g_engine->_inputManager->getAssociatedKey(type);
			text += ". " + i->_text;
			info.insertName(text);

			if (elementCount == 0)
				_element[elementCount].cache(text, _spacing, 0, &_bg);
			else
				_element[elementCount].cache(text, _spacing, _element[elementCount - 1].y + _element[elementCount - 1].h, &_bg);

			// Increment the element count only if the reply is unlocked
			// This means we will keep checking against element 0 until we find an unlocked reply
			// e.g. if replies 0,1,2 are locked, then element[0] will get reply[3]
			elementCount++;
		}
	}

	// Unused element buttons are hidden
	for (; elementCount < _element.size(); elementCount++)
		_element[elementCount]._visible = false;
}

void ReplyMenu::setUI() {
	Menu<ReplyButton>::setUI();
	_bg.setUI();
	_tone.setUI();
}

} // End of namespace Crab