1 /*---------------------------------------------------------------------------*
2
3 Copyright (C) 2010-2012 Nintendo. All rights reserved.
4
5 These coded instructions, statements, and computer programs contain
6 proprietary information of Nintendo of America Inc. and/or Nintendo
7 Company Ltd., and are protected by Federal copyright law. They may
8 not be disclosed to third parties or copied or duplicated in any form,
9 in whole or in part, without the prior written consent of Nintendo.
10
11 *---------------------------------------------------------------------------*/
12
13
14 ////////////////////////////////////////////////////
15 //
16 // Scroll Bar functions
17 //
18 ////////////////////////////////////////////////////
19
Add(const ScrollBar & item)20 ScrollBar* ScrollBar::Add(const ScrollBar& item)
21 {
22 ScrollBar* pItem = new ScrollBar(item);
23 item.window->menuItems.push_back(pItem);
24 return pItem;
25 }
26
27 // Updates the scroll bar
Update()28 bool ScrollBar::Update()
29 {
30 // A draggable bar
31 if (this->count <= this->max)
32 return true;
33
34 float sizeY = this->count - this->max;
35
36 float offY = *this->pPos / sizeY;
37
38 offY = (0.5 - offY) * (this->height - this->subHeight);
39
40 // No longer selecting the bar!
41 if (!PositivePressed())
42 this->status = 0;
43
44 if (this->status == 2)
45 {
46 offY = CursorY() - this->oldY;
47 offY = 0.5 - offY / (this->height - this->subHeight);
48 *this->pPos = offY * sizeY;
49
50 // Clamp the values!
51 if (*this->pPos >= this->count - this->max)
52 {
53 this->wasMax = true;
54 *this->pPos = this->count - this->max;
55 }
56
57 if (*this->pPos <= 0)
58 *this->pPos = 0;
59
60 return false;
61 }
62
63 int status;
64
65 // We don't care as much if the cursor isn't being triggered
66 if (!PositiveTriggered())
67 status = 1;
68 else
69 status = 2;
70
71 // See if we just hit something
72 if (CursorColliding(this->x, this->y + offY, this->width, this->subHeight))
73 {
74 this->status = status;
75
76 if (status == 2)
77 this->oldY = CursorY() - offY;
78 }
79 else
80 this->status = 0;
81
82 return (this->status == 0);
83 }
84
85 // Draws the scroll bar
Draw()86 void ScrollBar::Draw()
87 {
88 // If we WERE at the bottom, STAY at the bottom
89 if (wasMax && oldPos == *pPos)
90 *pPos = count - max;
91
92 wasMax = false;
93
94 // Clamp the values!
95 if (*pPos >= count - max)
96 {
97 wasMax = true;
98 *pPos = count - max;
99 }
100
101 if (*pPos <= 0)
102 *pPos = 0;
103
104 // A draggable bar
105 if (count > max)
106 {
107 float sizeY = count - max;
108
109 float offY = *pPos / sizeY;
110
111 subHeight = height * (1.0 - (float)sizeY / count);
112
113 if (subHeight < width)
114 subHeight = width;
115
116 offY = (0.5 - offY) * (height - subHeight);
117
118 CVec4 color = WhiteDarker(status);
119 DrawQuad(position, size, CVec4(1.0, 1.0, 1.0, 0.2));
120
121 DrawQuad(CVec3(x, y + offY, z + 0.0001), CVec2(width, subHeight), color);
122
123 DrawLine(CVec3(x - width / 2, y - height / 2, z + 0.0002), CVec2(0, height), CVec4(0, 0, 0));
124 }
125
126 oldPos = *pPos;
127 }
128
129 // Resets the scroll bar's status
Reset()130 void ScrollBar::Reset()
131 {
132 if (this->status > 0)
133 this->status = 0;
134 }
135