Skip to content

Commit

Permalink
Merge pull request #297 from matrx-software/continuous_bug_fixing
Browse files Browse the repository at this point in the history
Closes #215 - added option for obj visualization from top left or center
  • Loading branch information
thaije committed May 16, 2021
2 parents 7543af5 + 59f2078 commit 5d7c832
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions matrx/api/api.py
Expand Up @@ -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'}

Expand Down Expand Up @@ -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"])
Expand Down
1 change: 1 addition & 0 deletions matrx/defaults.py
Expand Up @@ -24,6 +24,7 @@
ENVOBJECT_VIS_COLOUR = "#4286f4"
ENVOBJECT_VIS_OPACITY = 1.0
ENVOBJECT_VIS_DEPTH = 80
ENVOBJECT_VIS_FROM_CENTER = True

######################
# GridWorld defaults #
Expand Down
12 changes: 10 additions & 2 deletions matrx/objects/env_object.py
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions matrx_visualizer/static/js/gen_grid.js
Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 5d7c832

Please sign in to comment.