]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD_label.cxx
httpd: better handling of first-time notifications
[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 "HUD.hxx"
27 #include "HUD_private.hxx"
28
29 #include <Main/globals.hxx>
30
31 HUD::Label::Label(HUD *hud, const SGPropertyNode *n, float x, float y) :
32     Item(hud, n, x, y),
33     _input(n->getNode("input", false)),
34     _box(n->getBoolValue("box", false)),
35     _pointer_width(n->getFloatValue("pointer-width", 7.0)),
36     _pointer_length(n->getFloatValue("pointer-length", 5.0)),
37     _blink_condition(0),
38     _blink_interval(n->getFloatValue("blinking/interval", -1.0f)),
39     _blink_target(0.0),
40     _blink_state(true)
41 {
42     const SGPropertyNode *node = n->getNode("blinking/condition");
43     if (node)
44        _blink_condition = sgReadCondition(globals->get_props(), node);
45
46     const char *halign = n->getStringValue("halign", "center");
47     if (!strcmp(halign, "left"))
48         _halign = LEFT;
49     else if (!strcmp(halign, "right"))
50         _halign = RIGHT;
51     else
52         _halign = HCENTER;
53
54     _halign |= VCENTER;
55
56     const char *pre = n->getStringValue("prefix", 0);
57     const char *post = n->getStringValue("postfix", 0);
58     const char *fmt = n->getStringValue("format", 0);
59
60     if (pre)
61         _format = pre;
62
63     if (fmt)
64         _format += fmt;
65     else
66         _format += "%s";
67
68     if (post)
69         _format += post;
70
71     _mode = check_format(_format.c_str());
72     if (_mode == INVALID) {
73         SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid format '" << _format.c_str()
74                 << "' in <label> '" << _name << '\'');
75         _format = "INVALID";
76         _mode = NONE;
77     }
78
79     blink();
80 }
81
82
83 void HUD::Label::draw(void)
84 {
85     if (!((_mode == NONE || _input.isValid()) && blink()))
86         return;
87
88     if (_box) {
89         float l, r, p;
90         float pw = _pointer_width / 2.0;
91
92         l = _center_x - pw;
93         r = _center_x + pw;
94         bool draw_parallel = fabs(_pointer_width - _w) > 2.0; // draw lines left and right of arrow?
95
96         if (option_bottom()) {
97             if (draw_parallel) {
98                 draw_line(_x, _y, l, _y);
99                 draw_line(r, _y, _x + _w, _y);
100             }
101             p = _y - _pointer_length;
102             draw_line(l, _y, _center_x, p);
103             draw_line(_center_x, p, r, _y);
104         } else
105             draw_line(_x, _y, _x + _w, _y);
106
107         if (option_top()) {
108             if (draw_parallel) {
109                 draw_line(_x, _y + _h, l, _y + _h);
110                 draw_line(r, _y + _h, _x + _w, _y + _h);
111             }
112             p = _y + _h + _pointer_length;
113             draw_line(l, _y + _h, _center_x, p);
114             draw_line(_center_x, p, r, _y + _h);
115         } else
116             draw_line(_x + _w, _y + _h, _x, _y + _h);
117
118         l = _center_y - pw;
119         r = _center_y + pw;
120         draw_parallel = fabs(_pointer_width - _h) > 2.0;
121
122         if (option_left()) {
123             if (draw_parallel) {
124                 draw_line(_x, _y, _x, l);
125                 draw_line(_x, r, _x, _y + _h);
126             }
127             p = _x - _pointer_length;
128             draw_line(_x, l, p, _center_y);
129             draw_line(p, _center_y, _x, r);
130         } else
131             draw_line(_x, _y + _h, _x, _y);
132
133         if (option_right()) {
134             if (draw_parallel) {
135                 draw_line(_x + _w, _y, _x + _w, l);
136                 draw_line(_x + _w, r, _x + _w, _y + _h);
137             }
138             p = _x + _w + _pointer_length;
139             draw_line(_x + _w, l, p, _center_y);
140             draw_line(p, _center_y, _x + _w, r);
141         } else
142             draw_line(_x + _w, _y, _x + _w, _y + _h);
143     }
144
145     const int BUFSIZE = 256;
146     char buf[BUFSIZE+1];
147         buf[ BUFSIZE] = '\0';  // Be sure to terminate properly
148     if (_mode == NONE)
149         snprintf(buf, BUFSIZE, _format.c_str(), 0);
150     else if (_mode == STRING)
151         snprintf(buf, BUFSIZE, _format.c_str(), _input.getStringValue());
152     else if (_mode == INT)
153         snprintf(buf, BUFSIZE, _format.c_str(), int(_input.getFloatValue()));
154     else if (_mode == LONG)
155         snprintf(buf, BUFSIZE, _format.c_str(), long(_input.getFloatValue()));
156     else if (_mode == FLOAT)
157         snprintf(buf, BUFSIZE, _format.c_str(), float(_input.getFloatValue()));
158     else if (_mode == DOUBLE) // not really supported yet
159         snprintf(buf, BUFSIZE, _format.c_str(), double(_input.getFloatValue()));
160
161     if (_halign & HCENTER)
162         draw_text(_center_x, _center_y, buf, _halign, get_digits());
163     else if (_halign & LEFT)
164         draw_text(_x, _center_y, buf, _halign, get_digits());
165     else // if (_halign & RIGHT)
166         draw_text(_x + _w, _center_y, buf, _halign, get_digits());
167 }
168
169
170 bool HUD::Label::blink()
171 {
172     if (_blink_interval < 0.0f)
173         return true;
174
175     if (_blink_condition && !_blink_condition->test())
176         return true;
177
178     if (_hud->timer() < _blink_target)
179         return _blink_state;
180
181     _blink_target = _hud->timer() + _blink_interval;
182     return _blink_state = !_blink_state;
183 }
184
185