Position the panel
parent
d1a4c396e2
commit
016a39a466
52
UI.py
52
UI.py
|
@ -13,6 +13,7 @@ class GenericUI:
|
||||||
self.visible = True
|
self.visible = True
|
||||||
self.clear_colour = (0, 0, 0)
|
self.clear_colour = (0, 0, 0)
|
||||||
self.outline_colour = (255, 0, 0)
|
self.outline_colour = (255, 0, 0)
|
||||||
|
self.outline_thickness = 3
|
||||||
self.text_colour = (255, 255, 255)
|
self.text_colour = (255, 255, 255)
|
||||||
|
|
||||||
self.background = pygame.Surface((self.width, self.height))
|
self.background = pygame.Surface((self.width, self.height))
|
||||||
|
@ -27,21 +28,26 @@ class GenericUI:
|
||||||
self.children = None
|
self.children = None
|
||||||
|
|
||||||
def process_events(self, event):
|
def process_events(self, event):
|
||||||
|
draw_update = False
|
||||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
mouse_pos = pygame.mouse.get_pos()
|
mouse_pos = pygame.mouse.get_pos()
|
||||||
if self.hold_function and self.collide_at(mouse_pos):
|
if self.hold_function and self.collide_at(mouse_pos):
|
||||||
self.hold_function(mouse_pos)
|
self.hold_function(mouse_pos)
|
||||||
|
draw_update = True
|
||||||
|
|
||||||
if event.type == pygame.MOUSEBUTTONUP:
|
if event.type == pygame.MOUSEBUTTONUP:
|
||||||
mouse_pos = pygame.mouse.get_pos()
|
mouse_pos = pygame.mouse.get_pos()
|
||||||
if self.release_function and self.collide_at(mouse_pos):
|
if self.release_function and self.collide_at(mouse_pos):
|
||||||
mouse_pos = pygame.mouse.get_pos()
|
|
||||||
if event.button == 1:
|
if event.button == 1:
|
||||||
print('mouse click')
|
print('mouse click')
|
||||||
self.release_function(mouse_pos)
|
self.release_function(mouse_pos)
|
||||||
|
draw_update = True
|
||||||
|
|
||||||
|
return draw_update
|
||||||
|
|
||||||
def collide_at(self, pos):
|
def collide_at(self, pos):
|
||||||
x0, y0 = self.get_offset_pos()
|
x0, y0 = self.get_offset_pos()
|
||||||
|
print(x0, y0, pos)
|
||||||
rect_check = pygame.rect.Rect(x0, y0, self.rect.width, self.rect.height)
|
rect_check = pygame.rect.Rect(x0, y0, self.rect.width, self.rect.height)
|
||||||
return rect_check.collidepoint(pos)
|
return rect_check.collidepoint(pos)
|
||||||
|
|
||||||
|
@ -64,6 +70,7 @@ class GenericUI:
|
||||||
self.rect.x = x
|
self.rect.x = x
|
||||||
self.rect.y = y
|
self.rect.y = y
|
||||||
|
|
||||||
|
|
||||||
class TextBox(GenericUI):
|
class TextBox(GenericUI):
|
||||||
def __init__(self, x, y, width, height, text='Button', text_size=25):
|
def __init__(self, x, y, width, height, text='Button', text_size=25):
|
||||||
super().__init__(x, y, width, height)
|
super().__init__(x, y, width, height)
|
||||||
|
@ -170,14 +177,17 @@ class ScrollList(GenericUI):
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
def process_events(self, event):
|
def process_events(self, event):
|
||||||
super().process_events(event)
|
draw_update = super().process_events(event)
|
||||||
if event.type == pygame.MOUSEBUTTONUP:
|
if event.type == pygame.MOUSEBUTTONUP:
|
||||||
mouse_pos = pygame.mouse.get_pos()
|
mouse_pos = pygame.mouse.get_pos()
|
||||||
if self.collide_at(mouse_pos):
|
if self.collide_at(mouse_pos):
|
||||||
if event.button == 4:
|
if event.button == 4:
|
||||||
self.scroll_up()
|
self.scroll_up()
|
||||||
|
draw_update = True
|
||||||
if event.button == 5:
|
if event.button == 5:
|
||||||
self.scroll_down()
|
self.scroll_down()
|
||||||
|
draw_update = True
|
||||||
|
return draw_update
|
||||||
|
|
||||||
def offset_text_rects(self, offset):
|
def offset_text_rects(self, offset):
|
||||||
prev_offset = self.y_offset
|
prev_offset = self.y_offset
|
||||||
|
@ -203,8 +213,9 @@ class ScrollList(GenericUI):
|
||||||
|
|
||||||
def check_click_pos(self, *args):
|
def check_click_pos(self, *args):
|
||||||
pos = args[0]
|
pos = args[0]
|
||||||
relative_pos_x = pos[0] - self.x
|
x0, y0 = self.get_offset_pos()
|
||||||
relative_pos_y = pos[1] - self.y
|
relative_pos_x = pos[0] - x0
|
||||||
|
relative_pos_y = pos[1] - y0
|
||||||
mouse_pos = (relative_pos_x, relative_pos_y)
|
mouse_pos = (relative_pos_x, relative_pos_y)
|
||||||
for i, rect in enumerate(self.text_rects):
|
for i, rect in enumerate(self.text_rects):
|
||||||
if rect.collidepoint(mouse_pos):
|
if rect.collidepoint(mouse_pos):
|
||||||
|
@ -272,6 +283,7 @@ class CallPanel(GenericUI):
|
||||||
|
|
||||||
def __init__(self, x, y, width, height):
|
def __init__(self, x, y, width, height):
|
||||||
super().__init__(x, y, width, height)
|
super().__init__(x, y, width, height)
|
||||||
|
self.background.set_colorkey(None)
|
||||||
self.confirm_output = Signal(args=['output'])
|
self.confirm_output = Signal(args=['output'])
|
||||||
|
|
||||||
self.text_size = 20
|
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,
|
self.output_box = TextBox(margins+width_spacings*3+ui_width*2, margins+height_spacings,
|
||||||
ui_width, ui_height, text_size=self.text_size)
|
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)
|
ui_width, ui_height, text='OK', text_size=self.text_size)
|
||||||
self.confirm_button.clicked.connect(self.emit_output)
|
self.confirm_button.clicked.connect(self.emit_output)
|
||||||
|
|
||||||
|
@ -312,13 +324,23 @@ class CallPanel(GenericUI):
|
||||||
|
|
||||||
def redraw(self):
|
def redraw(self):
|
||||||
super().redraw()
|
super().redraw()
|
||||||
|
#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:
|
for element in self.children:
|
||||||
self.background.blit(element.background, element.get_pos())
|
self.background.blit(element.background, element.get_pos())
|
||||||
|
|
||||||
def process_events(self, event):
|
def process_events(self, event):
|
||||||
|
draw_update = False
|
||||||
for element in self.children:
|
for element in self.children:
|
||||||
element.process_events(event)
|
if element.process_events(event):
|
||||||
|
draw_update = True
|
||||||
|
|
||||||
|
if draw_update:
|
||||||
self.redraw()
|
self.redraw()
|
||||||
|
return draw_update
|
||||||
|
|
||||||
def print_list_selection(self, text, num,**kwargs):
|
def print_list_selection(self, text, num,**kwargs):
|
||||||
self.output_text[num] = text
|
self.output_text[num] = text
|
||||||
|
@ -337,7 +359,7 @@ class TestScreen(view.PygView):
|
||||||
self.scroll_menu = ScrollList(100, 100, 100, 200, texts=texts)
|
self.scroll_menu = ScrollList(100, 100, 100, 200, texts=texts)
|
||||||
self.button = Button(300, 100, 50, 25, text_size=18)
|
self.button = Button(300, 100, 50, 25, text_size=18)
|
||||||
self.textbox = TextBox(300, 250, 200, 100, text="Test")
|
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.panel.confirm_output.connect(self.print_panel_output)
|
||||||
|
|
||||||
#[self.scroll_menu, self.button, self.textbox]
|
#[self.scroll_menu, self.button, self.textbox]
|
||||||
|
@ -347,6 +369,11 @@ class TestScreen(view.PygView):
|
||||||
self.left_mouse_down = False
|
self.left_mouse_down = False
|
||||||
self.double_click_event = pygame.USEREVENT + 1
|
self.double_click_event = pygame.USEREVENT + 1
|
||||||
|
|
||||||
|
self.draw_function()
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
self.screen.blit(self.background, (0, 0))
|
||||||
|
|
||||||
def draw_function(self):
|
def draw_function(self):
|
||||||
for element in self.elements:
|
for element in self.elements:
|
||||||
self.screen.blit(element.background, element.get_pos())
|
self.screen.blit(element.background, element.get_pos())
|
||||||
|
@ -358,21 +385,17 @@ class TestScreen(view.PygView):
|
||||||
def run(self):
|
def run(self):
|
||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
|
draw_update = False
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
running = False
|
running = False
|
||||||
elif event.type == pygame.KEYDOWN:
|
elif event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_ESCAPE:
|
if event.key == pygame.K_ESCAPE:
|
||||||
running = False
|
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:
|
for element in self.elements:
|
||||||
element.process_events(event)
|
if element.process_events(event):
|
||||||
|
draw_update = True
|
||||||
|
|
||||||
if event.type == pygame.MOUSEBUTTONUP:
|
if event.type == pygame.MOUSEBUTTONUP:
|
||||||
if event.button == 1:
|
if event.button == 1:
|
||||||
|
@ -390,6 +413,7 @@ class TestScreen(view.PygView):
|
||||||
self.double_clicking = False
|
self.double_clicking = False
|
||||||
print('double click disabled')
|
print('double click disabled')
|
||||||
|
|
||||||
|
if draw_update:
|
||||||
self.draw_function()
|
self.draw_function()
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
11
main.py
11
main.py
|
@ -35,6 +35,9 @@ class GameScreen(view.PygView):
|
||||||
self.screen.blit(stats_line, (self.table.player_stats_x[i],
|
self.screen.blit(stats_line, (self.table.player_stats_x[i],
|
||||||
self.table.player_stats_y[i]+self.table.stats_height*j/3))
|
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()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
|
||||||
|
@ -48,7 +51,7 @@ class GameScreen(view.PygView):
|
||||||
self.running = True
|
self.running = True
|
||||||
#self.player_thread.start()
|
#self.player_thread.start()
|
||||||
while self.running:
|
while self.running:
|
||||||
|
draw_update = False
|
||||||
all_events = pygame.event.get()
|
all_events = pygame.event.get()
|
||||||
for event in all_events:
|
for event in all_events:
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
|
@ -59,9 +62,11 @@ class GameScreen(view.PygView):
|
||||||
if event.key == pygame.K_p:
|
if event.key == pygame.K_p:
|
||||||
if not self.table.ongoing:
|
if not self.table.ongoing:
|
||||||
self.table.ongoing = True
|
self.table.ongoing = True
|
||||||
|
self.table.process_panel(event)
|
||||||
if self.table.ongoing:
|
if self.table.ongoing:
|
||||||
self.table.continue_game(all_events)
|
self.table.continue_game(all_events)
|
||||||
|
|
||||||
|
|
||||||
#if not self.player_commands.empty():
|
#if not self.player_commands.empty():
|
||||||
# player_cmd = self.player_commands.get()
|
# player_cmd = self.player_commands.get()
|
||||||
# print("Player Command Received: " + player_cmd)
|
# print("Player Command Received: " + player_cmd)
|
||||||
|
@ -95,7 +100,7 @@ if __name__ == '__main__':
|
||||||
with open('last_game_rng.rng', 'wb') as f:
|
with open('last_game_rng.rng', 'wb') as f:
|
||||||
pickle.dump(rng_state, 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)
|
autoplay=AUTOPLAY, view_all_cards=VIEW_ALL_CARDS)
|
||||||
|
|
||||||
main_view.run()
|
main_view.run()
|
29
table.py
29
table.py
|
@ -1,4 +1,5 @@
|
||||||
import pygame
|
import pygame
|
||||||
|
import UI
|
||||||
import cards
|
import cards
|
||||||
import players
|
import players
|
||||||
import view
|
import view
|
||||||
|
@ -127,7 +128,7 @@ class Table:
|
||||||
self.player_stats_y = (playdecky[0] + w_deck - self.stats_height,
|
self.player_stats_y = (playdecky[0] + w_deck - self.stats_height,
|
||||||
playdecky[1] - self.stats_height - stats_spacing,
|
playdecky[1] - self.stats_height - stats_spacing,
|
||||||
playdecky[2],
|
playdecky[2],
|
||||||
playdecky[3] + w_deck + stats_spacing)
|
playdecky[3] - w_deck - stats_spacing)
|
||||||
|
|
||||||
self.player_stats = [[], [], [], []]
|
self.player_stats = [[], [], [], []]
|
||||||
|
|
||||||
|
@ -191,6 +192,18 @@ class Table:
|
||||||
self.ongoing = False
|
self.ongoing = False
|
||||||
self.require_player_input = 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):
|
def center_text_on_surface(self, surf, rendered_text, clear_colour):
|
||||||
line_center = surf.get_rect().center
|
line_center = surf.get_rect().center
|
||||||
text_rect = rendered_text.get_rect(center=line_center)
|
text_rect = rendered_text.get_rect(center=line_center)
|
||||||
|
@ -280,6 +293,20 @@ class Table:
|
||||||
def get_pos(self):
|
def get_pos(self):
|
||||||
return self.x, self.y
|
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):
|
def continue_game(self, game_events):
|
||||||
"""
|
"""
|
||||||
This is where the FSM is. State transition should occur here.
|
This is where the FSM is. State transition should occur here.
|
||||||
|
|
Loading…
Reference in New Issue