Class

Scroll

stdtask.Scroll(selectObj, viewCount)

指定されたSelectオブジェクトをラップし, スクロール機能を追加するクラス. すなわち, indexの値をscroll + offset (0 <= offset < viewCount) に分解し, かつ, 直前の状態からの遷移が自然になるようにこれらの値を更新する.

たとえば「optionsの要素のうち一度に表示できるものの個数が最大5個」であるような UIを作りたい場合, 次のようなコードを書く.

class MyUI extends stdtask.Scroll{
    constructor(options){
        const select = new stdtask.Select(
            options.length, ["ArrowUp", "ArrowDown"], "Enter", "Escape"
        );
        super(select, 5);
        this.options = options;
    }

    draw(GE, ctx){
        for(let i = 0; i < 5; i++){
            const targetIndex = this.scroll + i;
            my_paint_operation( this.options[targetIndex] );  // 普通はiも使うはずだが
        }
    }

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

選択肢の個数が表示できる最大数に満たない場合, 普通にSelectを使うのと変わらない. また, SelectではなくCyclicSelectを渡してもよい.

Constructor

# new Scroll(selectObj, viewCount)

一度に表示できる選択肢の個数がviewCountであるようなインスタンスを作る.

Parameters:
Name Type Description
selectObj stdtask.Select

ラップするSelectオブジェクト (CyclicSelectなどでも可)

viewCount number

一度に表示できる選択肢の個数

Properties:
Name Type Description
scroll number

現在のスクロール量

offset number

現在の表示区間において選択されている要素が何番目にあるか. より正確には selectObj.index == this.scroll + this.offset を満たす整数である (selectObjはコンストラクタで与えたSelectオブジェクト)

active boolean

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

View Source stdtask.js, line 369

Methods

# action(GE, index)

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

Parameters:
Name Type Description
GE stdgam.GameEngine

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

index number

this.indexの値

View Source stdtask.js, line 476

# 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 465

# cancel(GE, index)

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

Parameters:
Name Type Description
GE stdgam.GameEngine

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

index number

this.indexの値

View Source stdtask.js, line 484

# execute(GE)

1フレーム分のタスク処理を行う. より正確には, ラップされているSelectオブジェクトのexecuteを実行した後, その結果に基づき this.scroll と this.offset を更新する.

Parameters:
Name Type Description
GE stdgam.GameEngine

タスク処理に用いるGameEngine

View Source stdtask.js, line 399

# initScroll()

ラップされているSelectオブジェクトのindexの値を元に, this.scroll と this.offset の値を初期化する.

具体的には次のように決める.

  1. indexの値がviewCount未満なら, scroll = 0, offset = index でよい.
  2. そうでないとき, 先に offset = viewCount - 1 を決定してしまい,

それから scroll = index - offset とすればよい.

View Source stdtask.js, line 415

# resize(n)

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

Parameters:
Name Type Description
n number

新しい項目数

View Source stdtask.js, line 431

# viewCount() → {number}

View Source stdtask.js, line 389

一度に表示できる選択肢の個数

number