/+junk/c_sdl_joypad_ducktape

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad_ducktape
1 by Gustav Hartvigsson
Initial code.
1
#include "GameButton.h"
2
#include "game_utils.h"
3
7 by Gustav Hartvigsson
* Added licensing information to the files.
4
/**
5
 * This file, as the rest of the project is under MIT license.
6
 * see http://opensource.org/licenses/MIT
7
 *
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
8
 * Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
7 by Gustav Hartvigsson
* Added licensing information to the files.
9
 */
10
1 by Gustav Hartvigsson
Initial code.
11
GameButton * game_button_new (int x, int y, int h, int w) {
12
  GameButton * self = malloc (sizeof(GameButton));
13
  fprintf (stdout, "Creating GameButton: address: %ld\n", (long) self);
14
  SDL_Rect * base = (SDL_Rect *) self;
15
  base->x = x;
16
  base->y = y;
17
  base->h = h;
18
  base->w = w;
19
  self->clicked = NULL;
20
  self->is_clicked = false;
21
  self->fill = false;
22
  self->hover = NULL;
23
  self->is_hover = false;
24
  self->is_stop_hover = false;
25
  self->colour = game_color_create_color (0,0,255,255);
26
  return self;
27
}
28
29
30
bool game_button_envelopes_SDL_Point (GameButton *self, SDL_Point * point) {
31
  if (SDL_EnclosePoints (point, 1, (SDL_Rect *)self, NULL)) {
32
    return true;
33
  } else {
34
    return false;
35
  }
36
}
37
38
bool game_button_check_clicked (GameButton * self, MousePointer * pointer) {
39
  SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL);
40
  if (contains) {
41
     if (pointer->pressed) {
42
       game_button_do_callback (self, "clicked");
43
       return true;
44
     }
45
     
46
  }
47
  self->is_clicked = false;
48
  return false;
49
}
50
51
bool game_button_check_hover (GameButton * self, MousePointer * pointer) {
52
  SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL);
53
  if (contains) {
54
    if (!self->is_clicked ) {
55
      game_button_do_callback (self, "hover");
56
    }
57
    self->is_hover = true;
58
    self->is_stop_hover = false;
59
    return true;
60
  } else if (self->is_hover) {
61
    game_button_do_callback (self, "stop_hover");
62
    self->is_stop_hover = true;
63
  }
64
  self->is_hover = false;
65
  return false;
66
}
67
68
void game_button_set_callback (GameButton * self,
69
                               const char * name,
70
                               BtnCallback callback,
71
                               void * data) {
72
  if (!strcmp(name, "clicked")) {
73
    self->clicked = callback;
74
    self->clicked_data = data;
75
  } else if (!strcmp(name, "hover")) {
76
    self->hover = callback;
77
    self->hover_data = data;
78
  } else if (!strcmp(name, "stop_hover") && self->hover) {
79
    self->stop_hover = callback;
80
    self->stop_hover_data = data;
81
  } else {
82
    fprintf (stderr,
83
             "GameButton: Can not set callback \"%s\", it does not exist!\n",
84
             name);
85
    return;
86
  }
87
  return;
88
}
89
90
91
void game_button_do_callback (GameButton * self,
92
                              const char * name) {
93
  BtnCallback callback;
94
  if (!strcmp(name, "clicked") && self->clicked) {
95
    if (self->is_clicked) {
96
     return;
97
    }
98
    callback = self->clicked;
99
    callback (self, self->clicked_data);
100
  } else if (!strcmp(name, "hover") && self->hover) {
101
    if (self->is_hover) {
102
      return;
103
    }
104
    callback = self->hover;
105
    callback (self, self->hover_data);
106
  } else if (!strcmp(name, "stop_hover") && self->stop_hover) {
107
    if (self->is_stop_hover) {
108
      return;
109
    }
110
    callback = self->stop_hover;
111
    callback (self, self->stop_hover_data);
112
  } else {
113
        fprintf (stderr,
114
             "GameButton: Can not do callback \"%s\", it does not exist, or is undefined!\n",
115
             name);
116
    return;
117
  }
118
  self->is_clicked = true;
119
  return;
120
}
121
122
void game_button_draw (GameButton * self, SDL_Renderer * renderer) {
123
  SDL_Color oc = game_color_set_draw_color (renderer, self->colour);
124
  if (self->fill) {
125
    SDL_RenderFillRect (renderer , (SDL_Rect *) self);
126
  } else {
127
    SDL_RenderDrawRect (renderer , (SDL_Rect *) self);
128
  }
129
  game_color_set_draw_color  (renderer, oc); /* reset the colour */
130
}
131
132
void game_button_set_fill (GameButton * self, bool fill) {
133
  self->fill = fill;
134
}
135
136
void game_button_free (GameButton * self) {
137
  fprintf (stdout, "Freeing GameButton: address: %ld\n", (long) self);
138
  free (self);
139
}