/src/ftk_animation_scale.c

http://ftk.googlecode.com/ · C · 147 lines · 93 code · 25 blank · 29 comment · 12 complexity · db256983ecd11e22b83f5760d1c0afea MD5 · raw file

  1. /*
  2. * File: ftk_animation_scale.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: scale 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_scale.h"
  33. typedef struct _AnimationScalePrivInfo
  34. {
  35. float end;
  36. float start;
  37. int duration;
  38. int alpha_enable;
  39. FtkBitmap* back_win;
  40. FtkBitmap* front_win;
  41. FtkRect back_win_rect;
  42. FtkRect front_win_rect;
  43. }PrivInfo;
  44. static Ret ftk_animation_scale_step(FtkAnimation* thiz)
  45. {
  46. FtkRect r = {0};
  47. FtkRect dst_r = {0};
  48. float percent = 0;
  49. DECL_PRIV(thiz, priv);
  50. r = priv->back_win_rect;
  51. if(r.width > 0 && r.height > 0)
  52. {
  53. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->back_win, &r, &r, 0xff);
  54. ftk_logd("%s:%d %d %d %d %d\n", __func__, __LINE__, r.x, r.y, r.width, r.height);
  55. }
  56. percent = priv->start + (priv->end - priv->start) * ftk_animation_get_percent(thiz);
  57. r = priv->front_win_rect;
  58. dst_r.width = (int)(r.width * percent);
  59. dst_r.height = (int)(r.height * percent);
  60. if(dst_r.width > 0 && dst_r.height > 0)
  61. {
  62. int alpha = priv->alpha_enable ? (int)(0xff * percent) & 0xff : 0xff;
  63. dst_r.x = r.x + (r.width - dst_r.width)/2;
  64. dst_r.y = r.y + (r.height - dst_r.height)/2;
  65. ftk_canvas_draw_bitmap(ftk_shared_canvas(), priv->front_win, &r, &dst_r, alpha);
  66. }
  67. r = priv->back_win_rect;
  68. r.x = FTK_MIN(r.x, dst_r.y);
  69. r.y = FTK_MIN(r.y, dst_r.y);
  70. r.width = FTK_MAX(r.width, dst_r.width);
  71. r.height = FTK_MAX(r.height, dst_r.height);
  72. ftk_logd("%s:%d %d %d %d %d\n", __func__, __LINE__, r.x, r.y, r.width, r.height);
  73. ftk_canvas_show(ftk_shared_canvas(), ftk_default_display(), &r, r.x, r.y);
  74. return RET_OK;
  75. }
  76. static Ret ftk_animation_scale_reset(FtkAnimation* thiz, FtkBitmap* old_win, FtkBitmap* new_win,
  77. FtkRect* old_win_rect, FtkRect* new_win_rect)
  78. {
  79. DECL_PRIV(thiz, priv);
  80. const char* src = ftk_animation_get_param(thiz, "src");
  81. const char* alpha_enable = ftk_animation_get_param(thiz, "alpha");
  82. return_val_if_fail(src != NULL, RET_FAIL);
  83. priv->alpha_enable = alpha_enable != NULL && strcmp(alpha_enable, "enable") == 0;
  84. priv->start = ftk_animation_get_param_float(thiz, "from", 1.0);
  85. priv->end = ftk_animation_get_param_float(thiz, "to", 1.0);
  86. if(strstr(src, "new_window") != NULL)
  87. {
  88. priv->back_win = old_win;
  89. priv->back_win_rect = *old_win_rect;
  90. priv->front_win = new_win;
  91. priv->front_win_rect = *new_win_rect;
  92. }
  93. else
  94. {
  95. priv->back_win = new_win;
  96. priv->back_win_rect = *new_win_rect;
  97. priv->front_win = old_win;
  98. priv->front_win_rect = *old_win_rect;
  99. }
  100. ftk_logd("%s: src=%s start=%f end=%f old(%d %d %d %d) new(%d %d %d %d)",
  101. __func__, src, priv->start, priv->end,
  102. old_win_rect->x, old_win_rect->y, old_win_rect->width, old_win_rect->height,
  103. new_win_rect->x, new_win_rect->y, new_win_rect->width, new_win_rect->height);
  104. return RET_OK;
  105. }
  106. static void ftk_animation_scale_destroy(FtkAnimation* thiz)
  107. {
  108. FTK_FREE(thiz);
  109. return;
  110. }
  111. FtkAnimation* ftk_animation_scale_create(void)
  112. {
  113. FtkAnimation* thiz = FTK_NEW_PRIV(FtkAnimation);
  114. if(thiz != NULL)
  115. {
  116. ftk_animation_set_name(thiz, "scale");
  117. thiz->step = ftk_animation_scale_step;
  118. thiz->reset = ftk_animation_scale_reset;
  119. thiz->destroy = ftk_animation_scale_destroy;
  120. }
  121. return thiz;
  122. }