1 /*
2 * Author: spencer jackson 2014
3 * ssjackson71@gmail.com
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 *
20 */
21
22
23 #ifndef FFF_ENVDISPLAY_H
24 #define FFF_ENVDISPLAY_H
25
26
27 #include <FL/Fl_Widget.H>
28 #include <valarray>
29 #include <string>
30
31 //avtk drawing method (adapted)
default_env_display_drawing(cairo_t * cr,char c)32 static void default_env_display_drawing(cairo_t *cr, char c)
33 {
34 }
35
36
37 namespace ffffltk
38 {
39
40 class EnvAsciiDisplay: public Fl_Widget
41 {
42 public:
43 EnvAsciiDisplay(int _x, int _y, int _w, int _h, const char *_label = ""):
Fl_Widget(_x,_y,_w,_h,_label)44 Fl_Widget(_x, _y, _w, _h, _label)
45 {
46 x = _x;
47 y = _y;
48 w = _w;
49 h = _h;
50
51 //label = _label;
52
53 drawing_w = 100;
54 drawing_h = 100;
55 drawing_f = &default_env_display_drawing;
56
57 nchars = 1;
58 periods = true;
59
60 bgcr = 0;
61 }
62
~EnvAsciiDisplay()63 ~EnvAsciiDisplay()
64 {
65 cairo_surface_destroy(bgsurf);
66 cairo_destroy(bgcr);
67 }
68
69 int x, y, w, h;
70 //const char* label;
71
72 int drawing_w;
73 int drawing_h;
74 void (*drawing_f)(cairo_t*,char);//pointer to draw function
75 void (*bg_drawing_f)(cairo_t*);//pointer to draw function
76 int nchars;
77 bool periods;
78
79 cairo_t * bgcr;
80 cairo_surface_t *bgsurf;
81
draw()82 void draw()
83 {
84
85 if(!bgcr)
86 {
87 //initialize bg
88 bgsurf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,w,h);//w,h;
89 bgcr = cairo_create(bgsurf);
90 bg_drawing_f(bgcr);
91 }
92 if (damage() & FL_DAMAGE_ALL)
93 {
94 cairo_t *cr = Fl::cairo_cc();
95
96
97 //calcluate scale and centering
98 double scalex,
99 scaley,
100 shiftx=0,
101 shifty=0,
102 offset = 0;//distance between characters
103 //scalex = w/(double)(drawing_w);
104 scalex = w/(double)(drawing_w*nchars);
105 scaley = h/(double)drawing_h;
106 if(scalex > scaley)
107 {
108 scalex = scaley;
109 shiftx = (w - scalex*drawing_w*nchars)/2.f;
110 //shiftx = (w - scalex*drawing_w)/2.f;
111 }
112 else
113 {
114 scaley = scalex;
115 shifty = h - scaley*drawing_h;
116 }
117 offset = scalex*drawing_w;
118
119 //call the draw function for each character
120 const char* str = label();
121 char c;
122 int j = 0;
123 for (int i=0; i<nchars; i++)
124 {
125 c = str[j++];
126 if(c == 0)
127 {
128 //draw blanks
129 j--;
130 }
131 else if(!periods && str[j] == '.')
132 {
133 c+=128;//add period to digit
134 j++;
135 }
136
137 cairo_save( cr );
138
139 //move
140 cairo_translate(cr,x+shiftx+i*offset,y+shifty);
141 //scale
142 cairo_scale(cr,scalex,scaley);
143 cairo_set_source_surface(cr,bgsurf,0,0);
144 cairo_paint(cr);
145 if(drawing_f) drawing_f(cr,c);
146 else default_display_drawing(cr,c);
147
148 cairo_restore(cr);
149 }
150
151 }
152 }
153
resize(int X,int Y,int W,int H)154 void resize(int X, int Y, int W, int H)
155 {
156 Fl_Widget::resize(X,Y,W,H);
157 x = X;
158 y = Y;
159 w = W;
160 h = H;
161 redraw();
162 }
163 };
164
165 } // ffffltk
166
167 #endif // FFF_BACKGROUND_H
168
169