def __init__(self, x, y, window, width=None): """ Create Console :param x: X Pos :param y: Y Pos :param window: Window of Program :param width: Width of Console or None if Width is width of window """ super(Console, self).__init__(x, y) if width is None: width = window.width self.window = window self.width = width self.return_text = "" self.send_text = "" self.cursor = False self.focus = False self.cursortimer = 20 self.accepted = "éèàçù€ " + string.digits + string.ascii_letters + string.punctuation self.render_entry = None self.commands = {} self.lastcommands = [] self.current_command = 0 self.font = Font(size=18, background=Color.from_name("WHITE"), color=Color.from_name("BLACK")) self.add_command("print", print_command) self.add_command("debug", debug_command) self.update_render()
def __init__(self, x, y, text, command=None, font=Font(), size=Vec2(100, 40), background=Color.from_name("GRAY").darker(5)): """ Create Button :param x: X Pos :param y: Y Pos :param text: Text of Button :param command: Function triggered when button is clicked :param font: Font of Button :param size: Size of Button :param background: Path of background image or Color """ super(Button, self).__init__(x, y) self.text = text self.command = command self.font = font self.background = background self.size = size self.ishover = False self.render = None self.old_render = None self.old_pos = None self.update_render()
def __init__(self, x, y, width=200, font=Font(color=Color.from_name("BLACK")), accepted="éèàçù€ " + string.digits + string.ascii_letters + string.punctuation, image=None): """ Create Entry :param x: X Pos :param y: Y Pos :param width: Width of Widget :param font: Font of Widget :param image: Background Image of Widget (or None) """ super(Entry, self).__init__(x, y) self.width = width self.font = font self.image = image self.text = "" self.cursor = False self.focus = False self.cursortimer = 20 self.accepted = accepted self.update_render()
def __init__(self, x, y, *texts): """ Create Selector :param x: X Pos :param y: Y Pos :param texts: Texts which are to select .. warning:: You must give one text minimum. """ super(Selector, self).__init__(x, y) self.texts = texts self.current = 0 self.render_pred = None self.render_next = None self.max_size_text = None self.font = Font() self.update_max_size() self.update_render()
def __init__(self, world): """ Constructor of UISystem :param world: World of EntitySystem .. note:: You may not use this constructor. EntitySystem is already create in World """ self.world = world self.widgets = [] self.debug_font = Font(bold=True, color=Color.from_name("RED"))
def __init__(self, text, font=Font()): """ Create TextComponent This Component is here to add text to entity Required Components : / :param text: Text of Component :param font: Font of Component """ super(TextComponent, self).__init__() self.font = font self.text = text self.render = self.font.render(self.text)
def __init__(self, x, y, text, font=Font()): """ Create Label :param x: X Pos :param y: Y Pos :param text: Text of Label :param font: Font of Label """ super(Label, self).__init__(x, y) self.text = text self.font = font self.render = self.font.render(text) self.old_render = None self.old_pos = None
def __init__(self, x, y, text, font=Font(), checked=False, scale=1): """ Create checkbox :param x: X Pos :param y: Y Pos :param text: Text of Checkbox :param font: Font of Checkbox :param checked: True if checkbox is checked or False :param scale: Scale of checkbox """ super(Checkbox, self).__init__(x, y) self.text = text self.font = font self.checked = checked self.scale = scale self.render_btn = None self.update_render()
from pyengine2 import Window from pyengine2.Widgets import Label from pyengine2.Utils import Config, Font, Color import os window = Window(500, 500, debug=True) config = Config(os.path.join(os.path.dirname(__file__), "config.json")) if not config.created: dic = {"label": {"text": "Bonjour", "color": "RED"}} config.create(dic) text = Label(100, 100, config.get("label.text", "Bonjour"), Font(color=Color.from_name(config.get("label.color", "RED")))) window.world.ui_system.add_widget(text) window.run()
pro1.update_render() pro2.value += 10 if pro2.value > 100: pro2.value = 0 pro2.update_render() images = [os.path.join(os.path.dirname(__file__), "sprite0.png")] game = Window(750, 500, debug=True) label = Label(100, 100, "Label") b = Button(200, 200, "Button", label_management) c = Checkbox(300, 300, "checkbox", scale=2) i = Image(100, 200, "sprite0.png", Vec2(20, 20)) en = Entry(100, 300, font=Font(size=18, color=Color.from_name("BLACK"))) select = Selector(200, 100, "Ceci", "est", "un", "test", "!", "SELECTOR !") pro1 = ProgressBar(400, 100) pro2 = ProgressBar(400, 200, ("sprite0.png", "sprite1.png"), value=34) con = Console(50, 400, game, 650) e = Entity() e.add_component(PositionComponent(0, 0)) e.add_component(SpriteComponent("sprite0.png", size=Vec2(78, 50), rotation=45)) e.add_component(ShowComponent()) e.add_component(ControlComponent("FOURDIRECTION")) game.world.ui_system.add_widget(label) game.world.ui_system.add_widget(b) game.world.ui_system.add_widget(c)
from pyengine2 import Window from pyengine2.Widgets import Label, Button, Entry from pyengine2.Utils import Color, Font def screenshot(): title = en.text if title == "": title = "screen" title += ".jpg" game.screenshot(path=title) game = Window(600, 300, debug=True) label = Label(300 - Font(size=30).rendered_size("Screenshot")[0] / 2, 40, "Screenshot", font=Font(size=30)) b = Button(250, 160, "Screen", screenshot) en = Entry(200, 100, font=Font(size=18, color=Color.from_name("BLACK"))) game.world.ui_system.add_widget(label) game.world.ui_system.add_widget(b) game.world.ui_system.add_widget(en) game.run()
class Console(Widget): def __init__(self, x, y, window, width=None): """ Create Console :param x: X Pos :param y: Y Pos :param window: Window of Program :param width: Width of Console or None if Width is width of window """ super(Console, self).__init__(x, y) if width is None: width = window.width self.window = window self.width = width self.return_text = "" self.send_text = "" self.cursor = False self.focus = False self.cursortimer = 20 self.accepted = "éèàçù€ " + string.digits + string.ascii_letters + string.punctuation self.render_entry = None self.commands = {} self.lastcommands = [] self.current_command = 0 self.font = Font(size=18, background=Color.from_name("WHITE"), color=Color.from_name("BLACK")) self.add_command("print", print_command) self.add_command("debug", debug_command) self.update_render() def update_render(self): """ Update render of Console .. note:: You must use this method after any change in Console """ font_render = self.font.render(self.return_text) self.render = pygame.Surface( (self.width, 36 + font_render.get_rect().height), pygame.SRCALPHA, 32).convert_alpha() self.render.blit(font_render, (4, 0)) self.render_entry = pygame.Surface((self.width, 35), pygame.SRCALPHA, 32).convert_alpha() self.render_entry.fill((50, 50, 50)) white = pygame.Surface((self.width - 8, 27)) white.fill((255, 255, 255)) self.render_entry.blit(white, (4, 4)) if len(self.send_text) or self.cursor: if self.cursor: text = self.font.render(self.send_text + "I") else: text = self.font.render(self.send_text) self.render_entry.blit( text, (5, self.render_entry.get_rect().height / 2 - text.get_rect().height / 2)) self.render.blit(self.render_entry, (0, font_render.get_rect().height + 1)) def update(self): """ Update Console .. note:: You may not use this method. UISystem make it for you """ if self.showed and self.active and self.focus: if self.cursortimer <= 0: self.cursor = not self.cursor self.cursortimer = 20 self.update_render() self.cursortimer -= 1 def event(self, evt): """ Manage Event :param evt: Event triggered .. note:: You may not use this method. UISystem make it for you """ if self.showed and self.active: if evt.type == const.KEYDOWN and self.focus: self.keypress(evt.key, evt.mod) elif evt.type == const.TEXTINPUT and self.focus: if evt.text in self.accepted: self.send_text += evt.text self.update_render() elif evt.type == const.MOUSEBUTTONDOWN and evt.button == const.BUTTON_LEFT: font_render = self.font.render(self.return_text) if self.render_entry.get_rect( x=self.x, y=self.y + font_render.get_rect().height + 1).collidepoint(evt.pos[0], evt.pos[1]): self.focus = True else: self.focus = False self.cursor = False self.update_render() def keypress(self, key, mod): """ Manage KEYDOWN Event :param key: Key Pressed :param mod: Modifier of Key .. note:: You may not use this method. Entry make it for you """ mod -= 4096 if key == const.K_BACKSPACE: if len(self.send_text): self.send_text = self.send_text[:-1] self.update_render() elif key == const.K_RETURN: self.lastcommands.append(self.send_text) self.current_command = len(self.lastcommands) self.execute_command(self.send_text) self.send_text = "" elif key == const.K_UP: if self.current_command > 0: self.current_command -= 1 self.send_text = self.lastcommands[self.current_command] self.update_render() elif key == const.K_DOWN: if self.current_command < len(self.lastcommands) - 1: self.current_command += 1 self.send_text = self.lastcommands[self.current_command] self.update_render() elif self.current_command < len(self.lastcommands): self.send_text = "" self.current_command += 1 self.update_render() def set_return(self, text): """ Set Return of Console :param text: Text to be in Return """ self.return_text = "> " + text def add_command(self, command, function): """ Add command to Console :param command: Command :param function: Function triggered when command is call """ if command in self.commands.keys(): logger.warning("Command overrided : " + command) self.commands[command] = function def delete_command(self, command): """ Remove command from Console :param command: Command """ try: del self.commands[command] except KeyError: logger.warning("Command '" + command + "' doesn't exist") def execute_command(self, command): """ Execute command of Console :param command: Console to execute .. note:: You may not use this method. Console make it for you. """ if len(command.split(" ")) > 1: args = command.split(" ")[1:] else: args = [] command = command.split(" ")[0] if command in self.commands: self.commands[command](self, self.window, args) else: logger.warning("Unknown command : " + command)
def __init__(self, width, height, color=Color.from_name("BLACK"), title="PyEngine 2", icon=None, limit_fps=None, update_rate=60, centered=True, debug=False): """ Create Window :param width: Width of Window :param height: Height of Window :param color: Color of background of Window :param title: Title of Window :param icon: Icon of Window (None if you have any icon) :param limit_fps: Max FPS of Window (None if you don't want limit) :param update_rate: Rate of World's update :param centered: True if you want centered window, else False :param debug: True if you want some debug infos, else False """ self.update_rate = update_rate self.debug = debug self.old_debug = debug self.color = color self.width = width self.height = height self.limit_fps = limit_fps if icon is not None: pygame.display.set_icon(pygame.image.load(icon)) if centered: os.environ['SDL_VIDEO_CENTERED'] = '1' pygame.display.set_caption(title) self.screen = pygame.display.set_mode((width, height)) pygame.time.set_timer(const.USEREVENT, round(1000 / update_rate)) self.clock = pygame.time.Clock() self.is_running = False self.debug_font = Font(bold=True, color=Color.from_name("ORANGE")) self.world = World(self) self.old_world = None self.music_system = MusicSystem() self.sound_system = SoundSystem() self.callbacks = { "START": None, "STOP": None, "OUTOFWINDOW": None, "CHANGEWORLD": None } self.fps_timer = 30 try: self.fps_label = self.debug_font.render( "FPS : " + str(round(self.clock.get_fps()))) except OverflowError: self.fps_label = self.debug_font.render("FPS : Infinity") self.old_fps = None if debug: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO)
class Window: def __init__(self, width, height, color=Color.from_name("BLACK"), title="PyEngine 2", icon=None, limit_fps=None, update_rate=60, centered=True, debug=False): """ Create Window :param width: Width of Window :param height: Height of Window :param color: Color of background of Window :param title: Title of Window :param icon: Icon of Window (None if you have any icon) :param limit_fps: Max FPS of Window (None if you don't want limit) :param update_rate: Rate of World's update :param centered: True if you want centered window, else False :param debug: True if you want some debug infos, else False """ self.update_rate = update_rate self.debug = debug self.old_debug = debug self.color = color self.width = width self.height = height self.limit_fps = limit_fps if icon is not None: pygame.display.set_icon(pygame.image.load(icon)) if centered: os.environ['SDL_VIDEO_CENTERED'] = '1' pygame.display.set_caption(title) self.screen = pygame.display.set_mode((width, height)) pygame.time.set_timer(const.USEREVENT, round(1000 / update_rate)) self.clock = pygame.time.Clock() self.is_running = False self.debug_font = Font(bold=True, color=Color.from_name("ORANGE")) self.world = World(self) self.old_world = None self.music_system = MusicSystem() self.sound_system = SoundSystem() self.callbacks = { "START": None, "STOP": None, "OUTOFWINDOW": None, "CHANGEWORLD": None } self.fps_timer = 30 try: self.fps_label = self.debug_font.render( "FPS : " + str(round(self.clock.get_fps()))) except OverflowError: self.fps_label = self.debug_font.render("FPS : Infinity") self.old_fps = None if debug: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) def set_callback(self, callback, function): """ Set callback of Window :param callback: Callback to set :param function: Function which is triggered by the callback """ if callback in self.callbacks.keys(): self.callbacks[callback] = function else: logger.warning("The callback '{}' doesn't exist.", callback) def call(self, callback, *param): """ Call a callback :param callback: Callback to call :param param: Parameters of callback .. note:: You may not use this method. Window and EntitySystem make it for you """ if callback in self.callbacks.keys( ) and self.callbacks[callback] is not None: self.callbacks[callback](*param) def screenshot(self, pos=(0, 0), size=None, path="screenshot.jpg"): if size is None: size = (self.width - pos[0], self.height - pos[1]) rect = pygame.Rect(pos, size) sub = self.screen.subsurface(rect) pygame.image.save(sub, path) def stop(self): """Stop Window""" self.is_running = False self.call("STOP") logger.debug("Stop Window") def run(self): """Run Window""" self.is_running = True self.call("START") logger.debug("Start Window") while self.is_running: for event in pygame.event.get(): self.process_event(event) if self.old_world != self.world: self.call("CHANGEWORLD", self.old_world, self.world) self.screen.fill(self.color.get_rgba()) self.old_world = self.world if self.old_debug != self.debug: self.screen.fill(self.color.get_rgba()) self.old_debug = self.debug rects = self.world.show(self.screen) if self.debug: rects_debug = self.world.show_debug(self.screen) if len(rects_debug): rects += rects_debug if self.old_fps != self.fps_label and self.old_fps is not None: self.screen.fill(self.color.get_rgba(), self.old_fps.get_rect(x=10, y=10)) self.screen.blit(self.fps_label, (10, 10)) if self.limit_fps is None: self.clock.tick() else: self.clock.tick(self.limit_fps) if self.debug and self.old_fps != self.fps_label: self.old_fps = self.fps_label rects.append(self.fps_label.get_rect(x=10, y=10)) pygame.display.update(rects) pygame.quit() def process_event(self, evt): """ Process event :param evt: Event to be processed .. note:: You may not use this method. Window make it for you. """ if evt.type == const.QUIT: self.stop() elif evt.type == const.USEREVENT: self.world.update() if self.debug: self.fps_timer -= 1 if self.fps_timer <= 0: try: self.fps_label = self.debug_font.render( "FPS : " + str(round(self.clock.get_fps()))) except OverflowError: self.fps_label = self.debug_font.render( "FPS : Infinity") self.fps_timer = 30 elif evt.type == self.music_system.ENDSOUND: self.music_system.next_song() else: self.world.event(evt)
class Selector(Widget): def __init__(self, x, y, *texts): """ Create Selector :param x: X Pos :param y: Y Pos :param texts: Texts which are to select .. warning:: You must give one text minimum. """ super(Selector, self).__init__(x, y) self.texts = texts self.current = 0 self.render_pred = None self.render_next = None self.max_size_text = None self.font = Font() self.update_max_size() self.update_render() def update_render(self): """ Update render of Selector .. note:: You must use this method after any change of Selector """ self.render_pred = pygame.Surface((25, 25), pygame.SRCALPHA, 32).convert_alpha() self.render_pred.fill(Color.from_name("GRAY").darker(5).get_rgba()) text_render = self.font.render("<") x = 25 / 2 - text_render.get_rect().width / 2 y = 25 / 2 - text_render.get_rect().height / 2 self.render_pred.blit(text_render, (x, y)) self.render_next = pygame.Surface((25, 25), pygame.SRCALPHA, 32).convert_alpha() self.render_next.fill(Color.from_name("GRAY").darker(5).get_rgba()) text_render = self.font.render(">") x = 25 / 2 - text_render.get_rect().width / 2 y = 25 / 2 - text_render.get_rect().height / 2 self.render_next.blit(text_render, (x, y)) text_render = self.font.render(self.texts[self.current]) self.render = pygame.Surface( (25 + 5 + self.max_size_text + 5 + 25, 25), pygame.SRCALPHA, 32).convert_alpha() self.render.blit(self.render_pred, (0, 0)) self.render.blit(text_render, (25 + 5 + self.max_size_text / 2 - text_render.get_rect().width / 2, 25 / 2 - text_render.get_rect().height / 2)) self.render.blit(self.render_next, (25 + 5 + self.max_size_text + 5, 0)) def update_max_size(self): """ Update maxi_size_text of Selector .. note:: You must use this method after change list of texts in Selector """ maxi = 0 for i in self.texts: maxi = clamp(maxi, self.font.rendered_size(i)[0]) self.max_size_text = maxi def event(self, evt): """ Manage Event :param evt: Event triggered .. note:: You may not use this method. UISystem make it for you """ if self.showed and self.active and evt.type == const.MOUSEBUTTONDOWN and evt.button == const.BUTTON_LEFT: if self.render_pred.get_rect(x=self.x, y=self.y).collidepoint( evt.pos[0], evt.pos[1]): if self.current == 0: self.current = len(self.texts) - 1 else: self.current -= 1 self.update_render() elif self.render_next.get_rect(x=self.x + 40 + self.max_size_text, y=self.y).collidepoint( evt.pos[0], evt.pos[1]): if self.current == len(self.texts) - 1: self.current = 0 else: self.current += 1 self.update_render()