
#include "Tscene.h"


// deroulé de l'action dans Mcycle
void Tscene::pick_ray(int x, int y)
{
    enum {PICK_NULL, PICK_MECA, PICK_PERSO, PICK_OBJET};
    float dist, distMem = 36.0f; // on limite l'action à 6x6 m de distance
    OMat4 view;
	OVec3_f ray_origin;     // position du point souris
    OVec3_f ray_direction;  // direction centre ecran vers point souris
    OVec3_f coin_bas, coin_haut;
    OVec3_i pick(PICK_NULL,0,0);

    lblInfo1.setText("rien");

    view.loadIdentity();
    camera.lookAt(view);

    pickRay.getRay(x,y, ECO_getViewPortW(), ECO_getViewPortH(),	view, m_proj3d, ray_origin, ray_direction);

    for (size_t i = 0; i < meca.size(); i++)
    {
        coin_bas = meca[i].getBound_min();
        coin_haut = meca[i].getBound_max();
        dist = pickRay.test(ray_origin, ray_direction, coin_bas, coin_haut, meca[i].getModel());

        if (dist < distMem)
        {
            distMem = dist;
            pick.set(PICK_MECA, i, 0);
            lblInfo1.setText(meca[i].getCode() + " / " + ECO_floatToStr(dist));
        }
    }

    if (pick.x == PICK_NULL)
    {
    coin_bas.set(-0.5f, 0.0f, -0.5f); // verticale y (glb) pour perso
    coin_haut.set(0.5f, 2.0f, 0.5f);
    for (size_t i = 0; i < perso.size(); i++)
    {
        //coin_bas = perso[i].getBound_min();
        //coin_haut = perso[i].getBound_max();
        dist = pickRay.test(ray_origin, ray_direction, coin_bas, coin_haut, perso[i].getModel());

        if (dist < distMem)
        {
            distMem = dist;
            pick.set(PICK_PERSO, i, 0);
            lblInfo1.setText(perso[i].getCode() + " / " + ECO_floatToStr(dist));
        }
    }
    }

    if (pick.x == PICK_NULL)
    {
    coin_bas.set(-0.25f, -0.25f, -0.25f); // verticale y (glb) pour objet
    coin_haut.set(0.25f,  0.25f,  0.25f);
    for (size_t i = 0; i < objet.size(); i++)
    {
        //coin_bas = objet[i].getBound_min();
        //coin_haut = objet[i].getBound_max();
        dist = pickRay.test(ray_origin, ray_direction, coin_bas, coin_haut, objet[i].getModel());

        if (dist < distMem)
        {
            distMem = dist;
            pick.set(PICK_OBJET, i, 0);
            lblInfo1.setText(objet[i].getCode() + " / " + ECO_floatToStr(dist));
        }
    }
    }

    // ---

    switch (pick.x)
    {
    case PICK_MECA:
        if (meca[pick.y].getAnim_state() == MEC_ANIM_RUN) break;

        // moulin et coffre
        if (meca[pick.y].getCode() == "mou100" && meca[pick.y].getAnim_state() != MEC_ANIM_RUN)
        {
            meca[pick.y].anim_start_cycle("cycle");
        }
        else // if meca[pick.y].getcode() == "cof100"
        if (meca[pick.y].getState() == MEC_FERME)
        {
            if (meca[pick.y].getAnim("ouvrir") > -1) meca[pick.y].anim_start("ouvrir");
        }
        else
        {
            if (meca[pick.y].getAnim("fermer") > -1) meca[pick.y].anim_start("fermer");
        }
        break;

    case PICK_PERSO:
        perso[pick.y].setSelect();
        break;

    case PICK_OBJET:
        objet[pick.y].setSelect();
        break;
    }


}


// survol de la souris
void Tscene::pick_ray_move(int x, int y)
{
    enum {PICK_NULL, PICK_MECA, PICK_meca, PICK_OBJET};
    float dist, distMem = 15.0f; // on limite l'action à 15 m de distance
    OMat4 view;
	OVec3_f ray_origin;     // position du point souris
    OVec3_f ray_direction;  // direction centre ecran vers point souris
    OVec3_f coin_bas, coin_haut;
    OVec3_i idx(PICK_NULL,0,0);

    view.loadIdentity();
    camera.lookAt(view);

    ECO_mouse_set(IDC_ARROW);

    pickRay.getRay(x,y, ECO_getViewPortW(), ECO_getViewPortH(),	view, m_proj3d, ray_origin, ray_direction);

    for (size_t i = 0; i < meca.size(); i++)
    {
        coin_bas = meca[i].getBound_min();
        coin_haut = meca[i].getBound_max();
        dist = pickRay.test(ray_origin, ray_direction, coin_bas, coin_haut, meca[i].getModel());

        if (dist < distMem)
        {
            distMem = dist;
            idx.set(PICK_meca, i, 0);
            //lblInfo1.setText(meca[i].getCode() + " / " + ECO_floatToStr(dist));
        }
    }

    if (idx.x == PICK_meca)
    {
        ECO_mouse_set(IDC_HAND);
    }

}
