Skip to content

Commit a965527

Browse files
committed
refactor: delegate input processing responsibility to typing_core
- Add high-level input processing methods to TypingCore: * process_character_input() - handles character input with result * process_enter_input() - handles Enter key with line-end logic * process_tab_input() - handles Tab key input - Add InputResult enum to communicate typing results clearly - Replace low-level typing_screen input handling with typing_core delegation - Simplify typing_screen key handlers to use new high-level API - Remove process_keystroke() in favor of handle_input_result() - Centralize all typing logic decisions in typing_core layer
1 parent 1ed5560 commit a965527

File tree

2 files changed

+83
-42
lines changed

2 files changed

+83
-42
lines changed

src/game/screens/typing_screen.rs

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{
22
super::{
33
stage_renderer::StageRenderer,
4-
typing_core::{ProcessingOptions, TypingCore},
4+
typing_core::{InputResult, ProcessingOptions, TypingCore},
55
},
66
CountdownScreen,
77
};
@@ -218,58 +218,39 @@ impl TypingScreen {
218218
}
219219

220220
fn handle_tab_key(&mut self) -> Result<SessionState> {
221-
if !self.typing_core.can_accept_input() {
222-
return Ok(SessionState::Continue);
223-
}
224-
225-
let is_correct = self.typing_core.check_character_match('\t');
226-
self.process_keystroke('\t', is_correct)
221+
let result = self.typing_core.process_tab_input();
222+
self.scoring_engine.record_keystroke('\t', self.typing_core.current_position_to_type());
223+
self.handle_input_result(result)
227224
}
228225

229226
fn handle_enter_key(&mut self) -> Result<SessionState> {
230-
if !self.typing_core.can_accept_input() {
231-
return Ok(SessionState::Continue);
232-
}
233-
234-
let is_correct = self.typing_core.is_at_line_end_for_enter();
227+
let result = self.typing_core.process_enter_input();
235228
self.scoring_engine.record_keystroke('\n', self.typing_core.current_position_to_type());
236-
237-
if is_correct {
238-
self.current_mistake_position = None;
239-
self.typing_core.handle_newline_advance();
240-
if self.typing_core.is_completed() {
241-
return Ok(SessionState::Complete);
242-
}
243-
} else {
244-
self.record_mistake();
245-
}
246-
247-
Ok(SessionState::Continue)
229+
self.handle_input_result(result)
248230
}
249231

250232
fn handle_character_input(&mut self, ch: char) -> Result<SessionState> {
251-
if !self.typing_core.can_accept_input() {
252-
return Ok(SessionState::Continue);
253-
}
254-
255-
let is_correct = self.typing_core.check_character_match(ch);
256-
self.process_keystroke(ch, is_correct)
257-
}
258-
259-
fn process_keystroke(&mut self, ch: char, is_correct: bool) -> Result<SessionState> {
233+
let result = self.typing_core.process_character_input(ch);
260234
self.scoring_engine.record_keystroke(ch, self.typing_core.current_position_to_type());
235+
self.handle_input_result(result)
236+
}
261237

262-
if is_correct {
263-
self.current_mistake_position = None;
264-
self.typing_core.advance_to_next_character();
265-
if self.typing_core.is_completed() {
266-
return Ok(SessionState::Complete);
238+
fn handle_input_result(&mut self, result: InputResult) -> Result<SessionState> {
239+
match result {
240+
InputResult::Correct => {
241+
self.current_mistake_position = None;
242+
Ok(SessionState::Continue)
267243
}
268-
} else {
269-
self.record_mistake();
244+
InputResult::Incorrect => {
245+
self.record_mistake();
246+
Ok(SessionState::Continue)
247+
}
248+
InputResult::Completed => {
249+
self.current_mistake_position = None;
250+
Ok(SessionState::Complete)
251+
}
252+
InputResult::NoAction => Ok(SessionState::Continue),
270253
}
271-
272-
Ok(SessionState::Continue)
273254
}
274255

275256
fn record_mistake(&mut self) {

src/game/typing_core.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,64 @@ impl TypingCore {
427427
}
428428
}
429429
}
430+
431+
// High-level input processing methods
432+
pub fn process_character_input(&mut self, input_char: char) -> InputResult {
433+
if !self.can_accept_input() {
434+
return InputResult::NoAction;
435+
}
436+
437+
if self.check_character_match(input_char) {
438+
self.advance_to_next_character();
439+
if self.is_completed() {
440+
InputResult::Completed
441+
} else {
442+
InputResult::Correct
443+
}
444+
} else {
445+
InputResult::Incorrect
446+
}
447+
}
448+
449+
pub fn process_enter_input(&mut self) -> InputResult {
450+
if !self.can_accept_input() {
451+
return InputResult::NoAction;
452+
}
453+
454+
if self.is_at_line_end_for_enter() {
455+
self.handle_newline_advance();
456+
if self.is_completed() {
457+
InputResult::Completed
458+
} else {
459+
InputResult::Correct
460+
}
461+
} else {
462+
InputResult::Incorrect
463+
}
464+
}
465+
466+
pub fn process_tab_input(&mut self) -> InputResult {
467+
if !self.can_accept_input() {
468+
return InputResult::NoAction;
469+
}
470+
471+
if self.check_character_match('\t') {
472+
self.advance_to_next_character();
473+
if self.is_completed() {
474+
InputResult::Completed
475+
} else {
476+
InputResult::Correct
477+
}
478+
} else {
479+
InputResult::Incorrect
480+
}
481+
}
482+
}
483+
484+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
485+
pub enum InputResult {
486+
Correct, // Input was correct, continue
487+
Incorrect, // Input was incorrect (mistake)
488+
Completed, // Input was correct and typing is complete
489+
NoAction, // No input accepted (already completed)
430490
}

0 commit comments

Comments
 (0)