Skip to content

Commit

Permalink
Terrain selection and zoom tools
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Oct 22, 2024
1 parent 4d0c5da commit 02acf08
Show file tree
Hide file tree
Showing 15 changed files with 998 additions and 335 deletions.
2 changes: 1 addition & 1 deletion StarterProject.eldiron

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions creator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub mod prelude {
pub use crate::tools::selection::SelectionTool;
pub use crate::tools::terrain::draw::TerrainDrawTool;
pub use crate::tools::terrain::height::TerrainHeightTool;
pub use crate::tools::terrain::selection::TerrainSelectionTool;
pub use crate::tools::terrain::zoom::TerrainZoomTool;
pub use crate::tools::tiledrawer::TileDrawerTool;
pub use crate::tools::tilemap::TilemapTool;
pub use crate::tools::zoom::ZoomTool;
Expand Down
9 changes: 9 additions & 0 deletions creator/src/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,15 @@ impl Sidebar {
self.stack_layout_id.clone(),
SidebarMode::Model as usize,
));
} else if id.name == "Update Minimap" {
if let Some(region) = project.get_region_mut(&server_ctx.curr_region) {
if let Some(render_view) = ui.get_render_view("MiniMap") {
let dim = *render_view.dim();
let buffer = render_view.render_buffer_mut();
buffer.resize(dim.width, dim.height);
draw_minimap(region, buffer, false);
}
}
} else if id.name == "Update Minimaps" {
if let Some(region) = project.get_region_mut(&server_ctx.curr_region) {
if let Some(render_view) = ui.get_render_view("MiniMap") {
Expand Down
58 changes: 52 additions & 6 deletions creator/src/terraineditor.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use shared::prelude::*;

use crate::editor::TILEDRAWER;
use crate::prelude::*;

pub struct TerrainEditor {
pub buffer: TheRGBABuffer,
pub grid_size: i32,
}

#[allow(clippy::new_without_default)]
impl TerrainEditor {
pub fn new() -> Self {
Self {
buffer: TheRGBABuffer::default(),
grid_size: 0,
}
}

Expand Down Expand Up @@ -81,17 +84,19 @@ impl TerrainEditor {
scroll_to: bool,
) {
if let Some(region) = project.get_region_mut(&server_ctx.curr_region) {
self.grid_size = region.grid_size;
let region_width = region.width * region.grid_size;
let region_height = region.height * region.grid_size;

let mut buffer = TheRGBABuffer::new(TheDim::sized(region_width, region_height));
crate::minimap::draw_minimap(region, &mut buffer, true);
self.draw_selection(ui, ctx, server_ctx, None);

if let Some(editor) = ui.get_rgba_layout("TerrainMap") {
if let Some(rgba_view) = editor.rgba_view_mut().as_rgba_view() {
rgba_view.set_mode(TheRGBAViewMode::TileEditor);
rgba_view.set_grid(Some(1));

let region_width = region.width * region.grid_size;
let region_height = region.height * region.grid_size;

let mut buffer = TheRGBABuffer::new(TheDim::sized(region_width, region_height));
crate::minimap::draw_minimap(region, &mut buffer, true);

self.buffer = buffer.clone();

rgba_view.set_buffer(buffer);
Expand All @@ -104,4 +109,45 @@ impl TerrainEditor {
}
}
}

pub fn draw_selection(
&mut self,
ui: &mut TheUI,
ctx: &mut TheContext,
server_ctx: &ServerContext,
hover_pos: Option<Vec2i>,
) {
let tiledrawer = TILEDRAWER.lock().unwrap();

if let Some(editor) = ui.get_rgba_layout("TerrainMap") {
if let Some(rgba_view) = editor.rgba_view_mut().as_rgba_view() {
let b = rgba_view.buffer_mut();
if self.buffer.len() == b.len() {
b.pixels_mut().copy_from_slice(self.buffer.pixels());
}

// Selection
if let Some(tilearea) = &server_ctx.tile_selection {
tiledrawer.draw_tile_selection(
&tilearea.merged(),
b,
self.grid_size,
WHITE,
ctx,
);
}

// Hover
if let Some(hover_pos) = hover_pos {
let x0 = hover_pos.x * self.grid_size;
let y0 = hover_pos.y * self.grid_size;

b.draw_rect_outline(
&TheDim::new(x0, y0, self.grid_size, self.grid_size),
&[128, 128, 128, 255],
);
}
}
}
}
}
40 changes: 37 additions & 3 deletions creator/src/toollist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl ToolList {
let terrain_tools: Vec<Box<dyn Tool>> = vec![
Box::new(TerrainDrawTool::new()),
Box::new(TerrainHeightTool::new()),
Box::new(TerrainSelectionTool::new()),
Box::new(TerrainZoomTool::new()),
];
let material_tools: Vec<Box<dyn Tool>> = vec![Box::new(MaterialNodeEditTool::new())];
let model_tools: Vec<Box<dyn Tool>> = vec![Box::new(ModelNodeEditTool::new())];
Expand Down Expand Up @@ -239,6 +241,29 @@ impl ToolList {
}
}
TerrainEditor => {
if (*c == '-' || *c == '=' || *c == '+') && (ui.ctrl || ui.logo) {
// Global Zoom In / Zoom Out
if let Some(region) =
project.get_region_mut(&server_ctx.curr_region)
{
if *c == '=' || *c == '+' {
region.zoom += 0.2;
} else {
region.zoom -= 0.2;
}
region.zoom = region.zoom.clamp(1.0, 5.0);
server.set_zoom(region.id, region.zoom);
if let Some(layout) = ui.get_rgba_layout("TerrainMap") {
layout.set_zoom(region.zoom);
layout.relayout(ctx);
}
if let Some(edit) = ui.get_text_line_edit("TerrainMap Zoom") {
edit.set_value(TheValue::Float(region.zoom));
}
return true;
}
}

let mut tool_uuid = None;
for tool in self.terrain_tools.iter() {
if tool.accel() == Some(*c) {
Expand Down Expand Up @@ -298,7 +323,10 @@ impl ToolList {
}
}
TheEvent::TileEditorClicked(id, coord) => {
if id.name == "Region Editor View" || id.name == "Screen Editor View" {
if id.name == "Region Editor View"
|| id.name == "Screen Editor View"
|| id.name == "TerrainMap View"
{
let mut coord_f = Vec2f::from(*coord);
if id.name == "Region Editor View" {
if let Some(editor) = ui.get_rgba_layout("Region Editor") {
Expand All @@ -321,7 +349,10 @@ impl ToolList {
}
}
TheEvent::TileEditorDragged(id, coord) => {
if id.name == "Region Editor View" || id.name == "Screen Editor View" {
if id.name == "Region Editor View"
|| id.name == "Screen Editor View"
|| id.name == "TerrainMap View"
{
let mut coord_f = Vec2f::from(*coord);
if id.name == "Region Editor View" {
if let Some(editor) = ui.get_rgba_layout("Region Editor") {
Expand All @@ -344,7 +375,10 @@ impl ToolList {
}
}
TheEvent::TileEditorUp(id) => {
if id.name == "Region Editor View" || id.name == "Screen Editor View" {
if id.name == "Region Editor View"
|| id.name == "Screen Editor View"
|| id.name == "TerrainMap View"
{
self.get_current_tool().tool_event(
ToolEvent::TileUp,
ToolContext::TwoD,
Expand Down
7 changes: 7 additions & 0 deletions creator/src/tools/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ impl Tool for SelectionTool {
) -> bool {
match tool_event {
Activate => {
if let Some(layout) = ui.get_sharedvlayout("Shared VLayout") {
layout.set_mode(TheSharedVLayoutMode::Top);
}

if let Some(layout) = ui.get_hlayout("Game Tool Params") {
layout.clear();

Expand Down Expand Up @@ -97,6 +101,9 @@ impl Tool for SelectionTool {
return true;
}
DeActivate => {
if let Some(layout) = ui.get_sharedvlayout("Shared VLayout") {
layout.set_mode(TheSharedVLayoutMode::Shared);
}
server_ctx.tile_selection = None;
ui.set_widget_context_menu("Region Editor View", None);
if let Some(layout) = ui.get_hlayout("Game Tool Params") {
Expand Down
Loading

0 comments on commit 02acf08

Please sign in to comment.