Class

Select

stdtask.Select(itemCount, dirKeys, actionKey, cancelKey, firstValueopt, waitopt, modalopt)

indexプロパティを持ち, キー入力に応じて

  • indexの増減
  • this.action(GE, index)の実行
  • this.cancel(GE, index)の実行

を実行するタスクオブジェクトを実装する.

ここで登場したindexプロパティは「複数の選択肢から1つを選ばせるUI」における 現在選択されている要素のインデックスを抽象化したものである.

たとえば, 「矢印キーでindexを変化させてEnterで決定, ESCでキャンセル」という UIを作る場合,

class MyUI extends stdtask.Select{
    constructor(){
        super(選択肢の個数, ["ArrowUp", "ArrowDown"], "Enter", "Escape");
    }

    action(GE, index){ 決定時の処理 }
    cancel(GE, index){ キャンセル時の処理 }
}

のようにすればよい. または, Selectを継承するのではなく,

class MyUI{
    constructor(){
        this.select = new stdtask.Select(
            選択肢の個数, ["ArrowUp", "ArrowDown"], "Enter", "Escape"
        );
        this.select.bind(this);
    }

    execute(GE){ return this.select.execute(GE); }
    action(GE, index){ 決定時の処理 }
    cancel(GE, index){ キャンセル時の処理 }
}

のように委譲してもよい.

Constructor

# new Select(itemCount, dirKeys, actionKey, cancelKey, firstValueopt, waitopt, modalopt)

指定された設定に基づきインスタンスを生成する.

Parameters:
Name Type Attributes Default Description
itemCount number

選択肢の個数. this.indexは 0 ~ (itemCount-1) の範囲を動く

dirKeys Array.<string>

this.indexの増減に使うキーのキーコード. dirKeys[0]が減少, dirKeys[1]が増加のキーとして扱われる

actionKey string

このキーが押されたとき, this.action を実行する

cancelKey string

このキーが押されたとき, this.cancel を実行する

firstValue number <optional>
0

this.indexの初期値

wait number <optional>
10

dirKeysに属するキーを押しっぱなしにしたとき, どの程度の間隔が空いていればキーイベントを受理するか指定する

modal boolean <optional>
true

自分より後ろのタスク処理をブロックするか

Properties:
Name Type Description
index number

このオブジェクトが管理するパラメータ

active boolean

(stdgam.Sceneの意味で) このオブジェクトが有効か

View Source stdtask.js, line 179

Methods

# action(GE, index)

actionKeyとして指定したキーが押されたときに呼び出される. デフォルトでは何もしない.

Parameters:
Name Type Description
GE stdgam.GameEngine

タスク処理を実行するために使うGameEngine

index number

this.indexの値

View Source stdtask.js, line 289

# bind(other)

actionやcancelの実行を指定したオブジェクトに委任する. すなわち, 以下の処理を行う.

  • other.action(GE, index) を実行するだけの関数を this.action に代入
  • other.cancel(GE, index) を実行するだけの関数を this.cancel に代入
Parameters:
Name Type Description
other Object

action/cancelの処理を委任されるオブジェクト

View Source stdtask.js, line 278

# cancel(GE, index)

cancelKeyとして指定したキーが押されたときに呼び出される. デフォルトでは何もしない.

Parameters:
Name Type Description
GE stdgam.GameEngine

タスク処理を実行するために使うGameEngine

index number

this.indexの値

View Source stdtask.js, line 297

# execute(GE)

1フレーム分のタスク処理を実行する. 具体的には, キー入力に応じてthis.indexを増減させたり this.action/this.cancel を実行したりする.

【注意】this.action/this.canelを実行したフレームでは, modalの設定と 無関係に必ず false を返す. なぜならば, もしこれら処理の中で タスクリストの変更が発生した場合「forループの途中で中身を変更する」のと 同じ状況が生じるためである. 後続のタスク処理をブロックすることで安全にループを抜けることができる.

Parameters:
Name Type Description
GE stdgam.GameEngine

タスク処理に用いるGameEngine

View Source stdtask.js, line 243

次のいずれかの条件を満たすとき false を返す

  1. 自分より後ろのタスク処理をブロックする設定の場合
  2. このフレームにおいてthis.action や this.cancel を実行した場合

そうでないとき true を返す

# move()

k == 0 のとき, this.indexを1減らす (ただし0未満にはならない). k == 1 のとき, this.indexを1増やす (ただし「選択肢の個数-1」を超えない).

View Source stdtask.js, line 266

# resize(n)

項目数を変更する. これによりthis.indexの値が範囲外になる場合, this.indexを max(n-1, 0) に変更する.

Parameters:
Name Type Description
n number

新しい項目数

View Source stdtask.js, line 220