Design Pattern Examples
Overview of object-oriented design patterns
flyweight_classes.py
Go to the documentation of this file.
9
10
18
19
36 def __init__(self, offset_x = 0, image_width = 0, image_height = 0, position_x = 0.0, position_y = 0.0, velocity_x = 0.0, velocity_y = 0.0) -> None:
37 self.OffsetXToImage = offset_x
38 self.ImageWidth = image_width
39 self.ImageHeight = image_height
40 self.Position_X = position_x
41 self.Position_Y = position_y
42 self.Velocity_X = velocity_x
43 self.Velocity_Y = velocity_y
44
45
60
61
62#========================================================================
63#========================================================================
64#========================================================================
65
66
68
69
71
72
73 @property
74 def Context(self) -> Flyweight_Context:
75 return self._context
76
77
78 @Context.setter
79 def Context(self, context : Flyweight_Context) -> None:
80 self._context = context
81
82
83
84 @property
85 def ImageWidth(self) -> int:
86 if self._resource:
87 return self._context.ImageWidth
88 return 0
89
90
91
92 @property
93 def ImageHeight(self) -> int:
94 if self._resource:
95 return self._context.ImageHeight
96 return 0
97
98
99
100
109 def __init__(self, resource = None, context = Flyweight_Context()) -> None:
110 self._resource = resource
111 self._context = context
112
113
122
123
124
148 def Render(self, display : list[bytearray], offset_x : int, image_width : int, image_height : int, position_x : int, position_y : int) -> None:
149 if self._resource:
150 self._resource.Render(display, offset_x, image_width, image_height, position_x, position_y)
151
152
153#========================================================================
154#========================================================================
155#========================================================================
156
157
158
171
172
174
175
176 @property
177 def ResourceId(self) -> int:
178 return self._resourceId
179
180
181
182 @property
183 def ImageWidth(self) -> int:
184 if self._resource:
185 return len(self._resource[0])
186 return 0
187
188
189
190 @property
191 def ImageHeight(self) -> int:
192 if self._resource:
193 return len(self._resource)
194 return 0
195
196
197
198
204 def __init__(self, resource : list[bytearray], resourceId : int):
205 self._resource = resource
206 self._resourceId = resourceId
207
208
212
213
214
229 def Render(self, display : list[bytearray], offset_x : int, image_width : int, image_height : int, position_x : int, position_y : int) -> None:
230 display_width = len(display[0])
231 display_height = len(display)
232 starting_position_x = position_x
233 starting_position_y = position_y
234
235 # Size of image to render (can be smaller than actual image if image
236 # lies partially of right or bottom of display).
237 image_render_width = image_width
238 image_render_height = image_height
239
240 # Position into image to start rendering from (non-zero if
241 # image is off the left or top edge of display).
242 starting_row_in_image = 0
243 starting_col_in_image = offset_x
244
245 # Clip the image to the display.
246 if starting_position_x < 0:
247 starting_col_in_image = -starting_position_x
248 image_render_width += starting_position_x
249 starting_position_x = 0
250 elif (starting_position_x + image_render_width) > display_width:
251 image_render_width = display_width - starting_position_x
252
253 if starting_position_y < 0:
254 starting_row_in_image = -starting_position_y
255 image_render_height += starting_position_y
256 starting_position_y = 0
257 elif (starting_position_y + image_render_height) > display_height:
258 image_render_height = display_height - starting_position_y
259
260 # If the image is even partially visible, render it
261 if image_render_width > 0 and image_render_height > 0:
262 current_display_row = starting_position_y
263 current_image_row = starting_row_in_image
264 for row in range(0, image_render_height):
265 for col in range(0, image_render_width):
266 display[current_display_row][starting_position_x + col] = \
267 self._resource[current_image_row][starting_col_in_image + col]
268 current_display_row += 1
269 current_image_row += 1
270
271
272
282 def CreateFlyweight(self, context : Flyweight_Context) -> Flyweight_Class:
283 return Flyweight_Class(self, context)
284
285
286#========================================================================
287#========================================================================
288#========================================================================
289
290
291
292
308
309 _resources = [] # type list[BigResource]
310
311
312 _nextResourceId = 0
313
314
318 def GetNextResourceId() -> int:
319 nextId = BigResourceManager._nextResourceId
320 ++BigResourceManager._nextResourceId
321 return nextId
322
323
324
331 def FindResource(resourceId : int) -> BigResource:
332 foundResource = None # type: BigResource
333
334 for resource in BigResourceManager._resources:
335 if resource.ResourceId == resourceId:
336 foundResource = resource
337 break
338
339 return foundResource
340
341
342
348 def AddResource(rawResource : list[bytearray]) -> int:
349 newResourceId = BigResourceManager.GetNextResourceId()
350 BigResourceManager._resources.append(BigResource(rawResource, newResourceId))
351 return newResourceId
352
353
354
368 def CreateFlyweight(bigResourceId : int, context : Flyweight_Context) -> Flyweight_Class:
369 flyweightClass = None
370 bigResource = BigResourceManager.FindResource(bigResourceId)
371 if bigResource:
372 flyweightClass = bigResource.CreateFlyweight(context)
373 return flyweightClass
374
int ImageWidth(self)
Property getter for the "image" width of the resource: value = o.ImageWidth
int ImageHeight(self)
Property getter for the "image" height of the resource: value = o.ImageHeight
None Render(self, list[bytearray] display, int offset_x, int image_width, int image_height, int position_x, int position_y)
Render the big resource into the given display at the given position.
_resource
a list of bytearrays representing the image (big resource)
Flyweight_Class CreateFlyweight(self, Flyweight_Context context)
Generate a Flyweight instance that will represent this big resource in some context-dependent way.
int ResourceId(self)
Property getter for the resource ID for this resource: value = o.ResourceId
def __init__(self, list[bytearray] resource, int resourceId)
Constructor (accessibly only to the class factory).
Flyweight_Class CreateFlyweight(int bigResourceId, Flyweight_Context context)
Create a new instance of the Flyweight_Class associated with the given big resource and a context,...
int AddResource(list[bytearray] rawResource)
Add a new big resource and return the ID of the resource.
int GetNextResourceId()
Retrieve the next resource ID that can be used.
BigResource FindResource(int resourceId)
Retrieve the BigResource corresponding to the specified ID.
int ImageWidth(self)
Property getter for the "image" width from underlying big resource: value = o.ImageWidth
int ImageHeight(self)
Property getter for the "image" height from underlying big resource: value = o.ImageHeight
None Render(self, list[bytearray] display, int offset_x, int image_width, int image_height, int position_x, int position_y)
Render the image associated with this flyweight instance into the given display at the given position...
_resource
The big resource being referenced by this flyweight class.
None __init__(self, resource=None, context=Flyweight_Context())
Constructor.
Flyweight_Context Context(self)
Property getter for the context for this class instance: value = o.Context
Represents the context for an instance of the Flyweight_Class.
None __init__(self, offset_x=0, image_width=0, image_height=0, position_x=0.0, position_y=0.0, velocity_x=0.0, velocity_y=0.0)
Constructor.
Position_X
X position of the top left corner of the big resource.
OffsetXToImage
Offset from left edge of big resource image to the left edge of the image for this context.
Position_Y
Y position of the top left corner of the big resource.