Commit: f19fcf7aadc278d74198a6f86cadadb0313b2bfe
Author: Lukas Tönne
Date:   Wed Oct 22 18:12:42 2014 +0200
Branches: hair_immediate_fixes
https://developer.blender.org/rBf19fcf7aadc278d74198a6f86cadadb0313b2bfe

Merge branch 'master' into hair_immediate_fixes

Conflicts:
        source/blender/blenkernel/intern/key.c

===================================================================



===================================================================

diff --cc source/blender/blenkernel/BKE_key.h
index 4b3d582,892c42b..142245b
--- a/source/blender/blenkernel/BKE_key.h
+++ b/source/blender/blenkernel/BKE_key.h
@@@ -107,9 -96,11 +107,13 @@@ void    BKE_key_convert_from_curve(stru
  float (*BKE_key_convert_to_vertcos(struct Object *ob, struct KeyBlock 
*kb))[3];
  void    BKE_key_convert_from_vertcos(struct Object *ob, struct KeyBlock *kb, 
float (*vertCos)[3]);
  void    BKE_key_convert_from_offset(struct Object *ob, struct KeyBlock *kb, 
float (*ofs)[3]);
 +void    BKE_key_convert_to_hair_keys(struct KeyBlock *kb, struct Object *ob, 
struct ParticleSystem *psys);
 +void    BKE_key_convert_from_hair_keys(struct Object *ob, struct 
ParticleSystem *psys, struct KeyBlock *kb);
  
+ /* other management */
+ bool    BKE_keyblock_move(struct Object *ob, int org_index, int new_index);
+ 
+ 
  /* key.c */
  extern int slurph_opt;
  
diff --cc source/blender/blenkernel/intern/key.c
index 62310c3,69b375f..ee27108
--- a/source/blender/blenkernel/intern/key.c
+++ b/source/blender/blenkernel/intern/key.c
@@@ -2256,44 -2056,90 +2256,132 @@@ void BKE_key_convert_from_offset(Objec
        }
  }
  
 +/************************* Mesh ************************/
 +
 +void BKE_key_convert_to_hair_keys(struct KeyBlock *kb, struct Object 
*UNUSED(ob), struct ParticleSystem *psys)
 +{
 +      ParticleData *pa;
 +      HairKey *hkey;
 +      float *fp;
 +      int i, k;
 +      
 +      fp = kb->data;
 +      for (i = 0, pa = psys->particles; i < psys->totpart; ++i, ++pa) {
 +              for (k = 0, hkey = pa->hair; k < pa->totkey; ++k, ++hkey) {
 +                      copy_v3_v3(hkey->co, fp);
 +                      fp += 3;
 +              }
 +      }
 +}
 +
 +void BKE_key_convert_from_hair_keys(struct Object *UNUSED(ob), struct 
ParticleSystem *psys, struct KeyBlock *kb)
 +{
 +      ParticleData *pa;
 +      HairKey *hkey;
 +      float *fp;
 +      int i, k;
 +
 +      if (kb->data) MEM_freeN(kb->data);
 +
 +      kb->totelem = 0;
 +      for (i = 0, pa = psys->particles; i < psys->totpart; ++i, ++pa) {
 +              kb->totelem += pa->totkey;
 +      }
 +      kb->data = MEM_mallocN(psys->key->elemsize * kb->totelem, "kb->data");
 +
 +      fp = kb->data;
 +      for (i = 0, pa = psys->particles; i < psys->totpart; ++i, ++pa) {
 +              for (k = 0, hkey = pa->hair; k < pa->totkey; ++k, ++hkey) {
 +                      copy_v3_v3(fp, hkey->co);
 +                      fp += 3;
 +              }
 +      }
 +}
++
+ /* ==========================================================*/
+ 
+ /** Move shape key from org_index to new_index. Safe, clamps index to valid 
range, updates reference keys,
+  * the object's active shape index, the 'frame' value in case of absolute 
keys, etc.
+  * Note indices are expected in real values (not 'fake' shapenr +1 ones).
+  *
+  * \param org_index if < 0, current object's active shape will be used as 
skey to move.
+  * \return true if something was done, else false.
+  */
+ bool BKE_keyblock_move(Object *ob, int org_index, int new_index)
+ {
+       Key *key = BKE_key_from_object(ob);
+       KeyBlock *kb;
+       const int act_index = ob->shapenr - 1;
+       const int totkey = key->totkey;
+       int i;
+       bool rev, in_range = false;
+ 
+       if (org_index < 0) {
+               org_index = act_index;
+       }
+ 
+       CLAMP(new_index, 0, key->totkey - 1);
+       CLAMP(org_index, 0, key->totkey - 1);
+ 
+       if (new_index == org_index) {
+               return false;
+       }
+ 
+       rev = ((new_index - org_index) < 0) ? true : false;
+ 
+       /* We swap 'org' element with its previous/next neighbor (depending on 
direction of the move) repeatedly,
+        * until we reach final position.
+        * This allows us to only loop on the list once! */
+       for (kb = (rev ? key->block.last : key->block.first), i = (rev ? totkey 
- 1 : 0);
+            kb;
+            kb = (rev ? kb->prev : kb->next), rev ? i-- : i++)
+       {
+               if (i == org_index) {
+                       in_range = true;  /* Start list items swapping... */
+               }
+               else if (i == new_index) {
+                       in_range = false;  /* End list items swapping. */
+               }
+ 
+               if (in_range) {
+                       KeyBlock *other_kb = rev ? kb->prev : kb->next;
+ 
+                       /* Swap with previous/next list item. */
+                       BLI_listbase_swaplinks(&key->block, kb, other_kb);
+ 
+                       /* Swap absolute positions. */
+                       SWAP(float, kb->pos, other_kb->pos);
+ 
+                       kb = other_kb;
+               }
+ 
+               /* Adjust relative indices, this has to be done on the whole 
list! */
+               if (kb->relative == org_index) {
+                       kb->relative = new_index;
+               }
+               else if (kb->relative < org_index && kb->relative >= new_index) 
{
+                       /* remove after, insert before this index */
+                       kb->relative++;
+               }
+               else if (kb->relative > org_index && kb->relative <= new_index) 
{
+                       /* remove before, insert after this index */
+                       kb->relative--;
+               }
+       }
+ 
+       /* Need to update active shape number if it's affected, same principle 
as for relative indices above. */
+       if (org_index == act_index) {
+               ob->shapenr = new_index + 1;
+       }
+       else if (act_index < org_index && act_index >= new_index) {
+               ob->shapenr++;
+       }
+       else if (act_index > org_index && act_index <= new_index) {
+               ob->shapenr--;
+       }
+ 
+       /* First key is always refkey, matches interface and BKE_key_sort */
+       key->refkey = key->block.first;
+ 
+       return true;
+ }

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to