Skip to content

Commit

Permalink
Kivy: Add a button prompt box
Browse files Browse the repository at this point in the history
  • Loading branch information
alwaysintreble committed Jun 5, 2024
1 parent da2f0f9 commit b60d6d7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
5 changes: 5 additions & 0 deletions data/client.kv
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,8 @@
rectangle: self.x-2, self.y-2, self.width+4, self.height+4
<ServerToolTip>:
pos_hint: {'center_y': 0.5, 'center_x': 0.5}
<MessageBoxLabel>:
valign: "middle"
halign: "center"
text_size: self.width, None
height: self.texture_size[1]
43 changes: 33 additions & 10 deletions kvui.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,25 +380,48 @@ def insert_text(self, substring, from_undo=False):
return super(ConnectBarTextInput, self).insert_text(s, from_undo=from_undo)


class MessageBox(Popup):
class MessageBoxLabel(Label):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class MessageBoxLabel(Label):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._label.refresh()
self.size = self._label.texture.size
if self.width + 50 > Window.width:
self.text_size[0] = Window.width - 50
self._label.refresh()
self.size = self._label.texture.size
if self.width + 50 > Window.width:
self.text_size[0] = Window.width - 50
self._label.refresh()
self.size = self._label.texture.size


class MessageBox(Popup):

def __init__(self, title, text, error=False, **kwargs):
label = MessageBox.MessageBoxLabel(text=text)
label = MessageBoxLabel(text=text)
separator_color = [217 / 255, 129 / 255, 122 / 255, 1.] if error else [47 / 255., 167 / 255., 212 / 255, 1.]
super().__init__(title=title, content=label, size_hint=(None, None), width=max(100, int(label.width) + 40),
super().__init__(title=title, content=label, size_hint=(0.4, 0.4), width=max(100, int(label.width) + 40),
separator_color=separator_color, **kwargs)
self.height += max(0, label.height - 18)


class ButtonsPrompt(Popup):
def __init__(self, title: str, text: str, response: typing.Callable[[str], None],
*prompts: str, **kwargs):
layout = BoxLayout(orientation="vertical")
label = MessageBoxLabel(text=text)
layout.add_widget(label)
button_layout = BoxLayout(orientation="horizontal", size_hint_y=0.4)

def on_release(button: Button, *args):
self.dismiss()
response(button.text)

for prompt in prompts:
button_layout.add_widget(Button(text=prompt, on_release=on_release))
layout.add_widget(button_layout)

separator_color = [47 / 255., 167 / 255., 212 / 255, 1.]
super().__init__(title=title, content=layout, size_hint=(0.4, 0.4), width=max(100, int(label.width) + 40),
separator_color=separator_color, auto_dismiss=False, **kwargs)


class GameManager(App):
logging_pairs = [
("Client", "Archipelago"),
Expand Down

0 comments on commit b60d6d7

Please sign in to comment.