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