示例#1
0
 def __init__(self, pos):
     self.dim = (800, 600)
     self.pos = list(pos)
     self.cadre = Cadre(self.dim, pos, C.WHITE, set_transparent=True)
     self.text_newwindow = TextBox((self.dim[0], 80), pos, C.WHITE,
                                   'New Window')
     self.text_dim = TextBox((400, 60), (pos[0] + 200, pos[1] + 150),
                             C.WHITE,
                             "Enter window dimension",
                             font=Font.f(30))
     self.input_x = InputText(DIM_INPDIM, (pos[0] + 210, pos[1] + 250),
                              C.WHITE,
                              text='1000',
                              limit=4)
     self.input_y = InputText(DIM_INPDIM, (pos[0] + 410, pos[1] + 250),
                              C.WHITE,
                              text='1000',
                              limit=4)
     self.text_name = TextBox((150, 60), (pos[0] + 100, pos[1] + 350),
                              C.WHITE,
                              'File name:',
                              font=Font.f(30))
     self.input_name = InputText(DIM_INPNAME, (pos[0] + 300, pos[1] + 350),
                                 C.WHITE)
     self.button_done = Button(DIM_BDONE, (pos[0] + 650, pos[1] + 500),
                               C.LIGHT_BLUE,
                               'Done',
                               font=Font.f(30))
示例#2
0
 def __init__(self, dim, pos):
     self.cadre = Cadre(dim, pos, C.WHITE)
     self.text_create = TextBox(DIM_INPNAME, (pos[0] + 40, pos[1] + 100),
                                C.WHITE,
                                'Create a python file?',
                                font=Font.f(30))
     self.button_yes = Button(DIM_BDONE, (pos[0] + 40, pos[1] + 180),
                              C.LIGHT_GREEN,
                              'Yes',
                              font=Font.f(30))
     self.button_no = Button(DIM_BDONE, (pos[0] + 200, pos[1] + 180),
                             C.LIGHT_RED,
                             'No',
                             font=Font.f(30))
示例#3
0
 def __init__(self, ChessGame):
     self.ChessGame = ChessGame
     self.cadre = Cadre((600, 1600), POS_MENU)
     self.text_turn = TextBox((400, 80),
                              (POS_MENU[0] + 50, POS_MENU[1] + 50),
                              text="Turn white",
                              font=Font.f(50),
                              centered=False)
     self.text_poss_moves = TextBox((600, 60),
                                    (POS_MENU[0] + 50, POS_MENU[1] + 210),
                                    text="Possible moves: 20",
                                    font=Font.f(50),
                                    centered=False)
     self.text_end = TextBox((450, 120),
                             (POS_MENU[0] + 50, POS_MENU[1] + 360),
                             font=Font.f(50))
     self.button_start = Button((450, 100),
                                (POS_MENU[0] + 50, POS_MENU[1] + 590),
                                C.LIGHT_GREEN,
                                text="Start new  game",
                                font=Font.f(50))
     self.state = 'start'
示例#4
0
    def __init__(self, pos, color, objtype):
        # don't scale pos as it is the mouse pos
        super().__init__(DIM_DRAG, pos,color, scale_pos=False)
        self.input_text = InputText(DIM_DRAG, pos, C.WHITE, font=Font.f(25), centered=True, scale_pos=False)
        self.objtype = objtype
        self.selected = True
        self.SELECT_COLOR = C.BLUE
        self.set_corners_points()
        self.color_choice = 'default' # store color choice way: default, text or range

        # color range
        self.ptr_pos = None
        self.bptr_y = None

        if self.objtype == 'TextBox' or self.objtype == 'Button':
            self.can_input = True
        else:
            self.can_input = False
