]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD_label.cxx
add optional <format> property for <tape>s
[flightgear.git] / src / Instrumentation / HUD / HUD_label.cxx
1 // HUD_label.cxx -- HUD Label
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  [micheleamerica#geocities:com]
6 // Copyright (C) 2006  Melchior FRANZ  [mfranz#aon:at]
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include <simgear/props/condition.hxx>
27 #include "HUD.hxx"
28
29
30 HUD::Label::Label(HUD *hud, const SGPropertyNode *n, float x, float y) :
31     Item(hud, n, x, y),
32     _input(n->getNode("input", false)),
33     _box(n->getBoolValue("box", false)),
34     _pointer_width(n->getFloatValue("pointer-width", 7.0)),
35     _pointer_length(n->getFloatValue("pointer-length", 5.0)),
36     _blink_condition(0),
37     _blink_interval(n->getFloatValue("blinking/interval", -1.0f)),
38     _blink_target(0.0),
39     _blink_state(true)
40 {
41     const SGPropertyNode *node = n->getNode("blinking/condition");
42     if (node)
43        _blink_condition = sgReadCondition(globals->get_props(), node);
44
45     const char *halign = n->getStringValue("halign", "center");
46     if (!strcmp(halign, "left"))
47         _halign = LEFT;
48     else if (!strcmp(halign, "right"))
49         _halign = RIGHT;
50     else
51         _halign = HCENTER;
52
53     _halign |= VCENTER;
54
55     const char *pre = n->getStringValue("prefix", 0);
56     const char *post = n->getStringValue("postfix", 0);
57     const char *fmt = n->getStringValue("format", 0);
58
59     if (pre)
60         _format = pre;
61
62     if (fmt)
63         _format += fmt;
64     else
65         _format += "%s";
66
67     if (post)
68         _format += post;
69
70     _mode = check_format(_format.c_str());
71     if (_mode == INVALID) {
72         SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid format '" << _format.c_str() << '\'');
73         _format = "INVALID";
74         _mode = NONE;
75     }
76
77     blink();
78 }
79
80
81 void HUD::Label::draw(void)
82 {
83     if (!(_mode == NONE || _input.isValid() && blink()))
84         return;
85
86     if (_box) {
87         float l, r, p;
88         float pw = _pointer_width / 2.0;
89
90         l = _center_x - pw;
91         r = _center_x + pw;
92         bool draw_parallel = fabsf(_pointer_width - _w) > 2.0; // draw lines left and right of arrow?
93
94         if (option_bottom()) {
95             if (draw_parallel) {
96                 draw_line(_x, _y, l, _y);
97                 draw_line(r, _y, _x + _w, _y);
98             }
99             p = _y - _pointer_length;
100             draw_line(l, _y, _center_x, p);
101             draw_line(_center_x, p, r, _y);
102         } else
103             draw_line(_x, _y, _x + _w, _y);
104
105         if (option_top()) {
106             if (draw_parallel) {
107                 draw_line(_x, _y + _h, l, _y + _h);
108                 draw_line(r, _y + _h, _x + _w, _y + _h);
109             }
110             p = _y + _h + _pointer_length;
111             draw_line(l, _y + _h, _center_x, p);
112             draw_line(_center_x, p, r, _y + _h);
113         } else
114             draw_line(_x + _w, _y + _h, _x, _y + _h);
115
116         l = _center_y - pw;
117         r = _center_y + pw;
118         draw_parallel = fabsf(_pointer_width - _h) > 2.0;
119
120         if (option_left()) {
121             if (draw_parallel) {
122                 draw_line(_x, _y, _x, l);
123                 draw_line(_x, r, _x, _y + _h);
124             }
125             p = _x - _pointer_length;
126             draw_line(_x, l, p, _center_y);
127             draw_line(p, _center_y, _x, r);
128         } else
129             draw_line(_x, _y + _h, _x, _y);
130
131         if (option_right()) {
132             if (draw_parallel) {
133                 draw_line(_x + _w, _y, _x + _w, l);
134                 draw_line(_x + _w, r, _x + _w, _y + _h);
135             }
136             p = _x + _w + _pointer_length;
137             draw_line(_x + _w, l, p, _center_y);
138             draw_line(p, _center_y, _x + _w, r);
139         } else
140             draw_line(_x + _w, _y, _x + _w, _y + _h);
141     }
142
143     const int BUFSIZE = 256;
144     char buf[BUFSIZE];
145     if (_mode == NONE)
146         snprintf(buf, BUFSIZE, _format.c_str());
147     else if (_mode == STRING)
148         snprintf(buf, BUFSIZE, _format.c_str(), _input.getStringValue());
149     else if (_mode == INT)
150         snprintf(buf, BUFSIZE, _format.c_str(), int(_input.getFloatValue()));
151     else if (_mode == LONG)
152         snprintf(buf, BUFSIZE, _format.c_str(), long(_input.getFloatValue()));
153     else if (_mode == FLOAT)
154         snprintf(buf, BUFSIZE, _format.c_str(), float(_input.getFloatValue()));
155     else if (_mode == DOUBLE) // not really supported yet
156         snprintf(buf, BUFSIZE, _format.c_str(), double(_input.getFloatValue()));
157
158     if (_halign & HCENTER)
159         draw_text(_center_x, _center_y, buf, _halign, get_digits());
160     else if (_halign & LEFT)
161         draw_text(_x, _center_y, buf, _halign, get_digits());
162     else // if (_halign & RIGHT)
163         draw_text(_x + _w, _center_y, buf, _halign, get_digits());
164 }
165
166
167 bool HUD::Label::blink()
168 {
169     if (_blink_interval < 0.0f)
170         return true;
171
172     if (_blink_condition && !_blink_condition->test())
173         return true;
174
175     if (_hud->timer() < _blink_target)
176         return _blink_state;
177
178     _blink_target = _hud->timer() + _blink_interval;
179     return _blink_state = !_blink_state;
180 }
181
182