summaryrefslogtreecommitdiff
path: root/gui/src/chainImageSet.cpp
blob: 252e4e63d11ccf983a9c6d0e39124e06a763d20a (plain)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "chainImageSet.h"

void chainImageSet::drawOutput(){

	//movment and rotation are working on their own but not together.

	float camera_throw= (float)outputSize.y/(float)outputSize.x; //the ratio of z distance to x width

	camera_throw*=fitFactor; //fudge factor to allow tweening, 

	//printf("Drawing chain: %i images\n",images.size());

	if (images.size()){

/*
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(-ofGetWidth()/2, ofGetWidth()/2, ofGetHeight()/2, -ofGetHeight()/2, 0.0f, 5000.0f);
*/
		glMatrixMode ( GL_MODELVIEW );
		glLoadIdentity ( );
		/* is not what we want
		glOrtho(
				0,
				(*currentImage)->getTransform().x*2,
			 	0,
			 	(*currentImage)->getTransform().y*2,
			 	-100,
			 	(*currentImage)->getTransform().x*4
		 	);
		*/
		glScalef(8,8,8); //easy way to avoid camera cutoff

		gluLookAt( (*currentImage)->getTransform().x,
					(*currentImage)->getTransform().y, //	 i1.linkPos.y+(xform.y*intervalpoint),
					(*currentImage)->getWidth()*camera_throw*(*currentImage)->getScale(), 
					(*currentImage)->getTransform().x,
					(*currentImage)->getTransform().y, //	 i1.linkPos.y+(xform.y*intervalpoint),
					0, 
					sin(-(*currentImage)->getRotation()*(PI/180)),
					cos(-(*currentImage)->getRotation()*(PI/180)),
					0);	



		
		(*currentImage)->drawChain(DEFAULT_FADEIN,additive,intensity);

	}

}

void chainImageSet::drawGui(int x,int y,bool is_selected){	

	float t_xoffs=0.0;
	float borderwidth=THUMB_SIZE*0.1; //ofGetWindowHeight()*(1.0-THUMB_BORDER_RATIO)*0.5;

	

	/*
	float relscale=((float)ofGetWindowHeight())/THUMB_SIZE;
	glScalef(relscale,relscale,relscale);
	//scale view by distance betweeen thumnail size and window size: doesn't work
	*/

	//draw each image, outlined

	glPushMatrix();

	glTranslatef(x,y,0);

	for(auto ii=images.begin(); ii != images.end(); ii++){
		
		ofSetColor(255,255,255);

		float thumbx=(*ii)->thumbnail.getWidth()/2;
		float thumby=(*ii)->thumbnail.getHeight()/2;
		float thumbscale=(*ii)->thumbnail.getWidth()/(*ii)->getWidth();

		
		//why do I have to set this every time??
		(*ii)->thumbnail.setAnchorPercent(0.5,0.5);
		
		if ((*ii)->thumbnail.isAllocated()){
			(*ii)->thumbnail.setUseTexture(true);
			(*ii)->thumbnail.draw(
				t_xoffs+borderwidth+thumbx,
				borderwidth+thumby,
				(*ii)->thumbnail.getWidth(),
				(*ii)->thumbnail.getHeight()
			);
		}
		

		ofSetColor(255,255,255);
		if (ii==selected) ofSetColor(255,0,0);
		if (ii==currentImage) ofSetColor(0,255,0);

		ofDrawLine(t_xoffs+borderwidth,borderwidth,
			t_xoffs+borderwidth+(*ii)->thumbnail.getWidth(),borderwidth);
		ofDrawLine(t_xoffs+borderwidth+(*ii)->thumbnail.getWidth(),borderwidth,
			t_xoffs+borderwidth+(*ii)->thumbnail.getWidth(),borderwidth+(*ii)->thumbnail.getHeight());
		ofDrawLine(t_xoffs+borderwidth+(*ii)->thumbnail.getWidth(),borderwidth+(*ii)->thumbnail.getHeight(),
			t_xoffs+borderwidth,borderwidth+(*ii)->thumbnail.getHeight());
		ofDrawLine(t_xoffs+borderwidth,borderwidth+(*ii)->thumbnail.getHeight(),
			t_xoffs+borderwidth,borderwidth);

		ofSetColor(255,255,255);
		
		if ((*ii)->link){
			ofPoint lp=(*ii)->linkPos;
			if (ii==selected){
				lp+=dragPoint;
			}

			float subpictx=t_xoffs+borderwidth+thumbx+(lp.x*thumbscale);
			float subpicty=borderwidth+thumby+(lp.y*thumbscale);

			ofDrawLine(
				subpictx,
				subpicty,
				t_xoffs+(borderwidth*3)+(*ii)->thumbnail.getWidth(),
				borderwidth+thumby

			);

			glPushMatrix();

				glTranslatef(subpictx,subpicty,0);
			
				float r=(*ii)->linkRot;
				if (ii==selected) r+=dragRotate;
				glRotatef(r,0,0,1);

				//printf("Sub image: centre at %f,%f \n",ii->link->thumbnail.getAnchorPoint().x,ii->link->thumbnail.getAnchorPoint().y);
				
				(*ii)->link->thumbnail.setAnchorPercent(0.5,0.5);

				float thescale=(*ii)->linkScale;
				if (ii==selected){
					thescale*=(1.0f+dragScale);
				}
				if ((*ii)->link->thumbnail.isAllocated()){

					(*ii)->link->thumbnail.draw(
						0,
						0,
						(*ii)->link->thumbnail.getWidth()*thescale,
						(*ii)->link->thumbnail.getHeight()*thescale
					);
				}

				ofPoint p1=ofPoint(
					-((*ii)->link->thumbnail.getWidth()*thescale*0.5),
					-((*ii)->link->thumbnail.getHeight()*thescale*0.5));
				ofPoint p2=ofPoint(
					((*ii)->link->thumbnail.getWidth()*thescale*0.5),
					-((*ii)->link->thumbnail.getHeight()*thescale*0.5));
				ofPoint p3=ofPoint(
					((*ii)->link->thumbnail.getWidth()*thescale*0.5),
					((*ii)->link->thumbnail.getHeight()*thescale*0.5));
				ofPoint p4=ofPoint(
					-((*ii)->link->thumbnail.getWidth()*thescale*0.5),
					((*ii)->link->thumbnail.getHeight()*thescale*0.5));

				ofDrawLine(p1,p2);
				ofDrawLine(p2,p3);
				ofDrawLine(p3,p4);
				ofDrawLine(p4,p1);



			glPopMatrix();

		}
		
		
		t_xoffs+=(*ii)->thumbnail.getWidth()+(borderwidth*2);

			
	}

	if (is_selected) {
		ofSetColor(0,0,255);
		ofPolyline box;
		box.addVertex(ofPoint(5,5));
		box.addVertex(ofPoint(t_xoffs-5,5));
		box.addVertex(ofPoint(t_xoffs-5,(THUMB_SIZE/THUMB_BORDER_RATIO)-10));
		box.addVertex(ofPoint(5,(THUMB_SIZE/THUMB_BORDER_RATIO)-10));
		box.addVertex(ofPoint(5,5));
		box.draw();
	}
	

	glPopMatrix();

}

bool chainImageSet::add(std::string filename,glm::vec2 pos){
	printf("Dropped file: %s at %f,%f \n",filename.c_str(),pos.x,pos.y);
/*
attempt to add file to chain.
find if file exists in data folder
if not make a symbolic link
*/
	auto image=std::make_unique<chainImage>();

	image->init(ofPoint(0,0),
		0.3, //currentDefaultImageRatio,
		0 //default rotation
	);

	printf("Loading file: %s linkscale %f\n",
		filename.c_str(),
		image->linkScale);

	loader.loadFromDisk(*image,filename);

	loadingImages.push_back(std::move(image));

	//if (image.load(filename)){

	

	//loadingImages.push_back(image);
	
	return true;
	//}
	//printf("Could not load file: %s \n",filename.c_str());

	//return false;
}


