gui.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*******************************************************************************
  2. * Emu51
  3. * gui.cpp:
  4. * Created by mlt on 22/03/23.
  5. ******************************************************************************/
  6. #include <allegro.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <emu51.h>
  10. #include <gui.h>
  11. // Returns state of a single bit which is on 'pos' position in tmp_b byte
  12. bool checkBit(uint8_t tmp_b, int pos)
  13. {
  14. if ( tmp_b & ( 1 << pos ))
  15. {
  16. return true;
  17. }
  18. else
  19. {
  20. return false;
  21. }
  22. }
  23. void draw_SEG_digit(BITMAP *buf, int x, int y, int color, int size, uint8_t segm_code)
  24. {
  25. int darkc, normc;
  26. normc = color;
  27. darkc = makecol(getr(normc) / 4, getg(normc) / 4, getb(normc) / 4);
  28. if ( checkBit(segm_code, 1))
  29. {
  30. color = normc;
  31. }
  32. else
  33. {
  34. color = darkc;
  35. }
  36. vline(buf, x + size, y, y + size, color);
  37. if ( checkBit(segm_code, 2))
  38. {
  39. color = normc;
  40. }
  41. else
  42. {
  43. color = darkc;
  44. }
  45. vline(buf, x + size, y + size, y + 2 * size, color);
  46. if ( checkBit(segm_code, 4))
  47. {
  48. color = normc;
  49. }
  50. else
  51. {
  52. color = darkc;
  53. }
  54. vline(buf, x, y + 2 * size, y + size, color);
  55. if ( checkBit(segm_code, 5))
  56. {
  57. color = normc;
  58. }
  59. else
  60. {
  61. color = darkc;
  62. }
  63. vline(buf, x, y + size, y, color);
  64. if ( checkBit(segm_code, 6))
  65. {
  66. color = normc;
  67. }
  68. else
  69. {
  70. color = darkc;
  71. }
  72. hline(buf, x, y + size, x + size, color);
  73. if ( checkBit(segm_code, 0))
  74. {
  75. color = normc;
  76. }
  77. else
  78. {
  79. color = darkc;
  80. }
  81. hline(buf, x, y, x + size, color);
  82. if ( checkBit(segm_code, 3))
  83. {
  84. color = normc;
  85. }
  86. else
  87. {
  88. color = darkc;
  89. }
  90. hline(buf, x + size, y + 2 * size, x, color);
  91. if ( checkBit(segm_code, 7))
  92. {
  93. color = normc;
  94. }
  95. else
  96. {
  97. color = darkc;
  98. }
  99. putpixel(buf, x + size + 2, y + 2 * size, color);
  100. }
  101. void draw_LED_bin(BITMAP *buf, int x, int y, int color, uint8_t code)
  102. {
  103. int darkc, normc;
  104. int i;
  105. normc = color;
  106. darkc = makecol(getr(normc) / 4, getg(normc) / 4, getb(normc) / 4);
  107. for ( i = 0 ; i < 8 ; i++ )
  108. {
  109. if ( checkBit(code, 7 - i))
  110. {
  111. color = normc;
  112. }
  113. else
  114. {
  115. color = darkc;
  116. }
  117. circlefill(buf, x + 7 * i, y, 2, color);
  118. }
  119. }
  120. void change_char(char *string, char ch, int pos)
  121. {
  122. string[pos - 1] = ch;
  123. }
  124. void insert_char(char *string, char ch, int pos, int length)
  125. {
  126. for ( int c = length ; c > pos ; c-- )
  127. {
  128. string[c - 1] = string[c - 2];
  129. }
  130. string[pos - 1] = ch;
  131. }
  132. void cut_char(char *string, int pos, int length)
  133. {
  134. for ( int c = pos ; c < length ; c++ )
  135. {
  136. string[c - 1] = string[c];
  137. }
  138. string[length - 1] = (char) ( 32 );
  139. }
  140. void change_ext(char *s)
  141. {
  142. int c;
  143. for ( c = 254 ; c >= 0 ; c-- )
  144. {
  145. if ( s[c] == '.' )
  146. {
  147. s[c + 1] = 'h';
  148. s[c + 2] = 'e';
  149. s[c + 3] = 'x';
  150. break;
  151. }
  152. }
  153. return;
  154. }
  155. void GetText(char *text, BITMAP *buf, int x, int y, int w, int h, int length, char *title)
  156. {
  157. int c;
  158. BITMAP *surface = create_bitmap(w, h);
  159. BITMAP *cache = create_bitmap(w, h);
  160. blit(buf, cache, x, y, 0, 0, w, h);
  161. clear(surface);
  162. hline(surface, 2, surface->h - 2, surface->w - 2, makecol(255, 255, 255));
  163. vline(surface, 2, 2, surface->h - 2, makecol(255, 255, 255));
  164. vline(surface, surface->w - 2, 2, surface->h - 2, makecol(255, 255, 255));
  165. for ( c = 2 ; c <= 2 + 10 ; c += 2 )
  166. {
  167. hline(surface, 2, c, surface->w - 2, makecol(255, 255, 255));
  168. }
  169. textprintf_centre(surface, mainf, surface->w / 2, 4, makecol(255, 255, 255), " %s ", title);
  170. text[0] = 32;
  171. for ( c = 1 ; c <= length - 1 ; c++ )
  172. {
  173. text[c] = 32;
  174. }
  175. text[length - 1] = 0;
  176. clear_keybuf();
  177. int pos = 1;
  178. uint16_t key_p;
  179. uint16_t key_ch;
  180. bool flag;
  181. while ( key[KEY_ENTER] )
  182. {
  183. }
  184. while ( !key[KEY_ENTER] )
  185. {
  186. textprintf(surface, mainf, 15, surface->h / 2, makecol(255, 255, 255), "%s", text);
  187. hline(surface, 15 + ( pos - 1 ) * 8, surface->h / 2 + 8, 15 + pos * 8, makecol(255, 255, 255));
  188. blit(surface, buf, 0, 0, x, y, w, h);
  189. hline(surface, 15 + ( pos - 1 ) * 8, surface->h / 2 + 8, 15 + pos * 8, makecol(0, 0, 0));
  190. key_p = readkey();
  191. key_ch = key_p & 0xff;
  192. key_p = key_p >> 8;
  193. flag = false;
  194. switch ( key_p )
  195. {
  196. case KEY_BACKSPACE:
  197. if ( pos > 1 )
  198. {
  199. pos--;
  200. cut_char(text, pos, length);
  201. text[length - 2] = 32;
  202. text[length - 1] = 0;
  203. }
  204. flag = true;
  205. break;
  206. case KEY_DEL:
  207. cut_char(text, pos, length);
  208. text[length - 2] = 32;
  209. text[length - 1] = 0;
  210. flag = true;
  211. break;
  212. case KEY_LEFT:
  213. if ( pos > 1 )
  214. { pos--; }
  215. flag = true;
  216. break;
  217. case KEY_RIGHT:
  218. if ( pos < length )
  219. { pos++; }
  220. flag = true;
  221. break;
  222. }
  223. if ( !key[KEY_ENTER] && key_ch != 0 && pos < length && !flag )
  224. {
  225. insert_char(text, key_ch, pos, length);
  226. pos++;
  227. text[length - 1] = 0;
  228. }
  229. }
  230. text[length - 1] = 0;
  231. blit(cache, buf, 0, 0, x, y, w, h);
  232. destroy_bitmap(cache);
  233. destroy_bitmap(surface);
  234. }
  235. void ShowMessage(char *text, BITMAP *buf, int x, int y, int w, int h, char *title)
  236. {
  237. BITMAP *surface = create_bitmap(w, h);
  238. BITMAP *cache = create_bitmap(w, h);
  239. blit(buf, cache, x, y, 0, 0, w, h);
  240. clear(surface);
  241. hline(surface, 2, surface->h - 2, surface->w - 2, makecol(255, 255, 255));
  242. vline(surface, 2, 2, surface->h - 2, makecol(255, 255, 255));
  243. vline(surface, surface->w - 2, 2, surface->h - 2, makecol(255, 255, 255));
  244. for ( int c = 2 ; c <= 2 + 10 ; c += 2 )
  245. {
  246. hline(surface, 2, c, surface->w - 2, makecol(255, 255, 255));
  247. }
  248. textprintf_centre(surface, mainf, surface->w / 2, 4, makecol(255, 255, 255), " %s ", title);
  249. textprintf(surface, mainf, 15, surface->h / 2, makecol(255, 255, 255), "%s", text);
  250. textprintf_centre(surface, mainf, surface->w / 2, surface->h - 16, makecol(255, 0, 0), "Press ENTER");
  251. blit(surface, buf, 0, 0, x, y, w, h);
  252. while ( key[KEY_ENTER] )
  253. {
  254. }
  255. while ( !key[KEY_ENTER] )
  256. {
  257. }
  258. blit(cache, buf, 0, 0, x, y, w, h);
  259. destroy_bitmap(cache);
  260. destroy_bitmap(surface);
  261. }
  262. bool QuestionBox(char *text, BITMAP *buf, int x, int y, int w, int h, char *title, int _color)
  263. {
  264. BITMAP *surface = create_bitmap(w, h);
  265. BITMAP *cache = create_bitmap(w, h);
  266. int len, c, line = 0, pos = 0;
  267. blit(buf, cache, x, y, 0, 0, w, h);
  268. clear(surface);
  269. hline(surface, 2, surface->h - 2, surface->w - 2, makecol(255, 255, 255));
  270. vline(surface, 2, 2, surface->h - 2, makecol(255, 255, 255));
  271. vline(surface, surface->w - 2, 2, surface->h - 2, makecol(255, 255, 255));
  272. for ( c = 2 ; c <= 2 + 10 ; c += 2 )
  273. {
  274. hline(surface, 2, c, surface->w - 2, makecol(255, 255, 255));
  275. }
  276. textprintf_centre(surface, mainf, surface->w / 2, 4, makecol(255, 255, 255), " %s ", title);
  277. textprintf_centre(surface, mainf, surface->w / 2, surface->h - 16, makecol(255, 0, 0),
  278. "Press ENTER (OK) or ESC (Cancel)");
  279. len = (int) strlen(text);
  280. for ( c = 0 ; c < len ; c++ )
  281. {
  282. if ( text[c] == 10 )
  283. {
  284. line++;
  285. pos = 0;
  286. }
  287. else
  288. {
  289. if ( text[c] != 13 )
  290. {
  291. if ( text[c] > 126 || text[c] < 32 )
  292. { break; }
  293. textprintf(surface, mainf, 15 + pos * 8, 20 + line * 10, _color, "%c", text[c]);
  294. pos++;
  295. }
  296. }
  297. }
  298. blit(surface, buf, 0, 0, x, y, w, h);
  299. while ( key[KEY_ENTER] || key[KEY_ESC] )
  300. {
  301. }
  302. while ( !( key[KEY_ENTER] || key[KEY_ESC] ))
  303. {
  304. }
  305. if ( key[KEY_ENTER] )
  306. {
  307. return true;
  308. }
  309. else
  310. {
  311. return false;
  312. }
  313. blit(cache, buf, 0, 0, x, y, w, h);
  314. destroy_bitmap(cache);
  315. destroy_bitmap(surface);
  316. }
  317. void ShowMsgEx(char *text, BITMAP *buf, int x, int y, int w, int h, char *title, int _color)
  318. {
  319. BITMAP *surface = create_bitmap(w, h);
  320. BITMAP *cache = create_bitmap(w, h);
  321. int len, c, line = 0, pos = 0;
  322. blit(buf, cache, x, y, 0, 0, w, h);
  323. clear(surface);
  324. hline(surface, 2, surface->h - 2, surface->w - 2, makecol(255, 255, 255));
  325. vline(surface, 2, 2, surface->h - 2, makecol(255, 255, 255));
  326. vline(surface, surface->w - 2, 2, surface->h - 2, makecol(255, 255, 255));
  327. for ( c = 2 ; c <= 2 + 10 ; c += 2 )
  328. {
  329. hline(surface, 2, c, surface->w - 2, makecol(255, 255, 255));
  330. }
  331. textprintf_centre(surface, mainf, surface->w / 2, 4, makecol(255, 255, 255), " %s ", title);
  332. textprintf_centre(surface, mainf, surface->w / 2, surface->h - 16, makecol(255, 0, 0), "Press ENTER");
  333. len = (int) strlen(text);
  334. for ( c = 0 ; c < len ; c++ )
  335. {
  336. if ( text[c] == 10 )
  337. {
  338. line++;
  339. pos = 0;
  340. }
  341. else
  342. {
  343. if ( text[c] != 13 )
  344. {
  345. if ( text[c] > 126 || text[c] < 32 )
  346. { break; }
  347. textprintf(surface, mainf, 15 + pos * 8, 20 + line * 10, _color, "%c", text[c]);
  348. pos++;
  349. }
  350. }
  351. }
  352. blit(surface, buf, 0, 0, x, y, w, h);
  353. while ( key[KEY_ENTER] )
  354. {
  355. }
  356. while ( !key[KEY_ENTER] )
  357. {
  358. }
  359. blit(cache, buf, 0, 0, x, y, w, h);
  360. destroy_bitmap(cache);
  361. destroy_bitmap(surface);
  362. }