//
// Generated by dtk
// Translation Unit: J3DMaterialAttach.cpp
//

#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/J3DGraphAnimator/J3DMaterialAttach.h"
#include "JSystem/JKernel/JKRHeap.h"
#include "JSystem/J3DGraphAnimator/J3DMaterialAnm.h"
#include "JSystem/J3DGraphBase/J3DSys.h"

/* 802F5C48-802F5C70       .text clear__16J3DMaterialTableFv */
void J3DMaterialTable::clear() {
    mMaterialNum = 0;
    mMaterialNodePointer = NULL;
    mMaterialName = NULL;
    mUniqueMatNum = 0;
    mMaterialBase = NULL;
    mTexture = NULL;
    mTextureName = NULL;
    mbIsLocked = 0;
}

/* 802F5C70-802F5CAC       .text __ct__16J3DMaterialTableFv */
J3DMaterialTable::J3DMaterialTable() {
    clear();
}

/* 802F5CAC-802F5CF4       .text __dt__16J3DMaterialTableFv */
J3DMaterialTable::~J3DMaterialTable() {
}

/* 802F5CF4-802F5DC8       .text entryMatColorAnimator__16J3DMaterialTableFP11J3DAnmColor */
s32 J3DMaterialTable::entryMatColorAnimator(J3DAnmColor* pAnm) {
    s32 ret = 0;
    u16 materialNum = pAnm->getUpdateMaterialNum();

    if (isLocked())
        return J3DErrType_Locked;

    for (u16 i = 0; i < materialNum; i++) {
        u16 materialID = pAnm->getUpdateMaterialID(i);
        if (materialID != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(materialID)->getMaterialAnm();
            if (pMatAnm == NULL)
                ret = 1;
            else
                pMatAnm->setMatColorAnm(0, new J3DMatColorAnm(pAnm, i));
        }
    }

    return ret;
}

/* 802F5DC8-802F6058       .text entryTexMtxAnimator__16J3DMaterialTableFP19J3DAnmTextureSRTKey */
s32 J3DMaterialTable::entryTexMtxAnimator(J3DAnmTextureSRTKey* btk) {
    s32 ret = 0;
    u16 materialNum = btk->getUpdateMaterialNum();

    if (isLocked())
        return J3DErrType_Locked;

    for (u16 no = 0; no < materialNum; no++) {
        u16 materialID = btk->getUpdateMaterialID(no);
        if (materialID != 0xFFFF) {
            J3DMaterial * pMaterial = getMaterialNodePointer(materialID);
            J3DMaterialAnm * pMatAnm = pMaterial->getMaterialAnm();
            u8 texMtxID = btk->getUpdateTexMtxID(no);

            if (pMatAnm == NULL) {
                ret = 1;
                continue;
            }

            if (texMtxID != 0xFF) {
                if (pMaterial->getTexGenBlock()->getTexMtx(texMtxID) == NULL) {
                    J3DTexMtx* pMtx = new J3DTexMtx;
                    pMaterial->getTexGenBlock()->setTexMtx(texMtxID, pMtx);
                }

                if (pMaterial->getTexCoord(texMtxID) != NULL)
                    pMaterial->getTexCoord(texMtxID)->setTexGenMtx(GX_TEXMTX0 + (texMtxID & 0xFF) * 3);

                J3DTexMtx *tmtx = pMaterial->getTexMtx(texMtxID);
                J3DTexMtxAnm* tmtxanm = new J3DTexMtxAnm(btk, no);

                J3DTexMtxInfo& tmtxinfo = tmtx->getTexMtxInfo();
                tmtxinfo.mInfo = (tmtxinfo.mInfo & 0x7F) | btk->getTexMtxCalcType() << 7;
                tmtxinfo.mCenter.x = btk->getSRTCenter(no).x;
                tmtxinfo.mCenter.y = btk->getSRTCenter(no).y;
                tmtxinfo.mCenter.z = btk->getSRTCenter(no).z;

                pMatAnm->setTexMtxAnm(texMtxID, tmtxanm);
            }
        }
    }

    return ret;
}

/* 802F6058-802F61E8       .text entryTevRegAnimator__16J3DMaterialTableFP15J3DAnmTevRegKey */
s32 J3DMaterialTable::entryTevRegAnimator(J3DAnmTevRegKey* pAnm) {
    s32 ret = 0;
    u16 cRegMaterialNum = pAnm->getCRegUpdateMaterialNum();
    u16 kRegMaterialNum = pAnm->getKRegUpdateMaterialNum();

    if (isLocked())
        return J3DErrType_Locked;

    for (u16 i = 0; i < cRegMaterialNum; i++) {
        u16 materialID = pAnm->getCRegUpdateMaterialID(i);
        if (materialID != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(materialID)->getMaterialAnm();
            u8 colorId = pAnm->getAnmCRegKeyTable()[i].mColorId;
            if (pMatAnm == NULL)
                ret = 1;
            else
                pMatAnm->setTevColorAnm(colorId, new J3DTevColorAnm(pAnm, i));
        }
    }

    for (u16 i = 0; i < kRegMaterialNum; i++) {
        u16 materialID = pAnm->getKRegUpdateMaterialID(i);
        if (materialID != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(materialID)->getMaterialAnm();
            u8 colorId = pAnm->getAnmKRegKeyTable()[i].mColorId;
            if (pMatAnm == NULL)
                ret = 1;
            else
                pMatAnm->setTevKColorAnm(colorId, new J3DTevKColorAnm(pAnm, i));
        }
    }

    return ret;
}

/* 802F61E8-802F6260       .text removeMatColorAnimator__16J3DMaterialTableFP11J3DAnmColor */
int J3DMaterialTable::removeMatColorAnimator(J3DAnmColor* pAnm) {
    s32 ret = 0;
    u16 materialNum = pAnm->getUpdateMaterialNum();

    for (u16 i = 0; i < materialNum; i++) {
        u16 materialID = pAnm->getUpdateMaterialID(i);
        if (materialID != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(materialID)->getMaterialAnm();
            if (pMatAnm == NULL)
                ret = 1;
            else
                pMatAnm->setMatColorAnm(0, NULL);
        }
    }

    return ret;
}

/* 802F6260-802F62F4       .text removeTexNoAnimator__16J3DMaterialTableFP16J3DAnmTexPattern */
int J3DMaterialTable::removeTexNoAnimator(J3DAnmTexPattern* pAnm) {
    s32 ret = 0;
    u16 materialNum = pAnm->getUpdateMaterialNum();

    for (u16 i = 0; i < materialNum; i++) {
        u16 materialID = pAnm->getUpdateMaterialID(i);
        if (materialID != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(materialID)->getMaterialAnm();
            u8 texNo = pAnm->getAnmTable()[i].mTexNo;
            if (pMatAnm == NULL)
                ret = 1;
            else
                pMatAnm->setTexNoAnm(texNo, NULL);
        }
    }

    return ret;
}

/* 802F62F4-802F6398       .text removeTexMtxAnimator__16J3DMaterialTableFP19J3DAnmTextureSRTKey */
int J3DMaterialTable::removeTexMtxAnimator(J3DAnmTextureSRTKey* pAnm) {
    s32 ret = 0;
    u16 materialNum = pAnm->getUpdateMaterialNum();

    for (u16 i = 0; i < materialNum; i++) {
        u16 materialID = pAnm->getUpdateMaterialID(i);
        if (materialID != 0xFFFF) {
            J3DMaterial * pMaterial = getMaterialNodePointer(materialID);
            J3DMaterialAnm * pMatAnm = pMaterial->getMaterialAnm();
            u8 texMtxID = pAnm->getUpdateTexMtxID(i);
            if (pMatAnm == NULL) {
                ret = 1;
            } else if (texMtxID != 0xFF) {
                pMatAnm->setTexMtxAnm(texMtxID, NULL);
            }
        }
    }

    return ret;
}

/* 802F6398-802F64B8       .text removeTevRegAnimator__16J3DMaterialTableFP15J3DAnmTevRegKey */
int J3DMaterialTable::removeTevRegAnimator(J3DAnmTevRegKey* pAnm) {
    s32 ret = 0;
    u16 cRegMaterialNum = pAnm->getCRegUpdateMaterialNum();
    u16 kRegMaterialNum = pAnm->getKRegUpdateMaterialNum();

    for (u16 i = 0; i < cRegMaterialNum; i++) {
        if (pAnm->getCRegUpdateMaterialID(i) != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(pAnm->getCRegUpdateMaterialID(i))->getMaterialAnm();
            u8 colorId = pAnm->getAnmCRegKeyTable()[i].mColorId;
            if (pMatAnm == NULL)
                ret = 1;
            else
                pMatAnm->setTevColorAnm(colorId, NULL);
        }
    }

    for (u16 i = 0; i < kRegMaterialNum; i++) {
        if (pAnm->getKRegUpdateMaterialID(i) != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(pAnm->getKRegUpdateMaterialID(i))->getMaterialAnm();
            u8 colorId = pAnm->getAnmKRegKeyTable()[i].mColorId;
            if (pMatAnm == NULL) {
                ret = 1;
            } else {
                pMatAnm->setTevKColorAnm(colorId, NULL);
            }
        }
    }

    return ret;
}

/* 802F64B8-802F6550       .text setMatColorAnimator__16J3DMaterialTableFP11J3DAnmColorP14J3DMatColorAnm */
s32 J3DMaterialTable::setMatColorAnimator(J3DAnmColor* pAnm, J3DMatColorAnm* pAnmR) {
    s32 ret = 0;
    u16 materialNum = pAnm->getUpdateMaterialNum();

    if (isLocked())
        return J3DErrType_Locked;

    for (u16 i = 0; i < materialNum; i++) {
        u16 materialID = pAnm->getUpdateMaterialID(i);
        if (materialID != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(materialID)->getMaterialAnm();
            if (pMatAnm == NULL)
                ret = 1;
            else
                pMatAnm->setMatColorAnm(0, &pAnmR[i]);
        }
    }

    return ret;
}

