1import fsui
2from launcher.i18n import gettext
3from launcher.netplay.irc import LOBBY_CHANNEL
4from launcher.netplay.irc_broadcaster import IRCBroadcaster
5from launcher.netplay.netplay import Netplay
6from launcher.ui.skin import Skin
7
8
9class NetplayPanel(fsui.Panel):
10    def __init__(self, parent, header=True):
11        fsui.Panel.__init__(self, parent)
12        Skin.set_background_color(self)
13        self.layout = fsui.VerticalLayout()
14
15        if header:
16            hori_layout = fsui.HorizontalLayout()
17            self.layout.add(hori_layout, fill=True)
18            self.layout.add_spacer(0)
19
20            label = fsui.HeadingLabel(self, gettext("Net Play"))
21            hori_layout.add(label, margin=10)
22
23            hori_layout.add_spacer(0, expand=True)
24
25        # label = fsui.Label(self, "Netplay is currently disabled in the "
26        #                          "development versions.")
27        # self.layout.add(label, margin=10)
28        # label = fsui.Label(self, "Please use the stable FS-UAE series for "
29        #                          "netplay in the meantime.")
30        # self.layout.add(label, margin=10)
31        # return
32
33        # TODO
34        gettext("Nick:")
35        gettext("Connect")
36        gettext("Disconnect")
37
38        # self.nick_label = fsui.Label(self, _("Nick:"))
39        # hori_layout.add(self.nick_label,
40        #         margin=10, margin_top=0, margin_bottom=0)
41        #
42        # self.nick_field = fsui.TextField(self, Settings.get("irc_nick"))
43        # self.nick_field.set_min_width(130)
44        # hori_layout.add(self.nick_field, margin_right=10)
45        # #self.nick_field.on_changed = self.on_nick_change
46        #
47        # self.connect_button = fsui.Button(self, _("Connect"))
48        # hori_layout.add(self.connect_button, margin_right=10)
49        # #self.connect_button.activated.connect(self.on_connect_button)
50        #
51        # self.disconnect_button = fsui.Button(self, _("Disconnect"))
52        # hori_layout.add(self.disconnect_button, margin_right=10)
53        # #self.disconnect_button.activated.connect(self.on_disconnect_button)
54
55        hori_layout = fsui.HorizontalLayout()
56        self.layout.add(hori_layout, fill=True, expand=True)
57
58        ver_layout = fsui.VerticalLayout()
59        hori_layout.add(ver_layout, fill=True)
60        self.channel_list = fsui.ListView(self)
61        self.channel_list.set_min_width(212)
62        self.channel_list.on_select_item = self.on_select_channel
63        ver_layout.add(self.channel_list, fill=True, expand=True, margin=10)
64        self.nick_list = fsui.ListView(self)
65        ver_layout.add(self.nick_list, fill=True, expand=True, margin=10)
66
67        self.text_area = fsui.TextArea(self, font_family="monospace")
68        hori_layout.add(
69            self.text_area, fill=True, expand=True, margin=10, margin_left=0
70        )
71
72        self.input_field = fsui.TextField(self)
73        self.input_field.activated.connect(self.on_input)
74        self.layout.add(self.input_field, fill=True, margin=10, margin_top=0)
75
76        self.active_channel = LOBBY_CHANNEL
77
78        self.input_field.focus()
79
80        self.netplay = Netplay()
81        IRCBroadcaster.add_listener(self)
82
83    def on_destroy(self):
84        print("NetplayPanel.on_destroy")
85        IRCBroadcaster.remove_listener(self)
86        self.netplay.disconnect()
87
88    def on_show(self):
89        # FIXME: currently disabled
90        # return
91        if not self.netplay.is_connected():
92            self.netplay.connect()
93        self.input_field.focus()
94
95    def on_select_channel(self, index):
96        # index = self.channel_list.get_index()
97        # if index == 0:
98        #     channel = ""
99        # else:
100        # assert index is not None
101        if index is None:
102            return
103        channel = self.channel_list.get_item(index)
104        self.netplay.irc.set_active_channel_name(channel)
105        self.input_field.focus()
106
107    def on_input(self):
108        command = self.input_field.get_text().strip()
109        if not command:
110            return
111        if self.netplay.handle_command(command):
112            pass
113        else:
114            self.netplay.irc.handle_command(command)
115        self.input_field.set_text("")
116
117    def set_active_channel(self, channel):
118        if channel == self.active_channel:
119            return
120        self.text_area.set_text("")
121        # self.text_area.append_text(IRC.channel(channel).get_text())
122        ch = self.netplay.irc.channel(channel)
123        for i, line in enumerate(ch.lines):
124            self.text_area.append_text(line, ch.colors[i])
125        self.active_channel = channel
126        self.update_nick_list()
127        for i in range(self.channel_list.get_item_count()):
128            if self.channel_list.get_item(i) == channel:
129                self.channel_list.set_index(i)
130
131    def update_channel_list(self):
132        items = sorted(self.netplay.irc.channels.keys())
133        # items[0] = "IRC ({0})".format(Settings.get_irc_server())
134        # items[0] = Settings.get_irc_server()
135        self.channel_list.set_items(items)
136
137    def update_nick_list(self):
138        items = self.netplay.irc.channel(self.active_channel).get_nick_list()
139        self.nick_list.set_items(items)
140
141    def on_irc(self, key, args):
142        if key == "active_channel":
143            self.set_active_channel(args["channel"])
144        elif key == "nick_list":
145            if args["channel"] == self.active_channel:
146                self.update_nick_list()
147        elif key == "channel_list":
148            self.update_channel_list()
149        elif key == "message":
150            if args["channel"] == self.active_channel:
151                self.text_area.append_text(
152                    args["message"], color=args["color"]
153                )
154            self.window.alert()
155