Newer
Older
IrisClient / app / src / main / java / org / ntlab / irisclient / DrawingActivity.java
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.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import org.ntlab.irisclient.models.Member;
import org.ntlab.irisclient.viewmodels.DrawingStateViewModel;

import java.io.ByteArrayOutputStream;
import java.util.List;

public class DrawingActivity extends AppCompatActivity implements View.OnClickListener {

    private DrawingCanvas drawingCanvas;
    private DrawingStateViewModel drawingStateViewModel;

    private List<String> keywordList;
    private Integer drawingNum;
    private Integer drawingNow = 0;

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

        //Irisから必要な情報を取得
        Iris iris = (Iris) this.getApplication();
        Boolean bMaster = iris.isMaster();
        List<Member> memberList = iris.getMemberList();

        drawingStateViewModel= new ViewModelProvider(this).get(DrawingStateViewModel.class);

        findViewById(R.id.clearButton).setOnClickListener(this);//画面クリアボタン
        findViewById(R.id.eraserButton).setOnClickListener(this);//消しゴムボタン
        findViewById(R.id.blackPenButton).setOnClickListener(this);//消しゴムボタン

        ToggleButton okToggleButton = (ToggleButton) findViewById(R.id.toggleButton);
        okToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    okToggleButton.setTextOn("完了");
                    drawingCanvas.setEnabled(true);
                    drawingCanvas.setClickable(true);
                    drawingCanvas.setColor(Color.BLACK);

                } else {
                    okToggleButton.setTextOff("描き直す");
                    drawingCanvas.setEnabled(false);
                    drawingCanvas.setClickable(false);
                    drawingCanvas.setColor(Color.TRANSPARENT);

                    //putDrawing
                    drawingCanvas.setDrawingCacheEnabled(true);//キャッシュを取得する設定にする
                    drawingCanvas.buildDrawingCache();
                    Bitmap bitmap = Bitmap.createBitmap(drawingCanvas.getDrawingCache());
                    drawingCanvas.destroyDrawingCache();//既存のキャッシュをクリアする
                    drawingStateViewModel.putDrawing(bitmap);
                }
            }
        });


        TextView textKeyword =(TextView)findViewById(R.id.textKeyword);//keywordのTextView
        TextView textMemberNum = (TextView)findViewById(R.id.drawingNowText);//残りのイラスト枚数のTextView
        drawingCanvas = (DrawingCanvas) findViewById(R.id.drawingCanvas);

        //残りのイラスト枚数の表示
        drawingNum = 16 / 2; // 一人あたりが描く枚数(テスト部屋用,部屋作成からスタートする場合はこの行をコメントアウトして ↓ ふたつのコメントを解除)
        //drawingNum = 16 / memberList.size(); // 一人あたりが描く枚数
        //if (16 % memberList.size() != 0) drawingNum++; // 余りが出た場合プラス1枚

        //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);
                drawingNow++;
                textMemberNum.setText(""+drawingNow+"/"+drawingNum);
                drawingCanvas.clearCanvas();

                okToggleButton.setChecked(true);
            }
        });

        //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);
                }
            }
        });

        //タイマースタート呼び出し
        drawingStateViewModel.start(500,(Iris)getApplication());
    }

    @Override
    public void onClick(View v){
        if(v.getId() == R.id.clearButton){//clearButtonが押されたときの処理
            drawingCanvas.clearCanvas();
        }else if(v.getId() == R.id.eraserButton){//eraserButtonが押されたときの処理
            drawingCanvas.setColor(Color.WHITE);
        }else if(v.getId() == R.id.blackPenButton){
            drawingCanvas.setColor(Color.BLACK);
        }
    }

}