diff options
author | Joe Anderson <jandew+dev@gmail.com> | 2016-02-23 10:33:34 -0800 |
---|---|---|
committer | Joe Anderson <jandew+dev@gmail.com> | 2016-02-23 10:33:34 -0800 |
commit | 2b8ae2b1e120f7ee2b65f901b29d458c792840b8 (patch) | |
tree | 10c030a61c0ea7555e4867f9dab0ab6726b3834f /oo.py | |
parent | 131f3e9380ad5ac9261e3c4a578e9d71c5a84477 (diff) | |
download | oo-2b8ae2b1e120f7ee2b65f901b29d458c792840b8.tar.gz oo-2b8ae2b1e120f7ee2b65f901b29d458c792840b8.zip |
merge input functions into ooPlay.get_input
Diffstat (limited to 'oo.py')
-rw-r--r-- | oo.py | 87 |
1 files changed, 45 insertions, 42 deletions
@@ -559,13 +559,30 @@ class ooPlay: self.display_subroutine(x, y) self.screen.refresh() - def get_input(self, options): + pause_length = 80 + def get_input(self, options, delay=-1): """Waits for input to match an option, returns matched option. options must be an iterable of strings and ints. - Each character in each string and each int are checked against. - If the input matches any character in a string, - then the character is returned instead of the ordinal. + + Each character in each string and each int are checked against. + + If the input matches any character in a string, + then the character is returned instead of the ordinal. + + delay is in units of self.pause_length, + the standard delay per char for the class. + + delay may be an integer or float. + + If delay is negative, then input is blocking. + + If nonnegative, then input is non-blocking, + but keeps waiting if the input is not in options. + + Implementation is such that keypresses other than + those in options can shorten or lengthen the pause, + but the pause is capped at twice the intended delay. """ options = list(options) chars = [] @@ -576,43 +593,27 @@ class ooPlay: if type(option) is int: ords.append(option) breaks = list(map(ord, chars)) + ords - while True: - inp = self.screen.getch() - if inp in breaks: - break + + if delay == -1: + while True: + inp = self.screen.getch() + if inp in breaks: + break + else: + breaks.append(-1) + pause = int(self.pause_length * delay) + self.screen.timeout(pause) + while True: + inp = self.screen.getch() + if inp in breaks: break + pause //= 2 + self.screen.timeout(pause) + self.screen.timeout(-1) + if inp in map(ord, chars): return chr(inp) return inp - pause_length = 80 - def sleep(self, delay=1): - """Like most sleep functions, but can be escaped with spacebar. - - delay is in units of self.pause_length, - the standard delay per char for the class. - delay may be a positive integer or float. - - Implementation is such that keypresses other than spacebar - can shorten or lengthen the pause, - but the pause is capped at twice the intended delay. - - Returns True if spacebar was pressed. - """ - pause = int(self.pause_length * delay) - self.screen.timeout(pause) - while True: - inp = self.screen.getch() - if inp == -1: - space_pressed = False - break - if inp == ord(" "): - space_pressed = True - break - pause //= 2 - self.screen.timeout(pause) - self.screen.timeout(-1) - return space_pressed - def write(self, string=None, pause=None): """Write string to the bottom line. @@ -646,7 +647,8 @@ class ooPlay: centered_string = string.center(width, " ") self.screen.addstr(self.Y - 1, 0, centered_string, color) self.screen.refresh() - if pause: self.sleep(2*len(string)) + if pause: + if -1 != self.get_input(" ", delay=2*len(string)): return return # scrolling through a wider string @@ -654,12 +656,13 @@ class ooPlay: strings = [string[i:i + width] for i in range(len(string) - width + 1)] self.screen.addstr(self.Y - 1, 0, strings[0], color) self.screen.refresh() - if self.sleep(width): return + if -1 != self.get_input(" ", delay=width): return for s in strings: self.screen.addstr(self.Y - 1, 0, s, color) self.screen.refresh() - if self.sleep(): return - if pause: self.sleep(width) + if -1 != self.get_input(" ", delay=1): return + if pause: + if -1 != self.get_input(" ", delay=width): return def write_menu(self, string, controls=[]): """Writes string, waits for acceptable input, returns upper input. @@ -727,7 +730,7 @@ class ooPlay: """Write the help menu.""" self.write("use spacebar to skip or repeat a message") self.write("use the capital letter to make a selection") - self.write("use up to return to a previous menu") + self.write("use the up key to return to a previous menu") self.write("use return to exit the help menu") help_menu = \ ("Controls or Gameplay?", |