CUIを簡単に扱えるようにするPythonライブラリです
ライブラリ構造
CUILib
├─console
├─CUI
├─Window
├─WindowManager
├─MainloopEventCode
├─ScreenArea
├─InitializeError
├─PropertyError
├─ScreenSize
└─Vector2
コンソールを操作するこのライブラリで一番低レイヤなモジュールです
スクリーンの大きさを定めてコンソールの初期化を行います。 consoleモジュールを使用する際には必ず最初に実行する必要があります。
NT系OSではclsを、Unix系OSではclearコマンドを実行します。
カーソルを1行目1文字目に合わせてからコンソールに文字を表示させます
カーソルをl行目v文字目に合わせます
Updateを常時実行します Updateの戻り値にはMainloopEventCode型を返すことができます
- MainloopEventCode.Continue: メインループを引き続き実行させます
- MainloopEventCode.Exit_Mainloop: メインループから抜けます
ユーザーがCtrl+Cを押した場合にもメインループから抜けます 戻り値は終了コードです。(現在は0のみ返します)
consoleが初期化済みであるかを返します
スクリーンの大きさを取得します
カーソルの可視状態を制御します
コンソールを操作する基本的なモジュールです
スクリーンの大きさを定めてコンソールの初期化を行います consoleが未初期化状態の場合はconsoleも同時に初期化します consoleのスクリーンの大きさと異なる場合はInitializeErrorを返します
座標を指定してテキスト入力待ち状態にします
座標を指定してテキストを出力します スクリーンの大きさを超える引数が渡された場合、ValueErrorを返します
draw関数が実行できるかを返します
半角のみを出力可能にします より厳密にはASCII文字のみを出力可能にします
出力する場所を指定します
- ScreenArea.Back_Screen: バックスクリーンに出力し、screen_update関数が呼び出されてから描画します
- ScreenArea.Real_Screen: コンソールに出力します
出力時のみカーソルを非表示にし、set_draw_screen(ScreenArea.Back_Screen)を実行してからconsole.mainloopを呼び出します
コンソールの大きさを取得します
CUIが初期済みであるかを返します
CUI上にWindowのようなものを出力するクラスです
コンストラクタです
- name: Window名(タイトル)
- x, y: 座標
- width, height: Windowサイズ
- useframe: Windowのフレームを表示するか
- isreadonly: Windowの出力データ以外を読み取り専用にするか
Windowに対してテキストを出力します
Windowの出力済みのテキストデータを初期化します
コンソールにWindowを表示させます
現在のWindowサイズを取得します
現在のWindow名(タイトル)を取得します
現在のWindowの座標を取得します
オブジェクトが読み取り専用かを取得します
オブジェクトが枠を描画しているか判定します
Window名(タイトル)を変更します
Windowサイズを変更します
座標を変更します
Windowオブジェクトを一括管理するモジュールです
WindowManagerの初期化を行います CUIが初期化済みである必要があります
登録済みのWindowを最背面から順に描画します
Windowをnameとして登録します
nameを最前面にします
すべてのWindowを辞書配列で取得します
nameのWindowオブジェクトを取得します
登録済みのWindowの数を取得します
nameのWindowオブジェクトを除外し、そのオブジェクトを返します
WindowManagerが初期化済みであるかを返します
CUI/console.mainloopで使うイベントコード用の列挙型です
- MainloopEventCode.Continue: メインループを引き続き実行させます
- MainloopEventCode.Exit_Mainloop: メインループから抜けます
CUI.set_draw_screenで使う設定用の列挙型です
- ScreenArea.Back_Screen: バックスクリーンに出力し、screen_update関数が呼び出されてから描画します
- ScreenArea.Real_Screen: コンソールに出力します
モジュールを初期化する際に何かしらの原因で失敗すると返される例外です。
Windowのプロパティの変更でreadonlyの時に返される例外です。
スクリーンの大きさ用のクラスです
コンストラクタです
タプルで大きさを返します
widthを取得します
heightを取得します
二次元座標用のクラスです
コンストラクタです
座標を取得します
X座標を取得します
Y座標を取得します
import CUILib
import keyboard
CUILib.CUI.initialize(100, 30)
CUILib.WindowManager.initialize()
CUILib.WindowManager.register_window("Window", CUILib.Window("Window Window Window Window", 1, 1, 20, 10))
Text = "Hello World, CUILib! This is a Window object."
l = []
c = -1
for i in range(len(Text)):
if i%(CUILib.WindowManager.get_window("Window").get_size().get_width()-2) == 0:
l.append("")
c += 1
l[c] += Text[i]
CUILib.WindowManager.get_window("Window").draw(2, 2, l)
pressedr = False
pressedl = False
pressedu = False
pressedd = False
pressedD = False
pressedA = False
def Update():
global pressedr, pressedl, pressedu, pressedd, pressedD, pressedA
window = CUILib.WindowManager.get_window("Window")
window_size = window.get_size()
window_position = window.get_pos()
if keyboard.is_pressed("esc"):
return CUILib.MainloopEventCode.Exit_Mainloop
if not pressedr and keyboard.is_pressed("right") and window_position.get_x()+window_size.get_width()-1 < CUILib.CUI.get_size().get_width():
window.set_pos(window_position.get_x()+1, window_position.get_y())
pressedr = True
if not pressedl and keyboard.is_pressed("left") and window_position.get_x() > 1:
window.set_pos(window_position.get_x()-1, window_position.get_y())
pressedl = True
if not pressedu and keyboard.is_pressed("up") and window_position.get_y() > 1:
window.set_pos(window_position.get_x(), window_position.get_y()-1)
pressedu = True
if not pressedd and keyboard.is_pressed("down") and window_position.get_y()+window_size.get_height()-1 < CUILib.CUI.get_size().get_height():
window.set_pos(window_position.get_x(), window_position.get_y()+1)
pressedd = True
if not pressedD and keyboard.is_pressed("d") and window_position.get_x()+window_size.get_width()-1 < CUILib.CUI.get_size().get_width():
pressedD = True
window.resize(window_size.get_width()+1, window_size.get_height())
window.clear()
l = []
c = -1
for i in range(len(Text)):
if i%(window.get_size().get_width()-2) == 0:
l.append("")
c += 1
l[c] += Text[i]
window.draw(2, 2, l)
if not pressedA and keyboard.is_pressed("a") and window_size.get_width() > 21:
pressedA = True
window.resize(window_size.get_width()-1, window_size.get_height())
window.clear()
l = []
c = -1
for i in range(len(Text)):
if i%(window.get_size().get_width()-2) == 0:
l.append("")
c += 1
l[c] += Text[i]
window.draw(2, 2, l)
if not keyboard.is_pressed("right"):
pressedr = False
if not keyboard.is_pressed("left"):
pressedl = False
if not keyboard.is_pressed("up"):
pressedu = False
if not keyboard.is_pressed("down"):
pressedd = False
if not keyboard.is_pressed("d"):
pressedD = False
if not keyboard.is_pressed("a"):
pressedA = False
CUILib.WindowManager.show()
return CUILib.MainloopEventCode.Continue
CUILib.CUI.mainloop(Update)
CUILib.console.set_cursor_visible(True)バグはTwitterのDMかDiscordのDMで教えてください 嫌な顔しながら修正します それ以外はどこでも構いません エゴサはしないので直接伝えたいならDMでお願いします
あとデバッグちゃんとしてないので探せばバグはあると思います。