Module: Nuklear::UI::Container

Included in:
Context, Col, ComboBox, Group, Menu, MenuBar, MenuItem, OptionGroup, Row, SelectList, Tree, Window
Defined in:
lib/nuklear/ui/container.rb,
ext/nuklear/nkrb_ui_container.c

Defined Under Namespace

Classes: LayoutRowDynamic

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/nuklear/ui/container.rb', line 71

def method_missing(name, *args)
  if respond_to?(:"ui_#{name}")
    commands << [:"ui_#{name}", *args]
  else
    super
  end
end

Instance Method Details

#<<(other) ⇒ Object



34
35
36
# File 'lib/nuklear/ui/container.rb', line 34

def <<(other)
  commands << other
end

#commandsObject



4
5
6
# File 'lib/nuklear/ui/container.rb', line 4

def commands
  @commands ||= []
end

#find(&block) ⇒ Object

Traverses this container and any of its sub-containers, yielding each item to the block. Returns each item for which the block returned true.



53
54
55
56
57
58
59
# File 'lib/nuklear/ui/container.rb', line 53

def find(&block)
  matches = []
  traverse do |item|
    matches << item if yield(item)
  end
  return matches
end

#layout_row_dynamic(height, ncols) ⇒ Object



67
68
69
# File 'lib/nuklear/ui/container.rb', line 67

def layout_row_dynamic(height, ncols)
  LayoutRowDynamic.new(height, ncols).tap { |row| commands << row }
end

#run_command(context, command) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nuklear/ui/container.rb', line 8

def run_command(context, command)
  if command.respond_to?(:to_command)
    if !command.respond_to?(:enabled?) || command.enabled?
      cmd = command.to_command
      result = send(cmd[0], context, *cmd[1..-1]) do |ret|
        command.result(ret, context) if command.respond_to?(:result)
      end
      command.result(result, context) if !result.nil? && command.respond_to?(:result)
    end
  elsif command.respond_to?(:[])
    send(command[0], context, *command[1..-1])
  else
    raise "Unexpected command #{command.inspect}"
  end
end

#run_commands(context) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/nuklear/ui/container.rb', line 24

def run_commands(context)
  commands.each do |command|
    if command.respond_to?(:to_commands)
      command.to_commands.each { |cmd| run_command(context, cmd) }
    else
      run_command(context, command)
    end
  end
end

#traverse {|_self| ... } ⇒ Object

Iterates over every item in this container, and yields each item (starting with the container itself) to the block.

Yields:

  • (_self)

Yield Parameters:



40
41
42
43
44
45
46
47
48
49
# File 'lib/nuklear/ui/container.rb', line 40

def traverse(&block)
  yield self
  commands.each do |item|
    if item.respond_to?(:traverse)
      item.traverse(&block)
    else
      yield item
    end
  end
end

#ui_checkbox(rcontext, rtext, ris_checked) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'ext/nuklear/nkrb_ui_selectables.c', line 3

VALUE nkrb_ui_checkbox(VALUE self, VALUE rcontext, VALUE rtext, VALUE ris_checked) {
  struct nk_context *context = nkrb_context_get(rcontext);
  const char *text = StringValueCStr(rtext);
  int is_checked = RTEST(ris_checked) ? 1 : 0;
  if (nk_checkbox_label(context, text, &is_checked)) {
    return is_checked ? Qtrue : Qfalse;
  }
  return Qnil;
}

#ui_color_picker(rcontext, rcolor, rtype) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'ext/nuklear/nkrb_ui_color_picker.c', line 3

VALUE nkrb_ui_color_picker(VALUE self, VALUE rcontext, VALUE rcolor, VALUE rtype) {
  struct nk_context *ctx = nkrb_context_get(rcontext);
  enum nk_color_format type = (SYM2ID(rtype) == rb_intern("rgb")) ? NK_RGB : NK_RGBA;
  struct nk_style_item *style = nkrb_style_item(rcolor);
  struct nk_color color;
  ASSIGN_STYLE_AS_COLOR(color, style);

  if (nk_color_pick(ctx, &color, type)) {
    style->data.color = color;
    return rcolor;
  }

  return Qnil;
}

#ui_combo(rcontext, rselected_index, rchoices, ritem_height, rwidth, rheight) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'ext/nuklear/nkrb_ui_combo.c', line 3

