SSブログ

Android SDK で,ゲームを作る (3) [プログラム三昧]このエントリーを含むはてなブックマーク#

WS000944.png

Model 部分を切り出された残りが,ユーザインターフェイスとつなぎです.

SeekNumberActivity オブジェクト

Model 部分を別ファイルに独立させたので,見通しが良くなりました.このオブジェクトは, GUI に関わる部分と, GUI と Model をつなぐインスタンスで構成されています.

/*
 * $Id$
 * --------------------------------------------
 * File         : SeekNumberActivity.java
 * Package      : org.noritan.seeknumber
 * Copyright    : Copyright (c) 2010 noritan.org
 * Organization : noritan.org
 * Created      : 2010/03/12
 * --------------------------------------------
 */
package org.noritan.seeknumber;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * This <code>SeekNumberActivity</code> class is an <code>Activity</code>
 * of the Seek Number Game.
 * 
 * @author noritan
 */
public class SeekNumberActivity extends Activity {
    private int columnCount = 4;
    private int rowCount = 4;
    private GridView content;
    private TextView statusBar;
    private Button startButton;
    private ButtonAdapter adapter;
    private SeekNumberModel model;
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int buttonCount = columnCount * rowCount;
        
        // Create a model object.
        model = new SeekNumberModel(buttonCount);

        // Create an adapter to model
        adapter = new ButtonAdapter(this, buttonCount);
        
        // Set-up the content GridView
        content = (GridView) findViewById(R.id.content);
        content.setNumColumns(columnCount);
        content.setAdapter(adapter);
        content.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick (
                AdapterView<?> parent,
                View view,
                int position,
                long id
            ) {
                model.click(position);
            }
        });
        
        // Prepare a reference to the status bar.
        statusBar = (TextView) findViewById(R.id.status);
        
        // Set-up the START button.
        startButton = (Button) findViewById(R.id.start);
        startButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                model.reorder();
            }
        });
        
        // Prepare an event listener of the model.
        model.addListener(new SeekNumberListener(){
            @Override
            public void initialized(int[] assignment) {
                // Initialize the status message and grid.
                statusBar.setText("GO!!");
                adapter.initialize(assignment);
            }
            @Override
            public void hit(int position) {
                // Modify the view of hit button.
                adapter.hit(position);
            }

            @Override
            public void finished() {
                // Set a message as a status.
                statusBar.setText("Finished !!");
            }
        });
    }
    

    /**
     * This <code>ButtonAdapter</code> class is used to provide views
     * for an attached {@link GridView} object.
     * 
     * @author noritan
     */
    public class ButtonAdapter extends BaseAdapter {
        /**
         * A <code>TextView</code> array type of <code>buttonArray</code>
         * field.
         * This field have a list of buttons provided as Views
         * put on a {@link android.widget.GridView} object.
         * 
         */
        private TextView[] buttonArray;
        
        /**
         * Construct a <code>ButtonAdapter</code> object.
         * Creates and initializes buttons regarding the buttonCount
         * parameter.
         *
         * @param context a parent {@link android.widget.GridView} object including
         * views provided by this object.
         * @param buttonCount Number of buttons to be created on the
         * parent {@link android.widget.GridView} object.
         * 
         * @see android.widget.GridView
         */
        public ButtonAdapter(Context context, int buttonCount) {
            // create a list of buttons.
            buttonArray = new TextView[buttonCount];
            for (int i = 0; i < buttonCount; i++) {
                TextView button = new TextView(context);
                button.setText(String.valueOf(i+1));
                button.setGravity(Gravity.CENTER);
                button.setPadding(5, 5, 5, 5);
                buttonArray[i] = button;
            }
        }
        /**
         * Initialize the label of each buttons.
         * The label for all buttons are re-assigned regarding the
         * parameter.
         * All buttons are initializes to be visible.
         * 
         * @param assignment indicates the label assignments for each
         * buttons.
         */
        public void initialize(int[] assignment) {
            for (int i = 0; i < buttonArray.length; i++) {
                TextView button = buttonArray[i];
                button.setText(String.valueOf(assignment[i]+1));
                button.setVisibility(View.VISIBLE);
            }
            notifyDataSetInvalidated();
        }
        /**
         * Show a behavior when a button is hit.
         * The position of the hit button is specified by
         * the parameter.
         * The hit button is set to INVISIBLE.
         * 
         * @param position position of the hit button.
         */
        public void hit(int position) {
            buttonArray[position].setVisibility(View.INVISIBLE);
            notifyDataSetInvalidated();
        }
        @Override
        public int getCount() {
            return buttonArray.length;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return buttonArray[position];
        }
    }    
}

ゲームロジックである Model を GUI に依存しないようにすることは,できましたが,さすがに Model に依存しない GUI とすることは出来ませんでした.このオブジェクトは, SeekNumberModel というオブジェクトに依存しています.

が, Model 内部の状態に関する情報を持たないように設計されていますので,ゲームのルールを変更する場合には, Model を変更するだけで対応することができます.また,見た目を変更する場合でも,この GUI オブジェクト部分だけを変更すれば良いようになっています.

レイアウト表現 : main.xml

ファイル main.xml で,レイアウトに必要なリソースを表現しています.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
    <TextView  
        android:id="@+id/title"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/app_name"
        android:gravity="center"
        android:layout_margin="10dp"
    />
    <GridView 
        android:id="@+id/content"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:columnWidth="60dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        android:background="#333333"
    />
    <TextView  
        android:id="@+id/status"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text=""
        android:gravity="center"
        android:layout_margin="10dp"
    />
    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_margin="10dp"
        android:text="START"
    />
</LinearLayout>

次回は,GUI 部分を GridView ではなく TableView で表現するように変更してみます.


nice!(0)  コメント(0)  トラックバック(0)  このエントリーを含むはてなブックマーク#

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

トラックバックの受付は締め切りました

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。