Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/label-formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
14 changes: 14 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions src/game_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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."""
Expand Down
23 changes: 23 additions & 0 deletions src/tests/test_track_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
31 changes: 28 additions & 3 deletions src/ui/imgui_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down