Skip to content

Commit

Permalink
feat: make it possible to deactivate the other functionalities when c…
Browse files Browse the repository at this point in the history
…licking on unpause
  • Loading branch information
Nathaniavm committed Nov 11, 2024
1 parent 409344e commit 3a33ebb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
32 changes: 12 additions & 20 deletions agent_parts_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from src.agent_parts.rectangle import Point
from src.agent_parts.creature import Creature

#To do : make it only possible to create limb or create joint
#To do : unclick the only_one_simultaneously_buttons when unpausing

def main():
# Initialize Pygame and Pymunk
Expand All @@ -34,6 +34,7 @@ def main():
make_limb_mode = False
make_motorjoint_mode = False

# Track the physics value
physics_value = 0

def handle_physics():
Expand Down Expand Up @@ -110,23 +111,12 @@ def add_motorjoint():
vision: Vision = Vision(Point(0,0))
creature = Creature(space, vision)

# Add limbs to the creature, placing them above the ground
#limb1 = creature.add_limb(100, 20, (300, 100), mass=1) # Positioned above the ground
#limb2 = creature.add_limb(100, 20, (350, 100), mass=1) # Positioned above the ground
#limb3 = creature.add_limb(110, 20, (400, 100), mass=5)

# Add a motor between limbs
#creature.add_motor(limb1, limb2, (50, 0), (-25, 0), rate=2, tolerance=30)
#creature.add_motor(limb2, limb3, (37, 0), (-23, 0), rate=-2, tolerance=50)

# Add limbs to the creature
limb1 = creature.add_limb(100, 20, (300, 300), mass=1)
limb2 = creature.add_limb(100, 20, (350, 300), mass=3)
limb3 = creature.add_limb(80, 40, (400, 300), mass=5)

# Add motors between limbs
#creature.add_motor(limb1, limb2, (25, 0), (-25, 0), rate=2)
#creature.add_motor(limb2, limb3, (37, 0), (-23, 0), rate=-2)
creature.add_motor_on_limbs(limb1, limb2, (325, 300))
creature.add_motor_on_limbs(limb2, limb3, (375, 300))

Expand Down Expand Up @@ -159,15 +149,16 @@ def add_motorjoint():
elif event.type == pygame.MOUSEBUTTONDOWN:
active_button = interface.handle_only_one_function(event)

# To make it possible to only click one of the only_one_simultaneously_buttons
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:
elif 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 @@ -202,6 +193,12 @@ def add_motorjoint():
else:
limbs_hovered.clear()

# If Pause button is clicked and any only_one_simultaneously_buttons are active, deactivate the active button
elif not active_button:
if interface.any_active_only_one_simultaneously_buttons_active():
interface.active_button.deactivate()
print("Deactivated all mutually exclusive buttons due to Pause")

elif event.type == MOUSEMOTION and make_limb_mode:
mouse_x, mouse_y = event.pos
mouse_pos = (mouse_x, mouse_y)
Expand Down Expand Up @@ -234,12 +231,7 @@ def add_motorjoint():

#creature.set_joint_rates([random.random()*2, random.random()*2])
# Render the creature
creature.render(screen)

#if not physics_on:
#interface.add_button(limb_button)
#else:
#interface.remove_button(limb_button)
creature.render(screen)
interface.render(screen)


Expand Down
11 changes: 4 additions & 7 deletions src/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,21 @@ def handle_events(self, event):
"""Handle events for all UI elements."""
for button in self.buttons:
button.is_clicked(event)


def handle_only_one_function(self, event, active_button: Button):
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 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
self.active_button = button
# 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

def any_active_only_one_simultaneously_buttons_active(self) -> bool:
return any(button.toggled for button in self.only_one_simultaneously_buttons)



Expand Down

0 comments on commit 3a33ebb

Please sign in to comment.