ASCROLL.CPP 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "mclib.h"
  5. #ifndef ASCROLL_H
  6. #include "ascroll.h"
  7. #endif
  8. aScrollBar::aScrollBar()
  9. {
  10. scrollMax = 0;
  11. scrollPos = 0;
  12. lastY = 0;
  13. }
  14. long aScrollBar::init(long xPos, long yPos, long w, long h)
  15. {
  16. long err;
  17. err = aObject::init(xPos,yPos,w,h);
  18. if (err)
  19. return err;
  20. topButton.setID( aMSG_SCROLLUP );
  21. bottomButton.setID( aMSG_SCROLLDOWN );
  22. scrollTab.setSinglePress();
  23. topButton.setSinglePress();
  24. bottomButton.setSinglePress();
  25. addChild( &topButton );
  26. addChild( &bottomButton );
  27. addChild( &scrollTab );
  28. topButton.setHoldTime( 0.001f );
  29. scrollTab.setHoldTime( 0.001f );
  30. bottomButton.setHoldTime( 0.001f );
  31. topButton.setPressFX( -1 );
  32. bottomButton.setPressFX( -1 );
  33. scrollTab.setPressFX( -1 );
  34. topButton.setHighlightFX( -1 );
  35. bottomButton.setHighlightFX( -1 );
  36. scrollTab.setHighlightFX( -1 );
  37. topButton.setDisabledFX( -1 );
  38. bottomButton.setDisabledFX( -1 );
  39. scrollTab.setDisabledFX( -1 );
  40. scrollTab.setTexture( (unsigned long)0 );
  41. moveTo(xPos,yPos);
  42. SetScrollMax(0);
  43. SetScrollPos(0);
  44. scrollInc = 1;
  45. pageInc = 10;
  46. scrollTab.lightEdge = 0xff005392;
  47. scrollTab.darkEdge = 0xff002D51;
  48. scrollTab.regularColor =0xff004275;
  49. return (NO_ERR);
  50. }
  51. void aScrollBar::destroy()
  52. {
  53. aObject::destroy();
  54. }
  55. void aScrollBar::render()
  56. {
  57. topButton.moveTo( globalX() + 2, topButton.globalY() );
  58. bottomButton.moveTo( globalX() + 2, bottomButton.globalY() );
  59. scrollTab.moveTo( globalX() + 2, scrollTab.globalY() );
  60. aObject::render();
  61. GUI_RECT area = { location[0].x, location[0].y, location[2].x, location[2].y };
  62. drawEmptyRect( area, color, color );
  63. }
  64. void aScrollBar::SetScrollMax(float newMax)
  65. {
  66. scrollMax = newMax;
  67. scrollTab.showGUIWindow(newMax != 0);
  68. ResizeAreas();
  69. }
  70. void aScrollBar::SetScrollPos(float newPos)
  71. {
  72. if (newPos < 0)
  73. newPos = 0;
  74. if (newPos > scrollMax)
  75. newPos = scrollMax;
  76. scrollPos = newPos;
  77. ResizeAreas();
  78. }
  79. void aScrollBar::SetScroll( long newScrollPos )
  80. {
  81. if ( newScrollPos < 0 )
  82. newScrollPos = 0;
  83. if ( newScrollPos > scrollMax )
  84. newScrollPos = scrollMax;
  85. if ( getParent() )
  86. getParent()->handleMessage( aMSG_SCROLLTO, newScrollPos );
  87. SetScrollPos( newScrollPos );
  88. }
  89. void aScrollBar::update()
  90. {
  91. long mouseX = userInput->getMouseX();
  92. long mouseY = userInput->getMouseY();
  93. if ( userInput->isLeftDrag() && lastY ) // dragging the little tab
  94. {
  95. int tmpLastY = mouseY;
  96. tmpLastY -= userInput->getMouseDragY();
  97. tmpLastY += lastY;
  98. float finalPos = (float)tmpLastY;
  99. // figure out what this translates to
  100. float physicalRange = height() - topButton.height() - bottomButton.height() - scrollTab.height() - 2.f;
  101. float RealRange = scrollMax;
  102. if ( !physicalRange )
  103. physicalRange = RealRange;
  104. //Check for what if both of the above are zero. Probably nothing to scroll to, eh?
  105. if ((fabs(physicalRange) > Stuff::SMALL) && (fabs(RealRange) > Stuff::SMALL))
  106. {
  107. float newScrollPos = .5 + (finalPos)*RealRange/physicalRange;
  108. if ( newScrollPos < 0 )
  109. newScrollPos = 0;
  110. if ( newScrollPos > scrollMax )
  111. newScrollPos = scrollMax;
  112. SetScroll( newScrollPos );
  113. scrollTab.press( true );
  114. }
  115. }
  116. else if ( pointInside( mouseX, mouseY ) )
  117. {
  118. if ( userInput->isLeftClick() || gos_GetKeyStatus(KEY_LMOUSE) == KEY_HELD
  119. || userInput->leftMouseReleased() )
  120. {
  121. lastY = 0;
  122. if ( scrollTab.pointInside( mouseX, mouseY ) && !userInput->leftMouseReleased() )
  123. lastY = scrollTab.top() - topButton.bottom();
  124. else if ( getParent() )
  125. {
  126. if ( !topButton.pointInside( mouseX, mouseY )
  127. && !bottomButton.pointInside( mouseX, mouseY )
  128. && !topButton.pointInside( userInput->getMouseDragX(), userInput->getMouseDragY() )
  129. && !bottomButton.pointInside( userInput->getMouseDragX(), userInput->getMouseDragY() )
  130. && mouseY > topButton.globalBottom()
  131. && mouseY < bottomButton.globalY()
  132. && pointInside( userInput->getMouseDragX(), userInput->getMouseDragY() )
  133. && !scrollTab.pointInside( mouseX, mouseY )
  134. && ( userInput->leftMouseReleased() || userInput->getMouseLeftHeld() > .5 ) )
  135. {
  136. // float physicalRange = height() - topButton.height() - bottomButton.height() - scrollTab.height();
  137. // float RealRange = scrollMax;
  138. // float delta = (float)mouseY - (topButton.globalY() + topButton.height());
  139. float newScrollPos = scrollPos;
  140. // if above the thumb, page up, otherwise page down
  141. if ( mouseY < scrollTab.globalY() )
  142. {
  143. newScrollPos = scrollPos - pageInc;
  144. }
  145. else if ( mouseY > scrollTab.globalBottom() )
  146. {
  147. newScrollPos = scrollPos + pageInc;
  148. }
  149. if( newScrollPos < 0 )
  150. newScrollPos = 0;
  151. if( newScrollPos > scrollMax )
  152. newScrollPos = scrollMax;
  153. getParent()->handleMessage( aMSG_SCROLLTO, newScrollPos );
  154. SetScrollPos( newScrollPos );
  155. }
  156. scrollTab.press( 0 );
  157. }
  158. }
  159. }
  160. if ( userInput->leftMouseReleased() )
  161. lastY = 0;
  162. aObject::update();
  163. }
  164. int aScrollBar::handleMessage( unsigned long message, unsigned long who )
  165. {
  166. switch (who )
  167. {
  168. case aMSG_SCROLLUP:
  169. SetScrollPos( scrollPos - scrollInc );
  170. break;
  171. case aMSG_SCROLLDOWN:
  172. SetScrollPos( scrollPos + scrollInc );
  173. break;
  174. }
  175. return getParent()->handleMessage( who, who );
  176. }
  177. void aScrollBar::ResizeAreas(void)
  178. {
  179. float range, position;
  180. if (scrollMax == 0)
  181. return;
  182. float physicalRange = height() - topButton.height() - bottomButton.height() - 6.f;
  183. float RealRange = scrollMax;
  184. pageInc = physicalRange;
  185. float scrollTabSize = physicalRange * physicalRange/(physicalRange + RealRange);
  186. if ( scrollTabSize < scrollTab.width() )
  187. scrollTabSize = scrollTab.width();
  188. scrollTab.resize( scrollTab.width(), scrollTabSize);
  189. range = height() - topButton.height() - bottomButton.height() - scrollTab.height() - 6.f; // one scrollwidth for buttons, one for tab. 2 for lines at either end.
  190. position = range * scrollPos / scrollMax; // center of scroll tab;
  191. scrollTab.moveTo( globalX() + 2, topButton.globalY() + topButton.height() + position + 1);
  192. }
  193. void aScrollBar::Enable( bool enable )
  194. {
  195. scrollTab.disable( !enable );
  196. topButton.disable( !enable );
  197. bottomButton.disable( !enable );
  198. }
  199. long mcScrollBar::init ( long xPos, long yPos, long w, long h )
  200. {
  201. FitIniFile file;
  202. char path[256];
  203. strcpy( path, artPath );
  204. strcat( path, "scrollbar.fit" );
  205. if ( NO_ERR != file.open( path ) )
  206. {
  207. char error[256];
  208. sprintf( error, "couldn't open file %s", path );
  209. Assert( 0, 0, error );
  210. }
  211. setColor( 0, 0 ); // black background
  212. color = 0xff002F55; // outline color
  213. topButton.init( file, "ScrollButton0" );
  214. bottomButton.init( file, "ScrollButton1" );
  215. bottomButton.moveTo( bottomButton.x(), h - bottomButton.height() - 2 );
  216. scrollTab.init( file, "ScrollButton2" );
  217. scrollTab.moveTo( scrollTab.x(), topButton.bottom() + 1 );
  218. file.seekBlock( "Orange" );
  219. orangeInfo[0].init( &file, "Normal" );
  220. orangeInfo[1].init( &file, "Highlight" );
  221. orangeInfo[2].init( &file, "Pressed" );
  222. orangeInfo[3].init( &file, "Disabled" );
  223. file.seekBlock( "Green" );
  224. greenInfo[0].init( &file, "Normal" );
  225. greenInfo[1].init( &file, "Highlight" );
  226. greenInfo[2].init( &file, "Pressed" );
  227. greenInfo[3].init( &file, "Disabled" );
  228. aScrollBar::init( xPos, yPos, w, h );
  229. scrollTab.setTexture( (unsigned long)0 );
  230. return 0;
  231. }
  232. void mcScrollBar::resize(long w, long h)
  233. {
  234. aScrollBar::resize(w, h);
  235. bottomButton.moveTo( bottomButton.globalX(), globalY() + h - bottomButton.height() - 2 );
  236. scrollTab.moveTo( scrollTab.globalX(), topButton.globalY() + topButton.height() + 1 );
  237. ResizeAreas();
  238. }
  239. void mcScrollBar::setGreen()
  240. {
  241. topButton.setAnimationInfo( &greenInfo[0], &greenInfo[1], &greenInfo[2], &greenInfo[3] );
  242. bottomButton.setAnimationInfo( &greenInfo[0], &greenInfo[1], &greenInfo[2], &greenInfo[3] );
  243. scrollTab.setAnimationInfo( &greenInfo[0], &greenInfo[1], &greenInfo[2], &greenInfo[3] );
  244. scrollTab.lightEdge = 0xff6E7C00;
  245. scrollTab.darkEdge = 0xff303600;
  246. scrollTab.regularColor = 0xff586300;
  247. color = ( 0xff6E7C00 );
  248. }
  249. void mcScrollBar::setOrange()
  250. {
  251. topButton.setAnimationInfo( &orangeInfo[0], &orangeInfo[1], &orangeInfo[2], &orangeInfo[3] );
  252. bottomButton.setAnimationInfo( &orangeInfo[0], &orangeInfo[1], &orangeInfo[2], &orangeInfo[3] );
  253. scrollTab.setAnimationInfo( &orangeInfo[0], &orangeInfo[1], &orangeInfo[2], &orangeInfo[3] );
  254. scrollTab.lightEdge = 0xffC66600;
  255. scrollTab.darkEdge = 0xff5C2F00;
  256. scrollTab.regularColor = 0xff9E5200;
  257. color = ( 0xff43311C );
  258. }
  259. void mcScrollButton::render()
  260. {
  261. if ( isShowing() && state != DISABLED )
  262. {
  263. setColor( regularColor );
  264. aButton::render();
  265. GUI_RECT rect = { globalX(), globalY(), globalRight()-1, globalBottom()-1 };
  266. drawEmptyRect( rect, lightEdge, darkEdge );
  267. }
  268. }