示例#5
0
class Editor:
    # tools
    button_text_box = Button(DIM_TOOLS, (0, POS_TY), C.XLIGHT_GREY,'TextBox')
    button_button = Button(DIM_TOOLS, (0, POS_TY+80), C.XLIGHT_GREY,'Button')
    button_input_text = Button(DIM_TOOLS, (0, POS_TY+160), C.XLIGHT_GREY,'InputText')
    button_cadre = Button(DIM_TOOLS, (0, POS_TY+240), C.XLIGHT_GREY,'Cadre')
    button_done = Button(DIM_BDONE, (2800, 1400), C.LIGHT_BLUE,'Done',font=Font.f(30))
    drag_surf = None
    objs = []
    state = 'running'

    # set attr here for on_resize method
    window = None
    TOP_LEFT     = [0,0]
    TOP_RIGHT    = [0,0]
    BOTTOM_LEFT  = [0,0]
    BOTTOM_RIGHT = [0,0]

    current_selected = None

    @classmethod
    def set_window(cls, window):
        cls.window = window
        # don't rescale window.dim to have the original dimension stored (for on_resize)
        dim = window.dim
        cls.cadre = Cadre(dim, POS_WIN, C.WHITE)
        cls.TOP_LEFT     = Interface.dim.scale([POS_WIN[0], POS_WIN[1]])
        cls.TOP_RIGHT    = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1]])
        cls.BOTTOM_LEFT  = Interface.dim.scale([POS_WIN[0], POS_WIN[1] + dim[1]])
        cls.BOTTOM_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1] + dim[1]] )

    @staticmethod
    def on_resize(factor):
        if Editor.window:
            dim = Editor.window.dim
            Editor.TOP_LEFT     = Interface.dim.scale([POS_WIN[0], POS_WIN[1]])
            Editor.TOP_RIGHT    = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1]])
            Editor.BOTTOM_LEFT  = Interface.dim.scale([POS_WIN[0], POS_WIN[1] + dim[1]])
            Editor.BOTTOM_RIGHT = Interface.dim.scale([POS_WIN[0] + dim[0], POS_WIN[1] + dim[1]] )

    @classmethod
    def create_obj(cls, pos, objtype):
        gui_obj = GuiObj(pos, C.WHITE, objtype)
        cls.objs.append(gui_obj)
        cls.current_selected = gui_obj
        Settings.set_obj(gui_obj)

    @classmethod
    def check_deselect(cls):
        if cls.current_selected:
            if cls.cadre.on_it() and not cls.current_selected.as_change_dim:
                if not cls.current_selected.changing_dim and not cls.current_selected.on_it():
                    return True
        return False

    @classmethod
    def check_done(cls):
        for gobj in cls.objs:
            if not gobj.name:
                return
        return True

    @classmethod
    def str_gobj(cls, gobj):
        '''
        Name, objtype, dim, pos, text, color, font
        '''
        string = gobj.name + '\n'
        string += gobj.objtype + '\n'
        string += f'{gobj.dim[0]} {gobj.dim[1]}\n'
        pos = gobj.get_real_pos()
        string += f'{pos[0]} {pos[1]}\n'
        string += gobj.input_text.content + '\n'
        string += str(gobj.color) + '\n' # can be either str or list
        string += str(gobj.input_text.font['size'])
        return string

    @classmethod
    def create_savefile(cls):
        # create file
        with open(cls.window.name+'.pygui','w') as file:
            sep = '\n'+SEPARATOR+'\n'
            # first write name, dim of window
            global_info = f'{cls.window.name} {cls.window.dim[0]} {cls.window.dim[1]}'
            file.write(global_info)
            file.write(sep)
            for gobj in cls.objs:
                file.write(cls.str_gobj(gobj))
                file.write(sep)

    @classmethod
    def display(cls):
        cls.cadre.display()
        cls.button_text_box.display()
        cls.button_button.display()
        cls.button_input_text.display()
        cls.button_cadre.display()
        cls.button_done.display()
        if cls.drag_surf:
            cls.drag_surf.run()
        
        for gui_obj in cls.objs:
            gui_obj.display()
    
    @classmethod
    def react_events(cls, events, pressed):
        pos = pygame.mouse.get_pos()
        
        Settings.run(events, pressed)

        # first check for changing dim
        for gobj in cls.objs:
            gobj.react_events(events, pressed)
        
        # check if click anywhere -> deselect gui obj
        for event in events:
            if event.type == pygame.MOUSEBUTTONUP:
                if cls.check_deselect():
                    cls.current_selected.selected = False
                    cls.current_selected = None
                    Settings.deselect()

        # then check for new selected
        for gobj in cls.objs:
            if gobj.pushed(events):
                if not cls.current_selected:
                    cls.current_selected = gobj
                    gobj.selected = True  
                    Settings.set_obj(gobj)
        
        # check for gui obj to remove
        if pressed[pygame.K_DELETE]:
            if cls.current_selected:
                cls.objs.remove(cls.current_selected)
                cls.current_selected = None
                Settings.deselect()

        # check for deselect by enter
        if pressed[pygame.K_RETURN]:
            if cls.current_selected:
                cls.current_selected.selected = False
                cls.current_selected = None
                Settings.deselect()

        # last check for new gui obj
        if cls.drag_surf:
            for event in events:
                if event.type == pygame.MOUSEBUTTONUP:
                    # set new obj on window
                    cls.create_obj(pos,cls.drag_surf.objtype)
                    # reset drag_surf
                    cls.drag_surf = None
        
        if cls.button_text_box.pushed(events):
            cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'TextBox')
        elif cls.button_button.pushed(events):
            cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'Button')
        elif cls.button_input_text.pushed(events):
            cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'InputText')
        elif cls.button_cadre.pushed(events):
            cls.drag_surf = DragSurf(pos, C.LIGHT_BLUE, 'Cadre')
        
        # end edition
        if cls.button_done.pushed(events):
            cls.create_savefile()
            cls.state = 'done'
示例#6
0
    def save(cls):
        # name
        cls.obj.name = cls.input_name.content

        try:
            # scale dim
            dim = (float(cls.input_dimx.content),
                   float(cls.input_dimy.content))
            dim = Interface.dim.scale(dim)
        except:
            print('Incorrect dim')
            dim = cls.obj.dim

        try:
            # scale pos
            pos = [
                float(cls.input_coordx.content),
                float(cls.input_coordy.content)
            ]
            pos[0] += POS_WIN[0]
            pos[1] += POS_WIN[1]
            pos = Interface.dim.scale(pos)
        except:
            print('Incorrect pos')
            pos = cls.obj.pos

        cls.obj.set_new_dim(dim, pos)

        # fontsize
        try:
            font_size = int(cls.input_font.content)
            font = Font.f(font_size)
            cls.obj.input_text.font = font
        except AttributeError:
            print('Incorrect font')

        # color
        try:
            # get text and not content !!! -> see if pretext or not
            str_color = cls.input_color.text
            # check if still default
            if str_color == 'default...':
                color = C.WHITE
                cls.obj.color = 'WHITE'
            else:
                # try to set a predefined color
                try:
                    color = getattr(C, str_color)
                    cls.obj.color = str_color
                    # store that color is chosen by text
                    cls.obj.color_choice = 'text'
                except AttributeError:
                    # get the color range color
                    color = cls.color_range.chosen_color
                    cls.obj.color = color  # in this case a tuple (and not str)
                    cls.obj.color_choice = 'range'

            cls.obj.input_text.set_color(color)
        except:
            print('Incorrect color')

        # color range
        # store in "original" size to be able to rescale in between
        cls.obj.ptr_pos = Interface.dim.inv_scale(cls.color_range.pointer.pos)
        cls.obj.bptr_pos = Interface.dim.inv_scale(
            cls.color_range.bar_pointer.pos)
