Skip to content

Commit

Permalink
Merge pull request #2 from hostedposted/development
Browse files Browse the repository at this point in the history
Version 1.3.0
  • Loading branch information
hostedposted authored Jun 21, 2022
2 parents 03c70c5 + 195321e commit 876bedb
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 11 deletions.
41 changes: 38 additions & 3 deletions docs/call-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,15 @@ Usable key names are:
| text_color | [:octicons-tag-24: 1.2.0](https://github.com/hostedposted/py-gui/tree/1.2.0) | HEX (int like 0xFF0000), RGB or RGBA (tuple like (255, 0, 0, 1)) | :material-close: | None (auto) | The color of the text. |
| center | [:octicons-tag-24: 1.0.2](https://github.com/hostedposted/py-gui/tree/1.0.2) | boolean | :material-close: | False | Wether or not the text should be centered. |
| wrap_text | [:octicons-tag-24: 1.0.2](https://github.com/hostedposted/py-gui/tree/1.0.2) | boolean | :material-close: | True | Wether or not the text should be wrapped. |
| font_size | [:octicons-tag-24: 1.3.0](https://github.com/hostedposted/py-gui/tree/1.3.0) | float or integer | :material-close: | 48 | The font size of the text. |

!!! warning

**Center and wrap cannot be used together (yet).** If both are enabled the text will not be centered.

!!! warning

Font size uses the ``imgui.set_window_font_scale`` function to scale. This means that with big font sizes, the text will be a bit blurry.

??? example

Expand Down Expand Up @@ -275,6 +279,39 @@ Returns the color selected as an RGB or RGBA tuple.
```
![Image Example](images/color-picker-example.jpg)

### Elements.combo(label, default_value, choices, key, wrap_text)

[:octicons-tag-24: 1.3.0](https://github.com/hostedposted/py-gui/tree/1.3.0) - Add a combo box to the frame.

| Parameter | Latest Change | Type | Required | Default Value | Description |
| :------------ | ---------------------------------------------------------------------------- | --------------- | ---------------- | ---------------- | -------------------------------------------------------------------- |
| label | [:octicons-tag-24: 1.3.0](https://github.com/hostedposted/py-gui/tree/1.3.0) | string | :material-check: | :material-close: | This text will appear after the combo. |
| default_value | [:octicons-tag-24: 1.3.0](https://github.com/hostedposted/py-gui/tree/1.3.0) | index (integer) | :material-check: | :material-close: | The default value of the combo. Should be an index of the choices. |
| choices | [:octicons-tag-24: 1.3.0](https://github.com/hostedposted/py-gui/tree/1.3.0) | list of strings | :material-check: | :material-close: | The choices that should be displayed. |
| key | [:octicons-tag-24: 1.3.0](https://github.com/hostedposted/py-gui/tree/1.3.0) | string or None | :material-close: | None | What the value will be saved under in the [state](#elementsstate_1). |
| wrap_text | [:octicons-tag-24: 1.3.0](https://github.com/hostedposted/py-gui/tree/1.3.0) | boolean | :material-close: | True | Wether or not the text should be wrapped. |

Returns the index of the selected choice.

??? example

Let's add a combo box to the frame.

```py linenums="1" hl_lines="7 8"
import pygui

window = pygui.Window("Hello World", width=1600, height=1200)

@window.frame("Hello World", width=1400, height=900)
def hello_world(elements: pygui.Elements):
choices = ["Option 1", "Option 2", "Option 3"]
selected = elements.combo("Select an option", 0, choices)
elements.text(f"You selected: {choices[selected]}")

window.start()
```
![Example Image](images/combo-example.jpg)

### Elements.input_int(label, default_value, minimum, maximum, key, wrap_text)

[:octicons-tag-24: 1.0.2](https://github.com/hostedposted/py-gui/tree/1.0.2) - Add an input to the frame that only accepts integers.
Expand Down Expand Up @@ -368,9 +405,7 @@ def hello_world(elements: pygui.Elements):
value = elements.input_int("What is your favorite number?", 7, key="favorite")
@elements.button("Add 2")
def add_2():
elements.state["favorite"] = value + 2 # (1)!
elements.state["favorite"] += 2

window.start()
```

1. As of now, the value will only be set in the state when it is changed.
Binary file added docs/images/combo-example.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ theme:

plugins:
- search
- minify:
minify_html: true

markdown_extensions:
- abbr
Expand Down
62 changes: 56 additions & 6 deletions pygui/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import collections.abc
import math
import warnings
from typing import Optional, Union
from typing import List, Optional, Union

import imgui

Expand Down Expand Up @@ -101,6 +101,7 @@ def text(
text_color: Optional[Union[tuple, int]] = None,
center: bool = False,
wrap_text: bool = True,
font_size: Union[int, float] = 48,
) -> None:
"""
Add's a text element to the GUI.
Expand All @@ -115,6 +116,8 @@ def text(
Wether or not the text should be centered, by default False
wrap_text : bool, optional
Wether or not the text should be wrapped, by default True
font_size : Union[int, float], optional
The font size of the text, by default 48
"""
if center and wrap_text:
warnings.warn(
Expand All @@ -140,7 +143,11 @@ def text(
imgui.set_cursor_pos_x((window_width - text_width) / 2)
if wrap_text:
imgui.push_text_wrap_pos(imgui.get_window_width() * WRAPPING_PERCENTAGE)
if font_size != 48:
imgui.set_window_font_scale(font_size / 48)
imgui.text(text)
if font_size != 48:
imgui.set_window_font_scale(1.0)
if isinstance(text_color, tuple):
imgui.pop_style_color()
if wrap_text:
Expand All @@ -151,7 +158,7 @@ def button(
text: str,
text_color: Optional[Union[tuple, int]] = None,
wrap_text: bool = True,
key: Optional[str] = None
key: Optional[str] = None,
):
"""
Create a button element.
Expand Down Expand Up @@ -216,6 +223,7 @@ def button_event(self, key: str, time_limit: int = 10):
Callable
A decorator for handling element's that only get rendered after a button click.
"""

def handler(func):
if imgui.get_time() - self.state.get(key, -math.inf) < time_limit:
func()
Expand Down Expand Up @@ -244,7 +252,7 @@ def checkbox(
"""
changed, value = imgui.checkbox(
" " + label,
self.state.get(
self.state.setdefault(
key or label, default_value
), # Adding a space to the label make's it look better
)
Expand Down Expand Up @@ -296,7 +304,7 @@ def color_picker(
default_value = default_value + (old_default_value[3],)
changed, value = func(
" " + label, # Adding a space to the label make's it look better
*self.state.get(key or label, default_value),
*self.state.setdefault(key or label, default_value),
)
if changed:
self.state[key or label] = value
Expand Down Expand Up @@ -341,7 +349,7 @@ def input_int(
if wrap_text:
imgui.push_text_wrap_pos(imgui.get_window_width() * WRAPPING_PERCENTAGE)
changed, value = imgui.input_int(
" " + label, self.state.get(key or label, default_value)
" " + label, self.state.setdefault(key or label, default_value)
) # Adding a space to the label make's it look better
if wrap_text:
imgui.pop_text_wrap_pos()
Expand Down Expand Up @@ -382,10 +390,52 @@ def input_text(
if wrap_text:
imgui.push_text_wrap_pos(imgui.get_window_width() * WRAPPING_PERCENTAGE)
changed, value = imgui.input_text(
" " + label, self.state.get(key or label, default_value), max_length + 1
" " + label,
self.state.setdefault(key or label, default_value),
max_length + 1,
) # Adding a space to the label make's it look better
if wrap_text:
imgui.pop_text_wrap_pos()
if changed:
self.state[key or label] = value
return value

def combo(
self,
label: str,
default_value: int,
choices: List[str],
key: Optional[str] = None,
wrap_text: bool = True,
) -> int:
"""
Create a combo element and add it to the frame.
Parameters
----------
label : str
The text to be displayed after the combo.
default_value : int
The default value of the combo. This should be the index of the choice in the choices list.
choices : List[str]
The list of choices to be displayed in the combo.
key : Optional[str], optional
A key for the combo. This can be used for accessing the state of the element before it is added to the frame, by default None
wrap_text : bool, optional
Wether or not the text should be wrapped to fit, by default True
Returns
-------
int
The index of the selected value.
"""
if wrap_text:
imgui.push_text_wrap_pos(imgui.get_window_width() * WRAPPING_PERCENTAGE)
changed, value = imgui.combo(
" " + label, self.state.setdefault(key or label, default_value), choices
) # Adding a space to the label make's it look better
if changed:
self.state[key or label] = value
if wrap_text:
imgui.pop_text_wrap_pos()
return value
8 changes: 7 additions & 1 deletion pygui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Menu(NamedTuple):
title: str
keys: Optional[List[KEY]]


Theme = Type[Literal["light", "dark", "auto"]]


Expand All @@ -92,7 +93,12 @@ class Window:
theme: Theme

def __init__(
self, title: str, width: int = 800, height: int = 600, font: str = None, theme: Theme = "auto"
self,
title: str,
width: int = 800,
height: int = 600,
font: str = None,
theme: Theme = "auto",
):
self.title = title
self.width = width
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "py-gui-tool"
version = "1.2.0"
version = "1.3.0"
description = "PyGui is an easy to use gui."
authors = ["hostedposted <hostedpostedsite@gmail.com>"]
license = "MIT"
Expand Down

0 comments on commit 876bedb

Please sign in to comment.