Skip to content

Commit

Permalink
Merge pull request #54 from remance/cleanup-improve-menu-code-I-did
Browse files Browse the repository at this point in the history
Cleanup improve menu code i did
  • Loading branch information
remance committed May 4, 2023
2 parents d79f225 + e70dfd0 commit 99d412b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion engine/game/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def __init__(self, main_dir, error_log):
self.module = int(self.config["USER"]["module"])

self.module_list = csv_read(self.data_dir, "module_list.csv", ("module",)) # get module list
self.module_folder = str(self.module_list[self.module][0]).strip("/")
self.module_folder = str(self.module_list[self.module][0]).strip("/").lower()
Game.module_dir = os.path.join(self.data_dir, "module", self.module_folder)
Game.ui_font = csv_read(self.module_dir, "ui_font.csv", ("ui",), header_key=True)
for item in Game.ui_font: # add ttf file extension for font data reading.
Expand Down
50 changes: 35 additions & 15 deletions engine/uimenu/uimenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,26 @@ def change_state(self, text):
self.event = False


class BrownMenuButton(UIMenu):
class Container:

def get_rect(self):
raise NotImplementedError()


class Containable:

def get_relative_position_inside_container(self):
raise NotImplementedError()

def get_size(self):
raise NotImplementedError()

def get_adjusted_rect_to_be_inside_container(self, container):
pos = self.get_relative_position_inside_container()
return pygame.rect.Rect(*[container.get_rect()[i] - self.get_size()[i] // 2 + (pos[i] + 1) * container.get_rect()[i+2] // 2 for i in range(2)], *self.get_size())


class BrownMenuButton(UIMenu, Containable):

@classmethod
@lru_cache
Expand Down Expand Up @@ -382,6 +401,9 @@ def __init__(self, pos, text="", width=200, parent=None):
self.rect = self.button_normal_image.get_rect(center=self.pos)
self.event = False

def get_relative_position_inside_container(self):
return self.pos

def update(self, mouse_pos, mouse_up, mouse_down):
self.mouse_over = False
self.image = self.button_normal_image
Expand All @@ -391,10 +413,10 @@ def update(self, mouse_pos, mouse_up, mouse_down):
if mouse_up:
self.event = True

if self.parent is not None:
self.rect = pygame.rect.Rect(*[
self.parent.get_rect()[i] - self.image.get_size()[i] // 2 + (self.pos[i] + 1) * self.parent.get_rect()[
i + 2] * 0.5 for i in range(2)], *self.image.get_size())
self.rect = self.get_adjusted_rect_to_be_inside_container(self.parent)

def get_size(self):
return self.image.get_size()

def change_state(self, text):
pass
Expand Down Expand Up @@ -1113,25 +1135,23 @@ def pop(self, pos, text_input):
self.rect = self.image.get_rect(bottomleft=self.pos)


class BoxUI(UIMenu):
class BoxUI(UIMenu, Containable, Container):

def __init__(self, size, parent):
UIMenu.__init__(self)
self.parent = parent
self.size = size
self.calculate_rect()
self.image = pygame.Surface(size)
self.image.fill("#222a2e")
self._layer = -1 # NOTE: not sure if this is good since underscore indicate it is a private variable but it works for now
self.pos = (0, 0)
self.rect = self.get_adjusted_rect_to_be_inside_container(self.parent)

def get_relative_position_inside_container(self):
return (0, 0)

def get_rect(self):
return self.rect

def calculate_rect(self):
# TODO: atm the box is always in center. it should be able to have a position.
# make a common method to caclulate position if have parent. (the expression used in MenuButton is good)
x, y = [self.parent.get_size()[i] // 2 - (self.size[i] * 0.5) for i in range(2)]
self.rect = (x, y, *self.size)

def update(self, *args):
self.calculate_rect()
def get_size(self):
return self.image.get_size()

0 comments on commit 99d412b

Please sign in to comment.