Skip to content

Commit

Permalink
Add textboxes. (facebookresearch#1957)
Browse files Browse the repository at this point in the history
  • Loading branch information
0mdc authored and dannymcy committed Jun 26, 2024
1 parent 90b5e26 commit c5850bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
19 changes: 19 additions & 0 deletions habitat-hitl/habitat_hitl/core/client_message_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def __init__(self, button_id: str, text: str, enabled: bool):
self.enabled = enabled


@dataclass
class UITextbox:
"""
Networked UI textbox. Use RemoteClientState.get_textbox_content() to retrieve content.
"""

def __init__(self, textbox_id: str, text: str, enabled: bool):
self.textbox_id = textbox_id
self.text = text
self.enabled = enabled


class ClientMessageManager:
r"""
Extends gfx-replay keyframes to include server messages to be interpreted by the clients.
Expand Down Expand Up @@ -163,6 +175,7 @@ def show_modal_dialogue_box(
title: str,
text: str,
buttons: List[UIButton],
textbox: Optional[UITextbox] = None,
destination_mask: Mask = Mask.ALL,
):
r"""
Expand All @@ -177,6 +190,12 @@ def show_modal_dialogue_box(
"text": text,
"buttons": [],
}
if textbox is not None:
message["dialog"]["textbox"] = {
"id": textbox.textbox_id,
"text": textbox.text,
"enabled": textbox.enabled,
}
for button in buttons:
message["dialog"]["buttons"].append(
{
Expand Down
12 changes: 11 additions & 1 deletion habitat-hitl/habitat_hitl/core/remote_client_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# LICENSE file in the root directory of this source tree.

import math
from typing import Any, List, Optional, Set, Tuple
from typing import Any, Dict, List, Optional, Set, Tuple

import magnum as mn

Expand Down Expand Up @@ -51,6 +51,7 @@ def __init__(

# TODO: Handle UI in a different class.
self._pressed_ui_buttons: List[Set[str]] = []
self._textboxes: List[Dict[str, str]] = []

self._gui_inputs: List[GuiInput] = []
self._client_state_history: List[List[ClientState]] = []
Expand All @@ -60,6 +61,7 @@ def __init__(
self._client_state_history.append([])
self._receive_rate_trackers.append(AverageRateTracker(2.0))
self._pressed_ui_buttons.append(set())
self._textboxes.append({})

self._client_loading: List[bool] = [False] * users.max_user_count

Expand Down Expand Up @@ -102,6 +104,10 @@ def bind_gui_input(self, gui_input: GuiInput, user_index: int) -> None:
def ui_button_pressed(self, user_index: int, button_id: str) -> bool:
return button_id in self._pressed_ui_buttons[user_index]

def get_textbox_content(self, user_index: int, textbox_id: str) -> str:
user_textboxes = self._textboxes[user_index]
return user_textboxes.get(textbox_id, "")

def get_history_length(self) -> int:
"""Length of client state history preserved. Anything beyond this horizon is discarded."""
return 4
Expand Down Expand Up @@ -236,6 +242,8 @@ def _update_input_state(
if ui is not None:
for button in ui.get("buttonsPressed", []):
self._pressed_ui_buttons[user_index].add(button)
for textbox_id, text in ui.get("textboxes", {}).items():
self._textboxes[user_index][textbox_id] = text

input_json = (
client_state["input"] if "input" in client_state else None
Expand Down Expand Up @@ -472,12 +480,14 @@ def on_frame_end(self) -> None:
for user_index in self._users.indices(Mask.ALL):
self._gui_inputs[user_index].on_frame_end()
self._pressed_ui_buttons[user_index].clear()
self._textboxes[user_index].clear()
self._new_connection_records = None

def clear_history(self, user_mask=Mask.ALL) -> None:
for user_index in self._users.indices(user_mask):
self._client_state_history[user_index].clear()
self._pressed_ui_buttons[user_index].clear()
self._textboxes[user_index].clear()

def kick(self, user_mask: Mask) -> None:
"""
Expand Down

0 comments on commit c5850bf

Please sign in to comment.