AWind
DC.h
1 #pragma once
2 /*
3  AWind.h - Arduino window library support for Color TFT LCD Boards
4  Copyright (C)2015 Andrei Degtiarev. All right reserved
5 
6 
7  You can always find the latest version of the library at
8  https://github.com/AndreiDegtiarev/AWind
9 
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the CC BY-NC-SA 3.0 license.
13  Please see the included documents for further information.
14 
15  Commercial use of this library requires you to buy a license that
16  will allow commercial use. This includes using the library,
17  modified or not, as a tool to sell products.
18 
19  The license applies to all part of the library including the
20  examples and tools supplied with the library.
21 */
22 #include "Log.h"
23 #include "Color.h"
24 #include "UTFT.h"
25 #ifdef _VARIANT_ARDUINO_DUE_X_ //DUE
26 extern char *dtostrf(double val, signed char width, unsigned char prec, char *sout);
27 #endif
28 class DC
30 {
31  UTFT *_lcd;
32  int _offset_x;
33  int _offset_y;
34  int _last_x;
35  int _last_y;
36  char _buffer[15];
37 public:
39  {
40  Landscape,
41  Portrate,
42  };
44  DC(UTFT *lcd)
45  {
46  _lcd=lcd;
47  Reset();
48  }
50  DC()
51  {
52  extern UTFT *globalLcd;
53  _lcd=globalLcd;
54  Reset();
55  }
57  ScreenOrientation ScreenOrientation()
58  {
59  return _lcd->orient == LANDSCAPE ? Landscape : Portrate;
60  }
63  {
64  return _lcd->getDisplayXSize()-1;
65  }
68  {
69  return _lcd->getDisplayYSize()-1;
70  }
72  void Reset()
73  {
74  _offset_x = 0;
75  _offset_y = 0;
76  _last_x = 0;
77  _last_y = 0;
78  }
80 
84  void Offset(int offset_x,int offset_y)
85  {
86  _offset_x+=offset_x;
87  _offset_y+=offset_y;
88  }
90  int ToDC_X(int x)
91  {
92  return x+_offset_x;
93  }
95  int ToDC_Y(int y)
96  {
97  return y+_offset_y;
98  }
100  void Rectangle(int left,int top,int right,int bottom)
101  {
102  _lcd->drawRect(ToDC_X(left),ToDC_Y(top),ToDC_X(right),ToDC_Y(bottom));
103  }
105  void Rectangle3D(int left,int top,int right,int bottom,Color color1,Color color2)
106  {
107  SetColor(color2);
108  MoveTo(left,bottom);
109  LineTo(right,bottom);
110  LineTo(right,top);
111  SetColor(color1);
112  LineTo(left,top);
113  LineTo(left,bottom);
114  }
116  void FillRect(int left,int top,int right,int bottom)
117  {
118  _lcd->fillRect (ToDC_X(left),ToDC_Y(top),ToDC_X(right),ToDC_Y(bottom));
119  }
121  void FillGradientRect(int left,int top,int right,int bottom,Color color1,Color color2)
122  {
123  int start_x=ToDC_X(left);
124  int start_y=ToDC_Y(top);
125  int length_x=ToDC_X(right)-start_x;
126  int length_y=ToDC_Y(bottom)-start_y;
127  byte rgb[3];
128  byte rgb1[3];
129  byte rgb2[3];
130  rgb1[0]=color1.GetR();
131  rgb1[1]=color1.GetG();
132  rgb1[2]=color1.GetB();
133  rgb2[0]=color2.GetR();
134  rgb2[1]=color2.GetG();
135  rgb2[2]=color2.GetB();
136  float fctr;
137  for(int i=0;i<=length_y;i++)
138  {
139  fctr=(float)i/length_y;
140  for(int j=0;j<3;j++)
141  {
142  rgb[j]=rgb1[j]+fctr*(rgb2[j]-rgb1[j]);
143  //out<<rgb2[j]<<" ";
144  }
145  //out<<endln;
146  _lcd->setColor(rgb[0],rgb[1],rgb[2]);
147  _lcd->drawHLine (start_x,start_y+i,length_x);
148  }
149  }
151  void FillRoundRect(int left,int top,int right,int bottom)
152  {
153  _lcd->fillRoundRect (ToDC_X(left),ToDC_Y(top),ToDC_X(right),ToDC_Y(bottom));
154  }
156  void DrawRoundRect(int left,int top,int right,int bottom)
157  {
158  _lcd->drawRoundRect (ToDC_X(left),ToDC_Y(top),ToDC_X(right),ToDC_Y(bottom));
159  }
161  void DrawNumber(int number,int x,int y)
162  {
163  sprintf(_buffer,"%d",number);
164  DrawText(_buffer,x,y);
165  }
167 
171  void DrawNumber(float number,int dec,int x,int y)
172  {
173  dtostrf(number,0,dec,_buffer);
174  DrawText(_buffer,x,y);
175  }
177  void DrawText(const __FlashStringHelper * text,int x,int y)
178  {
179  x=ToDC_X(x);
180  y=ToDC_Y(y);
181  int stl, i;
182 
183  stl = strlen_P((const char PROGMEM *)text);
184  if (_lcd->orient==PORTRAIT)
185  {
186  if (x==RIGHT)
187  x=(_lcd->disp_x_size+1)-(stl*_lcd->cfont.x_size);
188  if (x==CENTER)
189  x=((_lcd->disp_x_size+1)-(stl*_lcd->cfont.x_size))/2;
190  }
191  else
192  {
193  if (x==RIGHT)
194  x=(_lcd->disp_y_size+1)-(stl*_lcd->cfont.x_size);
195  if (x==CENTER)
196  x=((_lcd->disp_y_size+1)-(stl*_lcd->cfont.x_size))/2;
197  }
198  for (i=0; i<stl; i++)
199  {
200  unsigned char c = pgm_read_byte(&((const char PROGMEM *)text)[i]);
201  DrawSymbol(c, x + (i*(_lcd->cfont.x_size)), y);
202  }
203 
204  }
206  int FontWidth()
207  {
208  return _lcd->cfont.x_size;
209  }
212  {
213  return _lcd->cfont.y_size;
214  }
216  void DrawSymbol(const char c,int dc_x,int dc_y)
217  {
218  _lcd->printChar(c, dc_x, dc_y);
219  }
221  void DrawChar(const char c,int x, int y)
222  {
223  x=ToDC_X(x);
224  y=ToDC_Y(y);
225  DrawSymbol(c, x, y);
226  }
228  void DrawCaret(int pos, int x, int y)
229  {
230  x = x+(pos*_lcd->cfont.x_size);
231  DrawChar('_', x, y+2);
232  }
235  void DrawText(const char * text,int x,int y)
236  {
237  x=ToDC_X(x);
238  y=ToDC_Y(y);
239  //_lcd->print(text,x,y);
240  //out<<text<<" "<<strlen(text)<<endln;
241  for(int i=0;i<strlen(text);i++)
242  {
243  char c=text[i];
244  //if(c==' ')
245  // break;
246  DrawSymbol(c, x + (i*(_lcd->cfont.x_size)), y);
247  }
248  }
250  void Sector(int x0, int y0, int radius,float angle_rad)
251  {
252  x0=ToDC_X(x0);
253  y0=ToDC_Y(y0);
254  int x = 0;
255  int y = radius;
256  float tan_stop=tan(angle_rad/2);
257  int delta = 2 - 2 * radius;
258  int error = 0;
259  float ratio;
260  while(y >= 0) {
261  ratio=x/(float)y;
262  //_lcd->drawPixel(x0 + x, y0 + y);
263  if(ratio>tan_stop)
264  break;
265  _lcd->drawPixel(x0 + x, y0 - y);
266  //_lcd->drawPixel(x0 - x, y0 + y);
267  _lcd->drawPixel(x0 - x, y0 - y);
268  error = 2 * (delta + y) - 1;
269  if(delta < 0 && error <= 0)
270  {
271  ++x;
272  delta += 2 * x + 1;
273  continue;
274  }
275  error = 2 * (delta - x) - 1;
276  if(delta > 0 && error > 0)
277  {
278  --y;
279  delta += 1 - 2 * y;
280  continue;
281  }
282  ++x;
283  delta += 2 * (x - y);
284  --y;
285  }
286  }
287  void SetDeviceColor(Color color)
288  {
289  _lcd->setColor(color.GetR(),color.GetG(),color.GetB());
290  }
291 
292  void SetColor(Color color)
293  {
294  SetDeviceColor(color);
295  }
296  void SetBackColor(Color color)
297  {
298  _lcd->setBackColor(VGA_TRANSPARENT);
299  SetDeviceColor(color);
300  }
301  void SetFont(uint8_t *font)
302  {
303  _lcd->setFont(font);
304  }
305  void MoveTo(int x,int y)
306  {
307  _last_x=ToDC_X(x);
308  _last_y=ToDC_Y(y);
309  }
310  void LineTo(int x,int y)
311  {
312  _lcd->drawLine(_last_x,_last_y,ToDC_X(x),ToDC_Y(y));
313  _last_x=ToDC_X(x);
314  _last_y=ToDC_Y(y);
315  }
316  void Line(int x1,int y1,int x2,int y2)
317  {
318  MoveTo(x1,y1);
319  LineTo(x2,y2);
320  }
321 };
void Rectangle(int left, int top, int right, int bottom)
Draws rectangle. Input coordinates have to be defined in the window coordinate system.
Definition: DC.h:100
void DrawText(const __FlashStringHelper *text, int x, int y)
Draws PROGMEM string. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:177
Implements definition and operations with color.
Definition: Color.h:27
void DrawSymbol(const char c, int dc_x, int dc_y)
Draws symbol. Input coordinates have to be defined in the screen system.
Definition: DC.h:216
int ToDC_Y(int y)
Converts y coordinate from window into screen coordinate system.
Definition: DC.h:95
int FontHeight()
Returns symbol jeight for the current font.
Definition: DC.h:211
void FillRect(int left, int top, int right, int bottom)
Fills rectangle. Input coordinates have to be defined in the window coordinate system.
Definition: DC.h:116
void Sector(int x0, int y0, int radius, float angle_rad)
Draws sector. Input coordinates have to be defined in the window coordinate system.
Definition: DC.h:250
void DrawNumber(float number, int dec, int x, int y)
Draws float number. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:171
void DrawNumber(int number, int x, int y)
Draws integer number. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:161
ScreenOrientation ScreenOrientation()
Returns screen orientation vertical or horisontal.
Definition: DC.h:57
Device context. Abstraction layer to the device specific drawing code. Coordinates in drawing functio...
Definition: DC.h:29
void DrawCaret(int pos, int x, int y)
Draw caret. Input coordinates have to be defined in the window coordinate system. ...
Definition: DC.h:228
void DrawChar(const char c, int x, int y)
Draws a character. Input coordinates have to be defined in the window coordinate system.
Definition: DC.h:221
int FontWidth()
Returns symbol width for the current font.
Definition: DC.h:206
int DeviceWidth()
Returns screen width.
Definition: DC.h:62
void Offset(int offset_x, int offset_y)
Initializes drawing coordinate system offset.
Definition: DC.h:84
void Reset()
Resets device context into initial condition.
Definition: DC.h:72
int ToDC_X(int x)
Converts x coordinate from window into screen coordinate system.
Definition: DC.h:90
DC()
Constructor that used locally. This constructor assumes that UTFT library is initialized already...
Definition: DC.h:50
void Rectangle3D(int left, int top, int right, int bottom, Color color1, Color color2)
Draws rectangle with 3D border. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:105
void DrawText(const char *text, int x, int y)
Definition: DC.h:235
void DrawRoundRect(int left, int top, int right, int bottom)
Draws rounded rectangle. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:156
void FillGradientRect(int left, int top, int right, int bottom, Color color1, Color color2)
Fills rectangle with gradient color. Input coordinates have to be defined in the window coordinate sy...
Definition: DC.h:121
int DeviceHeight()
Returns screen height.
Definition: DC.h:67
void FillRoundRect(int left, int top, int right, int bottom)
Fills rounded rectangle. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:151
DC(UTFT *lcd)
Constructor for global context, that created only once in WindowsManager.
Definition: DC.h:44