void chainImageSet::keyPressed(ofKeyEventArgs &keyargs){
	//printf("Got key %i, modifiers:%i\n",keyargs.key,keyargs.modifiers);
	switch(keyargs.key){
		case '[':
			if (selected==images.begin()){
				selected=--images.end();
			}
			else --selected;
			break;
		case ']':
			++selected;
			if (selected==images.end()){
				selected=images.begin();
			}
			break;
		case OF_KEY_UP:
			(*selected)->linkPos.y--;
			break;
		case OF_KEY_DOWN:
			(*selected)->linkPos.y++;
			break;
		case OF_KEY_LEFT:
			(*selected)->linkPos.x--;
			break;
		case OF_KEY_RIGHT:
			(*selected)->linkPos.x++;
			break;
		case OF_KEY_BACKSPACE:{

			if (images.size()==1){
				//also OF_KEY_DEL ?
				images.clear();
				filename.clear();
				break;
			}
			
			auto previous=selected;
			if (previous==images.begin()){
				previous=images.end();
				previous--;
			}
			else {
				previous--;
			}
			(*previous)->link=(*selected)->link;
			printf("DELETING: %s \n",(*selected)->filename.c_str());
			printf("LINKING %s to %s\n",(*previous)->filename.c_str(),(*selected)->link->filename.c_str());
			images.erase(selected);
			//printf("%s is now selected\n",(*selected)->filename.c_str()); //crashes
			selected=previous;
			selected++;
			if (selected==images.end()){
				selected=images.begin();
			}
			printf("%s is set selected\n",(*selected)->filename.c_str());
			break;

			
		}
		case 's':
		case 83: {
			//printf("Saving... keyargs modifiers:%i\n",keyargs.modifiers);
			//saves over last filename or use control to choose a new one
			if (!filename.length()||keyargs.hasModifier(OF_KEY_CONTROL)){
				ofFileDialogResult saveFileResult = ofSystemSaveDialog(ofGetTimestampString() + ".json" , "Save your file");
				if (saveFileResult.bSuccess){
					filename=saveFileResult.filePath;	
					//printf("Got filename: %s\n",filename.c_str());
				}
				//else printf("ofSystemSaveDialog failed.\n");
			}
			if (filename.length()){
				//printf("Saving %s\n",filename.c_str());
				saveJson(filename);
			}
			
			break;
		}
		case 'l':{
			//load_chain();
			ofFileDialogResult openFileResult= ofSystemLoadDialog("Select a json preset"); 
		
			//Check if the user opened a file
			if (openFileResult.bSuccess){
				
				
				bool success=loadJson(openFileResult.fileName);

				ofLogVerbose("Load %s %s\n",
					openFileResult.filePath.c_str(),
					success?"succeeded":"failed");
				
				
			}else {
				ofLogVerbose("User hit cancel");
			}
			break;

		}
	}
}

//--------------------------------------------------------------
void chainImageSet::mouseDragged(int x, int y, int button){
	switch (button){
		case OF_MOUSE_BUTTON_1:
			dragPoint=ofPoint(x-clickPoint.x,y-clickPoint.y)*((*selected)->getHeight()/ofGetWindowHeight());
			break;
		case OF_MOUSE_BUTTON_2:
			//alt-click
			dragRotate=((clickPoint.x-x)/((*selected)->thumbnail.getWidth()))*180.0f;
			break;
		case OF_MOUSE_BUTTON_3:
			//control-click
			dragScale=(y-clickPoint.y)/((*selected)->thumbnail.getHeight());
			//if (dragScale*selected->linkScale<0.15){
			//	dragScale=0.15/selected->linkScale;
			//}
			break;
	}
	}

