コード例 #1
0
ファイル: Property.cs プロジェクト: bradder555/NuklearSharp
        public static void DrawProperty(this CommandBuffer _out_, nk_style_property style, RectangleF *bounds,
                                        RectangleF *label, uint state, string name, nk_font font)
        {
            var           text = new nk_text();
            nk_style_item background;

            if ((state & NK_WIDGET_STATE_ACTIVED) != 0)
            {
                background = style.active;
                text.text  = style.label_active;
            }
            else if ((state & NK_WIDGET_STATE_HOVER) != 0)
            {
                background = style.hover;
                text.text  = style.label_hover;
            }
            else
            {
                background = style.normal;
                text.text  = style.label_normal;
            }

            if (background.type == NK_STYLE_ITEM_IMAGE)
            {
                _out_.DrawImage(*bounds, background.Image, nk_white);
                text.Background = Color.Transparent;
            }
            else
            {
                text.Background = background.Color;
                _out_.FillRect(*bounds, style.Rounding, background.Color);
                _out_.StrokeRect(*bounds, style.Rounding, style.border,
                                 background.Color);
            }

            text.padding = new Vector2(0, 0);
            _out_.WidgetText(*label, name, &text, nk_text_align.NK_TEXT_CENTERED, font);
        }
コード例 #2
0
ファイル: Property.cs プロジェクト: bradder555/NuklearSharp
        public static void DoProperty(ref uint ws, CommandBuffer _out_, RectangleF property, string name,
                                      nk_property_variant *variant, float inc_per_pixel, ref string buffer, ref int state, ref int cursor,
                                      ref int select_begin, ref int select_end, nk_style_property style, int filter, nk_input _in_,
                                      nk_font font,
                                      nk_text_edit text_edit, int behavior)
        {
            var filters = new NkPluginFilter[2];

            filters[0] = nk_filter_decimal;
            filters[1] = nk_filter_float;

            bool   active;
            int    old;
            float  size;
            string dst   = null;
            var    left  = new RectangleF();
            var    right = new RectangleF();
            var    label = new RectangleF();
            var    edit  = new RectangleF();
            var    empty = new RectangleF();

            left.Height  = font.Size / 2;
            left.Width   = left.Height;
            left.X       = property.X + style.border + style.padding.X;
            left.Y       = property.Y + style.border + property.Height / 2.0f - left.Height / 2;
            size         = font.width(name);
            label.X      = left.X + left.Width + style.padding.X;
            label.Width  = size + 2 * style.padding.X;
            label.Y      = property.Y + style.border + style.padding.Y;
            label.Height = property.Height - (2 * style.border + 2 * style.padding.Y);
            right.Y      = left.Y;
            right.Width  = left.Width;
            right.Height = left.Height;
            right.X      = property.X + property.Width - (right.Width + style.padding.X);
            if (state == NK_PROPERTY_EDIT)
            {
                size = font.width(buffer);

                size += style.edit.Cursor_size;
                dst   = buffer;
            }
            else
            {
                var _string_ = string.Empty;
                switch (variant->kind)
                {
                case NK_PROPERTY_INT:
                    _string_ = variant->value.i.ToString();
                    break;

                case NK_PROPERTY_FLOAT:
                    _string_ = variant->value.f.ToString();
                    break;

                case NK_PROPERTY_DOUBLE:
                    _string_ = variant->value.d.ToString();
                    break;
                }

                size = font.width(_string_);
                dst  = _string_;
            }

            edit.Width = size + 2 * style.padding.X;
            edit.Width = edit.Width < right.X - (label.X + label.Width)
                                ? edit.Width
                                : right.X - (label.X + label.Width);
            edit.X       = right.X - (edit.Width + style.padding.X);
            edit.Y       = property.Y + style.border;
            edit.Height  = property.Height - 2 * style.border;
            empty.Width  = edit.X - (label.X + label.Width);
            empty.X      = label.X + label.Width;
            empty.Y      = property.Y;
            empty.Height = property.Height;
            old          = state == NK_PROPERTY_EDIT ? 1 : 0;
            PropertyBehavior(ref ws, _in_, property, label, edit,
                             empty,
                             ref state, variant, inc_per_pixel);
            if (style.draw_begin != null)
            {
                style.draw_begin(_out_);
            }
            _out_.DrawProperty(style, &property, &label, ws, name, font);
            if (style.draw_end != null)
            {
                style.draw_end(_out_);
            }
            if (
                DoButtonSymbol(ref ws, _out_, left, style.sym_left, behavior,
                               style.dec_button,
                               _in_, font))
            {
                switch (variant->kind)
                {
                case NK_PROPERTY_INT:
                    variant->value.i =
                        (variant->value.i - variant->step.i < variant->max_value.i
                                                                ? variant->value.i - variant->step.i
                                                                : variant->max_value.i) < variant->min_value.i
                                                                ? variant->min_value.i
                                                                : variant->value.i - variant->step.i < variant->max_value.i
                                                                        ? variant->value.i - variant->step.i
                                                                        : variant->max_value.i;
                    break;

                case NK_PROPERTY_FLOAT:
                    variant->value.f =
                        (variant->value.f - variant->step.f < variant->max_value.f
                                                                ? variant->value.f - variant->step.f
                                                                : variant->max_value.f) < variant->min_value.f
                                                                ? variant->min_value.f
                                                                : variant->value.f - variant->step.f < variant->max_value.f
                                                                        ? variant->value.f - variant->step.f
                                                                        : variant->max_value.f;
                    break;

                case NK_PROPERTY_DOUBLE:
                    variant->value.d =
                        (variant->value.d - variant->step.d < variant->max_value.d
                                                                ? variant->value.d - variant->step.d
                                                                : variant->max_value.d) < variant->min_value.d
                                                                ? variant->min_value.d
                                                                : variant->value.d - variant->step.d < variant->max_value.d
                                                                        ? variant->value.d - variant->step.d
                                                                        : variant->max_value.d;
                    break;
                }
            }

            if (
                DoButtonSymbol(ref ws, _out_, right, style.sym_right, behavior,
                               style.inc_button,
                               _in_, font))
            {
                switch (variant->kind)
                {
                case NK_PROPERTY_INT:
                    variant->value.i =
                        (variant->value.i + variant->step.i < variant->max_value.i
                                                                ? variant->value.i + variant->step.i
                                                                : variant->max_value.i) < variant->min_value.i
                                                                ? variant->min_value.i
                                                                : variant->value.i + variant->step.i < variant->max_value.i
                                                                        ? variant->value.i + variant->step.i
                                                                        : variant->max_value.i;
                    break;

                case NK_PROPERTY_FLOAT:
                    variant->value.f =
                        (variant->value.f + variant->step.f < variant->max_value.f
                                                                ? variant->value.f + variant->step.f
                                                                : variant->max_value.f) < variant->min_value.f
                                                                ? variant->min_value.f
                                                                : variant->value.f + variant->step.f < variant->max_value.f
                                                                        ? variant->value.f + variant->step.f
                                                                        : variant->max_value.f;
                    break;

                case NK_PROPERTY_DOUBLE:
                    variant->value.d =
                        (variant->value.d + variant->step.d < variant->max_value.d
                                                                ? variant->value.d + variant->step.d
                                                                : variant->max_value.d) < variant->min_value.d
                                                                ? variant->min_value.d
                                                                : variant->value.d + variant->step.d < variant->max_value.d
                                                                        ? variant->value.d + variant->step.d
                                                                        : variant->max_value.d;
                    break;
                }
            }

            if (old != NK_PROPERTY_EDIT && state == NK_PROPERTY_EDIT)
            {
                buffer = dst;
                cursor = buffer != null ? buffer.Length : 0;
                active = false;
            }
            else
            {
                active = state == NK_PROPERTY_EDIT;
            }

            nk_textedit_clear_state(text_edit, NK_TEXT_EDIT_SINGLE_LINE, filters[filter]);
            text_edit.Active = active;

            text_edit.Text.str = dst;

            var length = dst != null ? dst.Length : 0;

            text_edit.Cursor =
                (cursor < length ? cursor : length) < 0
                                        ? 0
                                        : cursor < length
                                                ? cursor
                                                : length;
            text_edit.SelectionStart =
                (select_begin < length ? select_begin : length) < 0
                                        ? 0
                                        : select_begin < length
                                                ? select_begin
                                                : length;
            text_edit.SelectionEnd =
                (select_end < length ? select_end : length) < 0
                                        ? 0
                                        : select_end < length
                                                ? select_end
                                                : length;
            text_edit.Mode = NK_TEXT_EDIT_MODE_INSERT;
            DoEdit(ref ws, _out_, edit, NK_EDIT_FIELD | NK_EDIT_AUTO_SELECT,
                   filters[filter], text_edit,
                   style.edit, state == NK_PROPERTY_EDIT ? _in_ : null, font);
            cursor       = text_edit.Cursor;
            select_begin = text_edit.SelectionStart;
            select_end   = text_edit.SelectionEnd;
            if (text_edit.Active && nk_input_is_key_pressed(_in_, NK_KEY_ENTER))
            {
                text_edit.Active = false;
            }
            if (active && !text_edit.Active)
            {
                state = NK_PROPERTY_DEFAULT;

                switch (variant->kind)
                {
                case NK_PROPERTY_INT:
                    int.TryParse(buffer, out variant->value.i);
                    variant->value.i =
                        (variant->value.i < variant->max_value.i
                                                                ? variant->value.i
                                                                : variant->max_value.i) <
                        variant->min_value.i
                                                                ? variant->min_value.i
                                                                : variant->value.i < variant->max_value.i
                                                                        ? variant->value.i
                                                                        : variant->max_value.i;
                    break;

                case NK_PROPERTY_FLOAT:
                    float.TryParse(buffer, out variant->value.f);
                    variant->value.f =
                        (variant->value.f < variant->max_value.f
                                                                ? variant->value.f
                                                                : variant->max_value.f) <
                        variant->min_value.f
                                                                ? variant->min_value.f
                                                                : variant->value.f < variant->max_value.f
                                                                        ? variant->value.f
                                                                        : variant->max_value.f;
                    break;

                case NK_PROPERTY_DOUBLE:
                    double.TryParse(buffer, out variant->value.d);
                    variant->value.d =
                        (variant->value.d < variant->max_value.d
                                                                ? variant->value.d
                                                                : variant->max_value.d) <
                        variant->min_value.d
                                                                ? variant->min_value.d
                                                                : variant->value.d < variant->max_value.d
                                                                        ? variant->value.d
                                                                        : variant->max_value.d;
                    break;
                }
            }
        }