Logo

index : binary-tree

---

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/things/binary-tree.git/html/src/tooltip.jai blob: af87effa71749b9d378a15f0f637f250c93710ba [raw] [clear marker]

        
0
1
2
3tooltip :: (mouse: Vector2, text: string) {
4
5 update_text_render :: () #expand {
6 array_reset_keeping_memory(*`codepoints);
7 array_reset_keeping_memory(*`offsets);
8
9 `rect_padded = rect;
10 `rect_padded.x += PADDING;
11 `rect_padded.y += PADDING;
12 `rect_padded.width -= PADDING;
13
14 `text_height = wrap_text(
15 GetFontDefault(),
16 to_c_string(`text),
17 *`codepoints,
18 *`offsets,
19 `rect_padded,
20 `FONT_SIZE,
21 `SPACING,
22 `MY_COLOR
23 );
24 }
25
26 BACKGROUND :: Color.{ 34, 31, 45, 242 };
27 OFFSET :: Vector2.{ 24.0, 33.0 };
28 LINES :: Color.{ 99, 90, 122, 240 };
29 FONT_SIZE :: 20.0;
30 SPACING :: 4.0;
31 PADDING :: 16.0;
32 MY_COLOR :: WHITE;
33
34 rect: Rectangle;
35 rect.x = mouse.x + OFFSET.x;
36 rect.y = mouse.y + OFFSET.y;
37 rect.width = #run 14.0 * 34;
38 rect.height = #run PADDING * 1.5;
39
40 codepoints: [..]s32;
41 offsets: [..]Vector2;
42 rect_padded: Rectangle;
43
44 text_height: float;
45 update_text_render();
46
47 rect.height += text_height;
48
49 screen_w := cast(float) GetScreenWidth();
50 screen_h := cast(float) GetScreenHeight();
51
52 if rect.x + rect.width > screen_w {
53 rect.x = mouse.x - OFFSET.x - rect.width;
54 update_text_render();
55 }
56
57 if rect.y + rect.height > screen_h {
58 rect.y = mouse.y - OFFSET.y - rect.height;
59 update_text_render();
60 }
61
62 DrawRectangleRounded(rect, 0.1, 6, BACKGROUND);
63 DrawRectangleRoundedLinesEx(rect, 0.1, 6, 1.0, LINES);
64
65 draw_text(
66 GetFontDefault(),
67 codepoints,
68 offsets,
69 FONT_SIZE,
70 SPACING,
71 MY_COLOR
72 );
73}
74
75tooltip_text :: (index: int, value: int) -> string {
76 node_type: string;
77 bv_upper: string;
78 bv_lower: string;
79 sa_info: string;
80
81 if index == 0 {
82 node_type = "Root Node";
83 } else {
84 if value == {
85 case NODE_EMPTY;
86 node_type = "Empty Node";
87 case NODE_INTERNAL;
88 node_type = "Internal Node";
89 case;
90 node_type = "Child Node";
91 bv := bvs[value];
92 bv_upper = tprint("\nBV Upper Bound: %", bv.upper_bound);
93 bv_lower = tprint("\nBV Lower Bound: %", bv.lower_bound);
94
95 sa := aabb_bv_compute_surface_area(bv);
96 sa_info = tprint("\nSurface Area: %", sa);
97 }
98 }
99
100 sb: String_Builder;
101 print_to_builder(*sb, "Type: %\n", node_type);
102 print_to_builder(*sb, "Index: %\n", index);
103 append(*sb, bv_upper);
104 append(*sb, bv_lower);
105 append(*sb, sa_info);
106
107 text := builder_to_string(*sb);
108 return text;
109}
110
111
112#scope_file
113
114
115/** C R E D I T
116
117Original proc `wrap_text` is from:
118 - @Raysan (Original Author), https://github.com/raysan5
119 - @ahmedqarmout2 (Jai Adoption), https://github.com/ahmedqarmout2
120
121*/
122
123wrap_text :: (
124 font: Font,
125 text: *u8,
126 codepoints: *[..]s32,
127 offsets: *[..]Vector2,
128 rec: Rectangle,
129 font_size: float,
130 spacing: float,
131 tint: Color,
132 word_wrap: bool = true
133)
134 -> text_height: float
135{
136 length: s32 = cast(s32)TextLength(text);
137
138 text_offset_y: float = 0.0;
139 text_offset_x: float = 0.0;
140
141 scale_factor: float = font_size / cast(float)font.baseSize;
142
143 my_enum :: enum { MEASURE_STATE :: 0; DRAW_STATE :: 1; }
144 state: my_enum = ifx word_wrap then .MEASURE_STATE else .DRAW_STATE;
145
146 start_line: s32 = -1; // Index where to begin drawing (where a line begins)
147 end_line: s32 = -1; // Index where to stop drawing (where a line ends)
148 lastk: s32 = -1; // Holds last value of the character position
149
150 i: s32 = 0;
151 k: s32 = 0;
152 while i < length {
153 defer i += 1;
154 defer k += 1;
155
156 codepoint_byte_count: s32 = 0;
157 codepoint: s32 = GetCodepoint(*text[i], *codepoint_byte_count);
158 index: s32 = GetGlyphIndex(font, codepoint);
159
160 if codepoint == 0x3f then codepoint_byte_count = 1;
161 i += (codepoint_byte_count - 1);
162
163 glyph_width: float = 0;
164
165 if codepoint != #char "\n" {
166 glyph_width = ifx (font.glyphs[index].advanceX == 0)
167 then font.recs[index].width * scale_factor
168 else font.glyphs[index].advanceX * scale_factor;
169
170 if (i + 1 < length) glyph_width = glyph_width + spacing;
171 }
172
173 if (state == .MEASURE_STATE) {
174 if codepoint == #char " "
175 || codepoint == #char "\t"
176 || codepoint == #char "\n"
177 then end_line = i;
178
179 if text_offset_x + glyph_width > rec.width {
180 end_line = ifx (end_line < 1) then i else end_line;
181
182 if i == end_line then end_line -= codepoint_byte_count;
183
184 if (start_line + codepoint_byte_count) == end_line
185 then end_line = (i - codepoint_byte_count);
186
187 state = ifx (state == .MEASURE_STATE) then .DRAW_STATE else .MEASURE_STATE;
188 }
189 else if i + 1 == length
190 {
191 end_line = i;
192 state = ifx (state == .MEASURE_STATE) then .DRAW_STATE else .MEASURE_STATE;
193 }
194 else if codepoint == #char "\n"
195 {
196 state = ifx (state == .MEASURE_STATE) then .DRAW_STATE else .MEASURE_STATE;
197 }
198
199 if state == .DRAW_STATE {
200 text_offset_x = 0;
201 i = start_line;
202 glyph_width = 0;
203
204 tmp: s32 = lastk;
205 lastk = k - 1;
206 k = tmp;
207 }
208 } else {
209 if codepoint == #char "\n" {
210 if !word_wrap {
211 text_offset_y += (font.baseSize + font.baseSize / 2) * scale_factor;
212 text_offset_x = 0;
213 }
214 } else {
215 if !word_wrap && text_offset_x + glyph_width > rec.width {
216 text_offset_y += (font.baseSize + font.baseSize / 2) * scale_factor;
217 text_offset_x = 0;
218 }
219
220 if codepoint != #char " " && codepoint != #char "\t" {
221 array_add(codepoints, codepoint);
222 array_add(offsets, { rec.x + text_offset_x, rec.y + text_offset_y });
223 }
224 }
225
226 if word_wrap && i == end_line {
227 text_offset_y += (font.baseSize + font.baseSize / 2) * scale_factor;
228 text_offset_x = 0;
229 start_line = end_line;
230 end_line = -1;
231 glyph_width = 0;
232 k = lastk;
233
234 state = ifx (state == .MEASURE_STATE) then .DRAW_STATE else .MEASURE_STATE;
235 }
236 }
237
238 if text_offset_x != 0 || codepoint != #char " " then text_offset_x += glyph_width;
239 }
240
241 return text_offset_y;
242}
243
244draw_text :: (
245 font: Font,
246 codepoints: [..]s32,
247 offsets: [..]Vector2,
248 font_size: float,
249 spacing: float,
250 tint: Color
251) {
252 assert(codepoints.count == offsets.count);
253
254 for codepoints {
255 x := offsets[it_index].x;
256 y := offsets[it_index].y;
257
258 DrawTextCodepoint(
259 font,
260 it,
261 { x, y },
262 font_size,
263 tint
264 );
265 }
266}
267
268
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit