2 * building bone information
4 * License : The MIT License
5 * Copyright(c) 2010 MikuToga Partners
8 package jp.sourceforge.mikutoga.pmd.pmdloader;
10 import java.util.Iterator;
11 import java.util.LinkedList;
12 import java.util.List;
13 import jp.sourceforge.mikutoga.corelib.ListUtil;
14 import jp.sourceforge.mikutoga.parser.ParseStage;
15 import jp.sourceforge.mikutoga.parser.pmd.PmdBoneHandler;
16 import jp.sourceforge.mikutoga.parser.pmd.PmdLimits;
17 import jp.sourceforge.mikutoga.pmd.BoneGroup;
18 import jp.sourceforge.mikutoga.pmd.BoneInfo;
19 import jp.sourceforge.mikutoga.pmd.BoneType;
20 import jp.sourceforge.mikutoga.pmd.IKChain;
21 import jp.sourceforge.mikutoga.pmd.PmdModel;
22 import jp.sourceforge.mikutoga.pmd.Pos3d;
27 class BoneBuilder implements PmdBoneHandler {
29 private final List<BoneInfo> boneList;
30 private Iterator<BoneInfo> boneIt;
31 private BoneInfo currentBone = null;
33 private final List<IKChain> ikChainList;
34 private Iterator<IKChain> ikChainIt;
35 private IKChain currentIkChain = null;
37 private final List<BoneGroup> boneGroupList;
38 private Iterator<BoneGroup> boneGroupIt;
39 private BoneGroup currentBoneGroup = null;
45 BoneBuilder(PmdModel model){
48 this.boneList = model.getBoneList();
49 this.ikChainList = model.getIKChainList();
50 this.boneGroupList = model.getBoneGroupList();
57 * @param stage {@inheritDoc}
58 * @param loops {@inheritDoc}
61 public void loopStart(ParseStage stage, int loops){
62 assert stage instanceof PmdBoneStage;
64 if(stage == PmdBoneHandler.BONE_LIST){
65 ListUtil.prepareDefConsList(this.boneList, BoneInfo.class, loops);
66 ListUtil.assignIndexedSerial(this.boneList);
68 this.boneIt = this.boneList.iterator();
69 if(this.boneIt.hasNext()){
70 this.currentBone = this.boneIt.next();
72 }else if(stage == PmdBoneHandler.IK_LIST){
73 ListUtil.prepareDefConsList(this.ikChainList,
77 this.ikChainIt = this.ikChainList.iterator();
78 if(this.ikChainIt.hasNext()){
79 this.currentIkChain = this.ikChainIt.next();
81 }else if(stage == PmdBoneHandler.IKCHAIN_LIST){
83 }else if(stage == PmdBoneHandler.BONEGROUP_LIST){
84 ListUtil.prepareDefConsList(this.boneGroupList,
87 ListUtil.assignIndexedSerial(this.boneGroupList);
89 this.boneGroupIt = this.boneGroupList.iterator();
91 assert this.boneGroupIt.hasNext();
92 this.boneGroupIt.next(); // デフォルトボーングループを読み飛ばす
94 if(this.boneGroupIt.hasNext()){
95 this.currentBoneGroup = this.boneGroupIt.next();
97 }else if(stage == PmdBoneHandler.GROUPEDBONE_LIST){
101 throw new AssertionError();
109 * @param stage {@inheritDoc}
112 public void loopNext(ParseStage stage){
113 assert stage instanceof PmdBoneStage;
115 if(stage == PmdBoneHandler.BONE_LIST){
116 if(this.boneIt.hasNext()){
117 this.currentBone = this.boneIt.next();
119 }else if(stage == PmdBoneHandler.IK_LIST){
120 if(this.ikChainIt.hasNext()){
121 this.currentIkChain = this.ikChainIt.next();
123 }else if(stage == PmdBoneHandler.IKCHAIN_LIST){
125 }else if(stage == PmdBoneHandler.BONEGROUP_LIST){
126 if(this.boneGroupIt.hasNext()){
127 this.currentBoneGroup = this.boneGroupIt.next();
129 }else if(stage == PmdBoneHandler.GROUPEDBONE_LIST){
133 throw new AssertionError();
140 * @param stage {@inheritDoc}
143 public void loopEnd(ParseStage stage){
144 assert stage instanceof PmdBoneStage;
146 if(stage == PmdBoneHandler.BONE_LIST){
148 }else if(stage == PmdBoneHandler.IK_LIST){
150 }else if(stage == PmdBoneHandler.IKCHAIN_LIST){
152 }else if(stage == PmdBoneHandler.BONEGROUP_LIST){
154 }else if(stage == PmdBoneHandler.GROUPEDBONE_LIST){
158 throw new AssertionError();
164 * 所属グループの無いボーンをデフォルトボーングループへ登録する。
166 private void pickOrphanBone(){
167 List<BoneInfo> orpahnList = new LinkedList<BoneInfo>();
168 orpahnList.addAll(this.boneList);
169 for(BoneGroup group : this.boneGroupList){
170 orpahnList.removeAll(group.getBoneList());
173 BoneGroup defaultGroup = this.boneGroupList.get(0);
174 defaultGroup.getBoneList().addAll(orpahnList);
181 * @param boneName {@inheritDoc}
182 * @param boneKind {@inheritDoc}
185 public void pmdBoneInfo(String boneName, byte boneKind){
186 this.currentBone.getBoneName().setPrimaryText(boneName);
187 BoneType type = BoneType.decode(boneKind);
188 this.currentBone.setBoneType(type);
194 * @param parentId {@inheritDoc}
195 * @param tailId {@inheritDoc}
196 * @param ikId {@inheritDoc}
199 public void pmdBoneLink(int parentId, int tailId, int ikId){
200 BoneInfo prevBone = null;
201 if(0 <= parentId && parentId < PmdLimits.MAX_BONE){
202 prevBone = this.boneList.get(parentId);
205 BoneInfo nextBone = null;
207 nextBone = this.boneList.get(tailId);
210 BoneInfo ikBone = null;
211 if(this.currentBone.getBoneType() == BoneType.LINKEDROT){
214 this.currentBone.setRotationRatio(ratio);
215 }else if(0 < ikId && ikId < PmdLimits.MAX_BONE){
216 ikBone = this.boneList.get(ikId);
219 this.currentBone.setPrevBone(prevBone);
220 this.currentBone.setNextBone(nextBone);
221 this.currentBone.setIKBone(ikBone);
228 * @param xPos {@inheritDoc}
229 * @param yPos {@inheritDoc}
230 * @param zPos {@inheritDoc}
233 public void pmdBonePosition(float xPos, float yPos, float zPos){
234 Pos3d position = this.currentBone.getPosition();
235 position.setXPos(xPos);
236 position.setYPos(yPos);
237 position.setZPos(zPos);
243 * @param boneId {@inheritDoc}
244 * @param targetId {@inheritDoc}
245 * @param depth {@inheritDoc}
246 * @param weight {@inheritDoc}
249 public void pmdIKInfo(int boneId, int targetId, int depth, float weight){
250 BoneInfo bone = this.boneList.get(boneId);
251 this.currentIkChain.setIkBone(bone);
253 BoneInfo target = this.boneList.get(targetId);
254 this.currentIkChain.getChainedBoneList().add(0, target);
256 this.currentIkChain.setIKDepth(depth);
257 this.currentIkChain.setIKWeight(weight);
264 * @param childId {@inheritDoc}
267 public void pmdIKChainInfo(int childId){
268 BoneInfo chain = this.boneList.get(childId);
269 this.currentIkChain.getChainedBoneList().add(chain);
275 * @param groupName {@inheritDoc}
278 public void pmdBoneGroupInfo(String groupName){
279 this.currentBoneGroup.getGroupName().setPrimaryText(groupName);
285 * @param boneId {@inheritDoc}
286 * @param groupId {@inheritDoc}
289 public void pmdGroupedBoneInfo(int boneId, int groupId){
290 BoneInfo bone = this.boneList.get(boneId);
291 BoneGroup group = this.boneGroupList.get(groupId);
292 group.getBoneList().add(bone);