diff --git a/docs/label-formats.md b/docs/label-formats.md index 203892d..79bbc57 100644 --- a/docs/label-formats.md +++ b/docs/label-formats.md @@ -50,10 +50,16 @@ These are computed for you and are usually what you want: | `altitude_100ft` | Altitude in hundreds of feet (flight-level style) | | `altitude_1000ft` | Altitude in thousands of feet (angels) | | `altitude_ft_floor100` | Altitude in feet, rounded down to the nearest 100 | +| `short_alt` | Altitude in hundreds of feet, zero-padded to 3 digits (24,606 ft -> `246`, 4,900 ft -> `049`) | | `speed_kt` | Calibrated airspeed in knots | | `magnetic_heading` | Heading in degrees, magnetic if enabled in settings | | `bullseye` | Bearing and range from bullseye - see below | +`short_alt` is special: it is precomputed and refreshed about every 20 seconds instead of +being recalculated for every label on every frame, so it is the cheapest way to show +altitude - handy when many tracks are visible or the radar update interval is very low. +It is already a zero-padded string, so it needs no format spec: just `{short_alt}`. + ### Aliases | Alias | Equivalent to | diff --git a/src/app.py b/src/app.py index 0970d34..bc20eee 100644 --- a/src/app.py +++ b/src/app.py @@ -34,6 +34,11 @@ MINIMUM_DRAG_DISTANCE = 5 MAXIMUM_HOVER_DISTANCE = 40 +# How often (seconds) every object's short_alt label value is recomputed. Aircraft climb +# or descend a few thousand ft/min at most, so hundreds-of-feet resolution stays +# accurate enough between refreshes while costing nothing per frame. +SHORT_ALT_UPDATE_INTERVAL = 20.0 + class Clock: @@ -74,6 +79,7 @@ def __init__(self, *args, **kwargs): self.gpu_frame_time_us = 0 self.cpu_frame_time = 0 self.radar_sleep = 0 + self.short_alt_sleep = 0.0 # GPU timing management must only be initialized after OpenGL context has been created self.gpu_timer: GPUTimer | None = None @@ -322,11 +328,19 @@ def on_loop(self, delta_time): mag_variation = self._ImguiUI.map_gl.get_current_magnetic_variation() bull_u, bull_v = self.gamestate.get_bullseye_pos() + # Periodically refresh the cheap short_alt label value for every object + self.short_alt_sleep += delta_time + update_short_alt = self.short_alt_sleep >= SHORT_ALT_UPDATE_INTERVAL + if update_short_alt: + self.short_alt_sleep = 0.0 + for obj_dict in self.gamestate.objects.values(): for obj in obj_dict.values(): obj.bull_x = bull_u obj.bull_y = bull_v obj.magnetic_variation = mag_variation if use_magnetic else 0.0 + if update_short_alt: + obj.update_short_alt() self.radar_sleep += delta_time if self.radar_sleep > config.app_config.get("radar", "update_interval", diff --git a/src/game_object.py b/src/game_object.py index 13a32dc..746eb11 100644 --- a/src/game_object.py +++ b/src/game_object.py @@ -127,6 +127,7 @@ class GameObject: # UI / runtime metadata icon: int | None + short_alt: str # Altitude in hundreds of feet, zero-padded to 3 digits, refreshed periodically color_rgba: tuple[float, float, float, float] visible: bool override_name: Optional[str] @@ -165,6 +166,7 @@ def __init__(self, object_id: str, object_type: GameObjectType, initial: ACMIObj self.visible = True # Default visibility # UI / runtime initialization + self.short_alt = "000" self.color_rgba = (1.0, 1.0, 1.0, 1.0) self.override_name = None self.override_color = None @@ -174,6 +176,7 @@ def __init__(self, object_id: str, object_type: GameObjectType, initial: ACMIObj # Apply initial data if provided if initial is not None: self.apply_delta(initial) + self.update_short_alt() # Initialize icon based on type self.icon, icon_color = get_icon_style(self) @@ -323,6 +326,17 @@ def altitude_ft_floor100(self) -> float: """Get the altitude in feet, floored to the nearest 100 ft.""" return (self.altitude_ft // 100) * 100 + def update_short_alt(self): + """Refresh short_alt: altitude in hundreds of feet, zero-padded to 3 digits + (15,743 ft -> "157", 4,900 ft -> "049"). + + Unlike the altitude_* properties, short_alt is a precomputed string attribute so + label templates using {short_alt} cost only an attribute lookup per frame. It is + refreshed periodically from the app loop (and once on object creation) rather + than recomputed for every label every frame. + """ + self.short_alt = f"{int(self.Altitude * METERS_TO_FT) // 100:03d}" + @property def speed_kt(self) -> float: """Get the speed in knots.""" diff --git a/src/tests/test_track_labels.py b/src/tests/test_track_labels.py index 1322f87..315ff36 100644 --- a/src/tests/test_track_labels.py +++ b/src/tests/test_track_labels.py @@ -49,6 +49,29 @@ def test_multiple_placeholders(self, track): def test_raw_acmi_field(self, track): assert evaluate_input_format("{Mach:.2f}", track) == "0.85" + def test_short_alt(self, track): + """short_alt is a precomputed zero-padded string (hundreds of feet).""" + track.update_short_alt() # 7500 m -> 24606 ft -> 246 + + assert evaluate_input_format("{short_alt}", track) == "246" + + def test_short_alt_pads_below_1000ft_hundreds(self, track): + """Altitudes below 10,000 ft keep a leading zero, e.g. 4900 ft -> 049.""" + track.Altitude = 1493.52 # metres -> 4900 ft + track.update_short_alt() + + assert evaluate_input_format("{short_alt}", track) == "049" + + def test_short_alt_is_stale_until_polled(self, track): + """short_alt only changes when update_short_alt() is called, not per-read.""" + track.update_short_alt() + track.Altitude = 9000.0 + + assert evaluate_input_format("{short_alt}", track) == "246" + + track.update_short_alt() # 9000 m -> 29527 ft -> 295 + assert evaluate_input_format("{short_alt}", track) == "295" + @pytest.mark.parametrize("alias,expected", [("{id}", "3001"), ("{name}", "Viper11"), ("{type}", "FIXEDWING")]) def test_aliases(self, track, alias, expected): diff --git a/src/ui/imgui_ui.py b/src/ui/imgui_ui.py index 58ef661..d4b24a9 100644 --- a/src/ui/imgui_ui.py +++ b/src/ui/imgui_ui.py @@ -50,6 +50,7 @@ example_object.AOA = 5.0 example_object.Coalition = "U.S." example_object.Name = "F-16C" +example_object.update_short_alt() IMGUI_INI_FILENAME = "openradar_imgui.ini" @@ -82,6 +83,28 @@ def help_marker(description: str): imgui.end_tooltip() +def help_marker_with_items(description: str, items: list[str], columns: int = 3): + """Help marker whose tooltip shows a description followed by a multi-column item + list, so long lists (e.g. label variables) fit on screen.""" + imgui.text_disabled("(?)") + if imgui.is_item_hovered(): + imgui.begin_tooltip() + imgui.push_text_wrap_pos(imgui.get_font_size() * 35.0) + imgui.text_unformatted(description) + imgui.pop_text_wrap_pos() + if items and imgui.begin_table("##help_marker_items", columns): + rows = (len(items) + columns - 1) // columns + for row in range(rows): + imgui.table_next_row() + for col in range(columns): + imgui.table_set_column_index(col) + idx = col * rows + row # fill down each column, then across + if idx < len(items): + imgui.text_unformatted(items[idx]) + imgui.end_table() + imgui.end_tooltip() + + def TextCentered(text: str): window_width, window_height = imgui.get_window_size() text_width, text_height = imgui.calc_text_size(text) @@ -1395,9 +1418,11 @@ def track_labels_window(self): imgui.text("Label Contents") imgui.same_line() - help_marker("Add the text to be displayed on the label. " - "This can be a static string or a formatted string with variables." - "Valid variables include: \n" + "\n".join(get_all_attributes(example_object).keys())) + help_marker_with_items( + "Add the text to be displayed on the label. " + "This can be a static string or a formatted string with variables. " + "Valid variables include:", + sorted(get_all_attributes(example_object).keys())) changed, user_input = imgui.input_text("##Label Contents", selected_label.label_format) if changed: