src/ascension/ui/text.h

Thu, 18 Apr 2024 22:53:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Apr 2024 22:53:55 +0200
changeset 65
9c44c55d327a
parent 59
764fbb013252
permissions
-rw-r--r--

consistently refer to windows by ID - fixes #381

This change discovered that the font cache is completely broken. We created issue #387 for this.

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  * Copyright 2023 Mike Becker. All rights reserved.
     4  *
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions are met:
     7  *
     8  *   1. Redistributions of source code must retain the above copyright
     9  *      notice, this list of conditions and the following disclaimer.
    10  *
    11  *   2. Redistributions in binary form must reproduce the above copyright
    12  *      notice, this list of conditions and the following disclaimer in the
    13  *      documentation and/or other materials provided with the distribution.
    14  *
    15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    25  * POSSIBILITY OF SUCH DAMAGE.
    26  */
    28 #ifndef ASCENSION_UI_TEXT_H
    29 #define ASCENSION_UI_TEXT_H
    31 #include "font.h"
    32 #include "../scene.h"
    33 #include "../texture.h"
    34 #include "../utils.h"
    36 #include <cx/string.h>
    38 typedef struct AscText {
    39     extend_asc_scene_node;
    40     cxmutstr text;
    41     AscFont font;
    42     asc_col4i color;
    43     unsigned short max_width;
    44     unsigned short offx;
    45     AscTexture tex;
    46 } AscText;
    48 enum asc_text_alignment {
    49     ASC_TEXT_ALIGN_LEFT = 0x00,
    50     ASC_TEXT_ALIGN_CENTERED = 0x01,
    51     ASC_TEXT_ALIGN_RIGHT = 0x02
    52 };
    53 #define ASC_TEXT_ALIGNMENT_MASK 0x03
    54 #define ASC_TEXT_CENTERED_FLAG  0x04
    56 struct asc_text_create_args {
    57     int x;
    58     int y;
    59     char const *text;
    60     enum asc_text_alignment alignment;
    61     unsigned short max_width;
    62 };
    64 /**
    65  * Creates a text node.
    66  *
    67  * The current context ink and font will be used.
    68  *
    69  * @param args initial arguments for creating the node
    70  * @return the scene node
    71  *
    72  * @see asc_ink()
    73  * @see asc_font()
    74  */
    75 AscSceneNode *asc_text_create(struct asc_text_create_args args);
    77 /**
    78  * Creates a text node.
    79  *
    80  * The current context ink and font will be used.
    81  *
    82  * This is a convenience macro that lets you use the arguments
    83  * as named parameters. Usage example:
    84  * @code
    85  * AscSceneNode *mytext = asc_text( .x = 10, .y = 15 );
    86  * @endcode
    87  *
    88  * @param ... initial arguments for creating the node
    89  * @return the scene node
    90  *
    91  * @see asc_ink()
    92  * @see asc_font()
    93  */
    94 #define asc_text(...) \
    95     asc_text_create((struct asc_text_create_args) { __VA_ARGS__ })
    97 /**
    98  * Sets the text alignment.
    99  *
   100  * @param node the text node
   101  */
   102 __attribute__((__nonnull__))
   103 static inline void asc_text_alignment(
   104         AscSceneNode *node,
   105         enum asc_text_alignment alignment
   106 ) {
   107     asc_set_flag_masked(node->flags, ASC_TEXT_ALIGNMENT_MASK, alignment);
   108     asc_node_update(node);
   109 }
   111 /**
   112  * Decides whether the text shall be centered.
   113  *
   114  * @param node the text node
   115  * @param centered true when the text shall be centered
   116  */
   117 __attribute__((__nonnull__))
   118 static inline void asc_text_centered(
   119         AscSceneNode *node,
   120         bool centered
   121 ) {
   122     if (centered) {
   123         asc_clear_flag(node->flags, ASC_TEXT_ALIGN_CENTERED);
   124     } else {
   125         asc_set_flag(node->flags, ASC_TEXT_ALIGN_CENTERED);
   126     }
   127     asc_node_update(node);
   128 }
   130 /**
   131  * Sets a new maximum width for the text.
   132  *
   133  * @param node the text node
   134  * @param max_width the new maximum width
   135  */
   136 __attribute__((__nonnull__))
   137 static inline void asc_text_max_width(
   138         AscSceneNode *node,
   139         unsigned max_width
   140 ) {
   141     ((AscText*)node)->max_width = max_width;
   142     asc_node_update(node);
   143 }
   145 /**
   146  *
   147  * @param node
   148  * @param format
   149  * @param ...
   150  */
   151 __attribute__((__nonnull__))
   152 void asc_text_printf(
   153         AscSceneNode *node,
   154         char const* format,
   155         ...
   156 );
   158 /**
   159  * Releases all the memory of this node.
   160  *
   161  * @param node the text node
   162  */
   163 void asc_text_free(AscText *node);
   165 #endif //ASCENSION_UI_TEXT_H

mercurial