merge ui into main

rust-rewrite
Patrick Cleavelin 2024-02-07 17:16:49 -06:00
commit 5d7a75de7f
16 changed files with 2211 additions and 1001 deletions

View File

@ -9,5 +9,5 @@ odin_highlighter:
odin build plugins/highlighter/src/ -build-mode:dll -no-entry-point -out:bin/highlighter
grep:
cargo b --manifest-path=plugins/grep/Cargo.toml
nightly-cargo b --manifest-path=plugins/grep/Cargo.toml
cp plugins/grep/target/debug/libgrep_plugin.dylib bin/

View File

@ -5,11 +5,11 @@
"systems": "systems"
},
"locked": {
"lastModified": 1701680307,
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
@ -87,11 +87,11 @@
},
"nixpkgs_2": {
"locked": {
"lastModified": 1703013332,
"narHash": "sha256-+tFNwMvlXLbJZXiMHqYq77z/RfmpfpiI3yjL6o/Zo9M=",
"lastModified": 1705856552,
"narHash": "sha256-JXfnuEf5Yd6bhMs/uvM67/joxYKoysyE3M2k6T3eWbg=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "54aac082a4d9bb5bbc5c4e899603abfb76a3f6d6",
"rev": "612f97239e2cc474c13c9dafa0df378058c5ad8d",
"type": "github"
},
"original": {
@ -131,11 +131,11 @@
"nixpkgs": "nixpkgs_3"
},
"locked": {
"lastModified": 1704075545,
"narHash": "sha256-L3zgOuVKhPjKsVLc3yTm2YJ6+BATyZBury7wnhyc8QU=",
"lastModified": 1706235145,
"narHash": "sha256-3jh5nahTlcsX6QFcMPqxtLn9p9CgT9RSce5GLqjcpi4=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "a0df72e106322b67e9c6e591fe870380bd0da0d5",
"rev": "3a57c4e29cb2beb777b2e6ae7309a680585b8b2f",
"type": "github"
},
"original": {

View File

@ -16,12 +16,19 @@
local-rust = (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain).override {
extensions = [ "rust-analysis" ];
};
local-nightly-rust = (pkgs.rust-bin.fromRustupToolchainFile ./plugins/grep/rust-toolchain.toml).override {
extensions = [ "rust-analysis" ];
};
nightly-cargo = pkgs.writeShellScriptBin "nightly-cargo" ''
export RUSTC="${local-nightly-rust}/bin/rustc";
exec "${local-nightly-rust}/bin/cargo" "$@"
'';
fixed-odin = pkgs.odin.overrideAttrs (finalAttrs: prevAttr: rec {
src = pkgs.fetchFromGitHub {
owner = "pcleavelin";
repo = "Odin";
rev = "59aa05170d54edff75aed220bb1653fc369573d7";
hash = "sha256-ZMcVugE0uRHba8jmQjTyQ9KKDUdIVSELggKDz9iSiwY=";
rev = "7b9ea9eca02bf5dd295439a46ed6103a0c4a44ff";
hash = "sha256-pxvU5veB1NEYPfer5roiLp/od2Pv4l1jJah0OHwb5yo=";
};
LLVM_CONFIG = "${pkgs.llvmPackages_17.llvm.dev}/bin/llvm-config";
nativeBuildInputs = with pkgs; prevAttr.nativeBuildInputs ++ [ libcxx libcxxabi ];
@ -58,7 +65,10 @@
buildInputs = with pkgs; (if pkgs.system == "aarch64-darwin" || pkgs.system == "x86_64-darwin" then [
fixed-odin
local-rust
nightly-cargo
rust-analyzer
SDL2
SDL2_ttf
darwin.apple_sdk.frameworks.CoreData
darwin.apple_sdk.frameworks.Kernel
darwin.apple_sdk.frameworks.CoreVideo

View File

@ -166,6 +166,145 @@ pub struct IteratorVTable {
pub until_end_of_word: *const c_void,
}
#[repr(C)]
pub struct UiInteraction {
pub hovering: bool,
pub clicked: bool,
}
#[repr(C)]
struct InternalUiSemanticSize {
kind: isize,
value: isize,
}
#[repr(isize)]
pub enum UiAxis {
Horizontal = 0,
Vertical,
}
pub enum UiSemanticSize {
FitText,
Exact(isize),
ChildrenSum,
Fill,
PercentOfParent(isize),
}
impl From<UiSemanticSize> for InternalUiSemanticSize {
fn from(value: UiSemanticSize) -> Self {
let (kind, value) = match value {
UiSemanticSize::FitText => (0, 0),
UiSemanticSize::Exact(value) => (1, value),
UiSemanticSize::ChildrenSum => (2, 0),
UiSemanticSize::Fill => (3, 0),
UiSemanticSize::PercentOfParent(value) => (4, value),
};
Self { kind, value }
}
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct UiContext(*const c_void);
#[repr(C)]
#[derive(Clone, Copy)]
pub struct UiBox(*const c_void);
type UiPushParentProc = extern "C" fn(ui_context: UiContext, ui_box: UiBox);
type UiPopParentProc = extern "C" fn(ui_context: UiContext);
type UiFloatingProc =
extern "C" fn(ui_context: UiContext, label: *const i8, pos: [isize; 2]) -> UiBox;
type UiRectProc = extern "C" fn(
ui_context: UiContext,
label: *const i8,
border: bool,
border: bool,
axis: UiAxis,
size: [InternalUiSemanticSize; 2],
) -> UiBox;
type UiSimpleProc = extern "C" fn(ui_context: UiContext, label: *const i8) -> UiInteraction;
type UiBufferProc = extern "C" fn(ui_context: UiContext, buffer: Buffer, show_line_numbers: bool);
#[repr(C)]
pub struct UiVTable {
ui_context: UiContext,
push_parent: UiPushParentProc,
pop_parent: UiPopParentProc,
spacer: UiSimpleProc,
floating: UiFloatingProc,
rect: UiRectProc,
button: UiSimpleProc,
label: UiSimpleProc,
buffer: UiBufferProc,
buffer_from_index: UiBufferProc,
}
impl UiVTable {
pub fn push_parent(&self, ui_box: UiBox) {
(self.push_parent)(self.ui_context, ui_box);
}
pub fn pop_parent(&self) {
(self.pop_parent)(self.ui_context);
}
pub fn spacer(&self, label: &CStr) -> UiInteraction {
(self.spacer)(self.ui_context, label.as_ptr())
}
pub fn push_rect(
&self,
label: &CStr,
show_background: bool,
show_border: bool,
axis: UiAxis,
horizontal_size: UiSemanticSize,
vertical_size: UiSemanticSize,
inner: impl FnOnce(&UiVTable),
) {
let rect = (self.rect)(
self.ui_context,
label.as_ptr(),
show_background,
show_border,
axis,
[horizontal_size.into(), vertical_size.into()],
);
self.push_parent(rect);
inner(self);
self.pop_parent();
}
pub fn push_floating(&self, label: &CStr, x: isize, y: isize, inner: impl FnOnce(&UiVTable)) {
let floating = (self.floating)(self.ui_context, label.as_ptr(), [x, y]);
self.push_parent(floating);
inner(self);
self.pop_parent();
}
pub fn label(&self, label: &CStr) -> UiInteraction {
(self.label)(self.ui_context, label.as_ptr())
}
pub fn button(&self, label: &CStr) -> UiInteraction {
(self.button)(self.ui_context, label.as_ptr())
}
pub fn buffer(&self, buffer: Buffer, show_line_numbers: bool) {
(self.buffer)(self.ui_context, buffer, show_line_numbers)
}
}
type OnColorBufferProc = extern "C" fn(plugin: Plugin, buffer: *const c_void);
type OnHookProc = extern "C" fn(plugin: Plugin, buffer: Buffer);
type InputGroupProc = extern "C" fn(plugin: Plugin, input_map: InputMap);
@ -176,8 +315,10 @@ type WindowGetBufferProc = extern "C" fn(plugin: Plugin, window: *const c_void)
#[repr(C)]
pub struct Plugin {
state: *const c_void,
pub iter_table: IteratorVTable,
pub buffer_table: BufferVTable,
pub ui_table: UiVTable,
pub register_hook: extern "C" fn(hook: Hook, on_hook: OnHookProc),
pub register_highlighter:
@ -361,119 +502,246 @@ pub enum Hook {
#[repr(i32)]
pub enum Key {
KeyNull = 0, // Key: NULL, used for no key pressed
// Alphanumeric keys
Apostrophe = 39, // key: '
Comma = 44, // Key: ,
Minus = 45, // Key: -
Period = 46, // Key: .
Slash = 47, // Key: /
Zero = 48, // Key: 0
One = 49, // Key: 1
Two = 50, // Key: 2
Three = 51, // Key: 3
Four = 52, // Key: 4
Five = 53, // Key: 5
Six = 54, // Key: 6
Seven = 55, // Key: 7
Eight = 56, // Key: 8
Nine = 57, // Key: 9
Semicolon = 59, // Key: ;
Equal = 61, // Key: =
A = 65, // Key: A | a
B = 66, // Key: B | b
C = 67, // Key: C | c
D = 68, // Key: D | d
E = 69, // Key: E | e
F = 70, // Key: F | f
G = 71, // Key: G | g
H = 72, // Key: H | h
I = 73, // Key: I | i
J = 74, // Key: J | j
K = 75, // Key: K | k
L = 76, // Key: L | l
M = 77, // Key: M | m
N = 78, // Key: N | n
O = 79, // Key: O | o
P = 80, // Key: P | p
Q = 81, // Key: Q | q
R = 82, // Key: R | r
S = 83, // Key: S | s
T = 84, // Key: T | t
U = 85, // Key: U | u
V = 86, // Key: V | v
W = 87, // Key: W | w
X = 88, // Key: X | x
Y = 89, // Key: Y | y
Z = 90, // Key: Z | z
LeftBracket = 91, // Key: [
Backslash = 92, // Key: '\'
RightBracket = 93, // Key: ]
Grave = 96, // Key: `
// Function keys
Space = 32, // Key: Space
Escape = 256, // Key: Esc
Enter = 257, // Key: Enter
Tab = 258, // Key: Tab
Backspace = 259, // Key: Backspace
Insert = 260, // Key: Ins
Delete = 261, // Key: Del
Right = 262, // Key: Cursor right
Left = 263, // Key: Cursor left
Down = 264, // Key: Cursor down
Up = 265, // Key: Cursor up
PageUp = 266, // Key: Page up
PageDown = 267, // Key: Page down
Home = 268, // Key: Home
End = 269, // Key: End
CapsLock = 280, // Key: Caps lock
ScrollLock = 281, // Key: Scroll down
NumLock = 282, // Key: Num lock
PrintScreen = 283, // Key: Print screen
Pause = 284, // Key: Pause
F1 = 290, // Key: F1
F2 = 291, // Key: F2
F3 = 292, // Key: F3
F4 = 293, // Key: F4
F5 = 294, // Key: F5
F6 = 295, // Key: F6
F7 = 296, // Key: F7
F8 = 297, // Key: F8
F9 = 298, // Key: F9
F10 = 299, // Key: F10
F11 = 300, // Key: F11
F12 = 301, // Key: F12
LeftShift = 340, // Key: Shift left
LeftControl = 341, // Key: Control left
LeftAlt = 342, // Key: Alt left
LeftSuper = 343, // Key: Super left
RightShift = 344, // Key: Shift right
RightControl = 345, // Key: Control right
RightAlt = 346, // Key: Alt right
RightSuper = 347, // Key: Super right
KbMenu = 348, // Key: KB menu
// Keypad keys
Kp0 = 320, // Key: Keypad 0
Kp1 = 321, // Key: Keypad 1
Kp2 = 322, // Key: Keypad 2
Kp3 = 323, // Key: Keypad 3
Kp4 = 324, // Key: Keypad 4
Kp5 = 325, // Key: Keypad 5
Kp6 = 326, // Key: Keypad 6
Kp7 = 327, // Key: Keypad 7
Kp8 = 328, // Key: Keypad 8
Kp9 = 329, // Key: Keypad 9
KpDecimal = 330, // Key: Keypad .
KpDivide = 331, // Key: Keypad /
KpMultiply = 332, // Key: Keypad *
KpSubtract = 333, // Key: Keypad -
KpAdd = 334, // Key: Keypad +
KpEnter = 335, // Key: Keypad Enter
KpEqual = 336, // Key: Keypad =
// Android key buttons
Back = 4, // Key: Android back button
VolumeUp = 24, // Key: Android volume up button
VolumeDown = 25, // Key: Android volume down button
UNKNOWN = 0,
Enter = 13,
ESCAPE = 27,
BACKSPACE = 8,
TAB = 9,
Space = 32,
EXCLAIM = 33,
QUOTEDBL = 34,
HASH = 35,
PERCENT = 37,
DOLLAR = 36,
AMPERSAND = 38,
QUOTE = 39,
LEFTPAREN = 40,
RIGHTPAREN = 41,
ASTERISK = 42,
PLUS = 43,
COMMA = 44,
MINUS = 45,
PERIOD = 46,
SLASH = 47,
NUM0 = 48,
NUM1 = 49,
NUM2 = 50,
NUM3 = 51,
NUM4 = 52,
NUM5 = 53,
NUM6 = 54,
NUM7 = 55,
NUM8 = 56,
NUM9 = 57,
COLON = 58,
SEMICOLON = 59,
LESS = 60,
EQUAL = 61,
GREATER = 62,
QUESTION = 63,
AT = 64,
LEFTBRACKET = 91,
BACKSLASH = 92,
RIGHTBRACKET = 93,
CARET = 94,
UNDERSCORE = 95,
BACKQUOTE = 96,
A = 97,
B = 98,
C = 99,
D = 100,
E = 101,
F = 102,
G = 103,
H = 104,
I = 105,
J = 106,
K = 107,
L = 108,
M = 109,
N = 110,
O = 111,
P = 112,
Q = 113,
R = 114,
S = 115,
T = 116,
U = 117,
V = 118,
W = 119,
X = 120,
Y = 121,
Z = 122,
CAPSLOCK = 1073741881,
F1 = 1073741882,
F2 = 1073741883,
F3 = 1073741884,
F4 = 1073741885,
F5 = 1073741886,
F6 = 1073741887,
F7 = 1073741888,
F8 = 1073741889,
F9 = 1073741890,
F10 = 1073741891,
F11 = 1073741892,
F12 = 1073741893,
PRINTSCREEN = 1073741894,
SCROLLLOCK = 1073741895,
PAUSE = 1073741896,
INSERT = 1073741897,
HOME = 1073741898,
PAGEUP = 1073741899,
DELETE = 127,
END = 1073741901,
PAGEDOWN = 1073741902,
RIGHT = 1073741903,
LEFT = 1073741904,
DOWN = 1073741905,
UP = 1073741906,
NUMLOCKCLEAR = 1073741907,
KpDivide = 1073741908,
KpMultiply = 1073741909,
KpMinus = 1073741910,
KpPlus = 1073741911,
KpEnter = 1073741912,
Kp1 = 1073741913,
Kp2 = 1073741914,
Kp3 = 1073741915,
Kp4 = 1073741916,
Kp5 = 1073741917,
Kp6 = 1073741918,
Kp7 = 1073741919,
Kp8 = 1073741920,
Kp9 = 1073741921,
Kp0 = 1073741922,
KpPeriod = 1073741923,
APPLICATION = 1073741925,
POWER = 1073741926,
KpEquals = 1073741927,
F13 = 1073741928,
F14 = 1073741929,
F15 = 1073741930,
F16 = 1073741931,
F17 = 1073741932,
F18 = 1073741933,
F19 = 1073741934,
F20 = 1073741935,
F21 = 1073741936,
F22 = 1073741937,
F23 = 1073741938,
F24 = 1073741939,
EXECUTE = 1073741940,
HELP = 1073741941,
MENU = 1073741942,
SELECT = 1073741943,
STOP = 1073741944,
AGAIN = 1073741945,
UNDO = 1073741946,
CUT = 1073741947,
COPY = 1073741948,
PASTE = 1073741949,
FIND = 1073741950,
MUTE = 1073741951,
VOLUMEUP = 1073741952,
VOLUMEDOWN = 1073741953,
KpComma = 1073741957,
KpEqualsas400 = 1073741958,
ALTERASE = 1073741977,
SYSREQ = 1073741978,
CANCEL = 1073741979,
CLEAR = 1073741980,
PRIOR = 1073741981,
RETURN2 = 1073741982,
SEPARATOR = 1073741983,
OUT = 1073741984,
OPER = 1073741985,
CLEARAGAIN = 1073741986,
CRSEL = 1073741987,
EXSEL = 1073741988,
Kp00 = 1073742000,
Kp000 = 1073742001,
THOUSANDSSEPARATOR = 1073742002,
DECIMALSEPARATOR = 1073742003,
CURRENCYUNIT = 1073742004,
CURRENCYSUBUNIT = 1073742005,
KpLeftparen = 1073742006,
KpRightparen = 1073742007,
KpLeftbrace = 1073742008,
KpRightbrace = 1073742009,
KpTab = 1073742010,
KpBackspace = 1073742011,
KpA = 1073742012,
KpB = 1073742013,
KpC = 1073742014,
KpD = 1073742015,
KpE = 1073742016,
KpF = 1073742017,
KpXor = 1073742018,
KpPower = 1073742019,
KpPercent = 1073742020,
KpLess = 1073742021,
KpGreater = 1073742022,
KpAmpersand = 1073742023,
KpDblampersand = 1073742024,
KpVerticalbar = 1073742025,
KpDblverticalbar = 1073742026,
KpColon = 1073742027,
KpHash = 1073742028,
KpSpace = 1073742029,
KpAt = 1073742030,
KpExclam = 1073742031,
KpMemstore = 1073742032,
KpMemrecall = 1073742033,
KpMemclear = 1073742034,
KpMemadd = 1073742035,
KpMemsubtract = 1073742036,
KpMemmultiply = 1073742037,
KpMemdivide = 1073742038,
KpPlusminus = 1073742039,
KpClear = 1073742040,
KpClearentry = 1073742041,
KpBinary = 1073742042,
KpOctal = 1073742043,
KpDecimal = 1073742044,
KpHexadecimal = 1073742045,
LCTRL = 1073742048,
LSHIFT = 1073742049,
LALT = 1073742050,
LGUI = 1073742051,
RCTRL = 1073742052,
RSHIFT = 1073742053,
RALT = 1073742054,
RGUI = 1073742055,
MODE = 1073742081,
AUDIONEXT = 1073742082,
AUDIOPREV = 1073742083,
AUDIOSTOP = 1073742084,
AUDIOPLAY = 1073742085,
AUDIOMUTE = 1073742086,
MEDIASELECT = 1073742087,
WWW = 1073742088,
MAIL = 1073742089,
CALCULATOR = 1073742090,
COMPUTER = 1073742091,
AcSearch = 1073742092,
AcHome = 1073742093,
AcBack = 1073742094,
AcForward = 1073742095,
AcStop = 1073742096,
AcRefresh = 1073742097,
AcBookmarks = 1073742098,
BRIGHTNESSDOWN = 1073742099,
BRIGHTNESSUP = 1073742100,
DISPLAYSWITCH = 1073742101,
KBDILLUMTOGGLE = 1073742102,
KBDILLUMDOWN = 1073742103,
KBDILLUMUP = 1073742104,
EJECT = 1073742105,
SLEEP = 1073742106,
APP1 = 1073742107,
APP2 = 1073742108,
AUDIOREWIND = 1073742109,
AUDIOFASTFORWARD = 1073742110,
}
#[repr(i32)]

View File

@ -4,7 +4,6 @@ package buffer_search;
import "core:runtime"
import "core:fmt"
import "core:path/filepath"
import "vendor:raylib"
import p "../../src/plugin"
import "../../src/theme"
@ -99,34 +98,91 @@ buffer_list_iter :: proc(plugin: Plugin, buffer_index: ^int) -> (int, int, bool)
draw_buffer_window :: proc "c" (plugin: Plugin, win: rawptr) {
context = runtime.default_context();
runtime.free_all(context.temp_allocator);
win := cast(^BufferListWindow)win;
if win == nil {
return;
}
screen_width := plugin.get_screen_width();
screen_height := plugin.get_screen_height();
directory := string(plugin.get_current_directory());
canvas := plugin.ui.floating(plugin.ui.ui_context, "buffer search canvas", {0,0});
plugin.ui.push_parent(plugin.ui.ui_context, canvas);
{
defer plugin.ui.pop_parent(plugin.ui.ui_context);
plugin.ui.spacer(plugin.ui.ui_context, "left spacer");
centered_container := plugin.ui.rect(plugin.ui.ui_context, "centered container", false, false, .Vertical, {{4, 75}, {3, 0}});
plugin.ui.push_parent(plugin.ui.ui_context, centered_container);
{
defer plugin.ui.pop_parent(plugin.ui.ui_context);
plugin.ui.spacer(plugin.ui.ui_context, "top spacer");
ui_window := plugin.ui.rect(plugin.ui.ui_context, "buffer search window", true, true, .Horizontal, {{3, 0}, {4, 75}});
plugin.ui.push_parent(plugin.ui.ui_context, ui_window);
{
defer plugin.ui.pop_parent(plugin.ui.ui_context);
buffer_list_view := plugin.ui.rect(plugin.ui.ui_context, "buffer list view", false, false, .Vertical, {{4, 60}, {3, 0}});
plugin.ui.push_parent(plugin.ui.ui_context, buffer_list_view);
{
defer plugin.ui.pop_parent(plugin.ui.ui_context);
_buffer_index := 0;
for index in buffer_list_iter(plugin, &_buffer_index) {
buffer := plugin.buffer.get_buffer_info_from_index(index);
relative_file_path, _ := filepath.rel(directory, string(buffer.file_path), context.temp_allocator)
text := fmt.ctprintf("%s:%d", relative_file_path, buffer.cursor.line+1);
if index == win.selected_index {
plugin.ui.button(plugin.ui.ui_context, text);
} else {
plugin.ui.label(plugin.ui.ui_context, text);
}
}
}
buffer_preview := plugin.ui.rect(plugin.ui.ui_context, "buffer preview", true, false, .Horizontal, {{3, 0}, {3, 0}});
plugin.ui.push_parent(plugin.ui.ui_context, buffer_preview);
{
defer plugin.ui.pop_parent(plugin.ui.ui_context);
plugin.ui.buffer_from_index(plugin.ui.ui_context, win.selected_index, false);
}
}
plugin.ui.spacer(plugin.ui.ui_context, "bottom spacer");
}
plugin.ui.spacer(plugin.ui.ui_context, "right spacer");
}
/*
screen_width := plugin.get_screen_width();
screen_height := plugin.get_screen_height();
source_font_width := plugin.get_font_width();
source_font_height := plugin.get_font_height();
win_rec := raylib.Rectangle {
x = f32(screen_width/8),
y = f32(screen_height/8),
width = f32(screen_width - screen_width/4),
height = f32(screen_height - screen_height/4),
win_rec := [4]f32 {
f32(screen_width/8),
f32(screen_height/8),
f32(screen_width - screen_width/4),
f32(screen_height - screen_height/4),
};
plugin.draw_rect(
i32(win_rec.x),
i32(win_rec.y),
i32(win_rec.width),
i32(win_rec.height),
i32(win_rec.z),
i32(win_rec.w),
.Background4
);
win_margin := raylib.Vector2 { f32(source_font_width), f32(source_font_height) };
win_margin := [2]f32 { f32(source_font_width), f32(source_font_height) };
buffer_prev_width := (win_rec.width - win_margin.x*2) / 2;
buffer_prev_height := win_rec.height - win_margin.y*2;
buffer_prev_width := (win_rec.z - win_margin.x*2) / 2;
buffer_prev_height := win_rec.w - win_margin.y*2;
glyph_buffer_width := int(buffer_prev_width) / source_font_width - 1;
glyph_buffer_height := int(buffer_prev_height) / source_font_height;
@ -134,7 +190,7 @@ draw_buffer_window :: proc "c" (plugin: Plugin, win: rawptr) {
directory := string(plugin.get_current_directory());
plugin.draw_rect(
i32(win_rec.x + win_rec.width / 2),
i32(win_rec.x + win_rec.z / 2),
i32(win_rec.y + win_margin.y),
i32(buffer_prev_width),
i32(buffer_prev_height),
@ -151,7 +207,7 @@ draw_buffer_window :: proc "c" (plugin: Plugin, win: rawptr) {
if index == win.selected_index {
plugin.draw_buffer_from_index(
index,
int(win_rec.x + win_margin.x + win_rec.width / 2),
int(win_rec.x + win_margin.x + win_rec.z / 2),
int(win_rec.y + win_margin.y),
glyph_buffer_width,
glyph_buffer_height,
@ -174,4 +230,5 @@ draw_buffer_window :: proc "c" (plugin: Plugin, win: rawptr) {
runtime.free_all(context.temp_allocator);
}
*/
}

View File

@ -0,0 +1,3 @@
[toolchain]
channel = "nightly-2024-01-24"

View File

@ -1,6 +1,6 @@
use std::{
error::Error,
ffi::OsString,
ffi::{CString, OsString},
path::Path,
str::FromStr,
sync::mpsc::{Receiver, Sender},
@ -8,10 +8,10 @@ use std::{
};
use grep::{
regex::{RegexMatcherBuilder},
regex::RegexMatcherBuilder,
searcher::{BinaryDetection, SearcherBuilder, Sink, SinkError},
};
use plugin_rs_bindings::{Buffer, Closure, Hook, InputMap, Key, PaletteColor, Plugin};
use plugin_rs_bindings::{Buffer, Closure, Hook, InputMap, Key, Plugin, UiAxis, UiSemanticSize};
use std::sync::mpsc::channel;
use walkdir::WalkDir;
@ -213,6 +213,7 @@ pub extern "C" fn OnInitialize(plugin: Plugin) {
None => 0,
};
if match_count > 0 {
let index_threshold = std::cmp::max(max_mats_to_draw-4, 0) as usize;
if window.selected_match < match_count-1 {
@ -226,6 +227,7 @@ pub extern "C" fn OnInitialize(plugin: Plugin) {
window.top_index = 0;
}
}
}
}), "move selection down\0".as_ptr());
}), draw_window, free_window, Some(Closure!((_plugin: Plugin, window: *const std::ffi::c_void) -> Buffer => {
let window = Box::leak(unsafe { Box::<GrepWindow>::from_raw(window as *mut GrepWindow) });
@ -271,62 +273,58 @@ extern "C" fn draw_window(plugin: Plugin, window: *const std::ffi::c_void) {
let screen_width = (plugin.get_screen_width)() as i32;
let screen_height = (plugin.get_screen_height)() as i32;
let font_width = (plugin.get_font_width)() as i32;
let font_height = (plugin.get_font_height)() as i32;
let x = screen_width / 8;
let y = screen_height / 8;
let width = screen_width - screen_width / 4;
let height = screen_height - screen_height / 4;
let buffer_prev_width = (width - font_width * 2) / 2;
let glyph_buffer_width = buffer_prev_width / font_width - 1;
let glyph_buffer_height = 1;
let dir = plugin.get_current_directory();
let directory = Path::new(dir.as_ref());
(plugin.draw_rect)(x, y, width, height, PaletteColor::Background4);
(plugin.draw_rect)(
x + font_width,
y + font_height,
width - font_width * 2,
height - font_height * 3,
PaletteColor::Background3,
);
plugin
.ui_table
.push_floating(c"grep canvas", 0, 0, |ui_table| {
// TODO: make some primitive that centers a Box
ui_table.spacer(c"left spacer");
if let Some(buffer) = window.input_buffer {
(plugin.draw_rect)(
x + font_width,
y + height - font_height * 2,
buffer_prev_width,
font_height,
PaletteColor::Background2,
);
(plugin.draw_buffer)(
buffer,
(x + font_width) as isize,
(y + height - font_height * 2) as isize,
(glyph_buffer_width) as isize,
(glyph_buffer_height) as isize,
ui_table.push_rect(
c"centered container",
false,
);
}
false,
UiAxis::Vertical,
UiSemanticSize::PercentOfParent(75),
UiSemanticSize::Fill,
|ui_table| {
ui_table.spacer(c"top spacer");
ui_table.push_rect(
c"grep window",
true,
true,
UiAxis::Vertical,
UiSemanticSize::Fill,
UiSemanticSize::PercentOfParent(75),
|ui_table| {
if let Ok(sink) = window.rx.try_recv() {
window.sink = Some(sink);
}
if let Some(sink) = &window.sink {
if !sink.matches.is_empty() {
ui_table.push_rect(
c"results list",
false,
false,
UiAxis::Vertical,
UiSemanticSize::Fill,
UiSemanticSize::Fill,
|ui_table| match &window.sink {
Some(sink) if !sink.matches.is_empty() => {
let num_mats_to_draw = std::cmp::min(
(sink.matches.len() - window.top_index) as i32,
(height - font_height * 2) / (font_height) - 1,
(height - font_height) / (font_height),
);
let max_mat_length = (width - font_width * 2) / font_width;
for (i, mat) in sink.matches[window.top_index..].iter().enumerate() {
for (i, mat) in
sink.matches[window.top_index..].iter().enumerate()
{
let index = i + window.top_index;
if i as i32 >= num_mats_to_draw {
break;
@ -343,37 +341,70 @@ extern "C" fn draw_window(plugin: Plugin, window: *const std::ffi::c_void) {
let text = match mat.line_number {
Some(line_number) => format!(
"{}:{}:{}: {}",
relative_file_path, line_number, mat.column, matched_text
relative_file_path,
line_number,
mat.column,
matched_text
),
None => format!(
"{}:{}: {}",
relative_file_path, mat.column, matched_text
),
None => format!("{}:{}: {}", relative_file_path, mat.column, matched_text),
};
let text = if text.len() > max_mat_length as usize {
text.as_str().split_at(max_mat_length as usize).0
} else {
&text
};
let text = format!("{text}\0");
if index == window.selected_match {
(plugin.draw_rect)(
x + font_width,
y + font_height + ((index - window.top_index) as i32) * font_height,
(text.chars().count() as i32) * font_width,
font_height,
PaletteColor::Background2,
// TODO: don't use button here, but apply a style
// to `label`
ui_table.button(
&CString::new(text).expect("valid text"),
);
} else {
ui_table.label(
&CString::new(text).expect("valid text"),
);
}
}
}
Some(_) | None => {
ui_table.spacer(c"top spacer");
ui_table.push_rect(
c"centered text container",
false,
false,
UiAxis::Horizontal,
UiSemanticSize::Fill,
UiSemanticSize::Fill,
|ui_table| {
ui_table.spacer(c"left spacer");
ui_table.label(c"no results");
ui_table.spacer(c"right spacer");
},
);
ui_table.spacer(c"bottom spacer");
}
},
);
(plugin.draw_text)(
text.as_ptr() as *const i8,
(x + font_width) as f32,
(y + font_height + ((index - window.top_index) as i32) * font_height) as f32,
PaletteColor::Foreground2,
ui_table.push_rect(
c"grep window",
true,
false,
UiAxis::Vertical,
UiSemanticSize::Fill,
UiSemanticSize::Exact(font_height as isize),
|ui_table| {
if let Some(buffer) = window.input_buffer {
ui_table.buffer(buffer, false);
}
},
);
}
}
}
},
);
ui_table.spacer(c"bottom spacer");
},
);
ui_table.spacer(c"right spacer");
});
}
extern "C" fn on_buffer_input(plugin: Plugin, buffer: Buffer) {

View File

@ -277,6 +277,9 @@ is_rust_keyword :: proc(plugin: Plugin, start: BufferIter, end: BufferIter) -> (
return;
}
// TODO: split logic into single line coloring, and multi-line coloring.
// single line coloring can be done directly on the glyph buffer
// (with some edge cases, literally, the edge of the screen)
color_buffer_odin :: proc "c" (plugin: Plugin, buffer: rawptr) {
context = runtime.default_context();

View File

@ -2,7 +2,7 @@ package core
import "core:runtime"
import "core:fmt"
import "vendor:raylib"
import "vendor:sdl2"
import "../plugin"
@ -42,12 +42,15 @@ close_window_and_free :: proc(state: ^State) {
State :: struct {
ctx: runtime.Context,
sdl_renderer: ^sdl2.Renderer,
font_atlas: FontAtlas,
mode: Mode,
should_close: bool,
screen_height: int,
screen_width: int,
font: raylib.Font,
width_dpi_ratio: f32,
height_dpi_ratio: f32,
directory: string,
@ -86,14 +89,14 @@ Action :: struct {
description: string,
}
InputMap :: struct {
key_actions: map[raylib.KeyboardKey]Action,
ctrl_key_actions: map[raylib.KeyboardKey]Action,
key_actions: map[plugin.Key]Action,
ctrl_key_actions: map[plugin.Key]Action,
}
new_input_map :: proc() -> InputMap {
input_map := InputMap {
key_actions = make(map[raylib.KeyboardKey]Action),
ctrl_key_actions = make(map[raylib.KeyboardKey]Action),
key_actions = make(map[plugin.Key]Action),
ctrl_key_actions = make(map[plugin.Key]Action),
}
return input_map;
@ -106,7 +109,7 @@ delete_input_map :: proc(input_map: ^InputMap) {
// NOTE(pcleavelin): might be a bug in the compiler where it can't coerce
// `EditorAction` to `InputGroup` when given as a proc parameter, that is why there
// are two functions
register_plugin_key_action_single :: proc(input_map: ^InputMap, key: raylib.KeyboardKey, action: PluginEditorAction, description: string = "") {
register_plugin_key_action_single :: proc(input_map: ^InputMap, key: plugin.Key, action: PluginEditorAction, description: string = "") {
if ok := key in input_map.key_actions; ok {
// TODO: log that key is already registered
fmt.eprintln("plugin key already registered with single action", key);
@ -118,7 +121,7 @@ register_plugin_key_action_single :: proc(input_map: ^InputMap, key: raylib.Keyb
};
}
register_key_action_single :: proc(input_map: ^InputMap, key: raylib.KeyboardKey, action: EditorAction, description: string = "") {
register_key_action_single :: proc(input_map: ^InputMap, key: plugin.Key, action: EditorAction, description: string = "") {
if ok := key in input_map.key_actions; ok {
// TODO: log that key is already registered
fmt.eprintln("key already registered with single action", key);
@ -130,7 +133,7 @@ register_key_action_single :: proc(input_map: ^InputMap, key: raylib.KeyboardKey
};
}
register_key_action_group :: proc(input_map: ^InputMap, key: raylib.KeyboardKey, input_group: InputGroup, description: string = "") {
register_key_action_group :: proc(input_map: ^InputMap, key: plugin.Key, input_group: InputGroup, description: string = "") {
if ok := key in input_map.key_actions; ok {
// TODO: log that key is already registered
fmt.eprintln("key already registered with single action", key);
@ -142,7 +145,7 @@ register_key_action_group :: proc(input_map: ^InputMap, key: raylib.KeyboardKey,
};
}
register_ctrl_key_action_single :: proc(input_map: ^InputMap, key: raylib.KeyboardKey, action: EditorAction, description: string = "") {
register_ctrl_key_action_single :: proc(input_map: ^InputMap, key: plugin.Key, action: EditorAction, description: string = "") {
if ok := key in input_map.key_actions; ok {
// TODO: log that key is already registered
fmt.eprintln("key already registered with single action", key);
@ -154,7 +157,7 @@ register_ctrl_key_action_single :: proc(input_map: ^InputMap, key: raylib.Keyboa
};
}
register_ctrl_key_action_group :: proc(input_map: ^InputMap, key: raylib.KeyboardKey, input_group: InputGroup, description: string = "") {
register_ctrl_key_action_group :: proc(input_map: ^InputMap, key: plugin.Key, input_group: InputGroup, description: string = "") {
if ok := key in input_map.key_actions; ok {
// TODO: log that key is already registered
fmt.eprintln("key already registered with single action", key);

View File

@ -8,7 +8,6 @@ import "core:math"
import "core:slice"
import "core:runtime"
import "core:strings"
import "vendor:raylib"
import "../theme"
import "../plugin"
@ -752,7 +751,7 @@ update_glyph_buffer :: proc(buffer: ^FileBuffer) {
}
}
draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, font: raylib.Font, show_line_numbers: bool = true) {
draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, show_line_numbers: bool = true) {
update_glyph_buffer(buffer);
if highlighter, exists := state.highlighters[buffer.extension]; exists {
highlighter(state.plugin_vtable, buffer);
@ -771,9 +770,11 @@ draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, fon
// draw cursor
if state.mode == .Normal {
raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), i32(state.source_font_width), i32(state.source_font_height), theme.get_palette_raylib_color(.Background4));
draw_rect(state, cursor_x, cursor_y, state.source_font_width, state.source_font_height, .Background4);
//raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), i32(state.source_font_width), i32(state.source_font_height), theme.get_palette_raylib_color(.Background4));
} else if state.mode == .Insert {
raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), i32(state.source_font_width), i32(state.source_font_height), theme.get_palette_raylib_color(.Green));
draw_rect(state, cursor_x, cursor_y, state.source_font_width, state.source_font_height, .Green);
// raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), i32(state.source_font_width), i32(state.source_font_height), theme.get_palette_raylib_color(.Green));
num_line_break := 0;
line_length := 0;
@ -793,14 +794,16 @@ draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, fon
cursor_x += line_length * state.source_font_width;
}
raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), i32(state.source_font_width), i32(state.source_font_height), theme.get_palette_raylib_color(.Blue));
draw_rect(state, cursor_x, cursor_y, state.source_font_width, state.source_font_height, .Blue);
//raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), i32(state.source_font_width), i32(state.source_font_height), theme.get_palette_raylib_color(.Blue));
}
for j in 0..<buffer.glyph_buffer_height {
text_y := y + state.source_font_height * j;
if show_line_numbers {
raylib.DrawTextEx(font, raylib.TextFormat("%d", begin + j + 1), raylib.Vector2 { f32(x), f32(text_y) }, f32(state.source_font_height), 0, theme.get_palette_raylib_color(.Background4));
draw_text(state, fmt.tprintf("%d", begin + j + 1), x, text_y);
//raylib.DrawTextEx(font, raylib.TextFormat("%d", begin + j + 1), raylib.Vector2 { f32(x), f32(text_y) }, f32(state.source_font_height), 0, theme.get_palette_raylib_color(.Background4));
}
for i in 0..<buffer.glyph_buffer_width {
@ -809,7 +812,8 @@ draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, fon
if glyph.codepoint == 0 { break; }
raylib.DrawTextCodepoint(font, rune(glyph.codepoint), raylib.Vector2 { f32(text_x), f32(text_y) }, f32(state.source_font_height), theme.get_palette_raylib_color(glyph.color));
draw_codepoint(state, rune(glyph.codepoint), text_x, text_y, glyph.color);
//raylib.DrawTextCodepoint(font, rune(glyph.codepoint), raylib.Vector2 { f32(text_x), f32(text_y) }, f32(state.source_font_height), theme.get_palette_raylib_color(glyph.color));
}
}
}

157
src/core/gfx.odin Normal file
View File

@ -0,0 +1,157 @@
package core
import "core:fmt"
import "vendor:sdl2"
import "vendor:sdl2/ttf"
import "../theme"
scale :: 2;
start_char :: ' ';
end_char :: '~';
FontAtlas :: struct {
texture: ^sdl2.Texture,
font: ^ttf.Font,
max_width: int,
max_height: int,
}
gen_font_atlas :: proc(state: ^State, path: cstring) -> FontAtlas {
free_font_atlas(state.font_atlas);
font_height := i32(state.source_font_height*scale);
atlas := FontAtlas {
// FIXME: check if this failed
font = ttf.OpenFont(path, font_height),
}
ttf.SetFontStyle(atlas.font, ttf.STYLE_NORMAL);
// NOTE: not sure if I like the look of this or not yet
// ttf.SetFontHinting(atlas.font, ttf.HINTING_MONO);
minx, maxx, miny, maxy: i32;
advanced: i32;
for char in start_char..=end_char {
ttf.GlyphMetrics32(atlas.font, char, &minx, &maxx, &miny, &maxy, &advanced);
width := maxx-minx;
height := maxy+miny;
if width > i32(atlas.max_width) {
atlas.max_width = int(width);
}
if height > i32(atlas.max_height) {
atlas.max_height = int(height);
}
}
font_width := i32(atlas.max_width);
font_height = i32(atlas.max_height);
state.source_font_width = int(font_width/scale);
state.source_font_height = int(font_height/scale);
temp_surface: ^sdl2.Surface;
// FIXME: check if this failed
font_surface := sdl2.CreateRGBSurface(0, font_width * (end_char-start_char + 1), font_height, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
rect: sdl2.Rect;
white := sdl2.Color { 0xff, 0xff, 0xff, 0xff };
for char, index in start_char..=end_char {
rect.x = i32(index) * font_width;
rect.y = 0;
// FIXME: check if this failed
temp_surface = ttf.RenderGlyph32_Blended(atlas.font, char, white);
// NOTE(pcleavelin): holy schmoley batman, it took hours of researching to find out
// that the way to properly blend the texture atlas WAS TO DISABLE BLENDING!
// and of course it's ONE GUY on a forum that has the answer, but it's for some
// reason not even listed on the first page of Google when you search for:
// "sdl_ttf rendering antialiasing"
//
// But the reason why this is needed (I /think/) is because you want to
// directly write all the RGBA data to `font_surface` without it attempting to
// blend a surface that has pure black
sdl2.SetSurfaceBlendMode(temp_surface, .NONE);
src_rect := sdl2.Rect {
0,
0,
temp_surface.w,
temp_surface.h
};
// FIXME: check if this failed
sdl2.BlitSurface(temp_surface, &src_rect, font_surface, &rect);
sdl2.FreeSurface(temp_surface);
}
// FIXME: check if this failed
atlas.texture = sdl2.CreateTextureFromSurface(state.sdl_renderer, font_surface);
sdl2.SetTextureScaleMode(atlas.texture, .Best);
return atlas;
}
free_font_atlas :: proc(font_atlas: FontAtlas) {
if font_atlas.font != nil {
ttf.CloseFont(font_atlas.font);
}
if font_atlas.texture != nil {
sdl2.DestroyTexture(font_atlas.texture);
}
}
draw_rect_outline :: proc(state: ^State, x,y,w,h: int, color: theme.PaletteColor) {
color := theme.get_palette_color(color);
sdl2.SetRenderDrawColor(state.sdl_renderer, color.r, color.g, color.b, color.a);
sdl2.RenderDrawRect(state.sdl_renderer, &sdl2.Rect { i32(x), i32(y), i32(w), i32(h) });
}
draw_rect :: proc(state: ^State, x,y,w,h: int, color: theme.PaletteColor) {
color := theme.get_palette_color(color);
sdl2.SetRenderDrawColor(state.sdl_renderer, color.r, color.g, color.b, color.a);
sdl2.RenderFillRect(state.sdl_renderer, &sdl2.Rect { i32(x), i32(y), i32(w), i32(h) });
}
draw_codepoint :: proc(state: ^State, codepoint: rune, x,y: int, color: theme.PaletteColor) {
color := theme.get_palette_color(color);
if codepoint >= start_char && codepoint <= end_char {
codepoint := codepoint - start_char;
src_rect := sdl2.Rect {
x = i32(codepoint) * i32(state.font_atlas.max_width),
y = 0,
w = i32(state.font_atlas.max_width),
h = i32(state.font_atlas.max_height),
};
dest_rect := sdl2.Rect {
x = i32(x),
y = i32(y),
w = i32(state.font_atlas.max_width/scale),
h = i32(state.font_atlas.max_height/scale),
};
sdl2.SetTextureColorMod(state.font_atlas.texture, color.r, color.g, color.b);
sdl2.RenderCopy(state.sdl_renderer, state.font_atlas.texture, &src_rect, &dest_rect);
}
}
draw_text :: proc(state: ^State, text: string, x,y: int, color: theme.PaletteColor = .Foreground1) {
for char, idx in text {
if char < start_char || char > end_char {
draw_codepoint(state, '?', x + idx * state.source_font_width, y, color);
} else {
draw_codepoint(state, char, x + idx * state.source_font_width, y, color);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,6 @@ package plugin;
import "core:intrinsics"
import "core:dynlib"
import "core:fmt"
import "vendor:raylib"
import "../theme"
@ -85,6 +84,48 @@ Iterator :: struct {
until_end_of_word: rawptr,
}
UiInteraction :: struct {
hovering: bool,
clicked: bool
}
UiAxis :: enum {
Horizontal = 0,
Vertical,
}
UiSemanticSize :: struct {
kind: int,
value: int,
}
UiBox :: rawptr;
UiPushParentProc :: proc "c" (ui_context: rawptr, box: UiBox);
UiPopParentProc :: proc "c" (ui_context: rawptr);
UiFloatingProc :: proc "c" (ui_context: rawptr, label: cstring, pos: [2]int) -> UiBox;
UiCreateBoxProc :: proc "c" (ui_context: rawptr, label: cstring) -> UiBox;
UiRectProc :: proc "c" (ui_context: rawptr, label: cstring, background: bool, border: bool, axis: UiAxis, size: [2]UiSemanticSize) -> UiBox;
UiSimpleProc :: proc "c" (ui_context: rawptr, label: cstring) -> UiInteraction;
UiBufferProc :: proc "c" (ui_context: rawptr, buffer: rawptr, show_line_numbers: bool);
UiBufferIndexProc :: proc "c" (ui_context: rawptr, buffer: int, show_line_numbers: bool);
Ui :: struct {
ui_context: rawptr,
push_parent: UiPushParentProc,
pop_parent: UiPopParentProc,
spacer: UiSimpleProc,
floating: UiFloatingProc,
rect: UiRectProc,
button: UiSimpleProc,
label: UiSimpleProc,
buffer: UiBufferProc,
buffer_from_index: UiBufferIndexProc,
}
OnColorBufferProc :: proc "c" (plugin: Plugin, buffer: rawptr);
InputGroupProc :: proc "c" (plugin: Plugin, input_map: rawptr);
InputActionProc :: proc "c" (plugin: Plugin);
@ -98,6 +139,7 @@ Plugin :: struct {
state: rawptr,
iter: Iterator,
buffer: Buffer,
ui: Ui,
register_hook: proc "c" (hook: Hook, on_hook: OnHookProc),
register_highlighter: proc "c" (extension: cstring, on_color_buffer: OnColorBufferProc),
@ -127,120 +169,246 @@ Hook :: enum {
}
Key :: enum {
KEY_NULL = 0, // Key: NULL, used for no key pressed
// Alphanumeric keys
APOSTROPHE = 39, // Key: '
COMMA = 44, // Key: ,
MINUS = 45, // Key: -
PERIOD = 46, // Key: .
SLASH = 47, // Key: /
ZERO = 48, // Key: 0
ONE = 49, // Key: 1
TWO = 50, // Key: 2
THREE = 51, // Key: 3
FOUR = 52, // Key: 4
FIVE = 53, // Key: 5
SIX = 54, // Key: 6
SEVEN = 55, // Key: 7
EIGHT = 56, // Key: 8
NINE = 57, // Key: 9
SEMICOLON = 59, // Key: ;
EQUAL = 61, // Key: =
A = 65, // Key: A | a
B = 66, // Key: B | b
C = 67, // Key: C | c
D = 68, // Key: D | d
E = 69, // Key: E | e
F = 70, // Key: F | f
G = 71, // Key: G | g
H = 72, // Key: H | h
I = 73, // Key: I | i
J = 74, // Key: J | j
K = 75, // Key: K | k
L = 76, // Key: L | l
M = 77, // Key: M | m
N = 78, // Key: N | n
O = 79, // Key: O | o
P = 80, // Key: P | p
Q = 81, // Key: Q | q
R = 82, // Key: R | r
S = 83, // Key: S | s
T = 84, // Key: T | t
U = 85, // Key: U | u
V = 86, // Key: V | v
W = 87, // Key: W | w
X = 88, // Key: X | x
Y = 89, // Key: Y | y
Z = 90, // Key: Z | z
LEFT_BRACKET = 91, // Key: [
BACKSLASH = 92, // Key: '\'
RIGHT_BRACKET = 93, // Key: ]
GRAVE = 96, // Key: `
// Function keys
SPACE = 32, // Key: Space
ESCAPE = 256, // Key: Esc
ENTER = 257, // Key: Enter
TAB = 258, // Key: Tab
BACKSPACE = 259, // Key: Backspace
INSERT = 260, // Key: Ins
DELETE = 261, // Key: Del
RIGHT = 262, // Key: Cursor right
LEFT = 263, // Key: Cursor left
DOWN = 264, // Key: Cursor down
UP = 265, // Key: Cursor up
PAGE_UP = 266, // Key: Page up
PAGE_DOWN = 267, // Key: Page down
HOME = 268, // Key: Home
END = 269, // Key: End
CAPS_LOCK = 280, // Key: Caps lock
SCROLL_LOCK = 281, // Key: Scroll down
NUM_LOCK = 282, // Key: Num lock
PRINT_SCREEN = 283, // Key: Print screen
PAUSE = 284, // Key: Pause
F1 = 290, // Key: F1
F2 = 291, // Key: F2
F3 = 292, // Key: F3
F4 = 293, // Key: F4
F5 = 294, // Key: F5
F6 = 295, // Key: F6
F7 = 296, // Key: F7
F8 = 297, // Key: F8
F9 = 298, // Key: F9
F10 = 299, // Key: F10
F11 = 300, // Key: F11
F12 = 301, // Key: F12
LEFT_SHIFT = 340, // Key: Shift left
LEFT_CONTROL = 341, // Key: Control left
LEFT_ALT = 342, // Key: Alt left
LEFT_SUPER = 343, // Key: Super left
RIGHT_SHIFT = 344, // Key: Shift right
RIGHT_CONTROL = 345, // Key: Control right
RIGHT_ALT = 346, // Key: Alt right
RIGHT_SUPER = 347, // Key: Super right
KB_MENU = 348, // Key: KB menu
// Keypad keys
KP_0 = 320, // Key: Keypad 0
KP_1 = 321, // Key: Keypad 1
KP_2 = 322, // Key: Keypad 2
KP_3 = 323, // Key: Keypad 3
KP_4 = 324, // Key: Keypad 4
KP_5 = 325, // Key: Keypad 5
KP_6 = 326, // Key: Keypad 6
KP_7 = 327, // Key: Keypad 7
KP_8 = 328, // Key: Keypad 8
KP_9 = 329, // Key: Keypad 9
KP_DECIMAL = 330, // Key: Keypad .
KP_DIVIDE = 331, // Key: Keypad /
KP_MULTIPLY = 332, // Key: Keypad *
KP_SUBTRACT = 333, // Key: Keypad -
KP_ADD = 334, // Key: Keypad +
KP_ENTER = 335, // Key: Keypad Enter
KP_EQUAL = 336, // Key: Keypad =
// Android key buttons
BACK = 4, // Key: Android back button
MENU = 82, // Key: Android menu button
VOLUME_UP = 24, // Key: Android volume up button
VOLUME_DOWN = 25, // Key: Android volume down button
UNKNOWN = 0,
ENTER = 13,
ESCAPE = 27,
BACKSPACE = 8,
TAB = 9,
SPACE = 32,
EXCLAIM = 33,
QUOTEDBL = 34,
HASH = 35,
PERCENT = 37,
DOLLAR = 36,
AMPERSAND = 38,
QUOTE = 39,
LEFTPAREN = 40,
RIGHTPAREN = 41,
ASTERISK = 42,
PLUS = 43,
COMMA = 44,
MINUS = 45,
PERIOD = 46,
SLASH = 47,
NUM0 = 48,
NUM1 = 49,
NUM2 = 50,
NUM3 = 51,
NUM4 = 52,
NUM5 = 53,
NUM6 = 54,
NUM7 = 55,
NUM8 = 56,
NUM9 = 57,
COLON = 58,
SEMICOLON = 59,
LESS = 60,
EQUAL = 61,
GREATER = 62,
QUESTION = 63,
AT = 64,
LEFTBRACKET = 91,
BACKSLASH = 92,
RIGHTBRACKET = 93,
CARET = 94,
UNDERSCORE = 95,
BACKQUOTE = 96,
A = 97,
B = 98,
C = 99,
D = 100,
E = 101,
F = 102,
G = 103,
H = 104,
I = 105,
J = 106,
K = 107,
L = 108,
M = 109,
N = 110,
O = 111,
P = 112,
Q = 113,
R = 114,
S = 115,
T = 116,
U = 117,
V = 118,
W = 119,
X = 120,
Y = 121,
Z = 122,
CAPSLOCK = 1073741881,
F1 = 1073741882,
F2 = 1073741883,
F3 = 1073741884,
F4 = 1073741885,
F5 = 1073741886,
F6 = 1073741887,
F7 = 1073741888,
F8 = 1073741889,
F9 = 1073741890,
F10 = 1073741891,
F11 = 1073741892,
F12 = 1073741893,
PRINTSCREEN = 1073741894,
SCROLLLOCK = 1073741895,
PAUSE = 1073741896,
INSERT = 1073741897,
HOME = 1073741898,
PAGEUP = 1073741899,
DELETE = 127,
END = 1073741901,
PAGEDOWN = 1073741902,
RIGHT = 1073741903,
LEFT = 1073741904,
DOWN = 1073741905,
UP = 1073741906,
NUMLOCKCLEAR = 1073741907,
KP_DIVIDE = 1073741908,
KP_MULTIPLY = 1073741909,
KP_MINUS = 1073741910,
KP_PLUS = 1073741911,
KP_ENTER = 1073741912,
KP_1 = 1073741913,
KP_2 = 1073741914,
KP_3 = 1073741915,
KP_4 = 1073741916,
KP_5 = 1073741917,
KP_6 = 1073741918,
KP_7 = 1073741919,
KP_8 = 1073741920,
KP_9 = 1073741921,
KP_0 = 1073741922,
KP_PERIOD = 1073741923,
APPLICATION = 1073741925,
POWER = 1073741926,
KP_EQUALS = 1073741927,
F13 = 1073741928,
F14 = 1073741929,
F15 = 1073741930,
F16 = 1073741931,
F17 = 1073741932,
F18 = 1073741933,
F19 = 1073741934,
F20 = 1073741935,
F21 = 1073741936,
F22 = 1073741937,
F23 = 1073741938,
F24 = 1073741939,
EXECUTE = 1073741940,
HELP = 1073741941,
MENU = 1073741942,
SELECT = 1073741943,
STOP = 1073741944,
AGAIN = 1073741945,
UNDO = 1073741946,
CUT = 1073741947,
COPY = 1073741948,
PASTE = 1073741949,
FIND = 1073741950,
MUTE = 1073741951,
VOLUMEUP = 1073741952,
VOLUMEDOWN = 1073741953,
KP_COMMA = 1073741957,
KP_EQUALSAS400 = 1073741958,
ALTERASE = 1073741977,
SYSREQ = 1073741978,
CANCEL = 1073741979,
CLEAR = 1073741980,
PRIOR = 1073741981,
RETURN2 = 1073741982,
SEPARATOR = 1073741983,
OUT = 1073741984,
OPER = 1073741985,
CLEARAGAIN = 1073741986,
CRSEL = 1073741987,
EXSEL = 1073741988,
KP_00 = 1073742000,
KP_000 = 1073742001,
THOUSANDSSEPARATOR = 1073742002,
DECIMALSEPARATOR = 1073742003,
CURRENCYUNIT = 1073742004,
CURRENCYSUBUNIT = 1073742005,
KP_LEFTPAREN = 1073742006,
KP_RIGHTPAREN = 1073742007,
KP_LEFTBRACE = 1073742008,
KP_RIGHTBRACE = 1073742009,
KP_TAB = 1073742010,
KP_BACKSPACE = 1073742011,
KP_A = 1073742012,
KP_B = 1073742013,
KP_C = 1073742014,
KP_D = 1073742015,
KP_E = 1073742016,
KP_F = 1073742017,
KP_XOR = 1073742018,
KP_POWER = 1073742019,
KP_PERCENT = 1073742020,
KP_LESS = 1073742021,
KP_GREATER = 1073742022,
KP_AMPERSAND = 1073742023,
KP_DBLAMPERSAND = 1073742024,
KP_VERTICALBAR = 1073742025,
KP_DBLVERTICALBAR = 1073742026,
KP_COLON = 1073742027,
KP_HASH = 1073742028,
KP_SPACE = 1073742029,
KP_AT = 1073742030,
KP_EXCLAM = 1073742031,
KP_MEMSTORE = 1073742032,
KP_MEMRECALL = 1073742033,
KP_MEMCLEAR = 1073742034,
KP_MEMADD = 1073742035,
KP_MEMSUBTRACT = 1073742036,
KP_MEMMULTIPLY = 1073742037,
KP_MEMDIVIDE = 1073742038,
KP_PLUSMINUS = 1073742039,
KP_CLEAR = 1073742040,
KP_CLEARENTRY = 1073742041,
KP_BINARY = 1073742042,
KP_OCTAL = 1073742043,
KP_DECIMAL = 1073742044,
KP_HEXADECIMAL = 1073742045,
LCTRL = 1073742048,
LSHIFT = 1073742049,
LALT = 1073742050,
LGUI = 1073742051,
RCTRL = 1073742052,
RSHIFT = 1073742053,
RALT = 1073742054,
RGUI = 1073742055,
MODE = 1073742081,
AUDIONEXT = 1073742082,
AUDIOPREV = 1073742083,
AUDIOSTOP = 1073742084,
AUDIOPLAY = 1073742085,
AUDIOMUTE = 1073742086,
MEDIASELECT = 1073742087,
WWW = 1073742088,
MAIL = 1073742089,
CALCULATOR = 1073742090,
COMPUTER = 1073742091,
AC_SEARCH = 1073742092,
AC_HOME = 1073742093,
AC_BACK = 1073742094,
AC_FORWARD = 1073742095,
AC_STOP = 1073742096,
AC_REFRESH = 1073742097,
AC_BOOKMARKS = 1073742098,
BRIGHTNESSDOWN = 1073742099,
BRIGHTNESSUP = 1073742100,
DISPLAYSWITCH = 1073742101,
KBDILLUMTOGGLE = 1073742102,
KBDILLUMDOWN = 1073742103,
KBDILLUMUP = 1073742104,
EJECT = 1073742105,
SLEEP = 1073742106,
APP1 = 1073742107,
APP2 = 1073742108,
AUDIOREWIND = 1073742109,
AUDIOFASTFORWARD = 1073742110,
}

View File

@ -1,7 +1,5 @@
package theme
import "vendor:raylib"
PaletteColor :: enum {
Background,
Foreground,
@ -97,7 +95,14 @@ light_palette := []u32 {
0x928374ff,
};
get_palette_raylib_color :: proc(palette_color: PaletteColor) -> raylib.Color {
return raylib.GetColor(palette[palette_color]);
get_palette_color :: proc(palette_color: PaletteColor) -> [4]u8 {
color: [4]u8;
c := palette[palette_color];
for i in 0..<4 {
color[i] = u8((c >> (8*u32(3-i)))&0xff);
}
return color;
}

View File

@ -3,16 +3,29 @@ package ui
import "core:fmt"
import "core:strings"
import "core:math"
import "vendor:raylib"
import "vendor:sdl2"
import "../core"
import "../theme"
root: ^Box = nil;
current_parent: ^Box = nil;
persistent: map[Key]^Box = nil;
current_interaction_index: int = 0;
Context :: struct {
root: ^Box,
current_parent: ^Box,
persistent: map[Key]^Box,
current_interaction_index: int,
clips: [dynamic]Rect = nil;
clips: [dynamic]Rect,
renderer: ^sdl2.Renderer,
mouse_x: int,
mouse_y: int,
mouse_left_down: bool,
last_mouse_left_down: bool,
mouse_right_down: bool,
last_mouse_right_down: bool,
}
Rect :: struct {
pos: [2]int,
@ -25,6 +38,7 @@ Key :: struct {
}
Interaction :: struct {
hovering: bool,
clicked: bool,
}
@ -35,10 +49,12 @@ Flag :: enum {
DrawText,
DrawBorder,
DrawBackground,
Floating,
CustomDrawFunc,
}
SemanticSizeKind :: enum {
FitText,
FitText = 0,
Exact,
ChildrenSum,
Fill,
@ -55,6 +71,7 @@ Axis :: enum {
Vertical = 1,
}
CustomDrawFunc :: proc(state: ^core.State, box: ^Box, user_data: rawptr);
Box :: struct {
first: ^Box,
last: ^Box,
@ -72,30 +89,36 @@ Box :: struct {
axis: Axis,
semantic_size: [2]SemanticSize,
computed_size: [2]int,
computed_pos: [2]int,
computed_pos: [2]int
hot: int,
active: int,
custom_draw_func: CustomDrawFunc,
user_data: rawptr,
}
init :: proc() {
if persistent == nil {
persistent = make(map[Key]^Box);
}
init :: proc(renderer: ^sdl2.Renderer) -> Context {
root := new(Box);
root.key = gen_key(nil, "root", 69);
if clips == nil {
clips = make([dynamic]Rect);
}
root = new(Box);
root.key = gen_key("root", 69);
current_parent = root;
return Context {
root = root,
current_parent = root,
persistent = make(map[Key]^Box),
clips = make([dynamic]Rect),
renderer = renderer,
};
}
gen_key :: proc(label: string, value: int) -> Key {
gen_key :: proc(ctx: ^Context, label: string, value: int) -> Key {
key_label := ""
if current_parent == nil || len(current_parent.key.label) < 1 {
if ctx != nil && (ctx.current_parent == nil || len(ctx.current_parent.key.label) < 1) {
key_label = strings.clone(label);
} else if ctx != nil {
key_label = fmt.aprintf("%s:%s", ctx.current_parent.key.label, label);
} else {
key_label = fmt.aprintf("%s:%s", current_parent.key.label, label);
key_label = fmt.aprintf("%s",label);
}
return Key {
@ -104,22 +127,23 @@ gen_key :: proc(label: string, value: int) -> Key {
};
}
make_box :: proc(key: Key, label: string, flags: bit_set[Flag], axis: Axis, semantic_size: [2]SemanticSize) -> ^Box {
@(private)
make_box :: proc(ctx: ^Context, key: Key, label: string, flags: bit_set[Flag], axis: Axis, semantic_size: [2]SemanticSize) -> ^Box {
box: ^Box = nil;
if cached_box, exists := persistent[key]; exists {
if cached_box.last_interacted_index < current_interaction_index {
old_cached_box := persistent[key];
if cached_box, exists := ctx.persistent[key]; exists {
if cached_box.last_interacted_index < ctx.current_interaction_index {
old_cached_box := ctx.persistent[key];
free(old_cached_box);
box = new(Box);
persistent[key] = box;
ctx.persistent[key] = box;
} else {
box = cached_box;
}
} else {
box = new(Box);
persistent[key] = box;
ctx.persistent[key] = box;
}
box.key = key;
@ -128,22 +152,20 @@ make_box :: proc(key: Key, label: string, flags: bit_set[Flag], axis: Axis, sema
box.first = nil;
box.last = nil;
box.next = nil;
box.prev = current_parent.last;
box.parent = current_parent;
box.prev = ctx.current_parent.last;
box.parent = ctx.current_parent;
box.flags = flags;
box.axis = axis;
box.semantic_size = semantic_size;
box.computed_pos = {};
box.computed_size = {};
if current_parent.last != nil {
current_parent.last.next = box;
if ctx.current_parent.last != nil {
ctx.current_parent.last.next = box;
}
if current_parent.first == nil {
current_parent.first = box;
if ctx.current_parent.first == nil {
ctx.current_parent.first = box;
}
current_parent.last = box;
ctx.current_parent.last = box;
return box;
}
@ -173,67 +195,99 @@ ChildrenSum :[2]SemanticSize: {
}
};
push_box :: proc(label: string, flags: bit_set[Flag], axis: Axis = .Horizontal, semantic_size: [2]SemanticSize = FitText, value: int = 0) -> ^Box {
key := gen_key(label, value);
box := make_box(key, label, flags, axis, semantic_size);
Fill :[2]SemanticSize: {
SemanticSize {
kind = .Fill,
},
SemanticSize {
kind = .Fill,
}
};
push_box :: proc(ctx: ^Context, label: string, flags: bit_set[Flag], axis: Axis = .Horizontal, semantic_size: [2]SemanticSize = FitText, value: int = 0) -> ^Box {
key := gen_key(ctx, label, value);
box := make_box(ctx, key, label, flags, axis, semantic_size);
return box;
}
push_parent :: proc(box: ^Box) {
current_parent = box;
push_parent :: proc(ctx: ^Context, box: ^Box) {
ctx.current_parent = box;
}
pop_parent :: proc() {
if current_parent.parent != nil {
current_parent = current_parent.parent;
pop_parent :: proc(ctx: ^Context) {
if ctx.current_parent.parent != nil {
ctx.current_parent = ctx.current_parent.parent;
}
}
test_box :: proc(box: ^Box) -> Interaction {
test_box :: proc(ctx: ^Context, box: ^Box) -> Interaction {
hovering: bool;
mouse_is_clicked := !ctx.last_mouse_left_down && ctx.mouse_left_down;
if ctx.mouse_x >= box.computed_pos.x && ctx.mouse_x <= box.computed_pos.x + box.computed_size.x &&
ctx.mouse_y >= box.computed_pos.y && ctx.mouse_y <= box.computed_pos.y + box.computed_size.y
{
hovering = true;
}
if hovering {
box.hot += 1;
} else {
box.hot = 0;
}
return Interaction {
clicked = false,
hovering = hovering,
clicked = hovering && mouse_is_clicked,
};
}
delete_box_children :: proc(box: ^Box, keep_persistent: bool = true) {
delete_box_children :: proc(ctx: ^Context, box: ^Box, keep_persistent: bool = true) {
iter := BoxIter { box.first, 0 };
for box in iterate_box(&iter) {
delete_box(box, keep_persistent);
delete_box(ctx, box, keep_persistent);
}
}
delete_box :: proc(box: ^Box, keep_persistent: bool = true) {
delete_box_children(box, keep_persistent);
delete_box :: proc(ctx: ^Context, box: ^Box, keep_persistent: bool = true) {
delete_box_children(ctx, box, keep_persistent);
if !(box.key in persistent) || !keep_persistent {
if !(box.key in ctx.persistent) || !keep_persistent {
delete(box.key.label);
free(box);
}
}
prune :: proc() {
iter := BoxIter { root.first, 0 };
prune :: proc(ctx: ^Context) {
iter := BoxIter { ctx.root.first, 0 };
for box in iterate_box(&iter) {
delete_box_children(box);
delete_box_children(ctx, box);
if !(box.key in persistent) {
if !(box.key in ctx.persistent) {
free(box);
}
}
root_key := root.key;
root^ = {
key = root_key,
};
current_parent = root;
computed_pos := ctx.root.computed_pos;
computed_size := ctx.root.computed_size;
root_key := ctx.root.key;
ctx.root.first = nil;
ctx.root.last = nil;
ctx.root.next = nil;
ctx.root.prev = nil;
ctx.root.parent = nil;
ctx.current_parent = ctx.root;
}
ancestor_size :: proc(box: ^Box, axis: Axis) -> int {
if box == nil || box.parent == nil {
return root.computed_size[axis];
// TODO: consider not using `ctx` here
ancestor_size :: proc(ctx: ^Context, box: ^Box, axis: Axis) -> int {
if box == nil || box.parent == nil || .Floating in box.flags {
return ctx.root.computed_size[axis];
}
switch box.parent.semantic_size[axis].kind {
@ -244,27 +298,46 @@ ancestor_size :: proc(box: ^Box, axis: Axis) -> int {
return box.parent.computed_size[axis];
case .ChildrenSum:
return ancestor_size(box.parent, axis);
return ancestor_size(ctx, box.parent, axis);
}
return 1337;
}
compute_layout :: proc(canvas_size: [2]int, font_width: int, font_height: int, box: ^Box = root) {
prev_non_floating_sibling :: proc(ctx: ^Context, box: ^Box) -> ^Box {
if box == nil {
return nil;
} else if box.prev == nil {
return nil;
} else if !(.Floating in box.prev.flags) {
return box.prev;
} else {
return prev_non_floating_sibling(ctx, box.prev);
}
}
compute_layout :: proc(ctx: ^Context, canvas_size: [2]int, font_width: int, font_height: int, box: ^Box) {
if box == nil { return; }
axis := Axis.Horizontal;
if box.parent != nil {
if box.parent != nil && !(.Floating in box.flags) {
axis = box.parent.axis;
box.computed_pos = box.parent.computed_pos;
}
if box.prev != nil {
box.computed_pos[axis] = box.prev.computed_pos[axis] + box.prev.computed_size[axis];
if .Floating in box.flags {
// box.computed_pos = {0,0};
} else if box.prev != nil {
prev := prev_non_floating_sibling(ctx, box);
if prev != nil {
box.computed_pos[axis] = prev.computed_pos[axis] + prev.computed_size[axis];
}
}
post_compute_size := [2]bool { false, false };
compute_children := true;
if box == root {
if box == ctx.root {
box.computed_size = canvas_size;
} else {
switch box.semantic_size.x.kind {
@ -276,29 +349,30 @@ compute_layout :: proc(canvas_size: [2]int, font_width: int, font_height: int, b
box.computed_size.x = box.semantic_size.x.value;
}
case .ChildrenSum: {
compute_children = false;
box.computed_size.x = 0;
//compute_children = false;
post_compute_size[int(Axis.Horizontal)] = true;
// box.computed_size.x = 0;
iter := BoxIter { box.first, 0 };
for child in iterate_box(&iter) {
compute_layout(canvas_size, font_width, font_height, child);
// iter := BoxIter { box.first, 0 };
// for child in iterate_box(&iter) {
// compute_layout(canvas_size, font_width, font_height, child);
switch box.axis {
case .Horizontal: {
box.computed_size.x += child.computed_size.x;
}
case .Vertical: {
if child.computed_size.x > box.computed_size.x {
box.computed_size.x = child.computed_size.x;
}
}
}
}
// switch box.axis {
// case .Horizontal: {
// box.computed_size.x += child.computed_size.x;
// }
// case .Vertical: {
// if child.computed_size.x > box.computed_size.x {
// box.computed_size.x = child.computed_size.x;
// }
// }
// }
// }
}
case .Fill: {
}
case .PercentOfParent: {
box.computed_size.x = int(f32(ancestor_size(box, .Horizontal))*(f32(box.semantic_size.x.value)/100.0));
box.computed_size.x = int(f32(ancestor_size(ctx, box, .Horizontal))*(f32(box.semantic_size.x.value)/100.0));
}
}
switch box.semantic_size.y.kind {
@ -310,32 +384,34 @@ compute_layout :: proc(canvas_size: [2]int, font_width: int, font_height: int, b
box.computed_size.y = box.semantic_size.y.value;
}
case .ChildrenSum: {
compute_children = false;
should_post_compute := false;
number_of_fills := 0;
box.computed_size.y = 0;
parent_size := ancestor_size(box, .Vertical);
//compute_children = false;
post_compute_size[Axis.Vertical] = true;
iter := BoxIter { box.first, 0 };
for child in iterate_box(&iter) {
compute_layout(canvas_size, font_width, font_height, child);
// should_post_compute := false;
// number_of_fills := 0;
// box.computed_size.y = 0;
// parent_size := ancestor_size(box, .Vertical);
if child.semantic_size.y.kind == .Fill {
number_of_fills += 1;
should_post_compute := true;
}
// iter := BoxIter { box.first, 0 };
// for child in iterate_box(&iter) {
// compute_layout(canvas_size, font_width, font_height, child);
switch box.axis {
case .Horizontal: {
if child.computed_size.y > box.computed_size.y {
box.computed_size.y = child.computed_size.y;
}
}
case .Vertical: {
box.computed_size.y += child.computed_size.y;
}
}
}
// if child.semantic_size.y.kind == .Fill {
// number_of_fills += 1;
// should_post_compute := true;
// }
// switch box.axis {
// case .Horizontal: {
// if child.computed_size.y > box.computed_size.y {
// box.computed_size.y = child.computed_size.y;
// }
// }
// case .Vertical: {
// box.computed_size.y += child.computed_size.y;
// }
// }
// }
// if should_post_compute {
// iter := BoxIter { box.first, 0 };
@ -349,7 +425,7 @@ compute_layout :: proc(canvas_size: [2]int, font_width: int, font_height: int, b
case .Fill: {
}
case .PercentOfParent: {
box.computed_size.y = int(f32(ancestor_size(box, .Vertical))*(f32(box.semantic_size.y.value)/100.0));
box.computed_size.y = int(f32(ancestor_size(ctx, box, .Vertical))*(f32(box.semantic_size.y.value)/100.0));
}
}
}
@ -369,7 +445,9 @@ compute_layout :: proc(canvas_size: [2]int, font_width: int, font_height: int, b
our_size := box.computed_size;
for child in iterate_box(&iter) {
compute_layout(canvas_size, font_width, font_height, child);
if .Floating in child.flags { continue; }
compute_layout(ctx, canvas_size, font_width, font_height, child);
if child.semantic_size[box.axis].kind == .Fill {
number_of_fills[box.axis] += 1;
should_post_compute = true;
@ -378,12 +456,12 @@ compute_layout :: proc(canvas_size: [2]int, font_width: int, font_height: int, b
}
}
if should_post_compute {
if true || should_post_compute {
iter := BoxIter { box.first, 0 };
for child in iterate_box(&iter) {
for axis in 0..<2 {
if child.semantic_size[axis].kind == .Fill {
if child_size[axis] >= our_size[axis] {
if false && child_size[axis] >= our_size[axis] {
child.computed_size[axis] = our_size[axis] / number_of_fills[axis];
} else {
child.computed_size[axis] = (our_size[axis] - child_size[axis]) / number_of_fills[axis];
@ -391,17 +469,56 @@ compute_layout :: proc(canvas_size: [2]int, font_width: int, font_height: int, b
}
}
compute_layout(canvas_size, font_width, font_height, child);
compute_layout(ctx, canvas_size, font_width, font_height, child);
if child.label == "2" {
fmt.println(child.label, child.computed_size, box.label, our_size, child_size, number_of_fills);
}
}
}
}
if post_compute_size[Axis.Horizontal] {
box.computed_size[Axis.Horizontal] = 0;
iter := BoxIter { box.first, 0 };
for child in iterate_box(&iter) {
switch box.axis {
case .Horizontal: {
box.computed_size[Axis.Horizontal] += child.computed_size[Axis.Horizontal];
}
case .Vertical: {
if child.computed_size[Axis.Horizontal] > box.computed_size[Axis.Horizontal] {
box.computed_size[Axis.Horizontal] = child.computed_size[Axis.Horizontal];
}
}
}
}
}
if post_compute_size[Axis.Vertical] {
box.computed_size[Axis.Vertical] = 0;
iter := BoxIter { box.first, 0 };
for child in iterate_box(&iter) {
switch box.axis {
case .Horizontal: {
if child.computed_size[Axis.Vertical] > box.computed_size[Axis.Vertical] {
box.computed_size[Axis.Vertical] = child.computed_size[Axis.Vertical];
}
}
case .Vertical: {
box.computed_size[Axis.Vertical] += child.computed_size[Axis.Vertical];
}
}
}
}
}
push_clip :: proc(pos: [2]int, size: [2]int) {
push_clip :: proc(ctx: ^Context, pos: [2]int, size: [2]int) {
rect := Rect { pos, size };
if len(clips) > 0 {
parent_rect := clips[len(clips)-1];
if len(ctx.clips) > 0 {
parent_rect := ctx.clips[len(ctx.clips)-1];
if rect.pos.x >= parent_rect.pos.x &&
rect.pos.y >= parent_rect.pos.y &&
@ -421,72 +538,96 @@ push_clip :: proc(pos: [2]int, size: [2]int) {
}
}
raylib.BeginScissorMode(
sdl2.RenderSetClipRect(ctx.renderer, &sdl2.Rect {
i32(rect.pos.x),
i32(rect.pos.y),
i32(rect.size.x),
i32(rect.size.y)
);
});
append(&clips, rect);
// raylib.BeginScissorMode(
// i32(rect.pos.x),
// i32(rect.pos.y),
// i32(rect.size.x),
// i32(rect.size.y)
// );
append(&ctx.clips, rect);
}
pop_clip :: proc() {
raylib.EndScissorMode();
pop_clip :: proc(ctx: ^Context) {
//raylib.EndScissorMode();
if len(clips) > 0 {
rect := pop(&clips);
if len(ctx.clips) > 0 {
rect := pop(&ctx.clips);
raylib.BeginScissorMode(
sdl2.RenderSetClipRect(ctx.renderer, &sdl2.Rect {
i32(rect.pos.x),
i32(rect.pos.y),
i32(rect.size.x),
i32(rect.size.y)
);
});
// raylib.BeginScissorMode(
// i32(rect.pos.x),
// i32(rect.pos.y),
// i32(rect.size.x),
// i32(rect.size.y)
// );
} else {
sdl2.RenderSetClipRect(ctx.renderer, nil);
}
}
draw :: proc(font: raylib.Font, font_width: int, font_height: int, box: ^Box = root) {
draw :: proc(ctx: ^Context, state: ^core.State, font_width: int, font_height: int, box: ^Box) {
if box == nil { return; }
// NOTE: for some reason if you place this right before the
// for loop, the clipping only works for the first child. Compiler bug?
push_clip(box.computed_pos, box.computed_size);
defer pop_clip();
push_clip(ctx, box.computed_pos, box.computed_size);
defer pop_clip(ctx);
if .DrawBorder in box.flags {
raylib.DrawRectangleLines(
i32(box.computed_pos.x),
i32(box.computed_pos.y),
i32(box.computed_size.x),
i32(box.computed_size.y),
theme.get_palette_raylib_color(.Background4)
if .Hoverable in box.flags && box.hot > 0 {
core.draw_rect(
state,
box.computed_pos.x,
box.computed_pos.y,
box.computed_size.x,
box.computed_size.y,
.Background2
);
}
if .DrawBackground in box.flags {
raylib.DrawRectangle(
i32(box.computed_pos.x),
i32(box.computed_pos.y),
i32(box.computed_size.x),
i32(box.computed_size.y),
theme.get_palette_raylib_color(.Background1)
else if .DrawBackground in box.flags {
core.draw_rect(
state,
box.computed_pos.x,
box.computed_pos.y,
box.computed_size.x,
box.computed_size.y,
.Background1
);
}
if .DrawBorder in box.flags {
core.draw_rect_outline(
state,
box.computed_pos.x,
box.computed_pos.y,
box.computed_size.x,
box.computed_size.y,
.Background4
);
}
if .DrawText in box.flags {
for codepoint, index in box.label {
raylib.DrawTextCodepoint(
font,
rune(codepoint),
raylib.Vector2 { f32(box.computed_pos.x + index * font_width), f32(box.computed_pos.y) },
f32(font_height),
theme.get_palette_raylib_color(.Foreground1)
);
core.draw_text(state, box.label, box.computed_pos.x, box.computed_pos.y);
}
if .CustomDrawFunc in box.flags && box.custom_draw_func != nil {
box.custom_draw_func(state, box, box.user_data);
}
iter := BoxIter { box.first, 0 };
for child in iterate_box(&iter) {
draw(font, font_width, font_height, child);
draw(ctx, state, font_width, font_height, child);
}
}
@ -509,7 +650,7 @@ iterate_box :: proc(iter: ^BoxIter, print: bool = false) -> (box: ^Box, idx: int
return box, iter.index, true;
}
debug_print :: proc(box: ^Box, depth: int = 0) {
debug_print :: proc(ctx: ^Context, box: ^Box, depth: int = 0) {
iter := BoxIter { box.first, 0 };
for box, idx in iterate_box(&iter, true) {
@ -520,50 +661,82 @@ debug_print :: proc(box: ^Box, depth: int = 0) {
fmt.print(">");
}
fmt.println(idx, "Box", box.label, "#", box.key.label, "first", transmute(rawptr)box.first, "parent", transmute(rawptr)box.parent, box.computed_size);
debug_print(box, depth+1);
debug_print(ctx, box, depth+1);
}
if depth == 0 {
fmt.println("persistent");
for p in persistent {
for p in ctx.persistent {
fmt.println(p);
}
}
}
spacer :: proc(label: string) -> ^Box {
return push_box(label, {}, semantic_size = {make_semantic_size(.Fill, 0), make_semantic_size(.Fill, 0)});
spacer :: proc(ctx: ^Context, label: string, flags: bit_set[Flag] = {}, semantic_size: [2]SemanticSize = {{.Fill, 0}, {.Fill,0}}) -> Interaction {
box := push_box(ctx, label, flags, semantic_size = semantic_size);
return test_box(ctx, box);
}
button :: proc(label: string) -> Interaction {
box := push_box(label, {.Clickable, .Hoverable, .DrawText, .DrawBorder, .DrawBackground});
push_floating :: proc(ctx: ^Context, label: string, pos: [2]int, flags: bit_set[Flag] = {.Floating}, axis: Axis = .Horizontal, semantic_size: [2]SemanticSize = Fill) -> ^Box {
box := push_box(ctx, label, flags, semantic_size = semantic_size);
box.computed_pos = pos;
return test_box(box);
return box;
}
two_buttons_test :: proc(label1: string, label2: string) {
push_parent(push_box("two_button_container", {.DrawBorder}, .Vertical, semantic_size = ChildrenSum));
push_rect :: proc(ctx: ^Context, label: string, background: bool = true, border: bool = true, axis: Axis = .Vertical, semantic_size: [2]SemanticSize = Fill) -> ^Box {
return push_box(ctx, label, {.DrawBackground if background else nil, .DrawBorder if border else nil}, axis, semantic_size = semantic_size);
}
button("1");
button("2");
button(label1);
button(label2);
button("5");
button("6");
label :: proc(ctx: ^Context, label: string) -> Interaction {
box := push_box(ctx, label, {.DrawText});
return test_box(ctx, box);
}
button :: proc(ctx: ^Context, label: string) -> Interaction {
box := push_box(ctx, label, {.Clickable, .Hoverable, .DrawText, .DrawBorder, .DrawBackground});
return test_box(ctx, box);
}
custom :: proc(ctx: ^Context, label: string, draw_func: CustomDrawFunc, user_data: rawptr) -> Interaction {
box := push_box(ctx, label, {.DrawBorder, .CustomDrawFunc}, semantic_size = { make_semantic_size(.Fill), make_semantic_size(.Fill) });
box.custom_draw_func = draw_func;
box.user_data = user_data;
return test_box(ctx, box);
}
two_buttons_test :: proc(ctx: ^Context, label1: string, label2: string) {
push_parent(ctx, push_box(ctx, "TWO BUTTONS TEST", {.DrawBorder}, .Vertical, semantic_size = {make_semantic_size(.PercentOfParent, 100), { .Fill, 256}}));
button(ctx, "Row 1");
button(ctx, "Row 2");
button(ctx, label1);
button(ctx, label2);
button(ctx, "Row 5");
button(ctx, "Row 6");
push_parent(push_box("two_button_container_inner", {.DrawBorder}, semantic_size = ChildrenSum));
button("second first button");
{
push_parent(push_box("two_button_container_inner", {.DrawBorder}, .Vertical, semantic_size = {make_semantic_size(.PercentOfParent, 50), { .Exact, 256}}));
defer pop_parent();
push_parent(ctx, push_box(ctx, "two_button_container_inner", {.DrawBorder}, semantic_size = {make_semantic_size(.Fill, 0), { .Fill, 64}}));
defer pop_parent(ctx);
button("first inner most button");
button("inner_button2");
button("inner_button3");
push_box(ctx, "1", {.DrawText, .DrawBackground, .DrawBorder}, semantic_size = {make_semantic_size(.Fill, 100), { .FitText, 256}})
push_box(ctx, "2", {.DrawText, .DrawBackground, .DrawBorder}, semantic_size = {make_semantic_size(.Fill, 100), { .FitText, 256}})
{
push_parent(ctx, push_box(ctx, "two_button_container_inner_inner", {.DrawBorder}, .Vertical, semantic_size = {make_semantic_size(.Fill, 50), { .ChildrenSum, 256}}));
defer pop_parent(ctx);
button(ctx, "this is a test button");
button(ctx, "me in the middle");
button(ctx, "look at me, I'm a test button too");
}
button("inner_button3");
pop_parent();
button("Help me I'm falling");
pop_parent();
push_box(ctx, "End", {.DrawBorder, .DrawBackground, .DrawText}, .Horizontal, semantic_size = {make_semantic_size(.Fill, 0), { .FitText, 0}})
}
button(ctx, "Help me I'm falling");
pop_parent(ctx);
}

View File

@ -1,152 +1,151 @@
package ui
import "core:math"
import "vendor:raylib"
import "../core"
import "../theme"
MenuBarItemOnClick :: proc(state: ^core.State, item: ^MenuBarItem);
text_padding :: 4;
MenuBarItem :: struct {
text: string,
selected: bool,
sub_items: []MenuBarItem,
on_click: MenuBarItemOnClick,
}
MenuBarState :: struct {
items: []MenuBarItem,
}
draw_menu_bar_item :: proc(state: ^core.State, item: ^MenuBarItem, x, y: i32, parent_width, parent_height: i32, font_height: int, horizontal: bool = false) {
foreground_color := theme.PaletteColor.Foreground3;
if horizontal {
if item.selected {
foreground_color = theme.PaletteColor.Background4;
} else {
foreground_color = theme.PaletteColor.Foreground4;
}
}
item_text := raylib.TextFormat("%s", item.text);
item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
raylib.DrawRectangle(x, y, parent_width, i32(font_height), theme.get_palette_raylib_color(foreground_color));
raylib.DrawTextEx(state.font, item_text, raylib.Vector2 { f32(x + text_padding), f32(y) }, f32(font_height), 0, theme.get_palette_raylib_color(.Background1));
if item.selected {
// TODO: change to parent_width
largest_sub_item: int
for sub_item in item.sub_items {
largest_sub_item = math.max(len(sub_item.text), largest_sub_item);
}
this_width := i32(largest_sub_item * state.source_font_width + text_padding*2);
sub_list_x := x;
if horizontal {
sub_list_x += parent_width;
}
for _, index in item.sub_items {
sub_item := &item.sub_items[index];
item_text := raylib.TextFormat("%s", sub_item.text);
item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
index_offset := 1;
if horizontal {
index_offset = 0;
}
item_y := y + i32(font_height * (index+index_offset));
draw_menu_bar_item(state, sub_item, sub_list_x, item_y, this_width, 0, font_height, true);
}
}
}
draw_menu_bar :: proc(state: ^core.State, data: ^MenuBarState, x, y: i32, parent_width, parent_height: i32, font_height: int) {
raylib.DrawRectangle(x, y, parent_width, i32(font_height), theme.get_palette_raylib_color(.Background3));
raylib.DrawTextEx(state.font, "Editor", raylib.Vector2 { f32(x), f32(y) }, f32(font_height), 0, theme.get_palette_raylib_color(.Foreground1));
x := x + i32((len("Editor") + 4) * state.source_font_width);
for _, index in data.items {
item := &data.items[index];
item_text := raylib.TextFormat("%s", item.text);
item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
item_x := x + (i32(item_width) + text_padding*2) * i32(index);
draw_menu_bar_item(state, item, item_x, y, i32(item_width + text_padding*2), i32(font_height), font_height);
}
}
test_menu_item :: proc(state: ^core.State, item: ^MenuBarItem, rect: raylib.Rectangle, mouse_pos: raylib.Vector2, mouse_has_clicked: bool, font_height: int, horizontal: bool) -> bool {
if raylib.CheckCollisionPointRec(mouse_pos, rect) {
item.selected = true;
if item.on_click != nil && mouse_has_clicked {
item.on_click(state, item);
}
} else if item.selected {
largest_sub_item: int
for sub_item in item.sub_items {
largest_sub_item = math.max(len(sub_item.text), largest_sub_item);
}
this_width := i32(largest_sub_item * state.source_font_width + text_padding*2);
sub_list_x := rect.x;
if horizontal {
sub_list_x += rect.width;
}
has_sub_item_selected := false;
for _, index in item.sub_items {
sub_item := &item.sub_items[index];
item_text := raylib.TextFormat("%s", sub_item.text);
item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
index_offset := 1;
if horizontal {
index_offset = 0;
}
item_y := rect.y + f32(font_height * (index+index_offset));
sub_rec := raylib.Rectangle {
x = sub_list_x,
y = item_y,
width = f32(this_width),
height = f32(font_height),
};
if test_menu_item(state, sub_item, sub_rec, mouse_pos, mouse_has_clicked, font_height, true) {
has_sub_item_selected = true;
}
}
item.selected = has_sub_item_selected;
} else {
item.selected = false;
}
return item.selected;
}
test_menu_bar :: proc(state: ^core.State, menu_bar: ^MenuBarState, x, y: i32, mouse_pos: raylib.Vector2, mouse_has_clicked: bool, font_height: int) {
x := x + i32((len("Editor") + 4) * state.source_font_width);
for _, index in menu_bar.items {
item := &menu_bar.items[index];
item_text := raylib.TextFormat("%s", item.text);
item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
item_rec := raylib.Rectangle {
x = f32(x) + (item_width + f32(text_padding*2)) * f32(index),
y = f32(y),
width = f32(item_width + text_padding*2),
height = f32(font_height),
};
test_menu_item(state, item, item_rec, mouse_pos, mouse_has_clicked, font_height, false);
}
}
// MenuBarItemOnClick :: proc(state: ^core.State, item: ^MenuBarItem);
//
// text_padding :: 4;
//
// MenuBarItem :: struct {
// text: string,
// selected: bool,
// sub_items: []MenuBarItem,
// on_click: MenuBarItemOnClick,
// }
//
// MenuBarState :: struct {
// items: []MenuBarItem,
// }
//
// draw_menu_bar_item :: proc(state: ^core.State, item: ^MenuBarItem, x, y: i32, parent_width, parent_height: i32, font_height: int, horizontal: bool = false) {
// foreground_color := theme.PaletteColor.Foreground3;
// if horizontal {
// if item.selected {
// foreground_color = theme.PaletteColor.Background4;
// } else {
// foreground_color = theme.PaletteColor.Foreground4;
// }
// }
//
// item_text := raylib.TextFormat("%s", item.text);
// item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
//
// raylib.DrawRectangle(x, y, parent_width, i32(font_height), theme.get_palette_raylib_color(foreground_color));
// raylib.DrawTextEx(state.font, item_text, raylib.Vector2 { f32(x + text_padding), f32(y) }, f32(font_height), 0, theme.get_palette_raylib_color(.Background1));
//
// if item.selected {
// // TODO: change to parent_width
// largest_sub_item: int
// for sub_item in item.sub_items {
// largest_sub_item = math.max(len(sub_item.text), largest_sub_item);
// }
//
// this_width := i32(largest_sub_item * state.source_font_width + text_padding*2);
// sub_list_x := x;
// if horizontal {
// sub_list_x += parent_width;
// }
// for _, index in item.sub_items {
// sub_item := &item.sub_items[index];
// item_text := raylib.TextFormat("%s", sub_item.text);
// item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
//
// index_offset := 1;
// if horizontal {
// index_offset = 0;
// }
// item_y := y + i32(font_height * (index+index_offset));
// draw_menu_bar_item(state, sub_item, sub_list_x, item_y, this_width, 0, font_height, true);
// }
// }
// }
//
// draw_menu_bar :: proc(state: ^core.State, data: ^MenuBarState, x, y: i32, parent_width, parent_height: i32, font_height: int) {
// raylib.DrawRectangle(x, y, parent_width, i32(font_height), theme.get_palette_raylib_color(.Background3));
//
// raylib.DrawTextEx(state.font, "Editor", raylib.Vector2 { f32(x), f32(y) }, f32(font_height), 0, theme.get_palette_raylib_color(.Foreground1));
//
// x := x + i32((len("Editor") + 4) * state.source_font_width);
//
// for _, index in data.items {
// item := &data.items[index];
// item_text := raylib.TextFormat("%s", item.text);
// item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
//
// item_x := x + (i32(item_width) + text_padding*2) * i32(index);
// draw_menu_bar_item(state, item, item_x, y, i32(item_width + text_padding*2), i32(font_height), font_height);
// }
// }
//
// test_menu_item :: proc(state: ^core.State, item: ^MenuBarItem, rect: raylib.Rectangle, mouse_pos: raylib.Vector2, mouse_has_clicked: bool, font_height: int, horizontal: bool) -> bool {
// if raylib.CheckCollisionPointRec(mouse_pos, rect) {
// item.selected = true;
//
// if item.on_click != nil && mouse_has_clicked {
// item.on_click(state, item);
// }
// } else if item.selected {
// largest_sub_item: int
// for sub_item in item.sub_items {
// largest_sub_item = math.max(len(sub_item.text), largest_sub_item);
// }
//
// this_width := i32(largest_sub_item * state.source_font_width + text_padding*2);
// sub_list_x := rect.x;
// if horizontal {
// sub_list_x += rect.width;
// }
//
// has_sub_item_selected := false;
// for _, index in item.sub_items {
// sub_item := &item.sub_items[index];
// item_text := raylib.TextFormat("%s", sub_item.text);
// item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
//
// index_offset := 1;
// if horizontal {
// index_offset = 0;
// }
// item_y := rect.y + f32(font_height * (index+index_offset));
//
// sub_rec := raylib.Rectangle {
// x = sub_list_x,
// y = item_y,
// width = f32(this_width),
// height = f32(font_height),
// };
//
// if test_menu_item(state, sub_item, sub_rec, mouse_pos, mouse_has_clicked, font_height, true) {
// has_sub_item_selected = true;
// }
// }
//
// item.selected = has_sub_item_selected;
// } else {
// item.selected = false;
// }
//
// return item.selected;
// }
//
// test_menu_bar :: proc(state: ^core.State, menu_bar: ^MenuBarState, x, y: i32, mouse_pos: raylib.Vector2, mouse_has_clicked: bool, font_height: int) {
// x := x + i32((len("Editor") + 4) * state.source_font_width);
//
// for _, index in menu_bar.items {
// item := &menu_bar.items[index];
// item_text := raylib.TextFormat("%s", item.text);
// item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
//
// item_rec := raylib.Rectangle {
// x = f32(x) + (item_width + f32(text_padding*2)) * f32(index),
// y = f32(y),
// width = f32(item_width + text_padding*2),
// height = f32(font_height),
// };
//
// test_menu_item(state, item, item_rec, mouse_pos, mouse_has_clicked, font_height, false);
// }
// }