Skip to content

Commit

Permalink
refactor touch index calculation
Browse files Browse the repository at this point in the history
slightly expand prompt touch regions
  • Loading branch information
odudex committed Oct 25, 2024
1 parent 2fbe698 commit 9ab895d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 27 deletions.
19 changes: 9 additions & 10 deletions src/krux/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,13 @@ def prompt(self, text, offset_y=0):
if MINIMAL_DISPLAY:
return self.ctx.input.wait_for_button() == BUTTON_ENTER
offset_y += (len(self.ctx.display.to_lines(text)) + 1) * FONT_HEIGHT
self.x_keypad_map.append(DEFAULT_PADDING)
self.x_keypad_map.append(0)
self.x_keypad_map.append(self.ctx.display.width() // 2)
self.x_keypad_map.append(self.ctx.display.width() - DEFAULT_PADDING)
self.x_keypad_map.append(self.ctx.display.width())
y_key_map = offset_y - (3 * FONT_HEIGHT // 2)
self.y_keypad_map.append(y_key_map)
y_key_map += 4 * FONT_HEIGHT
y_key_map = min(y_key_map, self.ctx.display.height())
self.y_keypad_map.append(y_key_map)
if self.ctx.input.touch is not None:
self.ctx.input.touch.clear_regions()
Expand Down Expand Up @@ -389,14 +390,12 @@ def prompt(self, text, offset_y=0):
theme.no_esc_color,
)
elif self.ctx.input.touch is not None:
for region in self.x_keypad_map:
self.ctx.display.draw_line(
region,
self.y_keypad_map[0] + FONT_HEIGHT,
region,
self.y_keypad_map[0] + 3 * FONT_HEIGHT,
theme.frame_color,
)
self.ctx.display.draw_vline(
self.ctx.display.width() // 2,
self.y_keypad_map[0] + FONT_HEIGHT,
2 * FONT_HEIGHT,
theme.frame_color,
)
btn = self.ctx.input.wait_for_button()
if btn in (BUTTON_PAGE, BUTTON_PAGE_PREV):
answer = not answer
Expand Down
76 changes: 59 additions & 17 deletions src/krux/touch.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,66 @@ def valid_position(self, data):
return False
return True

# def highlight_region(self, x_index, y_index):
# """Outlines the region of the current index"""
# import lcd
# from .themes import theme

# # Draw outline delimiting the region
# if y_index >= 0 and x_index >= 0:
# y_start = self.y_regions[y_index] if y_index < len(self.y_regions) else 0
# y_start += 1
# y_end = (
# self.y_regions[y_index + 1]
# if y_index + 1 < len(self.y_regions)
# else self.height
# )
# y_end -= 1
# x_start = self.x_regions[x_index] if x_index < len(self.x_regions) else 0
# x_start += 1
# x_end = (
# self.x_regions[x_index + 1]
# if x_index + 1 < len(self.x_regions)
# else self.height
# )
# x_end -= 1

# lcd.draw_outline(
# x_start,
# y_start,
# x_end - x_start,
# y_end - y_start,
# theme.fg_color,
# )

def _extract_index(self, data):
"""Gets an index from touched points, x and y delimiters"""
index = 0
if self.y_regions:
for region in self.y_regions:
if data[1] > region:
index += 1
index -= 1
if self.x_regions: # if 2D array
index *= len(self.x_regions) - 1
x_index = 0
for x_region in self.x_regions:
if data[0] > x_region:
x_index += 1
x_index -= 1
index += x_index
return index
return 0
"""
Gets an index from touched points, x and y delimiters.
The index is calculated based on the position of the touch within the defined regions.
"""
y_index = 0
x_index = 0

# Calculate y index
for region in self.y_regions:
if data[1] > region:
y_index += 1
y_index -= 1 if y_index > 0 else 0

# Calculate x index if x regions are defined (2D array)
if self.x_regions:
for x_region in self.x_regions:
if data[0] > x_region:
x_index += 1
x_index -= 1 # Adjust index to be zero-based
# Combine y and x indices to get the final index
index = y_index * (len(self.x_regions) - 1) + x_index
else:
index = y_index

# self.highlight_region(x_index, y_index)

return index

def _store_points(self, data):
"""Store pressed points and calculare an average pressed point"""
Expand Down

0 comments on commit 9ab895d

Please sign in to comment.