forked from yy-yyaa/crown-tools
/
MainWindow.cs
213 lines (177 loc) · 5.85 KB
/
MainWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
* License: https://github.com/taylor001/crown/blob/master/LICENSE
*/
using System;
using Gtk;
using Gdk;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using Crown.Console;
using Crown;
using System.Collections;
public partial class MainWindow: Gtk.Window
{
private EntryHistory History = new EntryHistory(256);
private ConsoleClient Client = null;
private ActionGroup Actions = null;
private UIManager UI = new UIManager();
private ScrolledWindow scrolledwindow1;
private TextView textview1;
private Entry entry1;
private string Address = "127.0.0.1";
private int Port = 10001;
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
ActionEntry[] entries = new ActionEntry[] {
new ActionEntry("menu-engine", null, "_Engine", null, null, null ),
new ActionEntry("unpause", null, "Unpause", "<ctrl>U", null, OnUnpause ),
new ActionEntry("menu-script", null, "_Script", null, null, null ),
new ActionEntry("rebuild-and-reload", null, "Rebuild And Reload", null, null, OnRebuildAndReload ),
new ActionEntry("menu-connect", null, "_Connect", null, null, null ),
new ActionEntry("reconnect", null, "Reconnect", "<ctrl>R", null, null )
};
Actions = new ActionGroup("group");
Actions.Add(entries);
UI.InsertActionGroup(Actions, 0);
UI.AddUiFromResource("Menu.xml");
AddAccelGroup(UI.AccelGroup);
MenuBar menuBar = (MenuBar)UI.GetWidget("/MenuBar");
vbox1.PackStart(menuBar, false, false, 0);
// Create tags for color-formatted text
TextTag tagInfo = new Gtk.TextTag ("info");
tagInfo.BackgroundGdk = new Gdk.Color (255, 255, 255);
TextTag tagWarning = new Gtk.TextTag ("warning");
tagWarning.BackgroundGdk = new Gdk.Color (255, 255, 153);
TextTag tagError = new Gtk.TextTag ("error");
tagError.BackgroundGdk = new Gdk.Color (255, 153, 153);
TextTag tagDebug = new Gtk.TextTag ("debug");
tagDebug.BackgroundGdk = new Gdk.Color (224, 224, 224);
textview1 = new TextView();
textview1.Editable = false;
textview1.CanFocus = false;
TextBuffer textbuffer1 = textview1.Buffer;
textbuffer1.TagTable.Add (tagInfo);
textbuffer1.TagTable.Add (tagWarning);
textbuffer1.TagTable.Add (tagError);
textbuffer1.TagTable.Add (tagDebug);
scrolledwindow1 = new ScrolledWindow();
scrolledwindow1.Add(textview1);
vbox1.PackStart(scrolledwindow1, true, true, 0);
entry1 = new Entry();
entry1.KeyPressEvent += new KeyPressEventHandler(OnEntryKeyPressed);
entry1.Activated += new EventHandler(OnEntryActivated);
vbox1.PackStart(entry1, false, true, 0);
EnableMainMenu(false);
Actions.GetAction("menu-script").Sensitive = false;
Client = new ConsoleClient();
Client.ConnectedEvent += OnConnected;
Client.DisconnectedEvent += OnDisconnected;
Client.MessageReceivedEvent += OnMessageReceived;
Connect(Address, Port);
ShowAll();
}
protected void EnableMainMenu(bool enable)
{
Actions.GetAction("menu-engine").Sensitive = enable;
// Actions.GetAction("ScriptMenu").Sensitive = enable;
}
protected void OnConnected(object o, ConnectedArgs args)
{
EnableMainMenu(true);
LogInfo("Connected to " + args.Address + ":" + args.Port.ToString());
}
protected void OnDisconnected(object o, DisconnectedArgs args)
{
EnableMainMenu(false);
LogInfo("Disconnected\n");
}
protected void Connect(string addr, int port)
{
LogInfo("Trying " + addr + ":" + port.ToString());
Client.Connect(addr, port);
}
protected void OnMessageReceived(object o, MessageReceivedArgs args)
{
Hashtable obj = JSON.Decode(System.Text.Encoding.UTF8.GetBytes(args.Json));
if (obj["type"].ToString() == "message")
{
string severity = obj["severity"].ToString();
string message = obj["message"].ToString();
if (severity == "info") LogInfo(message);
else if (severity == "warning") LogWarning(message);
else if (severity == "error") LogError(message);
else if (severity == "debug") LogDebug(message);
}
}
protected void OnEntryActivated (object sender, EventArgs e)
{
string text = entry1.Text;
text = text.Trim ();
// Do processing only if we have text
if (text.Length > 0)
{
History.Push(text);
Client.SendScript(text);
LogInfo("> " + text);
}
entry1.Text = "";
}
protected void OnEntryKeyPressed (object o, KeyPressEventArgs args)
{
string text = "";
switch (args.Event.Key)
{
case Gdk.Key.Down: text = History.Next(); break;
case Gdk.Key.Up: text = History.Previous(); break;
default: return; // Ignore other keys
}
entry1.Text = text;
entry1.Position = entry1.Text.Length;
args.RetVal = true;
}
protected void OnUnpause(object o, EventArgs args)
{
Client.Send("{\"type\":\"command\",\"command\":\"unpause\"}");
}
protected void OnRebuildAndReload(object o, EventArgs args)
{
Client.Send("{\"type\":\"command\",\"command\":\"reload\"}");
}
// Logging
private void WriteLog(string text, string tag)
{
text += "\n";
Gtk.Application.Invoke (delegate {
TextIter endIter = textview1.Buffer.EndIter;
textview1.Buffer.Insert(ref endIter, text);
endIter.BackwardChars(text.Length);
textview1.Buffer.ApplyTag(textview1.Buffer.TagTable.Lookup(tag), endIter, textview1.Buffer.EndIter);
textview1.ScrollToMark(textview1.Buffer.CreateMark("bottom", textview1.Buffer.EndIter, false), 0, true, 0.0, 1.0);
});
}
private void LogInfo(string text)
{
WriteLog(text, "info");
}
private void LogWarning(string text)
{
WriteLog(text, "warning");
}
private void LogError(string text)
{
WriteLog(text, "error");
}
private void LogDebug(string text)
{
WriteLog(text, "debug");
}
private void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
}