VALUE nkrb_ui_combo(VALUE self, VALUE rcontext, VALUE rselected_index,
                    VALUE rchoices, VALUE ritem_height, VALUE rwidth, VALUE rheight) {
  struct nk_context *context = nkrb_context_get(rcontext);
  float width = (float) NUM2DBL(rwidth);
  float height = (float) NUM2DBL(rheight);
  int item_height = FIX2INT(ritem_height);
  int len = (int) RARRAY_LEN(rchoices);
  int selected_index = FIX2INT(rselected_index);
  int i;
  const char **choices = malloc(len * sizeof(const char *));
  for (i = 0; i < len; i++) {
    VALUE str = rb_funcall(rb_ary_entry(rchoices, i), rb_intern("to_s"), 0);
    choices[i] = StringValueCStr(str);
  }
  int result = nk_combo(context, choices, len, selected_index, item_height, nk_vec2(width, height));
  free(choices);
  return INT2FIX(result);
}

#ui_edit_focus(context, rflags) ⇒ Object



30
31
32
33
# File 'ext/nuklear/nkrb_ui_edit_string.c', line 30

VALUE nkrb_ui_edit_focus(VALUE self, VALUE context, VALUE rflags) {
  nk_edit_focus(nkrb_context_get(context), FIX2INT(rflags));
  return Qnil;
}

#ui_edit_string(context, rflags, rbuffer, max_length, rfilter) ⇒ Object



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
# File 'ext/nuklear/nkrb_ui_edit_string.c', line 3

VALUE nkrb_ui_edit_string(VALUE self, VALUE context, VALUE rflags, VALUE rbuffer, VALUE max_length, VALUE rfilter) {
  Check_Type(rbuffer, T_STRING);
  rfilter = rb_funcall(rfilter, rb_intern("to_s"), 0);
  int buffer_size = FIX2INT(max_length) + 1;
  char *buffer = malloc(buffer_size);
  memset(buffer, 0, buffer_size);
  char *ptr = RSTRING_PTR(rbuffer);
  int rbuffer_len = (int) RSTRING_LEN(rbuffer);
  if (rbuffer_len > buffer_size - 1) rbuffer_len = buffer_size - 1;
  memcpy(buffer, ptr, rbuffer_len);
  nk_plugin_filter filter = nk_filter_default;
  if (!strcmp(StringValueCStr(rfilter), "ascii"))   filter = nk_filter_ascii;
  if (!strcmp(StringValueCStr(rfilter), "float"))   filter = nk_filter_float;
  if (!strcmp(StringValueCStr(rfilter), "decimal")) filter = nk_filter_decimal;
  if (!strcmp(StringValueCStr(rfilter), "hex"))     filter = nk_filter_hex;
  if (!strcmp(StringValueCStr(rfilter), "octal"))   filter = nk_filter_oct;
  if (!strcmp(StringValueCStr(rfilter), "binary"))  filter = nk_filter_binary;
  int start_len = rbuffer_len;
  nk_flags result = nk_edit_string(nkrb_context_get(context), FIX2INT(rflags), buffer, &rbuffer_len, buffer_size - 1, filter);
  if (rbuffer_len != start_len || memcmp(buffer, ptr, rbuffer_len)) {
    rb_funcall(rbuffer, rb_intern("clear"), 0);
    rb_str_cat(rbuffer, buffer, rbuffer_len);
  }
  free(buffer);
  return INT2FIX(result);
}

#ui_layout_row_begin(context, format, row_height, ncols) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'ext/nuklear/nkrb_ui_layout.c', line 29

VALUE nkrb_ui_layout_row_begin(VALUE self, VALUE context, VALUE format, VALUE row_height, VALUE ncols) {
  struct nk_context *ctx = nkrb_context_get(context);
  enum nk_layout_format fmt = NK_DYNAMIC;
  if (SYM2ID(format) == rb_intern("static")) fmt = NK_STATIC;
  nk_layout_row_begin(ctx, fmt, (float) NUM2DBL(row_height), FIX2INT(ncols));
  if (rb_block_given_p()) {
    instance_eval_block(Qtrue);
  }
  nk_layout_row_end(ctx);
  return Qnil;
}

#ui_layout_row_dynamic(context, height, ncols) ⇒ Object



19
20
21
22
# File 'ext/nuklear/nkrb_ui_layout.c', line 19

VALUE nkrb_ui_layout_row_dynamic(VALUE self, VALUE context, VALUE height, VALUE ncols) {
  nk_layout_row_dynamic(nkrb_context_get(context), (float) NUM2DBL(height), FIX2INT(ncols));
  return Qtrue;
}

#ui_layout_row_push(context, value) ⇒ Object



41
42
43
44
# File 'ext/nuklear/nkrb_ui_layout.c', line 41

