OBUTTCUS.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 : OBUTTCUS.CPP
  21. //Description : Custom Button Object
  22. #include <OSYS.h>
  23. #include <OVGA.h>
  24. #include <OMOUSE.h>
  25. #include <OIMGRES.h>
  26. #include <OBUTTCUS.h>
  27. #include <OFONT.h>
  28. ButtonCustom customButton1;
  29. //-------- Begin of function ButtonCustom::ButtonCustom -------//
  30. //
  31. ButtonCustom::ButtonCustom() : custom_para(NULL, 0)
  32. {
  33. init_flag = 0;
  34. enable_flag = 0;
  35. button_key = 0;
  36. }
  37. //--------- End of function ButtonCustom::ButtonCustom -------//
  38. //-------- Begin of function ButtonCustom::create -------//
  39. //
  40. // <int> x1, y1, x2, y2 = coordination of the button
  41. // <ButtonCustomFP> funcPtr = function to be called when need to paint
  42. // <ButtonCustomPara> funcPara = applicated defined integer
  43. // [char] elasticFlag= Whether the button is elastic
  44. // Elastic button will pop up immediately when button release
  45. // Non-elastic button will remain pushed until pop() is called
  46. // (default : 1)
  47. // [char] defIsPushed = default pushed_flag : 1-Pushed, 0-Non-pushed
  48. // (default : 0)
  49. //
  50. void ButtonCustom::create( int pX1, int pY1, int pX2, int pY2,
  51. ButtonCustomFP funcPtr, ButtonCustomPara funcPara, char elasticFlag, char defIsPushed)
  52. {
  53. init_flag = 1;
  54. //--------- set button size --------//
  55. x1 = pX1;
  56. y1 = pY1;
  57. x2 = pX2;
  58. y2 = pY2;
  59. //------ set button parameters -----//
  60. body_fp = funcPtr;
  61. custom_para = funcPara;
  62. elastic_flag = elasticFlag;
  63. pushed_flag = defIsPushed;
  64. enable_flag = 1;
  65. }
  66. //--------- End of function ButtonCustom::create --------//
  67. //----------- Begin of function ButtonCustom::paint -----------//
  68. //
  69. // [int] defIsPushed = default pushed_flag : 1-Pushed, 0-Non-pushed
  70. // (default : pushed_flag)
  71. // [int] repaintBody = parameter passed to body_fp, (default 0)
  72. //
  73. void ButtonCustom::paint(int defIsPushed, int repaintBody)
  74. {
  75. if( !init_flag )
  76. return;
  77. if( !vga.use_back_buf )
  78. mouse.hide_area(x1, y1, x2, y2 );
  79. if( defIsPushed >= 0 )
  80. pushed_flag = defIsPushed;
  81. //------ display the button button -------//
  82. (*body_fp)(this, repaintBody);
  83. //--------------------------------------//
  84. if( !vga.use_back_buf )
  85. mouse.show_area();
  86. }
  87. //---------- End of function ButtonCustom::paint -----------//
  88. //-------- Begin of function ButtonCustom::detect -----------//
  89. //
  90. // Detect whether the button has been pressed,
  91. // if so, act correspondly.
  92. // Check for left mouse button only
  93. //
  94. // [unsigned] keyCode1 = if the specified key is pressed, emulate button pressed
  95. // (default : 0)
  96. //
  97. // [unsigned] keyCode2 = if the specified key is pressed, emulate button pressed
  98. // (default : 0)
  99. //
  100. // [int] detectRight = whether also detect the right button or not
  101. // (default : 0)
  102. //
  103. // [int] suspendPop = don't pop up the button even it should
  104. // (defalut : 0)
  105. //
  106. // Return : 1 - if left mouse button pressed
  107. // 2 - if right mouse button pressed
  108. // 3 - the key is pressed (only when keyCode is specified)
  109. // 0 - if not
  110. //
  111. int ButtonCustom::detect(unsigned keyCode1, unsigned keyCode2, int detectRight, int suspendPop)
  112. {
  113. int rc=0;
  114. if( !init_flag || !enable_flag )
  115. return 0;
  116. if( mouse.any_click(x1,y1,x2,y2,LEFT_BUTTON) )
  117. rc=1;
  118. else if( detectRight && mouse.any_click(x1,y1,x2,y2,RIGHT_BUTTON) )
  119. rc=2;
  120. else if(mouse.key_code)
  121. {
  122. unsigned mouseKey=mouse.key_code;
  123. if( mouseKey >= 'a' && mouseKey <= 'z' ) // non-case sensitive comparsion
  124. mouseKey -= 32; // convert from lower case to upper case
  125. if( mouseKey == keyCode1 || mouseKey == keyCode2 || mouseKey == button_key )
  126. {
  127. rc=3;
  128. }
  129. }
  130. if( !rc )
  131. return 0;
  132. //----- paint the button with pressed shape ------//
  133. #define PRESSED_TIMEOUT_SECONDS 1 // 1 seconds
  134. DWORD timeOutTime = m.get_time()+PRESSED_TIMEOUT_SECONDS*1000;
  135. if( elastic_flag )
  136. {
  137. if( !pushed_flag )
  138. paint(1);
  139. while( (rc==1 && mouse.left_press) || (rc==2 && mouse.right_press) )
  140. {
  141. sys.yield();
  142. mouse.get_event();
  143. if( m.get_time() >= timeOutTime )
  144. break;
  145. }
  146. if( elastic_flag )
  147. paint(0);
  148. }
  149. else // inelastic_flag button
  150. {
  151. if( suspendPop )
  152. pushed_flag = 1;
  153. else
  154. pushed_flag = !pushed_flag;
  155. paint(pushed_flag);
  156. while( (rc==1 && mouse.left_press) || (rc==2 && mouse.right_press) )
  157. {
  158. sys.yield();
  159. mouse.get_event();
  160. if( m.get_time() >= timeOutTime )
  161. break;
  162. }
  163. }
  164. return rc;
  165. }
  166. //----------- End of function ButtonCustom::detect -------------//
  167. //-------- Begin of function ButtonCustom::hide -----------//
  168. //
  169. // Disable and hide the button.
  170. //
  171. void ButtonCustom::hide()
  172. {
  173. if( !init_flag )
  174. return;
  175. vga.blt_buf( x1, y1, x2, y2, 0 );
  176. enable_flag=0;
  177. }
  178. //----------- End of function ButtonCustom::hide -------------//
  179. //----------- Begin of function ButtonCustom::disp_text_button_func -------------//
  180. //
  181. // a custom function to disp button with text only
  182. //
  183. // button->custom_para.ptr is the pointer of char
  184. //
  185. void ButtonCustom::disp_text_button_func(ButtonCustom *button, int repaintBody)
  186. {
  187. int x1 = button->x1;
  188. int y1 = button->y1;
  189. int x2 = button->x2;
  190. int y2 = button->y2;
  191. // modify x1,y1, x2,y2 to the button body
  192. if( button->pushed_flag )
  193. {
  194. vga.d3_panel2_down(x1, y1, x2, y2);
  195. x1++;
  196. y1++;
  197. }
  198. else
  199. {
  200. vga.d3_panel2_up(button->x1, button->y1, button->x2, button->y2);
  201. x2--;
  202. y2--;
  203. }
  204. // put name
  205. font_bible.center_put(x1, y1, x2, y2, (char *)button->custom_para.ptr );
  206. }
  207. //----------- End of function ButtonCustom::disp_text_button_func -------------//
  208. // ............................................................
  209. //-------- Begin of function ButtonCustomGroup::ButtonCustomGroup -------//
  210. ButtonCustomGroup::ButtonCustomGroup(int buttonNum)
  211. {
  212. button_pressed = 0;
  213. button_num = buttonNum;
  214. button_array = new ButtonCustom[buttonNum];
  215. }
  216. //---------- End of function ButtonCustomGroup::ButtonCustomGroup -------//
  217. //----------- Begin of function ButtonCustomGroup::~ButtonCustomGroup -----------//
  218. //
  219. ButtonCustomGroup::~ButtonCustomGroup()
  220. {
  221. delete[] button_array;
  222. }
  223. //-------------- End of function ButtonCustomGroup::~ButtonCustomGroup ----------//
  224. //--------- Begin of function ButtonCustomGroup::paint ----------//
  225. //
  226. // Paint all buttons in this button nation.
  227. //
  228. // [int] buttonPressed = the default pressed button
  229. // (default no change to button_pressed)
  230. //
  231. void ButtonCustomGroup::paint(int buttonPressed)
  232. {
  233. int i;
  234. if( buttonPressed >= 0 )
  235. button_pressed = buttonPressed;
  236. for( i=0 ; i<button_num ; i++ )
  237. button_array[i].paint(button_pressed==i);
  238. }
  239. //----------- End of function ButtonCustomGroup::paint ----------//
  240. //--------- Begin of function ButtonCustomGroup::detect ----------//
  241. //
  242. // Detect all buttons in this button nation.
  243. // Since only one button can be pressed at one time,
  244. // so if any one of them is pressed, the previously pressed one
  245. // will be pop up.
  246. //
  247. // Return : <int> -1 - if no button pressed
  248. // >=0 - the record no. of the button pressed
  249. //
  250. int ButtonCustomGroup::detect()
  251. {
  252. int i;
  253. for( i=0 ; i<button_num ; i++ )
  254. {
  255. if( !button_array[i].pushed_flag && button_array[i].detect() )
  256. {
  257. button_array[button_pressed].pop();
  258. button_pressed = i;
  259. return i;
  260. }
  261. }
  262. return -1;
  263. }
  264. //----------- End of function ButtonCustomGroup::detect ----------//
  265. //--------- Begin of function ButtonCustomGroup::push ----------//
  266. //
  267. // Push the specified button.
  268. //
  269. // <int> buttonId = Id. of the button.
  270. //
  271. void ButtonCustomGroup::push(int buttonId, int paintFlag)
  272. {
  273. int i;
  274. button_pressed = buttonId;
  275. if( paintFlag )
  276. for( i=0 ; i<button_num ; i++ )
  277. {
  278. if( i==buttonId )
  279. button_array[i].push();
  280. else
  281. button_array[i].pop();
  282. }
  283. else
  284. for( i=0 ; i<button_num ; i++ )
  285. {
  286. if( i==buttonId )
  287. button_array[i].pushed_flag = 1;
  288. else
  289. button_array[i].pushed_flag = 0;
  290. }
  291. }
  292. //----------- End of function ButtonCustomGroup::push ----------//
  293. //--------- Begin of function ButtonCustomGroup::operator[] ----------//
  294. //
  295. // <int> buttonId = Id. of the button, start from 0
  296. //
  297. ButtonCustom& ButtonCustomGroup::operator[](int buttonId)
  298. {
  299. err_when( buttonId<0 || buttonId >= button_num );
  300. return button_array[buttonId];
  301. }
  302. //----------- End of function ButtonCustomGroup::operator[] ----------//