sketchingpy.pygame_prompt
Simple prompt dialog for local "app" renderer based in Pygame GUI.
Uses MIT licensed code from https://pygame-gui.readthedocs.io.
License:
BSD
1"""Simple prompt dialog for local "app" renderer based in Pygame GUI. 2 3Uses MIT licensed code from https://pygame-gui.readthedocs.io. 4 5License: 6 BSD 7""" 8 9import warnings 10import typing 11 12import pygame # type: ignore 13import pygame_gui.core # type: ignore 14import pygame_gui.core.interfaces # type: ignore 15import pygame_gui.elements # type: ignore 16import pygame_gui._constants # type: ignore 17 18 19class PygameGuiPrompt(pygame_gui.elements.UIWindow): 20 """A prompt dialog for Pygame GUI. 21 22 Simple dialog box which asks the user to confirm (offers a cancel option) with the option for 23 the user to enter in text. 24 """ 25 26 def __init__(self, rect: pygame.Rect, action_long_desc: str, 27 manager=None, window_title: str = 'Prompt', action_short_name: str = 'pygame-gui.OK', 28 blocking: bool = True, object_id=None, visible: int = 1, 29 action_long_desc_text_kwargs: typing.Optional[typing.Dict[str, str]] = None): 30 """Create a new prompt dialog. 31 32 Args: 33 rect: The location and bounds of the dialog. 34 action_long_desc: Long string name description of the action the user is taking. 35 manager: The manager in which to register this dialog. 36 window_title: The title to display at the top of the dialog. 37 action_short_name: Short name description of the action the user is taking in the UI. 38 blocking: Flag indicating if the dialog should be blocking other UI interactions. 39 object_id: The Pygame GUI ID to assign to the prompt. 40 visible: Flag indicating if the prompt should start off being visible. 41 action_long_desc_text_kwargs: Localization and long description strings. 42 """ 43 44 if object_id is None: 45 object_id = pygame_gui.core.ObjectID('#prompt_dialog', None) # type: ignore 46 47 super().__init__( 48 rect, 49 manager, 50 window_display_title=window_title, 51 element_id='prompt_dialog', 52 object_id=object_id, 53 resizable=True, 54 visible=visible 55 ) 56 57 minimum_dimensions = (260, 200) 58 if rect.width < minimum_dimensions[0] or rect.height < minimum_dimensions[1]: 59 warn_string = ' '.join([ 60 'Initial size:', 61 str(rect.size), 62 'is less than minimum dimensions:', 63 str(minimum_dimensions) 64 ]) 65 warnings.warn(warn_string, UserWarning) 66 self.set_minimum_dimensions(minimum_dimensions) 67 68 self._cancel_button = pygame_gui.elements.UIButton( 69 relative_rect=pygame.Rect(-10, -40, -1, 30), 70 text='pygame-gui.Cancel', 71 manager=self.ui_manager, 72 container=self, 73 object_id='#cancel_button', 74 anchors={ 75 'left': 'right', 76 'right': 'right', 77 'top': 'bottom', 78 'bottom': 'bottom' 79 } 80 ) 81 82 self._confirm_button = pygame_gui.elements.UIButton( 83 relative_rect=pygame.Rect(-10, -40, -1, 30), 84 text=action_short_name, 85 manager=self.ui_manager, 86 container=self, 87 object_id='#confirm_button', 88 anchors={ 89 'left': 'right', 90 'right': 'right', 91 'top': 'bottom', 92 'bottom': 'bottom', 93 'left_target': self._cancel_button, 94 'right_target': self._cancel_button 95 } 96 ) 97 98 text_width = self.get_container().get_size()[0] - 10 99 text_height = (self.get_container().get_size()[1] - 50) / 2 - 5 100 self._prompt_text = pygame_gui.elements.UILabel( 101 text=action_long_desc, 102 relative_rect=pygame.Rect(5, 5, text_width, text_height), 103 manager=self.ui_manager, 104 container=self, 105 anchors={ 106 'left': 'left', 107 'right': 'right', 108 'top': 'top', 109 'bottom': 'bottom' 110 }, 111 text_kwargs=action_long_desc_text_kwargs 112 ) 113 114 self._input_box = pygame_gui.elements.UITextEntryBox( 115 relative_rect=pygame.Rect(5, 5 + text_height + 5, text_width, text_height), 116 manager=self.ui_manager, 117 container=self, 118 anchors={ 119 'left': 'left', 120 'right': 'right', 121 'top': 'top', 122 'bottom': 'bottom' 123 } 124 ) 125 126 self._final_text: typing.Optional[str] = None 127 128 self.set_blocking(blocking) 129 130 def get_text(self) -> str: 131 if self._final_text: 132 return self._final_text 133 else: 134 return self._input_box.get_text() 135 136 def process_event(self, event: pygame.event.Event) -> bool: 137 """Process dialog events. 138 139 Args: 140 event: The event to process. 141 """ 142 consumed_event = super().process_event(event) 143 144 button_pressed = event.type == pygame_gui._constants.UI_BUTTON_PRESSED 145 if button_pressed: 146 is_cancel = event.ui_element == self._cancel_button 147 is_confirm = event.ui_element == self._confirm_button 148 else: 149 is_cancel = False 150 is_confirm = False 151 152 if button_pressed and is_cancel: 153 self._final_text = '' 154 self.kill() 155 156 if button_pressed and is_confirm: 157 # old event - to be removed in 0.8.0 158 event_data = { 159 'user_type': pygame_gui._constants.OldType( 160 pygame_gui._constants.UI_CONFIRMATION_DIALOG_CONFIRMED 161 ), 162 'ui_element': self, 163 'ui_object_id': self.most_specific_combined_id 164 } 165 pygame.event.post(pygame.event.Event(pygame.USEREVENT, event_data)) 166 # new event 167 event_data = { 168 'ui_element': self, 169 'ui_object_id': self.most_specific_combined_id 170 } 171 pygame.event.post(pygame.event.Event( 172 pygame_gui._constants.UI_CONFIRMATION_DIALOG_CONFIRMED, 173 event_data 174 )) 175 self._final_text = self._input_box.get_text() 176 self.kill() 177 178 return consumed_event
class
PygameGuiPrompt(pygame_gui.elements.ui_window.UIWindow):
20class PygameGuiPrompt(pygame_gui.elements.UIWindow): 21 """A prompt dialog for Pygame GUI. 22 23 Simple dialog box which asks the user to confirm (offers a cancel option) with the option for 24 the user to enter in text. 25 """ 26 27 def __init__(self, rect: pygame.Rect, action_long_desc: str, 28 manager=None, window_title: str = 'Prompt', action_short_name: str = 'pygame-gui.OK', 29 blocking: bool = True, object_id=None, visible: int = 1, 30 action_long_desc_text_kwargs: typing.Optional[typing.Dict[str, str]] = None): 31 """Create a new prompt dialog. 32 33 Args: 34 rect: The location and bounds of the dialog. 35 action_long_desc: Long string name description of the action the user is taking. 36 manager: The manager in which to register this dialog. 37 window_title: The title to display at the top of the dialog. 38 action_short_name: Short name description of the action the user is taking in the UI. 39 blocking: Flag indicating if the dialog should be blocking other UI interactions. 40 object_id: The Pygame GUI ID to assign to the prompt. 41 visible: Flag indicating if the prompt should start off being visible. 42 action_long_desc_text_kwargs: Localization and long description strings. 43 """ 44 45 if object_id is None: 46 object_id = pygame_gui.core.ObjectID('#prompt_dialog', None) # type: ignore 47 48 super().__init__( 49 rect, 50 manager, 51 window_display_title=window_title, 52 element_id='prompt_dialog', 53 object_id=object_id, 54 resizable=True, 55 visible=visible 56 ) 57 58 minimum_dimensions = (260, 200) 59 if rect.width < minimum_dimensions[0] or rect.height < minimum_dimensions[1]: 60 warn_string = ' '.join([ 61 'Initial size:', 62 str(rect.size), 63 'is less than minimum dimensions:', 64 str(minimum_dimensions) 65 ]) 66 warnings.warn(warn_string, UserWarning) 67 self.set_minimum_dimensions(minimum_dimensions) 68 69 self._cancel_button = pygame_gui.elements.UIButton( 70 relative_rect=pygame.Rect(-10, -40, -1, 30), 71 text='pygame-gui.Cancel', 72 manager=self.ui_manager, 73 container=self, 74 object_id='#cancel_button', 75 anchors={ 76 'left': 'right', 77 'right': 'right', 78 'top': 'bottom', 79 'bottom': 'bottom' 80 } 81 ) 82 83 self._confirm_button = pygame_gui.elements.UIButton( 84 relative_rect=pygame.Rect(-10, -40, -1, 30), 85 text=action_short_name, 86 manager=self.ui_manager, 87 container=self, 88 object_id='#confirm_button', 89 anchors={ 90 'left': 'right', 91 'right': 'right', 92 'top': 'bottom', 93 'bottom': 'bottom', 94 'left_target': self._cancel_button, 95 'right_target': self._cancel_button 96 } 97 ) 98 99 text_width = self.get_container().get_size()[0] - 10 100 text_height = (self.get_container().get_size()[1] - 50) / 2 - 5 101 self._prompt_text = pygame_gui.elements.UILabel( 102 text=action_long_desc, 103 relative_rect=pygame.Rect(5, 5, text_width, text_height), 104 manager=self.ui_manager, 105 container=self, 106 anchors={ 107 'left': 'left', 108 'right': 'right', 109 'top': 'top', 110 'bottom': 'bottom' 111 }, 112 text_kwargs=action_long_desc_text_kwargs 113 ) 114 115 self._input_box = pygame_gui.elements.UITextEntryBox( 116 relative_rect=pygame.Rect(5, 5 + text_height + 5, text_width, text_height), 117 manager=self.ui_manager, 118 container=self, 119 anchors={ 120 'left': 'left', 121 'right': 'right', 122 'top': 'top', 123 'bottom': 'bottom' 124 } 125 ) 126 127 self._final_text: typing.Optional[str] = None 128 129 self.set_blocking(blocking) 130 131 def get_text(self) -> str: 132 if self._final_text: 133 return self._final_text 134 else: 135 return self._input_box.get_text() 136 137 def process_event(self, event: pygame.event.Event) -> bool: 138 """Process dialog events. 139 140 Args: 141 event: The event to process. 142 """ 143 consumed_event = super().process_event(event) 144 145 button_pressed = event.type == pygame_gui._constants.UI_BUTTON_PRESSED 146 if button_pressed: 147 is_cancel = event.ui_element == self._cancel_button 148 is_confirm = event.ui_element == self._confirm_button 149 else: 150 is_cancel = False 151 is_confirm = False 152 153 if button_pressed and is_cancel: 154 self._final_text = '' 155 self.kill() 156 157 if button_pressed and is_confirm: 158 # old event - to be removed in 0.8.0 159 event_data = { 160 'user_type': pygame_gui._constants.OldType( 161 pygame_gui._constants.UI_CONFIRMATION_DIALOG_CONFIRMED 162 ), 163 'ui_element': self, 164 'ui_object_id': self.most_specific_combined_id 165 } 166 pygame.event.post(pygame.event.Event(pygame.USEREVENT, event_data)) 167 # new event 168 event_data = { 169 'ui_element': self, 170 'ui_object_id': self.most_specific_combined_id 171 } 172 pygame.event.post(pygame.event.Event( 173 pygame_gui._constants.UI_CONFIRMATION_DIALOG_CONFIRMED, 174 event_data 175 )) 176 self._final_text = self._input_box.get_text() 177 self.kill() 178 179 return consumed_event
A prompt dialog for Pygame GUI.
Simple dialog box which asks the user to confirm (offers a cancel option) with the option for the user to enter in text.
PygameGuiPrompt( rect: pygame.rect.Rect, action_long_desc: str, manager=None, window_title: str = 'Prompt', action_short_name: str = 'pygame-gui.OK', blocking: bool = True, object_id=None, visible: int = 1, action_long_desc_text_kwargs: Optional[Dict[str, str]] = None)
27 def __init__(self, rect: pygame.Rect, action_long_desc: str, 28 manager=None, window_title: str = 'Prompt', action_short_name: str = 'pygame-gui.OK', 29 blocking: bool = True, object_id=None, visible: int = 1, 30 action_long_desc_text_kwargs: typing.Optional[typing.Dict[str, str]] = None): 31 """Create a new prompt dialog. 32 33 Args: 34 rect: The location and bounds of the dialog. 35 action_long_desc: Long string name description of the action the user is taking. 36 manager: The manager in which to register this dialog. 37 window_title: The title to display at the top of the dialog. 38 action_short_name: Short name description of the action the user is taking in the UI. 39 blocking: Flag indicating if the dialog should be blocking other UI interactions. 40 object_id: The Pygame GUI ID to assign to the prompt. 41 visible: Flag indicating if the prompt should start off being visible. 42 action_long_desc_text_kwargs: Localization and long description strings. 43 """ 44 45 if object_id is None: 46 object_id = pygame_gui.core.ObjectID('#prompt_dialog', None) # type: ignore 47 48 super().__init__( 49 rect, 50 manager, 51 window_display_title=window_title, 52 element_id='prompt_dialog', 53 object_id=object_id, 54 resizable=True, 55 visible=visible 56 ) 57 58 minimum_dimensions = (260, 200) 59 if rect.width < minimum_dimensions[0] or rect.height < minimum_dimensions[1]: 60 warn_string = ' '.join([ 61 'Initial size:', 62 str(rect.size), 63 'is less than minimum dimensions:', 64 str(minimum_dimensions) 65 ]) 66 warnings.warn(warn_string, UserWarning) 67 self.set_minimum_dimensions(minimum_dimensions) 68 69 self._cancel_button = pygame_gui.elements.UIButton( 70 relative_rect=pygame.Rect(-10, -40, -1, 30), 71 text='pygame-gui.Cancel', 72 manager=self.ui_manager, 73 container=self, 74 object_id='#cancel_button', 75 anchors={ 76 'left': 'right', 77 'right': 'right', 78 'top': 'bottom', 79 'bottom': 'bottom' 80 } 81 ) 82 83 self._confirm_button = pygame_gui.elements.UIButton( 84 relative_rect=pygame.Rect(-10, -40, -1, 30), 85 text=action_short_name, 86 manager=self.ui_manager, 87 container=self, 88 object_id='#confirm_button', 89 anchors={ 90 'left': 'right', 91 'right': 'right', 92 'top': 'bottom', 93 'bottom': 'bottom', 94 'left_target': self._cancel_button, 95 'right_target': self._cancel_button 96 } 97 ) 98 99 text_width = self.get_container().get_size()[0] - 10 100 text_height = (self.get_container().get_size()[1] - 50) / 2 - 5 101 self._prompt_text = pygame_gui.elements.UILabel( 102 text=action_long_desc, 103 relative_rect=pygame.Rect(5, 5, text_width, text_height), 104 manager=self.ui_manager, 105 container=self, 106 anchors={ 107 'left': 'left', 108 'right': 'right', 109 'top': 'top', 110 'bottom': 'bottom' 111 }, 112 text_kwargs=action_long_desc_text_kwargs 113 ) 114 115 self._input_box = pygame_gui.elements.UITextEntryBox( 116 relative_rect=pygame.Rect(5, 5 + text_height + 5, text_width, text_height), 117 manager=self.ui_manager, 118 container=self, 119 anchors={ 120 'left': 'left', 121 'right': 'right', 122 'top': 'top', 123 'bottom': 'bottom' 124 } 125 ) 126 127 self._final_text: typing.Optional[str] = None 128 129 self.set_blocking(blocking)
Create a new prompt dialog.
Arguments:
- rect: The location and bounds of the dialog.
- action_long_desc: Long string name description of the action the user is taking.
- manager: The manager in which to register this dialog.
- window_title: The title to display at the top of the dialog.
- action_short_name: Short name description of the action the user is taking in the UI.
- blocking: Flag indicating if the dialog should be blocking other UI interactions.
- object_id: The Pygame GUI ID to assign to the prompt.
- visible: Flag indicating if the prompt should start off being visible.
- action_long_desc_text_kwargs: Localization and long description strings.
def
process_event(self, event: pygame.event.Event) -> bool:
137 def process_event(self, event: pygame.event.Event) -> bool: 138 """Process dialog events. 139 140 Args: 141 event: The event to process. 142 """ 143 consumed_event = super().process_event(event) 144 145 button_pressed = event.type == pygame_gui._constants.UI_BUTTON_PRESSED 146 if button_pressed: 147 is_cancel = event.ui_element == self._cancel_button 148 is_confirm = event.ui_element == self._confirm_button 149 else: 150 is_cancel = False 151 is_confirm = False 152 153 if button_pressed and is_cancel: 154 self._final_text = '' 155 self.kill() 156 157 if button_pressed and is_confirm: 158 # old event - to be removed in 0.8.0 159 event_data = { 160 'user_type': pygame_gui._constants.OldType( 161 pygame_gui._constants.UI_CONFIRMATION_DIALOG_CONFIRMED 162 ), 163 'ui_element': self, 164 'ui_object_id': self.most_specific_combined_id 165 } 166 pygame.event.post(pygame.event.Event(pygame.USEREVENT, event_data)) 167 # new event 168 event_data = { 169 'ui_element': self, 170 'ui_object_id': self.most_specific_combined_id 171 } 172 pygame.event.post(pygame.event.Event( 173 pygame_gui._constants.UI_CONFIRMATION_DIALOG_CONFIRMED, 174 event_data 175 )) 176 self._final_text = self._input_box.get_text() 177 self.kill() 178 179 return consumed_event
Process dialog events.
Arguments:
- event: The event to process.
Inherited Members
- pygame_gui.elements.ui_window.UIWindow
- window_display_title
- resizable
- draggable
- edge_hovering
- minimum_dimensions
- bring_to_front_on_focused
- is_blocking
- resizing_mode_active
- start_resize_point
- start_resize_rect
- grabbed_window
- starting_grab_difference
- background_colour
- border_colour
- shape
- enable_title_bar
- title_bar_height
- window_element_container
- title_bar
- window_stack
- always_on_top
- set_blocking
- set_dimensions
- set_relative_position
- set_position
- check_clicked_inside_or_blocking
- update
- get_container
- can_hover
- check_hover
- get_top_layer
- change_layer
- kill
- rebuild
- rebuild_from_changed_theme_data
- should_use_window_edge_resize_cursor
- get_hovering_edge_id
- on_moved_to_front
- set_display_title
- disable
- enable
- show
- hide
- get_relative_mouse_pos
- are_contents_hovered
- get_layer_thickness
- pygame_gui.core.ui_element.UIElement
- ui_manager
- ui_container
- parent_element
- ui_group
- ui_theme
- object_ids
- class_ids
- element_ids
- element_base_ids
- combined_element_ids
- most_specific_combined_id
- shadow_width
- border_width
- shape_corner_radius
- tool_tip_text
- tool_tip_text_kwargs
- tool_tip_object_id
- tool_tip_delay
- tool_tip_wrap_width
- tool_tip
- rect
- dynamic_width
- dynamic_height
- anchors
- drawable_shape
- image
- blendmode
- relative_bottom_margin
- relative_right_margin
- layer_thickness
- starting_height
- is_enabled
- is_focused
- hover_time
- pre_debug_image
- dirty
- hovered
- set_container
- get_focus_set
- set_focus_set
- join_focus_sets
- remove_element_from_focus_set
- get_relative_rect
- get_abs_rect
- get_element_base_ids
- get_element_ids
- get_class_ids
- get_object_ids
- get_anchors
- set_anchors
- change_object_id
- update_containing_rect_position
- set_minimum_dimensions
- on_fresh_drawable_shape_ready
- on_hovered
- on_unhovered
- while_hovering
- hover_point
- focus
- unfocus
- set_visual_debug_mode
- get_image_clipping_rect
- set_image
- get_starting_height
- on_locale_changed
- get_anchor_targets
- tuple_extract
- update_theming
- set_tooltip
- pygame_gui.core.layered_gui_group.GUISprite
- blit_data
- source_rect
- add
- remove
- add_internal
- remove_internal
- groups
- alive
- visible
- layer