Piano / src / view /

piano-impl-view.c

  1. /*
  2. * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License);
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. #include "config.h"
  18. #include "player/piano-impl.h"
  19. #include "logger.h"
  20. #include "piano-structs.h"
  21. #include "view/piano-callbacks.h"
  22. #include <efl_extension.h>
  23.  
  24. void piano_clear_win(void *data)
  25. {
  26. RETM_IF(!data, "data is NULL");
  27.  
  28. app_data *ad = (app_data *)data;
  29. if (ad->buttonList) {
  30. Eina_List *l = NULL;
  31. note_struct *note = NULL;
  32. EINA_LIST_FOREACH(ad->buttonList, l, note) {
  33. evas_object_del(note->button);
  34. note->button = NULL;
  35. free(note);
  36. }
  37.  
  38. eina_list_free(ad->buttonList);
  39. ad->buttonList = NULL;
  40. }
  41.  
  42. ecore_timer_del(ad->timer);
  43. ad->timer = NULL;
  44. }
  45.  
  46. static void _createlayout(void *data)
  47. {
  48. RETM_IF(!data, "data is NULL");
  49.  
  50. app_data *ad = (app_data *)data;
  51.  
  52. ad->win->layout = elm_layout_add(ad->win->win);
  53.  
  54. elm_layout_file_set(ad->win->layout, get_path(THEME_PATH), "piano.layout");
  55. evas_object_size_hint_weight_set(ad->win->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
  56. evas_object_size_hint_align_set(ad->win->layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
  57. elm_win_resize_object_add(ad->win->win, ad->win->layout);
  58. evas_object_show(ad->win->layout);
  59. }
  60.  
  61. static void _noteButton_fill(void *data, int index)
  62. {
  63. RETM_IF(!data, "data is NULL");
  64.  
  65. note_struct *note = (note_struct *)data;
  66.  
  67. if (note->type == PIANO_NOTE_BLACK) {
  68. snprintf(note->sound_name, MAXNAMLEN, "black_%d.wav", index + 1);
  69.  
  70. char part_name[MAXNAMLEN] = {'\0'};
  71. snprintf(part_name, MAXNAMLEN, "note_black_%d", index + 1);
  72. elm_object_part_content_set(note->ad->win->layout, part_name, note->button);
  73.  
  74. elm_object_theme_set(note->button, note->ad->win->theme);
  75. elm_object_style_set(note->button, "circle/button_black");
  76. }
  77.  
  78. if (note->type == PIANO_NOTE_WHITE) {
  79. snprintf(note->sound_name, MAXNAMLEN, "white_%d.wav", index + 1);
  80.  
  81. char part_name[MAXNAMLEN] = {'\0'};
  82. snprintf(part_name, MAXNAMLEN, "note_white_%d", index + 1);
  83. elm_object_part_content_set(note->ad->win->layout, part_name, note->button);
  84.  
  85. elm_object_theme_set(note->button, note->ad->win->theme);
  86. elm_object_style_set(note->button, "circle/button_white");
  87. }
  88.  
  89. evas_object_show(note->button);
  90. evas_object_event_callback_add(note->button, EVAS_CALLBACK_MULTI_DOWN, piano_note_touch_down_cb, note);
  91. evas_object_event_callback_add(note->button, EVAS_CALLBACK_MULTI_UP, piano_note_touch_up_cb, note);
  92.  
  93. evas_object_event_callback_add(note->button, EVAS_CALLBACK_MOUSE_DOWN, piano_note_mouse_down_cb, note);
  94. evas_object_event_callback_add(note->button, EVAS_CALLBACK_MOUSE_UP, piano_note_mouse_up_cb, note);
  95.  
  96. evas_object_event_callback_add(note->button, EVAS_CALLBACK_MOUSE_OUT, piano_note_mouse_out_cb, note);
  97.  
  98. evas_object_event_callback_add(note->button, EVAS_CALLBACK_MOUSE_MOVE, piano_note_mouse_move_cb, note);
  99.  
  100. piano_note_player_init(note);
  101.  
  102. piano_note_player_prepare(note);
  103.  
  104. evas_object_data_set(note->button, NOTE_BUTTON_DATA_KEY_NAME, note);
  105.  
  106. note->ad->buttonList = eina_list_append(note->ad->buttonList, note);
  107. }
  108.  
  109. static void _createBlackNotes(void *data)
  110. {
  111. RETM_IF(!data, "data is NULL");
  112.  
  113. app_data *ad = (app_data *)data;
  114.  
  115. note_struct *prev_note = NULL;
  116.  
  117. const int black_buttons_count = 6;
  118.  
  119. for (int i = 0; i < black_buttons_count; i++) {
  120. note_struct *note = (note_struct *) calloc(1, sizeof(note_struct));
  121. RETM_IF(!note, "note is NULL");
  122.  
  123. note->button = elm_button_add(ad->win->win);
  124. note->type = PIANO_NOTE_BLACK;
  125. note->ad = ad;
  126. note->prev = prev_note;
  127.  
  128. if (prev_note) {
  129. prev_note->next = note;
  130. }
  131.  
  132. _noteButton_fill(note, i);
  133.  
  134. prev_note = note;
  135. }
  136. }
  137.  
  138. static void _createWhiteNotes(void *data)
  139. {
  140. RETM_IF(!data, "data is NULL");
  141.  
  142. app_data *ad = (app_data *)data;
  143.  
  144. note_struct *prev_note = NULL;
  145.  
  146. const int white_buttons_count = 8;
  147.  
  148. for (int i = 0; i < white_buttons_count; i++) {
  149. note_struct *note = (note_struct *)calloc(1, sizeof(note_struct));
  150. RETM_IF(!note, "note is NULL");
  151.  
  152. note->button = elm_button_add(ad->win->win);
  153. note->type = PIANO_NOTE_WHITE;
  154. note->ad = ad;
  155. note->prev = prev_note;
  156.  
  157. if (prev_note) {
  158. prev_note->next = note;
  159. }
  160.  
  161. _noteButton_fill(note, i);
  162.  
  163. prev_note = note;
  164. }
  165. }
  166.  
  167. static void _noteButton_fill_rect(void *data)
  168. {
  169. RETM_IF(!data, "data is NULL");
  170.  
  171. app_data *ad = (app_data *)data;
  172.  
  173. Eina_List *l = NULL;
  174. note_struct *note = NULL;
  175. EINA_LIST_FOREACH(ad->buttonList, l, note) {
  176. evas_object_geometry_get(note->button, &note->rect.x, &note->rect.y, &note->rect.w, &note->rect.h);
  177. }
  178. }
  179.  
  180. static void _createnotes(void *data)
  181. {
  182. RETM_IF(!data, "data is NULL");
  183.  
  184. _createWhiteNotes(data);
  185. _createBlackNotes(data);
  186.  
  187. _noteButton_fill_rect(data);
  188. }
  189.  
  190. static void _applyTheme(void *data)
  191. {
  192. RETM_IF(!data, "data is NULL");
  193.  
  194. app_data *ad = (app_data *)data;
  195.  
  196. ad->win->theme = elm_theme_new();
  197. if (ad->win->theme) {
  198. elm_theme_ref_set(ad->win->theme, NULL);
  199. elm_theme_extension_add(ad->win->theme, get_path(BUTTONS_STYLE_EDJ_PATH));
  200. }
  201. }
  202.  
  203. static void _layout_back_cb(void *data, Evas_Object *obj, void *event_info)
  204. {
  205. RETM_IF(!data, "data is NULL");
  206.  
  207. app_data *application_data = (app_data *)data;
  208.  
  209. elm_win_lower(application_data->win->win);
  210. }
  211.  
  212. static void _registerKeyDown(void *data)
  213. {
  214. RETM_IF(!data, "data is NULL");
  215.  
  216. app_data *application_data = (app_data *)data;
  217.  
  218. eext_object_event_callback_add(application_data->win->layout, EEXT_CALLBACK_BACK, _layout_back_cb, application_data);
  219. }
  220.  
  221. static void _createNoteTimer(void *data)
  222. {
  223. RETM_IF(!data, "data is NULL");
  224.  
  225. app_data *ad = (app_data *)data;
  226.  
  227. const double delay = 0.3;
  228.  
  229. ad->timer = ecore_timer_add(delay, piano_note_timer_cb, data);
  230. }
  231.  
  232. void piano_createview(void *data)
  233. {
  234. RETM_IF(!data, "data is NULL");
  235.  
  236. _applyTheme(data);
  237.  
  238. _createlayout(data);
  239.  
  240. _createnotes(data);
  241.  
  242. _registerKeyDown(data);
  243.  
  244. _createNoteTimer(data);
  245. }