summaryrefslogtreecommitdiff
path: root/rotord/src/nodes_drawing.h
blob: 81ef590a351e18206cc45382b16ec473e5fee7fb (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
#ifndef ROTOR_NODES_DRAWING
#define ROTOR_NODES_DRAWING

#include "rotor.h"
#include <cairo.h>

namespace Rotor {
	class Draw_node: public Image_node { //base class for drawing with cairo
		public:
			Draw_node(){image=nullptr;};
			Draw_node(map<string,string> &settings) {
				image=nullptr;
			};
			~Draw_node(){ if (image) delete image;};
			Draw_node* clone(map<string,string> &_settings) { return new Draw_node(_settings);};
			virtual void vector_output(cairo_t * cr,const Frame_spec &frame){};
			Image *output(const Frame_spec &frame){
				//get new or input image to draw upon
				if (image_inputs.size()) {
					if (image_inputs[0]->connection){
						//copy incoming image **writable
						if (image) delete image;
						image=(((Image_node*)image_inputs[0]->connection)->get_output(frame))->clone();                 
	                }
	                else {
	                	if (!image) image =new Image();
	                	image->setup(frame.w,frame.h);
	                }
			    }
			    else {
			    	if (!image) image =new Image();
			    	image->setup(frame.w,frame.h); //do this twice or use a goto	
			    }		   
			    //convert to 32 bit - this can probably be optimised further
			    cv::Mat chans;
				cv::cvtColor(image->rgb, chans, CV_BGR2RGBA, 4);
			    cairo_surface_t * cs = cairo_image_surface_create_for_data (chans.data,
                                                         CAIRO_FORMAT_RGB24,
                                                         image->w,
                                                         image->h,
                                                         image->w*4);
			    cairo_t * cr = cairo_create (cs);
				//do any kind of vector drawing
			   	vector_output(cr,frame);
				//convert frame back to 24 bits
				cv::cvtColor(chans,image->rgb,CV_RGBA2BGR,3);
				cairo_destroy(cr);
				cairo_surface_destroy(cs);
				return image;
			}
		private:
			Image *image; //is an image generator
	};
	class Hello_draw: public Draw_node {
		public:
			Hello_draw(){image=nullptr;};
			Hello_draw(map<string,string> &settings) {
				image=nullptr;
				base_settings(settings);
			};
			~Hello_draw(){ if (image) delete image;};
			Hello_draw* clone(map<string,string> &_settings) { return new Hello_draw(_settings);};
			void vector_output(cairo_t * cr,const Frame_spec &frame){
				cairo_text_extents_t te;
				cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
				cairo_select_font_face (cr, "Georgia",
				                  CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
				cairo_set_font_size (cr, 50);
				cairo_text_extents(cr, "hello, world!", &te);
				cairo_move_to (cr,(frame.w-te.width)/2,(frame.h-te.height)/2);
				cairo_show_text (cr, "hello, world!");
				cairo_fill(cr);
			}
		private:
			Image *image; //is an image generator
	};
#define SHAPE_circle 1
#define SHAPE_square 2
#define SHAPE_triangle 3
	class Shape: public Draw_node {
		public:
			Shape(){image=nullptr;};
			Shape(map<string,string> &settings) {
				image=nullptr;
				base_settings(settings);
				scale=find_setting(settings,"scale",1.0f);
				rotation=find_setting(settings,"rotation",0.0f);
				x=find_setting(settings,"x",0.0f);
				y=find_setting(settings,"y",0.0f);
				colour=Colour(find_setting(settings,"colour","FFFFFF"));
				string _shape=find_setting(settings,"shape","square");
				if (_shape=="circle") shape=SHAPE_circle;
				if (_shape=="square") shape=SHAPE_square;
				if (_shape=="triangle") shape=SHAPE_triangle;
			};
			void link_params() {
				for (auto p:parameter_inputs){
					if (p->parameter=="scale") {
						p->receiver=&scale;
					}
					if (p->parameter=="rotation") {
						p->receiver=&rotation;
					}
					if (p->parameter=="x") {
						p->receiver=&x;
					}
					if (p->parameter=="y") {
						p->receiver=&y;
					}
				}

			};
			~Shape(){ if (image) delete image;};
			Shape* clone(map<string,string> &_settings) { return new Shape(_settings);};
			void vector_output(cairo_t * cr,const Frame_spec &frame){
				cairo_set_source_rgb(cr, colour.Rfloat(),colour.Gfloat(),colour.Bfloat());
				cairo_save(cr); //not really even necessary?
				cairo_translate(cr, frame.w/2, frame.h/2);
				cairo_translate(cr, x * frame.w, y * frame.h);
				cairo_scale(cr, scale , scale );
				cairo_rotate(cr,(rotation/180.0f)*M_PI);
				switch(shape) {
					case SHAPE_square:
						cairo_rectangle(cr,-frame.w/2,-frame.w/2,frame.w,frame.w);
                        break;
					case SHAPE_circle:
						cairo_arc(cr,0,0,frame.w/2,0.0, 2 * M_PI);
                        break;
                    case SHAPE_triangle:
                    	//subtracting PI/2 =(1.5*PI)/3 so the triangle is pointing up
						cairo_line_to(cr,0,-frame.w/2);
						cairo_line_to(cr,cos((0.5 * M_PI)/3)*frame.w/2,sin((0.5 * M_PI)/3)*frame.w/2);
						cairo_line_to(cr,cos((2.5 * M_PI)/3)*frame.w/2,sin((2.5 * M_PI)/3)*frame.w/2);
						cairo_close_path(cr);
                        break;
				}
				cairo_restore (cr); //not really even necessary?
				cairo_fill(cr);
			}
		private:
			Image *image; //is an image generator
			int shape;
			float scale;
			float rotation;
			float x,y;
			Colour colour;
	};
}

#endif