Debian Codegraph @

src/Instrumentation/HUD/HUD_label.cxx

raw symbols SCIP
1 /*
2 * SPDX-FileName: HUD_label.cxx
3 * SPDX-FileComment: HUD Label
4 * SPDX-FileCopyrightText: Copyright (C) 1997 Michele F. America [micheleamerica#geocities:com]
5 * SPDX-FileContributor: Copyright (C) 2006 Melchior FRANZ [mfranz#aon:at]
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #ifdef HAVE_CONFIG_H
10 # include <config.h>
11 #endif
12
13 #include "HUD.hxx"
14 #include "HUD_private.hxx"
15
16 #include <Main/globals.hxx>
17
18 HUD::Label::Label(HUD *hud, const SGPropertyNode *n, float x, float y) :
19 Item(hud, n, x, y),
20 _input(n->getNode("input", false)),
21 _box(n->getBoolValue("box", false)),
22 _pointer_width(n->getFloatValue("pointer-width", 7.0)),
23 _pointer_length(n->getFloatValue("pointer-length", 5.0)),
24 _blink_condition(0),
25 _blink_interval(n->getFloatValue("blinking/interval", -1.0f)),
26 _blink_target(0.0),
27 _blink_state(true)
28 {
29 const SGPropertyNode *node = n->getNode("blinking/condition");
30 if (node)
31 _blink_condition = sgReadCondition(globals->get_props(), node);
32
33 std::string halign = n->getStringValue("halign", "center");
34 if (halign == "left")
35 _halign = LEFT;
36 else if (halign == "right")
37 _halign = RIGHT;
38 else
39 _halign = HCENTER;
40
41 _halign |= VCENTER;
42
43 std::string pre = n->getStringValue("prefix", "");
44 std::string post = n->getStringValue("postfix", "");
45 std::string fmt = n->getStringValue("format", "");
46
47 if (!pre.empty())
48 _format = pre;
49
50 if (!fmt.empty())
51 _format += fmt;
52 else
53 _format += "%s";
54
55 if (!post.empty())
56 _format += post;
57
58 _mode = check_format(_format.c_str());
59 if (_mode == INVALID) {
60 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid format '" << _format.c_str()
61 << "' in <label> '" << _name << '\'');
62 _format = "INVALID";
63 _mode = NONE;
64 }
65
66 blink();
67 }
68
69
70 void HUD::Label::draw(void)
71 {
72 if (!((_mode == NONE || _input.isValid()) && blink()))
73 return;
74
75 if (_box) {
76 float l, r, p;
77 float pw = _pointer_width / 2.0;
78
79 l = _center_x - pw;
80 r = _center_x + pw;
81 bool draw_parallel = fabs(_pointer_width - _w) > 2.0; // draw lines left and right of arrow?
82
83 if (option_bottom()) {
84 if (draw_parallel) {
85 draw_line(_x, _y, l, _y);
86 draw_line(r, _y, _x + _w, _y);
87 }
88 p = _y - _pointer_length;
89 draw_line(l, _y, _center_x, p);
90 draw_line(_center_x, p, r, _y);
91 } else
92 draw_line(_x, _y, _x + _w, _y);
93
94 if (option_top()) {
95 if (draw_parallel) {
96 draw_line(_x, _y + _h, l, _y + _h);
97 draw_line(r, _y + _h, _x + _w, _y + _h);
98 }
99 p = _y + _h + _pointer_length;
100 draw_line(l, _y + _h, _center_x, p);
101 draw_line(_center_x, p, r, _y + _h);
102 } else
103 draw_line(_x + _w, _y + _h, _x, _y + _h);
104
105 l = _center_y - pw;
106 r = _center_y + pw;
107 draw_parallel = fabs(_pointer_width - _h) > 2.0;
108
109 if (option_left()) {
110 if (draw_parallel) {
111 draw_line(_x, _y, _x, l);
112 draw_line(_x, r, _x, _y + _h);
113 }
114 p = _x - _pointer_length;
115 draw_line(_x, l, p, _center_y);
116 draw_line(p, _center_y, _x, r);
117 } else
118 draw_line(_x, _y + _h, _x, _y);
119
120 if (option_right()) {
121 if (draw_parallel) {
122 draw_line(_x + _w, _y, _x + _w, l);
123 draw_line(_x + _w, r, _x + _w, _y + _h);
124 }
125 p = _x + _w + _pointer_length;
126 draw_line(_x + _w, l, p, _center_y);
127 draw_line(p, _center_y, _x + _w, r);
128 } else
129 draw_line(_x + _w, _y, _x + _w, _y + _h);
130 }
131
132 const int BUFSIZE = 256;
133 char buf[BUFSIZE+1];
134 buf[ BUFSIZE] = '\0'; // Be sure to terminate properly
135 if (_mode == NONE)
136 snprintf(buf, BUFSIZE, _format.c_str(), 0);
137 else if (_mode == STRING)
138 snprintf(buf, BUFSIZE, _format.c_str(), _input.getStringValue().c_str());
139 else if (_mode == INT)
140 snprintf(buf, BUFSIZE, _format.c_str(), int(_input.getFloatValue()));
141 else if (_mode == LONG)
142 snprintf(buf, BUFSIZE, _format.c_str(), long(_input.getFloatValue()));
143 else if (_mode == FLOAT)
144 snprintf(buf, BUFSIZE, _format.c_str(), float(_input.getFloatValue()));
145 else if (_mode == DOUBLE) // not really supported yet
146 snprintf(buf, BUFSIZE, _format.c_str(), double(_input.getFloatValue()));
147
148 if (_halign & HCENTER)
149 draw_text(_center_x, _center_y, buf, _halign, get_digits());
150 else if (_halign & LEFT)
151 draw_text(_x, _center_y, buf, _halign, get_digits());
152 else // if (_halign & RIGHT)
153 draw_text(_x + _w, _center_y, buf, _halign, get_digits());
154 }
155
156
157 bool HUD::Label::blink()
158 {
159 if (_blink_interval < 0.0f)
160 return true;
161
162 if (_blink_condition && !_blink_condition->test())
163 return true;
164
165 if (_hud->timer() < _blink_target)
166 return _blink_state;
167
168 _blink_target = _hud->timer() + _blink_interval;
169 return _blink_state = !_blink_state;
170 }
171
172