Exemple #1
0
    def __init__(self,
                 prompt_text: str,
                 card_names: List[str],
                 button_names: List[str],
                 layout_info: Union[LayoutInfo, None] = None,
                 container: Union[pygame.Rect, 'UIElement', None] = None,
                 padding: Union[LayoutInfo, None] = None):
        super().__init__(layout_info, container, padding)
        self.selected_cards = []

        self.label = Label(prompt_text, get_default_layout(), None)
        label_width, label_height = self.label.element.font.size(
            self.label.text)
        self.label.layout_info.width = label_width
        prompt_li = LayoutInfo(left=0, right=0, top=0, height=label_height)
        prompt = Stack([self.label], 0, Orientation.HORIZONTAL,
                       Position.CENTER, prompt_li, self)

        scroll_li = LayoutInfo(left=0, right=0, top=0, height=.7)
        self.scroll_container = HorizontalScrollContainer(
            scroll_li, self, CardView, 0.035)
        self.card_view = self.scroll_container.scrollable
        old_on_scroll = self.card_view.on_scroll

        def new_on_scroll(*args, **kwargs):
            for card in self.card_view.cards:
                self.unsubscribe(card.image, 'on_click')
                self.unsubscribe(card.image, 'on_mouse_enter')
                self.unsubscribe(card.image, 'on_mouse_leave')
            old_on_scroll(*args, **kwargs)
            for card in self.card_view.cards:
                card.border_on_hover = False
                self.subscribe(card.image, 'on_click', self)
                self.subscribe(card.image, 'on_mouse_enter', self)
                self.subscribe(card.image, 'on_mouse_leave', self)

        self.card_view.on_scroll = new_on_scroll
        self.set_cards(card_names)

        self.buttons = {}
        button_stack = self.create_buttons(button_names)
        stack_elements = [prompt, self.scroll_container, button_stack]
        self.stack = Stack(ui_elements=stack_elements,
                           spacing=stack_spacing,
                           orientation=Orientation.VERTICAL,
                           alignment=Position.TOP,
                           layout_info=get_default_layout(),
                           container=self,
                           initial_spacing=True)

        self.layout(only_if_changed=False)
    def set_image(self, image_name):
        if self.image is not None:
            self.image.kill()
            self.image = None

        if image_name is not None:
            image_path = f'{images_dir}/{get_card_name(image_name)}.jpg'
            self.image = Image(get_default_layout(), self, image_path)
 def __init__(self,
              layout_info: LayoutInfo,
              container: UIElement,
              orientation: Orientation):
     self.orientation = orientation
     self.offset = 0
     super().__init__(layout_info, container)
     self.thumb = ScrollThumb(get_default_layout(),
                              self,
                              lambda: self.container.scrollable.content_size,
                              lambda: self.container.scrollable.offset,
                              RED)
    def cards(self, card_names: List[str]):
        if len(card_names) < len(self.cards):
            self.first_index = max(0, self.first_index - (len(self.cards) - len(card_names)))
        self._kill_cards()
        self._cards = []
        for card_name in card_names:
            card = Card(layout_info=get_default_layout(),
                        container=self,
                        image_name=card_name)

            card.enabled = card_name not in self.disabled_cards
            self.subscribe(card.image, 'on_click')
            self._cards.append(card)
        self.layout(only_if_changed=False)
Exemple #5
0
    def piles(self, pile_names: List[str]):
        self._kill_piles()
        self._piles = []
        for pile_name in pile_names:
            pile = Pile(layout_info=get_default_layout(),
                        container=self,
                        image_name=pile_name)

            pile.enabled = pile_name not in self.disabled_piles

            self.subscribe(pile.image, 'on_click')

            self._piles.append(pile)

        self.layout(only_if_changed=False)
Exemple #6
0
 def on_enable(self, enabled: bool):
     image_path = self.image_path if enabled else self.gray_image_path
     self._kill_image()
     self.image = Image(get_default_layout(), self, image_path)
     self.subscribe(self.image, 'on_mouse_enter', self)
     self.subscribe(self.image, 'on_mouse_leave', self)
def calculate_text_size(text, padding):
    global dummy
    if dummy is None:
        dummy = Label('', get_default_layout(), None).element
    return (x + padding for x in dummy.font.size(text))