Annotation of nono/lib/textscreen.h, revision 1.1.1.15

1.1       root        1: //
                      2: // nono
1.1.1.3   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
1.1.1.9   root        7: //
                      8: // テキストスクリーン
                      9: //
                     10: 
1.1       root       11: #pragma once
                     12: 
1.1.1.14  root       13: #include "nono.h"
1.1.1.7   root       14: #include <vector>
1.1.1.3   root       15: 
1.1       root       16: // 属性
1.1.1.3   root       17: class TA
                     18: {
                     19:  public:
                     20:        enum TextAttr : uint {
1.1.1.12  root       21:                // 文字(2バイト)の上位バイトが属性。
                     22:                //
                     23:                //  15  14  13  12  11  10   9   8
                     24:                // +---+---+---+---+---+---+---+---+
                     25:                // | B |     -     |  Color Code   |
                     26:                // +---+---+---+---+---+---+---+---+
                     27:                //
                     28:                // B はボールド。
                     29:                // Color Code は 4bit の前景色・背景色ペアの固定パレット。
                     30:                Normal                  = 0x00 << 8,    // 通常描画色
                     31:                Reverse                 = 0x01 << 8,    // 反転、背景黒
                     32:                Off                             = 0x02 << 8,    // オフを示す
                     33:                Disable                 = 0x03 << 8,    // 無効を示す
                     34:                ReverseRed              = 0x04 << 8,    // 反転、背景赤
                     35:                ReverseGreen    = 0x05 << 8,    // 反転、背景緑
                     36:                ReverseOrange   = 0x06 << 8,    // 反転、背景オレンジ
                     37: 
                     38:                Bold                    = 0x80 << 8,    // ボールド
                     39: 
                     40:                // エイリアス
                     41:                On                              = Reverse,                      // オンは反転で表現している
                     42:                Em                              = Bold | Normal,        // 通常色 + 強調のショートカット
1.1.1.3   root       43:        } attr {};
                     44: 
                     45:        TA() { }
                     46:        TA(TextAttr attr_) : attr(attr_) { }
1.1.1.7   root       47:        // uint からのコンストラクタ
                     48:        TA(uint attr_) : TA((TextAttr)attr_) { }
1.1.1.3   root       49: 
                     50:        // uint へのキャスト
                     51:        operator uint() const {
                     52:                return (uint)attr;
                     53:        }
                     54: 
1.1.1.7   root       55:        // static member
1.1.1.3   root       56:        // val によって On か Off を返す
                     57:        static const TA OnOff(bool val) {
                     58:                return (val) ? TA::On : TA::Off;
                     59:        }
1.1       root       60: };
                     61: 
                     62: //
                     63: // テキストスクリーン
                     64: //
                     65: // col × row で指定されるテキスト画面。
                     66: // 表示可能なのは 0x20..0x7e の printable 文字。
1.1.1.7   root       67: // 及び \n (0x0a) は復帰改行する。
                     68: // それ以外の 0x20 未満と 0x7f の動作は未定。
1.1       root       69: // 漢字は 2桁占有する。文字列として指定する時は UTF-8 だが、内部コードは
                     70: // Shift_JIS なので、第一水準、第二水準の文字だけを指定すること。
                     71: //
                     72: // TextScreen() コンストラクタで作成して Init(col, row) するか、
                     73: // TextScreen(col, row) コンストラクタを呼ぶかどちらか選択。
                     74: // チェックとかはしていないので併用しないこと。
                     75: //
1.1.1.7   root       76: // Locate などにより範囲外にカーソルを移動することはできる。
                     77: // ただしその位置を始点としての文字出力は無視される。
