Skip to content

Commit 7250ae8

Browse files
authored
Merge pull request #369 from ppizarror/fix-build
Menu resize
2 parents 03a44bb + 45cccb7 commit 7250ae8

23 files changed

+435
-112
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ pyproject.toml
3636
*.lock
3737

3838
# MacOS files
39-
.DS_Store
39+
.DS_Store
40+
**._*

build.bat

-3
This file was deleted.

build.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
pygame-menu
3+
https://github.com/ppizarror/pygame-menu
4+
5+
BUILD
6+
7+
License:
8+
-------------------------------------------------------------------------------
9+
The MIT License (MIT)
10+
Copyright 2017-2021 Pablo Pizarro R. @ppizarror
11+
12+
Permission is hereby granted, free of charge, to any person obtaining a
13+
copy of this software and associated documentation files (the "Software"),
14+
to deal in the Software without restriction, including without limitation
15+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
16+
and/or sell copies of the Software, and to permit persons to whom the Software
17+
is furnished to do so, subject to the following conditions:
18+
19+
The above copyright notice and this permission notice shall be included in all
20+
copies or substantial portions of the Software.
21+
22+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28+
-------------------------------------------------------------------------------
29+
"""
30+
31+
import os
32+
import sys
33+
34+
assert len(sys.argv) == 2, 'Argument is required, usage: build.py pip/twine/gource'
35+
mode = sys.argv[1].strip()
36+
python = 'python3' if not sys.platform == 'win32' else 'py -3.7'
37+
38+
if mode == 'pip':
39+
if os.path.isdir('dist'):
40+
for k in os.listdir('dist'):
41+
if 'pygame_menu-' in k:
42+
os.remove(f'dist/{k}')
43+
if os.path.isdir('build'):
44+
for k in os.listdir('build'):
45+
if 'bdist.' in k or k == 'lib':
46+
os.system(f'rm -rf build/{k}')
47+
os.system(f'{python} setup.py sdist bdist_wheel')
48+
49+
elif mode == 'twine':
50+
if os.path.isdir('dist'):
51+
os.system(f'{python} -m twine upload dist/*')
52+
else:
53+
raise FileNotFoundError('Not distribution been found, execute build.py pip')
54+
55+
elif mode == 'gource':
56+
os.system('gource -s 0.25 --title pygame-menu --disable-auto-rotate --key '
57+
'--highlight-users --disable-bloom --multi-sampling -w --transparent --path ./')
58+
59+
else:
60+
raise ValueError(f'Unknown mode {mode}')

codecov.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
ignore:
22
- "docs/*.py"
33
- "pygame_menu/__pyinstaller/*py"
4+
- "pygame_menu/examples/*.py"
5+
- "pygame_menu/examples/other/*.py"
46
- "test/*.py"

docs/_source/gallery.rst

+23-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ examples, simply execute these commands in a terminal:
1313
$> python3 -m pygame_menu.examples.scroll_menu
1414
$> python3 -m pygame_menu.examples.simple
1515
$> python3 -m pygame_menu.examples.timer_clock
16+
$> python3 -m pygame_menu.examples.window_resize
1617
1718
Other examples that show specific use cases of the menu are also provided:
1819

@@ -40,16 +41,6 @@ Also, check out widget-specific examples in the
4041
`Adding Widgets <https://pygame-menu.readthedocs.io/en/latest/_source/add_widgets.html>`_
4142
documentation page.
4243

43-
Simple example
44-
--------------
45-
46-
.. image:: ../_static/example_simple.gif
47-
:align: center
48-
:alt: A basic button menu
49-
:width: 600
50-
51-
Source: `examples/simple.py <https://github.com/ppizarror/pygame-menu/blob/master/pygame_menu/examples/simple.py>`_
52-
5344

5445
Game selector example
5546
---------------------
@@ -84,6 +75,17 @@ Scroll menu example
8475
Source: `examples/scroll_menu.py <https://github.com/ppizarror/pygame-menu/blob/master/pygame_menu/examples/scroll_menu.py>`_
8576

8677

78+
Simple example
79+
--------------
80+
81+
.. image:: ../_static/example_simple.gif
82+
:align: center
83+
:alt: A basic button menu
84+
:width: 600
85+
86+
Source: `examples/simple.py <https://github.com/ppizarror/pygame-menu/blob/master/pygame_menu/examples/simple.py>`_
87+
88+
8789
Timer clock example
8890
-------------------
8991

@@ -95,6 +97,17 @@ Timer clock example
9597
Source: `examples/timer_clock.py <https://github.com/ppizarror/pygame-menu/blob/master/pygame_menu/examples/timer_clock.py>`_
9698

9799

100+
Window resize example
101+
------------------------
102+
103+
.. image:: ../_static/example_window_resize.gif
104+
:align: center
105+
:alt: Reizable window example
106+
:width: 600
107+
108+
Source: `examples/window_resize.py <https://github.com/ppizarror/pygame-menu/blob/master/pygame_menu/examples/window_resize.py>`_
109+
110+
98111
Other - Calculator (Decoration, Events, OOP, Frames)
99112
----------------------------------------------------
100113

1.02 MB
Loading

pygame_menu/_scrollarea.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
from pygame_menu._types import Union, NumberType, Tuple, List, Dict, Tuple2NumberType, \
5757
CursorInputType, Optional, Tuple2IntType, NumberInstance, ColorInputType, \
58-
EventVectorType, EventType, VectorInstance, StringVector
58+
EventVectorType, EventType, VectorInstance, StringVector, Any
5959

6060

6161
def get_scrollbars_from_position(
@@ -145,6 +145,7 @@ class ScrollArea(Base):
145145
_menubar: 'pygame_menu.widgets.MenuBar'
146146
_parent_scrollarea: 'ScrollArea'
147147
_rect: 'pygame.Rect'
148+
_scrollbars_props: Tuple[Any, ...]
148149
_scrollbar_positions: Tuple[str, ...]
149150
_scrollbar_thick: int
150151
_scrollbars: List['ScrollBar']
@@ -220,20 +221,46 @@ def __init__(
220221
self._bg_surface = None
221222
self._bg_surface = None
222223
self._decorator = Decorator(self)
223-
self._rect = pygame.Rect(0, 0, int(area_width), int(area_height))
224224
self._scrollbar_positions = tuple(unique_scrolls) # Ensure unique
225225
self._scrollbar_thick = scrollbar_thick
226-
self._scrollbars = []
227226
self._translate = (0, 0)
228227
self._world = world
229228

230229
self._extend_x = extend_x
231230
self._extend_y = extend_y
232231
self._menubar = menubar
233232

233+
self._scrollbars_props = (scrollbar_color, scrollbar_thick, scrollbar_slider_color,
234+
scrollbar_slider_hover_color, scrollbar_slider_pad,
235+
scrollbar_cursor, shadow, shadow_color, shadow_position,
236+
shadow_offset, controls_joystick, controls_mouse,
237+
controls_touchscreen, controls_keyboard)
234238
self.set_parent_scrollarea(parent_scrollarea)
239+
self.create_rect(area_width, area_height)
240+
241+
# Menu reference
242+
self._menu = None
243+
244+
def create_rect(self, width: int, height: int) -> None:
245+
"""
246+
Create rect object.
247+
248+
:param width: Area width
249+
:param height: Area height
250+
"""
251+
assert isinstance(width, int)
252+
assert isinstance(height, int)
253+
self._rect = pygame.Rect(0, 0, int(width), int(height))
254+
self._scrollbars = []
235255
self._view_rect = self.get_view_rect()
236256

257+
# Unpack properties
258+
(scrollbar_color, scrollbar_thick, scrollbar_slider_color,
259+
scrollbar_slider_hover_color, scrollbar_slider_pad,
260+
scrollbar_cursor, shadow, shadow_color, shadow_position,
261+
shadow_offset, controls_joystick, controls_mouse, controls_touchscreen,
262+
controls_keyboard) = self._scrollbars_props
263+
237264
for pos in self._scrollbar_positions:
238265
assert_position(pos)
239266

@@ -283,9 +310,6 @@ def __init__(
283310

284311
self._apply_size_changes()
285312

286-
# Menu reference
287-
self._menu = None
288-
289313
def _make_background_surface(self) -> None:
290314
"""
291315
Create background surface.

