<commit_before>#! /usr/bin/python

## \file  menu.py
#  \brief General Menu Class
#  \author <NAME>
#  \date 2009
#  \version 1.0.3
#
#  This is a menu class written for pygame/Python.  The menu is designed to work
#  with a program using a finite state machine (but it could also be easily
#  modified to have the 'buttons' return functions).  The menu 'buttons' contain
#  a 'state' (a state could really be anything you want) and this 'state' is
#  what is returned when the user selects/presses the button.  The program
#  controlling the menu can then act on this returned state as required.  This
#  helps to write non-blocking code.
#
#  The menu can have text buttons, image buttons (that get highlighted on all
#  sides to detect which is selected), or any combination of the two.
#
#  The menu is flexible and can be dynamically changed.  The 'buttons' will
#  auto-magically update themselves the next time they are drawn to the screen
#  (via the update method, which calls the draw method).  The draw method should
#  not be called itself.  'Buttons' can be added or removed at any time.
#
#  The menu can be positioned by the top left corner (a rectangle containing all
#  buttons is what gets moved).  It can be changed to center the entire menu
#  (i.e. center that containing rectangle) on that same position coordinate.  Or
#  the user can center the entire menu on the self.draw_surface.  Note that if
#  the pygame screen is given to the menu, then the entire window will be
#  available to be drawn to.  But if the user gives the menu another pygame
#  surface, then that surface itself will need to be blitted to the pygame
#  screen at some point.  Furthermore, the user can align the buttons to align
#  on the left, tobe centerd, or to align themselves on the right.  Also, they
#  can be aligned vertically on the top, center, or bottom.
#
#  The user can dynamically change the colors of the font/highlights, the
#  padding between buttons (left/right and top/bottom), the thickness of the
#  highlight around image buttons, and the orientation of the menu (if the
#  'buttons' will be stacked top to bottom ('vertical') or left to right
#  ('horizontal').
#
#  The best way to figure out the menu is to tinker around with it.  Run the
#  example programs, change attributes, and play with the menu.
#
#
#       Copyright 2009 <NAME>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA or see <http://www.gnu.org/licenses/>.
#
#
#  Changelog
#     V1.0.0 - Initial Release
#     V1.0.1 - Added get_current_image method
#     V1.0.2 - Fixed a bug in the set_font method (to update the rect of the
#              text buttons when the font is changed
#     V1.0.3 - Added self.refresh_whole_surface_on_load functionality
#


#-------------------------------------------------------------------------------
#---[ Imports ]-----------------------------------------------------------------
#-------------------------------------------------------------------------------
import pygame


#-------------------------------------------------------------------------------
#---[ Defines ]-----------------------------------------------------------------
#-------------------------------------------------------------------------------
## RGB color for Black
BLACK = (0, 0, 0)

## RGB color for White
WHITE = (255, 255, 255)

## RGB color for Red
RED = (255, 0, 0)

## RGB color for Green
GREEN = (0, 255, 0)

## RGB color for Blue
BLUE = (0, 0, 255)

## This is a user event that should be sent whenever the game state is changed
#  (at the main game loop level)
EVENT_CHANGE_STATE = pygame.USEREVENT + 1

#-------------------------------------------------------------------------------
#---[ cMenu Class ]-------------------------------------------------------------
#-------------------------------------------------------------------------------
## This class is used to display and control a menu
#
class cMenu:
   ## ---[ __init__ ]-----------------------------------------------------------
   #  @param   self        The class itself, Python standard
   #  @param   x           The x location to shift the buttons by when the
   #                       button is drawn to the surface in the update method
   #  @param   y           The y location to shift the buttons by when the
   #                       button is drawn to the surface in the update method
   #  @param   h_pad       Number the extra pixels to pad the buttons by (on the
   #                       left/right)
   #  @param   v_pad       Number the extra pixels to pad the buttons by (on the
   #                       top/bottom).
   #  @param   orientation Should be 'vertical' or 'horizontal'.  The buttons
   #                       will be put vertically or horizontally until 'number'
   #                       (the next argument) of buttons have been created at
   #                       which point it will start a new row or column
   #  @param   number      The number of buttons to put vertically or
   #                       horizontally before starting a new row or column
   #  @param   background  The background to use for the buttons (what will show
   #                       up behind the buttons).  This is often the
   #                       surface that they will be blitted to via the update
   #                       method
   #  @param   buttonList  This is a list of buttons to be added (though
   #                       more buttons can be added via another method).  The
   #                       elements of the list should be tuples of 3 parts as
   #                       shown: ('text', state, image) where text is the text
   #                       that will be shown for the button, state is what will
   #                       be returned when the button is pressed (enter is
   #                       hit), and image is None if the button is just going
   #                       to display text, or else is an image itself if the
   #                       button will be displayed as an image instead of text.
   #
   #  Initialize the class
   #
   def __init__(self, x, y, h_pad, v_pad, orientation, number, background,
                buttonList):
      ## menu items
      self.menu_items = []                      # List of menu items
      self.font = pygame.font.Font(None, 32)    # Font to use

      self.x = x                                # Top left corner (of surface)
      self.y = y                                # relative to the screen/window
      self.change_number = number               # See description above
      self.orientation = orientation            # See description above
      self.horizontal_padding = h_pad           # See description above
      self.vertical_padding = v_pad             # See description above

      self.selection = 0                        # The currently selected button
      self.u_color = WHITE                      # Color for unselected text
      self.s_color = RED                        # Color for selected text
      self.image_highlight_color = BLUE         # Color for the image highlights
      self.image_highlight_offset = 2           # Addition padding around image
                                                # buttons only for the highlight

      self.background = background.copy()       # The unedited background image<commit_msg>Do not try to load SysFont<commit_after>#! /usr/bin/python

## \file  menu.py
#  \brief General Menu Class
#  \author <NAME>
#  \date 2009
#  \version 1.0.3
#
#  This is a menu class written for pygame/Python.  The menu is designed to work
#  with a program using a finite state machine (but it could also be easily
#  modified to have the 'buttons' return functions).  The menu 'buttons' contain
#  a 'state' (a state could really be anything you want) and this 'state' is
#  what is returned when the user selects/presses the button.  The program
#  controlling the menu can then act on this returned state as required.  This
#  helps to write non-blocking code.
#
#  The menu can have text buttons, image buttons (that get highlighted on all
#  sides to detect which is selected), or any combination of the two.
#
#  The menu is flexible and can be dynamically changed.  The 'buttons' will
#  auto-magically update themselves the next time they are drawn to the screen
#  (via the update method, which calls the draw method).  The draw method should
#  not be called itself.  'Buttons' can be added or removed at any time.
#
#  The menu can be positioned by the top left corner (a rectangle containing all
#  buttons is what gets moved).  It can be changed to center the entire menu
#  (i.e. center that containing rectangle) on that same position coordinate.  Or
#  the user can center the entire menu on the self.draw_surface.  Note that if
#  the pygame screen is given to the menu, then the entire window will be
#  available to be drawn to.  But if the user gives the menu another pygame
#  surface, then that surface itself will need to be blitted to the pygame
#  screen at some point.  Furthermore, the user can align the buttons to align
#  on the left, tobe centerd, or to align themselves on the right.  Also, they
#  can be aligned vertically on the top, center, or bottom.
#
#  The user can dynamically change the colors of the font/highlights, the
#  padding between buttons (left/right and top/bottom), the thickness of the
#  highlight around image buttons, and the orientation of the menu (if the
#  'buttons' will be stacked top to bottom ('vertical') or left to right
#  ('horizontal').
#
#  The best way to figure out the menu is to tinker around with it.  Run the
#  example programs, change attributes, and play with the menu.
#
#
#       Copyright 2009 <NAME>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA or see <http://www.gnu.org/licenses/>.
#
#
#  Changelog
#     V1.0.0 - Initial Release
#     V1.0.1 - Added get_current_image method
#     V1.0.2 - Fixed a bug in the set_font method (to update the rect of the
#              text buttons when the font is changed
#     V1.0.3 - Added self.refresh_whole_surface_on_load functionality
#


