UFO: Alien Invasion
Loading...
Searching...
No Matches
ui_node_button.cpp
Go to the documentation of this file.
1
8
9/*
10Copyright (C) 2002-2025 UFO: Alien Invasion.
11
12This program is free software; you can redistribute it and/or
13modify it under the terms of the GNU General Public License
14as published by the Free Software Foundation; either version 2
15of the License, or (at your option) any later version.
16
17This program is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20
21See the GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with this program; if not, write to the Free Software
25Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27*/
28
29#include "../ui_main.h"
30#include "../ui_actions.h"
31#include "../ui_parse.h"
32#include "../ui_behaviour.h"
33#include "../ui_font.h"
34#include "../ui_render.h"
35#include "../ui_sound.h"
36#include "../ui_sprite.h"
37#include "ui_node_button.h"
39#include "ui_node_panel.h"
40
42
43#define EXTRADATA_TYPE buttonExtraData_t
44#define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
45#define EXTRADATACONST(node) UI_EXTRADATACONST(node, EXTRADATA_TYPE)
46
47#include "../../client.h"
48
53{
54 const float* textColor;
55 vec2_t pos;
57 const char* font = UI_GetFontFromNode(node);
58
59 if (node->flash)
61
62 if (node->disabled) {
63 textColor = node->disabledColor;
64 iconStatus = SPRITE_STATUS_DISABLED;
65 } else if (node->state) {
66 textColor = node->selectedColor;
67 iconStatus = SPRITE_STATUS_HOVER;
68 } else {
69 textColor = node->color;
70 }
71
72 UI_GetNodeAbsPos(node, pos);
73
74 if (EXTRADATA(node).background) {
75 UI_DrawSpriteInBox(false, EXTRADATA(node).background, iconStatus, pos[0], pos[1], node->box.size[0], node->box.size[1]);
76 }
77
78 /* compute node box with padding */
79 uiBox_t inside;
80 inside.set(pos, node->box.size);
81 inside.expand(-node->padding);
82 uiBox_t content;
83 content.clear();
84
85 const bool hasIcon = EXTRADATA(node).icon != nullptr;
86 const char* text = UI_GetReferenceString(node, node->text);
87 const bool hasText = text != nullptr && *text != '\0';
88 if (hasText) {
89 text = _(text);
90 }
91
92 vec2_t iconPos;
93 Vector2Clear(iconPos);
94 if (hasIcon) {
95 content.size[0] += EXTRADATA(node).icon->size[0];
96 content.size[1] += EXTRADATA(node).icon->size[1];
97 }
98
99 vec2_t textPos;
100 Vector2Clear(textPos);
101 int textWidth;
102 if (hasText) {
103 textPos[0] = content.size[0];
104 const int availableTextWidth = inside.size[0] - content.size[0];
105 int height;
106 R_FontTextSize(font, text, availableTextWidth, LONGLINES_PRETTYCHOP, &textWidth, &height, nullptr, nullptr);
107 content.size[0] += textWidth;
108 if (height > content.size[1]) {
109 content.size[1] = height;
110 iconPos[1] = (height - content.size[1]) * 0.5;
111 } else {
112 textPos[1] = (content.size[1] - height) * 0.5;
113 }
114 }
115
116 inside.alignBox(content, (align_t)node->contentAlign);
117
118 /* display the icon at the left */
120 if (hasIcon) {
121 iconPos[0] += content.pos[0];
122 iconPos[1] += content.pos[1];
123 UI_DrawSpriteInBox(EXTRADATA(node).flipIcon, EXTRADATA(node).icon, iconStatus,
124 iconPos[0], iconPos[1], EXTRADATA(node).icon->size[0], EXTRADATA(node).icon->size[1]);
125 }
126
127 if (hasText) {
128 textPos[0] += content.pos[0];
129 textPos[1] += content.pos[1];
130 R_Color(textColor);
131 /* @todo here IMO we should not use contentalign. need to check other toolkits to check layout */
133 textPos[0], content.pos[1], textWidth, content.size[1],
134 text);
135 R_Color(nullptr);
136 }
137
138 if (node->flash)
140}
141
146{
147 node->padding = 8;
148 node->contentAlign = ALIGN_CC;
149 node->flashSpeed = 1.0;
150 Vector4Set(node->selectedColor, 1, 1, 1, 1);
151 Vector4Set(node->disabledColor, 0.5, 0.5, 0.5, 1.0);
152 Vector4Set(node->color, 1, 1, 1, 1);
153 Vector4Set(node->flashColor, 1, 1, 1, 0);
154}
155
160{
161 /* auto calc the size if none was given via script files */
162 if (node->box.size[1] == 0) {
163 const char* font = UI_GetFontFromNode(node);
164 node->box.size[1] = (UI_FontGetHeight(font) / 2) + (node->padding * 2);
165 }
166}
167
170 UI_EXTRADATA(node, buttonExtraData_t).background = sprite;
171}
172
173void UI_Button_SetIconByName(uiNode_t* node, const char* name) {
175 UI_EXTRADATA(node, buttonExtraData_t).icon = sprite;
176}
177
179{
180 behaviour->name = "button";
181 behaviour->manager = UINodePtr(new uiButtonNode());
182 behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
183 behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiButtonNode_t *");
184
185 /* Icon used to display the node
186 */
188 UI_RegisterExtradataNodeProperty(behaviour, "flipicon", V_BOOL, EXTRADATA_TYPE, flipIcon);
189
190 /* Sprite used to display the background */
191 UI_RegisterExtradataNodeProperty(behaviour, "background", V_UI_SPRITEREF, EXTRADATA_TYPE, background);
192}
void R_FontTextSize(const char *fontId, const char *text, int maxWidth, longlines_t method, int *width, int *height, int *lines, bool *isTruncated)
Supply information about the size of the text when it is linewrapped and rendered,...
Definition r_font.cpp:522
@ LONGLINES_PRETTYCHOP
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition r_state.cpp:1011
#define _(String)
Definition cl_shared.h:44
void draw(uiNode_t *node) override
Handles Button draw.
void onLoading(uiNode_t *node) override
Handles Button before loading. Used to set default attribute values.
void onLoaded(uiNode_t *node) override
Handled after the end of the load of the node from script (all data and/or child are set).
Primary header for client.
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition r_gl.h:110
align_t
We need this here for checking the boundaries from script values.
Definition scripts.h:89
@ ALIGN_CC
Definition scripts.h:94
@ V_BOOL
Definition scripts.h:50
Header for lua script functions.
node behaviour, how a node work
const char * name
void * lua_SWIG_typeinfo
UINodePtr manager
intptr_t extraDataSize
void alignBox(uiBox_t &inner, align_t direction)
Definition ui_nodes.cpp:698
void expand(int dist)
Definition ui_nodes.h:64
vec2_t size
Definition ui_nodes.h:52
vec2_t pos
Definition ui_nodes.h:51
void set(vec2_t pos, vec2_t size)
Definition ui_nodes.h:59
void clear()
Definition ui_nodes.h:54
Atomic structure used to define most of the UI.
Definition ui_nodes.h:80
bool disabled
Definition ui_nodes.h:102
vec4_t flashColor
Definition ui_nodes.h:129
bool flash
Definition ui_nodes.h:107
char * text
Definition ui_nodes.h:121
int contentAlign
Definition ui_nodes.h:120
vec4_t color
Definition ui_nodes.h:127
vec4_t selectedColor
Definition ui_nodes.h:128
uiBox_t box
Definition ui_nodes.h:96
float flashSpeed
Definition ui_nodes.h:108
int padding
Definition ui_nodes.h:109
vec4_t disabledColor
Definition ui_nodes.h:103
bool state
Definition ui_nodes.h:106
vec_t vec2_t[2]
Definition ufotypes.h:38
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
int UI_FontGetHeight(const char *fontID)
Definition ui_font.cpp:166
const char * UI_GetFontFromNode(const uiNode_t *const node)
Return the font for a specific node or default font.
Definition ui_font.cpp:145
void * UI_SWIG_TypeQuery(const char *name)
This function queries the SWIG type table for a type information structure. It is used in combination...
void UI_GetNodeAbsPos(const uiNode_t *node, vec2_t pos)
Returns the absolute position of a node.
Definition ui_node.cpp:526
SharedPtr< uiNode > UINodePtr
#define EXTRADATA_TYPE
#define EXTRADATA(node)
void UI_Button_SetBackgroundByName(uiNode_t *node, const char *name)
void UI_Button_SetIconByName(uiNode_t *node, const char *name)
void UI_RegisterButtonNode(uiBehaviour_t *behaviour)
#define UI_EXTRADATA(NODE, TYPE)
Definition ui_nodes.h:185
const char * UI_GetReferenceString(const uiNode_t *const node, const char *ref)
#define V_UI_SPRITEREF
Definition ui_parse.h:56
void UI_DisableFlashing(void)
Disables flashing effect for UI nodes.
int UI_DrawStringInBox(const char *fontID, align_t align, int x, int y, int width, int height, const char *text, longlines_t method)
draw a line into a bounding box
void UI_EnableFlashing(const vec4_t flashingColor, float speed)
Enables flashing effect for UI nodes.
void UI_DrawSpriteInBox(bool flip, const uiSprite_t *sprite, uiSpriteStatus_t status, int posX, int posY, int sizeX, int sizeY)
uiSprite_t * UI_GetSpriteByName(const char *name)
Return an sprite by is name.
uiSpriteStatus_t
Definition ui_sprite.h:32
@ SPRITE_STATUS_DISABLED
Definition ui_sprite.h:35
@ SPRITE_STATUS_HOVER
Definition ui_sprite.h:34
@ SPRITE_STATUS_NORMAL
Definition ui_sprite.h:33
#define Vector4Set(v, r, g, b, a)
Definition vector.h:62
#define Vector2Clear(a)
Definition vector.h:54