src/ascension/ui/text.h

Thu, 24 Apr 2025 19:53:40 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 24 Apr 2025 19:53:40 +0200
changeset 95
622887f7e954
parent 82
4e1e698f4b0d
permissions
-rw-r--r--

in preparation of more scenes, bring back AscScene struct

/*
 * 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 "../2d/sprite.h"

#include <cx/string.h>

typedef struct AscText {
    AscSprite base;
    cxmutstr text;
    AscFont font;
    asc_col4i color;
    unsigned short max_width;
    unsigned short offx;
} 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 {
    int x;
    int y;
    char const *text;
    enum asc_text_alignment alignment;
    unsigned short max_width;
};

/**
 * 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
 */
__attribute__((__nonnull__))
static inline void asc_text_alignment(
        AscSceneNode *node,
        enum asc_text_alignment alignment
) {
    asc_set_flag_masked(node->flags, ASC_TEXT_ALIGNMENT_MASK, alignment);
    asc_node_update(node);
}

/**
 * Decides whether the text shall be centered.
 *
 * @param node the text node
 * @param centered true when the text shall be centered
 */
__attribute__((__nonnull__))
static inline void asc_text_centered(
        AscSceneNode *node,
        bool centered
) {
    if (centered) {
        asc_clear_flag(node->flags, ASC_TEXT_ALIGN_CENTERED);
    } else {
        asc_set_flag(node->flags, ASC_TEXT_ALIGN_CENTERED);
    }
    asc_node_update(node);
}

/**
 * Sets a new maximum width for the text.
 *
 * @param node the text node
 * @param max_width the new maximum width
 */
__attribute__((__nonnull__))
static inline void asc_text_max_width(
        AscSceneNode *node,
        unsigned max_width
) {
    ((AscText*)node)->max_width = max_width;
    asc_node_update(node);
}

/**
 *
 * @param node
 * @param format
 * @param ...
 */
__attribute__((__nonnull__))
void asc_text_printf(
        AscSceneNode *node,
        char const* format,
        ...
);

#endif //ASCENSION_UI_TEXT_H

mercurial