widget.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. #include "eruta.h"
  2. #include "mem.h"
  3. #include "str.h"
  4. #include "image.h"
  5. #include "dynar.h"
  6. #include "flags.h"
  7. #include "ui.h"
  8. #include "widget.h"
  9. #include "draw.h"
  10. #include "bad.h"
  11. /*
  12. *
  13. * Control of Eruta should be possible comfortably though joystick,
  14. * keyboard, and a single mouse only, so people with physical limitations
  15. * can also play the game.
  16. *
  17. * The following widgets will be needed:
  18. * 1 ) BBConsole to log errors and interact with ruby/state.
  19. * 2 ) Menu (vertical, horizontal)
  20. * 3 ) Ring menu ?
  21. * 4 ) Notification/explanation display.
  22. * 5 ) Talk bubbles.
  23. * 6 ) Talk dialog with text display and option selection.
  24. * User interface is different depending on the game "mode"
  25. * Possible modes could be intro, start menu,
  26. * configuration, image/text display (story mode/cutscenes, etc)
  27. * normal mode, battle mode and menu mode.
  28. *
  29. * Idea for simplification: actually an widget and a mode
  30. * are comparable in that they deal with inpuut each differently, and
  31. * output each in differen ways. Only, a mode is a combination of
  32. * UI elements. For example, the main menu is a static background
  33. * with a vertical menu over it, and the menu has the focus.
  34. *
  35. * Ideas on event handling: The "problem" with allegro is that the
  36. * even types are sparse, and potentially stretch the whole integer range.
  37. * Hence, it's not realistic to use a simple array as a jump table. Some
  38. * more complex structure, like a hsah table, tree, etc, would be needed
  39. * for fast lookup of the event handler function. Hence, and probably by
  40. * design, a case statement is the best way to handle and dispatch the
  41. * input. The react system I wrote has the disadvantage that it will add
  42. * an additional pointer indirection to that switch statement, a problem the
  43. * console doesn't have. Hence, I'll go for a simple event handler
  44. * for every function that takes the allegro event like the console does.
  45. *
  46. * Hence every widget will have tthe following basic interface
  47. * functions:
  48. * free (on destruction)
  49. * done (on deinit)
  50. * event (on any allegro or user event or action)
  51. * update (called every n ticks, when the widget should recalculate it's position, etc)
  52. * draw (called when the widget should draw itself)
  53. *
  54. */
  55. /* Finds the BBWidgetHandler in the actions table for the given type. */
  56. BBWidgetHandler * bbwidgetactions_find(BBWidgetAction * actions, int type) {
  57. while((actions->type != -1) && (actions->handler != NULL)) {
  58. if (actions->type == type) {
  59. return actions->handler;
  60. }
  61. actions++;
  62. }
  63. return NULL;
  64. }
  65. /* Finds the BBWidgetHandler in the actions table for the given type and calls it
  66. * with widget and data as parameters. */
  67. int bbwidgetactions_handle(BBWidgetAction * actions, int type,
  68. BBWidget * widget, void * data) {
  69. BBWidgetHandler * handler = bbwidgetactions_find(actions, type);
  70. if (handler) { return handler(widget, data); }
  71. return BBWIDGET_HANDLE_IGNORE;
  72. }
  73. /** Makes a new style struct. */
  74. Style style_make(Color fore, Color back, Font * font, Image * background) {
  75. Style result = { fore, back, font, background };
  76. return result;
  77. }
  78. /** Initializes a style pointer by copying data from a style struct. */
  79. Style * style_initstyle(Style * self, Style style) {
  80. if(!self) return NULL;
  81. (*self) = style;
  82. return self;
  83. }
  84. /** Initializes a bounds pointer from it's member data. */
  85. Style * style_init( Style * self,
  86. Color fore, Color back, Font * font, Image * background) {
  87. return style_initstyle(self, style_make(fore, back, font, background));
  88. }
  89. /** Get foreground color of style. */
  90. Color style_forecolor(Style * self) {
  91. return self->forecolor;
  92. }
  93. /** Get background color of style. */
  94. Color style_backcolor(Style * self) {
  95. return self->backcolor;
  96. }
  97. /** Get background image of style. */
  98. Image * style_background(Style * self) {
  99. return self->background;
  100. }
  101. /** Get font of style. */
  102. Font * style_font(Style * self) {
  103. return self->font;
  104. }
  105. // typedef struct BBWidgetMethods_ BBWidgetMethods;
  106. /** Get bounds of widget. */
  107. Rebox bbwidget_bounds(BBWidget * self) {
  108. return self->bounds;
  109. }
  110. /** Get width of widget. */
  111. int bbwidget_w(BBWidget * self) {
  112. return rebox_w(&self->bounds);
  113. }
  114. /** Get height of widget. */
  115. int bbwidget_h(BBWidget * self) {
  116. return rebox_h(&self->bounds);
  117. }
  118. /** Get x position of widget. */
  119. int bbwidget_x(BBWidget * self) {
  120. return rebox_x(&self->bounds);
  121. }
  122. /** Get y position of widget. */
  123. int bbwidget_y(BBWidget * self) {
  124. return rebox_y(&self->bounds);
  125. }
  126. /** Get z position of widget. */
  127. int bbwidget_z(BBWidget * self) {
  128. return self->z;
  129. }
  130. /** Get foreground color of widget. */
  131. Color bbwidget_forecolor(BBWidget * self) {
  132. return style_forecolor(&self->style);
  133. }
  134. /** Get backgrund color of widget. */
  135. Color bbwidget_backcolor(BBWidget * self) {
  136. return style_backcolor(&self->style);
  137. }
  138. /** Get font of widget. */
  139. Font * bbwidget_font(BBWidget * self) {
  140. return style_font(&self->style);
  141. }
  142. /** Get background bitmap of widget. */
  143. Image * bbwidget_background(BBWidget * self) {
  144. return style_background(&self->style);
  145. }
  146. /** Gets the flags of a widget. */
  147. int bbwidget_flags(BBWidget * self) {
  148. return self->flags;
  149. }
  150. /** Gets the id of a widget. */
  151. int bbwidget_id(BBWidget * self, int id) {
  152. return self->id;
  153. }
  154. /** Sets all the flags of a widget at once. */
  155. int bbwidget_flags_(BBWidget * self, int flags) {
  156. return self->flags = flags;
  157. }
  158. /** Sets the id of a widget. */
  159. int bbwidget_id_(BBWidget * self, int id) {
  160. return self->id = id;
  161. }
  162. /** Sets an individual flag on the widget. */
  163. int bbwidget_flag(BBWidget * self, int flag) {
  164. return flags_set(&self->flags, flag);
  165. }
  166. /** Unsets an individual flag on the widget. */
  167. int bbwidget_unflag(BBWidget * self, int flag) {
  168. register int wflags = self->flags;
  169. return flags_unset(&self->flags, flag);
  170. }
  171. /** Sets or unsets an individual flag on the widget.
  172. If set is true the flag is set, if false it's unset. */
  173. int bbwidget_doflag(BBWidget * self, int flag, int set) {
  174. return flags_put(&self->flags, flag, set);
  175. }
  176. /** Checks if an individual flag is set */
  177. int bbwidget_flag_p(BBWidget * self, int flag) {
  178. return flags_get(self->flags, flag);
  179. }
  180. /** Checks if the widget is visible or not. */
  181. int bbwidget_visible(BBWidget * self) {
  182. return bbwidget_flag_p(self, BBWIDGET_FLAG_VISIBLE);
  183. }
  184. /** Checks if the widget is listening to input or not. */
  185. int bbwidget_listening(BBWidget * self) {
  186. return bbwidget_flag_p(self, BBWIDGET_FLAG_LISTENING);
  187. }
  188. /** Checks if the widget is active, hat is both visible and
  189. listening to input or not. */
  190. int bbwidget_active(BBWidget * self) {
  191. return bbwidget_flag_p(self, BBWIDGET_FLAG_ACTIVE);
  192. }
  193. /** Checks if the widget is focused or not. */
  194. int bbwidget_focused(BBWidget * self) {
  195. return bbwidget_flag_p(self, BBWIDGET_FLAG_FOCUSED);
  196. }
  197. /** Checks if the widget selected or not. */
  198. int bbwidget_selected(BBWidget * self) {
  199. return bbwidget_flag_p(self, BBWIDGET_FLAG_SELECTED);
  200. }
  201. /** Sets the widget to be visible or not depending on set. */
  202. int bbwidget_visible_(BBWidget * self, int set) {
  203. return bbwidget_doflag(self, BBWIDGET_FLAG_VISIBLE, set);
  204. }
  205. /** Sets the widget if the widget is listening to input or not depending
  206. on set. */
  207. int bbwidget_listening_(BBWidget * self, int set) {
  208. return bbwidget_doflag(self, BBWIDGET_FLAG_VISIBLE, set);
  209. }
  210. /** Sets the widget to be active or not */
  211. int bbwidget_active_(BBWidget * self, int set) {
  212. return bbwidget_doflag(self, BBWIDGET_FLAG_ACTIVE, set);
  213. }
  214. /** Sets if the widget is focused or not. */
  215. int bbwidget_focused_(BBWidget * self, int set) {
  216. return bbwidget_doflag(self, BBWIDGET_FLAG_FOCUSED, set);
  217. }
  218. /* Sets if the widget selected or not. */
  219. int bbwidget_selected_(BBWidget * self, int set) {
  220. return bbwidget_doflag(self, BBWIDGET_FLAG_SELECTED, set);
  221. }
  222. /* Sets up the method cache of a widget based on the actions. */
  223. BBWidget * bbwidget_metabfromacts(BBWidget * self, BBWidgetAction * acts) {
  224. if(!self) return NULL;
  225. if(!acts) return NULL;
  226. self->metab.done = bbwidgetactions_find(acts, BBWIDGET_EVENT_DONE);
  227. self->metab.draw = bbwidgetactions_find(acts, BBWIDGET_EVENT_DRAW);
  228. self->metab.free = bbwidgetactions_find(acts, BBWIDGET_EVENT_FREE);
  229. return self;
  230. }
  231. /* Sets up the acts, and also updates the method cache of a widget */
  232. BBWidget * bbwidget_acts_(BBWidget * self, BBWidgetAction * acts) {
  233. if(!self) return NULL;
  234. self->acts = acts;
  235. return bbwidget_metabfromacts(self, acts);
  236. }
  237. /* Fully initializes a widget. */
  238. BBWidget *
  239. bbwidget_initall(BBWidget * self, int id, BBWidgetAction * acts,
  240. Rebox bounds, Style style) {
  241. if(!self) return NULL;
  242. self->id = id;
  243. self->acts = acts;
  244. bbwidget_metabfromacts(self, acts);
  245. self->bounds = bounds;
  246. self->style = style;
  247. bbwidget_active_(self, TRUE);
  248. return self;
  249. }
  250. /* Initializes a widget with given bounds and style. */
  251. BBWidget *
  252. bbwidget_initbounds(BBWidget * self, int id, BBWidgetAction * acts, Rebox bounds) {
  253. Color fg = color_rgb(0,0,0);
  254. Color bg = color_rgb(255,0,0);
  255. Style style = { fg, bg, NULL, NULL };
  256. return bbwidget_initall(self, id, acts, bounds, style);
  257. }
  258. /* Initializes a widget from another one's bounds and style. */
  259. BBWidget * bbwidget_initparent(BBWidget * self, int id, BBWidget * parent) {
  260. return bbwidget_initall(self, id, parent->acts, parent->bounds, parent->style);
  261. }
  262. /* Allocates a widget */
  263. BBWidget * bbwidget_allocate() {
  264. return STRUCT_ALLOC(BBWidget);
  265. }
  266. /* Call when widget is not needed anymore. */
  267. BBWidget * bbwidget_done(BBWidget * widget) {
  268. // do nothing there as background image and font are NOT owned.
  269. return widget;
  270. }
  271. /* Frees a widget. Calls widget->methods->done(), then mem_free if
  272. the latter returns not NULL. Returns NULL. */
  273. BBWidget * bbwidget_free(BBWidget * self) {
  274. if (self && self->metab.done) {
  275. if(self->metab.done(self, NULL)) {
  276. mem_free(self);
  277. }
  278. }
  279. return NULL;
  280. }
  281. /** Generic widget drawing. */
  282. void bbwidget_draw(BBWidget * self) {
  283. if(self->metab.draw) {
  284. self->metab.draw(self, NULL);
  285. }
  286. }
  287. /** Generic widget event handling. */
  288. void bbwidget_handle(BBWidget * self, ALLEGRO_EVENT * event) {
  289. if(self->acts) {
  290. bbwidgetactions_handle(self->acts, event->any.type, self, event);
  291. }
  292. }
  293. /** Generic widget update. */
  294. void bbwidget_update(BBWidget * self, ALLEGRO_EVENT * event) {
  295. if(self->metab.update) {
  296. self->metab.update(self, event);
  297. }
  298. }
  299. /* Helper struct that keeps track of the BYTE positions within
  300. a c string or USTR where a line or word starts or ends with a given maxwidth. */
  301. typedef struct BBTextInfo_ BBTextInfo;
  302. struct BBTextInfo_ {
  303. int from_char;
  304. int start_char;
  305. int stop_char;
  306. int maxwidth;
  307. };
  308. /* Creates a temporary ustr as per al_ref_ustr but with
  309. start and stop as code positions, not byte positions. */
  310. const USTR *
  311. ustrinfo_newref(
  312. USTR_INFO * uinfo, const USTR * ustr, int start, int stop) {
  313. return ustr_refustr(uinfo, ustr,
  314. ustr_offset(ustr, start),
  315. ustr_offset(ustr, stop)
  316. );
  317. }
  318. /* Creates a temporary ustr that refers ustr but respecds the bounds of the
  319. textinfo (start_char and enc_char) */
  320. const USTR * bbtextinfo_refustr(BBTextInfo * self,
  321. USTR_INFO * uinfo,
  322. const USTR * ustr) {
  323. return ustrinfo_newref(uinfo, ustr, self->start_char, self->stop_char);
  324. }
  325. BBTextInfo *
  326. bbtextinfo_wordfromtext(BBTextInfo * self, USTR * ustr, Font * font) {
  327. int found;
  328. int start_pos;
  329. int end_pos;
  330. int now_char;
  331. int end_char;
  332. int len;
  333. int ch;
  334. if(!self) return NULL;
  335. now_char = self->from_char;
  336. self->start_char = now_char;
  337. ch = ustr_getnext(ustr, &now_char);
  338. while (ch > 0) {
  339. switch(ch) {
  340. case ' ': /* Found a space, here the word ends, include the space. */
  341. self->stop_char = now_char;
  342. return self;
  343. case '\n': /* A newline ends a word, include the newline. */
  344. self->stop_char = now_char;
  345. return self;
  346. default: /* Other characters mean the word is not finished yet. */
  347. break;
  348. }
  349. /* XXX: Should handle the case for languages that use no spaces,
  350. * by checking with al_get_ustr_width but it's not a pressing matter yet.
  351. */
  352. ch = ustr_getnext(ustr, &now_char);
  353. }
  354. // no word found, just set end here and be done.
  355. self->stop_char = now_char;
  356. /* return nULL to signify end */
  357. return NULL;
  358. }
  359. /** Prints a ustring, since puts or printf print too much some
  360. times for a refstring.*/
  361. int ustr_print(USTR * word) {
  362. size_t index;
  363. for(index = 0; index < ustr_length(word) ; index++) {
  364. putchar(ustr_get(word, index));
  365. }
  366. return index;
  367. }
  368. /** Gets the positions of the next line of text fort he given Unicode string
  369. and store them in the given info. If the info's from is set to 0 or less,
  370. the first line is assumed, otherwise, the line will be started from "from".
  371. Uses the given font to determine the width of the text as it is built.
  372. */
  373. BBTextInfo *
  374. bbtextinfo_linefromtext(BBTextInfo * self, USTR * ustr, Font * font) {
  375. BBTextInfo wordinfo;
  376. USTR_INFO lineuinfo;
  377. const USTR * line;
  378. USTR_INFO worduinfo = { 0, 0, 0};
  379. const USTR * word;
  380. int ch;
  381. int index;
  382. int width;
  383. int last_stop;
  384. self->start_char = self->from_char;
  385. wordinfo.from_char = self->from_char;
  386. while(bbtextinfo_wordfromtext(&wordinfo, ustr, font)) {
  387. word = bbtextinfo_refustr(&wordinfo, &worduinfo, ustr);
  388. line = ustrinfo_newref(&lineuinfo, ustr, self->start_char, wordinfo.stop_char);
  389. width = al_get_ustr_width(font, line);
  390. if (width > self->maxwidth) {
  391. /* XXX: handle case of text overflow by bluntly retuning the word as is.
  392. Should split single word based on length too.
  393. There is overflow if this is still the first word as see from wordinfo_start_char.
  394. */
  395. if (wordinfo.start_char == self->start_char) {
  396. self->stop_char = wordinfo.stop_char;
  397. } else {
  398. self->stop_char = wordinfo.start_char;
  399. }
  400. return self;
  401. }
  402. // If we get here, the word way still end on a newline character
  403. // check this case. XXX: It works like this because
  404. // stop_char is a bit wonky... it points at the first character of the
  405. // next word in this case...
  406. ch = ustr_get(ustr, wordinfo.stop_char - 1);
  407. if (ch == '\n') {
  408. self->start_char = self->from_char;
  409. self->stop_char = wordinfo.stop_char - 1;
  410. return self;
  411. }
  412. wordinfo.from_char = wordinfo.stop_char;
  413. }
  414. /* if we get here, the whole string fits. */
  415. self->start_char = self->from_char;
  416. self->stop_char = wordinfo.stop_char;
  417. /* Return NULL to tell caller text has been completely split up. */
  418. return NULL;
  419. }
  420. #define BBWIDGET_BORDER 3
  421. /** Draws a rounded frame as background for a widget. */
  422. void bbwidget_drawroundframe(BBWidget * self) {
  423. if(!self) return;
  424. draw_roundframe(bbwidget_x(self), bbwidget_y(self),
  425. bbwidget_w(self), bbwidget_h(self),
  426. BBWIDGET_BORDER,
  427. bbwidget_forecolor(self), bbwidget_backcolor(self));
  428. }
  429. /** Skips the text info to the next word or line of text. Must be called
  430. when looping over bbtextinfo_linefromtext. */
  431. BBTextInfo * bbtextinfo_next(BBTextInfo * self) {
  432. if(!self) return NULL;
  433. self->from_char = self->stop_char + 1;
  434. self->start_char = self->from_char;
  435. return self;
  436. }
  437. struct BBWidgetBox_ {
  438. struct BBWidget_ widget;
  439. };
  440. #ifndef COMMENT_
  441. /*
  442. * A label is simply a piece of text that is drawn at bounds.x, bounds.y
  443. * using style.font in style.forecolor. No background is drawn.
  444. struct BBWidgetLabel_ {
  445. BBWidget parent;
  446. USTR * text;
  447. };
  448. BBWidgetLabel * widgetlabel_init(BBWidgetLabel * self, BBWidget * parent, Rebox bounds,
  449. const char * text) {
  450. if(!bbwidget_initbounds((BBWidget *)self, bounds)) return NULL;
  451. self->text = ustr_new(text);
  452. return self;
  453. }
  454. BBWidgetLabel * widgetlabel_done(BBWidgetLabel * self) {
  455. if(!self) return NULL;
  456. ustr_free(self->text);
  457. return NULL;
  458. }
  459. struct BBWidgetChoose_ {
  460. BBWidget parent;
  461. Dynar * options;
  462. };
  463. */
  464. #endif
  465. /* A console is a console for command-line interaction and error display. When it's active it captures all input (as long as it's active) */
  466. struct BBConsole_ {
  467. BBWidget widget;
  468. USTRList text;
  469. int count;
  470. int max;
  471. int start;
  472. int charw;
  473. int cursor;
  474. char * buf;
  475. USTR * input;
  476. BBConsoleCommand * command; // called when a command has been entered, if set.
  477. void * command_data; // command data.
  478. };
  479. /* Converts a widget to a console. Only works if the pointer is wrapped correctly,
  480. by a console. */
  481. BBConsole * bbwidget_console(BBWidget * widget) {
  482. if (!widget) return NULL;
  483. return bad_container(widget, BBConsole, widget);
  484. }
  485. /** Sets the console's command function and data. */
  486. void bbconsole_command_(BBConsole * self, BBConsoleCommand * command, void * data) {
  487. self->command = command;
  488. self->command_data = data;
  489. }
  490. /** Let the console perform a command if possible. returns nonzero on error,
  491. zero if OK. */
  492. int bbconsole_docommand(BBConsole * self, const char * text) {
  493. if(!self) return -1;
  494. if(!self->command) return -2;
  495. return self->command(self, text, self->command_data);
  496. }
  497. /** Adds a line of text to the console. */
  498. int bbconsole_addstr(BBConsole * self, const char * str) {
  499. if(!self) return -1;
  500. if(!ustrlist_shiftcstr(&self->text, str)) {
  501. return -3;
  502. }
  503. while(ustrlist_size(&self->text) >= self->max) { // remove last node(s)
  504. ustrlist_droplast(&self->text);
  505. }
  506. return ustrlist_size(&self->text);
  507. }
  508. /** Adds a line of text to the console. */
  509. int bbconsole_addustr(BBConsole * self, const USTR * ustr) {
  510. if(!self) return -1;
  511. if(!ustrlist_shiftustr(&self->text, ustr)) {
  512. return -3;
  513. }
  514. while(ustrlist_size(&self->text) >= self->max) { // remove last node(s)
  515. ustrlist_droplast(&self->text);
  516. }
  517. return ustrlist_size(&self->text);
  518. }
  519. /** Puts a string on the console .*/
  520. int bbconsole_puts(BBConsole * self, const char * str) {
  521. int index;
  522. int size = strlen(str);
  523. int leftsize = size;
  524. int lines = 0;
  525. USTR_INFO uinfo;
  526. BBTextInfo info = { 0, 0, 0, 0};
  527. info.maxwidth = bbwidget_w(&self->widget) - 10;
  528. USTR * ustr;
  529. const USTR * uline;
  530. ustr = ustr_new(str);
  531. while(bbtextinfo_linefromtext(&info, ustr, self->widget.style.font)) {
  532. uline = bbtextinfo_refustr(&info, &uinfo, ustr);
  533. bbconsole_addustr(self, uline);
  534. // don't forget to skip to next line!!!
  535. bbtextinfo_next(&info);
  536. }
  537. uline = bbtextinfo_refustr(&info, &uinfo, ustr);
  538. bbconsole_addustr(self, uline);
  539. ustr_free(ustr);
  540. return lines;
  541. }
  542. #define BBCONSOLE_VPRINTF_MAX 1024
  543. /** Prints a formatted string on the console, truncaded to 1024 characters. */
  544. int bbconsole_vprintf(BBConsole * self, const char * format, va_list args) {
  545. char buffer[BBCONSOLE_VPRINTF_MAX] = { '\0' };
  546. vsnprintf(buffer, BBCONSOLE_VPRINTF_MAX, format, args);
  547. return bbconsole_puts(self, buffer);
  548. }
  549. /** Prints a formatted string on the console, truncaded to 1024 characters. */
  550. int bbconsole_printf(BBConsole * self, const char * format, ...) {
  551. int result;
  552. va_list args;
  553. va_start(args, format);
  554. result = bbconsole_vprintf(self, format, args);
  555. va_end(args);
  556. return result;
  557. }
  558. /** Draws a console. */
  559. int bbconsole_draw(BBWidget * widget, void * data) {
  560. BBConsole * self ;
  561. Font * font ;
  562. Color color ;
  563. USTRListNode * now;
  564. int high, linehigh, index, x, y, skip;
  565. int linew;
  566. if (!bbwidget_visible(widget)) return BBWIDGET_HANDLE_IGNORE;
  567. self = bbwidget_console(widget);
  568. font = bbwidget_font(widget);
  569. color = bbwidget_forecolor(widget);
  570. bbwidget_drawroundframe(widget);
  571. high = bbwidget_h(widget) - 10;
  572. x = bbwidget_x(widget) + 5;
  573. y = bbwidget_y(widget) - 5;
  574. linehigh = font_lineheight(font);
  575. now = ustrlist_head(&self->text);
  576. // skip start lines (to allow scrolling backwards)
  577. now = ustrlist_skipnode(&self->text, self->start);
  578. for (index = high-(linehigh*2); index > 0; index -= linehigh) {
  579. USTR * textstr;
  580. if(!now) break;
  581. textstr = ustrlistnode_ustr(now);
  582. if(textstr) {
  583. font_drawstr(font, color, x, y + index, 0, textstr);
  584. }
  585. now = ustrlistnode_next(now);
  586. }
  587. // draw input string
  588. font_drawstr(font, color, x, y + high - linehigh, 0, self->input);
  589. // Draw cursor
  590. linew = al_get_ustr_width(font, self->input);
  591. al_draw_line(x + linew, y + high - linehigh, x + linew, y + high, color, 1);
  592. // draw start for debugging
  593. al_draw_textf(font, color, x, y, 0, "start: %d, size: %d", self->start,
  594. ustrlist_size(&self->text));
  595. return BBWIDGET_HANDLE_OK;
  596. }
  597. /** Activates or deactivates the console. */
  598. void bbconsole_active_(BBConsole * self, int active) {
  599. if(!self) return;
  600. bbwidget_active_(&self->widget, active);
  601. }
  602. /** Returns nonzero if console is active zero if not. */
  603. int bbconsole_active(BBConsole * self) {
  604. if(!self) return 0;
  605. return bbwidget_active(&self->widget);
  606. }
  607. /** scrolls the console 1 step in the given direction. */
  608. int bbconsole_scroll(BBConsole * self, int direction) {
  609. if((!self) || (!direction)) return FALSE;
  610. if(direction < 0) self->start--;
  611. if(direction > 0) self->start++;
  612. /* Clamp start between 0 and size of list. */
  613. self->start = bad_clampi(self->start, 0, ustrlist_size(&self->text));
  614. return BBWIDGET_HANDLE_OK;
  615. }
  616. /* Key input event handler for console. */
  617. int bbconsole_handle_keychar(BBWidget * widget, void * data) {
  618. BBConsole * self = bbwidget_console(widget);
  619. ALLEGRO_EVENT * event = (ALLEGRO_EVENT *) data;
  620. int ch = event->keyboard.unichar;
  621. int kc = event->keyboard.keycode;
  622. switch(kc) {
  623. // ignore the start-console key
  624. case ALLEGRO_KEY_F1:
  625. case ALLEGRO_KEY_F3:
  626. return BBWIDGET_HANDLE_OK;
  627. case ALLEGRO_KEY_PGUP: return bbconsole_scroll(self, 1);
  628. case ALLEGRO_KEY_PGDN: return bbconsole_scroll(self, -1);
  629. case ALLEGRO_KEY_BACKSPACE:
  630. // remove last character typed.
  631. ustr_remove_chr(self->input, ustr_offset(self->input, -1));
  632. return BBWIDGET_HANDLE_OK;
  633. break;
  634. case ALLEGRO_KEY_ENTER: {
  635. const char * command = ustr_c(self->input);
  636. // execute command
  637. if(bbconsole_docommand(self, command)) {
  638. bbconsole_puts(self, "Error in running comand");
  639. bbconsole_puts(self, command);
  640. }
  641. ustr_truncate(self->input, 0);
  642. // empty string by truncating it
  643. return BBWIDGET_HANDLE_OK;
  644. }
  645. default:
  646. break;
  647. }
  648. ustr_appendch(self->input, ch);
  649. return BBWIDGET_HANDLE_OK;
  650. }
  651. /* Key down event handler for console. */
  652. int bbconsole_handle_keydown(BBWidget * widget, void * data) {
  653. BBConsole * self = bbwidget_console(widget);
  654. ALLEGRO_EVENT * event = (ALLEGRO_EVENT *) data;
  655. int ch = event->keyboard.unichar;
  656. int kc = event->keyboard.keycode;
  657. switch(kc) {
  658. case ALLEGRO_KEY_F1:
  659. case ALLEGRO_KEY_F3:
  660. bbconsole_active_(self, false);
  661. /* disable console if F1 is pressed.
  662. * Note: this shouldnever happen if react is set up well.
  663. */
  664. return BBWIDGET_HANDLE_OK;
  665. default:
  666. break;
  667. }
  668. return BBWIDGET_HANDLE_IGNORE;
  669. }
  670. /* Mouse axe event handler for console */
  671. int bbconsole_handle_mouseaxes(BBWidget * widget, void * data) {
  672. BBConsole * self = bbwidget_console(widget);
  673. ALLEGRO_EVENT * event = (ALLEGRO_EVENT *) data; int z = event->mouse.dz;
  674. // only capture mouse scroll wheel...
  675. if(z == 0) return BBWIDGET_HANDLE_IGNORE;
  676. if(z < 0) return bbconsole_scroll(self, -1);
  677. if(z > 0) return bbconsole_scroll(self, +1);
  678. return BBWIDGET_HANDLE_OK;
  679. }
  680. int bbconsole_handle_ignore(BBWidget * widget, void * data) {
  681. return BBWIDGET_HANDLE_IGNORE;
  682. }
  683. static BBWidgetAction bbconsole_actions[] = {
  684. { ALLEGRO_EVENT_KEY_DOWN , bbconsole_handle_keydown },
  685. { ALLEGRO_EVENT_KEY_UP , bbconsole_handle_ignore },
  686. { ALLEGRO_EVENT_KEY_CHAR , bbconsole_handle_keychar },
  687. { ALLEGRO_EVENT_MOUSE_AXES, bbconsole_handle_mouseaxes },
  688. { BBWIDGET_EVENT_DRAW , bbconsole_draw },
  689. { BBWIDGET_EVENT_DONE , bbconsole_done },
  690. { BBWIDGET_EVENT_FREE , bbconsole_free },
  691. { -1, NULL }
  692. };
  693. /** Let the console handle allegro events. Returns 0 if event was consumed,
  694. positive if not, and negative on error. */
  695. int bbconsole_handle(BBWidget * widget, ALLEGRO_EVENT * event) {
  696. if (!widget) return BBWIDGET_HANDLE_ERROR;
  697. if (!bbwidget_active(widget)) return BBWIDGET_HANDLE_IGNORE;
  698. return bbwidgetactions_handle(bbconsole_actions, event->type, widget, event);
  699. }
  700. /** Cleans up a console. */
  701. int bbconsole_done(BBWidget * widget, void * data) {
  702. BBConsole * self = bbwidget_console(widget);
  703. Lilis * aid;
  704. if(!self) return BBWIDGET_HANDLE_IGNORE;
  705. self->buf = mem_free(self->buf);
  706. ustr_free(self->input);
  707. self->input = NULL;
  708. ustrlist_done(&self->text);
  709. return BBWIDGET_HANDLE_OK;
  710. }
  711. /** Deallocates a console. */
  712. int bbconsole_free(BBWidget * widget, void * data) {
  713. BBConsole * self = bbwidget_console(widget);
  714. bbconsole_done(&self->widget, data);
  715. mem_free(self);
  716. return BBWIDGET_HANDLE_OK;
  717. }
  718. /** Allocates a console. */
  719. BBConsole * bbconsole_alloc() {
  720. return STRUCT_ALLOC(BBConsole);
  721. }
  722. /* Amount of lines of display the console hat o keep track of. */
  723. #define BBCONSOLE_MAX 200
  724. /** Initializes a console. */
  725. BBConsole * bbconsole_initall(BBConsole * self, int id, Rebox bounds, Style style) {
  726. if(!self) return NULL;
  727. if(!bbwidget_initall(&self->widget, id, bbconsole_actions, bounds, style)) {
  728. return NULL;
  729. }
  730. ustrlist_init(&self->text);
  731. // ustrlist_shiftcstr(&self->text, "empty line");
  732. bbwidget_active_(&self->widget, FALSE);
  733. self->count = 0;
  734. // max MUST be at least 2, 3 to see anything...
  735. self->max = BBCONSOLE_MAX;
  736. self->start = 0;
  737. self->charw = 80;
  738. self->buf = mem_alloc(self->charw + 1);
  739. // one extra for NULL at end .
  740. if(!self->buf) { bbconsole_done(&self->widget, NULL); return NULL; }
  741. self->input = ustr_new("");
  742. self->cursor= 0;
  743. if(!self->input) { bbconsole_done(&self->widget, NULL); return NULL; }
  744. self->command = NULL;
  745. self->command_data = NULL;
  746. return self;
  747. }
  748. /** Initializes a new console. */
  749. BBConsole * bbconsole_new(int id, Rebox bounds, Style style) {
  750. BBConsole * self = bbconsole_alloc();
  751. if(!bbconsole_initall(self, id, bounds, style)) {
  752. bbconsole_free(&self->widget, NULL);
  753. return NULL;
  754. }
  755. return self;
  756. }