Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tools.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools.py')
-rw-r--r--tools.py332
1 files changed, 238 insertions, 94 deletions
diff --git a/tools.py b/tools.py
index 7911168..bb9dbf8 100644
--- a/tools.py
+++ b/tools.py
@@ -31,6 +31,10 @@ from sugar.activity import activity
import gtk
import math
+PALETTE_MODE_SLIDER_ICON = 0
+PALETTE_MODE_ICONS = 1
+PALETTE_MODE_SLIDER_LABEL = 2
+
# Tools that can be superlcassed
class Tool(object):
@@ -38,6 +42,8 @@ class Tool(object):
icon = 'icon'
toolTip = 'Tool Tip'
toolAccelerator = None
+ palette_enabled = False
+ palette_data = None
def __init__(self, gameInstance):
self.game = gameInstance
@@ -53,16 +59,15 @@ class Tool(object):
toggle = self.game.world.run_physics
self.game.world.run_physics = not toggle
elif event.action == 'clear_all':
- if len(self.game.world.world.GetBodyList()) > 1:
- # Get bodies and destroy them too
- for body in self.game.world.world.GetBodyList():
- self.game.world.world.DestroyBody(body)
-
- # Add ground, because we destroyed it before
- self.game.world.add.ground()
- # Also clear the points recorded in pens.
- self.game.full_pos_list = \
- [[] for _ in self.game.full_pos_list]
+ # Get bodies and destroy them too
+ for body in self.game.world.world.GetBodyList():
+ self.game.world.world.DestroyBody(body)
+
+ # Add ground, because we destroyed it before
+ self.game.world.add.ground()
+ # Also clear the points recorded in pens.
+ self.game.full_pos_list = \
+ [[] for _ in self.game.full_pos_list]
elif event.action == 'focus_in':
self.game.in_focus = True
elif event.action == 'focus_out':
@@ -132,6 +137,36 @@ class CircleTool(Tool):
toolTip = _('Circle')
toolAccelerator = _('<ctrl>c')
+ palette_enabled = True
+ palette_mode = PALETTE_MODE_ICONS
+ palette_settings = [
+ {
+ "name": "density",
+ "icon_count": 3,
+ "icons": ["feather", "wood", "anvil"],
+ "icon_values": [0.5, 1.0, 10.0],
+ "active": "wood"
+ },
+ {
+ "name": "restitution",
+ "icon_count": 3,
+ "icons": ["basketball", "tennis-ball", "bowling-ball"],
+ "icon_values": [1, 0.16, 0.01],
+ "active": "tennis-ball"
+ },
+ {
+ "name": "friction",
+ "icon_count": 3,
+ "icons": ["ice-skate", "grass", "sandpaper"],
+ "icon_values": [0.5, 1, 2],
+ "active": "grass"
+ }]
+ palette_data = {
+ "density": 1.0,
+ "restitution": 0.16,
+ "friction": 1
+ }
+
def __init__(self, gameInstance):
Tool.__init__(self, gameInstance)
self.pt1 = None
@@ -144,9 +179,11 @@ class CircleTool(Tool):
self.pt1 = tuple_to_int(event.pos)
elif event.type == MOUSEBUTTONUP:
if event.button == 1:
- self.game.world.add.ball(self.pt1, self.radius,
- dynamic=True, density=1.0,
- restitution=0.16, friction=0.5)
+ self.game.world.add.ball(
+ self.pt1, self.radius,
+ dynamic=True, density=self.palette_data['density'],
+ restitution=self.palette_data['restitution'],
+ friction=self.palette_data['friction'])
self.pt1 = None
def draw(self):
@@ -173,6 +210,36 @@ class BoxTool(Tool):
toolTip = _('Box')
toolAccelerator = _('<ctrl>b')
+ palette_enabled = True
+ palette_mode = PALETTE_MODE_ICONS
+ palette_settings = [
+ {
+ "name": "density",
+ "icon_count": 3,
+ "icons": ["feather", "wood", "anvil"],
+ "icon_values": [0.5, 1.0, 10.0],
+ "active": "wood"
+ },
+ {
+ "name": "restitution",
+ "icon_count": 3,
+ "icons": ["basketball", "tennis-ball", "bowling-ball"],
+ "icon_values": [1, 0.16, 0.01],
+ "active": "tennis-ball"
+ },
+ {
+ "name": "friction",
+ "icon_count": 3,
+ "icons": ["ice-skate", "grass", "sandpaper"],
+ "icon_values": [0.5, 1, 2],
+ "active": "grass"
+ }]
+ palette_data = {
+ "density": 1.0,
+ "restitution": 0.16,
+ "friction": 1
+ }
+
def __init__(self, gameInstance):
Tool.__init__(self, gameInstance)
self.pt1 = None
@@ -192,13 +259,14 @@ class BoxTool(Tool):
self.rect = pygame.Rect(self.pt1,
(-self.width, -self.height))
self.rect.normalize()
- self.game.world.add.rect(self.rect.center,
- max(self.rect.width, 10) / 2,
- max(self.rect.height, 10) / 2,
- dynamic=True,
- density=1.0,
- restitution=0.16,
- friction=0.5)
+ self.game.world.add.rect(
+ self.rect.center,
+ max(self.rect.width, 10) / 2,
+ max(self.rect.height, 10) / 2,
+ dynamic=True,
+ density=self.palette_data['density'],
+ restitution=self.palette_data['restitution'],
+ friction=self.palette_data['friction'])
self.pt1 = None
def draw(self):
@@ -226,6 +294,36 @@ class TriangleTool(Tool):
toolTip = _('Triangle')
toolAccelerator = _('<ctrl>t')
+ palette_enabled = True
+ palette_mode = PALETTE_MODE_ICONS
+ palette_settings = [
+ {
+ "name": "density",
+ "icon_count": 3,
+ "icons": ["feather", "wood", "anvil"],
+ "icon_values": [0.5, 1.0, 10.0],
+ "active": "wood"
+ },
+ {
+ "name": "restitution",
+ "icon_count": 3,
+ "icons": ["basketball", "tennis-ball", "bowling-ball"],
+ "icon_values": [1, 0.16, 0.01],
+ "active": "tennis-ball"
+ },
+ {
+ "name": "friction",
+ "icon_count": 3,
+ "icons": ["ice-skate", "grass", "sandpaper"],
+ "icon_values": [0.5, 1, 2],
+ "active": "grass"
+ }]
+ palette_data = {
+ "density": 1.0,
+ "restitution": 0.16,
+ "friction": 1
+ }
+
def __init__(self, gameInstance):
Tool.__init__(self, gameInstance)
self.pt1 = None
@@ -262,11 +360,12 @@ class TriangleTool(Tool):
self.vertices = constructTriangleFromLine(self.pt1,
mouse_x_y)
- self.game.world.add.convexPoly(self.vertices,
- dynamic=True,
- density=1.0,
- restitution=0.16,
- friction=0.5)
+ self.game.world.add.convexPoly(
+ self.vertices,
+ dynamic=True,
+ density=self.palette_data['density'],
+ restitution=self.palette_data['restitution'],
+ friction=self.palette_data['friction'])
self.pt1 = None
self.vertices = None
@@ -296,6 +395,36 @@ class PolygonTool(Tool):
toolTip = _('Polygon')
toolAccelerator = _('<ctrl>p')
+ palette_enabled = True
+ palette_mode = PALETTE_MODE_ICONS
+ palette_settings = [
+ {
+ "name": "density",
+ "icon_count": 3,
+ "icons": ["feather", "wood", "anvil"],
+ "icon_values": [0.5, 1.0, 10.0],
+ "active": "wood"
+ },
+ {
+ "name": "restitution",
+ "icon_count": 3,
+ "icons": ["basketball", "tennis-ball", "bowling-ball"],
+ "icon_values": [1, 0.16, 0.01],
+ "active": "tennis-ball"
+ },
+ {
+ "name": "friction",
+ "icon_count": 3,
+ "icons": ["ice-skate", "grass", "sandpaper"],
+ "icon_values": [0.5, 1, 2],
+ "active": "grass"
+ }]
+ palette_data = {
+ "density": 1.0,
+ "restitution": 0.16,
+ "friction": 1
+ }
+
def __init__(self, gameInstance):
Tool.__init__(self, gameInstance)
self.vertices = None
@@ -319,11 +448,12 @@ class PolygonTool(Tool):
self.vertices = [[i[0] - delta_x, i[1] - delta_y]
for i in self.previous_vertices]
self.safe = True
- self.game.world.add.complexPoly(self.vertices,
- dynamic=True,
- density=1.0,
- restitution=0.16,
- friction=0.5)
+ self.game.world.add.complexPoly(
+ self.vertices,
+ dynamic=True,
+ density=self.palette_data['density'],
+ restitution=self.palette_data['restitution'],
+ friction=self.palette_data['friction'])
self.vertices = None
elif (event.type == MOUSEBUTTONUP or
event.type == MOUSEBUTTONDOWN):
@@ -336,11 +466,12 @@ class PolygonTool(Tool):
if distance(tuple_to_int(event.pos), self.vertices[0]) < 15 \
and self.safe:
self.vertices.append(self.vertices[0]) # Connect polygon
- self.game.world.add.complexPoly(self.vertices,
- dynamic=True,
- density=1.0,
- restitution=0.16,
- friction=0.5)
+ self.game.world.add.complexPoly(
+ self.vertices,
+ dynamic=True,
+ density=self.palette_data['density'],
+ restitution=self.palette_data['restitution'],
+ friction=self.palette_data['friction'])
self.previous_vertices = self.vertices[:]
self.vertices = None
elif distance(tuple_to_int(event.pos), self.vertices[0]) < 15:
@@ -375,6 +506,36 @@ class MagicPenTool(Tool):
toolTip = _('Draw')
toolAccelerator = _('<ctrl>d')
+ palette_enabled = True
+ palette_mode = PALETTE_MODE_ICONS
+ palette_settings = [
+ {
+ "name": "density",
+ "icon_count": 3,
+ "icons": ["feather", "wood", "anvil"],
+ "icon_values": [0.5, 1.0, 10.0],
+ "active": "wood"
+ },
+ {
+ "name": "restitution",
+ "icon_count": 3,
+ "icons": ["basketball", "tennis-ball", "bowling-ball"],
+ "icon_values": [1, 0.16, 0.01],
+ "active": "tennis-ball"
+ },
+ {
+ "name": "friction",
+ "icon_count": 3,
+ "icons": ["ice-skate", "grass", "sandpaper"],
+ "icon_values": [0.5, 1, 2],
+ "active": "grass"
+ }]
+ palette_data = {
+ "density": 1.0,
+ "restitution": 0.16,
+ "friction": 1
+ }
+
def __init__(self, gameInstance):
Tool.__init__(self, gameInstance)
self.vertices = None
@@ -395,10 +556,11 @@ class MagicPenTool(Tool):
for i in self.previous_vertices]
self.safe = True
if self.vertices and self.safe:
- self.game.world.add.complexPoly(self.vertices, dynamic=True,
- density=1.0,
- restitution=0.16,
- friction=0.5)
+ self.game.world.add.complexPoly(
+ self.vertices, dynamic=True,
+ density=self.palette_data['density'],
+ restitution=self.palette_data['restitution'],
+ friction=self.palette_data['friction'])
self.previous_vertices = self.vertices[:]
self.vertices = None
elif event.type == MOUSEMOTION and self.vertices:
@@ -554,6 +716,17 @@ class MotorTool(Tool):
icon = 'motor'
toolTip = _('Motor')
toolAccelerator = _('<ctrl>m')
+ # Palette settings
+ palette_enabled = True
+ palette_mode = PALETTE_MODE_SLIDER_ICON
+ palette_settings = {
+ "icon1": "motor-turtle",
+ "icon2": "motor-rabbit",
+ "min": 0,
+ "max": 500
+ }
+ # Default Value
+ palette_data = 10
def __init__(self, gameInstance):
Tool.__init__(self, gameInstance)
@@ -568,7 +741,9 @@ class MotorTool(Tool):
self.jb1 = self.game.world.get_bodies_at_pos(
tuple_to_int(event.pos))
if self.jb1:
- self.game.world.add.motor(self.jb1[0], self.jb1pos)
+ self.game.world.add.motor(
+ self.jb1[0], self.jb1pos,
+ speed=self.palette_data)
self.jb1 = self.jb1pos = None
def cancel(self):
@@ -657,54 +832,6 @@ class DestroyTool(Tool):
self.vertices = None
-class EraseAllTool(Tool):
- name = 'Erase All'
- icon = 'destroy-all'
- toolTip = _('Erase all')
-
- def __init__(self, gameInstance, activity=None):
- super(EraseAllTool, self).__init__(gameInstance)
- self.game = gameInstance
- self.response_alert = None
- self.activity = activity
-
- def handleToolEvent(self, event, action=False):
- if event.type == MOUSEBUTTONDOWN:
- if not action:
- # Add alert for confirm the delete all action.
- alert = ConfirmationAlert()
- alert.props.title = _('Delete all shapes?')
- alert.props.msg = _('This cannot be undone!')
- alert.connect('response', self.alert_info, event)
- self.activity.add_alert(alert)
- return
- else:
- if self.response_alert:
- self.response_alert = False
- # Obtain all figures
- bodys = []
- for body in self.game.world.world.GetBodyList():
- bodys.append(body)
-
- # Erase all ;)
- for body in bodys:
- self.game.world.world.DestroyBody(body)
-
- # The ground has deleted, restore..
- self.game.world.add.ground()
- else:
- pass
-
- def alert_info(self, alert, response_id, event):
- self.activity.remove_alert(alert)
- if response_id is gtk.RESPONSE_OK:
- self.response_alert = True
- elif response_id is gtk.RESPONSE_CANCEL:
- self.response_alert = False
-
- self.handleToolEvent(event, True)
-
-
# Track tool
class TrackTool(Tool):
name = 'Track'
@@ -762,11 +889,30 @@ class ChainTool(Tool):
toolTip = _("Chain")
toolAccelerator = "<ctrl>i"
+ # Palette settings
+ palette_enabled = True
+ palette_mode = PALETTE_MODE_SLIDER_LABEL
+ palette_settings = [
+ {
+ "label": "Chain Distance",
+ "min": 25,
+ "max": 50,
+ "data": "distance"
+ },
+ {
+ "label": "Chain Circle Size",
+ "min": 1,
+ "max": 10,
+ "data": "radius"
+ }]
+ palette_data = {
+ "distance": 25,
+ "radius": 1
+ }
+
def __init__(self, gameInstance):
Tool.__init__(self, gameInstance)
self.jb1 = self.jb2 = self.jb1pos = self.jb2pos = None
- self.circle_distance = 25
- self.circle_radius = 5
def handleToolEvent(self, event):
Tool.handleToolEvent(self, event)
@@ -806,14 +952,12 @@ class ChainTool(Tool):
first_iter = True
prevcircle = None
prevpos = None
- for p in range(5, d, self.circle_distance):
+ for p in range(5, d, int(self.palette_data['distance'])):
x = x1 + p * math.cos(bearing)
y = y1 + p * math.sin(bearing)
circle = self.game.world.add.ball(
- (x, y), self.circle_radius, dynamic=True)
+ (x, y), self.palette_data['radius'], dynamic=True)
circle.userData['color'] = (0, 0, 0)
- circle = self.game.world.get_bodies_at_pos(
- tuple_to_int((x, y)))[0]
if first_iter:
self.game.world.add.joint(circle, bodyA, (x, y), pos1, False)
first_iter = False