//--------------------------------------------------------------
void chainImageSet::mousePressed(int x, int y, int button){
	//todo: select
	clickPoint=ofPoint(x,y);
}

//--------------------------------------------------------------
void chainImageSet::mouseReleased(int x, int y, int button){
	if (images.size()){
		(*selected)->linkPos+=dragPoint;
		dragPoint=ofPoint(0,0);
		(*selected)->linkScale*=(1.0f+dragScale);
		dragScale=0.0f;
		(*selected)->linkRot+=dragRotate;
		dragRotate=0.0f;
	}
}

bool chainImageSet::saveJson(std::string filename){
	ofxJSON json;

	json["images"] = Json::Value(Json::arrayValue);

	for(auto ii=images.begin(); ii != images.end(); ii++){
		json["images"].append((*ii)->toJson());
	}


	json.save(filename, true);

}
bool chainImageSet::loadJson(std::string _filename){
	ofxJSON json;
	bool parsingSuccessful = json.open(_filename);

    if (parsingSuccessful)
    {
    	images.clear();

    	for (int i=0;i<json["images"].size();i++){
    		auto image=std::make_unique<chainImage>();
    		if (image->fromJson(json["images"][i])){
    			images.push_back(std::move(image));	
    		} 
    	}
    	for (auto ii=images.begin(); ii != images.end(); ii++){
			auto li=ii;
			li++;
			if (li==images.end()) {
				li=images.begin();
			}
			(*ii)->link=&(*(*li));

		}
		selected=images.begin();
		currentImage=images.begin();
		(*currentImage)->start();
		filename=_filename;
		return true;


    }
    ofLogVerbose("JSON load: parsing unsuccesful\n");
    return false;
}
	
void chainImageSet::updateOutput(){

	for (auto i=loadingImages.begin();i!=loadingImages.end();){

		//printf("Checking loadingImage: %s\n",(*i)->filename.c_str());


		if ((*i)->isLoaded){
			printf("Finished loadingImage: %s, linkscale %f\n",
				(*i)->filename.c_str(),
				(*i)->linkScale
				);
			
			images.push_back(std::move(*i));
			(*images.rbegin())->link=&(*(*images.begin()));
			(*images.rbegin())->setUseTexture(true);
 	    
			printf("Linked: %s to %s\n",
				(*images.rbegin())->filename.c_str(),
				(*images.rbegin())->link->filename.c_str());


			if (images.size()>1){
				(*(++images.rbegin()))->link=&(*(*images.rbegin()));
				printf("Linked: %s to %s\n",
					(*(++images.rbegin()))->filename.c_str(),
					(*(++images.rbegin()))->link->filename.c_str());
			}
			else {
				selected=images.begin();
				currentImage=images.begin();
				(*currentImage)->start();
				
			}
		
			loadingImages.erase(i);
			printf("Images: %i loadingImages %i\n",
				images.size(),
				loadingImages.size());
		}
		else i++;
		
	}

	
	if (images.size()){
		int switchResponse=(*currentImage)->updateOutput(decayFactor);
				
		switch(switchResponse){
			case SWITCH_FORWARD:{
				
				currentImage++;

				if (currentImage==images.end()){
					currentImage=images.begin();
				}

				ofLogNotice() << "Switched image forward to " << (*currentImage)->filename;
				
								
				(*currentImage)->start(false);
				(*currentImage)->updateOutput(decayFactor);

				break;
			}
			case SWITCH_NONE:
				break;
			case SWITCH_REVERSE:{

				if (currentImage==images.begin()){
					currentImage=images.end();
				}
				
				currentImage--;

				(*currentImage)->start(true);
				ofLogNotice() << "Switched images backward to " << (*currentImage)->filename;
				(*currentImage)->updateOutput(decayFactor);
				break;

			}
		}
	}

}