/* 802F6550-802F6600       .text setTexNoAnimator__16J3DMaterialTableFP16J3DAnmTexPatternP11J3DTexNoAnm */
s32 J3DMaterialTable::setTexNoAnimator(J3DAnmTexPattern* pAnm, J3DTexNoAnm* pAnmR) {
    s32 ret = 0;
    u16 materialNum = pAnm->getUpdateMaterialNum();

    if (isLocked())
        return J3DErrType_Locked;

    for (u16 i = 0; i < materialNum; i++) {
        u16 materialID = pAnm->getUpdateMaterialID(i);
        if (materialID != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(materialID)->getMaterialAnm();
            u8 texNo = pAnm->getAnmTable()[i].mTexNo;
            if (pMatAnm == NULL)
                ret = 1;
            else
                pMatAnm->setTexNoAnm(texNo, &pAnmR[i]);
        }
    }

    return ret;
}

/* 802F6600-802F6798       .text setTexMtxAnimator__16J3DMaterialTableFP19J3DAnmTextureSRTKeyP12J3DTexMtxAnmP12J3DTexMtxAnm */
s32 J3DMaterialTable::setTexMtxAnimator(J3DAnmTextureSRTKey* btk, J3DTexMtxAnm* pTexAnm, J3DTexMtxAnm* pDualAnmR) {
    s32 ret = 0;
    u16 materialNum = btk->getUpdateMaterialNum();

    if (isLocked())
        return J3DErrType_Locked;

    for (u16 no = 0; no < materialNum; no++) {
        u16 materialID = btk->getUpdateMaterialID(no);
        if (materialID != 0xFFFF) {
            J3DMaterial * pMaterial = getMaterialNodePointer(materialID);
            J3DMaterialAnm * pMatAnm = pMaterial->getMaterialAnm();
            u8 texMtxID = btk->getUpdateTexMtxID(no);
            if (pMatAnm == NULL) {
                ret = 1;
            } else if (texMtxID != 0xFF) {
                if (pMaterial->getTexCoord(texMtxID) != NULL)
                    pMaterial->getTexCoord(texMtxID)->setTexGenMtx(GX_TEXMTX0 + (texMtxID & 0xFF) * 3);

                J3DTexMtx *tmtx = pMaterial->getTexMtx(texMtxID);
                J3DTexMtxInfo& tmtxinfo = tmtx->getTexMtxInfo();
                tmtxinfo.mInfo = (tmtxinfo.mInfo & 0x7F) | btk->getTexMtxCalcType() << 7;
                tmtxinfo.mCenter.x = btk->getSRTCenter(no).x;
                tmtxinfo.mCenter.y = btk->getSRTCenter(no).y;
                tmtxinfo.mCenter.z = btk->getSRTCenter(no).z;

                pMatAnm->setTexMtxAnm(texMtxID, &pTexAnm[no]);
            }
        }
    }

    return ret;
}

/* 802F6798-802F68F0       .text setTevRegAnimator__16J3DMaterialTableFP15J3DAnmTevRegKeyP14J3DTevColorAnmP15J3DTevKColorAnm */
s32 J3DMaterialTable::setTevRegAnimator(J3DAnmTevRegKey* pAnm, J3DTevColorAnm* pCRegAnmR, J3DTevKColorAnm* pKRegAnmR) {
    s32 ret = 0;
    u16 cRegMaterialNum = pAnm->getCRegUpdateMaterialNum();
    u16 kRegMaterialNum = pAnm->getKRegUpdateMaterialNum();

    if (isLocked())
        return J3DErrType_Locked;

    for (u16 i = 0; i < cRegMaterialNum; i++) {
        if (pAnm->getCRegUpdateMaterialID(i) != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(pAnm->getCRegUpdateMaterialID(i))->getMaterialAnm();
            u8 colorId = pAnm->getAnmCRegKeyTable()[i].mColorId;
            if (pMatAnm == NULL)
                ret = 1;
            else
                pMatAnm->setTevColorAnm(colorId, &pCRegAnmR[i]);
        }
    }

    for (u16 i = 0; i < kRegMaterialNum; i++) {
        if (pAnm->getKRegUpdateMaterialID(i) != 0xFFFF) {
            J3DMaterialAnm * pMatAnm = getMaterialNodePointer(pAnm->getKRegUpdateMaterialID(i))->getMaterialAnm();
            u8 colorId = pAnm->getAnmKRegKeyTable()[i].mColorId;
            if (pMatAnm == NULL) {
                ret = 1;
            } else {
                pMatAnm->setTevKColorAnm(colorId, &pKRegAnmR[i]);
            }
        }
    }

    return ret;
}
