Skip to content
Discussion options

You must be logged in to vote

Here's an example of dragging virtual objects around within a widget, for a simulated aquarium:

def entity_at(offset: Offset, entities: list[Entity]) -> Entity | None:
    for entity in entities:
        if entity.x <= offset.x < entity.x + entity.width and entity.y <= offset.y < entity.y + entity.height:
            return entity
    return None

class Tank(Widget):

    dragging: var[Entity | None] = var[Entity | None](None)
    drag_offset: var[Offset | None] = var[Offset | None](None)

    def on_mouse_down(self, event: events.MouseDown) -> None:
        self.capture_mouse()
        self.dragging = entity_at(event.offset, Entity.instances)
        if self.dragging is not None:
       …

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@TomJGooding
Comment options

TomJGooding Jun 9, 2023
Collaborator Author

@TomJGooding
Comment options

TomJGooding Jun 11, 2023
Collaborator Author

@1j01
Comment options

@TomJGooding
Comment options

TomJGooding Jun 15, 2023
Collaborator Author

Answer selected by TomJGooding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants