Skip to content

Commit

Permalink
Fixing bugs in tile dragging.
Browse files Browse the repository at this point in the history
  • Loading branch information
b-guild committed Dec 26, 2024
1 parent dd50cde commit 7ea4519
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
11 changes: 8 additions & 3 deletions editor/src/plugins/tilemap/interaction_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,15 @@ impl InteractionMode for TileMapInteractionMode {
match self.mouse_mode {
MouseMode::None => (),
MouseMode::Dragging => {
editor_data.overlay.clear();
editor_data.erased_area.clear();
let tiles = editor_data.selected.iter().copied().collect::<Vec<_>>();
if let (Some(start), Some(end)) = (start, end) {
let offset = end - start;
if offset != Vector2::new(0, 0) {
let tiles = editor_data
.overlay
.keys()
.copied()
.map(|p| p + start)
.collect::<Vec<_>>();
editor_data.selected.clear();
editor_data
.selected
Expand All @@ -362,6 +365,8 @@ impl InteractionMode for TileMapInteractionMode {
));
}
}
editor_data.overlay.clear();
editor_data.erased_area.clear();
}
MouseMode::Drawing => {
let state = self.state.lock();
Expand Down
12 changes: 1 addition & 11 deletions editor/src/plugins/tilemap/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,22 +705,12 @@ impl PaletteWidget {
if self.kind == TilePaletteStage::Tiles && self.content.is_material_page(page) {
return;
}
let Some(tile_set) = state.tile_set.as_ref() else {
return;
};
let mut tile_set = tile_set.state();
let Some(tile_set) = tile_set.data() else {
return;
};
let tiles = state.selection_positions();
self.overlay.movable_position = Vector2::default();
self.overlay.erased_tiles.clear();
self.overlay.movable_tiles.clear();
for pos in tiles.iter() {
let Some(handle) = TileDefinitionHandle::try_new(page, *pos) else {
continue;
};
let Some(data) = tile_set.get_tile_render_data(self.kind, handle) else {
let Some(data) = self.content.get_tile_render_data(self.kind, page, *pos) else {
continue;
};
let _ = self.overlay.erased_tiles.insert(*pos);
Expand Down
28 changes: 28 additions & 0 deletions fyrox-impl/src/scene/tilemap/brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,34 @@ impl TileMapBrush {
}
}

/// Return the `TileRenderData` needed to render the tile at the given position on the givne page.
/// If there is no tile at that position or the tile set is missing or not loaded, then None is returned.
/// If there is a tile and a tile set, but the handle of the tile does not exist in the tile set,
/// then the rendering data for an error tile is returned using `TileRenderData::missing_tile()`.
pub fn get_tile_render_data(
&self,
stage: TilePaletteStage,
page: Vector2<i32>,
tile: Vector2<i32>,
) -> Option<TileRenderData> {
let handle = match stage {
TilePaletteStage::Pages => {
let page = self.pages.get(&tile)?;
page.icon
}
TilePaletteStage::Tiles => {
let page = self.pages.get(&page)?;
*page.tiles.get(&tile)?
}
};
let mut tile_set = self.tile_set.as_ref()?.state();
let data = tile_set
.data()?
.get_tile_render_data(TilePaletteStage::Tiles, handle)
.unwrap_or_else(TileRenderData::missing_data);
Some(data)
}

/// The tiles of a brush are references to tiles in the tile set.
/// This method converts handles within the brush into the handle that points to the corresponding
/// tile definition within the tile set.
Expand Down
23 changes: 23 additions & 0 deletions fyrox-impl/src/scene/tilemap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,29 @@ impl TileResource {
}
}

/// Return the `TileRenderData` needed to render the tile at the given position on the givne page.
/// If there is no tile at that position or the tile set is missing or not loaded, then None is returned.
/// If there is a tile and a tile set, but the handle of the tile does not exist in the tile set,
/// then the rendering data for an error tile is returned using `TileRenderData::missing_tile()`.
pub fn get_tile_render_data(
&self,
stage: TilePaletteStage,
page: Vector2<i32>,
tile: Vector2<i32>,
) -> Option<TileRenderData> {
match self {
TileResource::Empty => None,
TileResource::TileSet(resource) => resource
.state()
.data()?
.get_tile_render_data(stage, TileDefinitionHandle::try_new(page, tile)?),
TileResource::Brush(resource) => resource
.state()
.data()?
.get_tile_render_data(stage, page, tile),
}
}

/// Repeatedly call the given function with each tile for the given stage and page.
/// The function is given the position of the tile within the palette and the
/// data for rendering the tile.
Expand Down

0 comments on commit 7ea4519

Please sign in to comment.