Skip to content

Commit

Permalink
added an incomplete search box
Browse files Browse the repository at this point in the history
  • Loading branch information
RealRTTV committed Feb 20, 2024
1 parent 299d97c commit 207d72b
Show file tree
Hide file tree
Showing 10 changed files with 999 additions and 815 deletions.
4 changes: 3 additions & 1 deletion src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ 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_UV: Vec2u = Vec2u::new(64, 144);
pub const STAMP_BACKDROP_UV: Vec2u = Vec2u::new(16, 160);
pub const MAGNIFYING_GLASS_UV: Vec2u = Vec2u::new(96, 48);

pub const BYTE_UV: Vec2u = Vec2u::new(0, 0);
pub const SHORT_UV: Vec2u = Vec2u::new(16, 0);
Expand Down Expand Up @@ -135,8 +136,9 @@ pub const INT_ARRAY_GHOST_UV: Vec2u = Vec2u::new(112, 16);
pub const LONG_ARRAY_GHOST_UV: Vec2u = Vec2u::new(0, 48);
pub const CHUNK_GHOST_UV: Vec2u = Vec2u::new(64, 48);
pub const ALERT_UV: Vec2u = Vec2u::new(112, 144);
pub const BACKDROP_UV: Vec2u = Vec2u::new(32, 160);

pub const BASE_Z: u8 = 0;
pub const BASE_Z: u8 = 5;
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;
Expand Down
Binary file modified src/assets/atlas.hex
Binary file not shown.
Binary file modified src/assets/build/atlas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@
)]
#![feature(array_chunks)]
#![feature(box_patterns)]
#![feature(const_black_box)]
#![feature(core_intrinsics)]
#![feature(iter_array_chunks)]
#![feature(iter_next_chunk)]
#![feature(lazy_cell)]
#![feature(let_chains)]
#![feature(maybe_uninit_array_assume_init)]
#![feature(maybe_uninit_uninit_array)]
#![feature(new_uninit)]
#![feature(optimize_attribute)]
#![feature(stmt_expr_attributes)]
#![feature(unchecked_math)]
#![feature(lazy_cell)]
#![feature(const_collections_with_hasher)]
#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")]

Expand All @@ -54,7 +55,7 @@ use std::rc::Rc;
use std::time::Duration;

use compact_str::{CompactString, ToCompactString};
use static_assertions::{const_assert, const_assert_eq};
use static_assertions::const_assert_eq;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::wasm_bindgen;
use winit::window::Window;
Expand All @@ -64,11 +65,8 @@ use vertex_buffer_builder::VertexBufferBuilder;

use crate::assets::{BASE_TEXT_Z, BASE_Z, BOOKMARK_UV, BOOKMARK_Z, END_LINE_NUMBER_SEPARATOR_UV, HEADER_SIZE, HIDDEN_BOOKMARK_UV, INSERTION_UV, INVALID_STRIPE_UV, LINE_NUMBER_SEPARATOR_UV, LINE_NUMBER_Z, SCROLLBAR_BOOKMARK_Z, SELECTED_TOGGLE_OFF_UV, SELECTED_TOGGLE_ON_UV, SELECTION_UV, SORT_COMPOUND_BY_NAME, SORT_COMPOUND_BY_NOTHING, SORT_COMPOUND_BY_TYPE, STAMP_BACKDROP_UV, TEXT_UNDERLINE_UV, TOGGLE_Z, UNSELECTED_TOGGLE_OFF_UV, UNSELECTED_TOGGLE_ON_UV};
use crate::color::TextColor;
use crate::elements::chunk::{NbtChunk, NbtRegion};
use crate::elements::compound::{CompoundMap, NbtCompound};
use crate::elements::element::{NbtByte, NbtByteArray, NbtDouble, NbtFloat, NbtInt, NbtIntArray, NbtLong, NbtLongArray, NbtShort};
use crate::elements::list::NbtList;
use crate::elements::string::NbtString;
use crate::elements::compound::{CompoundMap};
use crate::elements::element::{NbtByteArray, NbtIntArray, NbtLongArray};
use crate::tree_travel::Navigate;
use crate::vertex_buffer_builder::Vec2u;
use crate::workbench::Workbench;
Expand All @@ -88,6 +86,8 @@ mod window;
pub mod workbench;
mod workbench_action;
mod element_action;
mod search_box;
mod text;

