uart: only show cursor in RX terminal when it gets focus

This commit is contained in:
Zihao Yu 2024-01-09 03:01:00 +08:00
parent a83a8819c8
commit 211469f27b
6 changed files with 30 additions and 4 deletions

View File

@ -8,5 +8,8 @@
1. 按下按钮可将8~12号中对应的LED亮灭效果取反
1. 8个数码管流水显示数字0-7
1. 窗口左下角为VGA输出, 将会展示一张图片
1. 窗口右侧中部为UART RX输入, 鼠标选中后可输入字符串, 按回车后字符串将通过UART RX端输入, 通过回环连接从UART TX端输出, 显示在右上方终端
1. 敲击键盘, 终端将会输出按键的扫描码
1. 窗口右侧中部为UART RX输入
* 鼠标选中后出现光标, 此时可输入字符串
* 在选中状态下按回车后, 字符串将通过UART RX端输入, 通过回环连接从UART TX端输出, 显示在右上方终端
* 鼠标点击其他位置, 可取消选中RX输入窗口, 后续输入将被键盘捕捉

View File

@ -8,6 +8,7 @@ private:
int w_in_char, h_in_char;
std::vector<uint8_t *> lines;
int cursor_x, cursor_y; // cursor_y start with all history when scrolling is supported
bool is_cursor_visible;
SDL_Texture *cursor_texture;
int screen_y;
bool dirty_screen;
@ -28,6 +29,7 @@ public:
void feed_ch(uint8_t ch);
void feed_str(const char *s);
void clear();
void set_cursor_visibility(bool v);
void update_gui();
};

View File

@ -25,7 +25,8 @@ public:
virtual void update_state();
void tx_receive();
void rx_send();
void rx_getchar(uint8_t);
void rx_getchar(uint8_t ch);
void rx_term_focus(bool v);
};
#endif

View File

@ -3,6 +3,7 @@
extern std::vector<Component *> components;
void uart_rx_getchar(uint8_t ch);
void uart_rx_term_focus(bool v);
static bool focus_uart_rx_term = false;
static void mousedown_handler(const SDL_Event &ev) {
@ -22,6 +23,7 @@ static void mousedown_handler(const SDL_Event &ev) {
if (click_uart_rx_term) SDL_StartTextInput();
else SDL_StopTextInput();
focus_uart_rx_term = click_uart_rx_term;
uart_rx_term_focus(focus_uart_rx_term);
}
}

View File

@ -10,6 +10,7 @@ Term::Term(SDL_Renderer *r, int x, int y, int w, int h):
h_in_char = region.h / 16;
uint8_t *l = add_line();
cursor_texture = new_texture(r, 10, 16, 0x10, 0x10, 0x10);
is_cursor_visible = true;
clear_screen();
dirty_line = new bool[h_in_char];
dirty_char = new bool[w_in_char * h_in_char];
@ -30,6 +31,12 @@ void Term::clear_screen() {
SDL_RenderFillRect(renderer, &region);
}
void Term::set_cursor_visibility(bool v) {
bool update_cursor_gui = is_cursor_visible ^ v;
is_cursor_visible = v;
if (update_cursor_gui) draw_cursor();
}
void Term::clear() {
while (lines.size() > 1) {
delete [] lines.back();
@ -96,7 +103,9 @@ void Term::draw_cursor() {
rect.w = 10, rect.h = 16;
rect.y += 16 * y;
rect.x += 10 * x;
SDL_RenderCopy(renderer, cursor_texture, NULL, &rect);
SDL_Texture *t = is_cursor_visible ? cursor_texture : get_font_texture(' ');
SDL_RenderCopy(renderer, t, NULL, &rect);
set_redraw();
}
}
@ -119,9 +128,9 @@ void Term::update_gui() {
rect.x = region.x + rect.w * x;
SDL_Texture *t = get_font_texture(ch);
SDL_RenderCopy(renderer, t, NULL, &rect);
set_redraw();
}
}
draw_cursor();
set_redraw();
init_dirty(false);
}

View File

@ -24,6 +24,7 @@ UART::UART(SDL_Renderer *rend, int cnt, int init_val, int ct, int x, int y, int
p_tx = (uint8_t *)pin_array[UART_TX].ptr;
rx_term->feed_str(rx_input_prompt);
rx_term->set_cursor_visibility(false);
rx_input = "";
rx_sending_str = "";
rx_update_gui = true;
@ -110,6 +111,10 @@ void UART::set_divisor(uint16_t d) {
divisor = d;
}
void UART::rx_term_focus(bool v) {
rx_term->set_cursor_visibility(v);
}
void init_uart(SDL_Renderer *renderer) {
int x = WINDOW_WIDTH / 2, y = 0, w = WINDOW_WIDTH / 2, h = WINDOW_HEIGHT / 2;
uart = new UART(renderer, 1, 0, UART_TYPE, x, y, w, h);
@ -121,3 +126,7 @@ void init_uart(SDL_Renderer *renderer) {
void uart_rx_getchar(uint8_t ch) {
uart->rx_getchar(ch);
}
void uart_rx_term_focus(bool v) {
uart->rx_term_focus(v);
}