PyxelMenu

Pyxel class for generating, displaying and controlling a menu

Source code in pyxel_menu.py
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
class PyxelMenu:
    """ Pyxel class for generating, displaying and controlling a menu """

    def __init__(self, x: int, y: int, options: list = None, limit: int = 5):
        """Class constructor

        Args:
            x (int): Position of the menu with respect to the left margin in pixels
            y (int): Position of the menu with respect to the up margin in pixels
            options (list): A list with the options to add. Defaults to None
            limit (int, optional): The limit of options to display. Defaults to 5.
        """
        self._limit = limit
        self._x = x
        self._y = y
        self._current_pos = 0
        self._cursor = {
            'type': 'circle',
            'color': 7
        }
        self._color = 7
        self._cursor_color = 7
        self._cursor_img = {
            'use': False,
            'img': 0,
            'u': 0,
            'v': 0,
            'colkey': None
        }
        self._highlight = {
            'use': False,
            'color': 7
        }
        self._options = options

    def draw(self):
        ''' Draw the menu '''
        if not self._options:
            return

        starty = self._y

        init = 0
        cursor_pos = self._current_pos

        if self._current_pos < len(self._options):
            if (
                self._current_pos > pyxel.floor(self._limit / 2) and 
                self._current_pos < len(self._options) - pyxel.floor(self._limit / 2)
            ):
                init = self._current_pos - pyxel.floor(self._limit / 2)
                cursor_pos = pyxel.floor(self._limit / 2)

            elif (
                self._current_pos >= len(self._options) - pyxel.floor(self._limit / 2)
            ):
                init = len(self._options) - self._limit
                cursor_pos = self._current_pos - init

        for i, option in enumerate(self._options[init:init + self._limit]):
            text_color = self._color

            if i == cursor_pos:
                if self._cursor_img['use']:
                    pyxel.blt(
                        self._x,
                        starty,
                        self._cursor_img['img'],
                        self._cursor_img['u'],
                        self._cursor_img['v'],
                        8,
                        8,
                        self._cursor_img['colkey']
                    )
                else:
                    if self._cursor['type'] == 'circle':
                        pyxel.circ(self._x + 5, starty + 2, 2, self._cursor['color'])
                    elif self._cursor['type'] == 'triangle':
                        pyxel.tri(self._x + 2, starty, self._x + 2, starty + 4, self._x + 6, starty + 2, self._cursor['color'])
                    elif self._cursor['type'] == 'square':
                        pyxel.rect(self._x + 2, starty, 5, 5, self._cursor['color'])

                if self._highlight['use']:
                    text_color = self._highlight['color']

            pyxel.text(self._x + 10, starty, option, text_color)
            starty += 8

    def get_current_pos(self):
        ''' Return selected option index '''
        return self._current_pos

    def get_current_text(self):
        ''' Return selected option text '''
        return self._options[self._current_pos]

    def move_up(self):
        ''' Move the cursor up one position '''
        if self._current_pos > 0:
            self._current_pos -= 1

    def move_down(self):
        ''' Move the cursor down one position '''
        if self._current_pos < len(self._options) - 1:
            self._current_pos += 1

    def set_cursor(self, cursor_type: str = 'circle', color: int = 7):
        """Defines the type and/or color of the cursor to be used

        Args:
            cursor_type (str, optional): The type of the cursor (circle, square, triangle). Defaults to 'circle'.
            color (int, optional): The color index of the Pyxel palette to use for the options (0-15). Defaults to 7.
        """
        if (
            cursor_type and
            cursor_type == 'circle' or
            cursor_type == 'square' or
            cursor_type == 'triangle'
        ):
            self._cursor['type'] = cursor_type

        if color >= 0 and color <= 15:
            self._cursor['color'] = color

    def set_cursor_img(self, img: int, u: int, v: int, colkey: int = None):
        """Set an image from the image bank as the cursor.

        Args:
            img (int): The image bank (0-2) to use
            u (int): Horizontal image position
            v (int): Vertical image position
            colkey (int, optional): If a color is indicated, it will be considered as transparent. Defaults to None.
        """
        self._cursor_img = {
            'use': True,
            'img': img,
            'u': u,
            'v': v,
            'colkey': colkey
        }

    def set_cursor_pos(self, pos: int):
        ''' Set the current position of the cursor '''
        self._current_pos = pos

    def set_highlight_color(self, color: int):
        """Sets the highlight color for the indicated option.

        Args:
            color (int): The color index of the Pyxel palette to use for the options (0-15)
        """
        if color >= 0 and color <= 15:
            self._highlight['color'] = color
            self._highlight['use'] = True

    def set_options(self, options: list):
        """Set the options for the menu

        Args:
            options (list): The options list
        """
        self._options = options

    def set_text_color(self, color: int):
        """Defines the color of the options

        Args:
            color (int): The color index of the Pyxel palette to use for the options (0-15)
        """
        if color < 0 or color > 15:
            return

        self._color = color

__init__(x, y, options=None, limit=5)

Class constructor

Parameters:

Name Type Description Default
x int

Position of the menu with respect to the left margin in pixels

required
y int

Position of the menu with respect to the up margin in pixels

required
options list

A list with the options to add. Defaults to None

None
limit int

The limit of options to display. Defaults to 5.

5
Source code in pyxel_menu.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(self, x: int, y: int, options: list = None, limit: int = 5):
    """Class constructor

    Args:
        x (int): Position of the menu with respect to the left margin in pixels
        y (int): Position of the menu with respect to the up margin in pixels
        options (list): A list with the options to add. Defaults to None
        limit (int, optional): The limit of options to display. Defaults to 5.
    """
    self._limit = limit
    self._x = x
    self._y = y
    self._current_pos = 0
    self._cursor = {
        'type': 'circle',
        'color': 7
    }
    self._color = 7
    self._cursor_color = 7
    self._cursor_img = {
        'use': False,
        'img': 0,
        'u': 0,
        'v': 0,
        'colkey': None
    }
    self._highlight = {
        'use': False,
        'color': 7
    }
    self._options = options

draw()

Draw the menu

Source code in pyxel_menu.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def draw(self):
    ''' Draw the menu '''
    if not self._options:
        return

    starty = self._y

    init = 0
    cursor_pos = self._current_pos

    if self._current_pos < len(self._options):
        if (
            self._current_pos > pyxel.floor(self._limit / 2) and 
            self._current_pos < len(self._options) - pyxel.floor(self._limit / 2)
        ):
            init = self._current_pos - pyxel.floor(self._limit / 2)
            cursor_pos = pyxel.floor(self._limit / 2)

        elif (
            self._current_pos >= len(self._options) - pyxel.floor(self._limit / 2)
        ):
            init = len(self._options) - self._limit
            cursor_pos = self._current_pos - init

    for i, option in enumerate(self._options[init:init + self._limit]):
        text_color = self._color

        if i == cursor_pos:
            if self._cursor_img['use']:
                pyxel.blt(
                    self._x,
                    starty,
                    self._cursor_img['img'],
                    self._cursor_img['u'],
                    self._cursor_img['v'],
                    8,
                    8,
                    self._cursor_img['colkey']
                )
            else:
                if self._cursor['type'] == 'circle':
                    pyxel.circ(self._x + 5, starty + 2, 2, self._cursor['color'])
                elif self._cursor['type'] == 'triangle':
                    pyxel.tri(self._x + 2, starty, self._x + 2, starty + 4, self._x + 6, starty + 2, self._cursor['color'])
                elif self._cursor['type'] == 'square':
                    pyxel.rect(self._x + 2, starty, 5, 5, self._cursor['color'])

            if self._highlight['use']:
                text_color = self._highlight['color']

        pyxel.text(self._x + 10, starty, option, text_color)
        starty += 8

get_current_pos()

Return selected option index

Source code in pyxel_menu.py
92
93
94
def get_current_pos(self):
    ''' Return selected option index '''
    return self._current_pos

