Blame src-nuklear/nuklear_text.c

b53a5c
#include "nuklear.h"
b53a5c
#include "nuklear_internal.h"
b53a5c
b53a5c
/* ===============================================================
b53a5c
 *
b53a5c
 *                              TEXT
b53a5c
 *
b53a5c
 * ===============================================================*/
b53a5c
NK_LIB void
b53a5c
nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
b53a5c
    const char *string, int len, const struct nk_text *t,
b53a5c
    nk_flags a, const struct nk_user_font *f)
b53a5c
{
b53a5c
    struct nk_rect label;
b53a5c
    float text_width;
b53a5c
b53a5c
    NK_ASSERT(o);
b53a5c
    NK_ASSERT(t);
b53a5c
    if (!o || !t) return;
b53a5c
b53a5c
    b.h = NK_MAX(b.h, 2 * t->padding.y);
b53a5c
    label.x = 0; label.w = 0;
b53a5c
    label.y = b.y + t->padding.y;
b53a5c
    label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
b53a5c
b53a5c
    text_width = f->width(f->userdata, f->height, (const char*)string, len);
b53a5c
    text_width += (2.0f * t->padding.x);
b53a5c
b53a5c
    /* align in x-axis */
b53a5c
    if (a & NK_TEXT_ALIGN_LEFT) {
b53a5c
        label.x = b.x + t->padding.x;
b53a5c
        label.w = NK_MAX(0, b.w - 2 * t->padding.x);
b53a5c
    } else if (a & NK_TEXT_ALIGN_CENTERED) {
b53a5c
        label.w = NK_MAX(1, 2 * t->padding.x + (float)text_width);
b53a5c
        label.x = (b.x + t->padding.x + ((b.w - 2 * t->padding.x) - label.w) / 2);
b53a5c
        label.x = NK_MAX(b.x + t->padding.x, label.x);
b53a5c
        label.w = NK_MIN(b.x + b.w, label.x + label.w);
b53a5c
        if (label.w >= label.x) label.w -= label.x;
b53a5c
    } else if (a & NK_TEXT_ALIGN_RIGHT) {
b53a5c
        label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
b53a5c
        label.w = (float)text_width + 2 * t->padding.x;
b53a5c
    } else return;
b53a5c
b53a5c
    /* align in y-axis */
b53a5c
    if (a & NK_TEXT_ALIGN_MIDDLE) {
b53a5c
        label.y = b.y + b.h/2.0f - (float)f->height/2.0f;
b53a5c
        label.h = NK_MAX(b.h/2.0f, b.h - (b.h/2.0f + f->height/2.0f));
b53a5c
    } else if (a & NK_TEXT_ALIGN_BOTTOM) {
b53a5c
        label.y = b.y + b.h - f->height;
b53a5c
        label.h = f->height;
b53a5c
    }
b53a5c
    nk_draw_text(o, label, (const char*)string, len, f, t->background, t->text);
b53a5c
}
b53a5c
NK_LIB void
b53a5c
nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b,
b53a5c
    const char *string, int len, const struct nk_text *t,
b53a5c
    const struct nk_user_font *f)
b53a5c
{
b53a5c
    float width;
b53a5c
    int glyphs = 0;
b53a5c
    int fitting = 0;
b53a5c
    int done = 0;
b53a5c
    struct nk_rect line;
b53a5c
    struct nk_text text;
b53a5c
    NK_INTERN nk_rune seperator[] = {' '};
b53a5c
b53a5c
    NK_ASSERT(o);
b53a5c
    NK_ASSERT(t);
b53a5c
    if (!o || !t) return;
b53a5c
b53a5c
    text.padding = nk_vec2(0,0);
b53a5c
    text.background = t->background;
b53a5c
    text.text = t->text;
b53a5c
b53a5c
    b.w = NK_MAX(b.w, 2 * t->padding.x);
b53a5c
    b.h = NK_MAX(b.h, 2 * t->padding.y);
b53a5c
    b.h = b.h - 2 * t->padding.y;
b53a5c
b53a5c
    line.x = b.x + t->padding.x;
b53a5c
    line.y = b.y + t->padding.y;
b53a5c
    line.w = b.w - 2 * t->padding.x;
b53a5c
    line.h = 2 * t->padding.y + f->height;
b53a5c
b53a5c
    fitting = nk_text_clamp(f, string, len, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
b53a5c
    while (done < len) {
b53a5c
        if (!fitting || line.y + line.h >= (b.y + b.h)) break;
b53a5c
        nk_widget_text(o, line, &string[done], fitting, &text, NK_TEXT_LEFT, f);
b53a5c
        done += fitting;
b53a5c
        line.y += f->height + 2 * t->padding.y;
b53a5c
        fitting = nk_text_clamp(f, &string[done], len - done, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
b53a5c
    }
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_text_colored(struct nk_context *ctx, const char *str, int len,
b53a5c
    nk_flags alignment, struct nk_color color)
b53a5c
{
b53a5c
    struct nk_window *win;
b53a5c
    const struct nk_style *style;
b53a5c
b53a5c
    struct nk_vec2 item_padding;
b53a5c
    struct nk_rect bounds;
b53a5c
    struct nk_text text;
b53a5c
b53a5c
    NK_ASSERT(ctx);
b53a5c
    NK_ASSERT(ctx->current);
b53a5c
    NK_ASSERT(ctx->current->layout);
b53a5c
    if (!ctx || !ctx->current || !ctx->current->layout) return;
b53a5c
b53a5c
    win = ctx->current;
b53a5c
    style = &ctx->style;
b53a5c
    nk_panel_alloc_space(&bounds, ctx);
b53a5c
    item_padding = style->text.padding;
b53a5c
b53a5c
    text.padding.x = item_padding.x;
b53a5c
    text.padding.y = item_padding.y;
b53a5c
    text.background = style->window.background;
b53a5c
    text.text = color;
b53a5c
    nk_widget_text(&win->buffer, bounds, str, len, &text, alignment, style->font);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_text_wrap_colored(struct nk_context *ctx, const char *str,
b53a5c
    int len, struct nk_color color)
b53a5c
{
b53a5c
    struct nk_window *win;
b53a5c
    const struct nk_style *style;
b53a5c
b53a5c
    struct nk_vec2 item_padding;
b53a5c
    struct nk_rect bounds;
b53a5c
    struct nk_text text;
b53a5c
b53a5c
    NK_ASSERT(ctx);
b53a5c
    NK_ASSERT(ctx->current);
b53a5c
    NK_ASSERT(ctx->current->layout);
b53a5c
    if (!ctx || !ctx->current || !ctx->current->layout) return;
b53a5c
b53a5c
    win = ctx->current;
b53a5c
    style = &ctx->style;
b53a5c
    nk_panel_alloc_space(&bounds, ctx);
b53a5c
    item_padding = style->text.padding;
b53a5c
b53a5c
    text.padding.x = item_padding.x;
b53a5c
    text.padding.y = item_padding.y;
b53a5c
    text.background = style->window.background;
b53a5c
    text.text = color;
b53a5c
    nk_widget_text_wrap(&win->buffer, bounds, str, len, &text, style->font);
b53a5c
}
b53a5c
#ifdef NK_INCLUDE_STANDARD_VARARGS
b53a5c
NK_API void
b53a5c
nk_labelf_colored(struct nk_context *ctx, nk_flags flags,
b53a5c
    struct nk_color color, const char *fmt, ...)
b53a5c
{
b53a5c
    va_list args;
b53a5c
    va_start(args, fmt);
b53a5c
    nk_labelfv_colored(ctx, flags, color, fmt, args);
b53a5c
    va_end(args);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_labelf_colored_wrap(struct nk_context *ctx, struct nk_color color,
b53a5c
    const char *fmt, ...)
b53a5c
{
b53a5c
    va_list args;
b53a5c
    va_start(args, fmt);
b53a5c
    nk_labelfv_colored_wrap(ctx, color, fmt, args);
b53a5c
    va_end(args);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_labelf(struct nk_context *ctx, nk_flags flags, const char *fmt, ...)
b53a5c
{
b53a5c
    va_list args;
b53a5c
    va_start(args, fmt);
b53a5c
    nk_labelfv(ctx, flags, fmt, args);
b53a5c
    va_end(args);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_labelf_wrap(struct nk_context *ctx, const char *fmt,...)
b53a5c
{
b53a5c
    va_list args;
b53a5c
    va_start(args, fmt);
b53a5c
    nk_labelfv_wrap(ctx, fmt, args);
b53a5c
    va_end(args);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_labelfv_colored(struct nk_context *ctx, nk_flags flags,
b53a5c
    struct nk_color color, const char *fmt, va_list args)
b53a5c
{
b53a5c
    char buf[256];
b53a5c
    nk_strfmt(buf, NK_LEN(buf), fmt, args);
b53a5c
    nk_label_colored(ctx, buf, flags, color);
b53a5c
}
b53a5c
b53a5c
NK_API void
b53a5c
nk_labelfv_colored_wrap(struct nk_context *ctx, struct nk_color color,
b53a5c
    const char *fmt, va_list args)
b53a5c
{
b53a5c
    char buf[256];
b53a5c
    nk_strfmt(buf, NK_LEN(buf), fmt, args);
b53a5c
    nk_label_colored_wrap(ctx, buf, color);
b53a5c
}
b53a5c
b53a5c
NK_API void
b53a5c
nk_labelfv(struct nk_context *ctx, nk_flags flags, const char *fmt, va_list args)
b53a5c
{
b53a5c
    char buf[256];
b53a5c
    nk_strfmt(buf, NK_LEN(buf), fmt, args);
b53a5c
    nk_label(ctx, buf, flags);
b53a5c
}
b53a5c
b53a5c
NK_API void
b53a5c
nk_labelfv_wrap(struct nk_context *ctx, const char *fmt, va_list args)
b53a5c
{
b53a5c
    char buf[256];
b53a5c
    nk_strfmt(buf, NK_LEN(buf), fmt, args);
b53a5c
    nk_label_wrap(ctx, buf);
b53a5c
}
b53a5c
b53a5c
NK_API void
b53a5c
nk_value_bool(struct nk_context *ctx, const char *prefix, int value)
b53a5c
{
b53a5c
    nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, ((value) ? "true": "false"));
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_value_int(struct nk_context *ctx, const char *prefix, int value)
b53a5c
{
b53a5c
    nk_labelf(ctx, NK_TEXT_LEFT, "%s: %d", prefix, value);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_value_uint(struct nk_context *ctx, const char *prefix, unsigned int value)
b53a5c
{
b53a5c
    nk_labelf(ctx, NK_TEXT_LEFT, "%s: %u", prefix, value);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_value_float(struct nk_context *ctx, const char *prefix, float value)
b53a5c
{
b53a5c
    double double_value = (double)value;
b53a5c
    nk_labelf(ctx, NK_TEXT_LEFT, "%s: %.3f", prefix, double_value);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_value_color_byte(struct nk_context *ctx, const char *p, struct nk_color c)
b53a5c
{
b53a5c
    nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%d, %d, %d, %d)", p, c.r, c.g, c.b, c.a);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_value_color_float(struct nk_context *ctx, const char *p, struct nk_color color)
b53a5c
{
b53a5c
    double c[4]; nk_color_dv(c, color);
b53a5c
    nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%.2f, %.2f, %.2f, %.2f)",
b53a5c
        p, c[0], c[1], c[2], c[3]);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_value_color_hex(struct nk_context *ctx, const char *prefix, struct nk_color color)
b53a5c
{
b53a5c
    char hex[16];
b53a5c
    nk_color_hex_rgba(hex, color);
b53a5c
    nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, hex);
b53a5c
}
b53a5c
#endif
b53a5c
NK_API void
b53a5c
nk_text(struct nk_context *ctx, const char *str, int len, nk_flags alignment)
b53a5c
{
b53a5c
    NK_ASSERT(ctx);
b53a5c
    if (!ctx) return;
b53a5c
    nk_text_colored(ctx, str, len, alignment, ctx->style.text.color);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_text_wrap(struct nk_context *ctx, const char *str, int len)
b53a5c
{
b53a5c
    NK_ASSERT(ctx);
b53a5c
    if (!ctx) return;
b53a5c
    nk_text_wrap_colored(ctx, str, len, ctx->style.text.color);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_label(struct nk_context *ctx, const char *str, nk_flags alignment)
b53a5c
{
b53a5c
    nk_text(ctx, str, nk_strlen(str), alignment);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_label_colored(struct nk_context *ctx, const char *str, nk_flags align,
b53a5c
    struct nk_color color)
b53a5c
{
b53a5c
    nk_text_colored(ctx, str, nk_strlen(str), align, color);
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_label_wrap(struct nk_context *ctx, const char *str)
b53a5c
{
b53a5c
    nk_text_wrap(ctx, str, nk_strlen(str));
b53a5c
}
b53a5c
NK_API void
b53a5c
nk_label_colored_wrap(struct nk_context *ctx, const char *str, struct nk_color color)
b53a5c
{
b53a5c
    nk_text_wrap_colored(ctx, str, nk_strlen(str), color);
b53a5c
}
b53a5c