Skip to content

Commit

Permalink
feat(UI-UX): set focus and bin enter keys
Browse files Browse the repository at this point in the history
  • Loading branch information
HOOK-Hawkins committed Mar 17, 2024
1 parent 5412d06 commit 09a5f89
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
9 changes: 7 additions & 2 deletions UI/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def __init__(self, master, root, chat='', *args, **kwargs):
self.root = root
self._create_widgets(chat)
self._display_widgets()
self.root.bind('<Return>', lambda *args: self._enter())
self.root.network_events['chat/receive'] = self._chat_receive

def _create_widgets(self, chat):
Expand All @@ -29,7 +30,7 @@ def _display_widgets(self):
self.btnSend.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=1, row=1, columnspan=2)

def _send(self):
self.root.out_queue.values[0].put({'message_type': 'chat/message', 'args' : {'message': self.etrMessage.val}})
list(self.root.out_queue.values())[0].put({'message_type': 'chat/message', 'args' : {'message': self.etrMessage.val}})
self.etrMessage.val = ""

def _chat_receive(self, queue):
Expand All @@ -38,4 +39,8 @@ def _chat_receive(self, queue):
self.txtChat.config(state=tk.DISABLED)

def on_destroy(self):
del self.master.network_events['chat/receive']
del self.master.network_events['chat/receive']

def _enter(self):
if(self.focus_get() == self.etrMessage.widget):
self._send()
13 changes: 8 additions & 5 deletions UI/field_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def error(self, queue, *args):
class Field(base_frame):
def __init__(self, master, chat, *args, starting_player, starting_player_symbol, opponent, opponent_symbol, **kwargs):
super().__init__(master)
self._create_widgets(chat)
self._create_widgets(chat, display_chat=len(kwargs)==1)
self.controller = field_controller(self, [starting_player, opponent], starting_player.uuid, **kwargs)
self._display_widgets()
print("wigets displayed")
Expand All @@ -90,24 +90,27 @@ def __init__(self, master, chat, *args, starting_player, starting_player_symbol,
self.master.network_events['game/error'] = self.controller.error
self.master.out_queue[starting_player.uuid].put({'message_type': 'game/gamestate', 'args' :{} })

def _create_widgets(self, chat):
def _create_widgets(self, chat, display_chat=True):
self.heading = tk.Label(self, text="Tic Tac Toe Kojote", font=self.master.title_font)
self.player = []
self.player.append(player(self, 1))
self.player.append(player(self, 2))
self.gamefield = gamefield(self)
self.chat = Chat(self, self.master, chat)
if(display_chat):
self.chat = Chat(self, self.master, chat)
self.close = tk.Button(self, text="close")

def _display_widgets(self):
self.columnconfigure([1,3], weight=1)
self.columnconfigure([1], weight=1)
self.rowconfigure(2, weight=1)

self.heading.grid(row=0, column=0, columnspan=3)
self.player[0].grid(row=1, column=0)
self.player[1].grid(row=1, column=2)
self.gamefield.grid(sticky=tk.N+tk.S+tk.E+tk.W, row=2, column=1)
self.chat.grid(sticky=tk.N+tk.S+tk.E+tk.W, row=1, column=3, rowspan=3)
if(hasattr(self, 'chat')):
self.columnconfigure(3, weight=1)
self.chat.grid(sticky=tk.N+tk.S+tk.E+tk.W, row=1, column=3, rowspan=3)
self.close.grid(row=3, column=2)

def on_destroy(self):
Expand Down
80 changes: 42 additions & 38 deletions UI/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ def _display_widgets(self):
# display the buttons created in the _create_widgets method
self.lblTitle.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=2, row=2, columnspan=3)
self.btnRdy.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=2, row=10)
if hasattr(self, 'btnRdy2'):
self.btnRdy2.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=2, row=10)
self.chat.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=4, row=4, columnspan=2, rowspan=7)
self.btnExit.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=5, row=1)

Expand Down Expand Up @@ -83,42 +81,6 @@ def on_destroy(self):
del self.master.network_events['lobby/status']
del self.master.network_events['game/start']

class Lobby_Overview(tk.Container):
def __init__(self, master):
super().__init__(master)
self._create_widgets()
self._display_widgets()

