コンピュータを楽しもう!!

今、自分が面白くていろいろやってみたことを書き綴りたいと思います。連絡先はtarosa.yでgmail.comです。

モジュール化ネタ その2

モジュール化ネタですが、ついでにLuaridaで良く使う関数もモジュール化してみました。でも、これはネタにしかならないです。サンプル程度ですね。

luarida_l_module

スクロールしていく文字表示を、printという関数にしました。これをモジュールにしたいと思います。モジュールファイル名は、luarida_l_module.luaとします。プログラムは下ですが、このモジュールで別のモジュールを読み込んでいます。それが、色定義モジュールです。luarida_c_module.luaとしました。
print関数の引数は可変にしました。可変にしなくても、引数足りなくてエラーって出ないんだっけ?
とりあえず、可変にしました。モジュール宣言で"lmo"としているので、lmo.print()という使い方になります。ついでに、文字編集でよく使うsplitも入れておきました。

------------------------------------------
--Luarida用のモジュール(luarida_l_module.lua)
------------------------------------------
--モジュール宣言-------------------------
module( "lmo", package.seeall )
--他のモジュール読み込み宣言
package.path =  system.getCardMnt().."/luarida/?.lua"
require( "luarida_c_module" )   --色定義モジュール
--関数宣言--------------------------------
print={}          --スクロールするテキスト表示
split={}          --文字の分解
--グローバル変数宣言----------------------
Gwide, Gheight = canvas.getviewSize()  --画面サイズ取得
------------------------------------------
--スクロールするテキスト表示
-- 文字, サイズ, 色, 背景色
------------------------------------------
function print(...)
 local t={...}
 local str = t[1]
 if( str==nil )then str = "" end
 local fontsize = t[2]
 if( fontsize==nil )then fontsize = 18 end
 local fcolor = t[3]
 if( fcolor==nil )then fcolor = col.black end
 local bcolor = t[4]
 if( bcolor==nil )then bcolor = col.white end
 --一度、見えないところにテキストを書いて、改行数を求める
 local sc = canvas.putTextBox( str, 0, Gheight+1, fontsize, fcolor, Gwide )
 --画面の絵をワークエリアに取り込みます
 canvas.getg( 0, fontsize*sc, Gwide-1, Gheight-1, 0, fontsize*sc, Gwide-1, Gheight-1 )
 --取り込んだ画面をスクロールさせて描きます
 canvas.putg( 0, 0, Gwide-1, Gheight-fontsize*sc-1, 0, fontsize*sc, Gwide-1, Gheight-1 )
 --書き出す部分をバックカラーで塗り潰します
 canvas.putRect(  0, Gheight-fontsize*sc-1, Gwide, Gheight, bcolor, 1 )
 --スクロールしたところにテキストを書きます
 canvas.drawTextBox( str, 0, Gheight-fontsize*sc, fontsize, fcolor, Gwide )
end
------------------------------------------
--文字の分解
------------------------------------------
function split(str, d)
local s = str
local t = {}
local p = "%s*(.-)%s*"..d.."%s*"
local f = function(v) table.insert(t, v) end
 if s ~= nil then
   string.gsub(s, p, f)
   f(string.gsub(s, p, ""))
 end
 return t
end

luarida_c_module

カラー定義を別モジュールにしました。下記です。col.whiteとかcol.blackというように使います。

------------------------------------------
--Luarida用の色モジュール(luarida_c_module.lua)
------------------------------------------
--モジュール宣言-------------------------
module( "col", package.seeall )
--関数宣言--------------------------------
--グローバル変数宣言----------------------
white = color(255,255,255)  --白
black = color(0,0,0)        --黒
red = color(255,0,0)        --赤
green = color(0,255,0)      --緑
blue = color(0,0,255)       --青

最後に使用サンプル

モジュールを使ってみるサンプルを書きました。lmoもcolも読めているようです。これから汎用に使えそうな関数があれば、lmoに追加していこうかと思います。
動作画面とサンプルプログラムを下に書きます。

--モジュール読み込み宣言
package.path =  system.getCardMnt().."/luarida/?.lua"
require( "luarida_l_module" )  --Luaridaで良く使う関数
require( "luarida_c_module" )  --色の定義

function main()
 lmo.print( "print動作するかな?")
 lmo.print( "色も反映するかな? Gwide="..lmo.Gwide,nil,col.red)
 lmo.print( "これは青色です。 Gheight="..lmo.Gheight,nil,col.blue)
 lmo.print( "これはフォント40でかいです。",40,col.green)
 touch(3)
end
main()
system.exit()

以上です。