Skip to content

Commit

Permalink
feat: make it possible to only create either limb or joint once a time
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniavm committed Nov 11, 2024
1 parent a31eb3c commit 409344e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
22 changes: 18 additions & 4 deletions agent_parts_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from src.agent_parts.rectangle import Point
from src.agent_parts.creature import Creature

#NOTE_TO_MYSELF: When add limb is clicked it doesn't go away when unpaused
#To do : make it only possible to create limb or create joint

def main():
# Initialize Pygame and Pymunk
pygame.init()
Expand Down Expand Up @@ -94,8 +95,8 @@ def add_motorjoint():
)

interface.add_button(pause_button)
interface.add_button(limb_button)
interface.add_button(motorjoint_button)
interface.add_only_one_simultaneously_buttons(limb_button)
interface.add_only_one_simultaneously_buttons(motorjoint_button)



Expand Down Expand Up @@ -147,6 +148,7 @@ def add_motorjoint():
if event.type == QUIT:
running = False
interface.handle_events(event)

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
print("Left arrow pressed")
Expand All @@ -155,7 +157,17 @@ def add_motorjoint():
if event.key == pygame.K_SPACE:
handle_physics()
elif event.type == pygame.MOUSEBUTTONDOWN:
if not physics_on:
active_button = interface.handle_only_one_function(event)

if not physics_on and active_button:
if active_button.text == "Add limb":
active_button.is_clicked(event)
print("Limb creation mode activated and motor joint creation mode deactivated")
elif active_button.text == "Add joint":
active_button.is_clicked(event)
print("Motor joint creation mode activated and limb creation mode deactivated")

if not active_button and not physics_on:
mouse_x, mouse_y = event.pos
mouse_pos = (mouse_x, mouse_y)
# List of limbs to make motorjoint on
Expand Down Expand Up @@ -187,6 +199,8 @@ def add_motorjoint():
creature.add_motor_on_limbs(limb_1, limb_2, mouse_pos)
print("Motor joint created between limbs!")
limbs_hovered.clear()
else:
limbs_hovered.clear()

elif event.type == MOUSEMOTION and make_limb_mode:
mouse_x, mouse_y = event.pos
Expand Down
35 changes: 25 additions & 10 deletions src/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,28 @@ def deactivate(self):
class Interface:
def __init__(self):
"""Initializes the elements in the interface"""
self.buttons = []
self.buttons = [] #for all buttons in the interface
self.active_button = Button
self.only_one_simultaneously_buttons = [] #buttons that can't be active simultaneously

def add_button(self, button: Button) -> Button:
self.buttons.append(button)
return button

def add_only_one_simultaneously_buttons(self, button: Button) -> Button:
self.only_one_simultaneously_buttons.append(button)
self.buttons.append(button) #don't have to append on both lists manually
return button

def remove_button(self, button: Button) -> Button:
if button in self.buttons:
self.buttons.remove(button)
return button

def remove_only_one_simultaneously_buttons(self, button: Button) -> Button:
if button in self.only_one_simultaneously_buttons:
self.buttons.remove(button)
return button

def render(self, screen):
"""Render all UI elements."""
Expand All @@ -64,22 +76,25 @@ def handle_events(self, event):
"""Handle events for all UI elements."""
for button in self.buttons:
button.is_clicked(event)
#if isinstance(button, Button):
#button.is_clicked(event)
#self.handle_only_one_function(event, button)


def handle_only_one_function(self, event, active_button: Button):
for button in self.buttons:
for button in self.only_one_simultaneously_buttons:
if button != active_button:
button.deactivate()
active_button.is_clicked(event) # This will call the callback if the button is clicked

def is_any_button_clicked(self, event) -> bool:
for button in self.buttons:
if button.is_clicked(event): # If any button is clicked
return True # Return True immediately
return False
def handle_only_one_function(self, event) -> Button|None:
"""Activates only one of the mutually exclusive buttons."""
for button in self.only_one_simultaneously_buttons:
if button.is_clicked(event): # If this button was clicked
# Deactivate all other mutually exclusive buttons
for other_button in self.only_one_simultaneously_buttons:
if other_button != button:
other_button.deactivate()
return button # Return the clicked (active) button
return None # No button was clicked




Expand Down

0 comments on commit 409344e

Please sign in to comment.