Skip to content

Commit

Permalink
- fixed bug with changing key on compound-likes
Browse files Browse the repository at this point in the history
- added animation and delay for stealing entries
  • Loading branch information
RealRTTV committed Feb 10, 2024
1 parent f4faaec commit 88d65d4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub const SORT_COMPOUND_BY_NAME: Vec2u = Vec2u::new(67, 131);
pub const SORT_COMPOUND_BY_TYPE: Vec2u = Vec2u::new(83, 131);
pub const FREEHAND_MODE_UV: Vec2u = Vec2u::new(0, 144);
pub const ENABLED_FREEHAND_MODE_UV: Vec2u = Vec2u::new(16, 144);
pub const STEAL_ANIMATION_OVERLAY: Vec2u = Vec2u::new(64, 144);

pub const BYTE_UV: Vec2u = Vec2u::new(0, 0);
pub const SHORT_UV: Vec2u = Vec2u::new(16, 0);
Expand Down Expand Up @@ -131,6 +132,7 @@ pub const CHUNK_GHOST_UV: Vec2u = Vec2u::new(64, 48);
pub const ALERT_UV: Vec2u = Vec2u::new(112, 144);

pub const BASE_Z: u8 = 0;
pub const JUST_OVERLAPPING_BASE_Z: u8 = BASE_Z + 1;
pub const BASE_TEXT_Z: u8 = 10;
pub const JUST_OVERLAPPING_BASE_TEXT_Z: u8 = BASE_TEXT_Z + 1;
pub const TOGGLE_Z: u8 = 20;
Expand Down
Binary file modified src/assets/atlas.hex
Binary file not shown.
Binary file modified src/assets/atlas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/elements/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ impl NbtCompound {
pub const fn open(&self) -> bool { self.open }

pub fn update_key(&mut self, idx: usize, key: CompactString) -> Option<CompactString> {
if self.entries.has(key.as_ref()) {
if self.entries.get_idx(idx).is_some_and(|(k, _)| k == key) {
Some(key)
} else if self.entries.has(key.as_ref()) {
None
} else {
Some(unsafe { self.entries.update_key_idx_unchecked(idx, key) })
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#![feature(alloc_error_handler)]
#![feature(iter_next_chunk)]
#![feature(const_collections_with_hasher)]
#![feature(duration_constants)]
#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")]

use std::cmp::Ordering;
Expand Down Expand Up @@ -158,7 +159,7 @@ macro_rules! tab_mut {
/// * wiki page for docs on minecraft's format of stuff
/// * [chunk](NbtChunk) section rendering
/// # Minor Features
/// * sort entries on file read toggle
/// * sort entries on file read config
/// * gear icon to swap toolbar with settings panel
/// * __ctrl + h__, open a playground `nbt` file to help with user interaction (bonus points if I have some way to tell if you haven't used this editor before)
/// * [`last_modified`](NbtChunk) field actually gets some impl
Expand Down
13 changes: 8 additions & 5 deletions src/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ use flate2::Compression;
use uuid::Uuid;

use crate::{Bookmark, LinkedQueue, OptionExt, panic_unchecked, RenderContext, StrExt};
use crate::assets::{
BYTE_ARRAY_GHOST_UV, BYTE_ARRAY_UV, BYTE_GHOST_UV, BYTE_UV, CHUNK_GHOST_UV, CHUNK_UV, COMPOUND_GHOST_UV, COMPOUND_ROOT_UV, COMPOUND_UV, DOUBLE_GHOST_UV, DOUBLE_UV, ENABLED_FREEHAND_MODE_UV, FLOAT_GHOST_UV, FLOAT_UV, FREEHAND_MODE_UV, GZIP_FILE_TYPE_UV, HEADER_SIZE, HELD_SCROLLBAR_UV, INT_ARRAY_GHOST_UV, INT_ARRAY_UV, INT_GHOST_UV, INT_UV, LINE_NUMBER_SEPARATOR_UV, LIST_GHOST_UV, LIST_UV, LONG_ARRAY_GHOST_UV, LONG_ARRAY_UV, LONG_GHOST_UV, LONG_UV, MCA_FILE_TYPE_UV, NBT_FILE_TYPE_UV, REDO_UV,
REGION_UV, SCROLLBAR_Z, SHORT_GHOST_UV, SHORT_UV, SNBT_FILE_TYPE_UV, STRING_GHOST_UV, STRING_UV, UNDO_UV, UNHELD_SCROLLBAR_UV, UNKNOWN_NBT_GHOST_UV, UNKNOWN_NBT_UV, ZLIB_FILE_TYPE_UV,
};
use crate::assets::{BYTE_ARRAY_GHOST_UV, BYTE_ARRAY_UV, BYTE_GHOST_UV, BYTE_UV, CHUNK_GHOST_UV, CHUNK_UV, COMPOUND_GHOST_UV, COMPOUND_ROOT_UV, COMPOUND_UV, DOUBLE_GHOST_UV, DOUBLE_UV, ENABLED_FREEHAND_MODE_UV, FLOAT_GHOST_UV, FLOAT_UV, FREEHAND_MODE_UV, GZIP_FILE_TYPE_UV, HEADER_SIZE, HELD_SCROLLBAR_UV, INT_ARRAY_GHOST_UV, INT_ARRAY_UV, INT_GHOST_UV, INT_UV, JUST_OVERLAPPING_BASE_Z, LINE_NUMBER_SEPARATOR_UV, LIST_GHOST_UV, LIST_UV, LONG_ARRAY_GHOST_UV, LONG_ARRAY_UV, LONG_GHOST_UV, LONG_UV, MCA_FILE_TYPE_UV, NBT_FILE_TYPE_UV, REDO_UV, REGION_UV, SCROLLBAR_Z, SHORT_GHOST_UV, SHORT_UV, SNBT_FILE_TYPE_UV, STEAL_ANIMATION_OVERLAY, STRING_GHOST_UV, STRING_UV, UNDO_UV, UNHELD_SCROLLBAR_UV, UNKNOWN_NBT_GHOST_UV, UNKNOWN_NBT_UV, ZLIB_FILE_TYPE_UV};
use crate::elements::chunk::NbtRegion;
use crate::elements::compound::NbtCompound;
use crate::elements::element::NbtElement;
Expand Down Expand Up @@ -88,7 +85,7 @@ impl Tab {
}

#[allow(clippy::too_many_lines)]
pub fn render(&self, builder: &mut VertexBufferBuilder, ctx: &mut RenderContext, held: bool, held_entry: Option<&NbtElement>, skip_tooltips: bool) {
pub fn render(&self, builder: &mut VertexBufferBuilder, ctx: &mut RenderContext, held: bool, held_entry: Option<&NbtElement>, skip_tooltips: bool, steal_delta: f32) {
let mouse_x = ctx.mouse_x;
let mouse_y = ctx.mouse_y;

Expand Down Expand Up @@ -273,6 +270,12 @@ impl Tab {
builder.draw_texture((208, 26), uv, (16, 16));
}
}

if steal_delta > 0.0 {
let y = ((mouse_y - HEADER_SIZE) & !15) + HEADER_SIZE;
let height = (16.0 * steal_delta).round() as usize;
builder.draw_texture_region_z((ctx.left_margin - 2, y + (16 - height)), JUST_OVERLAPPING_BASE_Z, STEAL_ANIMATION_OVERLAY, (builder.window_width() + 2 - ctx.left_margin, height), (16, 16));
}
}

pub fn draw_icon(&self, builder: &mut VertexBufferBuilder, pos: impl Into<(usize, usize)>, z: u8) {
Expand Down
50 changes: 46 additions & 4 deletions src/workbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::string::String;
use std::sync::mpsc::TryRecvError;
use std::time::SystemTime;
use std::time::{Duration, Instant, SystemTime};

use compact_str::{format_compact, CompactString, ToCompactString};
use fxhash::{FxBuildHasher, FxHashSet};
Expand Down Expand Up @@ -59,6 +59,7 @@ pub struct Workbench {
pub cursor_visible: bool,
alerts: Vec<Alert>,
pub scale: usize,
steal_animation_data: Option<(Instant, Vec2u)>,
}

impl Workbench {
Expand Down Expand Up @@ -87,6 +88,7 @@ impl Workbench {
cursor_visible: true,
alerts: vec![],
scale: 1,
steal_animation_data: None,
};
'create_tab: {
if let Some(x) = &std::env::args()
Expand Down Expand Up @@ -238,6 +240,9 @@ impl Workbench {
if state == ElementState::Released {
if self.process_action_wheel() { return true }
self.scrollbar_offset = None;
if button == MouseButton::Left {
self.steal_animation_data = None;
}
self.held_mouse_keys.remove(&button);
if y < 19 && x > 2 && y > 3 {
self.click_tab(button, window_properties);
Expand Down Expand Up @@ -313,9 +318,13 @@ impl Workbench {
_ => {}
}
}
if !freehand_mode && self.held_entry.is_empty() && y >= HEADER_SIZE + 16 && x >= self.left_margin() + 16 && button == MouseButton::Left {
if self.steal() {
break 'a;
if button == MouseButton::Left {
if self.try_steal(true) {
if self.steal_animation_data.as_ref().is_some_and(|x| x.0.elapsed() >= Duration::from_millis(500)) && self.steal() {
break 'a;
}
} else {
self.steal_animation_data = None;
}
}
if (self.window_width - 16..self.window_width).contains(&x) && (26..42).contains(&y) {
Expand Down Expand Up @@ -645,6 +654,29 @@ impl Workbench {
Ok(())
}

#[inline]
fn try_steal(&mut self, initialize: bool) -> bool {
let left_margin = self.left_margin();
let horizontal_scroll = self.horizontal_scroll();
let scroll = self.scroll();
let tab = tab_mut!(self);

if self.mouse_x + horizontal_scroll < left_margin + 16 || self.mouse_y < HEADER_SIZE || !self.held_entry.is_empty() || tab.freehand_mode { return false };

let y = (self.mouse_y - HEADER_SIZE) / 16 + scroll / 16;
if y < tab.value.height() && y > 0 {
let target_depth = (self.mouse_x + horizontal_scroll - left_margin - 16) / 16;
let (depth, (_, _, _, _)) = Traverse::new(y, &mut tab.value).enumerate().last();
if initialize {
self.steal_animation_data.get_or_insert((Instant::now(), (target_depth, y).into()));
}
if let Some((_, Vec2u { x: expected_depth, y: expected_y })) = self.steal_animation_data.clone() {
return depth == expected_depth && y == expected_y
}
}
false
}

#[inline]
fn steal(&mut self) -> bool {
let left_margin = self.left_margin();
Expand Down Expand Up @@ -2231,6 +2263,8 @@ impl Workbench {
#[inline]
fn set_tab(&mut self, idx: usize, window_properties: &mut WindowProperties<'_>) {
self.tab = idx.min(self.tabs.len().saturating_sub(1));
// on any tab switch this should be discarded.
self.steal_animation_data = None;
window_properties.window_title(format!("{} - NBT Workbench", tab!(self).name).as_str());
}

Expand Down Expand Up @@ -2291,6 +2325,7 @@ impl Workbench {
self.scrollbar_offset.is_some(),
self.held_entry.element(),
self.action_wheel.is_some(),
self.steal_animation_data.as_ref().map(|x| x.0.elapsed().min(Duration::from_millis(500)).as_millis() as f32 / 500.0).unwrap_or(0.0)
);
if let Some(selected_text) = &tab.selected_text {
builder.horizontal_scroll = horizontal_scroll;
Expand All @@ -2316,6 +2351,13 @@ impl Workbench {
if (!self.held_entry.is_empty() || tab!(self).freehand_mode) && self.action_wheel.is_none() && self.scrollbar_offset.is_none() {
self.try_mouse_scroll();
}
if self.try_steal(false) {
if self.steal_animation_data.as_ref().is_some_and(|x| x.0.elapsed() >= Duration::from_millis(500)) {
self.steal();
}
} else {
self.steal_animation_data = None;
}
}

#[inline]
Expand Down

0 comments on commit 88d65d4

Please sign in to comment.