/
conversation.cc
149 lines (140 loc) · 4.12 KB
/
conversation.cc
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
#include "conversation.h"
#include "base/logging.h"
#include <iomanip>
#include <iostream>
#define LINEHEIGHT 20
namespace gui
{
conversation::conversation(rpg::character & c, const u_int16 & w, const u_int16 & h, ::base::functor_0*e)
: ct(w-30, h/3), dlg(c), speaker(w-30, LINEHEIGHT), end(e)
{
Look->init("window.xml");
set_size (w, h);
ct.get_font()->setSize(LINEHEIGHT - 1);
speaker.get_font()->setSize(LINEHEIGHT - 1);
line = dlg.run(-1);
ct.set_multiline(true);
addchild(speaker, 15, 10);
addchild(ct, 15, LINEHEIGHT+10);
opty = 40;
optcount = 0;
update();
color = c.color();
ct.set_color(color);
}
void conversation::selectopt(bool down, void* arg)
{
answer* self = (answer*)arg;
self->obj->line = self->obj->dlg.run(self->which);
self->obj->update();
}
bool conversation::update()
{
while (optcount)
{
optcount--;
removechild(*options[optcount]);
delete options[optcount];
}
optcount = 0;
if (!line) //this is null if we hit the end of the conversation
{
if (end) {
(*end)();
delete end;
end = NULL;
}
//speaker.setString(string(""));
//ct.setString("(End)");
return false;
}
speaker.set_string(string(line->speaker()) + ":");
ct.set_string(line->text());
int i;
int y = opty + ct.height();
optcount = line->num_answers() > MAX_OPTS ? MAX_OPTS : line->num_answers();
font *f = ct.get_font();
//the color from here doesnt work
unsigned int fc = rpg::character::get_player()->color();
LOG(INFO) << logging::indent() << "color changed to 0x"
<< std::setw(8) << std::setfill('0') << std::hex << fc;
f->setColor(fc);
for (i = 0; i < optcount; i++)
{
answers[i].which = i;
answers[i].obj = this;
options[i] = new button(length()-40, LINEHEIGHT);
options[i]->set_callback(::base::make_functor(*this, &conversation::selectopt), (void*)&answers[i]);
options[i]->get_font()->setSize(LINEHEIGHT-1);
options[i]->set_multiline(true);
char tmp[16];
snprintf(tmp, 16, "%i)", i+1);
options[i]->set_string(string(tmp)+line->answer(i));
options[i]->set_center(false, false);
addchild(*options[i], 20, y);
y += options[i]->height() +5;
}
if (optcount == 0)
{
optcount = 1;
answers[0].which = -1;
answers[0].obj = this;
options[0] = new button(length()-40, LINEHEIGHT);
options[0]->set_callback(::base::make_functor(*this, &conversation::selectopt), (void*)&answers[i]);
options[0]->get_font()->setSize(LINEHEIGHT-1);
options[0]->set_multiline(true);
options[0]->set_string("1) (continue)");
options[0]->set_center(false, false);
options[0]->set_color(0xffffffff);
addchild(*options[0], 20, y);
y += options[0]->height() +5;
}
focus();
return true;
}
bool conversation::keyup(input::keyboard_event &k)
{
switch (k.key())
{
case input::keyboard_event::N1_KEY:
case input::keyboard_event::KP1_KEY:
selectopt(true, &answers[0]);
break;
case input::keyboard_event::N2_KEY:
case input::keyboard_event::KP2_KEY:
if (optcount > 0) selectopt(true, &answers[1]);
break;
case input::keyboard_event::N3_KEY:
case input::keyboard_event::KP3_KEY:
if (optcount > 1) selectopt(true, &answers[2]);
break;
case input::keyboard_event::N4_KEY:
case input::keyboard_event::KP4_KEY:
if (optcount > 2) selectopt(true, &answers[3]);
break;
case input::keyboard_event::N5_KEY:
case input::keyboard_event::KP5_KEY:
if (optcount > 3) selectopt(true, &answers[4]);
break;
case input::keyboard_event::N6_KEY:
case input::keyboard_event::KP6_KEY:
if (optcount > 4) selectopt(true, &answers[5]);
break;
case input::keyboard_event::N7_KEY:
case input::keyboard_event::KP7_KEY:
if (optcount > 5) selectopt(true, &answers[6]);
break;
case input::keyboard_event::N8_KEY:
case input::keyboard_event::KP8_KEY:
if (optcount > 6) selectopt(true, &answers[7]);
break;
case input::keyboard_event::N9_KEY:
case input::keyboard_event::KP9_KEY:
if (optcount > 7) selectopt(true, &answers[8]);
break;
default:
return layout::keyup(k);
}
return true;
}
};