]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu/control/visual/scrollbar.qc
I did not want to do that. Sorry, disk space. Some admin better get rid of that mess.
[divverent/nexuiz.git] / data / qcsrc / menu / control / visual / scrollbar.qc
1 // DP/Nex Menu
2 // control/visual/scrollbar.qc
3
4 const float _VSB_UP_HEIGHT = 20;
5 const float _VSB_DOWN_HEIGHT = 20;
6 const float _VSB_PRECISION = 5;
7 const float _VSB_MINHEIGHT = 10;
8
9 const vector _VSB_CLICKAREA_COLOR = '1 1 1';
10 const vector _VSB_SCOLLER_COLOR = '1 1 1';
11 const vector _VSB_SCROLLAREA_COLOR = '1 1 1';
12
13 // todo: maybe  use fields to retrieve the picture names?
14 const string _VSB_SCOLLER_PICTURE = "gfx/scroll_scroller";
15 const string _VSB_SCROLLAREA_PICTURE = "gfx/scroll_background";
16 const string _VSB_BUTTON_PICTURE = "gfx/scroll_up";
17
18 // scroller == scroll button
19 float() _IVSB_GetSizeOfScroller = {
20         local float lScrollerRange;
21         local float lVisibleSize;
22         local float lTotalSize;
23         local float lScrollerSize;
24
25         lTotalSize = self._target._child.size_y;
26         lVisibleSize = self.size_y;
27         lScrollerRange = self.size_y - _VSB_DOWN_HEIGHT - _VSB_UP_HEIGHT;
28
29         lScrollerSize = lVisibleSize * lScrollerRange / lTotalSize;
30         lScrollerSize = max( _VSB_MINHEIGHT, lScrollerSize );
31
32         return lScrollerSize;
33 }
34
35 void() Item_VScrollBar_Draw = {
36         local vector lPosition;
37         local vector lSize;
38
39         if( self._target == null_entity ) {
40                 return;
41         }
42
43         lPosition = self.pos;
44         lSize = self.size;
45         lSize_y = _VSB_UP_HEIGHT;
46         Menu_DrawPicture( lPosition, _VSB_BUTTON_PICTURE, lSize, _VSB_CLICKAREA_COLOR, 1.0, 0 );
47
48         lPosition_y = self.pos_y + _VSB_UP_HEIGHT;
49         lSize_y = self.size_y - _VSB_UP_HEIGHT - _VSB_DOWN_HEIGHT;
50         Menu_DrawPicture( lPosition, _VSB_SCROLLAREA_PICTURE,lSize, _VSB_SCROLLAREA_COLOR, 1.0, 0 );
51
52         lPosition_y = self.pos_y + self.size_y - _VSB_DOWN_HEIGHT;
53         lSize_y = _VSB_DOWN_HEIGHT;
54         Menu_DrawPicture( lPosition, _VSB_BUTTON_PICTURE, lSize, _VSB_CLICKAREA_COLOR, 1.0, 0 );
55
56         lSize_y = _IVSB_GetSizeOfScroller();
57         lPosition_y = self.pos_y + _VSB_UP_HEIGHT - self._target.origin_y * (self.size_y - _VSB_DOWN_HEIGHT - _VSB_UP_HEIGHT - lSize_y) / (self._target._child.size_y - self.size_y);
58         Menu_DrawPicture( lPosition, _VSB_SCOLLER_PICTURE, lSize, _VSB_SCOLLER_COLOR, 1.0, 0 );
59 };
60
61 void() _IVSB_RangeBound = {
62         self._target.origin_y = bound( - (self._target._child.size_y - self.size_y) , self._target.origin_y, 0 );
63 };
64
65 void() _IVSB_ScrollUp = {
66         self._target.origin_y = self._target.origin_y + _VSB_PRECISION;
67         _IVSB_RangeBound();
68 };
69
70 void() _IVSB_ScrollDown = {
71         self._target.origin_y = self._target.origin_y - _VSB_PRECISION;
72         _IVSB_RangeBound();
73 };
74
75 void( float lLevel ) _IVSB_MouseSelect = {
76         local float lScrollerRange;
77         local float lVisibleSize;
78         local float lTotalSize;
79         local float lScrollerSize;
80
81         lTotalSize = self._target._child.size_y;
82         lVisibleSize = self.size_y;
83         lScrollerRange = self.size_y - _VSB_DOWN_HEIGHT - _VSB_UP_HEIGHT;
84         lScrollerSize = _IVSB_GetSizeOfScroller();
85
86         lLevel = lLevel - _VSB_UP_HEIGHT - lScrollerSize / 2;
87         self._target.origin_y = - ( lLevel * (lTotalSize - lVisibleSize) / (lScrollerRange - lScrollerSize ) );
88         _IVSB_RangeBound();
89 };
90
91 bool( float pKey, float Ascii ) Item_VScrollBar_Key = {
92         if( self._target == null_entity ) {
93                 return false;
94         }
95
96         if( pKey == K_UPARROW ) {
97                 _IVSB_ScrollUp();
98                 return true;
99         } else if( pKey == K_DOWNARROW ) {
100                 _IVSB_ScrollDown();
101                 return true;
102         } else if( pKey == K_MOUSE1 ) {
103                 local float lLevel;
104
105                 if( !Util_InRect( Menu_Cursor_Position, self.pos, self.size ) ) {
106                         return false;
107                 }
108
109                 lLevel = Menu_Cursor_Position_y - self.pos_y;
110                 if( lLevel < _VSB_UP_HEIGHT ) {
111                         _IVSB_ScrollUp();
112                 } else if( lLevel >= self.size_y - _VSB_DOWN_HEIGHT ) {
113                         _IVSB_ScrollDown();
114                 } else {
115                         _IVSB_MouseSelect( lLevel );
116                 }
117                 return true;
118         } else if( pKey == K_MWHEELUP ) {
119                 _IVSB_ScrollUp();
120                 return true;
121         } else if( pKey == K_MWHEELDOWN ) {
122                 _IVSB_ScrollDown();
123                 return true;
124         }
125         return false;
126 };
127
128 void() Item_VScrollBar_Update =
129 {
130         if( self.target == "" ) {
131                 self._target = null_entity;
132         } else if( self.target != self._target.name ) {
133                 self._target = Menu_GetItem( self.target );
134         }
135
136         if( self._target == null_entity ) {
137                 return;
138         }
139
140         // set size height to the height of the scrollwindow's parent
141         self.size_y = self._target.size_y;
142
143         // if the scrollbar isnt needed it becomes invisible
144         if( self.size_y >= self._target._child.size_y ) {
145                 self.flag = self.flag | FLAG_HIDDEN;
146         } else if( self.flag & FLAG_HIDDEN ) {
147                 self.flag = self.flag - FLAG_HIDDEN;
148         }
149 };
150
151 void() Item_VScrollBar_Destroy = {
152         String_EntityFree( self, target );
153 };
154
155 void() Item_VScrollBar_Spawn = {
156         String_EntityZone( self, target );
157         Item_VScrollBar_Update();
158
159         self._update = Item_VScrollBar_Update;
160         self._key = Item_VScrollBar_Key;
161         self._draw = Item_VScrollBar_Draw;
162         self._destroy = Item_VScrollBar_Destroy;
163 };