--- nono/lib/textscreen.h 2026/04/29 17:04:28 1.1 +++ nono/lib/textscreen.h 2026/04/29 17:04:34 1.1.1.3 @@ -1,19 +1,39 @@ // // nono -// Copyright (C) 2018 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // #pragma once +#include "header.h" + // 属性 -enum TA { - Normal = 0x00 << 8, - // On, Off, Disable は3ビットを使った状態値。 - // これらが実際どう表現されるかはデバイスによる。 - On = 0x01 << 8, // ビットとかがオンであることを示す - Off = 0x02 << 8, // ビットとかがオフであることを示す - Disable = 0x03 << 8, // 機能などが無効であることを示す - Em = 0x04 << 8, // 値の変化など強調することを示す +class TA +{ + public: + enum TextAttr : uint { + Normal = 0x00 << 8, + // On, Off, Disable は3ビットを使った状態値。 + // これらが実際どう表現されるかはデバイスによる。 + On = 0x01 << 8, // ビットとかがオンであることを示す + Off = 0x02 << 8, // ビットとかがオフであることを示す + Disable = 0x03 << 8, // 機能などが無効であることを示す + Em = 0x04 << 8, // 値の変化など強調することを示す + } attr {}; + + TA() { } + TA(TextAttr attr_) : attr(attr_) { } + + // uint へのキャスト + operator uint() const { + return (uint)attr; + } + + // val によって On か Off を返す + static const TA OnOff(bool val) { + return (val) ? TA::On : TA::Off; + } }; // @@ -39,32 +59,61 @@ class TextScreen // 初期化 void Init(int col, int row); + // 折り返しモードかどうか + bool foldmode {}; + // クリア void Clear(); + // 現在位置を移動 + void Locate(int x, int y) { + X = x; + Y = y; + } + // 現在位置に1文字出力 (上位バイトは属性) void Putc(uint16 ch); - // 現在位置に字列を出力 + // 現在位置に文字列を出力 void Puts(const char *str); // 現在位置に指定の属性で文字列を出力 - void Puts(uint attr, const char *str); + void Puts(TA attr, const char *str); + + // 座標を指定して文字列を出力 + void Puts(int x, int y, const char *str) { + Locate(x, y); + Puts(str); + } + + // 座標と属性を指定して、文字列を出力 + void Puts(int x, int y, TA attr, const char *str) { + Locate(x, y); + Puts(attr, str); + } + + // 現在位置に属性なしで書式付き文字列を出力 + void Print(const char *fmt, ...) __printflike(2, 3); // 座標を指定して属性なしで書式付き文字列を出力 void Print(int x, int y, const char *fmt, ...) __printflike(4, 5); // 座標と属性を指定して、書式付き文字列を出力 - void Print(int x, int y, uint attr, const char *fmt, ...) + void Print(int x, int y, TA attr, const char *fmt, ...) __printflike(5, 6); // バッファアドレスを取得。(描画デバイス向け) - const uint16 *GetBuf() const { return textbuf; } + const uint16 *GetBuf() const { return textbuf.get(); } // XXX 生バッファを取得 // バッファの内部構造を変えた場合はこれを呼んでる人については // 見直しをすること。そのうちなんとかするかも - uint16 *GetRawBuf() { return textbuf; } + uint16 *GetRawBuf() { return textbuf.get(); } + + // 現在の X 座標を取得 + int GetX() const { return X; } + // 現在の Y 座標を取得 + int GetY() const { return Y; } // 桁数を取得 int GetCol() const { return col; } @@ -73,9 +122,12 @@ class TextScreen int GetRow() const { return row; } private: + // 改行(次の行の左端に移動) + void CRLF(); + // バッファ (幅x高さちょうどの長さ、ゼロ終端文字列ではない) // 上位8ビットは属性、下位8ビットが文字コード。 - uint16 *textbuf = NULL; + std::unique_ptr textbuf {}; // 幅 int col = 0;