bastd.ui.serverdialog

Dialog window controlled by the master server.

 1# Released under the MIT License. See LICENSE for details.
 2#
 3"""Dialog window controlled by the master server."""
 4
 5from __future__ import annotations
 6
 7from typing import TYPE_CHECKING
 8
 9import _ba
10import ba
11
12if TYPE_CHECKING:
13    from typing import Any
14
15
16class ServerDialogWindow(ba.Window):
17    """A dialog window driven by the master-server."""
18
19    def __init__(self, data: dict[str, Any]):
20        self._dialog_id = data['dialogID']
21        txt = ba.Lstr(translate=('serverResponses', data['text']),
22                      subs=data.get('subs', [])).evaluate()
23        txt = txt.strip()
24        txt_scale = 1.5
25        txt_height = (_ba.get_string_height(txt, suppress_warning=True) *
26                      txt_scale)
27        self._width = 500
28        self._height = 130 + min(200, txt_height)
29        uiscale = ba.app.ui.uiscale
30        super().__init__(root_widget=ba.containerwidget(
31            size=(self._width, self._height),
32            transition='in_scale',
33            scale=(1.8 if uiscale is ba.UIScale.SMALL else
34                   1.35 if uiscale is ba.UIScale.MEDIUM else 1.0)))
35        self._starttime = ba.time(ba.TimeType.REAL, ba.TimeFormat.MILLISECONDS)
36
37        ba.playsound(ba.getsound('swish'))
38        ba.textwidget(parent=self._root_widget,
39                      position=(self._width * 0.5,
40                                70 + (self._height - 70) * 0.5),
41                      size=(0, 0),
42                      color=(1.0, 3.0, 1.0),
43                      scale=txt_scale,
44                      h_align='center',
45                      v_align='center',
46                      text=txt,
47                      maxwidth=self._width * 0.85,
48                      max_height=(self._height - 110))
49        show_cancel = data.get('showCancel', True)
50        self._cancel_button: ba.Widget | None
51        if show_cancel:
52            self._cancel_button = ba.buttonwidget(
53                parent=self._root_widget,
54                position=(30, 30),
55                size=(160, 60),
56                autoselect=True,
57                label=ba.Lstr(resource='cancelText'),
58                on_activate_call=self._cancel_press)
59        else:
60            self._cancel_button = None
61        self._ok_button = ba.buttonwidget(
62            parent=self._root_widget,
63            position=((self._width - 182) if show_cancel else
64                      (self._width * 0.5 - 80), 30),
65            size=(160, 60),
66            autoselect=True,
67            label=ba.Lstr(resource='okText'),
68            on_activate_call=self._ok_press)
69        ba.containerwidget(edit=self._root_widget,
70                           cancel_button=self._cancel_button,
71                           start_button=self._ok_button,
72                           selected_child=self._ok_button)
73
74    def _ok_press(self) -> None:
75        if ba.time(ba.TimeType.REAL,
76                   ba.TimeFormat.MILLISECONDS) - self._starttime < 1000:
77            ba.playsound(ba.getsound('error'))
78            return
79        _ba.add_transaction({
80            'type': 'DIALOG_RESPONSE',
81            'dialogID': self._dialog_id,
82            'response': 1
83        })
84        ba.containerwidget(edit=self._root_widget, transition='out_scale')
85
86    def _cancel_press(self) -> None:
87        if ba.time(ba.TimeType.REAL,
88                   ba.TimeFormat.MILLISECONDS) - self._starttime < 1000:
89            ba.playsound(ba.getsound('error'))
90            return
91        _ba.add_transaction({
92            'type': 'DIALOG_RESPONSE',
93            'dialogID': self._dialog_id,
94            'response': 0
95        })
96        ba.containerwidget(edit=self._root_widget, transition='out_scale')
class ServerDialogWindow(ba.ui.Window):
17class ServerDialogWindow(ba.Window):
18    """A dialog window driven by the master-server."""
19
20    def __init__(self, data: dict[str, Any]):
21        self._dialog_id = data['dialogID']
22        txt = ba.Lstr(translate=('serverResponses', data['text']),
23                      subs=data.get('subs', [])).evaluate()
24        txt = txt.strip()
25        txt_scale = 1.5
26        txt_height = (_ba.get_string_height(txt, suppress_warning=True) *
27                      txt_scale)
28        self._width = 500
29        self._height = 130 + min(200, txt_height)
30        uiscale = ba.app.ui.uiscale
31        super().__init__(root_widget=ba.containerwidget(
32            size=(self._width, self._height),
33            transition='in_scale',
34            scale=(1.8 if uiscale is ba.UIScale.SMALL else
35                   1.35 if uiscale is ba.UIScale.MEDIUM else 1.0)))
36        self._starttime = ba.time(ba.TimeType.REAL, ba.TimeFormat.MILLISECONDS)
37
38        ba.playsound(ba.getsound('swish'))
39        ba.textwidget(parent=self._root_widget,
40                      position=(self._width * 0.5,
41                                70 + (self._height - 70) * 0.5),
42                      size=(0, 0),
43                      color=(1.0, 3.0, 1.0),
44                      scale=txt_scale,
45                      h_align='center',
46                      v_align='center',
47                      text=txt,
48                      maxwidth=self._width * 0.85,
49                      max_height=(self._height - 110))
50        show_cancel = data.get('showCancel', True)
51        self._cancel_button: ba.Widget | None
52        if show_cancel:
53            self._cancel_button = ba.buttonwidget(
54                parent=self._root_widget,
55                position=(30, 30),
56                size=(160, 60),
57                autoselect=True,
58                label=ba.Lstr(resource='cancelText'),
59                on_activate_call=self._cancel_press)
60        else:
61            self._cancel_button = None
62        self._ok_button = ba.buttonwidget(
63            parent=self._root_widget,
64            position=((self._width - 182) if show_cancel else
65                      (self._width * 0.5 - 80), 30),
66            size=(160, 60),
67            autoselect=True,
68            label=ba.Lstr(resource='okText'),
69            on_activate_call=self._ok_press)
70        ba.containerwidget(edit=self._root_widget,
71                           cancel_button=self._cancel_button,
72                           start_button=self._ok_button,
73                           selected_child=self._ok_button)
74
75    def _ok_press(self) -> None:
76        if ba.time(ba.TimeType.REAL,
77                   ba.TimeFormat.MILLISECONDS) - self._starttime < 1000:
78            ba.playsound(ba.getsound('error'))
79            return
80        _ba.add_transaction({
81            'type': 'DIALOG_RESPONSE',
82            'dialogID': self._dialog_id,
83            'response': 1
84        })
85        ba.containerwidget(edit=self._root_widget, transition='out_scale')
86
87    def _cancel_press(self) -> None:
88        if ba.time(ba.TimeType.REAL,
89                   ba.TimeFormat.MILLISECONDS) - self._starttime < 1000:
90            ba.playsound(ba.getsound('error'))
91            return
92        _ba.add_transaction({
93            'type': 'DIALOG_RESPONSE',
94            'dialogID': self._dialog_id,
95            'response': 0
96        })
97        ba.containerwidget(edit=self._root_widget, transition='out_scale')

A dialog window driven by the master-server.

ServerDialogWindow(data: dict[str, typing.Any])
20    def __init__(self, data: dict[str, Any]):
21        self._dialog_id = data['dialogID']
22        txt = ba.Lstr(translate=('serverResponses', data['text']),
23                      subs=data.get('subs', [])).evaluate()
24        txt = txt.strip()
25        txt_scale = 1.5
26        txt_height = (_ba.get_string_height(txt, suppress_warning=True) *
27                      txt_scale)
28        self._width = 500
29        self._height = 130 + min(200, txt_height)
30        uiscale = ba.app.ui.uiscale
31        super().__init__(root_widget=ba.containerwidget(
32            size=(self._width, self._height),
33            transition='in_scale',
34            scale=(1.8 if uiscale is ba.UIScale.SMALL else
35                   1.35 if uiscale is ba.UIScale.MEDIUM else 1.0)))
36        self._starttime = ba.time(ba.TimeType.REAL, ba.TimeFormat.MILLISECONDS)
37
38        ba.playsound(ba.getsound('swish'))
39        ba.textwidget(parent=self._root_widget,
40                      position=(self._width * 0.5,
41                                70 + (self._height - 70) * 0.5),
42                      size=(0, 0),
43                      color=(1.0, 3.0, 1.0),
44                      scale=txt_scale,
45                      h_align='center',
46                      v_align='center',
47                      text=txt,
48                      maxwidth=self._width * 0.85,
49                      max_height=(self._height - 110))
50        show_cancel = data.get('showCancel', True)
51        self._cancel_button: ba.Widget | None
52        if show_cancel:
53            self._cancel_button = ba.buttonwidget(
54                parent=self._root_widget,
55                position=(30, 30),
56                size=(160, 60),
57                autoselect=True,
58                label=ba.Lstr(resource='cancelText'),
59                on_activate_call=self._cancel_press)
60        else:
61            self._cancel_button = None
62        self._ok_button = ba.buttonwidget(
63            parent=self._root_widget,
64            position=((self._width - 182) if show_cancel else
65                      (self._width * 0.5 - 80), 30),
66            size=(160, 60),
67            autoselect=True,
68            label=ba.Lstr(resource='okText'),
69            on_activate_call=self._ok_press)
70        ba.containerwidget(edit=self._root_widget,
71                           cancel_button=self._cancel_button,
72                           start_button=self._ok_button,
73                           selected_child=self._ok_button)
Inherited Members
ba.ui.Window
get_root_widget