ui.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import tkinter as tk
  2. from lib.scrollable_frame import VerticalScrolledFrame
  3. from sccd.realtime.threads_platform import ThreadsPlatform
  4. class ChatWindowGUI(tk.Tk):
  5. def __init__(self):
  6. tk.Tk.__init__(self)
  7. self.resizable(width=tk.FALSE, height=tk.FALSE)
  8. self.width = 230
  9. self.height = 100
  10. self.labelwidth = 30
  11. self.frame = tk.Frame(self)
  12. self.frame.focus_set()
  13. self.chat_field = VerticalScrolledFrame(self.frame, bd='2', height=self.height, width=self.width, relief=tk.RIDGE)
  14. tk.Label(self.chat_field.interior, text='SCCD Chat Client -- Tk version', justify=tk.LEFT, anchor=tk.NW, width=self.labelwidth).pack()
  15. self.tk_buffer = tk.StringVar()
  16. input_frame = tk.Frame(self.frame, bd='2', height=100, width=self.width, relief=tk.RIDGE)
  17. self.input_text = tk.Label(input_frame, textvar=self.tk_buffer, anchor=tk.NW, justify=tk.LEFT, wraplength=self.width, width=self.labelwidth, background='grey')
  18. self.chat_field.pack(anchor=tk.NW)
  19. input_frame.pack(anchor=tk.NW, fill=tk.X)
  20. self.input_text.pack(anchor=tk.NW, fill=tk.X)
  21. self.frame.pack(anchor=tk.NW)
  22. def redraw_buffer(self, text):
  23. self.tk_buffer.set(text)
  24. def setColor(self, color):
  25. self.input_text.configure(background=color)
  26. def addMessage(self, msg, color):
  27. tk.Label(self.chat_field.interior, text=msg, anchor=tk.NW, justify=tk.LEFT, foreground=color, wraplength=230, width=30).pack(anchor=tk.NW)
  28. colors = {'info': 'black', 'local_message': 'red', 'remote_message': 'blue', 'warning': 'yellow'}
  29. window = ChatWindowGUI()
  30. buf = ""
  31. def init(eventloop):
  32. global window
  33. def on_key_press(key):
  34. eventloop.add_input_now(port="ui", event_name="input", params=[key.char])
  35. window.bind('<Key>', on_key_press)
  36. # window = ChatWindowGUI(on_key_press)
  37. # shows the message in the chat window, with the specified type (as a string, either "info", "local_message", or "remote_message"))
  38. def add_message(msg: str, type: str):
  39. window.addMessage(msg, colors[type])
  40. # adds a string to the input field and visualizes the change
  41. def append_to_buffer(char: str):
  42. global buf
  43. buf += char
  44. window.redraw_buffer(buf)
  45. # removes the final character from the input field and visualizes the change
  46. def remove_last_in_buffer():
  47. global buf
  48. buf = buf[:-1]
  49. window.redraw_buffer(buf)
  50. # clears the input field and visualizes the change
  51. def clear_input():
  52. global buf
  53. buf = ""
  54. window.redraw_buffer(buf)
  55. # color the input field in the 'join' mode
  56. def input_join():
  57. window.setColor('green')
  58. # color the input field in the 'message' mode
  59. def input_msg():
  60. window.setColor('white')
  61. # color the input field in the 'command' mode
  62. def input_command():
  63. window.setColor('grey')
  64. # returns the current content of the input field
  65. def get_buffer() -> str:
  66. return buf
  67. from sccd.action_lang.static.types import *
  68. SCCD_EXPORTS = {
  69. "add_message": (add_message, SCCDFunction([SCCDString, SCCDString])),
  70. "append_to_buffer": (append_to_buffer, SCCDFunction([SCCDString])),
  71. "remove_last_in_buffer": (remove_last_in_buffer, SCCDFunction([])),
  72. "clear_input": (clear_input, SCCDFunction([])),
  73. "input_join": (input_join, SCCDFunction([])),
  74. "input_msg": (input_msg, SCCDFunction([])),
  75. "input_command": (input_command, SCCDFunction([])),
  76. "get_buffer": (get_buffer, SCCDFunction([], SCCDString)),
  77. }