示例#7
0
class Settings:
    activate = False
    text_name = TextBox((150, DIM_Y), (POS_X, 200),
                        C.LIGHT_BLUE,
                        'Name:',
                        font=Font.f(25))
    input_name = InputText((250, DIM_Y), (POS_X + 150, 200),
                           C.WHITE,
                           font=Font.f(25),
                           limit=20)
    text_coord = TextBox((100, DIM_Y), (POS_X, 260),
                         C.LIGHT_BLUE,
                         'pos',
                         font=Font.f(25))
    input_coordx = InputText((150, DIM_Y), (POS_X + 100, 260),
                             C.WHITE,
                             font=Font.f(25))
    input_coordy = InputText((150, DIM_Y), (POS_X + 250, 260),
                             C.WHITE,
                             font=Font.f(25))
    text_dim = TextBox((100, DIM_Y), (POS_X, 320),
                       C.LIGHT_BLUE,
                       'dim',
                       font=Font.f(25))
    input_dimx = InputText((150, DIM_Y), (POS_X + 100, 320),
                           C.WHITE,
                           font=Font.f(25))
    input_dimy = InputText((150, DIM_Y), (POS_X + 250, 320),
                           C.WHITE,
                           font=Font.f(25))
    text_font = TextBox((200, DIM_Y), (POS_X, 380),
                        C.LIGHT_BLUE,
                        'Fontsize:',
                        font=Font.f(25))
    input_font = InputText((200, DIM_Y), (POS_X + 200, 380),
                           C.WHITE,
                           text='25',
                           font=Font.f(25),
                           limit=4)
    text_color = TextBox((200, DIM_Y), (POS_X, 440),
                         C.LIGHT_BLUE,
                         'Color:',
                         font=Font.f(25))
    input_color = InputText((200, DIM_Y), (POS_X + 200, 440),
                            C.WHITE,
                            text='',
                            font=Font.f(25),
                            pretext='default...')
    color_range = ColorRange((600, 330), (POS_X - 100, 520))

    @classmethod
    def set_pos(cls, pos):
        '''Update the position to have a unscaled relative position'''
        pos = Interface.dim.inv_scale(pos)  # keep precision
        pos[0] -= POS_WIN[0]
        pos[1] -= POS_WIN[1]
        cls.input_coordx.set_text(
            f'{pos[0]:.1f}')  # don't display all decimals
        cls.input_coordy.set_text(f'{pos[1]:.1f}')

    @classmethod
    def set_dim(cls, dim):
        '''Update the dimension to have unscaled dimension'''
        dim = Interface.dim.inv_scale(dim)
        cls.input_dimx.set_text(f'{dim[0]:.1f}')  # don't display all decimals
        cls.input_dimy.set_text(f'{dim[1]:.1f}')

    @classmethod
    def set_obj(cls, obj):
        cls.objtype = obj.objtype
        cls.obj = obj
        cls.activate = True
        # set attr to input
        cls.input_name.set_text(cls.obj.name)
        cls.input_font.set_text(cls.obj.input_text.font['size'])

        # set inp color text: if with pretext or not
        if cls.obj.color_choice == 'text':
            cls.input_color.set_text(cls.obj.color)
        elif cls.obj.color_choice == 'range':
            # set custom as pretext
            cls.input_color.pretext = 'custom...'
            cls.input_color.set_text('', with_pretext=True)
        else:
            cls.input_color.pretext = 'default...'
            cls.input_color.set_text('', with_pretext=True)

        # pos
        cls.set_pos(cls.obj.pos)

        # dim
        cls.set_dim(cls.obj.dim)

        # color range
        if cls.obj.ptr_pos and cls.obj.bptr_pos:
            cls.color_range.pointer.set_pos(cls.obj.ptr_pos, scale=True)
            cls.color_range.bar_pointer.set_pos(cls.obj.bptr_pos, scale=True)
        else:
            # default pos
            cls.color_range.pointer.set_pos((POS_X, 620), scale=True)
            cls.color_range.bar_pointer.set_pos((POS_X + 269, 520), scale=True)
        cls.color_range.set_color_range()

    @classmethod
    def run(cls, events, pressed):
        if cls.activate:
            cls.react_events(events, pressed)
            cls.display()

    @classmethod
    def save(cls):
        # name
        cls.obj.name = cls.input_name.content

        try:
            # scale dim
            dim = (float(cls.input_dimx.content),
                   float(cls.input_dimy.content))
            dim = Interface.dim.scale(dim)
        except:
            print('Incorrect dim')
            dim = cls.obj.dim

        try:
            # scale pos
            pos = [
                float(cls.input_coordx.content),
                float(cls.input_coordy.content)
            ]
            pos[0] += POS_WIN[0]
            pos[1] += POS_WIN[1]
            pos = Interface.dim.scale(pos)
        except:
            print('Incorrect pos')
            pos = cls.obj.pos

        cls.obj.set_new_dim(dim, pos)

        # fontsize
        try:
            font_size = int(cls.input_font.content)
            font = Font.f(font_size)
            cls.obj.input_text.font = font
        except AttributeError:
            print('Incorrect font')

        # color
        try:
            # get text and not content !!! -> see if pretext or not
            str_color = cls.input_color.text
            # check if still default
            if str_color == 'default...':
                color = C.WHITE
                cls.obj.color = 'WHITE'
            else:
                # try to set a predefined color
                try:
                    color = getattr(C, str_color)
                    cls.obj.color = str_color
                    # store that color is chosen by text
                    cls.obj.color_choice = 'text'
                except AttributeError:
                    # get the color range color
                    color = cls.color_range.chosen_color
                    cls.obj.color = color  # in this case a tuple (and not str)
                    cls.obj.color_choice = 'range'

            cls.obj.input_text.set_color(color)
        except:
            print('Incorrect color')

        # color range
        # store in "original" size to be able to rescale in between
        cls.obj.ptr_pos = Interface.dim.inv_scale(cls.color_range.pointer.pos)
        cls.obj.bptr_pos = Interface.dim.inv_scale(
            cls.color_range.bar_pointer.pos)

    @classmethod
    def deselect(cls):
        cls.activate = False
        cls.save()

    @classmethod
    def display(cls):
        cls.text_name.display()
        cls.input_name.display()
        cls.text_coord.display()
        cls.input_coordx.display()
        cls.input_coordy.display()
        cls.text_dim.display()
        cls.input_dimx.display()
        cls.input_dimy.display()
        cls.text_font.display()
        cls.input_font.display()
        cls.text_color.display()
        cls.input_color.display()
        cls.color_range.display()

    @classmethod
    def react_events(cls, events, pressed):
        cls.color_range.react_events(events, pressed)
        cls.input_font.run(events, pressed)
        cls.input_name.run(events, pressed)
        cls.input_color.run(events, pressed)
        cls.input_coordx.run(events, pressed)
        cls.input_coordy.run(events, pressed)
        cls.input_dimx.run(events, pressed)
        cls.input_dimy.run(events, pressed)
示例#8
0
from interface import Interface, TextBox, Button, InputText, Cadre, C, Font, Dimension, set_screen
import pygame
pygame.init()

E = lambda x: int(x * Dimension.f)
dim = Dimension((E(3000), E(1600)))
Font.init(dim.f)

screen = pygame.display.set_mode(dim.window)
screen.fill(C.WHITE)
pygame.display.set_caption('Game')

set_screen(screen)