forked from acaudwell/Gource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.cpp
298 lines (219 loc) · 7.25 KB
/
file.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
/*
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 "file.h"
float gGourceFileDiameter = 8.0;
std::vector<RFile*> gGourceRemovedFiles;
FXFont file_selected_font;
FXFont file_font;
RFile::RFile(const std::string & name, const vec3 & colour, const vec2 & pos, int tagid) : Pawn(name,pos,tagid) {
hidden = true;
size = gGourceFileDiameter * 1.05;
radius = size * 0.5;
setGraphic(gGourceSettings.file_graphic);
speed = 5.0;
nametime = gGourceSettings.filename_time;
name_interval = nametime;
namecol = vec3(1.0, 1.0, 1.0);
file_colour = colour;
last_action = 0.0f;
fade_start = -1.0f;
removed_timestamp = 0;
expired = false;
forced_removal = false;
removing = false;
shadow = true;
distance = 0;
setFilename(name);
if(!file_selected_font.initialized()) {
file_selected_font = fontmanager.grab(gGourceSettings.font_file, 18);
file_selected_font.dropShadow(true);
file_selected_font.roundCoordinates(false);
file_selected_font.setColour(vec4(gGourceSettings.selection_colour, 1.0f));
}
if(!file_font.initialized()) {
file_font = fontmanager.grab(gGourceSettings.font_file, gGourceSettings.filename_font_size);
file_font.dropShadow(true);
file_font.roundCoordinates(false);
file_font.setColour(vec4(gGourceSettings.filename_colour, 1.0f));
}
setSelected(false);
dir = 0;
}
RFile::~RFile() {
}
void RFile::remove(time_t removed_timestamp) {
last_action = elapsed;
fade_start = elapsed;
removing = true;
this->removed_timestamp = removed_timestamp;
}
void RFile::remove() {
forced_removal = true;
remove(0);
}
void RFile::setDir(RDirNode* dir) {
this->dir = dir;
}
RDirNode* RFile::getDir() const{
return dir;
}
vec2 RFile::getAbsolutePos() const{
return pos + dir->getPos();
}
bool RFile::overlaps(const vec2& pos) const {
vec2 abs_pos = getAbsolutePos();
float halfsize_x = size * 0.5f;
vec2 halfsize ( halfsize_x, halfsize_x * graphic_ratio );
Bounds2D file_bounds(abs_pos - halfsize, abs_pos + halfsize);
return file_bounds.contains(pos);
}
void RFile::setFilename(const std::string& abs_file_path) {
fullpath = abs_file_path;
size_t pos = fullpath.rfind('/');
if(pos != std::string::npos) {
path = name.substr(0,pos+1);
name = name.substr(pos+1, std::string::npos);
} else {
path = std::string("");
name = abs_file_path;
}
//trim name to just extension
size_t dotsep = name.rfind(".");
if(dotsep != std::string::npos && dotsep != name.size()-1) {
ext = name.substr(dotsep+1);
} else if(gGourceSettings.file_extension_fallback) {
ext = name;
}
}
void RFile::colourize() {
file_colour = ext.size() ? colourHash(ext) : vec3(1.0f, 1.0f, 1.0f);
}
const vec3& RFile::getNameColour() const{
return selected ? gGourceSettings.selection_colour : namecol;
}
void RFile::setFileColour(const vec3 & colour) {
file_colour = colour;
}
const vec3 & RFile::getFileColour() const{
return file_colour;
}
vec3 RFile::getColour() const{
if(selected) return vec3(1.0f);
float lc = elapsed - last_action;
if(lc<1.0f) {
return touch_colour * (1.0f-lc) + file_colour * lc;
}
return file_colour;
}
float RFile::getAlpha() const{
float alpha = Pawn::getAlpha();
//user fades out if not doing anything
if(fade_start > 0.0f) {
alpha = 1.0 - glm::clamp(elapsed - fade_start, 0.0f, 1.0f);
}
return alpha;
}
void RFile::logic(float dt) {
Pawn::logic(dt);
vec2 dest_pos = dest;
/*
if(dir->getParent() != 0 && dir->noDirs()) {
vec2 dirnorm = dir->getNodeNormal();
dest_pos = dirnorm + dest;
}*/
dest_pos = dest_pos * distance;
accel = dest_pos - pos;
// apply accel
vec2 accel2 = accel * speed * dt;
if(glm::length2(accel2) > glm::length2(accel)) {
accel2 = accel;
}
pos += accel2;
//files have no momentum
accel = vec2(0.0f, 0.0f);
if(fade_start < 0.0f && gGourceSettings.file_idle_time > 0.0f && (elapsed - last_action) > gGourceSettings.file_idle_time) {
fade_start = elapsed;
}
// has completely faded out
if(fade_start > 0.0f && !expired && (elapsed - fade_start) >= 1.0) {
expired = true;
bool found = false;
for(std::vector<RFile*>::iterator it = gGourceRemovedFiles.begin(); it != gGourceRemovedFiles.end(); it++) {
if((*it) == this) {
found = true;
break;
}
}
if(!found) {
gGourceRemovedFiles.push_back(this);
//fprintf(stderr, "expiring %s\n", fullpath.c_str());
}
}
if(isHidden() && !forced_removal) elapsed = 0.0;
}
void RFile::touch(time_t touched_timestamp, const vec3 & colour) {
if(forced_removal || (removing && touched_timestamp < removed_timestamp)) return;
//fprintf(stderr, "touch %s\n", fullpath.c_str());
fade_start = -1.0f;
removing = false;
removed_timestamp = 0;
last_action = elapsed;
touch_colour = colour;
//un expire file if touched after being removed
if(expired) {
for(std::vector<RFile*>::iterator it = gGourceRemovedFiles.begin(); it != gGourceRemovedFiles.end(); it++) {
if((*it) == this) {
gGourceRemovedFiles.erase(it);
break;
}
}
expired=false;
}
showName();
setHidden(false);
dir->fileUpdated(true);
}
void RFile::setHidden(bool hidden) {
if(this->hidden==true && hidden==false && dir !=0) {
dir->addVisible();
}
Pawn::setHidden(hidden);
}
void RFile::calcScreenPos(GLint* viewport, GLdouble* modelview, GLdouble* projection) {
static GLdouble screen_x, screen_y, screen_z;
vec2 text_pos = getAbsolutePos();
text_pos.x += 5.5f;
if(selected)
text_pos.y -= 2.0f;
else
text_pos.y -= 1.0f;
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;
screenpos.y = screen_y;
}
void RFile::drawNameText(float alpha) {
if(!selected && alpha <= 0.01) return;
float name_alpha = selected ? 1.0 : alpha;
if(selected) {
file_selected_font.draw(screenpos.x, screenpos.y, name);
} else {
file_font.setAlpha(name_alpha);
file_font.draw(screenpos.x, screenpos.y, gGourceSettings.file_extensions ? ext : name);
}
}
void RFile::draw(float dt) {
Pawn::draw(dt);
glLoadName(0);
}