-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Auto Reposition Frame for #821 #822
Draft
shenjackyuanjie
wants to merge
13
commits into
pyglet:master
Choose a base branch
from
shenjackyuanjie:auto-reposition
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
66f8655
make demo with many typing
shenjackyuanjie e7907d4
call_back -> callback add remove function
shenjackyuanjie d35d695
add some example
shenjackyuanjie 95b9f19
calculate
shenjackyuanjie 9066b2f
use hash(obj) instead of random.randint
shenjackyuanjie 008a38b
make the docs string example runable
shenjackyuanjie e865457
add example for RePositionFrame
shenjackyuanjie e179a17
move example to experimental
shenjackyuanjie b9bb45e
move stuf, add Grid
shenjackyuanjie e09639a
Fix name issue and add window to callback func
shenjackyuanjie 9a7dcf5
Merge remote-tracking branch 'upstream/master' into auto-reposition
shenjackyuanjie 9af9863
Fix example
shenjackyuanjie c818364
Add __all__ to gui
shenjackyuanjie File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import pyglet | ||
|
||
window = pyglet.window.Window(resizable=True) | ||
|
||
# create a frame | ||
reposition_frame = pyglet.gui.control.RePositionFrame(window) | ||
|
||
# create labels | ||
label = pyglet.text.Label("Hello World", x=0, y=0) | ||
b_label = pyglet.text.Label("Hello World with call back", x=0, y=0) | ||
|
||
# create a callback function | ||
def callback(obj, width, height, window): | ||
obj.x = width/3 | ||
obj.y = height/3 | ||
obj.text = f"Hello World with call back, width: {width}, height: {height}" | ||
|
||
# add the callback function to the frame | ||
reposition_frame.add_callback_func(b_label, callback) | ||
|
||
# add the calculate function to the frame | ||
# NOTICE: label need x, y, z, so the function must return a tuple with 3 elements | ||
reposition_frame.add_calculate_func(label, lambda obj, width, height, window: (width/2, height/2, 0)) | ||
|
||
# draw the labels | ||
@window.event | ||
def on_draw(): | ||
window.clear() | ||
label.draw() | ||
b_label.draw() | ||
|
||
# run the app | ||
pyglet.app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
from .widgets import * | ||
from .frame import * | ||
from .widgets import WidgetBase, PushButton, ToggleButton, Slider, TextEntry | ||
|
||
from .frame import Frame, MovableFrame | ||
|
||
from .control import RePositionFrame, GridFrame |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from typing import Callable, Union, Tuple, Dict, TYPE_CHECKING, TypeVar, Optional | ||
|
||
if TYPE_CHECKING: | ||
from pyglet.shapes import ShapeBase | ||
from pyglet.window import Window | ||
from pyglet.text import Label | ||
from pyglet.sprite import Sprite | ||
from pyglet.gui.widgets import WidgetBase | ||
|
||
Repositionable = Union[ShapeBase, Label, Sprite, WidgetBase] # just for typing | ||
else: | ||
Repositionable = TypeVar("Repositionable") # for type checking | ||
|
||
Num = Union[int, float] | ||
|
||
PotitionTuple = Union[Tuple[Num, Num], Tuple[Num, Num, Num]] | ||
|
||
CallBackFunc = Callable[[Repositionable, int, int, Window], None] | ||
CalculateFunc = Callable[[Repositionable, int, int, Window], PotitionTuple] | ||
|
||
CallBack = Union[CallBackFunc, CalculateFunc] | ||
IndexType = Union[int, str] | ||
|
||
__all__ = [ | ||
'RePositionFrame', | ||
'GridFrame', | ||
] | ||
|
||
class RePositionFrame: | ||
""" A Frame Like Object that allows for repositioning of widgets | ||
you can give A function and A widget/shape to let it reposition itself | ||
when the function is called | ||
|
||
>>> import pyglet | ||
>>> window = pyglet.window.Window(resizable=True) | ||
>>> reposition_frame = pyglet.gui.frame.RePositionFrame(window) | ||
>>> label = pyglet.text.Label("Hello World", x=0, y=0) | ||
>>> b_label = pyglet.text.Label("Hello World with call back", x=0, y=0) | ||
>>> def callback(obj, width, height, window): | ||
>>> obj.x = width/3 | ||
>>> obj.y = height/3 | ||
>>> obj.text = f"Hello World with call back, width: {width}, height: {height}" | ||
>>> reposition_frame.add_calculate_func(label, lambda obj, width, height, window: (width/2, height/2, 0)) | ||
>>> reposition_frame.add_callback_func(b_label, callback) | ||
>>> @window.event | ||
>>> def on_draw(): | ||
>>> window.clear() | ||
>>> label.draw() | ||
>>> b_label.draw() | ||
>>> pyglet.app.run() | ||
|
||
""" | ||
|
||
def __init__(self, window): | ||
window.push_handlers(self) | ||
self.window = window | ||
self.callback_dict: Dict[IndexType, Tuple[Repositionable, CallBackFunc]] = {} | ||
self.calculate_dict: Dict[IndexType, Tuple[Repositionable, CalculateFunc]] = {} | ||
|
||
def add_callback_func(self, obj: Repositionable, func: CallBackFunc, index: Optional[IndexType] = None) -> IndexType: | ||
""" Add A callback function to the frame | ||
|
||
:param obj: The object that will be repositioned | ||
:param func: The function that will be called | ||
:param index: The index of the object | ||
""" | ||
if index is None: | ||
index = hash(obj) | ||
self.callback_dict[index] = (obj, func) | ||
return index | ||
|
||
def add_calculate_func(self, obj: Repositionable, func: CalculateFunc, index: Optional[IndexType] = None) -> IndexType: | ||
""" Add A calculate function to the frame | ||
|
||
:param obj: The object that will be repositioned | ||
:param func: The function that will be called | ||
:param index: The index of the object | ||
""" | ||
if index is None: | ||
index = hash(obj) | ||
self.calculate_dict[index] = (obj, func) | ||
return index | ||
|
||
def remove_callback_func(self, index: IndexType): | ||
if index in self.callback_dict: | ||
self.callback_dict.pop(index) | ||
|
||
def remove_calculate_func(self, index: IndexType): | ||
if index in self.calculate_dict: | ||
self.calculate_dict.pop(index) | ||
|
||
def on_resize(self, width: int, height: int): | ||
""" Call all the functions when the window is resized """ | ||
for _, (obj, func) in self.callback_dict.items(): | ||
func(obj, width, height, self.window) | ||
|
||
for _, (obj, func) in self.calculate_dict.items(): | ||
obj.position = func(obj, width, height, self.window) | ||
|
||
|
||
class GridFrame(RePositionFrame): | ||
""" A TKinker grid like frame that allows for repositioning of widgets | ||
and also allow you to auto place the widgets in a grid like fashion | ||
""" | ||
|
||
# WIP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
__all__ = [ | ||
'Frame', | ||
'MovableFrame', | ||
] | ||
|
||
class Frame: | ||
"""The base Frame object, implementing a 2D spatial hash. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unused global variable Note