前回までの状況はこちら。
では、早速実装していきます。
まず、以前作成した数独解析プログラムはそのまま使用します。
しかし、出力処理部分はいまのところスケルトン化(クラス定義の形だけ残して、実際は何もしない)としておきます。
そして、新たにSquareObjectクラスを作成します。
これがマスのテクスチャなどを扱うクラスになります。
実装はこんな感じ。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sudokuGUI
{
    class SquareObject
    {
        private int _x;
        private int _y;
        private int _row;
        private int _col;
        private int _value;
        private asd.TextureObject2D _backTexture;
        private asd.TextObject2D _valueText;
        private const int offsetX = 10;
        private const int offsetY = 10;
        private const int fontOffsetX = 19;
        private const int fontOffsetY = 9;
        public SquareObject(int row, int col)
        {
            _row = row;
            _col = col;
            _x = row * 64 + offsetX;
            _y = col * 64 + offsetY;
            _value = 0;
            _backTexture = new asd.TextureObject2D();
            _backTexture.Position = new asd.Vector2DF(_x, _y);
            _backTexture.Texture = Resource.getTexture();
            _valueText = new asd.TextObject2D();
            _valueText.Font = Resource.getFont();
            _valueText.Position = new asd.Vector2DF(_x + fontOffsetX, _y + fontOffsetY);
        }
        
        public void setValue(int value)
        {
            _value = value;
            if(value == 0)
            {
                _valueText.Text = "";
            }
            else
            {
                _valueText.Text = value.ToString();
            }
        }
        public asd.TextureObject2D getBackTexture()
        {
            return _backTexture;
        }
        public asd.TextObject2D getTextObject()
        {
            return _valueText;
        }
    }
}
SquareObjectの中で、テクスチャオブジェクトとテキストオブジェクトを作成し、getメソッドで返すようにします。
setValueで数字を設定するときに表示する数字をテキストに設定します。しかし、0が入力されたときはテキストは表示しないようにします。
そして、Resourceクラスを作成しました。
テクスチャやフォントはここからgetして使用します。
実装はこんな感じです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sudokuGUI
{
    static class Resource
    {
        private static asd.Texture2D _texture = null;
        private static asd.Font _font = null;
        public static asd.Texture2D getTexture()
        {
            if(_texture == null)
            {
                _texture = asd.Engine.Graphics.CreateTexture2D("square.png");
            }
            return _texture;
        }
        public static asd.Font getFont()
        {
            if (_font == null)
            {
                _font = asd.Engine.Graphics.CreateFont("number.aff");
            }
            return _font;
        }
    }
}
staticクラスで作成し、最初に作成したオブジェクトをみんなで使い回す設計になっています。
テクスチャで用意した絵はこんな感じです。
メインの処理はこんな感じで実装してみました。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sudokuGUI
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Altseedを初期化する。
            asd.Engine.Initialize("数独解析ツール", 1000, 800, new asd.EngineOption());
            // 下地
            var background = new asd.GeometryObject2D();
            asd.Engine.AddObject2D(background);
            var bgRect = new asd.RectangleShape();
            bgRect.DrawingArea = new asd.RectF(0, 0, 1000, 800);
            background.Shape = bgRect;
            // テクスチャー
            asd.Texture2D texture = asd.Engine.Graphics.CreateTexture2D("squares.png");
            // マス
            int offsetX = 10;
            int offsetY = 10;
            var square = new asd.TextureObject2D();
            square.Position = new asd.Vector2DF(offsetX, offsetY);
            square.Texture = texture;
            asd.Engine.AddObject2D(square);
            var obj = new SquareObject(0, 0);
            obj.setValue(1);
            asd.Engine.AddObject2D(obj.getBackTexture());
            asd.Engine.AddObject2D(obj.getTextObject());
            // Altseedが進行可能かチェックする。
            while (asd.Engine.DoEvents())
            {
                // Altseedを更新する。
                asd.Engine.Update();
            }
            // Altseedを終了する。
            asd.Engine.Terminate();
        }
    }
}
実行すると、こんな感じです。
テキストを0に設定するとこうなります。
うん、いいんじゃ無いでしょうか?
次回はマウス操作でテクスチャをON/OFFする実装を追加して、9×9マスを作成したいと思います。