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

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.3   root       13: #include "header.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       root       78: class TextScreen
                     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);
                     93:        virtual ~TextScreen();
                     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:        }
                    107: 
                    108:        // 文字出力後の右移動
                    109:        void Ascend() {
                    110:                X++;
                    111:                if (X >= col) {
                    112:                        switch (Mode) {
                    113:                         case Fixed:
                    114:                                OutOfScreen = true;
                    115:                                break;
                    116:                         case Console:
                    117:                         case Ring:
                    118:                                CRLF();
                    119:                                break;
                    120:                        }
                    121:                }
1.1.1.3   root      122:        }
1.1.1.2   root      123: 
1.1.1.7   root      124:        // 復帰改行
                    125:        void CRLF()
                    126:        {
                    127:                X = 0;
                    128:                Y++;
                    129:                switch (Mode) {
                    130:                 case Fixed:
                    131:                        break;
                    132:                 case Console:
                    133:                        if (Y >= row) {
                    134:                                ScrollUp();
                    135:                                Y = row - 1;
                    136:                        }
                    137:                        break;
                    138:                 case Ring:
                    139:                        if (Y >= row) {
                    140:                                Y = 0;
                    141:                        }
                    142:                        break;
                    143:                }
                    144:                // どのモードであっても OutOfScreen を更新
                    145:                // (Y=-1 から CRLF したら表示可能になるべき)
                    146:                OutOfScreen = (Y < 0 || Y >= row);
                    147:        }
                    148: 
                    149:        // 1行の上スクロール
                    150:        void ScrollUp();
                    151: 
                    152:        //
                    153:        // 出力関数群
                    154:        // 出力関数はカーソル位置を更新する。
                    155:        //
                    156: 
1.1       root      157:        // 現在位置に1文字出力 (上位バイトは属性)
1.1.1.7   root      158:        void Putc(uint16 ch);
1.1       root      159: 
1.1.1.10  root      160:        // 座標を指定して1文字出力 (上位バイトは属性)
                    161:        void Putc(int x, int y, uint16 ch) {
                    162:                Locate(x, y);
                    163:                Putc(ch);
                    164:        }
                    165: 
1.1.1.11  root      166:        // 座標と属性を指定して1文字出力
                    167:        void Putc(int x, int y, TA attr, uint16 ch) {
                    168:                Locate(x, y);
                    169:                Putc((uint)attr | ch);
                    170:        }
                    171: 
1.1.1.3   root      172:        // 現在位置に文字列を出力
1.1       root      173:        void Puts(const char *str);
                    174: 
                    175:        // 現在位置に指定の属性で文字列を出力
1.1.1.3   root      176:        void Puts(TA attr, const char *str);
                    177: 
                    178:        // 座標を指定して文字列を出力
                    179:        void Puts(int x, int y, const char *str) {
                    180:                Locate(x, y);
                    181:                Puts(str);
                    182:        }
                    183: 
                    184:        // 座標と属性を指定して、文字列を出力
                    185:        void Puts(int x, int y, TA attr, const char *str) {
                    186:                Locate(x, y);
                    187:                Puts(attr, str);
                    188:        }
1.1       root      189: 
1.1.1.2   root      190:        // 現在位置に属性なしで書式付き文字列を出力
                    191:        void Print(const char *fmt, ...) __printflike(2, 3);
                    192: 
1.1       root      193:        // 座標を指定して属性なしで書式付き文字列を出力
                    194:        void Print(int x, int y, const char *fmt, ...) __printflike(4, 5);
                    195: 
                    196:        // 座標と属性を指定して、書式付き文字列を出力
1.1.1.3   root      197:        void Print(int x, int y, TA attr, const char *fmt, ...)
1.1       root      198:                __printflike(5, 6);
                    199: 
1.1.1.7   root      200:        // 現在のカーソル位置の文字を取得
                    201:        // カーソルは移動する。
                    202:        uint16 Getc();
                    203: 
                    204:        // 現在位置の属性を取得
                    205:        TA GetAttr() const;
                    206: 
                    207:        // 現在位置の属性を attr に設定
                    208:        // カーソルは移動しない。
                    209:        void SetAttr(TA attr);
1.1       root      210: 
1.1.1.7   root      211:        // バッファを取得。(描画デバイス向け)
                    212:        const std::vector<uint16>& GetBuf() const { return textbuf; }
1.1.1.2   root      213: 
                    214:        // 現在の X 座標を取得
                    215:        int GetX() const { return X; }
                    216:        // 現在の Y 座標を取得
                    217:        int GetY() const { return Y; }
1.1       root      218: 
                    219:        // 桁数を取得
                    220:        int GetCol() const { return col; }
                    221: 
                    222:        // 行数を取得
                    223:        int GetRow() const { return row; }
                    224: 
1.1.1.13! root      225:        // 行単位のコピー
        !           226:        void CopyRowsFrom(int dst_y, int row, const TextScreen& src, int src_y);
        !           227: 
1.1.1.4   root      228:        // ユーザ定義の引数
                    229:        // ご自由にお使いください。例えばモニタの出力開始オフセットやアドレス
                    230:        // など、呼び出し側が設定して書き出す側がそれに従った内容を出力する
                    231:        // ような用途。
1.1.1.5   root      232:        uint64 userdata {};
1.1.1.2   root      233: 
1.1.1.4   root      234:  protected:
1.1       root      235:        // バッファ (幅x高さちょうどの長さ、ゼロ終端文字列ではない)
                    236:        // 上位8ビットは属性、下位8ビットが文字コード。
1.1.1.7   root      237:        std::vector<uint16> textbuf {};
1.1       root      238: 
                    239:        // 幅
1.1.1.5   root      240:        int col {};
1.1       root      241: 
                    242:        // 高さ
1.1.1.5   root      243:        int row {};
1.1       root      244: 
                    245:        // カーソル X 座標
1.1.1.5   root      246:        int X {};
1.1       root      247: 
                    248:        // カーソル Y 座標
1.1.1.5   root      249:        int Y {};
1.1.1.7   root      250: 
                    251:        // カーソルが画面外に位置するとき true
                    252:        bool OutOfScreen {};
1.1       root      253: };

unix.superglobalmegacorp.com

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