nuklear 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +29 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +196 -0
  8. data/Rakefile +26 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/examples/arial.ttf +0 -0
  12. data/examples/calculator.rb +102 -0
  13. data/examples/hello_nuklear.rb +182 -0
  14. data/examples/lib/opengl_font.rb +32 -0
  15. data/examples/lib/opengl_init.rb +4 -0
  16. data/examples/lib/sdl2_init.rb +6 -0
  17. data/examples/lib/sdl2_input.rb +81 -0
  18. data/examples/lib/window.rb +47 -0
  19. data/ext/freetype/extconf.rb +26 -0
  20. data/ext/nuklear/extconf.rb +14 -0
  21. data/ext/nuklear/nkrb.c +79 -0
  22. data/ext/nuklear/nkrb.h +89 -0
  23. data/ext/nuklear/nkrb_buffer.c +80 -0
  24. data/ext/nuklear/nkrb_context.c +241 -0
  25. data/ext/nuklear/nkrb_font.c +80 -0
  26. data/ext/nuklear/nkrb_renderer.c +114 -0
  27. data/ext/nuklear/nkrb_style.c +61 -0
  28. data/ext/nuklear/nkrb_style_color.c +126 -0
  29. data/ext/nuklear/nkrb_style_image.c +32 -0
  30. data/ext/nuklear/nkrb_ui.c +32 -0
  31. data/ext/nuklear/nkrb_ui_builder.c +29 -0
  32. data/ext/nuklear/nkrb_ui_button.c +55 -0
  33. data/ext/nuklear/nkrb_ui_color_picker.c +20 -0
  34. data/ext/nuklear/nkrb_ui_combo.c +73 -0
  35. data/ext/nuklear/nkrb_ui_container.c +7 -0
  36. data/ext/nuklear/nkrb_ui_edit_string.c +38 -0
  37. data/ext/nuklear/nkrb_ui_group.c +27 -0
  38. data/ext/nuklear/nkrb_ui_label.c +30 -0
  39. data/ext/nuklear/nkrb_ui_layout.c +125 -0
  40. data/ext/nuklear/nkrb_ui_menu.c +49 -0
  41. data/ext/nuklear/nkrb_ui_menu_item.c +30 -0
  42. data/ext/nuklear/nkrb_ui_menubar.c +18 -0
  43. data/ext/nuklear/nkrb_ui_popup.c +24 -0
  44. data/ext/nuklear/nkrb_ui_progress.c +19 -0
  45. data/ext/nuklear/nkrb_ui_property.c +20 -0
  46. data/ext/nuklear/nkrb_ui_selectables.c +53 -0
  47. data/ext/nuklear/nkrb_ui_slider.c +19 -0
  48. data/ext/nuklear/nkrb_ui_tree.c +29 -0
  49. data/ext/nuklear/nkrb_ui_widget.c +7 -0
  50. data/ext/nuklear/nkrb_ui_window.c +43 -0
  51. data/ext/nuklear/nuklear.h +23378 -0
  52. data/ext/nuklear_renderer_opengl2/KHR/khrplatform.h +285 -0
  53. data/ext/nuklear_renderer_opengl2/extconf.rb +13 -0
  54. data/ext/nuklear_renderer_opengl2/glad.c +1432 -0
  55. data/ext/nuklear_renderer_opengl2/glad.h +2747 -0
  56. data/ext/nuklear_renderer_opengl2/nuklear_renderer_opengl2.c +197 -0
  57. data/ext/nuklear_renderer_opengl4/KHR/khrplatform.h +285 -0
  58. data/ext/nuklear_renderer_opengl4/extconf.rb +13 -0
  59. data/ext/nuklear_renderer_opengl4/glad.c +1782 -0
  60. data/ext/nuklear_renderer_opengl4/glad.h +3687 -0
  61. data/ext/nuklear_renderer_opengl4/nuklear_renderer_opengl4.c +255 -0
  62. data/lib/nuklear/context.rb +49 -0
  63. data/lib/nuklear/dsl.rb +46 -0
  64. data/lib/nuklear/event_buffer.rb +23 -0
  65. data/lib/nuklear/renderer/opengl24.rb +13 -0
  66. data/lib/nuklear/renderer.rb +108 -0
  67. data/lib/nuklear/style/color.rb +24 -0
  68. data/lib/nuklear/style/image.rb +9 -0
  69. data/lib/nuklear/style.rb +8 -0
  70. data/lib/nuklear/test_case.rb +30 -0
  71. data/lib/nuklear/ui/base.rb +34 -0
  72. data/lib/nuklear/ui/button.rb +77 -0
  73. data/lib/nuklear/ui/checkbox.rb +39 -0
  74. data/lib/nuklear/ui/col.rb +21 -0
  75. data/lib/nuklear/ui/color_picker.rb +31 -0
  76. data/lib/nuklear/ui/combo_box.rb +42 -0
  77. data/lib/nuklear/ui/container.rb +80 -0
  78. data/lib/nuklear/ui/edit_string.rb +48 -0
  79. data/lib/nuklear/ui/enableable.rb +29 -0
  80. data/lib/nuklear/ui/events.rb +23 -0
  81. data/lib/nuklear/ui/group.rb +31 -0
  82. data/lib/nuklear/ui/label.rb +21 -0
  83. data/lib/nuklear/ui/menu.rb +43 -0
  84. data/lib/nuklear/ui/menu_bar.rb +19 -0
  85. data/lib/nuklear/ui/menu_item.rb +34 -0
  86. data/lib/nuklear/ui/option.rb +17 -0
  87. data/lib/nuklear/ui/option_group.rb +22 -0
  88. data/lib/nuklear/ui/popup.rb +37 -0
  89. data/lib/nuklear/ui/progress.rb +33 -0
  90. data/lib/nuklear/ui/property.rb +28 -0
  91. data/lib/nuklear/ui/row.rb +28 -0
  92. data/lib/nuklear/ui/select_list.rb +31 -0
  93. data/lib/nuklear/ui/selectable.rb +21 -0
  94. data/lib/nuklear/ui/slider.rb +26 -0
  95. data/lib/nuklear/ui/text_align.rb +14 -0
  96. data/lib/nuklear/ui/tree.rb +62 -0
  97. data/lib/nuklear/ui/window.rb +175 -0
  98. data/lib/nuklear/ui.rb +33 -0
  99. data/lib/nuklear/version.rb +3 -0
  100. data/lib/nuklear.rb +26 -0
  101. data/nuklear.gemspec +41 -0
  102. metadata +233 -0
