UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_button.cpp
Go to the documentation of this file.
1 
9 /*
10 Copyright (C) 2002-2020 UFO: Alien Invasion.
11 
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 
21 See the GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, 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"
38 #include "ui_node_abstractnode.h"
39 #include "ui_node_panel.h"
40 
41 #include "../../../common/scripts_lua.h"
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 
168 void UI_Button_SetBackgroundByName(uiNode_t* node, const char* name) {
169  uiSprite_t* sprite = UI_GetSpriteByName(name);
170  UI_EXTRADATA(node, buttonExtraData_t).background = sprite;
171 }
172 
173 void UI_Button_SetIconByName(uiNode_t* node, const char* name) {
174  uiSprite_t* sprite = UI_GetSpriteByName(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 UI_Button_SetBackgroundByName(uiNode_t *node, const char *name)
vec2_t size
Definition: ui_nodes.h:52
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
Definition: ui_render.cpp:359
const char * name
Definition: ui_behaviour.h:41
#define _(String)
Definition: cl_shared.h:43
UINodePtr manager
Definition: ui_behaviour.h:43
#define V_UI_SPRITEREF
Definition: ui_parse.h:56
void UI_DrawSpriteInBox(bool flip, const uiSprite_t *sprite, uiSpriteStatus_t status, int posX, int posY, int sizeX, int sizeY)
Definition: ui_sprite.cpp:187
vec4_t flashColor
Definition: ui_nodes.h:129
void alignBox(uiBox_t &inner, align_t direction)
Definition: ui_nodes.cpp:698
#define EXTRADATA(node)
void UI_DisableFlashing(void)
Disables flashing effect for UI nodes.
Definition: ui_render.cpp:417
#define Vector2Clear(a)
Definition: vector.h:54
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
Definition: ui_behaviour.h:109
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition: r_state.cpp:1011
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, without actually rendering it. Any of the output parameters may be nullptr.
Definition: r_font.cpp:524
align_t
We need this here for checking the boundaries from script values.
Definition: scripts.h:90
bool state
Definition: ui_nodes.h:106
#define Vector4Set(v, r, g, b, a)
Definition: vector.h:62
vec4_t disabledColor
Definition: ui_nodes.h:103
SharedPtr< uiNode > UINodePtr
int padding
Definition: ui_nodes.h:109
void * lua_SWIG_typeinfo
Definition: ui_behaviour.h:57
#define UI_EXTRADATA(NODE, TYPE)
Definition: ui_nodes.h:185
bool flash
Definition: ui_nodes.h:107
const char * UI_GetReferenceString(const uiNode_t *const node, const char *ref)
Definition: ui_parse.cpp:1406
void clear()
Definition: ui_nodes.h:54
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
void UI_EnableFlashing(const vec4_t flashingColor, float speed)
Enables flashing effect for UI nodes.
Definition: ui_render.cpp:406
void * UI_SWIG_TypeQuery(const char *name)
This function queries the SWIG type table for a type information structure. It is used in combination...
bool disabled
Definition: ui_nodes.h:102
void UI_GetNodeAbsPos(const uiNode_t *node, vec2_t pos)
Returns the absolute position of a node.
Definition: ui_node.cpp:514
#define EXTRADATA_TYPE
vec4_t selectedColor
Definition: ui_nodes.h:128
intptr_t extraDataSize
Definition: ui_behaviour.h:54
const char * UI_GetFontFromNode(const uiNode_t *const node)
Return the font for a specific node or default font.
Definition: ui_font.cpp:145
Definition: scripts.h:50
void UI_Button_SetIconByName(uiNode_t *node, const char *name)
node behaviour, how a node work
Definition: ui_behaviour.h:39
void UI_RegisterButtonNode(uiBehaviour_t *behaviour)
int contentAlign
Definition: ui_nodes.h:120
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
vec4_t color
Definition: ui_nodes.h:127
void onLoading(uiNode_t *node) override
Handles Button before loading. Used to set default attribute values.
uiSpriteStatus_t
Definition: ui_sprite.h:32
int UI_FontGetHeight(const char *fontID)
Definition: ui_font.cpp:166
void draw(uiNode_t *node) override
Handles Button draw.
vec_t vec2_t[2]
Definition: ufotypes.h:38
float flashSpeed
Definition: ui_nodes.h:108
void expand(int dist)
Definition: ui_nodes.h:64
uiBox_t box
Definition: ui_nodes.h:96
uiSprite_t * UI_GetSpriteByName(const char *name)
Return an sprite by is name.
Definition: ui_sprite.cpp:115
void set(vec2_t pos, vec2_t size)
Definition: ui_nodes.h:59
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) ...
char * text
Definition: ui_nodes.h:121
vec2_t pos
Definition: ui_nodes.h:51