BeRTOS
levelbar.c
Go to the documentation of this file.
00001 
00011 #include "levelbar.h"
00012 
00013 
00020 void lbar_init(struct LevelBar *lb, struct Bitmap *bmp, int type, int min, int max, int pos,
00021         coord_t x1, coord_t y1, coord_t x2, coord_t y2)
00022 {
00023     lb->bitmap = bmp;
00024     lb->type = type;
00025     lb->min = min;
00026     lb->max = max;
00027     lb->pos = pos;
00028     lb->x1 = x1;
00029     lb->y1 = y1;
00030     lb->x2 = x2;
00031     lb->y2 = y2;
00032 }
00033 
00034 
00038 void lbar_setLevel(struct LevelBar *lb, int level)
00039 {
00040     if (level < lb->min)
00041         level = lb->min;
00042     if (level > lb->max)
00043         level = lb->max;
00044 
00045     lb->pos = level;
00046 }
00047 
00048 
00052 int lbar_getLevel(struct LevelBar *lb)
00053 {
00054     return lb->pos;
00055 }
00056 
00057 
00062 void lbar_changeLevel(struct LevelBar *lb, int delta)
00063 {
00064     lbar_setLevel(lb, lb->pos + delta);
00065 }
00066 
00067 
00071 void lbar_setMax(struct LevelBar *lb, int max)
00072 {
00073     lb->max = max;
00074 }
00075 
00076 
00080 void lbar_draw(struct LevelBar *lb)
00081 {
00082 #define BORDERW 1
00083 #define BORDERH 1
00084 
00085     /* Compute filled bar length in pixels */
00086     int totlen = (lb->type & LBAR_HORIZONTAL) ? lb->x2 - lb->x1 - BORDERW*4 : lb->y2 - lb->y1 - BORDERH*4;
00087     int range  = lb->max - lb->min;
00088     int barlen = ((long)(lb->pos - lb->min) * (long)totlen + range - 1) / range;
00089 
00090     // Draw border
00091     gfx_rectDraw(lb->bitmap, lb->x1, lb->y1, lb->x2, lb->y2);
00092 
00093     // Clear inside
00094     gfx_rectClear(lb->bitmap, lb->x1 + BORDERW, lb->y1 + BORDERH, lb->x2 - BORDERW, lb->y2 - BORDERH);
00095 
00096     // Draw bar
00097     if (lb->type & LBAR_HORIZONTAL)
00098         gfx_rectFill(lb->bitmap,
00099                 lb->x1 + BORDERW*2, lb->y1 + BORDERH*2,
00100                 lb->x1 + BORDERW*2 + barlen, lb->y2 - BORDERH*2);
00101     else
00102         gfx_rectFill(lb->bitmap,
00103                 lb->x1 + BORDERW*2, lb->y2 - BORDERH*2 - barlen,
00104                 lb->x2 - BORDERW*2, lb->y2 - BORDERH*2);
00105 }