@@ -0,0 +1,80 @@
1
+ #include "nkrb.h"
2
+
3
+ typedef struct {
4
+ struct { int width, height; const void *data; } img;
5
+ struct nk_font_atlas atlas;
6
+ struct nk_font *font;
7
+ struct nk_draw_null_texture null;
8
+ } NkRbFont;
9
+
10
+ #define VALUE_TO_NK(value, varname) \
11
+ NkRbFont *varname = NULL; \
12
+ Data_Get_Struct(value, NkRbFont, varname);
13
+
14
+ VALUE cNuklearFont = Qnil;
15
+ VALUE cNuklearNullTexture = Qnil;
16
+
17
+ struct nk_user_font *nkrb_font_to_nk(VALUE value) {
18
+ VALUE_TO_NK(value, nk);
19
+ return &(nk->font->handle);
20
+ }
21
+
22
+ void nkrb_null_texture_free(struct nk_draw_null_texture *tex) {
23
+ free(tex);
24
+ }
25
+
26
+ VALUE nkrb_font_get_null(VALUE value) {
27
+ VALUE_TO_NK(value, nk);
28
+ struct nk_draw_null_texture *tex = malloc(sizeof(struct nk_draw_null_texture));
29
+ memcpy(tex, &(nk->null), sizeof(struct nk_draw_null_texture));
30
+ return Data_Wrap_Struct(cNuklearNullTexture, NULL, nkrb_null_texture_free, tex);
31
+ }
32
+
33
+ void nkrb_font_free(NkRbFont *font) {
34
+ nk_font_atlas_clear(&(font->atlas));
35
+ }
36
+
37
+ VALUE nkrb_font_width(VALUE self, VALUE rtext) {
38
+ const char *text = StringValueCStr(rtext);
39
+ struct nk_user_font *handle = nkrb_font_to_nk(self);
40
+ float width = handle->width(handle->userdata, handle->height, text, (int) strlen(text));
41
+ return DBL2NUM(width);
42
+ }
43
+
44
+ VALUE nkrb_font_height(VALUE self) {
45
+ struct nk_user_font *handle = nkrb_font_to_nk(self);
46
+ return DBL2NUM(handle->height);
47
+ }
48
+
49
+ VALUE nkrb_font_new(int argc, VALUE *argv, VALUE self) {
50
+ if (argc < 2 || argc > 3)
51
+ rb_raise(rb_eArgError, "Invalid number of arguments (%d for 2 or 3): pass path, size, and an optional arguments hash", argc);
52
+ VALUE path = argv[0];
53
+ VALUE size = argv[1];
54
+ struct nk_font_config config;
55
+ config = nk_font_config((float) NUM2DBL(size));
56
+ if (argc == 3) {
57
+ // TODO support extra font config options
58
+ }
59
+
60
+ NkRbFont *font = malloc(sizeof(NkRbFont));
61
+ memset(font, 0, sizeof(NkRbFont));
62
+ nk_font_atlas_init_default(&(font->atlas));
63
+ nk_font_atlas_begin(&(font->atlas));
64
+ font->font = nk_font_atlas_add_from_file(&(font->atlas), StringValueCStr(path), (float) NUM2DBL(size), NULL);
65
+ if (font->font == NULL)
66
+ rb_raise(rb_eStandardError, "Failed to load font file %s", StringValueCStr(path));
67
+ font->img.data = nk_font_atlas_bake(&(font->atlas), &(font->img.width), &(font->img.height), NK_FONT_ATLAS_RGBA32);
68
+ VALUE bytes = rb_str_new((char *) font->img.data, font->img.width * font->img.height * 4);
69
+ VALUE tex = rb_yield_values(3, INT2FIX(font->img.width), INT2FIX(font->img.height), bytes);
70
+ nk_font_atlas_end(&(font->atlas), nk_handle_ptr((void *) NUM2ULL(tex)), &(font->null));
71
+ return Data_Wrap_Struct(cNuklearFont, NULL, nkrb_font_free, font);
72
+ }
73
+
74
+ void nkrb_font_init(void) {
75
+ cNuklearFont = rb_define_class_under(mNuklear, "Font", rb_cObject);
76
+ cNuklearNullTexture = rb_define_class_under(mNuklear, "NullTexture", rb_cObject);
77
+ rb_define_module_function(cNuklearFont, "new", nkrb_font_new, -1);
78
+ rb_define_method(cNuklearFont, "width", nkrb_font_width, 1);
79
+ rb_define_method(cNuklearFont, "height", nkrb_font_height, 0);
80
+ }
@@ -0,0 +1,114 @@
1
+ #include "nkrb.h"
2
+
3
+ static enum nk_draw_vertex_layout_attribute rb2nk_attribute(VALUE v) {
4
+ ID id = SYM2ID(v);
5
+ if (id == rb_intern("position")) return NK_VERTEX_POSITION;
6
+ else if (id == rb_intern("color")) return NK_VERTEX_COLOR;
7
+ else if (id == rb_intern("texcoord")) return NK_VERTEX_TEXCOORD;
8
+ else if (id == rb_intern("attribute_count")) return NK_VERTEX_ATTRIBUTE_COUNT;
9
+ else rb_raise(rb_eArgError, "Invalid vertex layout attribute name");
10
+ }
11
+
12
+ static enum nk_draw_vertex_layout_format rb2nk_format(VALUE v) {
13
+ ID id = SYM2ID(v);
14
+ if (id == rb_intern("schar")) return NK_FORMAT_SCHAR;
15
+ else if (id == rb_intern("sshort")) return NK_FORMAT_SSHORT;
16
+ else if (id == rb_intern("sint")) return NK_FORMAT_SINT;
17
+ else if (id == rb_intern("uchar")) return NK_FORMAT_UCHAR;
18
+ else if (id == rb_intern("ushort")) return NK_FORMAT_USHORT;
19
+ else if (id == rb_intern("uint")) return NK_FORMAT_UINT;
20
+ else if (id == rb_intern("float")) return NK_FORMAT_FLOAT;
21
+ else if (id == rb_intern("double")) return NK_FORMAT_DOUBLE;
22
+ else if (id == rb_intern("r8g8b8")) return NK_FORMAT_R8G8B8;
23
+ else if (id == rb_intern("r16g15b16")) return NK_FORMAT_R16G15B16;
24
+ else if (id == rb_intern("r32g32b32")) return NK_FORMAT_R32G32B32;
25
+ else if (id == rb_intern("r8g8b8a8")) return NK_FORMAT_R8G8B8A8;
26
+ else if (id == rb_intern("b8g8r8a8")) return NK_FORMAT_B8G8R8A8;
27
+ else if (id == rb_intern("r16g15b16a16")) return NK_FORMAT_R16G15B16A16;
28
+ else if (id == rb_intern("r32g32b32a32")) return NK_FORMAT_R32G32B32A32;
29
+ else if (id == rb_intern("r32g32b32a32_float")) return NK_FORMAT_R32G32B32A32_FLOAT;
30
+ else if (id == rb_intern("r32g32b32a32_double")) return NK_FORMAT_R32G32B32A32_DOUBLE;
31
+ else if (id == rb_intern("rgb32")) return NK_FORMAT_RGB32;
32
+ else if (id == rb_intern("rgba32")) return NK_FORMAT_RGBA32;
33
+ else rb_raise(rb_eArgError, "Invalid vertex layout format name");
34
+ }
35
+
36
+ VALUE nkrb_renderer_convert(VALUE self) {
37
+ struct nk_context *context = NULL;
38
+ struct nk_buffer *commands = NULL;
39
+ struct nk_buffer *vertices = NULL;
40
+ struct nk_buffer *indices = NULL;
41
+ Data_Get_Struct(rb_funcall(self, rb_intern("context"), 0), struct nk_context, context);
42
+ Data_Get_Struct(rb_funcall(self, rb_intern("commands"), 0), struct nk_buffer, commands);
43
+ Data_Get_Struct(rb_funcall(self, rb_intern("vertices"), 0), struct nk_buffer, vertices);
44
+ Data_Get_Struct(rb_funcall(self, rb_intern("vertex_indices"), 0), struct nk_buffer, indices);
45
+ VALUE rconfig = rb_funcall(self, rb_intern("convert_config"), 0);
46
+ VALUE rvertex_layout = rb_hash_aref(rconfig, ID2SYM(rb_intern("vertex_layout")));
47
+ struct nk_convert_config config;
48
+ struct nk_draw_vertex_layout_element *vertex_layout = alloca((RARRAY_LEN(rvertex_layout) + 1) * sizeof(struct nk_draw_vertex_layout_element));
49
+ for (int i = 0; i < RARRAY_LEN(rvertex_layout); i++) {
50
+ VALUE rlayout = RARRAY_AREF(rvertex_layout, i);
51
+ vertex_layout[i].attribute = rb2nk_attribute(RARRAY_AREF(rlayout, 0));
52
+ vertex_layout[i].format = rb2nk_format(RARRAY_AREF(rlayout, 1));
53
+ vertex_layout[i].offset = FIX2INT(RARRAY_AREF(rlayout, 2));
54
+ }
55
+ vertex_layout[RARRAY_LEN(rvertex_layout)].attribute = NK_VERTEX_LAYOUT_END;
56
+ memset(&config, 0, sizeof(config));
57
+ config.vertex_layout = vertex_layout;
58
+ config.vertex_size = FIX2INT(rb_hash_aref(rconfig, ID2SYM(rb_intern("vertex_size"))));
59
+ config.vertex_alignment = FIX2INT(rb_hash_aref(rconfig, ID2SYM(rb_intern("vertex_alignment"))));
60
+ config.null.texture = (nk_handle) (void *) NUM2ULL(rb_hash_aref(rconfig, ID2SYM(rb_intern("null"))));
61
+ config.circle_segment_count = NUM2UINT(rb_hash_aref(rconfig, ID2SYM(rb_intern("circle_segment_count"))));
62
+ config.curve_segment_count = NUM2UINT(rb_hash_aref(rconfig, ID2SYM(rb_intern("curve_segment_count"))));
63
+ config.arc_segment_count = NUM2UINT(rb_hash_aref(rconfig, ID2SYM(rb_intern("arc_segment_count"))));
64
+ config.global_alpha = (float) NUM2DBL(rb_hash_aref(rconfig, ID2SYM(rb_intern("global_alpha"))));
65
+ config.shape_AA = RTEST(rb_hash_aref(rconfig, ID2SYM(rb_intern("shape_aa")))) ? NK_ANTI_ALIASING_ON : NK_ANTI_ALIASING_OFF;
66
+ config.line_AA = RTEST(rb_hash_aref(rconfig, ID2SYM(rb_intern("line_aa")))) ? NK_ANTI_ALIASING_ON : NK_ANTI_ALIASING_OFF;
67
+ // all that for this
68
+ nk_flags result = nk_convert(context, commands, vertices, indices, &config);
69
+ switch(result) {
70
+ case NK_CONVERT_SUCCESS: break; // OK
71
+ case NK_CONVERT_INVALID_PARAM: rb_raise(rb_eRuntimeError, "Nuklear error: INVALID_PARAM");
72
+ case NK_CONVERT_COMMAND_BUFFER_FULL: rb_raise(rb_eRuntimeError, "Nuklear error: COMMAND_BUFFER_FULL");
73
+ case NK_CONVERT_VERTEX_BUFFER_FULL: rb_raise(rb_eRuntimeError, "Nuklear error: VERTEX_BUFFER_FULL");
74
+ case NK_CONVERT_ELEMENT_BUFFER_FULL: rb_raise(rb_eRuntimeError, "Nuklear error: ELEMENT_BUFFER_FULL");
75
+ default: rb_raise(rb_eRuntimeError, "Nuklear error: %d (unknown)", result);
76
+ }
77
+ return self;
78
+ }
79
+
80
+ VALUE nkrb_renderer_draw_foreach(VALUE self) {
81
+ struct nk_context *context = NULL;
82
+ struct nk_buffer *commands = NULL;
83
+ Data_Get_Struct(rb_funcall(self, rb_intern("context"), 0), struct nk_context, context);
84
+ Data_Get_Struct(rb_funcall(self, rb_intern("commands"), 0), struct nk_buffer, commands);
85
+
86
+ /* convert from command queue into draw list and draw to screen */
87
+ const struct nk_draw_command *cmd;
88
+ // const nk_draw_index *offset = NULL;
89
+ /* iterate over and execute each draw command */
90
+ int offset = 0;
91
+ nk_draw_foreach(cmd, context, commands) {
92
+ if (!cmd->elem_count) continue;
93
+ VALUE rcmd = rb_hash_new();
94
+ VALUE rclip_rect = rb_ary_new2(4);
95
+ rb_ary_store(rclip_rect, 0, DBL2NUM(cmd->clip_rect.x));
96
+ rb_ary_store(rclip_rect, 1, DBL2NUM(cmd->clip_rect.y));
97
+ rb_ary_store(rclip_rect, 2, DBL2NUM(cmd->clip_rect.w));
98
+ rb_ary_store(rclip_rect, 3, DBL2NUM(cmd->clip_rect.h));
99
+ rb_hash_aset(rcmd, ID2SYM(rb_intern("element_count")), INT2NUM(cmd->elem_count));
100
+ rb_hash_aset(rcmd, ID2SYM(rb_intern("texture_handle")), ULL2NUM((unsigned long long) cmd->texture.ptr));
101
+ rb_hash_aset(rcmd, ID2SYM(rb_intern("clip_rect")), rclip_rect);
102
+ rb_hash_aset(rcmd, ID2SYM(rb_intern("offset")), INT2NUM(offset));
103
+ rb_yield(rcmd);
104
+ offset += cmd->elem_count;
105
+ }
106
+
107
+ return self;
108
+ }
109
+
110
+ void nkrb_renderer_init(void) {
111
+ VALUE cRenderer = rb_define_class_under(mNuklear, "Renderer", rb_cObject);
112
+ rb_define_method(cRenderer, "nk_convert", nkrb_renderer_convert, 0);
113
+ rb_define_method(cRenderer, "nk_draw_foreach", nkrb_renderer_draw_foreach, 0);
114
+ }
@@ -0,0 +1,61 @@
1
+ #include "nkrb.h"
2
+
3
+ VALUE mNuklearStyle = Qnil;
4
+
5
+ struct nk_style_item *nkrb_style_item(VALUE value) {
6
+ struct nk_style_item *item;
7
+ Data_Get_Struct(value, struct nk_style_item, item);
8
+ return item;
9
+ }
10
+
11
+ void nkrb_style_item_free(struct nk_style_item *item) {
12
+ free(item);
13
+ }
14
+
15
+ void assign_button_style_from_ruby(struct nk_style_button *style, VALUE rstyle) {
16
+ if (!NIL_P(rstyle)) {
17
+ VALUE rnormal = rb_hash_aref(rstyle, ID2SYM(rb_intern("normal")));
18
+ VALUE rhover = rb_hash_aref(rstyle, ID2SYM(rb_intern("hover")));
19
+ VALUE ractive = rb_hash_aref(rstyle, ID2SYM(rb_intern("active")));
20
+ VALUE rborder_color = rb_hash_aref(rstyle, ID2SYM(rb_intern("border_color")));
21
+ VALUE rtext_background = rb_hash_aref(rstyle, ID2SYM(rb_intern("text_background")));
22
+ VALUE rtext_normal = rb_hash_aref(rstyle, ID2SYM(rb_intern("text_normal")));
23
+ VALUE rtext_hover = rb_hash_aref(rstyle, ID2SYM(rb_intern("text_hover")));
24
+ VALUE rtext_active = rb_hash_aref(rstyle, ID2SYM(rb_intern("text_active")));
25
+ VALUE rtext_alignment = rb_hash_aref(rstyle, ID2SYM(rb_intern("text_alignment")));
26
+ VALUE rborder = rb_hash_aref(rstyle, ID2SYM(rb_intern("border")));
27
+ VALUE rrounding = rb_hash_aref(rstyle, ID2SYM(rb_intern("rounding")));
28
+ VALUE rpadding = rb_hash_aref(rstyle, ID2SYM(rb_intern("padding")));
29
+ VALUE rimage_padding = rb_hash_aref(rstyle, ID2SYM(rb_intern("image_padding")));
30
+ VALUE rtouch_padding = rb_hash_aref(rstyle, ID2SYM(rb_intern("touch_padding")));
31
+
32
+ // TODO button style user callbacks
33
+ // nk_handle userdata;
34
+ // void(*draw_begin)(struct nk_command_buffer*, nk_handle userdata);
35
+ // void(*draw_end)(struct nk_command_buffer*, nk_handle userdata);
36
+
37
+ if (!NIL_P(rnormal)) style->normal = *nkrb_style_item(rnormal);
38
+ if (!NIL_P(rhover)) style->hover = *nkrb_style_item(rhover);
39
+ if (!NIL_P(ractive)) style->active = *nkrb_style_item(ractive);
40
+ if (!NIL_P(rborder_color)) { ASSIGN_STYLE_AS_COLOR(style->border_color, nkrb_style_item(rborder_color)); }
41
+ if (!NIL_P(rtext_background)) { ASSIGN_STYLE_AS_COLOR(style->text_background, nkrb_style_item(rtext_background)); }
42
+ if (!NIL_P(rtext_normal)) { ASSIGN_STYLE_AS_COLOR(style->text_normal, nkrb_style_item(rtext_normal)); }
43
+ if (!NIL_P(rtext_hover)) { ASSIGN_STYLE_AS_COLOR(style->text_hover, nkrb_style_item(rtext_hover)); }
44
+ if (!NIL_P(rtext_active)) { ASSIGN_STYLE_AS_COLOR(style->text_active, nkrb_style_item(rtext_active)); }
45
+ if (!NIL_P(rtext_alignment)) style->text_alignment = FIX2INT(rtext_alignment);
46
+ if (!NIL_P(rborder)) style->border = (float) RFLOAT_VALUE(rborder);
47
+ if (!NIL_P(rrounding)) style->rounding = (float) RFLOAT_VALUE(rrounding);
48
+ if (!NIL_P(rpadding)) style->padding = nk_vec2(RFLOAT_VALUE(rb_ary_entry(rpadding, 0)),
49
+ RFLOAT_VALUE(rb_ary_entry(rpadding, 1)));
50
+ if (!NIL_P(rimage_padding)) style->image_padding = nk_vec2(RFLOAT_VALUE(rb_ary_entry(rimage_padding, 0)),
51
+ RFLOAT_VALUE(rb_ary_entry(rimage_padding, 1)));
52
+ if (!NIL_P(rtouch_padding)) style->touch_padding = nk_vec2(RFLOAT_VALUE(rb_ary_entry(rtouch_padding, 0)),
53
+ RFLOAT_VALUE(rb_ary_entry(rtouch_padding, 1)));
54
+ }
55
+ }
56
+
57
+ void nkrb_style_init(void) {
58
+ mNuklearStyle = rb_define_module_under(mNuklear, "Style");
59
+ nkrb_style_color_init();
60
+ nkrb_style_image_init();
61
+ }
@@ -0,0 +1,126 @@
1
+ #include "nkrb.h"
2
+
3
+ #define NK_UNPACK(rvalue, varname) \
4
+ struct nk_style_item *varname; \
5
+ Data_Get_Struct(rvalue, struct nk_style_item, varname);
6
+
7
+
8
+ VALUE cNuklearStyleColor = Qnil;
9
+
10
+ VALUE nkrb_style_color_alloc(VALUE self) {
11
+ struct nk_style_item *style = malloc(sizeof(struct nk_style_item));
12
+ memset(style, 0, sizeof(struct nk_style_item));
13
+ style->type = NK_STYLE_ITEM_COLOR;
14
+ return Data_Wrap_Struct(self, NULL, nkrb_style_item_free, style);
15
+ }
16
+
17
+ VALUE nkrb_style_color_set_red(VALUE self, VALUE r) {
18
+ NK_UNPACK(self, style);
19
+ style->data.color.r = (nk_byte) (NK_SATURATE((float) NUM2DBL(r)) * 255.0f);
20
+ return r;
21
+ }
22
+
23
+ VALUE nkrb_style_color_get_red(VALUE self) {
24
+ NK_UNPACK(self, style);
25
+ return DBL2NUM(style->data.color.r / 255.0);
26
+ }
27
+
28
+ VALUE nkrb_style_color_set_green(VALUE self, VALUE g) {
29
+ NK_UNPACK(self, style);
30
+ style->data.color.g = (nk_byte) (NK_SATURATE((float) NUM2DBL(g)) * 255.0f);
31
+ return g;
32
+ }
33
+
34
+ VALUE nkrb_style_color_get_green(VALUE self) {
35
+ NK_UNPACK(self, style);
36
+ return DBL2NUM(style->data.color.g / 255.0);
37
+ }
38
+
39
+ VALUE nkrb_style_color_set_blue(VALUE self, VALUE b) {
40
+ NK_UNPACK(self, style);
41
+ style->data.color.b = (nk_byte) (NK_SATURATE((float) NUM2DBL(b)) * 255.0f);
42
+ return b;
43
+ }
44
+
45
+ VALUE nkrb_style_color_get_blue(VALUE self) {
46
+ NK_UNPACK(self, style);
47
+ return DBL2NUM(style->data.color.b / 255.0);
48
+ }
49
+
50
+ VALUE nkrb_style_color_set_alpha(VALUE self, VALUE a) {
51
+ NK_UNPACK(self, style);
52
+ style->data.color.a = (nk_byte) (NK_SATURATE((float) NUM2DBL(a)) * 255.0f);
53
+ return a;
54
+ }
55
+
56
+ VALUE nkrb_style_color_get_alpha(VALUE self) {
57
+ NK_UNPACK(self, style);
58
+ return DBL2NUM(style->data.color.a / 255.0);
59
+ }
60
+
61
+ VALUE nkrb_style_color_set_hue(VALUE self, VALUE a) {
62
+ NK_UNPACK(self, style);
63
+ nk_byte tmp[4];
64
+ nk_color_hsva_bv(tmp, style->data.color);
65
+ tmp[0] = (nk_byte) (NK_SATURATE((float) NUM2DBL(a)) * 255.0f);
66
+ style->data.color = nk_hsva_bv(tmp);
67
+ return a;
68
+ }
69
+
70
+ VALUE nkrb_style_color_get_hue(VALUE self) {
71
+ NK_UNPACK(self, style);
72
+ nk_byte tmp[4];
73
+ nk_color_hsva_bv(tmp, style->data.color);
74
+ return DBL2NUM(tmp[0] / 255.0);
75
+ }
76
+
77
+ VALUE nkrb_style_color_set_saturation(VALUE self, VALUE a) {
78
+ NK_UNPACK(self, style);
79
+ nk_byte tmp[4];
80
+ nk_color_hsva_bv(tmp, style->data.color);
81
+ tmp[1] = (nk_byte) (NK_SATURATE((float) NUM2DBL(a)) * 255.0f);
82
+ style->data.color = nk_hsva_bv(tmp);
83
+ return a;
84
+ }
85
+
86
+ VALUE nkrb_style_color_get_saturation(VALUE self) {
87
+ NK_UNPACK(self, style);
88
+ nk_byte tmp[4];
89
+ nk_color_hsva_bv(tmp, style->data.color);
90
+ return DBL2NUM(tmp[1] / 255.0);
91
+ }
92
+
93
+ VALUE nkrb_style_color_set_value(VALUE self, VALUE a) {
94
+ NK_UNPACK(self, style);
95
+ nk_byte tmp[4];
96
+ nk_color_hsva_bv(tmp, style->data.color);
97
+ tmp[3] = (nk_byte) (NK_SATURATE((float) NUM2DBL(a)) * 255.0f);
98
+ style->data.color = nk_hsva_bv(tmp);
99
+ return a;
100
+ }
101
+
102
+ VALUE nkrb_style_color_get_value(VALUE self) {
103
+ NK_UNPACK(self, style);
104
+ nk_byte tmp[4];
105
+ nk_color_hsva_bv(tmp, style->data.color);
106
+ return DBL2NUM(tmp[3] / 255.0);
107
+ }
108
+
109
+ void nkrb_style_color_init(void) {
110
+ cNuklearStyleColor = rb_define_class_under(mNuklearStyle, "Color", rb_cObject);
111
+ rb_define_alloc_func(cNuklearStyleColor, nkrb_style_color_alloc);
112
+ rb_define_method(cNuklearStyleColor, "red=", nkrb_style_color_set_red, 1);
113
+ rb_define_method(cNuklearStyleColor, "red", nkrb_style_color_get_red, 0);
114
+ rb_define_method(cNuklearStyleColor, "green=", nkrb_style_color_set_green, 1);
115
+ rb_define_method(cNuklearStyleColor, "green", nkrb_style_color_get_green, 0);
116
+ rb_define_method(cNuklearStyleColor, "blue=", nkrb_style_color_set_blue, 1);
117
+ rb_define_method(cNuklearStyleColor, "blue", nkrb_style_color_get_blue, 0);
118
+ rb_define_method(cNuklearStyleColor, "hue=", nkrb_style_color_set_hue, 1);
119
+ rb_define_method(cNuklearStyleColor, "hue", nkrb_style_color_get_hue, 0);
120
+ rb_define_method(cNuklearStyleColor, "saturation=", nkrb_style_color_set_saturation, 1);
121
+ rb_define_method(cNuklearStyleColor, "saturation", nkrb_style_color_get_saturation, 0);
122
+ rb_define_method(cNuklearStyleColor, "value=", nkrb_style_color_set_value, 1);
123
+ rb_define_method(cNuklearStyleColor, "value", nkrb_style_color_get_value, 0);
124
+ rb_define_method(cNuklearStyleColor, "alpha=", nkrb_style_color_set_alpha, 1);
125
+ rb_define_method(cNuklearStyleColor, "alpha", nkrb_style_color_get_alpha, 0);
126
+ }
@@ -0,0 +1,32 @@
1
+ #include "nkrb.h"
2
+
3
+ #define NK_UNPACK(rvalue, varname) \
4
+ struct nk_style_item *varname; \
5
+ Data_Get_Struct(rvalue, struct nk_style_item, varname);
6
+
7
+ VALUE cNuklearStyleImage = Qnil;
8
+
9
+ VALUE nkrb_style_image_alloc(VALUE self) {
10
+ struct nk_style_item *style = malloc(sizeof(struct nk_style_item));
11
+ memset(style, 0, sizeof(struct nk_style_item));
12
+ style->type = NK_STYLE_ITEM_IMAGE;
13
+ return Data_Wrap_Struct(self, NULL, nkrb_style_item_free, style);
14
+ }
15
+
16
+ VALUE nkrb_style_image_set_id(VALUE self, VALUE id) {
17
+ NK_UNPACK(self, style);
18
+ style->data.image.handle.id = FIX2INT(id);
19
+ return id;
20
+ }
21
+
22
+ VALUE nkrb_style_image_get_id(VALUE self) {
23
+ NK_UNPACK(self, style);
24
+ return INT2FIX(style->data.image.handle.id);
25
+ }
26
+
27
+ void nkrb_style_image_init(void) {
28
+ cNuklearStyleImage = rb_define_class_under(mNuklearStyle, "Image", rb_cObject);
29
+ rb_define_alloc_func(cNuklearStyleImage, nkrb_style_image_alloc);
30
+ rb_define_method(cNuklearStyleImage, "id=", nkrb_style_image_set_id, 1);
31
+ rb_define_method(cNuklearStyleImage, "id", nkrb_style_image_get_id, 0);
32
+ }
@@ -0,0 +1,32 @@
1
+ #include "nkrb.h"
2
+
3
+ VALUE mNuklearUI = Qnil;
4
+
5
+ void instance_eval_block(VALUE self) {
6
+ rb_need_block();
7
+ rb_funcall_with_block(self, rb_intern("instance_eval"), 0, NULL, rb_block_proc());
8
+ }
9
+
10
+ void nkrb_ui_init(void) {
11
+ mNuklearUI = rb_define_module_under(mNuklear, "UI");
12
+ nkrb_ui_container_init();
13
+
14
+ nkrb_ui_button_init();
15
+ nkrb_ui_checkbox_init();
16
+ nkrb_ui_color_picker_init();
17
+ nkrb_ui_combo_init();
18
+ nkrb_ui_edit_string_init();
19
+ nkrb_ui_group_init();
20
+ nkrb_ui_label_init();
21
+ nkrb_ui_layout_init();
22
+ nkrb_ui_menu_init();
23
+ nkrb_ui_menu_item_init();
24
+ nkrb_ui_menubar_init();
25
+ nkrb_ui_popup_init();
26
+ nkrb_ui_progress_init();
27
+ nkrb_ui_property_init();
28
+ nkrb_ui_slider_init();
29
+ nkrb_ui_tree_init();
30
+ nkrb_ui_widget_init();
31
+ nkrb_ui_window_init();
32
+ }
@@ -0,0 +1,29 @@
1
+ #include "nkrb.h"
2
+
3
+ #define UNPACK_CONTEXT(varname) struct nk_context *varname = nkrb_context_get(rb_ivar_get(self, rb_intern("@context")));
4
+
5
+ VALUE cNuklearUIBuilder = Qnil;
6
+
7
+ VALUE nkrb_ui_initialize(VALUE self, VALUE context) {
8
+ rb_ivar_set(self, rb_intern("@context"), context);
9
+ return self;
10
+ }
11
+
12
+
13
+ VALUE nkrb_ui_list_view_begin(VALUE self, VALUE id, VALUE flags, VALUE row_height, VALUE row_count) {
14
+ UNPACK_CONTEXT(ctx);
15
+ struct nk_list_view view;
16
+ if (nk_list_view_begin(ctx, &view, StringValueCStr(id), FIX2INT(flags), FIX2INT(row_height), FIX2INT(row_count))) {
17
+ if (rb_block_given_p()) {
18
+ instance_eval_block(self);
19
+ }
20
+ nk_list_view_end(&view);
21
+ }
22
+ return self;
23
+ }
24
+
25
+ void nkrb_ui_builder_init(void) {
26
+ cNuklearUIBuilder = rb_define_class_under(mNuklear, "UIBuilder", rb_cObject);
27
+ rb_define_method(cNuklearUIBuilder, "initialize", nkrb_ui_initialize, 1);
28
+ rb_define_method(cNuklearUIBuilder, "list_view", nkrb_ui_list_view_begin, 4);
29
+ }
@@ -0,0 +1,55 @@
1
+ #include "nkrb.h"
2
+
3
+ VALUE nkrb_ui_button(VALUE self, VALUE rcontext, VALUE roptions) {
4
+ struct nk_context *context = nkrb_context_get(rcontext);
5
+ VALUE rrepeat = rb_hash_aref(roptions, ID2SYM(rb_intern("repeat")));
6
+ VALUE rtitle = rb_hash_aref(roptions, ID2SYM(rb_intern("title")));
7
+ VALUE rcolor = rb_hash_aref(roptions, ID2SYM(rb_intern("color")));
8
+ VALUE rsymbol = rb_hash_aref(roptions, ID2SYM(rb_intern("symbol")));
9
+ VALUE rimage = rb_hash_aref(roptions, ID2SYM(rb_intern("image")));
10
+ VALUE rstyle = rb_hash_aref(roptions, ID2SYM(rb_intern("style")));
11
+ VALUE ralign = rb_hash_aref(roptions, ID2SYM(rb_intern("alignment")));
12
+ int alignment = 0;
13
+ if (!NIL_P(ralign)) alignment = FIX2INT(ralign);
14
+
15
+ struct nk_style_button style;
16
+ memcpy(&style, &context->style.button, sizeof(style));
17
+ assign_button_style_from_ruby(&style, rstyle);
18
+
19
+ enum nk_symbol_type symbol = NK_SYMBOL_NONE;
20
+ SET_RSYMBOL(symbol, rsymbol);
21
+
22
+ int repeat = RTEST(rrepeat);
23
+ int result = 0;
24
+ if (repeat) nk_button_set_behavior(context, NK_BUTTON_REPEATER);
25
+ if (NIL_P(rtitle) && symbol == NK_SYMBOL_NONE && NIL_P(rimage)) {
26
+ if (NIL_P(rcolor)) {
27
+ rb_raise(rb_eArgError, "need :title or :symbol, with optional :image, or :color");
28
+ } else {
29
+ struct nk_color color;
30
+ ASSIGN_STYLE_AS_COLOR(color, nkrb_style_item(rcolor));
31
+ result = nk_button_color(context, color);
32
+ }
33
+ } else {
34
+ if (NIL_P(rtitle) && NIL_P(rimage)) {
35
+ result = nk_button_symbol_styled(context, &style, symbol);
36
+ } else if (!NIL_P(rtitle) && NIL_P(rimage)) {
37
+ result = nk_button_symbol_text_styled(context, &style, symbol, RSTRING_PTR(rtitle), (int) RSTRING_LEN(rtitle), alignment);
38
+ } else if (NIL_P(rtitle) && !NIL_P(rimage)) {
39
+ struct nk_image img;
40
+ ASSIGN_STYLE_AS_IMAGE(img, nkrb_style_item(rimage));
41
+ result = nk_button_image_text_styled(context, &style, img, RSTRING_PTR(rtitle), (int) RSTRING_LEN(rtitle), alignment);
42
+ } else {
43
+ struct nk_image img;
44
+ ASSIGN_STYLE_AS_IMAGE(img, nkrb_style_item(rimage));
45
+ result = nk_button_image_styled(context, &style, img);
46
+ }
47
+ }
48
+ if (repeat) nk_button_set_behavior(context, NK_BUTTON_DEFAULT);
49
+
50
+ return INT2FIX(result);
51
+ }
52
+
53
+ void nkrb_ui_button_init(void) {
54
+ rb_define_method(mNuklearUIContainer, "ui_button", nkrb_ui_button, 2);
55
+ }
@@ -0,0 +1,20 @@
1
+ #include "nkrb.h"
2
+
3
+ VALUE nkrb_ui_color_picker(VALUE self, VALUE rcontext, VALUE rcolor, VALUE rtype) {
4
+ struct nk_context *ctx = nkrb_context_get(rcontext);
5
+ enum nk_color_format type = (SYM2ID(rtype) == rb_intern("rgb")) ? NK_RGB : NK_RGBA;
6
+ struct nk_style_item *style = nkrb_style_item(rcolor);
7
+ struct nk_color color;
8
+ ASSIGN_STYLE_AS_COLOR(color, style);
9
+
10
+ if (nk_color_pick(ctx, &color, type)) {
11
+ style->data.color = color;
12
+ return rcolor;
13
+ }
14
+
15
+ return Qnil;
16
+ }
17
+
18
+ void nkrb_ui_color_picker_init(void) {
19
+ rb_define_method(mNuklearUIContainer, "ui_color_picker", nkrb_ui_color_picker, 3);
20
+ }
@@ -0,0 +1,73 @@
1
+ #include "nkrb.h"
2
+
3
+ VALUE nkrb_ui_combo(VALUE self, VALUE rcontext, VALUE rselected_index,
4
+ VALUE rchoices, VALUE ritem_height, VALUE rwidth, VALUE rheight) {
5
+ struct nk_context *context = nkrb_context_get(rcontext);
6
+ float width = (float) NUM2DBL(rwidth);
7
+ float height = (float) NUM2DBL(rheight);
8
+ int item_height = FIX2INT(ritem_height);
9
+ int len = (int) RARRAY_LEN(rchoices);
10
+ int selected_index = FIX2INT(rselected_index);
11
+ int i;
12
+ const char **choices = malloc(len * sizeof(const char *));
13
+ for (i = 0; i < len; i++) {
14
+ VALUE str = rb_funcall(rb_ary_entry(rchoices, i), rb_intern("to_s"), 0);
15
+ choices[i] = StringValueCStr(str);
16
+ }
17
+ int result = nk_combo(context, choices, len, selected_index, item_height, nk_vec2(width, height));
18
+ free(choices);
19
+ return INT2FIX(result);
20
+ }
21
+
22
+ VALUE nkrb_ui_combobox(VALUE self, VALUE rcontext, VALUE rtext, VALUE rcolor,
23
+ VALUE rsymbol, VALUE rimage, VALUE rwidth, VALUE rheight) {
24
+ struct nk_context *context = nkrb_context_get(rcontext);
25
+ float width = (float) NUM2DBL(rwidth);
26
+ float height = (float) NUM2DBL(rheight);
27
+ int result = 0;
28
+ enum nk_symbol_type symbol = NK_SYMBOL_NONE;
29
+ SET_RSYMBOL(symbol, rsymbol);
30
+ struct nk_vec2 size = nk_vec2(width, height);
31
+
32
+ if (!NIL_P(rcolor)) {
33
+ struct nk_color color;
34
+ ASSIGN_STYLE_AS_COLOR(color, nkrb_style_item(rcolor));
35
+ result = nk_combo_begin_color(context, color, nk_vec2(width, height));
36
+ } else if (!NIL_P(rtext)) {
37
+ rtext = rb_funcall(rtext, rb_intern("to_s"), 0);
38
+ const char *text = StringValuePtr(rtext);
39
+ int len = (int) RSTRING_LEN(rtext);
40
+ if (!NIL_P(rsymbol)) {
41
+ result = nk_combo_begin_symbol_text(context, text, len, symbol, size);
42
+ } else if (!NIL_P(rimage)) {
43
+ struct nk_image img;
44
+ ASSIGN_STYLE_AS_IMAGE(img, nkrb_style_item(rimage));
45
+ result = nk_combo_begin_image_text(context, text, len, img, size);
46
+ } else {
47
+ result = nk_combo_begin_text(context, text, len, size);
48
+ }
49
+ } else {
50
+ if (!NIL_P(symbol)) {
51
+ result = nk_combo_begin_symbol(context, symbol, size);
52
+ } else if (!NIL_P(rimage)) {
53
+ struct nk_image img;
54
+ ASSIGN_STYLE_AS_IMAGE(img, nkrb_style_item(rimage));
55
+ result = nk_combo_begin_image(context, img, size);
56
+ } else {
57
+ return Qnil;
58
+ }
59
+ }
60
+
61
+ if (rb_block_given_p())
62
+ rb_yield(result ? Qtrue : Qfalse);
63
+
64
+ if (result)
65
+ nk_combo_end(context);
66
+
67
+ return Qnil;
68
+ }
69
+
70
+ void nkrb_ui_combo_init(void) {
71
+ rb_define_method(mNuklearUIContainer, "ui_combobox", nkrb_ui_combobox, 7);
72
+ rb_define_method(mNuklearUIContainer, "ui_combo", nkrb_ui_combo, 6);
73
+ }
@@ -0,0 +1,7 @@
1
+ #include "nkrb.h"
2
+
3
+ VALUE mNuklearUIContainer = Qnil;
4
+
5
+ void nkrb_ui_container_init(void) {
6
+ mNuklearUIContainer = rb_define_module_under(mNuklearUI, "Container");
7
+ }
@@ -0,0 +1,38 @@
1
+ #include "nkrb.h"
2
+
3
+ VALUE nkrb_ui_edit_string(VALUE self, VALUE context, VALUE rflags, VALUE rbuffer, VALUE max_length, VALUE rfilter) {
4
+ Check_Type(rbuffer, T_STRING);
5
+ rfilter = rb_funcall(rfilter, rb_intern("to_s"), 0);
6
+ int buffer_size = FIX2INT(max_length) + 1;
7
+ char *buffer = malloc(buffer_size);
8
+ memset(buffer, 0, buffer_size);
9
+ char *ptr = RSTRING_PTR(rbuffer);
10
+ int rbuffer_len = (int) RSTRING_LEN(rbuffer);
11
+ if (rbuffer_len > buffer_size - 1) rbuffer_len = buffer_size - 1;
12
+ memcpy(buffer, ptr, rbuffer_len);
13
+ nk_plugin_filter filter = nk_filter_default;
14
+ if (!strcmp(StringValueCStr(rfilter), "ascii")) filter = nk_filter_ascii;
15
+ if (!strcmp(StringValueCStr(rfilter), "float")) filter = nk_filter_float;
16
+ if (!strcmp(StringValueCStr(rfilter), "decimal")) filter = nk_filter_decimal;
17
+ if (!strcmp(StringValueCStr(rfilter), "hex")) filter = nk_filter_hex;
18
+ if (!strcmp(StringValueCStr(rfilter), "octal")) filter = nk_filter_oct;
19
+ if (!strcmp(StringValueCStr(rfilter), "binary")) filter = nk_filter_binary;
20
+ int start_len = rbuffer_len;
21
+ nk_flags result = nk_edit_string(nkrb_context_get(context), FIX2INT(rflags), buffer, &rbuffer_len, buffer_size - 1, filter);
22
+ if (rbuffer_len != start_len || memcmp(buffer, ptr, rbuffer_len)) {
23
+ rb_funcall(rbuffer, rb_intern("clear"), 0);
24
+ rb_str_cat(rbuffer, buffer, rbuffer_len);
25
+ }
26
+ free(buffer);
27
+ return INT2FIX(result);
28
+ }
29
+
30
+ VALUE nkrb_ui_edit_focus(VALUE self, VALUE context, VALUE rflags) {
31
+ nk_edit_focus(nkrb_context_get(context), FIX2INT(rflags));
32
+ return Qnil;
33
+ }
34
+
35
+ void nkrb_ui_edit_string_init(void) {
36
+ rb_define_method(mNuklearUIContainer, "ui_edit_string", nkrb_ui_edit_string, 5);
37
+ rb_define_method(mNuklearUIContainer, "ui_edit_focus", nkrb_ui_edit_focus, 2);
38
+ }