Position the panel

master
En Yi 2019-06-13 17:34:25 +01:00
parent d1a4c396e2
commit 016a39a466
3 changed files with 80 additions and 24 deletions

64
UI.py
View File

@ -13,6 +13,7 @@ class GenericUI:
self.visible = True
self.clear_colour = (0, 0, 0)
self.outline_colour = (255, 0, 0)
self.outline_thickness = 3
self.text_colour = (255, 255, 255)
self.background = pygame.Surface((self.width, self.height))
@ -27,21 +28,26 @@ class GenericUI:
self.children = None
def process_events(self, event):
draw_update = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
if self.hold_function and self.collide_at(mouse_pos):
self.hold_function(mouse_pos)
draw_update = True
if event.type == pygame.MOUSEBUTTONUP:
mouse_pos = pygame.mouse.get_pos()
if self.release_function and self.collide_at(mouse_pos):
mouse_pos = pygame.mouse.get_pos()
if event.button == 1:
print('mouse click')
self.release_function(mouse_pos)
draw_update = True
return draw_update
def collide_at(self, pos):
x0, y0 = self.get_offset_pos()
print(x0, y0, pos)
rect_check = pygame.rect.Rect(x0, y0, self.rect.width, self.rect.height)
return rect_check.collidepoint(pos)
@ -64,6 +70,7 @@ class GenericUI:
self.rect.x = x
self.rect.y = y
class TextBox(GenericUI):
def __init__(self, x, y, width, height, text='Button', text_size=25):
super().__init__(x, y, width, height)
@ -170,14 +177,17 @@ class ScrollList(GenericUI):
i += 1
def process_events(self, event):
super().process_events(event)
draw_update = super().process_events(event)
if event.type == pygame.MOUSEBUTTONUP:
mouse_pos = pygame.mouse.get_pos()
if self.collide_at(mouse_pos):
if event.button == 4:
self.scroll_up()
draw_update = True
if event.button == 5:
self.scroll_down()
draw_update = True
return draw_update
def offset_text_rects(self, offset):
prev_offset = self.y_offset
@ -203,8 +213,9 @@ class ScrollList(GenericUI):
def check_click_pos(self, *args):
pos = args[0]
relative_pos_x = pos[0] - self.x
relative_pos_y = pos[1] - self.y
x0, y0 = self.get_offset_pos()
relative_pos_x = pos[0] - x0
relative_pos_y = pos[1] - y0
mouse_pos = (relative_pos_x, relative_pos_y)
for i, rect in enumerate(self.text_rects):
if rect.collidepoint(mouse_pos):
@ -272,6 +283,7 @@ class CallPanel(GenericUI):
def __init__(self, x, y, width, height):
super().__init__(x, y, width, height)
self.background.set_colorkey(None)
self.confirm_output = Signal(args=['output'])
self.text_size = 20
@ -299,7 +311,7 @@ class CallPanel(GenericUI):
self.output_box = TextBox(margins+width_spacings*3+ui_width*2, margins+height_spacings,
ui_width, ui_height, text_size=self.text_size)
self.confirm_button = Button(margins+width_spacings*3+ui_width*2, margins+height_spacings*2,
self.confirm_button = Button(margins+width_spacings*3+ui_width*2, margins+height_spacings*2+ui_height,
ui_width, ui_height, text='OK', text_size=self.text_size)
self.confirm_button.clicked.connect(self.emit_output)
@ -312,13 +324,23 @@ class CallPanel(GenericUI):
def redraw(self):
super().redraw()
for element in self.children:
self.background.blit(element.background, element.get_pos())
#self.background.fill((255,0,255))
if self.visible:
outline = (0, 0, self.rect.w, self.rect.h)
pygame.draw.rect(self.background, self.outline_colour, outline, self.outline_thickness)
for element in self.children:
self.background.blit(element.background, element.get_pos())
def process_events(self, event):
draw_update = False
for element in self.children:
element.process_events(event)
self.redraw()
if element.process_events(event):
draw_update = True
if draw_update:
self.redraw()
return draw_update
def print_list_selection(self, text, num,**kwargs):
self.output_text[num] = text
@ -337,7 +359,7 @@ class TestScreen(view.PygView):
self.scroll_menu = ScrollList(100, 100, 100, 200, texts=texts)
self.button = Button(300, 100, 50, 25, text_size=18)
self.textbox = TextBox(300, 250, 200, 100, text="Test")
self.panel = CallPanel(0, 0, 300, 150)
self.panel = CallPanel(100, 100, 300, 150)
self.panel.confirm_output.connect(self.print_panel_output)
#[self.scroll_menu, self.button, self.textbox]
@ -347,6 +369,11 @@ class TestScreen(view.PygView):
self.left_mouse_down = False
self.double_click_event = pygame.USEREVENT + 1
self.draw_function()
pygame.display.flip()
self.screen.blit(self.background, (0, 0))
def draw_function(self):
for element in self.elements:
self.screen.blit(element.background, element.get_pos())
@ -358,21 +385,17 @@ class TestScreen(view.PygView):
def run(self):
running = True
while running:
draw_update = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_o:
self.scroll_menu.add_item(str('A'))
if event.key == pygame.K_p:
self.scroll_menu.remove_item(0)
if event.key == pygame.K_i:
self.scroll_menu.replace_list(['a','b','c'])
for element in self.elements:
element.process_events(event)
if element.process_events(event):
draw_update = True
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
@ -390,10 +413,11 @@ class TestScreen(view.PygView):
self.double_clicking = False
print('double click disabled')
self.draw_function()
if draw_update:
self.draw_function()
pygame.display.flip()
self.screen.blit(self.background, (0, 0))
pygame.display.flip()
self.screen.blit(self.background, (0, 0))
pygame.quit()

11
main.py
View File

@ -35,6 +35,9 @@ class GameScreen(view.PygView):
self.screen.blit(stats_line, (self.table.player_stats_x[i],
self.table.player_stats_y[i]+self.table.stats_height*j/3))
if self.table.calling_panel.visible:
self.screen.blit(self.table.calling_panel.background, self.table.calling_panel.get_pos())
pygame.display.flip()
@ -48,7 +51,7 @@ class GameScreen(view.PygView):
self.running = True
#self.player_thread.start()
while self.running:
draw_update = False
all_events = pygame.event.get()
for event in all_events:
if event.type == pygame.QUIT:
@ -59,9 +62,11 @@ class GameScreen(view.PygView):
if event.key == pygame.K_p:
if not self.table.ongoing:
self.table.ongoing = True
self.table.process_panel(event)
if self.table.ongoing:
self.table.continue_game(all_events)
#if not self.player_commands.empty():
# player_cmd = self.player_commands.get()
# print("Player Command Received: " + player_cmd)
@ -95,7 +100,7 @@ if __name__ == '__main__':
with open('last_game_rng.rng', 'wb') as f:
pickle.dump(rng_state, f)
main_view = GameScreen(800, 600, clear_colour=(255, 0, 0),
main_view = GameScreen(1000, 700, clear_colour=(255, 0, 0),
autoplay=AUTOPLAY, view_all_cards=VIEW_ALL_CARDS)
main_view.run()

View File

@ -1,4 +1,5 @@
import pygame
import UI
import cards
import players
import view
@ -127,7 +128,7 @@ class Table:
self.player_stats_y = (playdecky[0] + w_deck - self.stats_height,
playdecky[1] - self.stats_height - stats_spacing,
playdecky[2],
playdecky[3] + w_deck + stats_spacing)
playdecky[3] - w_deck - stats_spacing)
self.player_stats = [[], [], [], []]
@ -191,6 +192,18 @@ class Table:
self.ongoing = False
self.require_player_input = False
self.calling_panel = UI.CallPanel(playdeckx[0]+w_deck+5,playdecky[0]+w_deck-140,
250, 140)
self.calling_panel.parent = self
self.parent = None
def get_offset_pos(self):
x, y = 0, 0
if self.parent:
x, y = self.parent.get_offset_pos()
return x+self.x, y+self.y
def center_text_on_surface(self, surf, rendered_text, clear_colour):
line_center = surf.get_rect().center
text_rect = rendered_text.get_rect(center=line_center)
@ -280,6 +293,20 @@ class Table:
def get_pos(self):
return self.x, self.y
def process_panel(self, event):
draw_update = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_o:
self.calling_panel.visible = not self.calling_panel.visible
draw_update = True
if self.calling_panel.visible and \
self.calling_panel.process_events(event):
draw_update = True
if draw_update:
self.update_table.emit()
def continue_game(self, game_events):
"""
This is where the FSM is. State transition should occur here.