Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

115 řádky
3.8 KiB

  1. #include <stdbool.h>
  2. #include "clickable.h"
  3. bool Clickable_CheckBounds(Clickable *c, int x, int y) {
  4. if(c->dimensions.x < x
  5. &&c->dimensions.x+c->dimensions.w > x
  6. &&c->dimensions.y < y
  7. &&c->dimensions.y+c->dimensions.h > y
  8. ) {
  9. return true;
  10. }
  11. else {
  12. return false;
  13. }
  14. }
  15. Clickable *heldClickable;
  16. //Calls the clickables clickFunc iff the mouse is inside the clickable.
  17. //Returns true if it is clicked regardless of whether the clickFunc is NULL or not, and false otherwise.
  18. bool Clicky_ClickClickable(void *c_, int x, int y) {
  19. //printf("CALL: Clicky_ClickClickable\n Params: %p, %d, %d\n", c_, x, y);
  20. Clickable *c = c_;
  21. if(Clickable_CheckBounds(c, x, y)) {
  22. heldClickable = c;
  23. if(c->clickFunc != NULL) { //Not necessarily true.
  24. c->clickFunc((void*)c, x, y);
  25. }
  26. return true; //It doesn't matter if the clickable does anything, if it is clicked it reports it.
  27. }
  28. return false;
  29. }
  30. //Calls the clickables releaseFunc iff the mouse is inside the clickable.
  31. void Clicky_ReleaseClickable(void *c_, int x, int y) {
  32. Clickable *c = c_;
  33. if(Clickable_CheckBounds(c, x, y)) {
  34. heldClickable = c;
  35. if(c->releaseFunc != NULL) { //Not necessarily true.
  36. c->releaseFunc((void*)c, x, y);
  37. }
  38. }
  39. }
  40. //Calls the releasefunc of the held clickable, if it isn't NULL.
  41. void Clicky_ReleaseHeldClickable(int x, int y) {
  42. //printf("Releasing held clickable.\n");
  43. //printf(" heldClickable: %p\n", heldClickable);
  44. //printf(" heldClickable->x: %d\n", heldClickable->dimensions.x);
  45. //printf(" heldClickable->releaseFunc: %p\n", heldClickable->releaseFunc);
  46. if(heldClickable == NULL) {
  47. return; //Nothing to do.
  48. }
  49. //Only call the function if it exists.
  50. if(heldClickable->releaseFunc != NULL) {
  51. heldClickable->releaseFunc(heldClickable, x, y);
  52. }
  53. //Always clear this.
  54. heldClickable = NULL;
  55. }
  56. Dragable *currentDragged = NULL; //This gets dragged on.
  57. SDL_Rect dragOffset; //When a dragable gets dragged, this is the local offset into the container to move.
  58. void Clicky_DragUpdate(int x, int y) {
  59. if(currentDragged == NULL) {
  60. return; //Nothing to do here.
  61. }
  62. Dragable *d = currentDragged;
  63. d->coordinatesToDrag->x = x-dragOffset.x;
  64. d->coordinatesToDrag->y = y-dragOffset.y;
  65. //Check boundaries.
  66. if(d->boundaries != NULL) {
  67. //Left boundary.
  68. if(d->coordinatesToDrag->x < d->boundaries->x) {
  69. d->coordinatesToDrag->x = d->boundaries->x;
  70. }
  71. //Upper boundary.
  72. if(d->coordinatesToDrag->y < d->boundaries->y) {
  73. d->coordinatesToDrag->y = d->boundaries->y;
  74. }
  75. //Right boundary.
  76. if(d->coordinatesToDrag->x > d->boundaries->x + d->boundaries->w - d->coordinatesToDrag->w) {
  77. d->coordinatesToDrag->x = d->boundaries->x + d->boundaries->w - d->coordinatesToDrag->w;
  78. }
  79. //Lower boundary.
  80. if(d->coordinatesToDrag->y > d->boundaries->y + d->boundaries->h - d->coordinatesToDrag->h) {
  81. d->coordinatesToDrag->y = d->boundaries->y + d->boundaries->h - d->coordinatesToDrag->h;
  82. }
  83. //printf("New coords: %d, %d, %d, %d\n", d->coordinatesToDrag->x, d->coordinatesToDrag->y, d->coordinatesToDrag->x+d->coordinatesToDrag->w, d->coordinatesToDrag->y+d->coordinatesToDrag->h);
  84. }
  85. }
  86. //Sets the global variables for the dragging mechanism. They are used in the function Dragable_DragUpdate.
  87. void Dragable_ClickFunc(void *dragable, int x, int y) {
  88. //printf("Dragable clicked.\n");
  89. currentDragged = dragable;
  90. dragOffset.x = x - currentDragged->coordinatesToDrag->x;
  91. dragOffset.y = y - currentDragged->coordinatesToDrag->y;
  92. }
  93. //This function clears the global variables so nothing will be dragged.
  94. //The args are ignored.
  95. void Dragable_ReleaseFunc(void *dragable, int x, int y) {
  96. //printf("Dragable released.\n");
  97. currentDragged = NULL;
  98. dragOffset.x = 0;
  99. dragOffset.y = 0;
  100. }