Exemple #1
0
    def __init__(self, controller: Controller):
        super().__init__()

        self._controller = controller

        self._app = QApplication([])

        self._window = QWidget()
        self._window.setWindowTitle(
            QCoreApplication.translate("Project - ISBN Analysis",
                                       "Project - ISBN Analysis"))
        self._window.setFixedSize(930, 600)

        self._toolbar = Toolbar(self._window, self._controller)
        self._main = MainWindow(self._window, self._controller)
        self._window.show()

        sys.exit(self._app.exec())
	def __init__(self,x,y,w,h,p):
		Window.__init__(self,x,y,w,h,p)
		self.windows = []
		
		self.windows.append(ColorWheelWindow(100,300,280,260,self.parent))
		self.current_color = (255,100,0)
	
		# blank canvas window
		newdoc = pygame.Surface((640,480))
		newdoc.fill((255,255,255))
		self.windows.append(CanvasWindow(300,300,self.parent,newdoc))
		
		# create toolbar
		self.toolbar = Toolbar(self.parent)
		self.toolbar.items.append(ToolbarButton(0,0,'File', self.parent, ['New', 'Open']))
		self.toolbar.items.append(ToolbarButton(0,0,'View', self.parent, ['Tools', 'Colours', 'Answer this']))
		self.toolbar.render() # item appended, needs to be re-rendered

		self.draw()
from GUI.FileBrowser import FileBrowser
from GUI.ToolbarButton import ToolbarButton

from ToolWindow import ToolWindow
from MainWindow import MainWindow

# END IMPORTS

screen = pygame.display.set_mode((1280,720))

mainWindow = MainWindow(0,0,screen.get_width(),screen.get_height(), screen)
mainWindow.surface.fill((200,200,200))
windows = []
windows.append(Window(10, 10, 250, 100, screen))
windows.append(ToolWindow(100, 100, 400, 400, screen))
toolbar = Toolbar(screen)
toolbar.items.append(ToolbarButton(0,0,'View', screen, ['Tools']))
toolbar.render()

while True:
	scrolldown = False
	scrollup = False
	screen.fill((200,200,200))
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			sys.exit()
		if event.type == pygame.MOUSEBUTTONDOWN:
			if event.button == 5:
				scrolldown = True
			else:
Exemple #4
0
def main():
    # init GUI
    game_display, clock = pygame_init_function()

    # Create new game board object and test print it
    chess_board = ChessBoard()
    chess_board.test_print_board()  # Test function TODO: delete me in the end

    # All needed variables, TODO: Check if needed here
    quit_game = False
    all_tiles = []
    all_pieces = []
    selected_piece = None

    # This var is a list that holds the position in pixels of all tiles in relation to the user GUI
    all_squares_parameters = create_squares_parameters()

    # Show the menu to the user
    game_menu(game_display, chess_board)

    # Board drawing function
    draw_chess_pieces(game_display, chess_board, all_pieces, all_tiles)

    # Create toolbar
    toolbar = Toolbar(CONSTANT_PIXEL_SIZE * 9, CONSTANT_PIXEL_SIZE / 2,
                      (128, 128, 128))

    # GUI game loop until user quits the game
    while not quit_game:
        click = pygame.mouse.get_pressed()

        # Check's if the user quited the game
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game = True
                pygame.quit()
                quit()

            # Check's if the user selected a tile
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 2:
                    game_menu()
                    new_pieces = print_pieces_images(chess_board)
                    all_pieces = new_pieces
                if selected_piece is None:
                    mouse_x, mouse_y = pygame.mouse.get_pos()
                    for piece in range(len(all_pieces)):
                        if all_pieces[piece][1][0] < mouse_x < all_pieces[
                                piece][1][0] + CONSTANT_PIXEL_SIZE:
                            if all_pieces[piece][1][1] < mouse_y < all_pieces[
                                    piece][1][1] + CONSTANT_PIXEL_SIZE:
                                selected_piece = piece
                    if 0 < mouse_x < CONSTANT_PIXEL_SIZE * 9 and 0 < mouse_y < CONSTANT_PIXEL_SIZE / 2:
                        toolbar.click((mouse_x, mouse_y), chess_board)
                        all_pieces = print_pieces_images(chess_board)
                    print(selected_piece)

            # Fallow the user mouse drag
            if event.type == pygame.MOUSEMOTION and selected_piece is not None:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                all_pieces[selected_piece][1][
                    0] = mouse_x - CONSTANT_PIXEL_SIZE / 2
                all_pieces[selected_piece][1][
                    1] = mouse_y - CONSTANT_PIXEL_SIZE / 2

            # TODO: create drag and drop GUI
            # Check's if the user released the mouse
            if event.type == pygame.MOUSEBUTTONUP and selected_piece is not None:
                try:
                    print("Mouse button up")
                    print("Selected piece: " + str(selected_piece))
                    the_move = 0
                    for mov_destination in range(64):
                        if all_squares_parameters[mov_destination][0] < all_pieces[selected_piece][1][0] + \
                                CONSTANT_PIXEL_SIZE / 2 < all_squares_parameters[mov_destination][1]:
                            if all_squares_parameters[mov_destination][2] < all_pieces[selected_piece][1][1] + \
                                    CONSTANT_PIXEL_SIZE / 2 < all_squares_parameters[mov_destination][3]:
                                the_move = mov_destination
                    all_pieces[selected_piece][1][0] = all_squares_parameters[
                        the_move][0]
                    all_pieces[selected_piece][1][1] = all_squares_parameters[
                        the_move][2]

                    chess_board.move(all_pieces[selected_piece][2], the_move)
                    all_pieces = print_pieces_images(chess_board)
                    chess_board.test_print_board(
                    )  # Test function TODO: delete me in the end

                # TODO: to broad except catch
                except Exception as e:
                    print(e)
                selected_piece = None

        # reset board
        game_display.fill((127, 78, 40))
        toolbar.draw(game_display)
        notation_drawing(game_display)

        # redraw tiles
        for info in all_tiles:
            pygame.draw.rect(game_display, info[0], info[1])

        # redraw images
        for img in all_pieces:
            game_display.blit(img[0], img[1])

        pygame.display.update()
        clock.tick(60)
