Skip to content

Commit

Permalink
Add hover data to the chapter editor
Browse files Browse the repository at this point in the history
  • Loading branch information
thane98 committed Jul 9, 2023
1 parent 568a351 commit b3dc8fd
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 30 deletions.
26 changes: 22 additions & 4 deletions astra/src/editors/chapter_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub struct ChapterEditor {
dispos_kind: DisposKind,
coordinate_kind: CoordinateKind,
dispos_difficulty: Difficulty,
hovered_tile: Option<String>,
hovered_spawn: Option<String>,
script_open_error: Option<String>,
selected_chapter_index: Option<usize>,

Expand All @@ -153,6 +155,8 @@ impl ChapterEditor {
dispos_kind: DisposKind::Main,
coordinate_kind: CoordinateKind::Dispos,
dispos_difficulty: Difficulty::All,
hovered_tile: None,
hovered_spawn: None,
script_open_error: None,
selected_chapter_index: None,

Expand Down Expand Up @@ -509,9 +513,15 @@ impl ChapterEditor {
})
{
TopBottomPanel::bottom("dispos_bottom_panel").show(ctx, |ui| {
ui.horizontal(|ui| {
ui.horizontal_top(|ui| {
ui.label("Tile Brightness");
ui.add(Slider::new(&mut config.terrain_brightness, 0.0..=1.0));
if let Some(tile) = self.hovered_tile.as_deref() {
ui.label(format!("Tile: {}", tile));
}
if let Some(spawn) = self.hovered_spawn.as_deref() {
ui.label(format!("Spawn: {}", spawn));
}
});
});

Expand All @@ -529,7 +539,7 @@ impl ChapterEditor {
.and_then(|state| state.terrain.as_ref())
{
chapter_terrain.read(|terrain_data| {
changed = dispos_grid(
let result = dispos_grid(
ui,
terrain_data,
state,
Expand All @@ -539,6 +549,9 @@ impl ChapterEditor {
self.dispos_difficulty,
config.terrain_brightness,
);
changed = result.changed;
self.hovered_tile = result.hovered_tile;
self.hovered_spawn = result.hovered_spawn;
});
} else {
ui.centered_and_justified(|ui| {
Expand Down Expand Up @@ -716,20 +729,25 @@ impl ChapterEditor {
ui.horizontal(|ui| {
ui.label("Tile Brightness");
ui.add(Slider::new(&mut config.terrain_brightness, 0.0..=1.0));
if let Some(tile) = self.hovered_tile.as_deref() {
ui.label(format!("Tile: {}", tile));
}
});
});

self.terrain_content.side_panel(ctx, &state.terrain, state);

CentralPanel::default().show(ctx, |ui| {
chapter_terrain.write(|terrain_data| {
terrain_grid(
let result = terrain_grid(
ui,
terrain_data,
self.terrain_content.selection(),
state,
config.terrain_brightness,
)
);
self.hovered_tile = result.hovered_tile;
result.changed
});
});
} else {
Expand Down
45 changes: 34 additions & 11 deletions astra/src/widgets/dispos_grid.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::collections::HashMap;

use astra_formats::TerrainData;
Expand All @@ -15,6 +16,12 @@ struct SpawnData<'a> {
spawn: &'a mut Spawn,
}

pub struct DisposGridResult {
pub changed: bool,
pub hovered_tile: Option<String>,
pub hovered_spawn: Option<String>,
}

fn get_position(spawn: &Spawn, coordinate_kind: CoordinateKind) -> (usize, usize) {
match coordinate_kind {
CoordinateKind::Dispos => (
Expand Down Expand Up @@ -109,14 +116,16 @@ pub fn dispos_grid(
coordinate_kind: CoordinateKind,
difficulty: Difficulty,
brightness: f32,
) -> bool {
) -> DisposGridResult {
let selected_spawn_position = selected_spawn
.as_ref()
.and_then(|(group, index)| dispos.get(group).and_then(|group| group.get(*index)))
.map(|spawn| get_position(spawn, coordinate_kind));
let spawn_data = SpawnDataMap::new(dispos, coordinate_kind, difficulty);
let mut changed = false;
let mut move_pos = None;
let mut hovered_tile = None;
let mut hovered_spawn = None;
ScrollArea::both()
.id_source("spawn_grid_scroll")
.show(ui, |ui| {
Expand All @@ -127,21 +136,24 @@ pub fn dispos_grid(
for row in (0..(terrain.height as usize)).rev() {
for col in 0..(terrain.width as usize) {
let sprite = spawn_data.get_sprite(state, row, col);
let fill = terrain
let (tile_name, fill) = terrain
.terrains
.get(row * 32 + col)
.and_then(|tid| data.get(tid.as_str()))
.map(|tile| {
Color32::from_rgb(
(tile.color_r.unwrap_or_default() as f32 * brightness)
as u8,
(tile.color_g.unwrap_or_default() as f32 * brightness)
as u8,
(tile.color_b.unwrap_or_default() as f32 * brightness)
as u8,
(
tile.text(state),
Color32::from_rgb(
(tile.color_r.unwrap_or_default() as f32 * brightness)
as u8,
(tile.color_g.unwrap_or_default() as f32 * brightness)
as u8,
(tile.color_b.unwrap_or_default() as f32 * brightness)
as u8,
),
)
})
.unwrap_or_else(|| Color32::from_gray(0));
.unwrap_or_else(|| (Cow::Borrowed("???"), Color32::from_gray(0)));

// Put these in a container to please egui's grid.
let mut button = Button::new("").rounding(0.).fill(fill);
Expand All @@ -155,6 +167,13 @@ pub fn dispos_grid(
ui.image(&sprite, sprite.size_vec2());
});
}
if response.hovered() {
hovered_tile = Some(tile_name.into_owned());
if let Some(spawn_data) = spawn_data.get_spawn(row, col) {
hovered_spawn =
Some(spawn_data.spawn.text(state).into_owned());
}
}
if response.clicked() {
move_pos = Some((row, col));
} else if response.secondary_clicked() {
Expand All @@ -180,5 +199,9 @@ pub fn dispos_grid(
}
}

changed
DisposGridResult {
changed,
hovered_tile,
hovered_spawn,
}
}
51 changes: 36 additions & 15 deletions astra/src/widgets/terrain_grid.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
use std::borrow::Cow;

use astra_formats::{TerrainData, UString};
use egui::{Button, Color32, Grid, ScrollArea, Ui, Vec2};

use crate::model::ViewItem;
use crate::{EditorState, ListModel};

pub struct TerrainGridResult {
pub changed: bool,
pub hovered_tile: Option<String>,
}

pub fn terrain_grid(
ui: &mut Ui,
terrain: &mut TerrainData,
selected_tile_index: Option<usize>,
state: &EditorState,
brightness: f32,
) -> bool {
let mut change = None;
) -> TerrainGridResult {
let mut changed = None;
let mut hovered_tile = None;
ScrollArea::both()
.id_source("chapter_terrain_scroll")
.show(ui, |ui| {
Expand All @@ -20,21 +29,24 @@ pub fn terrain_grid(
state.terrain.read(|data| {
for row in (0..(terrain.height as usize)).rev() {
for col in 0..(terrain.width as usize) {
let fill = terrain
let (tile_name, fill) = terrain
.terrains
.get(row * 32 + col)
.and_then(|tid| data.get(tid.as_str()))
.map(|tile| {
Color32::from_rgb(
(tile.color_r.unwrap_or_default() as f32 * brightness)
as u8,
(tile.color_g.unwrap_or_default() as f32 * brightness)
as u8,
(tile.color_b.unwrap_or_default() as f32 * brightness)
as u8,
(
tile.text(state),
Color32::from_rgb(
(tile.color_r.unwrap_or_default() as f32 * brightness)
as u8,
(tile.color_g.unwrap_or_default() as f32 * brightness)
as u8,
(tile.color_b.unwrap_or_default() as f32 * brightness)
as u8,
),
)
})
.unwrap_or_else(|| Color32::from_gray(0));
.unwrap_or_else(|| (Cow::Borrowed("???"), Color32::from_gray(0)));

let response =
ui.add_sized([48., 48.], Button::new("").rounding(0.).fill(fill));
Expand All @@ -43,21 +55,30 @@ pub fn terrain_grid(
.and_then(|index| data.item(index))
.map(|tile| tile.tid.clone());
if let Some(tid) = new_tid {
change = Some((row, col, tid));
changed = Some((row, col, tid));
}
}
if response.hovered() {
hovered_tile = Some(tile_name.into_owned());
}
}
ui.end_row();
}
});
});
});
if let Some((row, col, tid)) = change {
if let Some((row, col, tid)) = changed {
let index = row * 32 + col;
if index < terrain.terrains.len() {
terrain.terrains[index] = UString(tid);
}
return true;
return TerrainGridResult {
changed: true,
hovered_tile,
};
}
TerrainGridResult {
changed: false,
hovered_tile,
}
false
}

0 comments on commit b3dc8fd

Please sign in to comment.