Actually make speedo units work
This commit is contained in:
parent
dd9ffecc57
commit
183f817e47
4 changed files with 49 additions and 25 deletions
|
@ -19,7 +19,9 @@ int hexagram_speedo_init(hexagram_speedo *speedo,
|
|||
double x,
|
||||
double y,
|
||||
double radius,
|
||||
double max_value,
|
||||
double max_value);
|
||||
|
||||
void hexagram_speedo_set_units(hexagram_speedo *speedo,
|
||||
hexagram_speedo_units units);
|
||||
|
||||
#endif /* _HEXAGRAM_SPEEDO_H */
|
||||
|
|
|
@ -12,8 +12,7 @@
|
|||
#define SECTION_HEIGHT_TOP 64
|
||||
#define SECTION_HEIGHT_BOTTOM 52
|
||||
|
||||
#define SPEEDO_MAX 200.0
|
||||
#define SPEEDO_UNITS HEXAGRAM_SPEEDO_MPH
|
||||
#define SPEEDO_MAX 322.0
|
||||
|
||||
#define PATTERN_WIDTH 100
|
||||
#define PATTERN_HEIGHT 87
|
||||
|
@ -102,8 +101,7 @@ int hexagram_cluster_init(hexagram_cluster *cluster,
|
|||
CLUSTER_HEIGHT / 2,
|
||||
CLUSTER_HEIGHT / 2,
|
||||
CLUSTER_HEIGHT / 2,
|
||||
SPEEDO_MAX,
|
||||
SPEEDO_UNITS) < 0) {
|
||||
SPEEDO_MAX) < 0) {
|
||||
goto error_speedo_init;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
#define DIAL_MAIN_BEZEL_WIDTH 16
|
||||
|
||||
#define DIAL_RADIUS_LEGEND_MAIN 0.675
|
||||
#define DIAL_RADIUS_LEGEND_MAIN 0.69
|
||||
#define DIAL_RADIUS_LEGEND_BOTTOM 0.725
|
||||
|
||||
#define DIAL_RADIUS_NEEDLE_MAIN_MIN 0.51
|
||||
|
|
60
src/speedo.c
60
src/speedo.c
|
@ -4,10 +4,12 @@
|
|||
#include <hexagram/gauge.h>
|
||||
#include <hexagram/speedo.h>
|
||||
|
||||
#define SPEEDO_ANGLE_MIN 232.0 * (M_PI / 180.0)
|
||||
#define SPEEDO_ANGLE_MAX 488.0 * (M_PI / 180.0)
|
||||
#define SPEED_FACTOR_MPH 0.621371
|
||||
|
||||
#define SPEEDO_FONT_FACE "Muli"
|
||||
#define ANGLE_MIN 232.0 * (M_PI / 180.0)
|
||||
#define ANGLE_MAX 488.0 * (M_PI / 180.0)
|
||||
|
||||
#define FONT_FACE "Muli"
|
||||
|
||||
static int draw_text(hexagram_speedo *speedo,
|
||||
cairo_t *cr,
|
||||
|
@ -18,7 +20,7 @@ static int draw_text(hexagram_speedo *speedo,
|
|||
cairo_text_extents_t extents;
|
||||
|
||||
cairo_select_font_face(cr,
|
||||
SPEEDO_FONT_FACE,
|
||||
FONT_FACE,
|
||||
CAIRO_FONT_SLANT_ITALIC,
|
||||
CAIRO_FONT_WEIGHT_BOLD);
|
||||
|
||||
|
@ -39,35 +41,41 @@ static int draw_text(hexagram_speedo *speedo,
|
|||
static int draw_bg(hexagram_gauge *gauge, cairo_t *cr) {
|
||||
hexagram_speedo *speedo = (hexagram_speedo *)gauge;
|
||||
|
||||
int speed;
|
||||
double factor = (speedo->units == HEXAGRAM_SPEEDO_KPH)?
|
||||
1.0: SPEED_FACTOR_MPH;
|
||||
|
||||
double font_size = (speedo->units == HEXAGRAM_SPEEDO_KPH)?
|
||||
0.085: 0.1;
|
||||
|
||||
int speed, max = (int)round(speedo->dial.max_value * factor);
|
||||
|
||||
if (hexagram_dial_draw_bg(&speedo->dial, cr) < 0) {
|
||||
goto error_dial_draw_bg;
|
||||
}
|
||||
|
||||
cairo_select_font_face(cr,
|
||||
SPEEDO_FONT_FACE,
|
||||
FONT_FACE,
|
||||
CAIRO_FONT_SLANT_ITALIC,
|
||||
CAIRO_FONT_WEIGHT_BOLD);
|
||||
|
||||
cairo_set_font_size(cr, speedo->dial.radius * 0.1);
|
||||
cairo_set_font_size(cr, speedo->dial.radius * font_size);
|
||||
cairo_set_source_rgb(cr, 1, 1, 1);
|
||||
|
||||
for (speed=0; speed<=(int)speedo->dial.max_value; speed += 5) {
|
||||
for (speed=0; speed<=max; speed += 5) {
|
||||
int large = speed % 10 == 0? 1: 0;
|
||||
|
||||
cairo_set_line_width(cr, large? 6.0: 2.0);
|
||||
hexagram_dial_draw_mark(&speedo->dial, cr, (double)speed, large);
|
||||
hexagram_dial_draw_mark(&speedo->dial, cr, (double)speed / factor, large);
|
||||
}
|
||||
|
||||
for (speed=0; speed<=(int)speedo->dial.max_value; speed += 20) {
|
||||
for (speed=0; speed<=max; speed += 20) {
|
||||
char buf[8];
|
||||
|
||||
if (snprintf(buf, 8, "%d", speed) < 0) {
|
||||
goto error_snprintf;
|
||||
}
|
||||
|
||||
hexagram_dial_draw_legend(&speedo->dial, cr, speed, buf);
|
||||
hexagram_dial_draw_legend(&speedo->dial, cr, (double)speed / factor, buf);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -80,18 +88,25 @@ error_dial_draw_bg:
|
|||
static int draw_fg(hexagram_gauge *gauge, cairo_t *cr) {
|
||||
hexagram_speedo *speedo = (hexagram_speedo *)gauge;
|
||||
char buf[8];
|
||||
double factor = (speedo->units == HEXAGRAM_SPEEDO_KPH)?
|
||||
1.0: SPEED_FACTOR_MPH;
|
||||
|
||||
if (hexagram_dial_draw_fg(&speedo->dial, cr) < 0) {
|
||||
goto error_dial_draw_fg;
|
||||
}
|
||||
|
||||
if (snprintf(buf, sizeof(buf), "%d", (int)speedo->dial.value) < 0) {
|
||||
if (snprintf(buf, sizeof(buf), "%d", (int)(speedo->dial.value * factor)) < 0) {
|
||||
goto error_snprintf;
|
||||
}
|
||||
|
||||
draw_text(speedo, cr, gauge->x, gauge->y - speedo->dial.radius * 0.01, buf, 0.32);
|
||||
|
||||
draw_text(speedo, cr, gauge->x, gauge->y + speedo->dial.radius * 0.2, "mph", 0.1);
|
||||
draw_text(speedo,
|
||||
cr,
|
||||
gauge->x,
|
||||
gauge->y + speedo->dial.radius * 0.2,
|
||||
speedo->units == HEXAGRAM_SPEEDO_KPH? "kph": "mph",
|
||||
0.1);
|
||||
|
||||
return 0;
|
||||
|
||||
|
@ -104,15 +119,14 @@ int hexagram_speedo_init(hexagram_speedo *speedo,
|
|||
double x,
|
||||
double y,
|
||||
double radius,
|
||||
double max_value,
|
||||
hexagram_speedo_units units) {
|
||||
double max_value) {
|
||||
if (hexagram_dial_init(&speedo->dial,
|
||||
HEXAGRAM_DIAL_MAIN,
|
||||
x,
|
||||
y,
|
||||
radius,
|
||||
SPEEDO_ANGLE_MIN,
|
||||
SPEEDO_ANGLE_MAX,
|
||||
ANGLE_MIN,
|
||||
ANGLE_MAX,
|
||||
0,
|
||||
max_value) < 0) {
|
||||
goto error_dial_init;
|
||||
|
@ -120,10 +134,20 @@ int hexagram_speedo_init(hexagram_speedo *speedo,
|
|||
|
||||
speedo->dial.gauge.draw_bg = draw_bg;
|
||||
speedo->dial.gauge.draw_fg = draw_fg;
|
||||
speedo->units = units;
|
||||
speedo->units = HEXAGRAM_SPEEDO_KPH;
|
||||
|
||||
return 0;
|
||||
|
||||
error_dial_init:
|
||||
return -1;
|
||||
}
|
||||
|
||||
void hexagram_speedo_set_units(hexagram_speedo *speedo,
|
||||
hexagram_speedo_units units) {
|
||||
switch (units) {
|
||||
case HEXAGRAM_SPEEDO_KPH:
|
||||
case HEXAGRAM_SPEEDO_MPH:
|
||||
speedo->units = units;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue