src/ascension/text.h

Fri, 01 Aug 2025 17:57:39 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 01 Aug 2025 17:57:39 +0200
changeset 235
e9540bfa5803
parent 219
62508d957a22
permissions
-rw-r--r--

make asc_ui_scale_auto() normalized with Full HD as 100%

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright 2023 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ASCENSION_UI_TEXT_H
#define ASCENSION_UI_TEXT_H

#include "font.h"
#include "mesh.h"
#include "texture.h"
#include "scene_node.h"

#include <cx/string.h>

typedef struct asc_text_s {
    AscSceneNode base;
    AscMesh mesh;
    AscTexture *texture;
    cxmutstr text;
    AscFont font;
    asc_col4i color;
    unsigned short max_width;
    /**
     * The automatically calculated offset in case the text is centered.
     */
    unsigned short offx;
    /**
     * The automatically calculated text dimension.
     */
    asc_vec2u dimension;
} AscText;

enum asc_text_alignment {
    ASC_TEXT_ALIGN_LEFT = 0x00,
    ASC_TEXT_ALIGN_CENTERED = 0x01,
    ASC_TEXT_ALIGN_RIGHT = 0x02
};
#define ASC_TEXT_ALIGNMENT_MASK 0x03
#define ASC_TEXT_CENTERED_FLAG  0x04

struct asc_text_create_args {
    AscFont font;
    int x;
    int y;
    const char *name;
    const char *text;
    asc_col4i color;
    enum asc_text_alignment alignment;
    unsigned short max_width;
    bool centered;
};

/**
 * Creates a text node.
 *
 * The current context ink and font will be used.
 *
 * @param args initial arguments for creating the node
 * @return the scene node
 *
 * @see asc_ink()
 * @see asc_font()
 */
AscSceneNode *asc_text_create(struct asc_text_create_args args);

/**
 * Creates a text node.
 *
 * The current context ink and font will be used.
 *
 * This is a convenience macro that lets you use the arguments
 * as named parameters. Usage example:
 * @code
 * AscSceneNode *mytext = asc_text( .x = 10, .y = 15 );
 * @endcode
 *
 * @param ... initial arguments for creating the node
 * @return the scene node
 *
 * @see asc_ink()
 * @see asc_font()
 */
#define asc_text(...) \
    asc_text_create((struct asc_text_create_args) { __VA_ARGS__ })

/**
 * Sets the text alignment.
 *
 * @param node the text node
 */
void asc_text_alignment(
        AscText *node,
        enum asc_text_alignment alignment
);

/**
 * Decides whether the text shall be centered.
 *
 * @param node the text node
 * @param centered true when the text shall be centered
 */
void asc_text_centered(AscText *node, bool centered);

/**
 * Sets a new maximum width for the text.
 *
 * @param node the text node
 * @param max_width the new maximum width
 */
void asc_text_max_width(AscText *node, unsigned max_width);

/**
 * Updates the content of a text node with formatted text.
 *
 * @param node the node
 * @param format the format string
 * @param ... the format arguments
 */
void asc_text_printf(
        AscText *node,
        const char *format,
        ...
);

#endif //ASCENSION_UI_TEXT_H

mercurial