pygame_menu/examples/window_resize.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
pygame-menu
3+
https://github.com/ppizarror/pygame-menu
4+
5+
EXAMPLE - WINDOW RESIZE
6+
Resize the menu when the window is resized.
7+
8+
License:
9+
-------------------------------------------------------------------------------
10+
The MIT License (MIT)
11+
Copyright 2017-2021 Pablo Pizarro R. @ppizarror
12+
13+
Permission is hereby granted, free of charge, to any person obtaining a
14+
copy of this software and associated documentation files (the "Software"),
15+
to deal in the Software without restriction, including without limitation
16+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
17+
and/or sell copies of the Software, and to permit persons to whom the Software
18+
is furnished to do so, subject to the following conditions:
19+
20+
The above copyright notice and this permission notice shall be included in all
21+
copies or substantial portions of the Software.
22+
23+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29+
-------------------------------------------------------------------------------
30+
"""
31+
32+
import pygame
33+
import pygame_menu
34+
35+
pygame.init()
36+
37+
surface = pygame.display.set_mode((600, 400), pygame.RESIZABLE)
38+
pygame.display.set_caption("Example resizable window")
39+
40+
menu = pygame_menu.Menu(
41+
height=100,
42+
theme=pygame_menu.themes.THEME_BLUE,
43+
title='Welcome',
44+
width=100
45+
)
46+
47+
48+
def on_resize() -> None:
49+
"""
50+
Function checked if the window is resized.
51+
"""
52+
window_size = surface.get_size()
53+
new_w, new_h = 0.75 * window_size[0], 0.7 * window_size[1]
54+
menu.resize(new_w, new_h)
55+
print(f'New menu size: {menu.get_size()}')
56+
57+
58+
menu.add.label('Resize the window!')
59+
user_name = menu.add.text_input('Name: ', default='John Doe', maxchar=10)
60+
menu.add.selector('Difficulty: ', [('Hard', 1), ('Easy', 2)])
61+
menu.add.button('Quit', pygame_menu.events.EXIT)
62+
menu.enable()
63+
on_resize() # Set initial size
64+
65+
if __name__ == '__main__':
66+
while True:
67+
events = pygame.event.get()
68+
for event in events:
69+
if event.type == pygame.QUIT:
70+
pygame.quit()
71+
break
72+
if event.type == pygame.VIDEORESIZE:
73+
# Update the surface
74+
surface = pygame.display.set_mode((event.w, event.h),
75+
pygame.RESIZABLE)
76+
# Call the menu event
77+
on_resize()
78+
79+
# Draw the menu
80+
surface.fill((25, 0, 50))
81+
82+
menu.update(events)
83+
menu.draw(surface)
84+
85+
pygame.display.flip()

0 commit comments

Comments
 (0)