001: #include "Button.h"
002: #include "SpritesList.h"
003: #include "Sdl.h"
004:
005: bool InitButtonFail (BUTTON* button)
006: {
007: ButtonClose (button);
008: return false;
009: }
010:
011: bool ButtonInit (BUTTON* button,
012: SDL_Renderer* renderer,
013: char* SprEnabled,
014: char* SprDisabled,
015: char* SprClicked,
016: int x,
017: int y,
018: bool enabled,
019: button_MouseOver over,
020: button_MouseOut out,
021: button_LeftClick leftclick)
022: {
023: bool ret = false;
024: // Init pointers in case of failure
025: button->SprClicked = NULL;
026: button->SprEnabled = NULL;
027: button->SprDisabled = NULL;
028:
029: // Load sprites
030: button->SprEnabled = LoadSprite (SprEnabled, renderer);
031: if (button->SprEnabled == NULL)
032: {
033: ButtonClose (button);
034: goto exit;
035: }
036:
037: if (SprDisabled != NULL) {
038: button->SprDisabled = LoadSprite (SprDisabled, renderer);
039: if (button->SprDisabled == NULL)
040: {
041: ButtonClose (button);
042: goto exit;
043: }
044: }
045:
046: if (SprClicked != NULL) {
047: button->SprClicked = LoadSprite (SprClicked, renderer);
048: if (button->SprClicked == NULL)
049: {
050: ButtonClose (button);
051: goto exit;
052: }
053: }
054:
055: // Callbacks
056: button->over = over;
057: button->out = out;
058: button->leftclick = leftclick;
059:
060: // Status
061: button->status = STATUS_MOUSE_OUT;
062: button->enabled = enabled;
063:
064: // Location and size
065: button->dest.x = x;
066: button->dest.y = y;
067: SDL_QueryTexture (button->SprEnabled, NULL, NULL, &button->dest.w, &button->dest.h);
068:
069: // Default sprite
070: button->sprite = enabled ? button->SprEnabled : button->SprDisabled;
071: ret = true;
072:
073: exit:
074: return true;
075: }
076:
077: void ButtonClose (BUTTON* button)
078: {
079: if (button->SprEnabled != NULL)
080: UnloadSprite (button->SprEnabled);
081: if (button->SprDisabled != NULL)
082: UnloadSprite (button->SprDisabled);
083: if (button->SprClicked != NULL)
084: UnloadSprite (button->SprClicked);
085: }
086:
087: bool ButtonUpdate (BUTTON* button, SDL_Event* event, int mouse_x, int mouse_y)
088: {
089: bool ret = true;
090: if (button->enabled) {
091: if ((mouse_x >= button->dest.x) && (mouse_x < button->dest.x + button->dest.w) && (mouse_y >= button->dest.y) &&
092: (mouse_y < button->dest.y + button->dest.h)) {
093: // The mouse is over the button
094: switch (button->status) {
095: case STATUS_MOUSE_OUT:
096: button->status = STATUS_MOUSE_OVER;
097: if (button->over != NULL)
098: ret = button->over ();
099: break;
100:
101: case STATUS_MOUSE_OVER:
102: if ((event->type == SDL_MOUSEBUTTONDOWN) && (event->button.button == SDL_BUTTON_LEFT)) {
103: button->status = STATUS_CLICK_LEFT_DOWN;
104: button->sprite = button->SprClicked == NULL ? button->SprEnabled : button->SprClicked;
105: break;
106: }
107: case STATUS_CLICK_LEFT_DOWN:
108: if ((event->type == SDL_MOUSEBUTTONUP) && (event->button.button == SDL_BUTTON_LEFT)) {
109: button->status = STATUS_CLICK_LEFT_UP;
110: ret = button->leftclick ();
111: button->status = STATUS_MOUSE_OVER;
112: button->sprite = button->SprEnabled;
113: break;
114: }
115: }
116: } else {
117: // The mouse is out of the button
118: if (button->status != STATUS_MOUSE_OUT) {
119: // But it was over before
120: button->status = STATUS_MOUSE_OUT;
121: if (button->out != NULL)
122: ret = button->out ();
123: }
124: }
125: } else { // Button is disabled
126: button->status = STATUS_MOUSE_OUT;
127: button->sprite = button->SprDisabled == NULL ? button->SprEnabled : button->SprDisabled;
128: }
129: return ret;
130: }
131:
132: bool ButtonDraw (BUTTON* button, SDL_Renderer* renderer)
133: {
134: return SDL_RenderCopy (renderer, button->sprite, NULL, &button->dest) == 0 ? true : false;
135: }