예제 #1
0
    def _display(self, screen):
        if self.selector.clicked:
            self.selected = True

        if self.selected:
            self._position_selector()

            for event in events.get():
                if event.type == pygame.MOUSEBUTTONUP:
                    self.selected = False
예제 #2
0
    def _update_clicked(self):
        clickrect = self.location.resolve()

        for event in events.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                if clickrect.collidepoint(pygame.mouse.get_pos()):
                    self.clicked = True
                    self._on_click_on()

                else:
                    self.clicked = False
                    self._on_click_off()
예제 #3
0
    def _position_selector(self):
        for event in events.get():
            if event.type == pygame.MOUSEMOTION:
                rel = event.rel[0] / self.scale
                rel += self.unhandled_change
                int_rel = int(rel)
                self.unhandled_change = rel - int_rel

                r = self.selector.location
                r.x += int_rel

                r.x = 0 if r.x < 0 else r.x
                r.x = self.location.w - r.w if self.location.w < r.x + r.w else r.x

                self.__dict__["percent"] = int(r.x / (self.location.w - r.w) *
                                               100)
예제 #4
0
    def _handle_clicks(self):
        clickrect = self.location.resolve()

        for event in events.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                if clickrect.collidepoint(*pygame.mouse.get_pos()):
                    if not self.clicked:
                        self.clicked = True
                        if not self.del_after_input:
                            self.del_after_input = self.del_after_input_setting

                elif self.clicked:
                    self.clicked = False
                    if (
                            len(self.text.text) == 0
                    ):  # if you click off when it has nothing it will reset
                        self.text.text = self.initial_text
예제 #5
0
    def _display(self, screen):
        # setup needs to happen AFTER the parent has been assigned
        if not self.setup:
            if not isinstance(self.parent, Selectable.ALLOWED_PARENTS):
                raise TypeError(
                    f"Component 'Selectable' cannot be added to a '{self.parent.__class__.__name__}' element"
                )
            self.setup = True

        # metrics data
        self.rect_metrics = self.parent.text.get_rect_metrics()
        self.rect_metrics_byline = self.parent.text.get_rect_metrics_byline()

        # if the text has changed, everything should be regenerated
        if self.parent.text.text != self.relevant_text:
            self.down_at = False
            self.up_at = False
            self.indices_state = False
            self.set_bounds_ = False
            self.relevant_text = self.parent.text.text

        for event in events.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.set_bounds_ = False
                self.indices_state = False
                if not self.down_at:
                    self.down_at = event.pos
                else:
                    self.down_at = False
                    self.up_at = False

            if event.type == pygame.MOUSEBUTTONUP:
                if self.down_at:
                    # can't have the same down_at and up_at
                    if event.pos == self.down_at:
                        self.down_at = False
                        self.up_at = False

                    else:
                        self.up_at = event.pos

            if self.indices_state:
                for textevent in key.get_all_events(True):
                    if textevent.key == pygame.K_c and textevent.mod & key.MOD:
                        key.remove_event(textevent)

                        start, end = self.get_bounds()
                        txt = self.parent.text.text[start:end + 1]
                        scrap.put(pygame.SCRAP_TEXT, txt)

        if self.set_bounds_:
            self.indices_state = self.set_bounds_

        elif self.down_at:
            pos = pygame.mouse.get_pos()
            rect = self.parent.location.resolve()

            if not self.indices_state or not self.up_at:

                p1 = pygame.mouse.get_pos() if not self.up_at else self.up_at
                p2 = self.down_at

                # if you've clicked but haven't dragged it shouldn't select the letter
                if p1 != p2:
                    self.indices_state = self._calculate_bounds(p1, p2)

        if self.indices_state:
            self._draw_bounds(screen, self.get_bounds())