diff --git a/matrx/api/api.py b/matrx/api/api.py index 91a2600..6b2d395 100644 --- a/matrx/api/api.py +++ b/matrx/api/api.py @@ -793,7 +793,7 @@ def __check_states_API_request(tick=None, ids=None, ids_required=False): # e.g. "god"), or a list of ' f'IDs(string) for requesting states of multiple agents'} # check if the api was reset during this time - if len(__states) is 0: + if len(__states) == 0: return False, {'error_code': 400, 'error_message': f'api is reconnecting to a new world'} @@ -974,7 +974,7 @@ def __reorder_state(state): # loop through all objects in the state for objID, obj in state.items(): - if objID is not "World": + if objID != "World": # make the sense capability JSON serializable if "sense_capability" in obj: new_state[objID]["sense_capability"] = str(obj["sense_capability"]) diff --git a/matrx/defaults.py b/matrx/defaults.py index 198c5c0..69bbe4b 100644 --- a/matrx/defaults.py +++ b/matrx/defaults.py @@ -24,6 +24,7 @@ ENVOBJECT_VIS_COLOUR = "#4286f4" ENVOBJECT_VIS_OPACITY = 1.0 ENVOBJECT_VIS_DEPTH = 80 +ENVOBJECT_VIS_FROM_CENTER = True ###################### # GridWorld defaults # diff --git a/matrx/objects/env_object.py b/matrx/objects/env_object.py index 24c011c..6ee6786 100644 --- a/matrx/objects/env_object.py +++ b/matrx/objects/env_object.py @@ -74,6 +74,8 @@ class EnvObject: is used by the Visualizer to draw objects in layers. visualize_opacity : Integer. Optional, default obtained from defaults.py Opacity of the object. From 0.0 to 1.0. + visualize_from_center: Boolean. Optional, by default True. + Whether an object should be visualized and scaled from its center point, or top left point. **custom_properties : Dict. Optional Any other keyword arguments. All these are treated as custom attributes. For example the property 'heat'=2.4 of an EnvObject representing a fire. @@ -82,7 +84,7 @@ class EnvObject: def __init__(self, location, name, class_callable, customizable_properties=None, is_traversable=None, is_movable=None, visualize_size=None, visualize_shape=None, visualize_colour=None, visualize_depth=None, - visualize_opacity=None, **custom_properties): + visualize_opacity=None, visualize_from_center=None, **custom_properties): # Set the object's name. self.obj_name = name @@ -136,6 +138,8 @@ def __init__(self, location, name, class_callable, customizable_properties=None, visualize_depth = defaults.ENVOBJECT_VIS_DEPTH if is_movable is None: is_movable = defaults.ENVOBJECT_IS_MOVABLE + if visualize_from_center is None: + visualize_from_center = defaults.ENVOBJECT_VIS_FROM_CENTER # Set the mandatory properties @@ -146,6 +150,7 @@ def __init__(self, location, name, class_callable, customizable_properties=None, self.visualize_size = visualize_size self.is_traversable = is_traversable self.is_movable = is_movable + self.visualize_from_center = visualize_from_center # Since carried_by cannot be defined beforehand (it contains the unique id's of objects that carry this object) # we set it to an empty list by default. @@ -228,6 +233,8 @@ def change_property(self, property_name, property_value): elif property_name == "is_movable": assert isinstance(property_value, bool) self.is_movable = property_value + elif property_name == visualize_from_center: + self.visualize_from_center = property_value return self.properties @@ -306,7 +313,8 @@ def properties(self): "shape": self.visualize_shape, "colour": self.visualize_colour, "depth": self.visualize_depth, - "opacity": self.visualize_opacity + "opacity": self.visualize_opacity, + "visualize_from_center": self.visualize_from_center } return properties diff --git a/matrx_visualizer/static/js/gen_grid.js b/matrx_visualizer/static/js/gen_grid.js index e8c1797..19899b5 100644 --- a/matrx_visualizer/static/js/gen_grid.js +++ b/matrx_visualizer/static/js/gen_grid.js @@ -119,7 +119,8 @@ function draw(state, world_settings, new_messages, accessible_chatrooms, new_tic "opacity": obj['visualization']['opacity'], "dimension": tile_size, // width / height of the tile "busy": (show_busy_condition ? obj['is_blocked_by_action'] : false), // show busy if available and requested - "selected": (object_selected == objID ? true : false) + "selected": (object_selected == objID ? true : false), + "visualize_from_center": obj['visualization']['visualize_from_center'] }; // Check if any subtiles have been defined and include them in the ob_vis_settings if so @@ -457,9 +458,12 @@ function gen_rectangle(obj_vis_settings, obj_element, element_type = "div") { // no subtiles defined, place the rectangle in the usual manner } else { - // coords of top left corner, such that it is centered in our tile - shape.style.left = ((1 - size) * 0.5 * tile_size) + "px"; - shape.style.top = ((1 - size) * 0.5 * tile_size) + "px"; + // there is a specific setting for rendering objects from the top left instead of center + if (!("visualize_from_center" in obj_vis_settings && obj_vis_settings['visualize_from_center'] == false)) { + // coords of top left corner, such that it is centered in our tile + shape.style.left = ((1 - size) * 0.5 * tile_size) + "px"; + shape.style.top = ((1 - size) * 0.5 * tile_size) + "px"; + } // width and height of rectangle shape.style.width = size * tile_size + "px";