1.1.1.14  root       78: class TextScreen final
1.1       root       79: {
                     80:  public:
1.1.1.7   root       81:        // 文字の送りモード
                     82:        enum ModeKind {
                     83:                // 右端は無視される
                     84:                Fixed,
                     85:                // 右端は次の行に改行し、最終行だったときは上スクロールする
                     86:                Console,
                     87:                // 右端は次の行に改行し、最終行右端は左上に戻る
                     88:                Ring,
                     89:        } Mode { Fixed };
                     90: 
1.1       root       91:        TextScreen();
                     92:        TextScreen(int col, int row);
1.1.1.14  root       93:        ~TextScreen();
1.1       root       94: 
                     95:        // 初期化
1.1.1.8   root       96:        void Init(int col, int row, ModeKind mode = ModeKind::Fixed);
1.1       root       97: 
1.1.1.4   root       98:        // 画面をクリアして、カーソルをホームポジションに移動
1.1       root       99:        void Clear();
                    100: 
1.1.1.2   root      101:        // 現在位置を移動
1.1.1.3   root      102:        void Locate(int x, int y) {
                    103:                X = x;
                    104:                Y = y;
1.1.1.7   root      105:                OutOfScreen = (X < 0 || X >= col || Y < 0 || Y >= row);
                    106:        }
1.1.1.15! root      107:        void SetX(int x) {
        !           108:                X = x;
        !           109:                OutOfScreen = (X < 0 || X >= col);
        !           110:        }
        !           111:        void SetY(int y) {
        !           112:                Y = y;
        !           113:                OutOfScreen = (Y < 0 || Y >= row);
        !           114:        }
1.1.1.7   root      115: 
                    116:        // 文字出力後の右移動
1.1.1.15! root      117:        void Ascend();
        !           118: 
        !           119:        // 復帰
        !           120:        void CR() noexcept {
        !           121:                X = 0;
1.1.1.3   root      122:        }
1.1.1.2   root      123: 
1.1.1.15! root      124:        // 改行
        !           125:        void LF();
        !           126: 
1.1.1.7   root      127:        // 復帰改行
1.1.1.15! root      128:        void CRLF() {
        !           129:                CR();
        !           130:                LF();
1.1.1.7   root      131:        }
                    132: 
                    133:        // 1行の上スクロール
                    134:        void ScrollUp();
                    135: 
                    136:        //
                    137:        // 出力関数群
                    138:        // 出力関数はカーソル位置を更新する。
                    139:        //
                    140: 
1.1       root      141:        // 現在位置に1文字出力 (上位バイトは属性)
1.1.1.7   root      142:        void Putc(uint16 ch);
1.1       root      143: 
1.1.1.10  root      144:        // 座標を指定して1文字出力 (上位バイトは属性)
                    145:        void Putc(int x, int y, uint16 ch) {
                    146:                Locate(x, y);
                    147:                Putc(ch);
                    148:        }
                    149: 
1.1.1.11  root      150:        // 座標と属性を指定して1文字出力
                    151:        void Putc(int x, int y, TA attr, uint16 ch) {
                    152:                Locate(x, y);
                    153:                Putc((uint)attr | ch);
                    154:        }
                    155: 
1.1.1.3   root      156:        // 現在位置に文字列を出力
1.1       root      157:        void Puts(const char *str);
                    158: 
                    159:        // 現在位置に指定の属性で文字列を出力
1.1.1.3   root      160:        void Puts(TA attr, const char *str);
                    161: 
                    162:        // 座標を指定して文字列を出力
                    163:        void Puts(int x, int y, const char *str) {
                    164:                Locate(x, y);
                    165:                Puts(str);
                    166:        }
                    167: 
                    168:        // 座標と属性を指定して、文字列を出力
                    169:        void Puts(int x, int y, TA attr, const char *str) {
                    170:                Locate(x, y);
                    171:                Puts(attr, str);
                    172:        }
1.1       root      173: 
1.1.1.2   root      174:        // 現在位置に属性なしで書式付き文字列を出力
                    175:        void Print(const char *fmt, ...) __printflike(2, 3);
                    176: 
1.1       root      177:        // 座標を指定して属性なしで書式付き文字列を出力
                    178:        void Print(int x, int y, const char *fmt, ...) __printflike(4, 5);
                    179: 
                    180:        // 座標と属性を指定して、書式付き文字列を出力
1.1.1.3   root      181:        void Print(int x, int y, TA attr, const char *fmt, ...)
1.1       root      182:                __printflike(5, 6);
                    183: 
1.1.1.7   root      184:        // 現在のカーソル位置の文字を取得
1.1.1.15! root      185:        // カーソルは移動しない。
1.1.1.7   root      186:        uint16 Getc();
                    187: 
1.1.1.15! root      188:        // 現在のカーソル位置に文字を出力。
        !           189:        // カーソルは移動しない。ch がカーソル移動を伴う制御文字なら動作不定。
        !           190:        void Setc(uint16 ch);
        !           191: 
        !           192:        // 指定のカーソル位置に文字を出力。
        !           193:        // カーソルは移動しない。ch がカーソル移動を伴う制御文字なら動作不定。
        !           194:        void Setc(int x, int y, uint16 ch);
        !           195: 
1.1.1.7   root      196:        // 現在位置の属性を取得
                    197:        TA GetAttr() const;
                    198: 
                    199:        // 現在位置の属性を attr に設定
                    200:        // カーソルは移動しない。
                    201:        void SetAttr(TA attr);
1.1       root      202: 
1.1.1.7   root      203:        // バッファを取得。(描画デバイス向け)
                    204:        const std::vector<uint16>& GetBuf() const { return textbuf; }
1.1.1.2   root      205: 
                    206:        // 現在の X 座標を取得
1.1.1.15! root      207:        int GetX() const noexcept { return X; }
1.1.1.2   root      208:        // 現在の Y 座標を取得
1.1.1.15! root      209:        int GetY() const noexcept { return Y; }
1.1       root      210: 
                    211:        // 桁数を取得
1.1.1.15! root      212:        int GetCol() const noexcept { return col; }
1.1       root      213: 
                    214:        // 行数を取得
1.1.1.15! root      215:        int GetRow() const noexcept { return row; }
1.1       root      216: 
1.1.1.13  root      217:        // 行単位のコピー
                    218:        void CopyRowsFrom(int dst_y, int row, const TextScreen& src, int src_y);
                    219: 
1.1.1.4   root      220:        // ユーザ定義の引数
                    221:        // ご自由にお使いください。例えばモニタの出力開始オフセットやアドレス
                    222:        // など、呼び出し側が設定して書き出す側がそれに従った内容を出力する
                    223:        // ような用途。
1.1.1.5   root      224:        uint64 userdata {};
1.1.1.2   root      225: 
1.1.1.15! root      226:  private:
1.1       root      227:        // バッファ (幅x高さちょうどの長さ、ゼロ終端文字列ではない)
                    228:        // 上位8ビットは属性、下位8ビットが文字コード。
1.1.1.7   root      229:        std::vector<uint16> textbuf {};
1.1       root      230: 
                    231:        // 幅
1.1.1.5   root      232:        int col {};
1.1       root      233: 
                    234:        // 高さ
1.1.1.5   root      235:        int row {};
1.1       root      236: 
                    237:        // カーソル X 座標
1.1.1.5   root      238:        int X {};
1.1       root      239: 
                    240:        // カーソル Y 座標
1.1.1.5   root      241:        int Y {};
1.1.1.7   root      242: 
                    243:        // カーソルが画面外に位置するとき true
                    244:        bool OutOfScreen {};
1.1       root      245: };

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.