Skip to content

Commit

Permalink
feat: Updating README.md and basic documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kerodekroma committed Oct 30, 2024
1 parent 4dd90f7 commit 74ba1f8
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 7 deletions.
132 changes: 125 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,131 @@
If you want to run in local you need first these libraries:

- pygame
# PySinglSlider

- pygbag
[![PyPI version](https://badge.fury.io/py/py-singl-slider.svg?version=latest)](https://badge.fury.io/py/py-single-slider)

Just run in terminal:
`PySinglSlider` is a customizable, simple single slider widget built with Pygame. Designed for quick setup and theming, it lets you add an interactive slider for Pygame applications that need fine-grained control over a value range.

~ python3 main.py
## Features

If you want to validate in a browser, just run:
- Easily customizable for different themes and styles.
- Supports adjustable value ranges.
- Includes event listening for smooth handling of slider interactions.
- Simple integration into any Pygame-based project.

~ python3 -m pygbag .
## Installation

Install `PySinglSlider` via [PyPI](https://pypi.org/project/PySinglSlider):

```bash
pip install PySinglSlider
```

## Getting Started

### Importing and Setting Up the Slider

To integrate `PySinglSlider` into your Pygame project, follow these steps:

1. Import `PySinglSlider` in your Pygame script.
2. Initialize the slider with optional parameters such as position, value range, initial value, and theme settings.
3. Handle events and render the slider in your game loop.

### Example Usage

Here's a basic example to get started:

```bash
import pygame
from PySinglSlider import PySinglSlider

pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

# Create slider

slider = PySinglSlider(x=100, y=250, min_value=0, max_value=100, initial_value=50, theme_name='default_theme')

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Pass events to the slider
slider.listen_event(event)

# Clear the screen
screen.fill((255, 255, 255))

# Render the slider
slider.render(screen)

# Display the current slider value
font = pygame.font.Font(None, 36)
value_text = font.render(f"Value: {slider.value:.2f}", True, (0, 0, 0))
screen.blit(value_text, (100, 400))

pygame.display.flip()
clock.tick(30)

pygame.quit()
```
### Parameters
- `x` (int): X-coordinate for slider position.
- `y` (int): Y-coordinate for slider position.
- `min_value` (int): Minimum value of the slider range.
- `max_value` (int): Maximum value of the slider range.
- `initial_value` (int): Starting value of the slider.
- `theme_name` (str): Name of the theme folder containing assets (images for handler, bar, etc.).
- `theme_path` (str, optional): Custom path to the theme directory if assets are not located in the default theme folder.
## Documentation
### Methods
#### `load_image(image_name, theme_name, theme_folder_path=None)`
Loads an image from the specified theme directory.
#### `get_rect()`
Returns the Pygame rectangle (Rect) of the slider bar.
#### `listen_event(event)`
Listens to and handles mouse or touch events for updating the slider’s handler position.
#### `get_current_value()`
Calculates and returns the slider's current value based on handler position.
#### `render(screen)`
Renders the slider's components (bar, handler) on the specified screen.
## Asset Customization
To customize the slider's appearance, you can create new images for the slider's handler and bar components in a folder with your chosen `theme_name`. Place this folder in the `theme` directory or specify the path using `theme_path`.
## Example Theme Folder Structure
```bash
project_root/
├── theme/
│ ├── default_theme/
│ │ ├── handler.png
│ │ ├── bar_center.png
│ │ ├── bar_corner.png
│ └── custom_theme/
│ ├── handler.png
│ ├── bar_center.png
│ ├── bar_corner.png
└── main.py
```
## License
This project is licensed under the MIT License. See the LICENSE file for more details.
Enjoy using `PySinglSlider`! Contributions, bug reports, and suggestions are welcome.
47 changes: 47 additions & 0 deletions py_singl_slider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,35 @@ def load_image(image_name, theme_name, theme_folder_path=None):
return pygame.image.load(image_path).convert_alpha()

class PySinglSlider:
"""
A customizable single slider component in Pygame.
Attributes:
scale (int): Scaling factor for assets.
initial_value (int): Initial slider value.
min_value (int): Minimum slider value.
max_value (int): Maximum slider value.
bar_x (int): X position of the bar.
bar_y (int): Y position of the bar.
bar_width (int): Width of the bar.
bar_height (int): Height of the bar.
handler_active_bg_color (tuple): Color when handler is active.
is_handler_down (bool): Indicates if the handler is currently being dragged.
value (int): Current value of the slider.
"""
def __init__(self, x=0, y=0, min_value=0, max_value=150, initial_value=0, theme_name= 'one', theme_path=None):
"""
Initialize the PySinglSlider with position, range, and theme.
Args:
x (int): X position of the slider bar.
y (int): Y position of the slider bar.
min_value (int): Minimum value of the slider.
max_value (int): Maximum value of the slider.
initial_value (int): Initial value of the slider.
theme_name (str): Theme to be applied for the slider's assets.
theme_path (str, optional): Custom theme directory path.
"""
self.scale = 2
self.initial_value = initial_value
self.min_value = min_value
Expand All @@ -26,9 +54,22 @@ def __init__(self, x=0, y=0, min_value=0, max_value=150, initial_value=0, theme_
self.value = self.get_current_value()

def get_rect(self):
"""
Get the slider's bounding rectangle.
Returns:
pygame.Rect: Rectangle representing the slider's bounding box.
"""
return pygame.Rect(self.bar_x, self.bar_y, self.bg_bar_center.get_rect().width, self.bg_bar_center.get_rect().height)

def setup_assets(self, theme_folder_path=None, theme_name='one'):
"""
Load and set up the slider's assets based on the theme.
Args:
theme_folder_path (str, optional): Path to the theme folder.
theme_name (str): Name of the theme to use.
"""
# handler
img_handler = load_image('handler.png', theme_name, theme_folder_path)
self.bg_handler = pygame.Surface((img_handler.get_width() * self.scale, img_handler.get_height() * self.scale))
Expand Down Expand Up @@ -71,6 +112,12 @@ def setup_assets(self, theme_folder_path=None, theme_name='one'):
self.bg_right_corner.set_colorkey((0, 0, 0))

def listen_event(self, event):
"""
Handle user input to control the slider.
Args:
event (pygame.event.Event): The event to handle, such as mouse or finger events.
"""
mouse_pos = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.FINGERDOWN:
if self.bar_rect.collidepoint(mouse_pos) or self.handler_rect.collidepoint(mouse_pos):
Expand Down

0 comments on commit 74ba1f8

Please sign in to comment.