forked from acaudwell/Gource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.cpp
416 lines (296 loc) · 10.6 KB
/
user.cpp
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
Copyright (C) 2009 Andrew Caudwell (acaudwell@gmail.com)
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/>.
*/
#include "user.h"
float gGourceBeamDist = 100.0;
float gGourceActionDist = 50.0;
float gGourcePersonalSpaceDist = 100.0;
RUser::RUser(const std::string& name, vec2 pos, int tagid) : Pawn(name,pos,tagid) {
this->name = name;
speed = gGourceSettings.max_user_speed;
size = 20.0 * gGourceSettings.user_scale;
shadowOffset = vec2(2.0, 2.0) * gGourceSettings.user_scale;
shadow = true;
highlighted=false;
assignUserImage();
setSelected(false);
last_action = 0.0;
action_interval = 0.2;
name_interval = 5.0;
min_units_ps = 100.0;
actionCount = activeCount = 0;
}
void RUser::addAction(RAction* action) {
if(action->source != this) return;
if(isIdle()) showName();
//name_interval = name_interval > 0.0 ? std::max(name_interval,nametime-1.0f) : nametime;
actions.push_back(action);
actionCount++;
}
// remove references to this file
void RUser::fileRemoved(RFile* f) {
for(std::list<RAction*>::iterator it = actions.begin(); it != actions.end(); ) {
RAction* a = *it;
if(a->target == f) {
it = actions.erase(it);
delete a;
actionCount--;
continue;
}
it++;
}
for(std::list<RAction*>::iterator it = activeActions.begin(); it != activeActions.end(); ) {
RAction* a = *it;
if(a->target == f) {
it = activeActions.erase(it);
delete a;
continue;
}
it++;
}
}
void RUser::applyForceUser(RUser* u) {
if(u==this) return;
vec2 u_pos = u->getPos();
vec2 dir = u_pos - pos;
float dist = glm::length(dir);
//different repelling force depending on how busy the user is
float desired_dist = getActionCount() == 0 ?
gGourcePersonalSpaceDist : (!actions.empty() && activeActions.empty()) ?
gGourcePersonalSpaceDist * 0.1 : gGourcePersonalSpaceDist * 0.5;
//resolve overlap
if(dist < 0.001) {
accel += 1.0f * normalise(vec2( (rand() % 100) - 50, (rand() % 100) - 50));
return;
}
//repelling force
if(dist < desired_dist) {
accel -= (desired_dist-dist) * normalise(dir);
}
}
void RUser::applyForceAction(RAction* action) {
RFile* f = action->target;
vec2 f_pos = f->getAbsolutePos();
vec2 dir = f_pos - pos;
float dist = glm::length(dir);
float desired_dist = gGourceActionDist;
//resolve overlap
if(dist < 0.001) {
accel += normalise(vec2( (rand() % 100) - 50, (rand() % 100) - 50));
return;
}
//repelling force
if(dist < desired_dist) {
accel -= (desired_dist - dist) * normalise(dir);
return;
}
if(dist > gGourceBeamDist) {
accel += (dist-gGourceBeamDist) * normalise(dir);
}
}
void RUser::applyForceToActions() {
if(activeActions.empty() && actions.empty()) return;
last_action = elapsed;
int action_influence = 0;
int max_influence = 3;
// move towards actions being worked on
for(std::list<RAction*>::iterator it = activeActions.begin(); it != activeActions.end(); it++) {
RAction* action = *it;
applyForceAction(action);
action_influence++;
if(action_influence >= max_influence) break;
}
if(!activeActions.empty()) return;
//if no actions being worked on, move towards one pending action
for(std::list<RAction*>::iterator it = actions.begin(); it != actions.end(); it++) {
RAction* action = *it;
applyForceAction(action);
break;
}
}
void RUser::colourize() {
usercol = colourHash(name);
}
void RUser::assignUserImage() {
colourize();
TextureResource* graphic = 0;
if(gGourceSettings.user_image_dir.size() > 0) {
//try their username
// TODO: replace with map of name -> image of all pngs and jpgs in directory
//gGourceSettings.user_image_dir + name + std::string(".jpg");
std::map<std::string, std::string>::iterator findimage;
findimage = gGourceSettings.user_image_map.find(name);
//do we have this image
if(findimage != gGourceSettings.user_image_map.end()) {
std::string imagefile = findimage->second;
if(!gGourceSettings.colour_user_images) usercol = vec3(1.0, 1.0, 1.0);
graphic = texturemanager.grabFile(imagefile, true, GL_CLAMP_TO_EDGE);
}
}
//TODO: trilinear probably should be an attribute of the texture
// perhaps the mipmap option should be an enum: eg TEX_MIPMAP_TRILINEAR
//nope
if(!graphic) {
if(gGourceSettings.default_user_image.size() > 0) {
if(!gGourceSettings.colour_user_images) usercol = vec3(1.0, 1.0, 1.0);
graphic = texturemanager.grabFile(gGourceSettings.default_user_image, true, GL_CLAMP_TO_EDGE);
} else {
graphic = texturemanager.grab("user.png", true, GL_CLAMP_TO_EDGE);
}
}
setGraphic(graphic);
usercol = usercol * 0.6f + vec3(1.0f) * 0.4f;
usercol *= 0.9f;
}
int RUser::getActionCount() {
return actionCount + activeCount;
}
int RUser::getPendingActionCount() {
return actionCount;
}
void RUser::logic(float t, float dt) {
Pawn::logic(dt);
action_interval -= dt;
bool find_nearby_action = false;
if(!actions.empty() && action_interval <= 0) {
find_nearby_action = true;
}
//add next active action, if it is in range
for(std::list<RAction*>::iterator it = actions.begin(); it != actions.end();) {
RAction* action = *it;
//add all files which are too old
if(gGourceSettings.max_file_lag>=0.0 && action->t < t - gGourceSettings.max_file_lag) {
it = actions.erase(it);
actionCount--;
action->rate = 2.0;
activeActions.push_back(action);
activeCount++;
continue;
}
if(!find_nearby_action) break;
float action_dist = glm::length(action->target->getAbsolutePos() - pos);
//queue first action in range
if(action_dist < gGourceBeamDist) {
it = actions.erase(it);
activeActions.push_back(action);
actionCount--; activeCount++;
break;
}
it++;
}
//reset action interval
if(action_interval <= 0) {
int total_actions = actionCount + activeCount;
action_interval = total_actions ? (1.0 / (float)total_actions) : 1.0;
}
//update actions
for(std::list<RAction*>::iterator it = activeActions.begin(); it != activeActions.end(); ) {
RAction* action = *it;
action->logic(dt);
if(action->isFinished()) {
it = activeActions.erase(it);
delete action;
activeCount--;
continue;
}
it++;
}
if(glm::length2(accel) > speed * speed) {
accel = normalise(accel) * speed;
}
pos += accel * dt;
accel = accel * std::max(0.0f, (1.0f - gGourceSettings.user_friction*dt));
}
void RUser::updateFont() {
if(selected) {
font = fontmanager.grab(gGourceSettings.font_file, 18);
font.dropShadow(true);
} else {
font = fontmanager.grab(gGourceSettings.font_file, gGourceSettings.scaled_user_font_size);
font.dropShadow(true);
}
font.alignTop(false);
namewidth = font.getWidth(name);
}
void RUser::setHighlighted(bool highlight) {
this->highlighted = highlight;
updateFont();
}
void RUser::setSelected(bool selected) {
Pawn::setSelected(selected);
updateFont();
}
const vec3& RUser::getNameColour() const {
return selected ? gGourceSettings.selection_colour : highlighted ? gGourceSettings.highlight_colour : namecol;
}
vec3 RUser::getColour() const{
if(selected) return vec3(1.0, 1.0, 1.0);
return usercol;
}
const std::string& RUser::getName() const {
return name;
}
float RUser::getAlpha() const {
float alpha = Pawn::getAlpha();
//user fades out if not doing anything
if(elapsed - last_action > gGourceSettings.user_idle_time) {
alpha = 1.0 - std::min(elapsed - last_action - gGourceSettings.user_idle_time, 1.0f);
}
return alpha;
}
bool RUser::isIdle() {
return (activeActions.empty() && actions.empty());
}
bool RUser::isFading() {
return isIdle() && (elapsed - last_action) > gGourceSettings.user_idle_time;
}
bool RUser::isInactive() {
return isIdle() && (elapsed - last_action) > 10.0;
}
bool RUser::nameVisible() const {
return (Pawn::nameVisible() || gGourceSettings.highlight_all_users || highlighted) ? true : false;
}
void RUser::calcScreenPos(GLint* viewport, GLdouble* modelview, GLdouble* projection) {
static GLdouble screen_x, screen_y, screen_z;
vec2 text_pos = pos;
text_pos.y -= dims.y * 0.5f;
gluProject( text_pos.x, text_pos.y, 0.0f, modelview, projection, viewport, &screen_x, &screen_y, &screen_z);
screen_y = (float)viewport[3] - screen_y;
screenpos.x = screen_x - namewidth * 0.5;
screenpos.y = screen_y - font.getMaxHeight();
}
void RUser::drawNameText(float alpha) {
float user_alpha = getAlpha();
if(gGourceSettings.highlight_all_users || highlighted || selected || alpha>0.0) {
vec3 name_col = getNameColour();
float name_alpha = (selected||highlighted||gGourceSettings.highlight_all_users) ? user_alpha : alpha;
font.setColour(vec4(name_col.x, name_col.y, name_col.z, name_alpha));
font.draw(screenpos.x, screenpos.y, name);
}
}
void RUser::updateActionsVBO(quadbuf& buffer) {
for(std::list<RAction*>::iterator it = activeActions.begin(); it != activeActions.end(); it++) {
RAction* action = *it;
action->drawToVBO(buffer);
}
}
void RUser::drawActions(float dt) {
for(std::list<RAction*>::iterator it = activeActions.begin(); it != activeActions.end(); it++) {
RAction* action = *it;
action->draw(dt);
}
}
void RUser::draw(float dt) {
if(gGourceSettings.hide_users) return;
Pawn::draw(dt);
}