UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_node_abstractscrollable.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #include "../ui_main.h"
27 #include "../ui_parse.h"
28 #include "../ui_behaviour.h"
29 #include "../ui_font.h"
30 #include "../ui_render.h"
31 #include "../ui_actions.h"
32 #include "../ui_lua.h"
33 
34 #include "ui_node_abstractnode.h"
36 
37 #include "../../client.h" /* gettext _() */
38 
39 #include "../../../common/scripts_lua.h"
40 
41 #define EXTRADATA_TYPE abstractScrollableExtraData_t
42 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
43 
48 {
49  assert(UI_Node_IsScrollableContainer(node));
50 
51  if (!Vector2Equal(node->box.size, EXTRADATA(node).cacheSize)) {
52  Vector2Copy(node->box.size, EXTRADATA(node).cacheSize);
53  return true;
54  }
55  return false;
56 }
57 
63 bool uiScroll_t::move (int viewPos)
64 {
65  /* clap position */
66  if (viewPos > this->fullSize - this->viewSize)
67  viewPos = this->fullSize - this->viewSize;
68  if (viewPos < 0)
69  viewPos = 0;
70 
71  /* no changes */
72  if (this->viewPos == viewPos) {
73  return false;
74  }
75 
76  this->viewPos = viewPos;
77  return true;
78 }
79 
87 bool uiScroll_t::set (int viewPos, int viewSize, int fullSize)
88 {
89  bool updated = false;
90 
91  /* default values */
92  if (viewPos == -1)
93  viewPos = this->viewPos;
94  if (viewSize == -1)
95  viewSize = this->viewSize;
96  if (fullSize == -1)
97  fullSize = this->fullSize;
98 
99  /* fix limits */
100  if (viewSize < 0)
101  viewSize = 0;
102  if (fullSize < 0)
103  fullSize = 0;
104  if (viewPos < 0)
105  viewPos = 0;
106 
107  /* clap position */
108  if (viewSize >= fullSize)
109  viewPos = 0;
110  else if (viewPos > fullSize - viewSize)
111  viewPos = fullSize - viewSize;
112 
113  /* update */
114  if (this->viewPos != viewPos) {
115  this->viewPos = viewPos;
116  updated = true;
117  }
118  if (this->viewSize != viewSize) {
119  this->viewSize = viewSize;
120  updated = true;
121  }
122  if (this->fullSize != fullSize) {
123  this->fullSize = fullSize;
124  updated = true;
125  }
126 
127  return updated;
128 }
129 
132  EXTRADATA(node).lua_onViewChange = LUA_NOREF;
133 }
134 
143 bool uiAbstractScrollableNode::setScrollY (uiNode_t* node, int viewPos, int viewSize, int fullSize)
144 {
145  bool updated;
146  assert(UI_Node_IsScrollableContainer(node));
147 
148  updated = EXTRADATA(node).scrollY.set(viewPos, viewSize, fullSize);
149 
150  if (updated) {
151  if (EXTRADATA(node).onViewChange) {
152  UI_ExecuteEventActions(node, EXTRADATA(node).onViewChange);
153  }
154  else if (EXTRADATA(node).lua_onViewChange != LUA_NOREF) {
155  UI_ExecuteLuaEventScript (node, EXTRADATA(node).lua_onViewChange);
156  }
157  }
158 
159 
160  return updated;
161 }
162 
164  const int pos = EXTRADATA(node).scrollY.viewPos - 10;
165  setScrollY(node, (pos >= 0)?pos:0, -1, -1);
166 }
168  setScrollY(node, EXTRADATA(node).scrollY.viewPos + 10, -1, -1);
169 }
171  setScrollY(node, EXTRADATA(node).scrollY.viewPos - 1, -1, -1);
172 }
174  setScrollY(node, EXTRADATA(node).scrollY.viewPos + 1, -1, -1);
175 }
177  setScrollY(node, 0, -1, -1);
178 }
180  setScrollY(node, EXTRADATA(node).scrollY.fullSize, -1, -1);
181 }
182 
186 static void UI_AbstractScrollableNodePageUp (uiNode_t* node, const uiCallContext_t* context)
187 {
189  b->pageUp(node);
190 }
191 
192 static void UI_AbstractScrollableNodePageDown (uiNode_t* node, const uiCallContext_t* context)
193 {
195  b->pageDown(node);
196 }
197 
198 static void UI_AbstractScrollableNodeMoveUp (uiNode_t* node, const uiCallContext_t* context)
199 {
201  b->moveUp(node);
202 }
203 
204 static void UI_AbstractScrollableNodeMoveDown (uiNode_t* node, const uiCallContext_t* context)
205 {
207  b->moveDown (node);
208 }
209 
210 static void UI_AbstractScrollableNodeMoveHome (uiNode_t* node, const uiCallContext_t* context)
211 {
213  b->moveHome (node);
214 }
215 
219 static void UI_AbstractScrollableNodeMoveEnd (uiNode_t* node, const uiCallContext_t* context)
220 {
222  b->moveEnd (node);
223 }
224 
230 {
231  assert(UI_Node_IsScrollableContainer(node));
232  return setScrollY(node, EXTRADATA(node).scrollY.viewPos + offset, -1, -1);
233 }
234 
236 {
237  behaviour->name = "abstractscrollable";
238  behaviour->manager = UINodePtr(new uiAbstractScrollableNode());
239  behaviour->isAbstract = true;
240  behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
241  behaviour->lua_SWIG_typeinfo = UI_SWIG_TypeQuery("uiAbstractScrollableNode_t *");
242 
243  /* position of the vertical view (into the full number of elements the node contain) */
244  UI_RegisterExtradataNodeProperty(behaviour, "viewpos", V_INT, EXTRADATA_TYPE, scrollY.viewPos);
245  /* size of the vertical view (proportional to the number of elements the node can display without moving) */
246  UI_RegisterExtradataNodeProperty(behaviour, "viewsize", V_INT, EXTRADATA_TYPE, scrollY.viewSize);
247  /* full vertical size (proportional to the number of elements the node contain) */
248  UI_RegisterExtradataNodeProperty(behaviour, "fullsize", V_INT, EXTRADATA_TYPE, scrollY.fullSize);
249  /* Called when one of the properties viewpos/viewsize/fullsize change */
250  UI_RegisterExtradataNodeProperty(behaviour, "onviewchange", V_UI_ACTION, EXTRADATA_TYPE, onViewChange);
251 
252  /* Call it to vertically scroll the document up */
254  /* Call it to vertically scroll the document down */
256  /* Call it to vertically scroll the document up */
258  /* Call it to vertically scroll the document down */
260  /* Call it to vertically reset the scroll position to 0 */
262  /* Call it to vertically move the scroll to the end of the document */
264 }
vec2_t size
Definition: ui_nodes.h:52
#define Vector2Equal(a, b)
Definition: vector.h:67
const char * name
Definition: ui_behaviour.h:41
void initNode(uiNode_t *node) override
static void UI_AbstractScrollableNodePageDown(uiNode_t *node, const uiCallContext_t *context)
uiBehaviour_t * behaviour
Definition: ui_nodes.h:83
UINodePtr manager
Definition: ui_behaviour.h:43
bool setScrollY(uiNode_t *node, int viewPos, int viewSize, int fullSize)
Set the Y scroll to a position, and call event if need.
bool UI_ExecuteLuaEventScript(uiNode_t *node, LUA_EVENT event)
Executes a lua event handler.
Definition: ui_lua.cpp:71
void UI_RegisterAbstractScrollableNode(uiBehaviour_t *behaviour)
#define EXTRADATA_TYPE
bool UI_Node_IsScrollableContainer(uiNode_t const *node)
Definition: ui_node.cpp:93
static void UI_AbstractScrollableNodeMoveHome(uiNode_t *node, const uiCallContext_t *context)
#define UI_RegisterExtradataNodeProperty(BEHAVIOUR, NAME, TYPE, EXTRADATATYPE, ATTRIBUTE)
Initialize a property from extradata of node.
Definition: ui_behaviour.h:109
bool set(int viewPos, int viewSize, int fullSize)
Set the scroll to a position.
bool isSizeChange(uiNode_t *node)
return true if the node size change and update the cache
SharedPtr< uiNode > UINodePtr
bool scrollY(uiNode_t *node, int offset)
Scroll the Y scroll with a relative position, and call event if need.
void * lua_SWIG_typeinfo
Definition: ui_behaviour.h:57
void UI_ExecuteEventActions(uiNode_t *source, const uiAction_t *firstAction)
Definition: ui_actions.cpp:726
PointerType get() const
Definition: sharedptr.h:197
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
base code for scrollable node
void * UI_SWIG_TypeQuery(const char *name)
This function queries the SWIG type table for a type information structure. It is used in combination...
Contain the context of the calling of a function.
Definition: ui_actions.h:208
#define EXTRADATA(node)
static void UI_AbstractScrollableNodePageUp(uiNode_t *node, const uiCallContext_t *context)
virtual void initNode(uiNode_t *node)
intptr_t extraDataSize
Definition: ui_behaviour.h:54
node behaviour, how a node work
Definition: ui_behaviour.h:39
static void UI_AbstractScrollableNodeMoveDown(uiNode_t *node, const uiCallContext_t *context)
#define Vector2Copy(src, dest)
Definition: vector.h:52
static void UI_AbstractScrollableNodeMoveEnd(uiNode_t *node, const uiCallContext_t *context)
Definition: scripts.h:52
#define V_UI_ACTION
Definition: ui_parse.h:54
uiBox_t box
Definition: ui_nodes.h:96
voidpf uLong offset
Definition: ioapi.h:45
static void UI_AbstractScrollableNodeMoveUp(uiNode_t *node, const uiCallContext_t *context)
const struct value_s * UI_RegisterNodeMethod(uiBehaviour_t *behaviour, const char *name, uiNodeMethod_t function)
Register a node method to a behaviour.
bool move(int viewPos)
Fix a new position.