#-------------------------------------------------------------------------------
#---[ Imports ]-----------------------------------------------------------------
#-------------------------------------------------------------------------------
import pygame
import fonts


#-------------------------------------------------------------------------------
#---[ Defines ]-----------------------------------------------------------------
#-------------------------------------------------------------------------------
## RGB color for Black
BLACK = (0, 0, 0)

## RGB color for White
WHITE = (255, 255, 255)

## RGB color for Red
RED = (255, 0, 0)

## RGB color for Green
GREEN = (0, 255, 0)

## RGB color for Blue
BLUE = (0, 0, 255)

## This is a user event that should be sent whenever the game state is changed
#  (at the main game loop level)
EVENT_CHANGE_STATE = pygame.USEREVENT + 1

#-------------------------------------------------------------------------------
#---[ cMenu Class ]-------------------------------------------------------------
#-------------------------------------------------------------------------------
## This class is used to display and control a menu
#
class cMenu:
   ## ---[ __init__ ]-----------------------------------------------------------
   #  @param   self        The class itself, Python standard
   #  @param   x           The x location to shift the buttons by when the
   #                       button is drawn to the surface in the update method
   #  @param   y           The y location to shift the buttons by when the
   #                       button is drawn to the surface in the update method
   #  @param   h_pad       Number the extra pixels to pad the buttons by (on the
   #                       left/right)
   #  @param   v_pad       Number the extra pixels to pad the buttons by (on the
   #                       top/bottom).
   #  @param   orientation Should be 'vertical' or 'horizontal'.  The buttons
   #                       will be put vertically or horizontally until 'number'
   #                       (the next argument) of buttons have been created at
   #                       which point it will start a new row or column
   #  @param   number      The number of buttons to put vertically or
   #                       horizontally before starting a new row or column
   #  @param   background  The background to use for the buttons (what will show
   #                       up behind the buttons).  This is often the
   #                       surface that they will be blitted to via the update
   #                       method
   #  @param   buttonList  This is a list of buttons to be added (though
   #                       more buttons can be added via another method).  The
   #                       elements of the list should be tuples of 3 parts as
   #                       shown: ('text', state, image) where text is the text
   #                       that will be shown for the button, state is what will
   #                       be returned when the button is pressed (enter is
   #                       hit), and image is None if the button is just going
   #                       to display text, or else is an image itself if the
   #                       button will be displayed as an image instead of text.
   #
   #  Initialize the class
   #
   def __init__(self, x, y, h_pad, v_pad, orientation, number, background,
                buttonList):
      ## menu items
      self.menu_items = []                      # List of menu items
      self.font = fonts.serif_normal            # Font to use

      self.x = x                                # Top left corner (of surface)
      self.y = y                                # relative to the screen/window
      self.change_number = number               # See description above
      self.orientation = orientation            # See description above
      self.horizontal_padding = h_pad           # See description above
      self.vertical_padding = v_pad             # See description above

      self.selection = 0                        # The currently selected button
      self.u_color = WHITE                      # Color for unselected text
      self.s_color = RED                        # Color for selected text
      self.image_highlight_color = BLUE         # Color for the image highlights
      self.image_highlight_offset = 2           # Addition padding around image
                                                # buttons only for the highlight

      self.background = background.copy()       # The unedited background image
{'fraction_non_alphanumeric': 0.14350994195381006, 'fraction_numerical': 0.00827466963072743, 'mean_word_length': 1.6036977491961415, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2378342', 'n_tokens_mistral': 3938, 'n_tokens_neox': 3739, 'n_words': 1935}