class MainWindow(Window):
	def __init__(self,x,y,w,h,p):
		Window.__init__(self,x,y,w,h,p)
		self.windows = []
		
		self.windows.append(ColorWheelWindow(100,300,280,260,self.parent))
		self.current_color = (255,100,0)
	
		# blank canvas window
		newdoc = pygame.Surface((640,480))
		newdoc.fill((255,255,255))
		self.windows.append(CanvasWindow(300,300,self.parent,newdoc))
		
		# create toolbar
		self.toolbar = Toolbar(self.parent)
		self.toolbar.items.append(ToolbarButton(0,0,'File', self.parent, ['New', 'Open']))
		self.toolbar.items.append(ToolbarButton(0,0,'View', self.parent, ['Tools', 'Colours', 'Answer this']))
		self.toolbar.render() # item appended, needs to be re-rendered

		self.draw()
		
	def draw(self):
		self.bar.draw()
		Window.draw(self)
			

	def update(self, mb_up, scrolldown, scrollup, keypressed):
		self.surface.fill((200,200,200))
		
		# handle toolbar events
		self.toolbar_events(mb_up)

		# toolbar rendering/drawing
		# Dealing with a list, so it's surrounded by try/except (in the event that no windows are on the screen)
		check_toolbar  = False
		try:
			if not self.windows[len(self.windows)-1].isClicked():
				check_toolbar = True
		except:
			check_toolbar = True

		if check_toolbar:
			for item in self.toolbar.items:
				mx, my = pygame.mouse.get_pos()
				if item.is_clicked(item.x, item.y, pygame.mouse.get_pressed()[0]):
					item.show_menu = True
					item.render()
					item.menu.render()
				elif item.show_menu:
					if mx > item.menu.x and mx<item.menu.x+item.menu.surface.get_width() and my > 0 and my < item.menu.y+item.menu.surface.get_height():
						item.show_menu = True
					else:
						item.show_menu = False
				elif pygame.mouse.get_pressed()[0]:
					item.show_menu = False
					item.render()

		
		try:
			# Update the last most element in the windows list, which is the one
			# currently in focus.
			self.windows[-1].update(mb_up, scrolldown, scrollup, keypressed)
			
			for w in xrange(len(self.windows)):
				if self.windows[w].title == 'File Browser':
					if self.windows[w].complete:
						image_file = pygame.image.load(self.windows[w].file_system.getsyspath('/')+'/'+self.windows[w].selected)
						self.windows.append(CanvasWindow(300,300,self.parent,image_file, self.windows[w].selected)) # testing window appended
						self.windows.remove(self.windows[w])
				elif self.windows[w].title[:6] == 'Canvas':
					self.windows[w].draw_color = self.current_color
				elif self.windows[w].title == 'Color Picker':
					self.current_color = self.windows[w].get_color()
				self.windows[w].draw()
				if self.windows[w].exit_btn.is_clicked(self.windows[w].x+self.windows[w].exit_btn.x,
				self.windows[w].y+self.windows[w].exit_btn.y, mb_up):
					self.windows.remove(self.windows[w])
					break
				if self.windows[w].is_clicked() and w != len(self.windows)-1:
					change = True
					for window in range(len(self.windows)-1,0,-1):
						if self.windows[window].clicked:
							change = False
						if window > w and self.windows[window].is_clicked():
							change=False
					if change:
						store = self.windows[w]
						self.windows.remove(self.windows[w])
						self.windows.append(store)
		except IndexError:
			pass # It's cool, there are just no windows on the screen.
			
		
		self.toolbar.draw()
		
	def toolbar_events(self, mouseclick):
		toolbar_clicked = self.toolbar.get_clicked(mouseclick) # returns a list of all clicked elements
		for clicked in toolbar_clicked:
			if clicked.text == 'Exit': # identification done using text, seems effiecient enough
				pygame.quit()
			elif clicked.text == 'Tools':
				self.windows.append(ToolWindow(100, 100, 400, 400, self.parent))
				mouseclick = False
			elif clicked.text == 'Colours':
				self.windows.append(ColorWheelWindow(100,300,280,260,self.parent))
			elif clicked.text == 'New':		
				newdoc =pygame.Surface((640,480))
				newdoc.fill((255,255,255))
				self.windows.append(CanvasWindow(300,300,self.parent,newdoc))
			elif clicked.text == 'Open':
				self.windows.append(FileBrowser(0,0,600,400,self.parent))
			elif clicked.text == 'Answer this':
				self.windows.append(Ask("Favourite Color?", self.parent, self.answergoeshere))