#[macro_export]
macro_rules! flags {
Expand Down Expand Up @@ -198,7 +198,7 @@ extern "C" {
fn save(name: &str, bytes: Vec<u8>);
}

pub static mut WORKBENCH: UnsafeCell<Workbench> = UnsafeCell::new(Workbench::uninit());
pub static mut WORKBENCH: UnsafeCell<Workbench> = UnsafeCell::new(unsafe { Workbench::uninit() });
pub static mut WINDOW_PROPERTIES: UnsafeCell<WindowProperties> = UnsafeCell::new(WindowProperties::new(unsafe { core::mem::transmute::<_, Rc<Window>>(1_usize) }));

#[cfg(target_arch = "wasm32")]
Expand Down
153 changes: 153 additions & 0 deletions src/search_box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
use std::ops::{Deref, DerefMut};

use winit::keyboard::KeyCode;
use crate::assets::DARK_STRIPE_UV;

use crate::color::TextColor;
use crate::StrExt;
use crate::text::{Cachelike, KeyResult, Text};
use crate::vertex_buffer_builder::{Vec2u, VertexBufferBuilder};

#[derive(Clone, Eq)]
pub struct SearchBoxCache {
value: String,
cursor: usize,
selection: Option<usize>,
}

impl PartialEq for SearchBoxCache {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}

impl Cachelike<SearchBoxAdditional> for SearchBoxCache {
fn new(text: &Text<SearchBoxAdditional, Self>) -> Self where Self: Sized {
Self {
value: text.value.clone(),
cursor: text.cursor,
selection: text.selection,
}
}

fn revert(self, text: &mut Text<SearchBoxAdditional, Self>) where Self: Sized {
text.value = self.value;
text.cursor = self.cursor;
text.selection = self.selection;
}
}

#[derive(Clone)]
pub struct SearchBoxAdditional {
selected: bool,
horizontal_scroll: usize,
}

pub struct SearchBox(Text<SearchBoxAdditional, SearchBoxCache>);

impl Deref for SearchBox {
type Target = Text<SearchBoxAdditional, SearchBoxCache>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for SearchBox {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl SearchBox {
pub fn new() -> Self {
Self(Text::new(String::new(), 0, true, SearchBoxAdditional { selected: false, horizontal_scroll: 0 }))
}

pub const fn uninit() -> Self {
Self(Text::uninit())
}

pub fn render(&self, builder: &mut VertexBufferBuilder) {
use std::fmt::Write;

let pos = Vec2u::new(284, 23);

builder.draw_texture_region_z(
pos,
0,
DARK_STRIPE_UV,
(builder.window_width() - 215 - pos.x, 22),
(16, 16),
);

builder.horizontal_scroll = self.horizontal_scroll;
if self.value.is_empty() {
builder.settings(pos + (0, 3), false, 0);
builder.color = TextColor::Gray.to_raw();
let _ = write!(builder, "Search...");
}
if self.is_selected() {
self.0.render(builder, TextColor::White, pos + (0, 3), 0);
} else {
builder.settings(pos + (0, 3), false, 0);
builder.color = TextColor::White.to_raw();
let _ = write!(builder, "{}", self.value);
}
builder.horizontal_scroll = 0;
}

#[inline]
pub fn deselect(&mut self) {
self.selected = false;
self.cursor = 0;
self.selection = None;
// better ui this way
// self.horizontal_scroll = 0;
}

#[inline]
pub fn select(&mut self, x: usize) {
let x = x + self.horizontal_scroll;
self.cursor = 'a: {
let mut current_x = 0;
for (idx, char) in self.value.char_indices() {
let width = if (x as u32) < 56832 { VertexBufferBuilder::CHAR_WIDTH[char as usize] as usize } else { 0 };
if current_x + width / 2 >= x {
break 'a idx;
}
current_x += width;
}
self.value.len()
};
self.selected = true;
self.interact();
}

#[inline]
pub fn search(&mut self) {
todo!()
}

#[inline]
#[must_use]
pub fn is_selected(&self) -> bool {
self.selected
}

#[inline]
pub fn post_input(&mut self, window_dims: (usize, usize)) {
let (window_width, _) = window_dims;
self.0.post_input();
let field_width = window_width - 215 - 284;
let precursor_width = self.value.split_at(self.cursor).0.width();
// 8px space just to look cleaner
let horizontal_scroll = (precursor_width + 8).saturating_sub(field_width);
self.horizontal_scroll = horizontal_scroll;
}

#[must_use]
pub fn on_key_press(&mut self, key: KeyCode, char: Option<char>, flags: u8) -> KeyResult {
self.0.on_key_press(key, char, flags)
}
}
Loading

0 comments on commit 207d72b

Please sign in to comment.