ここを読む前にボタン取得とタッチスクリーンの項目に眼を通しておいてください。
展開したscriptsフォルダの中にある「02 - TextPadsStylus.lua」を開くと以下のようなコードが記述してあります。
(コードがやや長めなので、最初からコメントを削除して行番号を付けてあります)
実行してみれば分かりますが、これは各種ボタンとタッチスクリーンの取得を行うプログラムです。
(scripts/02 - TextPadsStylus.lua)
01:SCREEN_BOTTOM = 0 02:SCREEN_TOP = 1 03:BGTopText = Screen.LoadTextBG() 04:BGBotText = Screen.LoadTextBG() 05:Screen.Initialize( SCREEN_TOP, BGTopText ) 06:Screen.Initialize( SCREEN_BOTTOM, BGBotText ) 07: 08:nOldX, nOldY = 0, 0 09:nNewX, nNewY = 0, 0 10:nRealX, nRealY = 0, 0 11:noPressCounter = 0 12: 13:BGBotText:Clear() 14:BGBotText:SetColor( 6 ) 15:BGBotText:FillRectWithChar( 0, 0, 31, 23, "X" ) 16:BGBotText:FillRectWithChar( 1, 1, 30, 22, " " ) 17:BGBotText:SetColor( 5 ) 18:BGBotText:PrintXY( 10, 0, " Input Test " ) 19: 20:while true do 21: bStylusNewPress = Stylus.NewPress() 22: bStylusDown = Stylus.Down() 23: bStylusReleased = Stylus.Released() 24: nRealX = Stylus.X() 25: nRealY = Stylus.Y() 26: nNewX = math.floor( nRealX / 8 ) 27: nNewY = math.floor( nRealY / 8 ) 28: 29: if bStylusNewPress then 30: BGTopText:PrintXY( nOldX, nOldY, " " ) 31: BGTopText:PrintXY( nNewX, nNewY, "1" ) 32: nOldX, nOldY = nNewX, nNewY 33: end 34: if bStylusDown then 35: BGTopText:PrintXY( nOldX, nOldY, " " ) 36: BGTopText:PrintXY( nNewX, nNewY, "2" ) 37: nOldX, nOldY = nNewX, nNewY 38: end 39: if bStylusReleased then 40: BGTopText:PrintXY( nOldX, nOldY, " " ) 41: BGTopText:PrintXY( nNewX, nNewY, "3" ) 42: nOldX, nOldY = nNewX, nNewY 43: end 44: 45: BGBotText:FillRectWithChar( 2, 2, 30, 14, " " ) 46: 47: BGBotText:SetColor( 2 ) 48: BGBotText:PrintXY( 2, 2, "A = " .. tostring( Pads.A() ) ) 49: BGBotText:PrintXY( 2, 3, "B = " .. tostring( Pads.B() ) ) 50: BGBotText:PrintXY( 2, 4, "X = " .. tostring( Pads.X() ) ) 51: BGBotText:PrintXY( 2, 5, "Y = " .. tostring( Pads.Y() ) ) 52: BGBotText:PrintXY( 2, 6, "R = " .. tostring( Pads.R() ) ) 53: BGBotText:PrintXY( 2, 7, "L = " .. tostring( Pads.L() ) ) 54: BGBotText:PrintXY( 2, 8, "Up = " .. tostring( Pads.Up() ) ) 55: BGBotText:PrintXY( 2, 9, "Down = " .. tostring( Pads.Down() ) ) 56: BGBotText:PrintXY( 2, 10, "Left = " .. tostring( Pads.Left() ) ) 57: BGBotText:PrintXY( 2, 11, "Right = " .. tostring( Pads.Right() ) ) 58: BGBotText:PrintXY( 2, 12, "Select = " .. tostring( Pads.Select() ) ) 59: BGBotText:PrintXY( 2, 13, "Stylus New Press = " .. tostring( bStylusNewPress ) ) 60: BGBotText:PrintXY( 2, 14, "Stylus Down = " .. tostring( bStylusDown ) ) 61: BGBotText:PrintXY( 2, 15, "Stylus Released = " .. tostring( bStylusReleased ) ) 62: BGBotText:PrintXY( 2, 16, "StylusXY = " .. nRealX .. "," .. nRealY ) 63: 64: BGBotText:SetColor( 0 ) 65: BGBotText:PrintXY( 2, 17, "Press Start to exit..." ) 66: 67: if Pads.Start() then 68: break 69: end 70: 71: Screen.WaitForVBL() 72:end
プログラムの解説
01:SCREEN_BOTTOM = 0 02:SCREEN_TOP = 1 03:BGTopText = Screen.LoadTextBG() 04:BGBotText = Screen.LoadTextBG() 05:Screen.Initialize( SCREEN_TOP, BGTopText ) 06:Screen.Initialize( SCREEN_BOTTOM, BGBotText )
毎度おなじみの画面の初期化処理です。
特筆すべきことはないのでさらっと飛ばしてしまいます。
08:nOldX, nOldY = 0, 0 09:nNewX, nNewY = 0, 0 10:nRealX, nRealY = 0, 0 11:noPressCounter = 0
変数の宣言と初期化処理をしています。
これらの変数はタッチスクリーンの押下位置の座標関連になります。
noPressCounterだけは存在意義が不明。
13:BGBotText:Clear() 14:BGBotText:SetColor( 6 ) 15:BGBotText:FillRectWithChar( 0, 0, 31, 23, "X" ) 16:BGBotText:FillRectWithChar( 1, 1, 30, 22, " " )
画面を消去し、文字色を青色にセットします。
その後FillRectWithChar()関数で指定した範囲を、指定した文字で埋め尽くしています。
上記の例では(0, 0)から(31, 23)までの範囲(画面全体)を全て「X」で埋めています。
また、すぐその後に(1, 1)から(30, 22)まで「 (半角スペース)」で上書きすることで消去しています。
FillRectWithChar(x1, y1, x2, y2, Char)
x1……範囲の左上座標X
y1……範囲の左上座標Y
x2……範囲の右下座標X
y2……範囲の右下座標Y
Char……埋め尽くす文字
17:BGBotText:SetColor( 5 ) 18:BGBotText:PrintXY( 10, 0, " Input Test " )
文字色を別な色でセットし直して、「 Input Text 」と表示しています。
しかし、バグなのか変更し忘れたのか、SetColor( 5 )に対応する色が黒色なので表示されていません。
(背景色と同じ色なので実際の画面上には見えません)
20:while true do
無限ループの始まりです。
このwhileに対応するendは72行目のものになります。
21: bStylusNewPress = Stylus.NewPress() 22: bStylusDown = Stylus.Down() 23: bStylusReleased = Stylus.Released()
タッチスクリーンに関するデータを取得し、変数に格納しています。
24: nRealX = Stylus.X() 25: nRealY = Stylus.Y()
タッチした座標をそれぞれ取得しています。
26: nNewX = math.floor( nRealX / 8 ) 27: nNewY = math.floor( nRealY / 8 )
座標を文字サイズ(デフォルトで8ドット)で割り、文字位置を算出しています。
29: if bStylusNewPress then 30: BGTopText:PrintXY( nOldX, nOldY, " " ) 31: BGTopText:PrintXY( nNewX, nNewY, "1" ) 32: nOldX, nOldY = nNewX, nNewY 33: end
画面がタッチされた瞬間に1を表示しています。
……が、1/60ですぐに消去されているので実質ここのプログラムは体感できません。
34: if bStylusDown then 35: BGTopText:PrintXY( nOldX, nOldY, " " ) 36: BGTopText:PrintXY( nNewX, nNewY, "2" ) 37: nOldX, nOldY = nNewX, nNewY 38: end
スクリーンをタッチされている間、上画面の同じ座標に2と表示しています。
39: if bStylusReleased then 40: BGTopText:PrintXY( nOldX, nOldY, " " ) 41: BGTopText:PrintXY( nNewX, nNewY, "3" ) 42: nOldX, nOldY = nNewX, nNewY 43: end
スクリーンに最後にタッチした位置を読み込み、上画面の同じ座標に3と表示しています。
45: BGBotText:FillRectWithChar( 2, 2, 30, 14, " " )
(2, 2)から(30, 14)までをクリアーしています。
47: BGBotText:SetColor( 2 ) 48: BGBotText:PrintXY( 2, 2, "A = " .. tostring( Pads.A() ) ) 49: BGBotText:PrintXY( 2, 3, "B = " .. tostring( Pads.B() ) ) 50: BGBotText:PrintXY( 2, 4, "X = " .. tostring( Pads.X() ) ) 51: BGBotText:PrintXY( 2, 5, "Y = " .. tostring( Pads.Y() ) ) 52: BGBotText:PrintXY( 2, 6, "R = " .. tostring( Pads.R() ) ) 53: BGBotText:PrintXY( 2, 7, "L = " .. tostring( Pads.L() ) ) 54: BGBotText:PrintXY( 2, 8, "Up = " .. tostring( Pads.Up() ) ) 55: BGBotText:PrintXY( 2, 9, "Down = " .. tostring( Pads.Down() ) ) 56: BGBotText:PrintXY( 2, 10, "Left = " .. tostring( Pads.Left() ) ) 57: BGBotText:PrintXY( 2, 11, "Right = " .. tostring( Pads.Right() ) ) 58: BGBotText:PrintXY( 2, 12, "Select = " .. tostring( Pads.Select() ) )
各種ボタンが押されている間、対応する位置にtrueと表示し続けます。
59: BGBotText:PrintXY( 2, 13, "Stylus New Press = " .. tostring( bStylusNewPress ) ) 60: BGBotText:PrintXY( 2, 14, "Stylus Down = " .. tostring( bStylusDown ) ) 61: BGBotText:PrintXY( 2, 15, "Stylus Released = " .. tostring( bStylusReleased ) )
タッチスクリーンの状態を取得して表示しています。
62: BGBotText:PrintXY( 2, 16, "StylusXY = " .. nRealX .. "," .. nRealY )
上画面のどこに文字列を表示しているかを文字ベースの座標で表示しています。
のですが、消去処理が上手く出来ていないので数値がとんでもないものになることも……。
BGBotText:FillRectWithChar( 2, 16, 30, 16, " " )
61行目と62行目の間にこの処理を追加すれば綺麗に表示されます。
64: BGBotText:SetColor( 0 ) 65: BGBotText:PrintXY( 2, 17, "Press Start to exit..." )
「STARTボタンでプログラムを終了します」と画面に表示。
67: if Pads.Start() then 68: break 69: end
STARTボタンが押されていればwhileループを抜け、プログラムを終了します。
71: Screen.WaitForVBL()
ゲームのFPSを60に設定するために使用する関数……とでも覚えて置いてください。
72:end
20行目のwhileに対応するendです。