VALUE nkrb_ui_layout_row_push(VALUE self, VALUE context, VALUE value) {
  nk_layout_row_push(nkrb_context_get(context), (float) NUM2DBL(value));
  return Qtrue;
}

#ui_layout_row_static(context, height, col_width, ncols) ⇒ Object



24
25
26
27
# File 'ext/nuklear/nkrb_ui_layout.c', line 24

VALUE nkrb_ui_layout_row_static(VALUE self, VALUE context, VALUE height, VALUE col_width, VALUE ncols) {
  nk_layout_row_static(nkrb_context_get(context), (float) NUM2DBL(height), FIX2INT(col_width), FIX2INT(ncols));
  return Qtrue;
}

#ui_layout_row_template_begin(context, height) ⇒ Object



46
47
48
49
# File 'ext/nuklear/nkrb_ui_layout.c', line 46

VALUE nkrb_ui_layout_row_template_begin(VALUE self, VALUE context, VALUE height) {
  nk_layout_row_template_begin(nkrb_context_get(context), (float) NUM2DBL(height));
  return Qnil;
}

#ui_layout_row_template_dynamic(context) ⇒ Object



51
52
53
54
# File 'ext/nuklear/nkrb_ui_layout.c', line 51

VALUE nkrb_ui_layout_row_template_push_dynamic(VALUE self, VALUE context) {
  nk_layout_row_template_push_dynamic(nkrb_context_get(context));
  return Qnil;
}

#ui_layout_row_template_end(context) ⇒ Object



66
67
68
69
# File 'ext/nuklear/nkrb_ui_layout.c', line 66

VALUE nkrb_ui_layout_row_template_end(VALUE self, VALUE context) {
  nk_layout_row_template_end(nkrb_context_get(context));
  return Qnil;
}

#ui_layout_row_template_static(context, width) ⇒ Object



61
62
63
64
# File 'ext/nuklear/nkrb_ui_layout.c', line 61

VALUE nkrb_ui_layout_row_template_push_static(VALUE self, VALUE context, VALUE width) {
  nk_layout_row_template_push_static(nkrb_context_get(context), (float) NUM2DBL(width));
  return Qnil;
}

#ui_layout_row_template_variable(context, min_width) ⇒ Object



56
57
58
59
# File 'ext/nuklear/nkrb_ui_layout.c', line 56

VALUE nkrb_ui_layout_row_template_push_variable(VALUE self, VALUE context, VALUE min_width) {
  nk_layout_row_template_push_variable(nkrb_context_get(context), (float) NUM2DBL(min_width));
  return Qnil;
}

#ui_layout_space_begin(context, format, height, widget_count) ⇒ Object



71
72
73
74
75
76
# File 'ext/nuklear/nkrb_ui_layout.c', line 71

VALUE nkrb_ui_layout_space_begin(VALUE self, VALUE context, VALUE format, VALUE height, VALUE widget_count) {
  enum nk_layout_format fmt = NK_DYNAMIC;
  if (SYM2ID(format) == rb_intern("static")) fmt = NK_STATIC;
  nk_layout_space_begin(nkrb_context_get(context), fmt, (float) NUM2DBL(height), FIX2INT(widget_count));
  return Qnil;
}

#ui_layout_space_end(context) ⇒ Object



95
96
97
98
# File 'ext/nuklear/nkrb_ui_layout.c', line 95

VALUE nkrb_ui_layout_space_end(VALUE self, VALUE context) {
  nk_layout_space_end(nkrb_context_get(context));
  return Qnil;
}

#ui_layout_space_push(context, rbounds) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/nuklear/nkrb_ui_layout.c', line 78

VALUE nkrb_ui_layout_space_push(VALUE self, VALUE context, VALUE rbounds) {
  VALUE left   = rb_hash_aref(rbounds, ID2SYM(rb_intern("left")));
  VALUE top    = rb_hash_aref(rbounds, ID2SYM(rb_intern("top")));
  VALUE width  = rb_hash_aref(rbounds, ID2SYM(rb_intern("width")));
  VALUE height = rb_hash_aref(rbounds, ID2SYM(rb_intern("height")));
  if (NIL_P(left))   rb_raise(rb_eArgError, ":left option is required");
  if (NIL_P(top))    rb_raise(rb_eArgError, ":top option is required");
  if (NIL_P(width))  rb_raise(rb_eArgError, ":width option is required");
  if (NIL_P(height)) rb_raise(rb_eArgError, ":height option is required");
  float x = (float) NUM2DBL(left);
  float y = (float) NUM2DBL(top);
  float w = (float) NUM2DBL(width);
  float h = (float) NUM2DBL(height);
  nk_layout_space_push(nkrb_context_get(context), nk_rect(x, y, w, h));
  return Qnil;
}