get_current_text()

Return selected option text

Source code in pyxel_menu.py
96
97
98
def get_current_text(self):
    ''' Return selected option text '''
    return self._options[self._current_pos]

move_down()

Move the cursor down one position

Source code in pyxel_menu.py
105
106
107
108
def move_down(self):
    ''' Move the cursor down one position '''
    if self._current_pos < len(self._options) - 1:
        self._current_pos += 1

move_up()

Move the cursor up one position

Source code in pyxel_menu.py
100
101
102
103
def move_up(self):
    ''' Move the cursor up one position '''
    if self._current_pos > 0:
        self._current_pos -= 1

set_cursor(cursor_type='circle', color=7)

Defines the type and/or color of the cursor to be used

Parameters:

Name Type Description Default
cursor_type str

The type of the cursor (circle, square, triangle). Defaults to 'circle'.

'circle'
color int

The color index of the Pyxel palette to use for the options (0-15). Defaults to 7.

7
Source code in pyxel_menu.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def set_cursor(self, cursor_type: str = 'circle', color: int = 7):
    """Defines the type and/or color of the cursor to be used

    Args:
        cursor_type (str, optional): The type of the cursor (circle, square, triangle). Defaults to 'circle'.
        color (int, optional): The color index of the Pyxel palette to use for the options (0-15). Defaults to 7.
    """
    if (
        cursor_type and
        cursor_type == 'circle' or
        cursor_type == 'square' or
        cursor_type == 'triangle'
    ):
        self._cursor['type'] = cursor_type

    if color >= 0 and color <= 15:
        self._cursor['color'] = color

set_cursor_img(img, u, v, colkey=None)

Set an image from the image bank as the cursor.

Parameters:

Name Type Description Default
img int

The image bank (0-2) to use

required
u int

Horizontal image position

required
v int

Vertical image position

required
colkey int

If a color is indicated, it will be considered as transparent. Defaults to None.

None
Source code in pyxel_menu.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def set_cursor_img(self, img: int, u: int, v: int, colkey: int = None):
    """Set an image from the image bank as the cursor.

    Args:
        img (int): The image bank (0-2) to use
        u (int): Horizontal image position
        v (int): Vertical image position
        colkey (int, optional): If a color is indicated, it will be considered as transparent. Defaults to None.
    """
    self._cursor_img = {
        'use': True,
        'img': img,
        'u': u,
        'v': v,
        'colkey': colkey
    }

set_cursor_pos(pos)

Set the current position of the cursor

Source code in pyxel_menu.py
145
146
147
def set_cursor_pos(self, pos: int):
    ''' Set the current position of the cursor '''
    self._current_pos = pos

set_highlight_color(color)

Sets the highlight color for the indicated option.

Parameters:

Name Type Description Default
color int

The color index of the Pyxel palette to use for the options (0-15)

required
Source code in pyxel_menu.py
149
150
151
152
153
154
155
156
157
def set_highlight_color(self, color: int):
    """Sets the highlight color for the indicated option.

    Args:
        color (int): The color index of the Pyxel palette to use for the options (0-15)
    """
    if color >= 0 and color <= 15:
        self._highlight['color'] = color
        self._highlight['use'] = True

set_options(options)

Set the options for the menu

Parameters:

Name Type Description Default
options list

The options list

required
Source code in pyxel_menu.py
159
160
161
162
163
164
165
def set_options(self, options: list):
    """Set the options for the menu

    Args:
        options (list): The options list
    """
    self._options = options

set_text_color(color)

Defines the color of the options

Parameters:

Name Type Description Default
color int

The color index of the Pyxel palette to use for the options (0-15)

required
Source code in pyxel_menu.py
167
168
169
170
171
172
173
174
175
176
def set_text_color(self, color: int):
    """Defines the color of the options

    Args:
        color (int): The color index of the Pyxel palette to use for the options (0-15)
    """
    if color < 0 or color > 15:
        return

    self._color = color