annotate bGrease/component/general.py @ 42:66a06de1bddc

Renamed dialog to dialogue
author KarstenBock@gmx.net
date Tue, 06 Sep 2011 14:37:54 +0200
parents ff3e395abf91
children e856b604b650
rev   line source
5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
1 #############################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
2 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
3 # Copyright (c) 2010 by Casey Duncan and contributors
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
4 # All Rights Reserved.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
5 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
6 # This software is subject to the provisions of the MIT License
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
7 # A copy of the license should accompany this distribution.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
11 #
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
12 #############################################################################
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
13
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
14 __version__ = '$Id$'
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
15
41
ff3e395abf91 Renamed grease to bGrease (Basic Grease) to get rid of conflicts with an already installed grease.
KarstenBock@gmx.net
parents: 5
diff changeset
16 from bGrease.component import base
ff3e395abf91 Renamed grease to bGrease (Basic Grease) to get rid of conflicts with an already installed grease.
KarstenBock@gmx.net
parents: 5
diff changeset
17 from bGrease.component import field
ff3e395abf91 Renamed grease to bGrease (Basic Grease) to get rid of conflicts with an already installed grease.
KarstenBock@gmx.net
parents: 5
diff changeset
18 from bGrease.entity import ComponentEntitySet
5
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
19
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
20
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
21 class Component(dict):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
22 """General component with a configurable schema
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
23
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
24 The field schema is defined via keyword args where the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
25 arg name is the field name and the value is the type object.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
26
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
27 The following types are supported for fields:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
28
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
29 - :class:`int`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
30 - :class:`float`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
31 - :class:`bool`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
32 - :class:`str`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
33 - :class:`object`
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
34 - |Vec2d|
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
35 - |Vec2dArray|
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
36 - |RGBA|
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
37 - |Rect|
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
38 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
39
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
40 deleted_entities = ()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
41 """List of entities deleted from the component since the last time step"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
42
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
43 new_entities = ()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
44 """List of entities added to the component since the last time step"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
45
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
46 def __init__(self, **fields):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
47 self.fields = {}
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
48 for fname, ftype in fields.items():
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
49 assert ftype in field.types, fname + " has an illegal field type"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
50 self.fields[fname] = field.Field(self, fname, ftype)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
51 self.entities = ComponentEntitySet(self)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
52 self._added = []
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
53 self._deleted = []
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
54
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
55 def set_world(self, world):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
56 self.world = world
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
57
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
58 def step(self, dt):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
59 """Update the component for the next timestep"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
60 delitem = super(Component, self).__delitem__
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
61 for entity in self._deleted:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
62 delitem(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
63 self.new_entities = self._added
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
64 self.deleted_entities = self._deleted
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
65 self._added = []
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
66 self._deleted = []
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
67
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
68 def set(self, entity, data=None, **data_kw):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
69 """Set the component data for an entity, adding it to the
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
70 component if it is not already a member.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
71
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
72 If data is specified, its data for the new entity's fields are
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
73 copied from its attributes, making it easy to copy another
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
74 entity's data. Keyword arguments are also matched to fields.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
75 If both a data attribute and keyword argument are supplied for
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
76 a single field, the keyword arg is used.
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
77 """
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
78 if data is not None:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
79 for fname, field in self.fields.items():
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
80 if fname not in data_kw and hasattr(data, fname):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
81 data_kw[fname] = getattr(data, fname)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
82 data = self[entity] = Data(self.fields, entity, **data_kw)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
83 return data
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
84
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
85 def __setitem__(self, entity, data):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
86 assert entity.world is self.world, "Entity not in component's world"
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
87 if entity not in self.entities:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
88 self._added.append(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
89 self.entities.add(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
90 super(Component, self).__setitem__(entity, data)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
91
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
92 def remove(self, entity):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
93 if entity in self.entities:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
94 self._deleted.append(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
95 self.entities.remove(entity)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
96 return True
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
97 return False
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
98
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
99 __delitem__ = remove
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
100
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
101 def __repr__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
102 return '<%s %x of %r>' % (
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
103 self.__class__.__name__, id(self), getattr(self, 'world', None))
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
104
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
105
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
106 class Singleton(Component):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
107 """Component that may contain only a single entity"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
108
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
109 def add(self, entity_id, data=None, **data_kw):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
110 if entity_id not in self._data:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
111 self.entity_id_set.clear()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
112 self._data.clear()
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
113 Component.add(self, entity_id, data, **data_kw)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
114
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
115 @property
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
116 def entity(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
117 """Return the entity in the component, or None if empty"""
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
118 if self._data:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
119 return self.manager[self._data.keys()[0]]
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
120
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
121
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
122 class Data(object):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
123
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
124 def __init__(self, fields, entity, **data):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
125 self.__dict__['_Data__fields'] = fields
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
126 self.__dict__['entity'] = entity
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
127 for field in fields.values():
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
128 if field.name in data:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
129 setattr(self, field.name, data[field.name])
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
130 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
131 setattr(self, field.name, field.default())
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
132
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
133 def __setattr__(self, name, value):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
134 if name in self.__fields:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
135 self.__dict__[name] = self.__fields[name].cast(value)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
136 else:
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
137 raise AttributeError("Invalid data field: " + name)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
138
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
139 def __repr__(self):
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
140 return '<%s(%r)>' % (self.__class__.__name__, self.__dict__)
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
141
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
142
bc88f7d5ca8b Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
143