def _create_widgets(self):
self.frame = tk.Frame(self)
self.innerframe = self.frame.widget
self.lblHeading = tk.Label(self.innerframe, text="Join public lobbies", font=self.master.master.title_font)

self.btnManual = tk.Button(self.innerframe, text="Join by address", command=lambda *args: self.manually())
self.etrAddress = tk.Entry(self.innerframe)
self.btnConnect = tk.Button(self.innerframe, text="Connect", command=lambda *args: self._connect())

def _display_widgets(self):
self.frame.pack(expand=True, fill=tk.BOTH)
self.innerframe.columnconfigure([0, 2, 4], weight=1)
self.innerframe.columnconfigure([1, 3], weight=5)
self.innerframe.rowconfigure([0, 4], weight=2)
self.innerframe.rowconfigure([1, 3], weight=1)
self.innerframe.rowconfigure([2], weight=40)
self.lblHeading.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=1, row=0, columnspan=3)
self.btnManual.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=1, row=4, columnspan=3)

def manually(self):
self.btnManual.grid_forget()
self.etrAddress.grid(column=1, row=10, sticky=tk.E+tk.W+tk.N+tk.S)
self.btnConnect.grid(column=3, row=10, sticky=tk.E+tk.W+tk.N+tk.S)

def _connect(self):
root = self.master.master
root.out_queue = {root.players[root.player].uuid: Queue()}
root.network_client = client_thread(root, in_queue=list(root.out_queue.values())[0], out_queue=root.in_queue, player=root.players[root.player], ip=self.etrAddress.get())
root.show(Join, local_players=[root.players[root.player].uuid])

class LocalProfileSelection(base_frame):
def __init__(self, master, *args, **kwargs):
super().__init__(master)
Expand Down Expand Up @@ -171,6 +133,48 @@ def _start_game(self):
self.master.out_queue[player2.uuid].put({'message_type': 'lobby/ready', 'args' : {'ready': True}})
self.master.show(Join, opponent=player_type.local, local_players=[player1.uuid, player2.uuid])

class Lobby_Overview(tk.Container):
def __init__(self, master):
super().__init__(master)
self._create_widgets()
self._display_widgets()

def _create_widgets(self):
self.frame = tk.Frame(self)
self.innerframe = self.frame.widget
self.lblHeading = tk.Label(self.innerframe, text="Join public lobbies", font=self.master.master.title_font)

self.btnManual = tk.Button(self.innerframe, text="Join by address", command=lambda *args: self.manually())
self.etrAddress = tk.Entry(self.innerframe)
self.btnConnect = tk.Button(self.innerframe, text="Connect", command=lambda *args: self._connect())
self.master.master.bind('<Return>', lambda *args: self._enter())

def _display_widgets(self):
self.frame.pack(expand=True, fill=tk.BOTH)
self.innerframe.columnconfigure([0, 2, 4], weight=1)
self.innerframe.columnconfigure([1, 3], weight=5)
self.innerframe.rowconfigure([0, 4], weight=2)
self.innerframe.rowconfigure([1, 3], weight=1)
self.innerframe.rowconfigure([2], weight=40)
self.lblHeading.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=1, row=0, columnspan=3)
self.btnManual.grid(sticky=tk.E+tk.W+tk.N+tk.S, column=1, row=4, columnspan=3)

def manually(self):
self.btnManual.grid_forget()
self.etrAddress.grid(column=1, row=10, sticky=tk.E+tk.W+tk.N+tk.S)
self.btnConnect.grid(column=3, row=10, sticky=tk.E+tk.W+tk.N+tk.S)
self.etrAddress.focus_set()

def _enter(self):
if(self.focus_get() == self.etrAddress.widget):
self._connect()

def _connect(self):
root = self.master.master
root.out_queue = {root.players[root.player].uuid: Queue()}
root.network_client = client_thread(root, in_queue=list(root.out_queue.values())[0], out_queue=root.in_queue, player=root.players[root.player], ip=self.etrAddress.get())
root.show(Join, local_players=[root.players[root.player].uuid])

class Multiplayer(base_frame):
def __new__(cls, master, *args, **kwargs):
if(len(master.players) == 0 or master.player == None):
Expand Down

0 comments on commit 09a5f89

Please sign in to comment.