Project

Profile

Help

How to connect?
Download (5.62 KB) Statistics View on GitHub Reload from mirrored respository
| Branch: | Tag: | Revision:

github / src / ui / window.py @ 424e22f2

1
"""This file contains the ClientWindow class."""
2

    
3
import wx
4

    
5
from ytranslate.tools import t
6

    
7
from dialogs.preferences import PreferencesDialog
8
from dialogs.macro import MacroDialog
9
from ui.event import EVT_FOCUS, FocusEvent, myEVT_FOCUS
10

    
11
class ClientWindow(wx.Frame):
12

    
13
    def __init__(self, engine):
14
        super(ClientWindow, self).__init__(None)
15
        self.engine = engine
16
        self.CreateMenuBar()
17
        self.InitUI()
18

    
19
    def _get_client(self):
20
        return self.panel.client
21
    def _set_client(self, client):
22
        self.panel.client = client
23
    client = property(_get_client, _set_client)
24

    
25
    def CreateMenuBar(self):
26
        """Create the GUI menu bar and hierarchy of menus."""
27
        menubar = wx.MenuBar()
28

    
29
        # Differemtn menus
30
        fileMenu = wx.Menu()
31
        gameMenu = wx.Menu()
32

    
33
        # File menu
34
        ## Preferences
35
        preferences = wx.MenuItem(fileMenu, -1, t("ui.menu.preferences"))
36
        self.Bind(wx.EVT_MENU, self.OnPreferences, preferences)
37
        fileMenu.AppendItem(preferences)
38

    
39
        ## Quit
40
        quit = wx.MenuItem(fileMenu, -1, t("ui.menu.quit"))
41
        self.Bind(wx.EVT_MENU, self.OnQuit, quit)
42
        fileMenu.AppendItem(quit)
43

    
44
        # Game menu
45
        macro = wx.MenuItem(fileMenu, -1, t("ui.menu.macro"))
46
        self.Bind(wx.EVT_MENU, self.OnMacro, macro)
47
        gameMenu.AppendItem(macro)
48

    
49
        menubar.Append(fileMenu, t("ui.menu.file"))
50
        menubar.Append(gameMenu, t("ui.menu.game"))
51

    
52
        self.SetMenuBar(menubar)
53

    
54
    def InitUI(self):
55
        self.panel = MUDPanel(self, self.engine)
56
        self.SetTitle("CocoMUD client")
57
        self.Maximize()
58
        self.Show()
59

    
60
    def OnPreferences(self, e):
61
        """Open the preferences dialog box."""
62
        dialog = PreferencesDialog(self.engine)
63
        dialog.ShowModal()
64
        dialog.Destroy()
65

    
66
    def OnMacro(self, e):
67
        """Open the macro dialog box."""
68
        dialog = MacroDialog(self.engine)
69
        dialog.ShowModal()
70
        dialog.Destroy()
71

    
72
    def OnQuit(self, e):
73
        self.Close()
74

    
75
    # Methods to handle client's events
76
    def handle_message(self, message):
77
        """The client has just received a message."""
78
        pos = self.panel.output.GetInsertionPoint()
79
        self.panel.output.write(message)
80
        self.panel.output.SetInsertionPoint(pos)
81

    
82
    def handle_option(self, command):
83
        """Handle the specified option.
84

85
        The command is a string representing the received option.
86
        The following options are supported:
87
            "hide":  the input should be hidden
88
            "show":  the input should be shown
89

90
        """
91
        if command == "hide":
92
            evt = FocusEvent(myEVT_FOCUS, -1, "password")
93
            wx.PostEvent(self.panel, evt)
94
        elif command == "show":
95
            evt = FocusEvent(myEVT_FOCUS, -1, "input")
96
            wx.PostEvent(self.panel, evt)
97

    
98
class MUDPanel(wx.Panel):
99

    
100
    def __init__(self, parent, engine):
101
        wx.Panel.__init__(self, parent)
102
        self.engine = engine
103
        self.client = None
104
        sizer = wx.BoxSizer(wx.VERTICAL)
105
        self.SetSizer(sizer)
106

    
107
        # Input
108
        s_input = wx.BoxSizer(wx.HORIZONTAL)
109
        l_input = wx.StaticText(self, -1, t("ui.client.input"))
110
        t_input = wx.TextCtrl(self, -1, "", size=(125, -1),
111
                style=wx.TE_PROCESS_ENTER)
112
        self.input = t_input
113

    
114
        # Password
115
        l_password = wx.StaticText(self, -1, t("ui.client.password"))
116
        t_password = wx.TextCtrl(self, -1, "", size=(20, -1),
117
                style=wx.TE_PROCESS_ENTER | wx.TE_PASSWORD)
118
        self.password = t_password
119
        t_password.Hide()
120

    
121
        # Add the input field in the sizer
122
        s_input.Add(l_input)
123
        s_input.Add(t_input, proportion=4)
124
        s_input.Add(l_password)
125
        s_input.Add(t_password, proportion=2)
126

    
127
        # Ouput
128
        l_output = wx.StaticText(self, -1, t("ui.client.output"))
129
        t_output = wx.TextCtrl(self, -1, "",
130
                size=(600, 400), style=wx.TE_MULTILINE|wx.TE_READONLY)
131
        self.output = t_output
132

    
133
        # Add the output fields in the sizer
134
        sizer.Add(s_input)
135
        sizer.Add(t_output, proportion=8)
136

    
137
        # Event handler
138
        t_input.Bind(wx.EVT_TEXT_ENTER, self.EvtText)
139
        t_password.Bind(wx.EVT_TEXT_ENTER, self.EvtText)
140
        self.Bind(EVT_FOCUS, self.OnFocus)
141
        t_input.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
142
        t_password.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
143
        t_output.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
144

    
145
    def EvtText(self, event):
146
        """One of the input fields is sending text."""
147
        self.input.Clear()
148
        self.password.Clear()
149
        msg = event.GetString().encode("utf-8", "replace")
150
        self.client.write(msg + "\r\n")
151

    
152
    def OnFocus(self, evt):
153
        """The GUIClient requires a change of focus.
154

155
        This event is triggered when the GUIClient asks a change of
156
        focus in the input field (hiding the password field) or in
157
        the password field (hiding the input field).
158

159
        """
160
        val = evt.GetValue()
161
        if val == "input":
162
            self.input.Show()
163
            self.input.SetFocus()
164
            self.password.Hide()
165
        elif val == "password":
166
            self.password.Show()
167
            self.password.SetFocus()
168
            self.input.Hide()
169

    
170
    def OnKeyDown(self, e):
171
        """A key is pressed in the window."""
172
        modifiers = e.GetModifiers()
173
        key = e.GetUnicodeKey()
174
        if not key:
175
            key = e.GetKeyCode()
176

    
177
        # Look for matching macros
178
        for code, macro in self.engine.macros.items():
179
            if code == (key, modifiers):
180
                macro.execute(self.engine, self.client)
181

    
182
        e.Skip()
(3-3/3)