button.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * ttk button widget
  7. */
  8. #include <toaru/graphics.h>
  9. #include <toaru/button.h>
  10. #include <toaru/sdf.h>
  11. #include <toaru/icon_cache.h>
  12. void ttk_button_draw(gfx_context_t * ctx, struct TTKButton * button) {
  13. if (button->width == 0) {
  14. return;
  15. }
  16. int hilight = button->hilight & 0xFF;
  17. int disabled = button->hilight & 0x100;
  18. /* Dark edge */
  19. if (hilight < 3) {
  20. struct gradient_definition edge = {button->height, button->y, rgb(166,166,166), rgb(136,136,136)};
  21. draw_rounded_rectangle_pattern(ctx, button->x, button->y, button->width, button->height, 4, gfx_vertical_gradient_pattern, &edge);
  22. }
  23. /* Sheen */
  24. if (hilight < 2) {
  25. draw_rounded_rectangle(ctx, button->x + 1, button->y + 1, button->width - 2, button->height - 2, 3, rgb(238,238,238));
  26. /* Button face - this should normally be a gradient */
  27. if (hilight == 1) {
  28. struct gradient_definition face = {button->height-3, button->y + 2, rgb(240,240,240), rgb(230,230,230)};
  29. draw_rounded_rectangle_pattern(ctx, button->x + 2, button->y + 2, button->width - 4, button->height - 3, 2, gfx_vertical_gradient_pattern, &face);
  30. } else {
  31. struct gradient_definition face = {button->height-3, button->y + 2, rgb(219,219,219), rgb(204,204,204)};
  32. draw_rounded_rectangle_pattern(ctx, button->x + 2, button->y + 2, button->width - 4, button->height - 3, 2, gfx_vertical_gradient_pattern, &face);
  33. }
  34. } else if (hilight == 2) {
  35. struct gradient_definition face = {button->height-2, button->y + 1, rgb(180,180,180), rgb(160,160,160)};
  36. draw_rounded_rectangle_pattern(ctx, button->x + 1, button->y + 1, button->width - 2, button->height - 2, 3, gfx_vertical_gradient_pattern, &face);
  37. }
  38. if (button->title[0] != '\033') {
  39. int label_width = draw_sdf_string_width(button->title, 16, SDF_FONT_THIN);
  40. int centered = (button->width - label_width) / 2;
  41. int centered_y = (button->height - 16) / 2;
  42. draw_sdf_string(ctx, button->x + centered + (hilight == 2), button->y + centered_y + (hilight == 2), button->title, 16, disabled ? rgb(120,120,120) : rgb(0,0,0), SDF_FONT_THIN);
  43. } else {
  44. sprite_t * icon = icon_get_16(button->title+1);
  45. int centered = button->x + (button->width - icon->width) / 2 + (hilight == 2);
  46. int centered_y = button->y + (button->height - icon->height) / 2 + (hilight == 2);
  47. if (disabled) {
  48. draw_sprite_alpha(ctx, icon, centered, centered_y, 0.5);
  49. } else {
  50. draw_sprite(ctx, icon, centered, centered_y);
  51. }
  52. }
  53. }