--- nono/lib/textscreen.cpp 2026/04/29 17:04:31 1.1.1.2 +++ nono/lib/textscreen.cpp 2026/04/29 17:05:26 1.1.1.11 @@ -1,15 +1,15 @@ // // nono -// Copyright (C) 2018 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // -#include "header.h" -#include "textscreen.h" - // // テキストスクリーン // +#include "textscreen.h" + // コンストラクタ TextScreen::TextScreen() { @@ -28,12 +28,16 @@ TextScreen::~TextScreen() // 初期化 void -TextScreen::Init(int arg_col, int arg_row) +TextScreen::Init(int arg_col, int arg_row, ModeKind mode) { + assert(arg_col > 0); + assert(arg_row > 0); + col = arg_col; row = arg_row; + Mode = mode; - textbuf.reset(new uint16 [col * row]); + textbuf.resize(col * row); X = 0; Y = 0; @@ -48,63 +52,70 @@ TextScreen::Clear() textbuf[i] = 0x0020; } - // 折り返しモードならクリアでホームポジションへ - if (foldmode) { - Locate(0, 0); - } + // ホームポジション + Locate(0, 0); } -// 現在位置を移動 +// 1 行上スクロール void -TextScreen::Locate(int x, int y) +TextScreen::ScrollUp() { - X = x; - Y = y; + memmove(&textbuf[0], &textbuf[col], + (row - 1) * col * sizeof(textbuf[0])); + for (int i = 0; i < col; i++) { + textbuf[(row - 1) * col + i] = 0x0020; + } } + // 現在のカーソル位置に1文字出力する。 -// カーソルは一つ移動する。 -// foldmode = false なら、移動後右もしくは下にはみ出しても何もしない。 -// foldmode = true なら、右にはみ出した場合は次行左端に移動、 -// 下にはみ出した場合はカーソル位置はそのままで上に1行スクロールさせる。 void TextScreen::Putc(uint16 ch) { - if (X >= col || Y >= row) { + if (ch == '\n') { + CRLF(); return; } - if (ch == '\n') { - CRLF(); - } else { - textbuf[(Y * col) + X] = ch; - X++; - - // 折り返しモード - if (foldmode && X >= col) { - CRLF(); - } + if (OutOfScreen) { + return; } + + textbuf[(Y * col) + X] = ch; + Ascend(); +} + +// 現在のカーソル位置の文字を取得 +// カーソルは移動する。 +// カーソルが範囲外だったときは 0 を返す。 +uint16 +TextScreen::Getc() +{ + if (OutOfScreen) { + return 0; + } + + uint16 ch = textbuf[(Y * col) + X]; + Ascend(); + return ch; } // テキストスクリーンに文字列を出力する。 -// 右もしくは下にはみ出した場合は無視する。 -// XXX どっかで拾えてもいいような void TextScreen::Puts(const char *str) { while (*str != '\0') { - Putc(*str++); + uint16 ch = (uint8)*str++; + Putc(ch); } } // テキストスクリーンに文字列を出力する。 -// 右もしくは下にはみ出した場合は無視する。 void -TextScreen::Puts(uint attr, const char *str) +TextScreen::Puts(TA attr, const char *str) { while (*str != '\0') { - Putc(attr | *str++); + Putc(((uint)attr) | *str++); } } @@ -139,7 +150,7 @@ TextScreen::Print(int x, int y, const ch // テキストスクリーンに座標と属性を指定して、書式付き文字列を出力する。 void -TextScreen::Print(int x, int y, uint attr, const char *fmt, ...) +TextScreen::Print(int x, int y, TA attr, const char *fmt, ...) { char buf[1024]; va_list ap; @@ -152,18 +163,51 @@ TextScreen::Print(int x, int y, uint att Puts(attr, buf); } -// 改行 +// 現在位置の属性を取得 +// カーソルが画面外の場合は TA::Normal を返す。 +TA +TextScreen::GetAttr() const +{ + if (OutOfScreen) { + return TA::Normal; + } + + return TA(textbuf[(Y * col) + X] & 0xff00); +} + +// 現在位置の属性を attr に設定する。 +// カーソルは移動しない。 void -TextScreen::CRLF() +TextScreen::SetAttr(TA attr) { - X = 0; - Y++; + if (OutOfScreen) { + return; + } - if (Y >= row) { - // スクロール - memmove(&textbuf[0], &textbuf[col], - (col - 1) * row * sizeof(textbuf[0])); - // カーソル位置は最下行 - Y = row - 1; + auto& ch = textbuf[(Y * col) + X]; + ch = (ch & 0xff) | (uint)attr; +} + +// src の src_y 行目から rows 行をこのスクリーンの dst_y 行目以降にコピーする。 +// 双方のカーソルは移動しない。 +// もし row がどちらかではみ出た場合はコピーをそこで終了する。 +// src と dst (この screen) の桁数が違うと assert する。 +void +TextScreen::CopyRowsFrom(int dst_y, int rows, const TextScreen& src, int src_y) +{ + assert(GetCol() == src.GetCol()); + + const auto& srcbuf = src.GetBuf(); + + // rows を小さいほうに揃える。 + if (rows > GetRow() - dst_y) { + rows = GetRow() - dst_y; } + if (rows > src.GetRow() - src_y) { + rows = src.GetRow() - src_y; + } + + // 内部構造を知っているので一気にコピー。 + memcpy(&textbuf[dst_y * col], &srcbuf[src_y * col], + sizeof(textbuf[0]) * col * rows); }