Skip to content
Snippets Groups Projects
Commit 55de8a88 authored by Stephen D's avatar Stephen D
Browse files

numbers-only text box for ssid field

parent aa0b830f
No related branches found
No related tags found
No related merge requests found
...@@ -31,7 +31,7 @@ impl Chat { ...@@ -31,7 +31,7 @@ impl Chat {
Self { Self {
callsign, callsign,
ssid, ssid,
textbox: MyTextBox::new(1, i32::try_from(HEIGHT).unwrap() - 24, 31, 1, 4), textbox: MyTextBox::new(1, i32::try_from(HEIGHT).unwrap() - 24, 31, 1, 4, false),
} }
} }
......
...@@ -39,6 +39,7 @@ impl ContactView { ...@@ -39,6 +39,7 @@ impl ContactView {
MAX_CONTACT_NAME_LENGTH.try_into().unwrap(), MAX_CONTACT_NAME_LENGTH.try_into().unwrap(),
1, 1,
1, 1,
false,
); );
name.set_text(&contact.name).unwrap(); name.set_text(&contact.name).unwrap();
name.selected = false; name.selected = false;
...@@ -49,6 +50,7 @@ impl ContactView { ...@@ -49,6 +50,7 @@ impl ContactView {
MAX_CONTACT_CALLSIGN_LENGTH.try_into().unwrap(), MAX_CONTACT_CALLSIGN_LENGTH.try_into().unwrap(),
1, 1,
1, 1,
false,
); );
callsign.set_text(&contact.callsign).unwrap(); callsign.set_text(&contact.callsign).unwrap();
callsign.selected = false; callsign.selected = false;
...@@ -59,6 +61,7 @@ impl ContactView { ...@@ -59,6 +61,7 @@ impl ContactView {
3, 3,
1, 1,
1, 1,
true,
); );
let mut ssid_buf: String<3> = String::new(); let mut ssid_buf: String<3> = String::new();
write!(ssid_buf, "{}", contact.ssid).unwrap(); write!(ssid_buf, "{}", contact.ssid).unwrap();
......
...@@ -40,6 +40,8 @@ pub struct TextBox { ...@@ -40,6 +40,8 @@ pub struct TextBox {
x: i32, x: i32,
y: i32, y: i32,
number_only: bool,
text: String<MAX_TEXT_LENGTH>, text: String<MAX_TEXT_LENGTH>,
pub selected: bool, pub selected: bool,
...@@ -47,7 +49,14 @@ pub struct TextBox { ...@@ -47,7 +49,14 @@ pub struct TextBox {
} }
impl TextBox { impl TextBox {
pub fn new(x: i32, y: i32, char_width: u32, char_height: u32, max_height: u32) -> Self { pub fn new(
x: i32,
y: i32,
char_width: u32,
char_height: u32,
max_height: u32,
number_only: bool,
) -> Self {
Self { Self {
max_height, max_height,
char_width, char_width,
...@@ -57,6 +66,7 @@ impl TextBox { ...@@ -57,6 +66,7 @@ impl TextBox {
cursor_i: 0, cursor_i: 0,
cur_line_len: 0, cur_line_len: 0,
num_lines: 1, num_lines: 1,
number_only,
x, x,
y, y,
...@@ -101,6 +111,8 @@ impl TextBox { ...@@ -101,6 +111,8 @@ impl TextBox {
self.cursor_x = 0; self.cursor_x = 0;
self.cursor_y = 0; self.cursor_y = 0;
self.cursor_i = 0; self.cursor_i = 0;
self.num_lines = 1;
self.cur_line_len = 0;
other other
} }
...@@ -260,12 +272,6 @@ impl Element for TextBox { ...@@ -260,12 +272,6 @@ impl Element for TextBox {
self.cursor_i -= 1; self.cursor_i -= 1;
} }
} }
KeyCode::Char(c) => {
if self.insert_char(c) {
self.cursor_inc_char();
self.cursor_i += 1;
}
}
KeyCode::Enter => { KeyCode::Enter => {
let _ = self.text.push('\n'); let _ = self.text.push('\n');
self.cursor_x = 0; self.cursor_x = 0;
...@@ -274,7 +280,20 @@ impl Element for TextBox { ...@@ -274,7 +280,20 @@ impl Element for TextBox {
self.cursor_i += 1; self.cursor_i += 1;
self.cur_line_len = 0; self.cur_line_len = 0;
} }
_ => {} c => {
let c = if self.number_only {
c.as_number()
} else {
c.as_char()
};
if let Some(c) = c {
if self.insert_char(c) {
self.cursor_inc_char();
self.cursor_i += 1;
}
}
}
} }
} }
......
...@@ -59,6 +59,34 @@ pub enum KeyCode { ...@@ -59,6 +59,34 @@ pub enum KeyCode {
} }
impl KeyCode { impl KeyCode {
pub fn as_char(&self) -> Option<char> {
if let KeyCode::Char(c) = self {
Some(*c)
} else {
None
}
}
pub fn as_number(&self) -> Option<char> {
let c = self.as_char()?;
let c = match c.to_ascii_lowercase() {
'0'..='9' => c,
'w' => '1',
'e' => '2',
'r' => '3',
's' => '4',
'd' => '5',
'f' => '6',
'z' => '7',
'x' => '8',
'c' => '9',
_ => return None,
};
Some(c)
}
fn from_keyboard(c: char, alt: bool, shift: bool) -> Self { fn from_keyboard(c: char, alt: bool, shift: bool) -> Self {
match (alt, shift, c) { match (alt, shift, c) {
(_, _, CALL_END) => KeyCode::CallEnd, (_, _, CALL_END) => KeyCode::CallEnd,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment