Example #1
0
    def __init__(self, position, *texts, font=Font(), color=Color.from_name("GRAY").darker(5)):
        """
            Create Selector Widget

            Parameters
            ----------
            position: Vec2
                Position of Selector
            texts: List of str
                Texts will be selected
            font: Font
                Font of Selector
            color: Color
                Color of Buttons
            
            .. warning:: You must give one text minimum.
        """
        super().__init__(position)
        self.texts = texts
        self.font = font
        self.color = color
        self.current = 0

        self.render_pred = None
        self.render_next = None
        self.max_size_text = None
        self.render = None
        self.update_size()
        self.update_render()
Example #2
0
    def __init__(self,
                 position,
                 text="",
                 command=None,
                 font=Font(),
                 size=Vec2(100, 40),
                 background=Color.from_name("GRAY").darker(5)):
        """
            Create Button Widget

            Parameters
            ----------
            position: Vec2
                Position of Button
            text: str
                Text of Button
            command: Function or None
                Click callback of Button
            font: Font
                Font of Button
            size: Vec2
                Size of Button
            background: Color or str
                Background of Button. May be a Color or a path to a image
        """
        super().__init__(position)
        self.text = text
        self.command = command
        self.font = font
        self.background = background
        self.size = size
        self.is_hover = False
        self.update_render()
Example #3
0
    def __init__(self,
                 position,
                 width=100,
                 font=Font(color=Color.from_name("BLACK")),
                 accepted=None,
                 image=None):
        """
            Create Entry Wiget

            Parameters
            ----------
            position: Vec2
                Position of Entry
            width: int
                Width of Entry
            font: Font
                Font of Label of Entry
            accepted: sstr
                String of accepted characters
            image: str or None
                Background image of Entry (or None)
        """
        super().__init__(position)

        self.width = width
        self.image = image
        self.label = Label(Vec2.zero(), "", font)
        self.text = ""
        self.focus = False
        self.accepted = accepted

        self.render = None
        self.update_render()
Example #4
0
    def __init__(self, position, size=Vec2(100, 100), font=Font(color=Color.from_name("BLACK")), accepted=None, image=None):
        """
            Create TextEdit Wiget

            Parameters
            ----------
            position: Vec2
                Position of TextEdit
            size: Vec2
                Size of TextEdit
            font: Font
                Font of Label of TextEdit
            accepted: str
                String of accepted characters
            image: str or None
                Path of Background image of TextEdit or None
        """
        super().__init__(position)

        self.size = size
        self.image = image
        self.label = Label(Vec2.zero(), "", font)
        self.text = ""
        self.focus = False
        self.accepted = accepted

        self.render = None
        self.update_render()
Example #5
0
    def __init__(self, position, text="", font=Font(), checked=False, scale=1):
        """
            Create Checkbox Widget

            Parameters
            ----------
            position: Vec2
                Position of Checkbox
            text: str
                Text of Checkbox
            font: Font
                Font of Checkbox
            checked: bool
                True if Checkbox is checked or False
            scale: int or float
                Scale of Checkbox
        """
        super().__init__(position)
        self.text = text
        self.font = font
        self.checked = checked
        self.scale = scale

        self.render = None
        self.render_btn = None
        self.update_render()
Example #6
0
    def __init__(self,
                 size=Vec2(900, 700),
                 color=Color.from_name("BLACK"),
                 title="PGGUI",
                 fps=60,
                 centered=True,
                 debug=False):
        """
            Create Window

            Parameters
            ----------
            size: int
                Size of Window
            color: Color
                Color of background of Window
            title: str
                Title of Window
            fps: int or None
                Max FPS of Window (None if you don't want limit)
            centered: bool
                True if you want centered window, else False
            debug: bool
                True if you want some debug infos, else False
        """

        self.fps = fps
        self.debug = debug
        self.color = color
        self.size = size

        if centered:
            os.environ['SDL_VIDEO_CENTERED'] = '1'

        pygame.display.set_caption(title)
        self.screen = pygame.display.set_mode(size.coords())

        self.clock = pygame.time.Clock()
        self.is_running = False
        self.debug_font = Font(bold=True, color=Color.from_name("ORANGE"))
        self.keys_callback = {}

        self.widgets = []
Example #7
0
    def __init__(self, position, text, font=Font(), background=None, spacing_line=2):
        """
            Create Label Widget

            Parameters
            ----------
            position: Vec2
                Position of Label
            text: str
                Text of Label
            font: Font
                Font of Label
            background: Color or None
                Background of Label. Set to None to transparent background
            spacing_line: int
                Space between two lines
        """
        super().__init__(position)
        self.text = text
        self.font = font
        self.background = background
        self.spacing_line = spacing_line
        self.update_render()
Example #8
0
from pggui import Window
from pggui.widgets import Entry
from pggui.utils import Vec2, Color, Font

import os

window = Window(Vec2(170, 260), debug=True)
file = os.path.join(os.path.dirname(__file__), "image.png")
window.add_widget(Entry(Vec2(10, 10)))
window.add_widget(Entry(Vec2(10, 60), 150))
window.add_widget(Entry(Vec2(10, 110),
                        font=Font(color=Color.from_name("RED"))))
window.add_widget(Entry(Vec2(10, 160), accepted="abc"))
window.add_widget(Entry(Vec2(10, 210), image=file))
window.run()
Example #9
0
from pggui import Window
from pggui.widgets import TextEdit
from pggui.utils import Vec2, Color, Font

import os

window = Window(Vec2(170, 510), debug=True)
file = os.path.join(os.path.dirname(__file__), "image.png")
window.add_widget(TextEdit(Vec2(10, 10)))
window.add_widget(TextEdit(Vec2(10, 120), Vec2(150, 50)))
window.add_widget(
    TextEdit(Vec2(10, 180), font=Font(color=Color.from_name("RED"))))
window.add_widget(TextEdit(Vec2(10, 290), accepted="abc"))
window.add_widget(TextEdit(Vec2(10, 400), image=file))
window.run()
Example #10
0
from pggui import Window
from pggui.widgets import Checkbox
from pggui.utils import Vec2, Color, Font

window = Window(Vec2(60, 150), debug=True)
window.add_widget(Checkbox(Vec2(10, 10), "Check 1"))
window.add_widget(Checkbox(Vec2(10, 40), "Check 2", font=Font(size=20)))
window.add_widget(Checkbox(Vec2(10, 70), "Check 3", checked=True))
window.add_widget(Checkbox(Vec2(10, 100), "Check 4", scale=2))
window.run()
Example #11
0
class Window:
    def __init__(self,
                 size=Vec2(900, 700),
                 color=Color.from_name("BLACK"),
                 title="PGGUI",
                 fps=60,
                 centered=True,
                 debug=False):
        """
            Create Window

            Parameters
            ----------
            size: int
                Size of Window
            color: Color
                Color of background of Window
            title: str
                Title of Window
            fps: int or None
                Max FPS of Window (None if you don't want limit)
            centered: bool
                True if you want centered window, else False
            debug: bool
                True if you want some debug infos, else False
        """

        self.fps = fps
        self.debug = debug
        self.color = color
        self.size = size

        if centered:
            os.environ['SDL_VIDEO_CENTERED'] = '1'

        pygame.display.set_caption(title)
        self.screen = pygame.display.set_mode(size.coords())

        self.clock = pygame.time.Clock()
        self.is_running = False
        self.debug_font = Font(bold=True, color=Color.from_name("ORANGE"))
        self.keys_callback = {}

        self.widgets = []

    def set_key_callback(self, key, callback):
        """
            Set Callback called after pressing key

            Parameters
            ----------
            key: Key
                Key will be pressed
            callback: Function or None
                Function will be called (or None to delete callback)
        """
        self.keys_callback[key.value] = callback

    def add_widget(self, widget):
        """
            Add Widget to Window

            Parameters
            ----------
            widget: Widget
                Widget to be added
        """
        if widget.parent is None:
            self.widgets.append(widget)

    def stop(self):
        """
            Stop Window
        """
        self.is_running = False

    def run(self):
        """
            Launch Window
        """
        self.is_running = True
        while self.is_running:
            for event in pygame.event.get():
                self.process_event(event)

            for i in self.widgets:
                i.update()

            self.screen.fill(self.color.get_rgba())

            for i in self.widgets:
                i.display(self.screen)

            if self.debug:
                try:
                    fps_label = self.debug_font.render(
                        "FPS : " + str(round(self.clock.get_fps())))
                except OverflowError:
                    fps_label = self.debug_font.render("FPS : Infinity")
                self.screen.blit(fps_label, (10, 10))

            if self.fps is None:
                self.clock.tick()
            else:
                self.clock.tick(self.fps)
            pygame.display.update()
        pygame.quit()

    def process_event(self, evt):
        """
            Process pygame event

            Parameters
            ----------
            evt: Event
                PyGame Event to be processed

            .. note:: You may not use this method. Window make it for you.
        """
        if evt.type == pgconst.QUIT:
            self.stop()

        if evt.type == pgconst.KEYUP and evt.key in self.keys_callback:
            self.keys_callback[evt.key]()

        for i in self.widgets:
            i.event(evt)
Example #12
0
from pggui import Window
from pggui.widgets import Label
from pggui.utils import Vec2, Color, Font

window = Window(Vec2(400, 400), debug=True)
window.add_widget(Label(Vec2(20, 30), "On y crois tous."))
window.add_widget(Label(Vec2(20, 50), "On y crois tous.", Font(size=20)))
window.add_widget(
    Label(Vec2(20, 80), "On y crois tous.", Font(size=20),
          Color.from_name("RED")))
window.add_widget(
    Label(Vec2(20, 110), "On y crois tous.\nOU PAS !", Font(size=20),
          Color.from_name("RED"), 0))
window.add_widget(
    Label(Vec2(20, 200), "On y crois pas tous.\nOU PAS PAS !", Font(size=20),
          Color.from_name("RED"), 10))
window.run()
Example #13
0
from pggui import Window
from pggui.widgets import Button
from pggui.utils import Vec2, Color, Font

import os


def click():
    print("clicked")


window = Window(Vec2(230, 110), debug=True)
file = os.path.join(os.path.dirname(__file__), "image.png")
window.add_widget(Button(Vec2(10, 10), "Click 1", click))
window.add_widget(Button(Vec2(120, 10), "Click 2", click, Font(size=20)))
window.add_widget(
    Button(Vec2(10, 60), "Click 3", click, background=Color.from_name("RED")))
window.add_widget(Button(Vec2(120, 60), "Click 4", click, background=file))
window.run()
Example #14
0
from pggui import Window
from pggui.widgets import Selector
from pggui.utils import Vec2, Color, Font


window = Window(Vec2(150, 140), debug=True)
window.add_widget(Selector(Vec2(10, 10), "Ceci est", "un test"))
window.add_widget(Selector(Vec2(10, 50), "Ceci est", "un test", font=Font(size=20)))
window.add_widget(Selector(Vec2(10, 100), "Ceci est", "un test", color=Color.from_name("RED")))
window.run()