Newer
Older
Cactus-CleanArchitecture / app / src / main / java / org / ntlab / radishforandroidstudio / cactusClient / tests / SampleUiFragment.java
package org.ntlab.radishforandroidstudio.cactusClient.tests;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import org.ntlab.radishforandroidstudio.R;
import org.ntlab.radishforandroidstudio.framework.RWT.RWTSurfaceView;
import org.ntlab.radishforandroidstudio.framework.RWT.RWTUIFragment;
import org.ntlab.radishforandroidstudio.framework.gameMain.OvergroundActor;
import org.ntlab.radishforandroidstudio.framework.gameMain.RealTime3DFragment;
import org.ntlab.radishforandroidstudio.framework.model3D.BaseObject3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Object3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
import org.ntlab.radishforandroidstudio.framework.physics.Ground;
import org.ntlab.radishforandroidstudio.java3d.Appearance;
import org.ntlab.radishforandroidstudio.java3d.Box;
import org.ntlab.radishforandroidstudio.java3d.IndexedTriangleArray;
import org.ntlab.radishforandroidstudio.java3d.Material;
import org.ntlab.radishforandroidstudio.java3d.Point3d;
import org.ntlab.radishforandroidstudio.java3d.TextureCubeMap;
import org.ntlab.radishforandroidstudio.java3d.TextureLoader;
import org.ntlab.radishforandroidstudio.java3d.Vector3f;

public class SampleUiFragment extends RealTime3DFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 3Dモデルの作成

        // サイコロの作成
        Appearance ap1 = new Appearance();
        Material m = new Material();
        m.setDiffuseColor(1.0f, 1.0f, 1.0f);
        ap1.setMaterial(m);
        TextureCubeMap tex1 = new TextureCubeMap(TextureCubeMap.BASE_LEVEL, TextureCubeMap.RGB, 0);
        TextureLoader loader = new TextureLoader(getResources(), R.drawable.dice_1, TextureLoader.BY_REFERENCE | TextureLoader.Y_UP);
        tex1.setImage(0, TextureCubeMap.POSITIVE_Z, loader.getImage());
        loader = new TextureLoader(getResources(), R.drawable.dice_6, TextureLoader.BY_REFERENCE | TextureLoader.Y_UP);
        tex1.setImage(0, TextureCubeMap.NEGATIVE_Z, loader.getImage());
        loader = new TextureLoader(getResources(), R.drawable.dice_4, TextureLoader.BY_REFERENCE | TextureLoader.Y_UP);
        tex1.setImage(0, TextureCubeMap.POSITIVE_X, loader.getImage());
        loader = new TextureLoader(getResources(), R.drawable.dice_3, TextureLoader.BY_REFERENCE | TextureLoader.Y_UP);
        tex1.setImage(0, TextureCubeMap.NEGATIVE_X, loader.getImage());
        loader = new TextureLoader(getResources(), R.drawable.dice_5, TextureLoader.BY_REFERENCE | TextureLoader.Y_UP);
        tex1.setImage(0, TextureCubeMap.POSITIVE_Y, loader.getImage());
        loader = new TextureLoader(getResources(), R.drawable.dice_2, TextureLoader.BY_REFERENCE | TextureLoader.Y_UP);
        tex1.setImage(0, TextureCubeMap.NEGATIVE_Y, loader.getImage());
        ap1.setTexture(tex1);
        Box b1 = new Box(1.0f, 1.0f, 1.0f, ap1);
        final Object3D obj1 = new Object3D("box", b1);
        obj1.scale(2.0, 2.0, 2.0);
        obj1.apply(new Position3D(0.0, 30.0, 0.0), false);
        OvergroundActor actor = new OvergroundActor(obj1, null);
        universe.place(actor);

        // 地面の作成
        IndexedTriangleArray groundGeometry = new IndexedTriangleArray(4,
                IndexedTriangleArray.COORDINATES | IndexedTriangleArray.NORMALS, 6);
        groundGeometry.setCoordinate(0, new Point3d(-20.0, 0.0, -20.0));
        groundGeometry.setCoordinate(1, new Point3d(20.0, 0.0, -20.0));
        groundGeometry.setCoordinate(2, new Point3d(20.0, 0.0, 20.0));
        groundGeometry.setCoordinate(3, new Point3d(-20.0, 0.0, 20.0));
        groundGeometry.setNormal(0, new Vector3f(0.0f, 1.0f, 0.0f));
        groundGeometry.setNormal(1, new Vector3f(0.0f, 1.0f, 0.0f));
        groundGeometry.setNormal(2, new Vector3f(0.0f, 1.0f, 0.0f));
        groundGeometry.setNormal(3, new Vector3f(0.0f, 1.0f, 0.0f));
        groundGeometry.setCoordinateIndices(0, new int[]{0, 3, 2});
        groundGeometry.setCoordinateIndices(3, new int[]{0, 2, 1});
        Appearance ap2 = new Appearance();
        Material m2 = new Material();
        m2.setDiffuseColor(0.5f, 1.0f, 0.2f);
        ap2.setMaterial(m2);
        Ground ground = new Ground(new BaseObject3D(groundGeometry, ap2));
        universe.place(ground);

        // カメラの設定
        camera.setViewPoint(new Position3D(0.0, 10.0, -20.0));
        camera.addTarget(actor);

        // フレームの間隔(ms)
        start(16);

        //onCreateOptionMenuの呼び出し
        setHasOptionsMenu(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        initGameWindowView();
        return parentView;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;

        }
        return super.onOptionsItemSelected(item);
    }

    public void initGameWindowView() {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        Fragment f = new RWTUIFragment();
        View uiLayout = parentView.findViewById(R.id.ui_layout);
        uiLayout.setOnTouchListener((RWTUIFragment) f);
        transaction.add(R.id.ui_layout, f);
        transaction.commit();
    }
}