9using System.Collections.Generic;
42 public Flyweight_Context(
int offset_x,
int image_width,
int image_height,
double position_x,
double position_y,
double velocity_x,
double velocity_y)
165 public void Render(List<
char[]> display,
int offset_x,
int image_width,
int image_height,
int position_x,
int position_y)
168 _resource.
Render(display, offset_x, image_width, image_height, position_x, position_y);
218 internal void Render(List<
char[]> display,
int offset_x,
int image_width,
int image_height,
int position_x,
int position_y)
220 int display_width = display[0].Length;
221 int display_height = display.Count;
222 int starting_position_x = position_x;
223 int starting_position_y = position_y;
227 int image_render_width = image_width;
228 int image_render_height = image_height;
232 int starting_row_in_image = 0;
233 int starting_col_in_image = offset_x;
236 if (starting_position_x < 0)
238 starting_col_in_image = -starting_position_x;
239 image_render_width += starting_position_x;
240 starting_position_x = 0;
242 else if (starting_position_x + image_render_width > display_width)
244 image_render_width = display_width - starting_position_x;
246 if (starting_position_y < 0)
248 starting_row_in_image = - starting_position_y;
249 image_render_height += starting_position_y;
250 starting_position_y = 0;
252 else if (starting_position_y + image_render_height > display_height)
254 image_render_height = display_height - starting_position_y;
258 if (image_render_width > 0 && image_render_height > 0)
260 int current_display_row = starting_position_y;
261 int current_image_row = starting_row_in_image;
262 for (
int row = 0; row < image_render_height; ++row)
264 for (
int col = 0; col < image_render_width; ++col)
266 display[current_display_row][starting_position_x + col] =
_resource[current_image_row][starting_col_in_image + col];
268 current_display_row += 1;
269 current_image_row += 1;
355 static List<BigResource>
_resources =
new List<BigResource>();
384 return newResourceId;
400 foundResource = resource;
405 return foundResource;
421 if (bigResource !=
null)
425 return flyweightClass;
Represents some big resource. In this case, a text "image" rendered as a list of strings....
int ImageHeight
Retrieve the "image" height of the resource.
BigResource(List< string > resource, int resourceId)
Constructor (accessibly only to the class factory).
Flyweight_Class CreateFlyweight(Flyweight_Context context)
Generate a Flyweight class that will represent this big resource in some context-dependent way.
int ImageWidth
Retrieve the "image" width of the resource.
void Render(List< char[]> 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.
int ResourceId
Retrieve the resource ID for this resource.
Represents a manager for big resources. Also provides the class factory for the Flyweight_Class insta...
static int _nextResourceId
The next ID to assign a raw resource for management.
static List< BigResource > _resources
A list of all big resources managed by this class.
static int GetNextResourceId()
Retrieve the next resource ID that can be used.
static int AddResource(List< string > rawResource)
Add a new big resource and return the ID of the resource.
static ? 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,...
static ? BigResource FindResource(int resourceId)
Retrieve the BigResource corresponding to the specified ID.
Associates a context with a big resource.
BigResource _resource
The big resource being referenced by this flyweight class. In C#, a class is always a reference to an...
int ImageHeight
Retrieve the "image" height from underlying big resource.
Flyweight_Context _context
The context associated with this class. The calling entity uses this context to manipulate the flywei...
int ImageWidth
Retrieve the "image" width from underlying big resource.
void Render(List< char[]> 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...
Flyweight_Class(BigResource resource, Flyweight_Context context)
Constructor (accessible only to the class factory).
Flyweight_Context Context
Retrieve or set the context for this class instance.
The namespace containing all Design Pattern Examples implemented in C#.
Represents the context for an instance of the Flyweight_Class. In this case, the context includes pos...
Flyweight_Context(int offset_x, int image_width, int image_height, double position_x, double position_y, double velocity_x, double velocity_y)
Constructor.