forked from vurtun/nuklear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzahnrad.h
1601 lines (1461 loc) · 59.5 KB
/
zahnrad.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2016 Micha Mettke
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef ZR_H_
#define ZR_H_
#ifdef __cplusplus
extern "C" {
#endif
/*
* ==============================================================
*
* CONSTANTS
*
* ===============================================================
*/
#define ZR_UTF_INVALID 0xFFFD
#define ZR_UTF_SIZE 4
/* describes the number of bytes a glyph consists of*/
#define ZR_INPUT_MAX 16
/* defines the max number of bytes to be added as text input in one frame */
#define ZR_MAX_COLOR_STACK 32
/* Number of temporary configuration color changes that can be stored */
#define ZR_MAX_ATTRIB_STACK 32
/* Number of temporary configuration attribute changes that can be stored */
#define ZR_MAX_FONT_STACK 32
/* Number of temporary configuration user font changes that can be stored */
#define ZR_MAX_FONT_HEIGHT_STACK 32
/* Number of temporary configuration font height changes that can be stored */
#define ZR_MAX_NUMBER_BUFFER 64
/*
* ==============================================================
*
* DEFINES
*
* ===============================================================
*/
#define ZR_COMPILE_WITH_FIXED_TYPES 1
/* setting this define to 1 adds header <stdint.h> for fixed sized types
* if 0 each type has to be set to the correct size*/
#define ZR_COMPILE_WITH_ASSERT 1
/* setting this define to 1 adds header <assert.h> for the assert macro
IMPORTANT: it also adds the standard library assert so only use it if wanted*/
#define ZR_COMPILE_WITH_VERTEX_BUFFER 1
/* setting this to 1 adds a vertex draw command list backend to this
library, which allows you to convert queue commands into vertex draw commands.
If you do not want or need a default backend you can set this flag to zero
and the module of the library will not be compiled */
#define ZR_COMPILE_WITH_FONT 1
/* setting this to 1 adds the `stb_truetype` and `stb_rect_pack` header
to this library and provides a default font for font loading and rendering.
If you already have font handling or do not want to use this font handler
you can just set this define to zero and the font module will not be compiled
and the two headers will not be needed. */
#define ZR_DISABLE_STB_RECT_PACK_IMPLEMENTATION 0
/* If you already provide the implementation for stb_rect_pack.h in one of your
files you have to define this as 1 to prevent another implementation and the
resulting symbol collision. */
#define ZR_DISABLE_STB_TRUETYPE_IMPLEMENTATION 0
/* If you already provide the implementation for stb_truetype.h in one of your
files you have to define this as 1 to prevent another implementation and the
resulting symbol collision. */
/*
* ===============================================================
*
* BASIC
*
* ===============================================================
*/
#if ZR_COMPILE_WITH_FIXED_TYPES
#include <stdint.h>
typedef uint32_t zr_uint;
typedef uint32_t zr_hash;
typedef uintptr_t zr_size;
typedef uintptr_t zr_ptr;
typedef uint32_t zr_flags;
typedef uint32_t zr_rune;
typedef uint8_t zr_byte;
#else
typedef unsigned int zr_uint;
typedef unsigned int zr_hash;
typedef unsigned long zr_size;
typedef zr_size zr_ptr;
typedef unsigned int zr_flags;
typedef unsigned int zr_rune;
typedef unsigned char zr_byte;
#endif
#if ZR_COMPILE_WITH_ASSERT
#ifndef ZR_ASSERT
#include <assert.h>
#define ZR_ASSERT(expr) assert(expr)
#endif
#else
#define ZR_ASSERT(expr)
#endif
struct zr_user_font;
struct zr_edit_box;
struct zr_user_font_glyph;
/* ===============================================================
* UTILITY
* ===============================================================*/
#define ZR_UNDEFINED (-1.0f)
#define ZR_FLAG(x) (1 << (x))
enum {zr_false, zr_true};
struct zr_color {zr_byte r,g,b,a;};
struct zr_vec2 {float x,y;};
struct zr_vec2i {short x, y;};
struct zr_rect {float x,y,w,h;};
struct zr_recti {short x,y,w,h;};
typedef char zr_glyph[ZR_UTF_SIZE];
typedef union {void *ptr; int id;} zr_handle;
struct zr_image {zr_handle handle;unsigned short w,h;unsigned short region[4];};
struct zr_scroll {unsigned short x, y;};
/* math */
struct zr_rect zr_get_null_rect(void);
struct zr_rect zr_rect(float x, float y, float w, float h);
struct zr_vec2 zr_vec2(float x, float y);
/* UTF-8 */
zr_size zr_utf_decode(const char*, zr_rune*, zr_size);
zr_size zr_utf_encode(zr_rune, char*, zr_size);
zr_size zr_utf_len(const char*, zr_size byte_len);
/* color */
struct zr_color zr_rgba(zr_byte r, zr_byte g, zr_byte b, zr_byte a);
struct zr_color zr_rgb(zr_byte r, zr_byte g, zr_byte b);
struct zr_color zr_rgba_f(float r, float g, float b, float a);
struct zr_color zr_rgb_f(float r, float g, float b);
struct zr_color zr_hsv(zr_byte h, zr_byte s, zr_byte v);
struct zr_color zr_hsv_f(float h, float s, float v);
struct zr_color zr_hsva(zr_byte h, zr_byte s, zr_byte v, zr_byte a);
struct zr_color zr_hsva_f(float h, float s, float v, float a);
struct zr_color zr_rgba32(zr_uint);
zr_uint zr_color32(struct zr_color);
void zr_colorf(float *r, float *g, float *b, float *a, struct zr_color);
void zr_color_hsv(int *out_h, int *out_s, int *out_v, struct zr_color);
void zr_color_hsv_f(float *out_h, float *out_s, float *out_v, struct zr_color);
void zr_color_hsva(int *h, int *s, int *v, int *a, struct zr_color);
void zr_color_hsva_f(float *out_h, float *out_s, float *out_v,
float *out_a, struct zr_color);
/* image */
zr_handle zr_handle_ptr(void*);
zr_handle zr_handle_id(int);
struct zr_image zr_image_ptr(void*);
struct zr_image zr_image_id(int);
int zr_image_is_subimage(const struct zr_image* img);
struct zr_image zr_subimage_ptr(void*, unsigned short w, unsigned short h,
struct zr_rect sub_region);
struct zr_image zr_subimage_id(int, unsigned short w, unsigned short h,
struct zr_rect sub_region);
/* ==============================================================
*
* MEMORY BUFFER
*
* ===============================================================*/
/* A basic (double)-buffer with linear allocation and resetting as only
freeing policy. The buffers main purpose is to control all memory management
inside the GUI toolkit and still leave memory control as much as possible in
the hand of the user. The memory is provided in three different ways.
The first way is to use a fixed size block of memory to be filled up.
Biggest advantage is a simple memory model. Downside is that if the buffer
is full there is no way to accesses more memory, which fits target
application with a GUI with roughly known memory consumptions.
The second way to manage memory is by extending the fixed size block by
querying information from the buffer about the used size and needed size and
allocate new memory if the buffer is full. While this approach is still
better than just using a fixed size memory block the reallocation still has
one invalid frame as consquence since the used memory information is only
available at the end of the frame which leads to the last way of handling
memory.
The last and most complicated way of handling memory is by allocator
callbacks. The user hereby registers callbacks to be called to allocate and
free memory if needed. While this solves most allocation problems it causes
some loss of flow control on the user side.
USAGE
----------------------------
To instantiate the buffer you either have to call the fixed size or
allocator initialization function and provide a memory block in the first
case and an allocator in the second case.
To allocate memory from the buffer you would call zr_buffer_alloc with a
request memory block size as well as an alignment for the block.
Finally to reset the memory at the end of the frame and when the memory
buffer inside the buffer is no longer needed you would call zr_buffer_reset.
To free all memory that has been allocated by an allocator if the buffer is
no longer being used you have to call zr_buffer_clear.
*/
struct zr_memory_status {
void *memory;
/* pointer to the currently used memory block inside the referenced buffer*/
unsigned int type;
/* type of the buffer which is either fixed size or dynamic */
zr_size size;
/* total size of the memory block */
zr_size allocated;
/* allocated amount of memory */
zr_size needed;
/* memory size that would have been allocated if enough memory was present*/
zr_size calls;
/* number of allocation calls referencing this buffer */
};
struct zr_allocator {
zr_handle userdata;
/* handle to your own allocator */
void*(*alloc)(zr_handle, zr_size);
/* allocation function pointer */
/* reallocation pointer of a previously allocated memory block */
void(*free)(zr_handle, void*);
/* callback function pointer to finally free all allocated memory */
};
enum zr_allocation_type {
ZR_BUFFER_FIXED,
/* fixed size memory buffer */
ZR_BUFFER_DYNAMIC
/* dynamically growing buffer */
};
enum zr_buffer_allocation_type {
ZR_BUFFER_FRONT,
/* allocate memory from the front of the buffer */
ZR_BUFFER_BACK,
/* allocate memory from the back of the buffer */
ZR_BUFFER_MAX
};
struct zr_buffer_marker {
int active;
/* flag indiciation if the marker was set */
zr_size offset;
/* offset of the marker inside the buffer */
};
struct zr_memory {void *ptr;zr_size size;};
struct zr_buffer {
struct zr_buffer_marker marker[ZR_BUFFER_MAX];
/* buffer marker to free a buffer to a certain offset */
struct zr_allocator pool;
/* allocator callback for dynamic buffers */
enum zr_allocation_type type;
/* memory management type */
struct zr_memory memory;
/* memory and size of the current memory block */
float grow_factor;
/* growing factor for dynamic memory management */
zr_size allocated;
/* total amount of memory allocated */
zr_size needed;
/* totally consumed memory given that enough memory is present */
zr_size calls;
/* number of allocation calls */
zr_size size;
/* current size of the buffer */
};
void zr_buffer_init(struct zr_buffer*, const struct zr_allocator*, zr_size size);
void zr_buffer_init_fixed(struct zr_buffer*, void *memory, zr_size size);
void zr_buffer_info(struct zr_memory_status*, struct zr_buffer*);
void zr_buffer_free(struct zr_buffer*);
void *zr_buffer_memory(struct zr_buffer*);
zr_size zr_buffer_total(struct zr_buffer*);
/* ===============================================================
*
* FONT
*
* ===============================================================*/
/* Font handling in this library can be achived in three different ways.
The first and simplest ways is by just using your font handling mechanism
and provide a simple callback for text string width calculation with
`zr_user_font`. This requires the default drawing output
and is not possible for the optional vertex buffer output.
The second way of font handling is by using the same `zr_user_font` struct
to referencing a font as before but providing a second callback for
`zr_user_font_glyph` querying which is used for text drawing in the optional
vertex buffer output. In addition to the callback it is also required to
provide a texture atlas from the font to draw.
The final and most complex way is to use the optional font baker
and font handling function, which requires two additional headers for
TTF font baking. While the previous two methods did no need any functions
outside callbacks and are therefore rather simple to handle, the final
font handling method is quite complex and you need to handle the complex
font baking API. The reason why it is complex is because there are multible
ways of using the API. For example it must be possible to use the font
for default command output as well as vertex buffer output. So for example
texture coordinates can either be UV for vertex buffer output or absolute
pixel for drawing function based on pixels. Furthermore it is possible to
incoperate custom user data into the resulting baked image (for example a
white pixel for the vertex buffer output).
In addition and probably the most complex aspect of the baking API was to
incoperate baking of multible fonts into one image.
In general the font baking API can be understood as having a number of
loaded in memory TTF-fonts, font baking configuration and optional custom
render data as input, while the output is made of font specific data, a big
glyph array of all baked glyphs and the baked image. The API
was designed that way to have a typical file format and not
a perfectly ready in memory library instance of a font. The reason is more
control and seperates the font baking code from the in library used font
format.
*/
typedef zr_size(*zr_text_width_f)(zr_handle, float h, const char*, zr_size len);
typedef void(*zr_query_font_glyph_f)(zr_handle handle, float font_height,
struct zr_user_font_glyph *glyph,
zr_rune codepoint, zr_rune next_codepoint);
#if ZR_COMPILE_WITH_VERTEX_BUFFER
struct zr_user_font_glyph {
struct zr_vec2 uv[2];
/* texture coordinates */
struct zr_vec2 offset;
/* offset between top left and glyph */
float width, height;
/* size of the glyph */
float xadvance;
/* offset to the next glyph */
};
#endif
struct zr_user_font {
zr_handle userdata;
/* user provided font handle */
float height;
/* max height of the font */
zr_text_width_f width;
/* font string width in pixel callback */
#if ZR_COMPILE_WITH_VERTEX_BUFFER
zr_query_font_glyph_f query;
/* font glyph callback to query drawing info */
zr_handle texture;
/* texture handle to the used font atlas or texture */
#endif
};
#ifdef ZR_COMPILE_WITH_FONT
enum zr_font_coord_type {
ZR_COORD_UV,
/* texture coordinates inside font glyphs are clamped between 0-1 */
ZR_COORD_PIXEL
/* texture coordinates inside font glyphs are in absolute pixel */
};
struct zr_baked_font {
float height;
/* height of the font */
float ascent, descent;
/* font glyphs ascent and descent */
zr_rune glyph_offset;
/* glyph array offset inside the font glyph baking output array */
zr_rune glyph_count;
/* number of glyphs of this font inside the glyph baking array output */
const zr_rune *ranges;
/* font codepoint ranges as pairs of (from/to) and 0 as last element */
};
struct zr_font_config {
void *ttf_blob;
/* pointer to loaded TTF file memory block */
zr_size ttf_size;
/* size of the loaded TTF file memory block */
float size;
/* bake pixel height of the font */
zr_rune oversample_h, oversample_v;
/* rasterize at hight quality for sub-pixel position */
int pixel_snap;
/* align very character to pixel boundry (if true set oversample (1,1)) */
enum zr_font_coord_type coord_type;
/* texture coordinate format with either pixel or UV coordinates */
struct zr_vec2 spacing;
/* extra pixel spacing between glyphs */
const zr_rune *range;
/* list of unicode ranges (2 values per range, zero terminated) */
struct zr_baked_font *font;
/* font to setup in the baking process */
};
struct zr_font_glyph {
zr_rune codepoint;
/* unicode codepoint */
float xadvance;
/* xoffset to the next character */
float x0, y0, x1, y1, w, h;
/* glyph bounding points in pixel inside the glyph image with top
* left and bottom right */
float u0, v0, u1, v1;
/* texture coordinates either in pixel or clamped (0.0 - 1.0) */
};
struct zr_font {
float size;
/* pixel height of the font */
float scale;
/* scale factor for different font size */
float ascent, descent;
/* font ascent and descent */
struct zr_font_glyph *glyphs;
/* font glyph array */
const struct zr_font_glyph *fallback;
/* fallback glyph */
zr_rune fallback_codepoint;
/* fallback glyph codepoint */
zr_rune glyph_count;
/* font glyph array size */
const zr_rune *ranges;
/* glyph unicode ranges in the font */
zr_handle atlas;
/* font image atlas handle */
};
/* some language glyph codepoint ranges */
const zr_rune *zr_font_default_glyph_ranges(void);
const zr_rune *zr_font_chinese_glyph_ranges(void);
const zr_rune *zr_font_cyrillic_glyph_ranges(void);
const zr_rune *zr_font_korean_glyph_ranges(void);
/* font baking functions (need to be called sequentially top to bottom) */
void zr_font_bake_memory(zr_size *temporary_memory, int *glyph_count,
struct zr_font_config*, int count);
/* this function calculates the needed memory for the baking process
Input:
- array of configuration for every font that should be baked into one image
- number of configuration fonts in the array
Output:
- amount of memory needed in the baking process
- total number of glyphs that need to be allocated
*/
int zr_font_bake_pack(zr_size *img_memory, int *img_width, int *img_height,
struct zr_recti *custom_space,
void *temporary_memory, zr_size temporary_size,
const struct zr_font_config*, int font_count);
/* this function packs together all glyphs and optional space into one
total image space and returns the needed image width and height.
Input:
- NULL or custom space inside the image (will be modifed to fit!)
- temporary memory block that will be used in the baking process
- size of the temporary memory block
- array of configuration for every font that should be baked into one image
- number of configuration fonts in the array
Output:
- calculated resulting size of the image in bytes
- pixel width of the resulting image
- pixel height of the resulting image
- custom space bounds with position and size inside image which can be
filled by the user
*/
void zr_font_bake(void *image_memory, int image_width, int image_height,
void *temporary_memory, zr_size temporary_memory_size,
struct zr_font_glyph*, int glyphs_count,
const struct zr_font_config*, int font_count);
/* this function bakes all glyphs into the pre-allocated image and
fills a glyph array with information.
Input:
- image memory buffer to bake the glyph into
- pixel width/height of the image
- temporary memory block that will be used in the baking process
- size of the temporary memory block
Output:
- image filled with glyphs
- filled glyph array
*/
void zr_font_bake_custom_data(void *img_memory, int img_width, int img_height,
struct zr_recti img_dst, const char *image_data_mask,
int tex_width, int tex_height,char white,char black);
/* this function bakes custom data in string format with white, black and zero
alpha pixels into the font image. The zero alpha pixel is represented as
any character beside the black and zero pixel character.
Input:
- image memory buffer to bake the custom data into
- image size (width/height) of the image in pixels
- custom texture data in string format
- texture size (width/height) of the custom image content
- character representing a white pixel in the texture data format
- character representing a black pixel in the texture data format
Output:
- image filled with custom texture data
*/
void zr_font_bake_convert(void *out_memory, int image_width, int image_height,
const void *in_memory);
/* this function converts alpha8 baking input image into a rgba8 output image.*/
void zr_font_init(struct zr_font*, float pixel_height, zr_rune fallback_codepoint,
struct zr_font_glyph*, const struct zr_baked_font*,
zr_handle atlas);
struct zr_user_font zr_font_ref(struct zr_font*);
const struct zr_font_glyph* zr_font_find_glyph(struct zr_font*, zr_rune unicode);
#endif
/* ===============================================================
*
* RENDERING
*
* ===============================================================*/
/* This library was designed to be render backend agnostic so it does
not draw anything to the screen. Instead all drawn primitives, widgets
are made of, are buffered into memory and make up a command queue.
Each frame therefore fills the command buffer with draw commands
that then need to be executed by the user and his own render backend.
After that the command buffer needs to be cleared and a new frame can be
started.
The reason for buffering simple primitives as draw commands instead of
directly buffering a hardware accessible format with vertex and element
buffer was to support native render backends like X11 and Win32.
That being said it is possible to convert the command buffer into a
hardware accessible format to support hardware based rendering as well.
*/
enum zr_command_type {
ZR_COMMAND_NOP,
ZR_COMMAND_SCISSOR,
ZR_COMMAND_LINE,
ZR_COMMAND_CURVE,
ZR_COMMAND_RECT,
ZR_COMMAND_CIRCLE,
ZR_COMMAND_ARC,
ZR_COMMAND_TRIANGLE,
ZR_COMMAND_TEXT,
ZR_COMMAND_IMAGE
};
/* command base and header of every comand inside the buffer */
struct zr_command {
enum zr_command_type type;
/* the type of the current command */
zr_size next;
/* absolute base pointer offset to the next command */
};
struct zr_command_scissor {
struct zr_command header;
short x, y;
unsigned short w, h;
};
struct zr_command_line {
struct zr_command header;
struct zr_vec2i begin;
struct zr_vec2i end;
struct zr_color color;
};
struct zr_command_curve {
struct zr_command header;
struct zr_vec2i begin;
struct zr_vec2i end;
struct zr_vec2i ctrl[2];
struct zr_color color;
};
struct zr_command_rect {
struct zr_command header;
unsigned int rounding;
short x, y;
unsigned short w, h;
struct zr_color color;
};
struct zr_command_circle {
struct zr_command header;
short x, y;
unsigned short w, h;
struct zr_color color;
};
struct zr_command_arc {
struct zr_command header;
short cx, cy;
unsigned short r;
float a[2];
struct zr_color color;
};
struct zr_command_triangle {
struct zr_command header;
struct zr_vec2i a;
struct zr_vec2i b;
struct zr_vec2i c;
struct zr_color color;
};
struct zr_command_image {
struct zr_command header;
short x, y;
unsigned short w, h;
struct zr_image img;
};
struct zr_command_text {
struct zr_command header;
const struct zr_user_font *font;
struct zr_color background;
struct zr_color foreground;
short x, y;
unsigned short w, h;
float height;
zr_size length;
char string[1];
};
enum zr_command_clipping {
ZR_CLIPPING_OFF = zr_false,
ZR_CLIPPING_ON = zr_true
};
struct zr_command_buffer {
struct zr_buffer *base;
/* memory buffer to store the command */
struct zr_rect clip;
/* current clipping rectangle */
int use_clipping;
/* flag if the command buffer should clip commands */
zr_size begin, end, last;
};
#if ZR_COMPILE_WITH_VERTEX_BUFFER
typedef unsigned short zr_draw_index;
typedef zr_uint zr_draw_vertex_color;
enum zr_anti_aliasing {
ZR_ANTI_ALIASING_OFF = zr_false,
/* renderes all primitives without anti-aliasing */
ZR_ANTI_ALIASING_ON
/* renderes all primitives with anti-aliasing */
};
struct zr_draw_vertex {
struct zr_vec2 position;
struct zr_vec2 uv;
zr_draw_vertex_color col;
};
struct zr_draw_command {
unsigned int elem_count;
/* number of elements in the current draw batch */
struct zr_rect clip_rect;
/* current screen clipping rectangle */
zr_handle texture;
/* current texture to set */
};
struct zr_draw_null_texture {
zr_handle texture;
/* texture handle to a texture with a white pixel */
struct zr_vec2 uv;
/* coordinates to the white pixel in the texture */
};
/* drawing routines for custom widgets */
void zr_draw_scissor(struct zr_command_buffer*, struct zr_rect);
void zr_draw_line(struct zr_command_buffer*, float, float, float,
float, struct zr_color);
void zr_draw_curve(struct zr_command_buffer*, float, float, float, float,
float, float, float, float, struct zr_color);
void zr_draw_rect(struct zr_command_buffer*, struct zr_rect,
float rounding, struct zr_color);
void zr_draw_circle(struct zr_command_buffer*, struct zr_rect, struct zr_color);
void zr_draw_arc(struct zr_command_buffer*, float cx, float cy, float radius,
float a_min, float a_max, struct zr_color);
void zr_draw_triangle(struct zr_command_buffer*, float, float, float, float,
float, float, struct zr_color);
void zr_draw_image(struct zr_command_buffer*, struct zr_rect, struct zr_image*);
void zr_draw_text(struct zr_command_buffer*, struct zr_rect,
const char *text, zr_size len, const struct zr_user_font*,
struct zr_color, struct zr_color);
#endif
/* ===============================================================
*
* GUI
*
* ===============================================================*/
enum zr_keys {
ZR_KEY_SHIFT,
ZR_KEY_DEL,
ZR_KEY_ENTER,
ZR_KEY_TAB,
ZR_KEY_BACKSPACE,
ZR_KEY_COPY,
ZR_KEY_CUT,
ZR_KEY_PASTE,
ZR_KEY_LEFT,
ZR_KEY_RIGHT,
ZR_KEY_MAX
};
/* every used mouse button */
enum zr_buttons {
ZR_BUTTON_LEFT,
ZR_BUTTON_MIDDLE,
ZR_BUTTON_RIGHT,
ZR_BUTTON_MAX
};
struct zr_mouse_button {
int down;
/* current button state */
unsigned int clicked;
/* button state change */
struct zr_vec2 clicked_pos;
/* mouse position of last state change */
};
struct zr_mouse {
struct zr_mouse_button buttons[ZR_BUTTON_MAX];
/* mouse button states */
struct zr_vec2 pos;
/* current mouse position */
struct zr_vec2 prev;
/* mouse position in the last frame */
struct zr_vec2 delta;
/* mouse travelling distance from last to current frame */
float scroll_delta;
/* number of steps in the up or down scroll direction */
};
struct zr_key {
int down;
unsigned int clicked;
};
struct zr_keyboard {
struct zr_key keys[ZR_KEY_MAX];
/* state of every used key */
char text[ZR_INPUT_MAX];
/* utf8 text input frame buffer */
zr_size text_len;
/* text input frame buffer length in bytes */
};
struct zr_input {
struct zr_keyboard keyboard;
/* current keyboard key + text input state */
struct zr_mouse mouse;
/* current mouse button and position state */
};
/* query input state */
int zr_input_has_mouse_click_in_rect(const struct zr_input*,
enum zr_buttons, struct zr_rect);
int zr_input_has_mouse_click_down_in_rect(const struct zr_input*, enum zr_buttons,
struct zr_rect, int down);
int zr_input_is_mouse_click_in_rect(const struct zr_input*,
enum zr_buttons, struct zr_rect);
int zr_input_any_mouse_click_in_rect(const struct zr_input*, struct zr_rect);
int zr_input_is_mouse_prev_hovering_rect(const struct zr_input*, struct zr_rect);
int zr_input_is_mouse_hovering_rect(const struct zr_input*, struct zr_rect);
int zr_input_mouse_clicked(const struct zr_input*, enum zr_buttons, struct zr_rect);
int zr_input_is_mouse_down(const struct zr_input*, enum zr_buttons);
int zr_input_is_mouse_pressed(const struct zr_input*, enum zr_buttons);
int zr_input_is_mouse_released(const struct zr_input*, enum zr_buttons);
int zr_input_is_key_pressed(const struct zr_input*, enum zr_keys);
int zr_input_is_key_released(const struct zr_input*, enum zr_keys);
int zr_input_is_key_down(const struct zr_input*, enum zr_keys);
/* ==============================================================
* STYLE
* ===============================================================*/
enum zr_style_colors {
ZR_COLOR_TEXT,
ZR_COLOR_TEXT_HOVERING,
ZR_COLOR_TEXT_ACTIVE,
ZR_COLOR_WINDOW,
ZR_COLOR_HEADER,
ZR_COLOR_BORDER,
ZR_COLOR_BUTTON,
ZR_COLOR_BUTTON_HOVER,
ZR_COLOR_BUTTON_ACTIVE,
ZR_COLOR_TOGGLE,
ZR_COLOR_TOGGLE_HOVER,
ZR_COLOR_TOGGLE_CURSOR,
ZR_COLOR_SELECTABLE,
ZR_COLOR_SELECTABLE_HOVER,
ZR_COLOR_SELECTABLE_TEXT,
ZR_COLOR_SLIDER,
ZR_COLOR_SLIDER_CURSOR,
ZR_COLOR_SLIDER_CURSOR_HOVER,
ZR_COLOR_SLIDER_CURSOR_ACTIVE,
ZR_COLOR_PROGRESS,
ZR_COLOR_PROGRESS_CURSOR,
ZR_COLOR_PROGRESS_CURSOR_HOVER,
ZR_COLOR_PROGRESS_CURSOR_ACTIVE,
ZR_COLOR_PROPERTY,
ZR_COLOR_PROPERTY_HOVER,
ZR_COLOR_PROPERTY_ACTIVE,
ZR_COLOR_INPUT,
ZR_COLOR_INPUT_CURSOR,
ZR_COLOR_INPUT_TEXT,
ZR_COLOR_COMBO,
ZR_COLOR_HISTO,
ZR_COLOR_HISTO_BARS,
ZR_COLOR_HISTO_HIGHLIGHT,
ZR_COLOR_PLOT,
ZR_COLOR_PLOT_LINES,
ZR_COLOR_PLOT_HIGHLIGHT,
ZR_COLOR_SCROLLBAR,
ZR_COLOR_SCROLLBAR_CURSOR,
ZR_COLOR_SCROLLBAR_CURSOR_HOVER,
ZR_COLOR_SCROLLBAR_CURSOR_ACTIVE,
ZR_COLOR_TABLE_LINES,
ZR_COLOR_TAB_HEADER,
ZR_COLOR_SCALER,
ZR_COLOR_COUNT
};
enum zr_style_rounding {
ZR_ROUNDING_BUTTON,
ZR_ROUNDING_SLIDER,
ZR_ROUNDING_CHECK,
ZR_ROUNDING_INPUT,
ZR_ROUNDING_PROPERTY,
ZR_ROUNDING_CHART,
ZR_ROUNDING_SCROLLBAR,
ZR_ROUNDING_MAX
};
enum zr_style_properties {
ZR_PROPERTY_ITEM_SPACING,
/* space between widgets */
ZR_PROPERTY_ITEM_PADDING,
/* padding inside widet between content */
ZR_PROPERTY_TOUCH_PADDING,
/* extra padding for touch devices */
ZR_PROPERTY_PADDING,
/* padding between window and widgets */
ZR_PROPERTY_SCALER_SIZE,
/* width and height of the window scaler */
ZR_PROPERTY_SCROLLBAR_SIZE,
/* width for vertical scrollbar and height for horizontal scrollbar*/
ZR_PROPERTY_SIZE,
/* min size of a window that cannot be undercut */
ZR_PROPERTY_MAX
};
enum zr_style_header_align {
ZR_HEADER_LEFT,
ZR_HEADER_RIGHT
};
enum zr_style_components {
ZR_DEFAULT_COLOR = 0x01,
/* default all colors inside the configuration struct */
ZR_DEFAULT_PROPERTIES = 0x02,
/* default all properites inside the configuration struct */
ZR_DEFAULT_ROUNDING = 0x04,
/* default all rounding values inside the configuration struct */
ZR_DEFAULT_ALL = 0xFFFF
/* default the complete configuration struct */
};
struct zr_saved_property {
enum zr_style_properties type;
/* identifier of the current modified property */
struct zr_vec2 value;
/* property value that has been saveed */
};
struct zr_saved_color {
enum zr_style_colors type;
/* identifier of the current modified color */
struct zr_color value;
/* color value that has been saveed */
};
struct zr_saved_font {
struct zr_user_font value;
/* user font reference */
int font_height_begin;
/* style font height stack begin */
int font_height_end;
/* user font height stack end */
};
struct zr_style_mod_stack {
zr_size property;
/* current property stack pushing index */
struct zr_saved_property properties[ZR_MAX_ATTRIB_STACK];
/* saved property stack */
int color;
/* current color stack pushing index */
struct zr_saved_color colors[ZR_MAX_COLOR_STACK];
/* saved color stack */
int font;
/* current font stack pushing index */
struct zr_saved_font fonts[ZR_MAX_FONT_STACK];
/* saved user font stack */
int font_height;
/* current font stack pushing index */
float font_heights[ZR_MAX_FONT_HEIGHT_STACK];
/* saved user font stack */
};
struct zr_style_header {
enum zr_style_header_align align;
/* header content alignment */
zr_rune close_symbol;
/* header close icon unicode rune */
zr_rune minimize_symbol;
/* header minimize icon unicode rune */
zr_rune maximize_symbol;
/* header maximize icon unicode rune */
};
struct zr_style {
struct zr_user_font font;
/* the from the user provided font */
float rounding[ZR_ROUNDING_MAX];
/* rectangle widget rounding */
struct zr_style_header header;
/* window header style */
struct zr_vec2 properties[ZR_PROPERTY_MAX];
/* configuration properties to modify the style */
struct zr_color colors[ZR_COLOR_COUNT];
/* configuration color to modify color */
struct zr_style_mod_stack stack;
/* modification stack */
};
/*===============================================================
* EDIT BOX
* ===============================================================*/
typedef int(*zr_filter)(const struct zr_edit_box*, zr_rune unicode);
typedef void(*zr_paste_f)(zr_handle, struct zr_edit_box*);
typedef void(*zr_copy_f)(zr_handle, const char*, zr_size size);
/* filter function */
struct zr_edit_box;
int zr_filter_default(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_ascii(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_float(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_decimal(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_hex(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_oct(const struct zr_edit_box*, zr_rune unicode);
int zr_filter_binary(const struct zr_edit_box*, zr_rune unicode);
enum zr_edit_flags {
ZR_EDIT_READ_ONLY = ZR_FLAG(0),
/* text inside the edit widget cannot be modified */
ZR_EDIT_CURSOR = ZR_FLAG(1),
/* edit widget will have a movable cursor */
ZR_EDIT_SELECTABLE = ZR_FLAG(2),
/* edit widget allows text selection */
ZR_EDIT_CLIPBOARD = ZR_FLAG(3),
/* edit widget tries to use the clipbard callback for copy & paste */
ZR_EDIT_SIGCOMIT = ZR_FLAG(4),
/* edit widget generateds ZR_EDIT_COMMITED event on enter */
ZR_EDIT_MULTILINE = ZR_FLAG(5)
/* edit widget with text wrapping text editing */
};
enum zr_edit_types {
ZR_EDIT_SIMPLE = 0,
ZR_EDIT_FIELD = (ZR_EDIT_CURSOR|ZR_EDIT_SELECTABLE|ZR_EDIT_CLIPBOARD),
ZR_EDIT_BOX = (ZR_EDIT_CURSOR|ZR_EDIT_SELECTABLE|
ZR_EDIT_CLIPBOARD|ZR_EDIT_MULTILINE)
};
enum zr_edit_events {
ZR_EDIT_ACTIVE = ZR_FLAG(0),
/* edit widget is currently being modified */
ZR_EDIT_INACTIVE = ZR_FLAG(1),
/* edit widget is not active and is not being modified */
ZR_EDIT_ACTIVATED = ZR_FLAG(2),
/* edit widget went from state inactive to state active */
ZR_EDIT_DEACTIVATED = ZR_FLAG(3),
/* edit widget went from state active to state inactive */
ZR_EDIT_COMMITED = ZR_FLAG(4)
/* edit widget has received an enter and lost focus */
};
/* editbox */
void zr_edit_box_clear(struct zr_edit_box*);
void zr_edit_box_add(struct zr_edit_box*, const char*, zr_size);
void zr_edit_box_remove(struct zr_edit_box*);
char *zr_edit_box_get(struct zr_edit_box*);
const char *zr_edit_box_get_const(const struct zr_edit_box*);
void zr_edit_box_at(struct zr_edit_box*, zr_size pos, zr_glyph, zr_size*);