/src/ftk_animation_expand.c

http://ftk.googlecode.com/ · C · 315 lines · 238 code · 48 blank · 29 comment · 47 complexity · 1682153831e48d0e60631c3348f4d2f1 MD5 · raw file

  1. /*
  2. * File: ftk_animation_expand.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: expand animation
  5. *
  6. * Copyright (c) 2009 - 2011 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2011-03-26 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_log.h"
  31. #include "ftk_globals.h"
  32. #include "ftk_animation_expand.h"
  33. typedef struct _AnimationExpandPrivInfo
  34. {
  35. int push;
  36. float end;
  37. float start;
  38. int duration;
  39. int alpha_enable;
  40. const char* base;
  41. FtkBitmap* back_win;
  42. FtkBitmap* front_win;
  43. FtkRect back_win_rect;
  44. FtkRect front_win_rect;
  45. }PrivInfo;
  46. static Ret ftk_animation_expand_step_base_left(FtkAnimation* thiz, float percent)
  47. {
  48. FtkRect r = {0};
  49. FtkRect dst_r = {0};
  50. DECL_PRIV(thiz, priv);
  51. r = priv->back_win_rect;
  52. if(r.width > 0 && r.height > 0)
  53. {
  54. dst_r = r;
  55. if(priv->push)
  56. {
  57. int offset = (int)(priv->front_win_rect.width * percent);
  58. dst_r.x = dst_r.x + offset;
  59. dst_r.width = r.width = r.width - offset;
  60. }
  61. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->back_win, &r, &dst_r, 0xff);
  62. }
  63. r = priv->front_win_rect;
  64. r.width = (int)(r.width * percent);
  65. if(r.width > 0 && r.height > 0)
  66. {
  67. int alpha = priv->alpha_enable ? (int)(0xff * percent) & 0xff : 0xff;
  68. dst_r = r;
  69. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->front_win, &r, &dst_r, alpha);
  70. }
  71. return RET_OK;
  72. }
  73. static Ret ftk_animation_expand_step_base_top(FtkAnimation* thiz, float percent)
  74. {
  75. FtkRect r = {0};
  76. FtkRect dst_r = {0};
  77. DECL_PRIV(thiz, priv);
  78. r = priv->back_win_rect;
  79. if(r.width > 0 && r.height > 0)
  80. {
  81. dst_r = r;
  82. if(priv->push)
  83. {
  84. int offset = (int)(priv->front_win_rect.height * percent);
  85. dst_r.y = dst_r.y + offset;
  86. dst_r.height = r.height = r.height - offset;
  87. }
  88. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->back_win, &r, &dst_r, 0xff);
  89. }
  90. r = priv->front_win_rect;
  91. r.height = (int)(r.height * percent);
  92. if(r.width > 0 && r.height > 0)
  93. {
  94. int alpha = priv->alpha_enable ? (int)(0xff * percent) & 0xff : 0xff;
  95. dst_r = r;
  96. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->front_win, &r, &dst_r, alpha);
  97. }
  98. return RET_OK;
  99. }
  100. static Ret ftk_animation_expand_step_base_right(FtkAnimation* thiz, float percent)
  101. {
  102. FtkRect r = {0};
  103. FtkRect dst_r = {0};
  104. DECL_PRIV(thiz, priv);
  105. r = priv->back_win_rect;
  106. if(r.width > 0 && r.height > 0)
  107. {
  108. dst_r = r;
  109. if(priv->push)
  110. {
  111. int offset = (int)(priv->front_win_rect.width * percent);
  112. r.x = r.x + offset;
  113. dst_r.width = r.width = r.width - offset;
  114. }
  115. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->back_win, &r, &dst_r, 0xff);
  116. }
  117. r = priv->front_win_rect;
  118. r.width = (int)(r.width * percent);
  119. r.x = priv->front_win_rect.width - r.width;
  120. if(r.width > 0 && r.height > 0)
  121. {
  122. int alpha = priv->alpha_enable ? (int)(0xff * percent) & 0xff : 0xff;
  123. dst_r = r;
  124. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->front_win, &r, &dst_r, alpha);
  125. }
  126. return RET_OK;
  127. }
  128. static Ret ftk_animation_expand_step_base_bottom(FtkAnimation* thiz, float percent)
  129. {
  130. FtkRect r = {0};
  131. FtkRect dst_r = {0};
  132. DECL_PRIV(thiz, priv);
  133. r = priv->back_win_rect;
  134. if(r.width > 0 && r.height > 0)
  135. {
  136. dst_r = r;
  137. if(priv->push)
  138. {
  139. int offset = (int)(priv->front_win_rect.height * percent);
  140. r.y = r.y + offset;
  141. dst_r.height = r.height = r.height - offset;
  142. }
  143. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->back_win, &r, &dst_r, 0xff);
  144. }
  145. r = priv->front_win_rect;
  146. r.height = (int)(r.height * percent);
  147. r.y = r.y + priv->front_win_rect.height - r.height;
  148. if(r.width > 0 && r.height > 0)
  149. {
  150. int alpha = priv->alpha_enable ? (int)(0xff * percent) & 0xff : 0xff;
  151. dst_r = r;
  152. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->front_win, &r, &dst_r, alpha);
  153. }
  154. return RET_OK;
  155. }
  156. static Ret ftk_animation_expand_step_base_center(FtkAnimation* thiz, float percent)
  157. {
  158. FtkRect r = {0};
  159. FtkRect dst_r = {0};
  160. DECL_PRIV(thiz, priv);
  161. r = priv->back_win_rect;
  162. if(r.width > 0 && r.height > 0)
  163. {
  164. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->back_win, &r, &r, 0xff);
  165. }
  166. r = priv->front_win_rect;
  167. r.width = (int)(r.width * percent);
  168. r.height = (int)(r.height * percent);
  169. if(r.width > 0 && r.height > 0)
  170. {
  171. int alpha = priv->alpha_enable ? (int)(0xff * percent) & 0xff : 0xff;
  172. dst_r = r;
  173. dst_r.x = r.x + (priv->front_win_rect.width - r.width)/2;
  174. dst_r.y = r.y + (priv->front_win_rect.height - r.height)/2;
  175. r.x = dst_r.x;
  176. r.y = dst_r.y;
  177. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->front_win, &r, &dst_r, alpha);
  178. }
  179. return RET_OK;
  180. }
  181. static Ret ftk_animation_expand_step(FtkAnimation* thiz)
  182. {
  183. FtkRect rf = {0};
  184. FtkRect rs = {0};
  185. float percent = 0;
  186. DECL_PRIV(thiz, priv);
  187. const char* base = ftk_animation_get_param(thiz, "base");
  188. return_val_if_fail(base != NULL, RET_FAIL);
  189. percent = priv->start + (priv->end - priv->start) * ftk_animation_get_percent(thiz);
  190. if(strcmp(base, "left") == 0)
  191. {
  192. ftk_animation_expand_step_base_left(thiz, percent);
  193. }
  194. else if(strcmp(base, "top") == 0)
  195. {
  196. ftk_animation_expand_step_base_top(thiz, percent);
  197. }
  198. else if(strcmp(base, "right") == 0)
  199. {
  200. ftk_animation_expand_step_base_right(thiz, percent);
  201. }
  202. else if(strcmp(base, "bottom") == 0)
  203. {
  204. ftk_animation_expand_step_base_bottom(thiz, percent);
  205. }
  206. else
  207. {
  208. ftk_animation_expand_step_base_center(thiz, percent);
  209. }
  210. rf = priv->back_win_rect;
  211. rs = priv->front_win_rect;
  212. rf.x = FTK_MIN(rf.x, rs.x);
  213. rf.y = FTK_MIN(rf.y, rs.y);
  214. rf.width = FTK_MAX(rf.width, rs.width);
  215. rf.height = FTK_MAX(rf.height, rs.height);
  216. ftk_canvas_show(ftk_shared_canvas(), ftk_default_display(), &rf, rf.x, rf.y);
  217. return RET_OK;
  218. }
  219. static Ret ftk_animation_expand_reset(FtkAnimation* thiz, FtkBitmap* old_win, FtkBitmap* new_win,
  220. FtkRect* old_win_rect, FtkRect* new_win_rect)
  221. {
  222. DECL_PRIV(thiz, priv);
  223. const char* src = ftk_animation_get_param(thiz, "src");
  224. const char* push = ftk_animation_get_param(thiz, "push");
  225. const char* alpha_enable = ftk_animation_get_param(thiz, "alpha");
  226. return_val_if_fail(src != NULL, RET_FAIL);
  227. priv->start = ftk_animation_get_param_float(thiz, "from", 1.0);
  228. priv->end = ftk_animation_get_param_float(thiz, "to", 1.0);
  229. priv->push = push != NULL && strcmp(push, "true") == 0;
  230. priv->alpha_enable = alpha_enable != NULL && strcmp(alpha_enable, "enable") == 0;
  231. if(strstr(src, "new_window") != NULL)
  232. {
  233. priv->back_win = old_win;
  234. priv->back_win_rect = *old_win_rect;
  235. priv->front_win = new_win;
  236. priv->front_win_rect = *new_win_rect;
  237. }
  238. else
  239. {
  240. priv->back_win = new_win;
  241. priv->back_win_rect = *new_win_rect;
  242. priv->front_win = old_win;
  243. priv->front_win_rect = *old_win_rect;
  244. }
  245. ftk_logd("%s: src=%s start=%f end=%f old(%d %d %d %d) new(%d %d %d %d)",
  246. __func__, src, priv->start, priv->end,
  247. old_win_rect->x, old_win_rect->y, old_win_rect->width, old_win_rect->height,
  248. new_win_rect->x, new_win_rect->y, new_win_rect->width, new_win_rect->height);
  249. return RET_OK;
  250. }
  251. static void ftk_animation_expand_destroy(FtkAnimation* thiz)
  252. {
  253. FTK_FREE(thiz);
  254. return;
  255. }
  256. FtkAnimation* ftk_animation_expand_create(void)
  257. {
  258. FtkAnimation* thiz = FTK_NEW_PRIV(FtkAnimation);
  259. if(thiz != NULL)
  260. {
  261. ftk_animation_set_name(thiz, "expand");
  262. thiz->step = ftk_animation_expand_step;
  263. thiz->reset = ftk_animation_expand_reset;
  264. thiz->destroy = ftk_animation_expand_destroy;
  265. }
  266. return thiz;
  267. }