#ui_layout_widget_bounds(context) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'ext/nuklear/nkrb_ui_layout.c', line 100

VALUE nkrb_ui_layout_widget_bounds(VALUE self, VALUE context) {
  struct nk_rect bounds = nk_layout_widget_bounds(nkrb_context_get(context));
  VALUE ret = rb_hash_new();
  rb_hash_aset(ret, ID2SYM(rb_intern("left")),   bounds.x);
  rb_hash_aset(ret, ID2SYM(rb_intern("top")),    bounds.y);
  rb_hash_aset(ret, ID2SYM(rb_intern("width")),  bounds.w);
  rb_hash_aset(ret, ID2SYM(rb_intern("height")), bounds.h);
  return Qnil;
}

#ui_menu_item(rcontext, roptions) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'ext/nuklear/nkrb_ui_menu_item.c', line 3

VALUE nkrb_ui_menu_item(VALUE self, VALUE rcontext, VALUE roptions) {
  struct nk_context *context = nkrb_context_get(rcontext);

  VALUE rtitle = rb_funcall(rb_hash_aref(roptions, ID2SYM(rb_intern("title"))), rb_intern("to_s"), 0);
  VALUE rimage = rb_hash_aref(roptions, ID2SYM(rb_intern("image")));
  VALUE rsymbol = rb_hash_aref(roptions, ID2SYM(rb_intern("symbol")));
  nk_flags align = FIX2INT(rb_hash_aref(roptions, ID2SYM(rb_intern("align"))));

  enum nk_symbol_type symbol = NK_SYMBOL_NONE;
  SET_RSYMBOL(symbol, rsymbol);

  int result = 0;
  if (!NIL_P(rimage)) {
    struct nk_image img;
    ASSIGN_STYLE_AS_IMAGE(img, nkrb_style_item(rimage))
    result = nk_menu_item_image_text(context, img, RSTRING_PTR(rtitle), (int) RSTRING_LEN(rtitle), align);
  } else if (!NIL_P(rsymbol)) {
    result = nk_menu_item_symbol_text(context, symbol, RSTRING_PTR(rtitle), (int) RSTRING_LEN(rtitle), align);
  } else {
    result = nk_menu_item_text(context, RSTRING_PTR(rtitle), (int) RSTRING_LEN(rtitle), align);
  }

  return result ? ID2SYM(rb_intern("clicked")) : Qnil;
}

#ui_option(rcontext, rtext, ris_checked) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'ext/nuklear/nkrb_ui_selectables.c', line 13

VALUE nkrb_ui_option(VALUE self, VALUE rcontext, VALUE rtext, VALUE ris_checked) {
  struct nk_context *context = nkrb_context_get(rcontext);
  const char *text = StringValueCStr(rtext);
  int is_checked = RTEST(ris_checked) ? 1 : 0;
  if (nk_option_label(context, text, is_checked)) {
    return Qtrue;
  } else {
    return Qfalse;
  }
}

#ui_selectable(rcontext, rimage, rtext, rselected, ralign) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'ext/nuklear/nkrb_ui_selectables.c', line 24

VALUE nkrb_ui_selectable(VALUE self, VALUE rcontext, VALUE rimage, VALUE rtext, VALUE rselected, VALUE ralign) {
  struct nk_context *context = nkrb_context_get(rcontext);
  int align = FIX2INT(ralign);
  int selected = RTEST(rselected) ? 1 : 0;
  int result;
  if (NIL_P(rtext)) return Qnil;

  const char *text = StringValuePtr(rtext);
  int len = (int) RSTRING_LEN(rtext);

  if (NIL_P(rimage))
    result = nk_selectable_text(context, text, len, align, &selected);
  else {
    struct nk_image img;
    ASSIGN_STYLE_AS_IMAGE(img, nkrb_style_item(rimage))
    result = nk_selectable_image_text(context, img, text, len, align, &selected);
  }

  if (result) {
    return selected ? Qtrue : Qfalse;
  }

  return Qnil;
}

#ui_window_close(context, rid) ⇒ Object



33
34
35
36
37
38
# File 'ext/nuklear/nkrb_ui_window.c', line 33

VALUE nkrb_ui_window_close(VALUE self, VALUE context, VALUE rid) {
  struct nk_context *ctx = nkrb_context_get(context);
  const char *id = StringValueCStr(rid);
  nk_window_close(ctx, id);
  return Qnil;
}