forked from acaudwell/Gource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.cpp
133 lines (97 loc) · 3.72 KB
/
action.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
/*
Copyright (C) 2009 Andrew Caudwell ([email protected])
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 "action.h"
RAction::RAction(RUser* source, RFile* target, float addedtime) {
this->source = source;
this->target = target;
this->addedtime = addedtime;
progress = 0.0;
rate = 0.5;
}
void RAction::apply() {
target->touch(colour);
}
void RAction::logic(float dt) {
if(progress >= 1.0) return;
if(progress == 0.0) {
apply();
}
float action_rate = std::min(10.0f, rate * std::max(1.0f, ((float)source->getPendingActionCount())));
progress = std::min(progress + action_rate * dt, 1.0f);
}
void RAction::drawToVBO(quadbuf& buffer) const {
if(isFinished()) return;
vec2 src = source->getPos();
vec2 dest = target->getAbsolutePos();
//TODO: could use glm::perp
vec2 n = normalise(dest - src);
vec2 perp = vec2(-n.y, n.x);
vec2 offset = perp * target->getSize() * 0.5f;
vec2 offset_src = offset * 0.3f;
float alpha = 1.0 - progress;
float alpha2 = alpha * 0.1;
vec4 col1 = vec4(colour, alpha);
vec4 col2 = vec4(colour, alpha2);
quadbuf_vertex v1(src - offset_src, col2, vec2(0.0f, 0.0f));
quadbuf_vertex v2(src + offset_src, col2, vec2(0.0f, 1.0f));
quadbuf_vertex v3(dest + offset, col1, vec2(1.0f, 1.0f));
quadbuf_vertex v4(dest - offset, col1, vec2(1.0f, 0.0f));
buffer.add(0, v1, v2, v3, v4);
}
void RAction::draw(float dt) {
if(isFinished()) return;
vec2 src = source->getPos();
vec2 dest = target->getAbsolutePos();
vec2 n = normalise(dest - src);
vec2 perp = vec2(-n.y, n.x);
vec2 offset = perp * target->getSize() * 0.5f;
vec2 offset_src = offset * 0.3f;
float alpha = 1.0 - progress;
float alpha2 = alpha * 0.1;
vec4 col1 = vec4(colour, alpha);
vec4 col2 = vec4(colour, alpha2);
glBegin(GL_QUADS);
glColor4fv(glm::value_ptr(col2));
glTexCoord2f(0.0,0.0);
glVertex2f(src.x - offset_src.x, src.y - offset_src.y);
glTexCoord2f(0.0,1.0);
glVertex2f(src.x + offset_src.x, src.y + offset_src.y);
glColor4fv(glm::value_ptr(col1));
glTexCoord2f(1.0,1.0);
glVertex2f(dest.x + offset.x, dest.y + offset.y);
glTexCoord2f(1.0,0.0);
glVertex2f(dest.x - offset.x, dest.y - offset.y);
glEnd();
}
CreateAction::CreateAction(RUser* source, RFile* target, float addedtime) : RAction(source, target, addedtime) {
colour = vec3(0.0, 1.0, 0.0);
}
RemoveAction::RemoveAction(RUser* source, RFile* target, float addedtime): RAction(source, target, addedtime) {
colour = vec3(1.0, 0.0, 0.0);
}
void RemoveAction::logic(float dt) {
float old_progress = progress;
RAction::logic(dt);
if(old_progress < 1.0 && progress >= 1.0) {
target->remove();
}
}
ModifyAction::ModifyAction(RUser* source, RFile* target, float addedtime, const vec3& modify_colour)
: modify_colour(modify_colour), RAction(source, target, addedtime) {
colour = vec3(1.0, 0.7, 0.3);
}
void ModifyAction::apply() {
RAction::apply();
target->setFileColour(modify_colour);
}