Newer
Older
IrisClient / app / src / main / java / org / ntlab / irisclient / DrawingActivity.java
g-akagi on 20 Oct 2022 3 KB AndroidManifestを元に戻す
package org.ntlab.irisclient;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.ntlab.irisclient.viewmodels.DrawingStateViewModel;

import java.util.List;

public class DrawingActivity extends AppCompatActivity implements View.OnClickListener {

    private DrawingCanvas drawingCanvas;
    private DrawingStateViewModel drawingStateViewModel;

    private List<String> keywordList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawing);

        //Irisから必要な情報を取得
        Iris iris = (Iris) this.getApplication();
        String rid = iris.getRid();
        String nickName = iris.getNickname();
        Boolean bMaster = iris.isMaster();

        drawingStateViewModel= new ViewModelProvider(this).get(DrawingStateViewModel.class);
        //viewModelに必用な情報をセット
        drawingStateViewModel.setRid(rid);
        drawingStateViewModel.setNickName(nickName);

        findViewById(R.id.okButtton).setOnClickListener(this);//お絵描き終了ボタン
        findViewById(R.id.clearButton).setOnClickListener(this);//画面クリアボタン
        findViewById(R.id.eraserButton).setOnClickListener(this);//消しゴムボタン

        TextView textKeyword =(TextView)findViewById(R.id.textKeyword);//keywordのTextView
        drawingCanvas = (DrawingCanvas) findViewById(R.id.drawingCanvas);

        //getKeywords

        drawingStateViewModel.getKeywords().observe(this, new Observer<List<String>>(){
           @Override
           public void onChanged(List<String> keywords) {
               keywordList = keywords;
           }
        });

        //getDno
        drawingStateViewModel.getDno().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer dno) {
                String keyword = keywordList.get(dno);
                textKeyword.setText("Dno: " + dno + ", Keyword: " + keyword);
            }
        });

        //getState
        drawingStateViewModel.getState().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer state) {
                Class nextActivity = GameMemberActivity.class;
                if(bMaster) nextActivity = GameMasterActivity.class;//自分がMasterならMaster用のGame画面へ遷移

                //stateが変わったらゲーム画面へ遷移
                if(state == 3) {
                    Intent intent = new Intent(DrawingActivity.this, nextActivity);
                    startActivity(intent);
                }
            }
        });

        //getKeywordの呼び出し
        drawingStateViewModel.init();
        //タイマースタート呼び出し
        drawingStateViewModel.start(500,iris);
    }

    @Override
    public void onClick(View v){
        if(v.getId() == R.id.okButtton) {//okButtonが押されたときの処理
            Bitmap bitmap = null;
            drawingStateViewModel.putDrawing(bitmap);
        }else if(v.getId() == R.id.clearButton){//clearButtonが押されたときの処理
            drawingCanvas.clearCanvas();
        }else if(v.getId() == R.id.eraserButton){//eraserButtonが押されたときの処理
            drawingCanvas.eraserPaint();
        }
    }

}