OBUTT3D.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OBUTT3D.CPP
  21. //Description : 3D Button Object
  22. #include <OSYS.h>
  23. #include <OVGA.h>
  24. #include <OHELP.h>
  25. #include <OMOUSE.h>
  26. #include <OIMGRES.h>
  27. #include <OBUTT3D.h>
  28. //---------- define static vars ----------//
  29. static char save_back_buf[BUTTON_ACTION_WIDTH * BUTTON_ACTION_HEIGHT];
  30. //-------- Begin of function Button3D::Button3D -------//
  31. //
  32. Button3D::Button3D()
  33. {
  34. init_flag = 0;
  35. enable_flag = 0;
  36. button_key = 0;
  37. }
  38. //--------- End of function Button3D::Button3D -------//
  39. //-------- Begin of function Button3D::create -------//
  40. //
  41. // Create BUTTON_TYPE_SHELL button.
  42. //
  43. // create(<int>,<int>,<int>,<int>,<char>)
  44. //
  45. // <int> x1, y1 = coordination of the button
  46. // <char> buttonStyle = style id. of the button
  47. // <char*> buttonName = name of the button
  48. // [int] elasticFlag= Whether the button is elastic
  49. // Elastic button will pop up immediately when button release
  50. // Non-elastic button will remain pushed until pop() is called
  51. // (default : 1)
  52. // [int] defIsPushed = default pushed_flag : 1-Pushed, 0-Non-pushed
  53. // (default : 0)
  54. //
  55. void Button3D::create(int pX1, int pY1, char buttonStyle,
  56. char* buttonName, char elasticFlag, char defIsPushed)
  57. {
  58. init_flag = 1;
  59. //------ get the button bitmap ------//
  60. char buttonFileName[] = "BUTUP_A";
  61. buttonFileName[6] = buttonStyle; // up button bitmap
  62. button_up_ptr = image_button.get_ptr(buttonFileName);
  63. buttonFileName[3] = 'D'; // down button bitmap
  64. buttonFileName[4] = 'N';
  65. button_down_ptr = image_button.get_ptr(buttonFileName);
  66. buttonFileName[3] = 'D'; // disabled button bitmap
  67. buttonFileName[4] = 'S';
  68. button_disabled_ptr = image_button.get_ptr(buttonFileName);
  69. //--------- set button size --------//
  70. short* iconPtr = (short*) button_up_ptr;
  71. x1 = pX1;
  72. y1 = pY1;
  73. x2 = x1+ *iconPtr++ - 1;
  74. y2 = y1+ *iconPtr - 1;
  75. //------ set button parameters -----//
  76. button_type = BUTTON_TYPE_SHELL;
  77. button_style = buttonStyle;
  78. elastic_flag = elasticFlag;
  79. pushed_flag = defIsPushed;
  80. enable_flag = 1;
  81. err_when( strlen(buttonName) > 8 );
  82. strcpy( help_code, buttonName );
  83. icon_ptr = image_button.get_ptr(buttonName);
  84. }
  85. //--------- End of function Button3D::create --------//
  86. //-------- Begin of function Button3D::create -------//
  87. //
  88. // Create BUTTON_TYPE_BITMAP button.
  89. //
  90. void Button3D::create(int pX1, int pY1, char* upButtonName,
  91. char* downButtonName, char elasticFlag, char defIsPushed)
  92. {
  93. init_flag = 1;
  94. //--------- set button size --------//
  95. button_up_ptr = image_button.get_ptr(upButtonName);
  96. button_down_ptr = image_button.get_ptr(downButtonName);
  97. err_when( !button_up_ptr );
  98. err_when( !button_down_ptr );
  99. x1 = pX1;
  100. y1 = pY1;
  101. x2 = x1+ *((short*)button_up_ptr) - 1;
  102. y2 = y1+ *((short*)(button_up_ptr)+1) - 1;
  103. //------ set button parameters -----//
  104. button_type = BUTTON_TYPE_BITMAP;
  105. elastic_flag = elasticFlag;
  106. pushed_flag = defIsPushed;
  107. enable_flag = 1;
  108. err_when( strlen(upButtonName) > 8 );
  109. strcpy( help_code, upButtonName );
  110. }
  111. //--------- End of function Button3D::create --------//
  112. //-------- Begin of function Button3D::update_bitmap -------//
  113. //
  114. // Update the bitmap of the button and repaint the button.
  115. //
  116. void Button3D::update_bitmap(char* buttonName)
  117. {
  118. err_when( strlen(buttonName) > 8 );
  119. strcpy( help_code, buttonName );
  120. icon_ptr = image_button.get_ptr(buttonName);
  121. paint();
  122. }
  123. //--------- End of function Button3D::update_bitmap --------//
  124. //-------- Begin of function Button3D::set_help_code -------//
  125. //
  126. void Button3D::set_help_code(char* helpCode)
  127. {
  128. strncpy( help_code, helpCode, HELP_CODE_LEN );
  129. help_code[HELP_CODE_LEN] = NULL;
  130. }
  131. //--------- End of function Button3D::set_help_code --------//
  132. //----------- Begin of function Button3D::paint -----------//
  133. //
  134. // [int] defIsPushed = default pushed_flag : 1-Pushed, 0-Non-pushed
  135. // (default : pushed_flag)
  136. //
  137. void Button3D::paint(int defIsPushed)
  138. {
  139. if( !init_flag )
  140. return;
  141. if( !vga.use_back_buf )
  142. mouse.hide_area(x1, y1, x2, y2 );
  143. if( defIsPushed >= 0 )
  144. pushed_flag = defIsPushed;
  145. //------ display the button button -------//
  146. if( button_type == BUTTON_TYPE_SHELL && icon_ptr )
  147. {
  148. int tx = x1 + ((x2-x1+1) - *((short*)icon_ptr))/2;
  149. int ty = y1 + ((y2-y1+1) - *((short*)icon_ptr+1))/2;
  150. tx -= 2;
  151. ty -= 3;
  152. //----------------------------------//
  153. if( enable_flag )
  154. {
  155. if( pushed_flag )
  156. Vga::active_buf->put_bitmap_trans_decompress( x1, y1, button_down_ptr );
  157. else
  158. Vga::active_buf->put_bitmap_trans_decompress( x1, y1, button_up_ptr );
  159. Vga::active_buf->put_bitmap_trans_decompress( tx, ty, (char*) icon_ptr ); // 0 means not clear background
  160. }
  161. else // button disabled
  162. {
  163. //--- put it on the back buffer, darken it and blt it back to the front buffer ---//
  164. if( !vga.use_back_buf )
  165. vga_back.read_bitmap( x1, y1, x1+BUTTON_ACTION_WIDTH-1, y1+BUTTON_ACTION_HEIGHT-1, save_back_buf );
  166. //------ display and darken ----//
  167. vga_back.put_bitmap_trans_decompress( x1, y1, button_up_ptr );
  168. vga_back.put_bitmap_trans_decompress( tx, ty, (char*) icon_ptr ); // 0 means not clear background
  169. vga_back.adjust_brightness( x1+1, y1+1, x1+BUTTON_ACTION_WIDTH-4, y1+BUTTON_ACTION_HEIGHT-2, -5 );
  170. //--- blt the button from the back to the front and restore the back buf ---//
  171. if( !vga.use_back_buf )
  172. {
  173. vga.blt_buf( x1, y1, x1+BUTTON_ACTION_WIDTH-1, y1+BUTTON_ACTION_HEIGHT-1, 0 );
  174. vga_back.put_bitmap( x1, y1, save_back_buf );
  175. }
  176. }
  177. }
  178. else if( button_type == BUTTON_TYPE_BITMAP )
  179. {
  180. if( pushed_flag )
  181. Vga::active_buf->put_bitmap_trans_decompress( x1, y1, button_down_ptr );
  182. else
  183. Vga::active_buf->put_bitmap_trans_decompress( x1, y1, button_up_ptr );
  184. }
  185. //--------------------------------------//
  186. if( !vga.use_back_buf )
  187. mouse.show_area();
  188. }
  189. //---------- End of function Button3D::paint -----------//
  190. //-------- Begin of function Button3D::detect -----------//
  191. //
  192. // Detect whether the button has been pressed,
  193. // if so, act correspondly.
  194. // Check for left mouse button only
  195. //
  196. // [unsigned] keyCode1 = if the specified key is pressed, emulate button pressed
  197. // (default : 0)
  198. //
  199. // [unsigned] keyCode2 = if the specified key is pressed, emulate button pressed
  200. // (default : 0)
  201. //
  202. // [int] detectRight = whether also detect the right button or not
  203. // (default : 0)
  204. //
  205. // [int] suspendPop = don't pop up the button even it should
  206. // (defalut : 0)
  207. //
  208. // Return : 1 - if left mouse button pressed
  209. // 2 - if right mouse button pressed
  210. // 3 - the key is pressed (only when keyCode is specified)
  211. // 0 - if not
  212. //
  213. int Button3D::detect(unsigned keyCode1, unsigned keyCode2, int detectRight, int suspendPop)
  214. {
  215. int rc=0;
  216. if( !init_flag )
  217. return 0;
  218. help.set_help( x1, y1, x2, y2, help_code );
  219. if( !enable_flag )
  220. return 0;
  221. //---------------------------------------------//
  222. if( mouse.any_click(x1,y1,x2,y2,LEFT_BUTTON) )
  223. rc=1;
  224. else if( detectRight && mouse.any_click(x1,y1,x2,y2,RIGHT_BUTTON) )
  225. rc=2;
  226. else if(mouse.key_code)
  227. {
  228. unsigned mouseKey=mouse.key_code;
  229. if( mouseKey >= 'a' && mouseKey <= 'z' ) // non-case sensitive comparsion
  230. mouseKey -= 32; // convert from lower case to upper case
  231. if( mouseKey == keyCode1 || mouseKey == keyCode2 || mouseKey == button_key )
  232. {
  233. rc=3;
  234. }
  235. }
  236. if( !rc )
  237. return 0;
  238. //----- paint the button with pressed shape ------//
  239. #define PRESSED_TIMEOUT_SECONDS 1 // 1 seconds
  240. DWORD timeOutTime = m.get_time()+PRESSED_TIMEOUT_SECONDS*1000;
  241. if( elastic_flag )
  242. {
  243. if( !pushed_flag )
  244. paint(1);
  245. while( (rc==1 && mouse.left_press) || (rc==2 && mouse.right_press) )
  246. {
  247. sys.yield();
  248. mouse.get_event();
  249. if( m.get_time() >= timeOutTime )
  250. break;
  251. }
  252. if( elastic_flag )
  253. paint(0);
  254. }
  255. else // inelastic_flag button
  256. {
  257. if( suspendPop )
  258. pushed_flag = 1;
  259. else
  260. pushed_flag = !pushed_flag;
  261. paint(pushed_flag);
  262. while( (rc==1 && mouse.left_press) || (rc==2 && mouse.right_press) )
  263. {
  264. sys.yield();
  265. mouse.get_event();
  266. if( m.get_time() >= timeOutTime )
  267. break;
  268. }
  269. }
  270. return rc;
  271. }
  272. //----------- End of function Button3D::detect -------------//
  273. //-------- Begin of function Button3D::hide -----------//
  274. //
  275. // Disable and hide the button.
  276. //
  277. void Button3D::hide()
  278. {
  279. if( !init_flag )
  280. return;
  281. vga.blt_buf( x1, y1, x2, y2, 0 );
  282. enable_flag=0;
  283. }
  284. //----------- End of function Button3D::hide -------------//