〜 テキスト 〜
1行に表示する最大幅を設定して自動的に折り返す

通常、テキストがウィンドウ端まで来ても、そのままウィンドウ外へと続いていきます。
テキストの長さが不定の場合で折り返し位置が分からない場合や、
自分で実装するのが面倒な時は、下記のサンプルのようなモジュールを使ってみては?
長い文章でもテキスト右端は気にしなくて良くなるでしょうが、下へは制限を設けていません。
コレを応用すれば、予め用意しておいたテキストエリア内にだけ表示させることも出来ますので、
ノベル系ゲームやRPGゲーム等に見られるテキストエリアで発揮することでしょう。
尚、指定した幅よりもフォントサイズの方が大きい場合に、
1文字も表示されず無限ループとならないような方法を取っています。
その為、指定幅よりも、フォントサイズの方が大きい場合だけ例外的に、はみ出します。
	

wordchk 対象変数, 位置
対象変数判別したい文字列の入った変数を指定する。
statに-1(範囲外)、0(半角)、1(全角)のいずれかがセットされる。
位置指定変数の位置をバイト単位で指定する。

retmes 対象文字列, 横幅
対象文字列自動改行表示させる文字列、または文字列型変数を指定する。
内部でwordchk命令を使用しているため、retmes命令の前にwordchk命令を定義すること!
横幅自動で折り返す、カレントポジションからの横幅をピクセル単位で指定する。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#module
#deffunc wordchk val, int
	mref txt, 24 : mref ichi, 1
	mref stt, 64
	peek chk, txt, ichi
	if chk = 0 : stt = -1 : return                                        // 範囲外
	if (chk > 128 & (chk < 160)) | (chk > 223) : stt = 1 : else : stt = 0 // 1byte=0 2byte=1
	return

#deffunc retmes str, int
	mref string, 32 : mref w, 1
	mref bmscr, 67
	sdim data, 4096
	data = string
	cx = csrx : cy = csry
	x = cx : y = cy
	strlen len, data
	repeat len
		// 改行
		strmid tmp, data, cnt
		if tmp = "\n" {
			x = cx : y += bmscr.32 // フォント高さ分下の位置にずらす
			continue cnt + 2
		}
		// その他の文字
		wordchk data, cnt
		strmid tmp, data, cnt, stat + 1
		pos 0, dispy : mes tmp
		ginfo 7
		if x - cx + prmx >= w : x = cx : y += prmy // 指定幅を超えたら改行
		pos x, y : mes tmp
		x += prmx
		if stat + 1 = 2 : continue cnt + 2 //全角文字なら、次回チェック位置を2バイト先に進める
	loop
	pos cx, y + ginfo_mesy
	return
#global

	haba = 200
	color 200, 200, 200 : boxf 200, 50, 200 + haba // 表示領域を色付け
	color
	pos 200, 50 : retmes "自動的に折り返すような、少し長めのtextを表示してみるtest。", haba
	stop

結果 = wordchk(対象変数, 位置)
結果-1(範囲外)、0(半角)、1(全角)のいずれかがセットされる。
対象変数判別したい文字列を指定する。
位置指定変数の位置をバイト単位で指定する。

retmes 対象文字列, 横幅
対象文字列自動改行表示させるテキスト、また文字列型変数を指定する。
内部でwordchk命令を使用しているため、retmes命令の前にwordchk命令を定義すること!
横幅自動で折り返す、カレントポジションからの横幅をピクセル単位で指定する。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#module
#defcfunc wordchk str txt, int ichi
	if ichi < 0 : return -1 // 範囲外
	s = txt
	chk = peek(s, ichi)
	if (chk > 128 & chk < 160) | chk > 223 : return 1 // 2byte=1
	if chk = 0 : return -1 : else : return 0          // 1byte=0 範囲外=-1

#deffunc retmes str string, int w, local data, local cx, local cy, local x, local y, local byte
	sdim data, strlen(string) + 1
	data = string
	cx = ginfo_cx : cy = ginfo_cy
	x = cx : y = cy
	repeat strlen(data)
		// 改行
		if strmid(data, cnt, 2) = "\n" {
			x = cx : y += ginfo_mesy
			continue cnt + 2
		}
		// その他の文字
		byte = wordchk(data, cnt) + 1
		pos 0, ginfo_dispx : mes strmid(data, cnt, byte)
		if x - cx + ginfo_mesx >= w : x = cx : y += ginfo_mesy // 指定幅を超えたら改行
		pos x, y : mes strmid(data, cnt, byte)
		x += ginfo_mesx
		if byte = 2 : continue cnt + 2 //全角文字なら、次回チェック位置を2バイト先に進める
	loop
	pos cx, y + ginfo_mesy
	return
#global

	haba = 200
	color 200, 200, 200 : boxf 200, 50, 200 + haba // 表示領域を色付け
	color
	pos 200, 50 : retmes "自動的に折り返すような、少し長めのtextを表示してみるtest。", haba