|
| 1 | +from collections.abc import Iterator |
| 2 | +from contextlib import contextmanager |
| 3 | +from types import EllipsisType |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
1 | 6 | import unrealsdk |
| 7 | +from unrealsdk import unreal |
2 | 8 |
|
3 | 9 | from mods_base import get_pc |
4 | 10 |
|
5 | | -__all__: tuple[str, ...] = ("show_hud_message",) |
| 11 | +if TYPE_CHECKING: |
| 12 | + from enum import IntEnum |
| 13 | + |
| 14 | + class ERewardPopup(IntEnum): |
| 15 | + ERP_BadassToken = 0 |
| 16 | + ERP_CharacterHead = 1 |
| 17 | + ERP_CharacterSkin = 2 |
| 18 | + ERP_VehicleSkin = 3 |
| 19 | + ERP_MAX = 4 |
| 20 | + |
| 21 | +else: |
| 22 | + ERewardPopup = unrealsdk.find_enum("ERewardPopup") |
| 23 | + |
| 24 | +__all__: tuple[str, ...] = ( |
| 25 | + "ERewardPopup", |
| 26 | + "blocking_message_hide", |
| 27 | + "blocking_message_show", |
| 28 | + "contextual_prompt_hide", |
| 29 | + "contextual_prompt_show", |
| 30 | + "display_blocking_message", |
| 31 | + "display_contextual_prompt", |
| 32 | + "display_message", |
| 33 | + "message_hide", |
| 34 | + "message_show", |
| 35 | + "show_big_notification", |
| 36 | + "show_hud_message", |
| 37 | + "show_reward_popup", |
| 38 | + "show_top_center_message", |
| 39 | +) |
6 | 40 |
|
7 | 41 |
|
8 | 42 | def show_hud_message(title: str, msg: str, duration: float = 2.5) -> None: |
@@ -39,3 +73,223 @@ def show_hud_message(title: str, msg: str, duration: float = 2.5) -> None: |
39 | 73 | True, |
40 | 74 | 0, |
41 | 75 | ) |
| 76 | + |
| 77 | + |
| 78 | +def show_big_notification( |
| 79 | + msg: str, |
| 80 | + ui_sound: unreal.UObject | None | EllipsisType = ..., |
| 81 | +) -> None: |
| 82 | + """ |
| 83 | + Displays a big notification message in the main in game hud. |
| 84 | +
|
| 85 | + Uses the message style of the Second Wind notification. |
| 86 | +
|
| 87 | + Note this message can only display up to 33 characters. |
| 88 | +
|
| 89 | + Args: |
| 90 | + msg: The message to display. |
| 91 | + ui_sound: An optional AkEvent to play when the message is displayed. |
| 92 | + If Ellipsis, default sound will be used. |
| 93 | + """ |
| 94 | + if (hud_movie := get_pc().GetHUDMovie()) is None: |
| 95 | + return |
| 96 | + |
| 97 | + sound_backup = None |
| 98 | + sw_interaction = None |
| 99 | + for gfx_movie in unrealsdk.find_all("GearboxGFxMovie", exact=False): |
| 100 | + for interaction in gfx_movie.InteractionOverrideSounds: |
| 101 | + if interaction.Interaction == "SecondWind": |
| 102 | + sound_backup = interaction.AkEvent |
| 103 | + sw_interaction = interaction |
| 104 | + break |
| 105 | + |
| 106 | + if ui_sound is not Ellipsis and sw_interaction: |
| 107 | + sw_interaction.AkEvent = ui_sound |
| 108 | + |
| 109 | + backup_string = hud_movie.SecondWindString |
| 110 | + hud_movie.SecondWindString = msg |
| 111 | + hud_movie.DisplaySecondWind() |
| 112 | + hud_movie.SecondWindString = backup_string |
| 113 | + if sw_interaction: |
| 114 | + sw_interaction.AkEvent = sound_backup |
| 115 | + |
| 116 | + |
| 117 | +def show_top_center_message(msg: str, show_discovered_message: bool = False) -> None: |
| 118 | + """ |
| 119 | + Displays a message in the top center of the screen. |
| 120 | +
|
| 121 | + Uses the style of the new area discovered message. |
| 122 | +
|
| 123 | + Note this message can only display up to 41 characters. |
| 124 | +
|
| 125 | + Args: |
| 126 | + msg: The message to display. |
| 127 | + show_discovered_message: If True, the message 'You have discovered' header will show. |
| 128 | + """ |
| 129 | + if (hud_movie := get_pc().GetHUDMovie()) is None: |
| 130 | + return |
| 131 | + hud_movie.ShowWorldDiscovery("", "msg", show_discovered_message, False) |
| 132 | + |
| 133 | + |
| 134 | +def show_reward_popup( |
| 135 | + msg: str, |
| 136 | + reward_type: ERewardPopup = ERewardPopup.ERP_BadassToken, |
| 137 | +) -> None: |
| 138 | + """ |
| 139 | + Displays a reward popup with the given message and reward type. |
| 140 | +
|
| 141 | + Note this message can only display up to 33 characters. |
| 142 | +
|
| 143 | + Args: |
| 144 | + msg: The message to display in the popup. |
| 145 | + reward_type: The type of reward to display. Defaults to ERewardPopup.ERP_BadassToken. |
| 146 | + """ |
| 147 | + if (hud_movie := get_pc().GetHUDMovie()) is None: |
| 148 | + return |
| 149 | + |
| 150 | + icon = { |
| 151 | + ERewardPopup.ERP_BadassToken: "token", |
| 152 | + ERewardPopup.ERP_CharacterHead: "head", |
| 153 | + ERewardPopup.ERP_CharacterSkin: "playerSkin", |
| 154 | + ERewardPopup.ERP_VehicleSkin: "vehicleSkin", |
| 155 | + }.get(reward_type, "token") |
| 156 | + |
| 157 | + hud_movie.SingleArgInvokeS("p1.badassToken.gotoAndStop", "stop") |
| 158 | + hud_movie.SingleArgInvokeS("p1.badassToken.gotoAndStop", "go") |
| 159 | + hud_movie.SingleArgInvokeS("p1.badassToken.inner.gotoAndStop", icon) |
| 160 | + hud_movie.SetVariableString("p1.badassToken.inner.dispText.text", msg) |
| 161 | + |
| 162 | + |
| 163 | +def contextual_prompt_show(text: str, button_string: str) -> None: |
| 164 | + """ |
| 165 | + Displays a contextual prompt with the given text and button string. |
| 166 | +
|
| 167 | + Note both text and button_string can each display up to 16 characters. |
| 168 | +
|
| 169 | + Args: |
| 170 | + text: The text top to display in the prompt. |
| 171 | + button_string: The button string to display in the prompt. |
| 172 | + """ |
| 173 | + |
| 174 | + if (hud_movie := get_pc().GetHUDMovie()) is None: |
| 175 | + return |
| 176 | + contextual_prompt = hud_movie.ContextualPromptButtonString |
| 177 | + hud_movie.ContextualPromptButtonString = button_string |
| 178 | + hud_movie.ToggleContextualPrompt(text, True) |
| 179 | + hud_movie.ContextualPromptButtonString = contextual_prompt |
| 180 | + |
| 181 | + |
| 182 | +def contextual_prompt_hide() -> None: |
| 183 | + """Hides the currently displayed contextual prompt, if any.""" |
| 184 | + if (hud_movie := get_pc().GetHUDMovie()) is None: |
| 185 | + return |
| 186 | + hud_movie.ToggleContextualPrompt("", False) |
| 187 | + |
| 188 | + |
| 189 | +@contextmanager |
| 190 | +def display_contextual_prompt(text: str, button_string: str) -> Iterator[None]: |
| 191 | + """ |
| 192 | + Context manager to display and hide a contextual prompt. |
| 193 | +
|
| 194 | + This will display the prompt when entering the context and hide it when exiting. |
| 195 | +
|
| 196 | + Args: |
| 197 | + text: The text to display in the prompt. |
| 198 | + button_string: The button string to display in the prompt. |
| 199 | + """ |
| 200 | + contextual_prompt_show(text, button_string) |
| 201 | + try: |
| 202 | + yield |
| 203 | + finally: |
| 204 | + contextual_prompt_hide() |
| 205 | + |
| 206 | + |
| 207 | +def blocking_message_show(msg: str, reason: str | None = None) -> None: |
| 208 | + """ |
| 209 | + Displays a blocking message with the given text. |
| 210 | +
|
| 211 | + This message will block any input until it is hidden. |
| 212 | +
|
| 213 | + Note this message has no character limit, but only scales horizontally. |
| 214 | + Multiple lines will not be displayed correctly. |
| 215 | +
|
| 216 | + Args: |
| 217 | + msg: The message to display. |
| 218 | + reason: An optional reason for the blocking message, which will be displayed as a subtitle. |
| 219 | + If None, the default text will show. |
| 220 | + """ |
| 221 | + if (msg_movie := get_pc().GetOnlineMessageMovie()) is None: |
| 222 | + return |
| 223 | + |
| 224 | + backup = msg_movie.BlockingSubtitle |
| 225 | + msg_movie.BlockingSubtitle = reason if reason is not None else backup |
| 226 | + msg_movie.DisplayBlockingMessage(msg) |
| 227 | + msg_movie.BlockingSubtitle = backup |
| 228 | + |
| 229 | + |
| 230 | +def blocking_message_hide() -> None: |
| 231 | + """Hides the currently displayed blocking message, if any.""" |
| 232 | + if (msg_movie := get_pc().GetOnlineMessageMovie()) is None: |
| 233 | + return |
| 234 | + |
| 235 | + msg_movie.HideBlocking() |
| 236 | + |
| 237 | + |
| 238 | +@contextmanager |
| 239 | +def display_blocking_message(msg: str, reason: str | None = None) -> Iterator[None]: |
| 240 | + """ |
| 241 | + Context manager to display and hide a blocking message. |
| 242 | +
|
| 243 | + This will display the message when entering the context and hide it when exiting. |
| 244 | +
|
| 245 | + Args: |
| 246 | + msg: The message to display. |
| 247 | + reason: An optional reason for the blocking message, which will be displayed as a subtitle. |
| 248 | + If None, the default text will show. |
| 249 | + """ |
| 250 | + blocking_message_show(msg, reason) |
| 251 | + try: |
| 252 | + yield |
| 253 | + finally: |
| 254 | + blocking_message_hide() |
| 255 | + |
| 256 | + |
| 257 | +def message_show(msg: str) -> None: |
| 258 | + """ |
| 259 | + Displays a message on the left side of the screen. |
| 260 | +
|
| 261 | + Note this message has no character limit, but only scales horizontally. |
| 262 | + Multiple lines will not be displayed correctly. |
| 263 | +
|
| 264 | + Args: |
| 265 | + msg: The message to display. |
| 266 | + """ |
| 267 | + if (msg_movie := get_pc().GetOnlineMessageMovie()) is None: |
| 268 | + return |
| 269 | + |
| 270 | + msg_movie.DisplayMessage(msg) |
| 271 | + |
| 272 | + |
| 273 | +def message_hide() -> None: |
| 274 | + """Hides the currently displayed message in the online message movie.""" |
| 275 | + if (msg_movie := get_pc().GetOnlineMessageMovie()) is None: |
| 276 | + return |
| 277 | + |
| 278 | + msg_movie.Hide() |
| 279 | + |
| 280 | + |
| 281 | +@contextmanager |
| 282 | +def display_message(msg: str) -> Iterator[None]: |
| 283 | + """ |
| 284 | + Context manager to display and hide a message. |
| 285 | +
|
| 286 | + This will display the message when entering the context and hide it when exiting. |
| 287 | +
|
| 288 | + Args: |
| 289 | + msg: The message to display. |
| 290 | + """ |
| 291 | + message_show(msg) |
| 292 | + try: |
| 293 | + yield |
| 294 | + finally: |
| 295 | + message_hide() |
0 commit comments