1 /**
2  * @file
3  */
4 
5 /*
6 Copyright (C) 2002-2013 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 
23 */
24 
25 #include "../ui_nodes.h"
26 #include "../ui_parse.h"
27 #include "../ui_behaviour.h"
28 #include "../ui_render.h"
29 #include "ui_node_tbar.h"
30 #include "ui_node_abstractvalue.h"
31 #include "ui_node_abstractnode.h"
32 
33 #define EXTRADATA_TYPE tbarExtraData_t
34 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
35 #define EXTRADATACONST(node) UI_EXTRADATACONST(node, EXTRADATA_TYPE)
36 
37 #define TEXTURE_WIDTH 250.0
38 
draw(uiNode_t * node)39 void uiTBarNode::draw (uiNode_t* node)
40 {
41 	/* dataImageOrModel is the texture name */
42 	float shx;
43 	vec2_t nodepos;
44 	const char* ref = UI_GetReferenceString(node, EXTRADATA(node).image);
45 	float pointWidth;
46 	float width;
47 	if (Q_strnull(ref))
48 		return;
49 
50 	UI_GetNodeAbsPos(node, nodepos);
51 
52 	pointWidth = TEXTURE_WIDTH / 100.0;	/* relative to the texture */
53 
54 	{
55 		float ps;
56 		const float min = getMin(node);
57 		const float max = getMax(node);
58 		float value = getValue(node);
59 		/* clamp the value */
60 		if (value > max)
61 			value = max;
62 		if (value < min)
63 			value = min;
64 		ps = (value - min) / (max - min) * 100;
65 		shx = EXTRADATA(node).texl[0];	/* left gap to the texture */
66 		shx += round(ps * pointWidth); /* add size from 0..TEXTURE_WIDTH */
67 	}
68 
69 	width = (shx * node->box.size[0]) / TEXTURE_WIDTH;
70 
71 	UI_DrawNormImageByName(false, nodepos[0], nodepos[1], width, node->box.size[1],
72 		shx, EXTRADATA(node).texh[1], EXTRADATA(node).texl[0], EXTRADATA(node).texl[1], ref);
73 }
74 
UI_RegisterTBarNode(uiBehaviour_t * behaviour)75 void UI_RegisterTBarNode (uiBehaviour_t* behaviour)
76 {
77 	behaviour->name = "tbar";
78 	behaviour->extends = "abstractvalue";
79 	behaviour->manager = UINodePtr(new uiTBarNode());
80 	behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
81 
82 	/* Image to use. Each behaviour use it like they want.
83 	 * @todo use V_REF_OF_STRING when its possible ('image' is never a cvar)
84 	 */
85 	UI_RegisterExtradataNodeProperty(behaviour, "image", V_CVAR_OR_STRING, EXTRADATA_TYPE, image);
86 
87 	/* @todo Need documentation */
88 	UI_RegisterExtradataNodeProperty(behaviour, "texh", V_POS, EXTRADATA_TYPE, texh);
89 	/* @todo Need documentation */
90 	UI_RegisterExtradataNodeProperty(behaviour, "texl", V_POS, EXTRADATA_TYPE, texl);
91 }
92