Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve list UI #60

Merged
merged 5 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions engine/game/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,41 @@ def __init__(self, main_dir, error_log):
self.lore_button, self.option_button, self.quit_button, main_menu_buttons_box)

# Battle map select menu button

# NOTE: not used anymore? if so, remove
class CustomBattleListAdapter:
def __init__(self, _list, _self):
self.list = _list
self.last_index = -1
self._self = _self

def __len__(self):
return len(self.list)

def __getitem__(self, item):
if item > len(self.list)-1: return None
return self.list[item]

def on_select(self, item_index, item_text):
_self = self._self
self.last_index = item_index
_self.current_map_select = item_index
_self.map_selected = _self.battle_map_folder[_self.current_map_select]
_self.create_preview_map()
print("test {0} {1}".format(item_index, item_text))

def get_highlighted_index(self):
return self.last_index

self.preset_map_list_box = uimenu.ListUI(pivot=(-0.9, -0.9), origin=(-1, -1), size=(.2, .8),
items=uimenu.ListAdapter(self.battle_map_list, self),
parent=self.screen, item_size=20) # TODO change to preset map list

# NOTE: not used anymore? if so, remove
self.custom_map_list_box = uimenu.ListUI(pivot=(-0.9, -0.9), origin=(-1, -1), size=(.2, .8),
items=CustomBattleListAdapter(self.battle_map_list, self),
parent=self.screen, item_size=40)

self.custom_battle_map_list_box = uimenu.ListUI(pivot=(-0.9, -0.9), origin=(-1, -1), size=(.2, .8),
items=uimenu.ListAdapter(self.battle_map_list, self, replace_on_select=custom_map_list_on_select),
parent=self.screen, item_size=20)
Expand Down
44 changes: 33 additions & 11 deletions engine/uimenu/uimenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,8 @@ def __init__(self, origin, pivot, size, items, parent, item_size, layer=0):
self.pivot = pivot
self.origin = origin
self.parent = parent
self.frame = load_image(game.module_dir, (1, 1), "list_frame.png", ("ui", "mainmenu_ui"))
frame_file = "new_button.png" # "list_frame.png" # using the button frame to test if it looks good
self.frame = load_image(game.module_dir, (1, 1), frame_file, ("ui", "mainmenu_ui"))
self.scroll_box_frame = load_image(game.module_dir, (1, 1), "scroll_box_frame.png", ("ui", "mainmenu_ui"))

self.item_size = item_size
Expand All @@ -1221,7 +1222,13 @@ def __init__(self, origin, pivot, size, items, parent, item_size, layer=0):

self.scroll_bar_height = self.rect[3] - 12
self.scroll_box_height = int(self.scroll_bar_height * (item_size / len(self.items)))
self.scroll_step_height = (self.scroll_bar_height - self.scroll_box_height) / self.get_number_of_items_outside_visible_list()
divider = self.get_number_of_items_outside_visible_list()

if divider is not None:
self.has_scroll = True
self.scroll_step_height = (self.scroll_bar_height - self.scroll_box_height) / divider
else:
self.has_scroll = False

self.scroll_box = make_image_by_frame(self.scroll_box_frame, (14, self.scroll_box_height))
self.scroll_box_index = 0
Expand All @@ -1230,7 +1237,7 @@ def __init__(self, origin, pivot, size, items, parent, item_size, layer=0):
def get_number_of_items_outside_visible_list(self):
r = len(self.items) - self.item_size
if r <= 0:
raise Exception("At the moment ListUI can only handle list that have enough items to require a scroll bar")
return None
return r

def get_item_height(self):
Expand All @@ -1256,25 +1263,28 @@ def get_refreshed_image(self):
if item_index == self.items.get_highlighted_index():
color = "#551617"

pygame.draw.rect(self.image, color, (6, 6 + i * item_height, size[0] - 25, item_height))
self.image.blit(font.render(self.items[item_index], True, (255 if item_index == self.selected_index else 178,) * 3),
pygame.draw.rect(self.image, color, (6, 6 + i * item_height, size[0] - 13*self.has_scroll-12, item_height))
self.image.blit(font.render(self.items[item_index], True, (255 if item_index == self.selected_index else 0,) * 3),
(20, item_height // 2 + 6 - 9 + i * item_height))

# draw scroll bar
pygame.draw.rect(self.image, "black", self.get_scroll_bar_rect())
if scroll_bar_rect := self.get_scroll_bar_rect():
pygame.draw.rect(self.image, "black", scroll_bar_rect)

# draw scroll box
scroll_box_rect = self.get_scroll_box_rect()
self.image.blit(self.scroll_box, scroll_box_rect)
if self.in_scroll_box or self.hold_scroll_box is not None:
pygame.draw.rect(self.image, (220, 190, 110) if self.hold_scroll_box is not None else (150,) * 3, scroll_box_rect, 1)
if scroll_box_rect := self.get_scroll_box_rect():
self.image.blit(self.scroll_box, scroll_box_rect)
if self.in_scroll_box or self.hold_scroll_box is not None:
pygame.draw.rect(self.image, (220, 190, 110) if self.hold_scroll_box is not None else (150,) * 3, scroll_box_rect, 1)

return self.image

def get_scroll_bar_rect(self):
if not self.has_scroll: return None
return pygame.Rect(self.rect[2] - 18, 6, 14, self.scroll_bar_height)

def get_scroll_box_rect(self):
if not self.has_scroll: return None
return pygame.Rect(self.rect[2] - 18, self.scroll_box_index * self.scroll_step_height + 6, *self.scroll_box.get_size())

def update(self):
Expand All @@ -1296,7 +1306,13 @@ def update(self):
self.in_scroll_box = False
in_list = False
if self.rect.collidepoint(mouse_pos):
in_list = True
self.mouse_over = True
if scroll_bar_rect := self.get_scroll_bar_rect():
if scroll_bar_rect.collidepoint(relative_mouse_pos):
if self.get_scroll_box_rect().collidepoint(relative_mouse_pos):
self.in_scroll_box = True
in_list = False

# Check for scrolling button
noiovl = self.get_number_of_items_outside_visible_list()
Expand All @@ -1309,7 +1325,7 @@ def update(self):
if self.scroll_box_index < 0:
self.scroll_box_index = 0

if self.get_scroll_bar_rect().collidepoint(relative_mouse_pos):
if self.has_scroll and self.get_scroll_bar_rect().collidepoint(relative_mouse_pos):
if self.get_scroll_box_rect().collidepoint(relative_mouse_pos):
self.in_scroll_box = True
else:
Expand Down Expand Up @@ -1337,6 +1353,12 @@ def update(self):
relative_index = ((relative_mouse_pos[1] - 6) // item_height)
if relative_index >= 0 and relative_index < self.item_size:
self.selected_index = relative_index + self.scroll_box_index


if self.selected_index is not None:
if self.selected_index >= len(self.items): self.selected_index = None

#if in_list and mljd and self.selected_index is not None:
if in_list and (mlju or mrju) and self.selected_index is not None:
self.items.on_select(self.selected_index, self.items[self.selected_index])

Expand Down