corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
4 *
5 * - Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 using System;
28 using System.Collections.Generic;
29 using System.Drawing;
30 using System.IO;
31 using System.Windows.Forms;
32  
33 namespace OpenMetaverse.GUI
34 {
35 /// <summary>
36 /// Panel GUI component for interfacing with local chat
37 /// </summary>
38 public class LocalChat : Panel
39 {
40 /// <summary>
41 /// A file that output should be logged to (or null, to disable logging)
42 /// </summary>
43 public string LogFile = null;
44  
45 private GridClient _Client;
46 private RichTextBox rtfOutput = new RichTextBox();
47 private TextBox txtInput = new TextBox();
48  
49 /// <summary>
50 /// Gets or sets the GridClient associated with this control
51 /// </summary>
52 public GridClient Client
53 {
54 get { return _Client; }
55 set { if (value != null) InitializeClient(value); }
56 }
57  
58 /// <summary>
59 /// Panel control for an unspecified client's local chat interaction
60 /// </summary>
61 public LocalChat()
62 {
63 rtfOutput.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
64 rtfOutput.BackColor = Color.FromKnownColor(KnownColor.Window);
65 rtfOutput.Width = this.Width;
66 rtfOutput.Height = this.Height - txtInput.Height;
67 rtfOutput.ReadOnly = true;
68 rtfOutput.Top = 0;
69 rtfOutput.Left = 0;
70  
71 txtInput.Dock = DockStyle.Bottom;
72 txtInput.KeyDown += new KeyEventHandler(txtInput_KeyDown);
73  
74 this.Controls.AddRange(new Control[] { txtInput, rtfOutput });
75 }
76  
77 /// <summary>
78 /// Panel control for the specified client's local chat interaction
79 /// </summary>
80 public LocalChat(GridClient client) : this ()
81 {
82 _Client = client;
83 }
84  
85 /// <summary>
86 /// Adds text of a specified color to the display output
87 /// </summary>
88 /// <param name="name"></param>
89 /// <param name="type"></param>
90 /// <param name="message"></param>
91 /// <param name="color"></param>
92 public void LogChat(string name, ChatType type, string message, Color color)
93 {
94 if (!this.IsHandleCreated) return;
95  
96 if (this.InvokeRequired)
97 {
98 this.BeginInvoke((MethodInvoker)delegate { LogChat(name, type, message, color); });
99 }
100 else
101 {
102 rtfOutput.SelectionStart = rtfOutput.Text.Length;
103 rtfOutput.SelectionColor = color;
104 DateTime now = DateTime.Now;
105 string output;
106 string volume;
107  
108 if (message.Length > 3 && message.Substring(0, 4).ToLower() == "/me ")
109 {
110 string text = message.Substring(4);
111 if (type == ChatType.Shout) volume = "(shouted) ";
112 else if (type == ChatType.Whisper) volume = "(whispered) ";
113 else volume = String.Empty;
114 output = string.Format("{0}[{1}:{2}] {3}{4} {5}", Environment.NewLine, now.Hour.ToString().PadLeft(2, '0'), now.Minute.ToString().PadLeft(2, '0'), volume, name, text);
115 }
116 else
117 {
118 if (type == ChatType.Shout) volume = " shouts";
119 else if (type == ChatType.Whisper) volume = " whispers";
120 else volume = String.Empty;
121 output = string.Format("{0}[{1}:{2}] {3}{4}: {5}", Environment.NewLine, now.Hour.ToString().PadLeft(2, '0'), now.Minute.ToString().PadLeft(2, '0'), name, volume, message);
122 }
123  
124 rtfOutput.SelectedText = output;
125 rtfOutput.ScrollToCaret();
126  
127 if (LogFile != null)
128 File.AppendAllText(LogFile, output);
129 }
130 }
131  
132 /// <summary>
133 /// Thread-safe method for adding text of a specified color to the display output
134 /// </summary>
135 /// <param name="text"></param>
136 /// <param name="color"></param>
137 public void LogText(string text, Color color)
138 {
139 if (this.InvokeRequired)
140 {
141 this.BeginInvoke((MethodInvoker)delegate { LogText(text, color); });
142 }
143 else
144 {
145 rtfOutput.SelectionStart = rtfOutput.Text.Length;
146 rtfOutput.SelectionColor = color;
147 DateTime now = DateTime.Now;
148 string output = string.Format("{0}[{1}:{2}] {3}", Environment.NewLine, now.Hour.ToString().PadLeft(2, '0'), now.Minute.ToString().PadLeft(2, '0'), text);
149 rtfOutput.SelectedText = output;
150 rtfOutput.ScrollToCaret();
151  
152 if (LogFile != null)
153 File.AppendAllText(LogFile, output);
154 }
155 }
156  
157 private void InitializeClient(GridClient client)
158 {
159 _Client = client;
160 _Client.Self.ChatFromSimulator += new EventHandler<ChatEventArgs>(Self_ChatFromSimulator);
161 }
162  
163 void Self_ChatFromSimulator(object sender, ChatEventArgs e)
164 {
165 if (e.AudibleLevel == ChatAudibleLevel.Fully && e.Type != ChatType.StartTyping && e.Type != ChatType.StopTyping)
166 {
167 Color color;
168 if (e.SourceType == ChatSourceType.Agent) color = Color.FromKnownColor(KnownColor.ControlText);
169 else color = Color.FromKnownColor(KnownColor.GrayText);
170 LogChat(e.FromName, e.Type, e.Message, color);
171 }
172 }
173  
174 void txtInput_KeyDown(object sender, KeyEventArgs e)
175 {
176 if (e.KeyCode == Keys.Enter && txtInput.Text != string.Empty)
177 {
178 e.SuppressKeyPress = true;
179 _Client.Self.Chat(txtInput.Text, 0, e.Control ? ChatType.Shout : ChatType.Normal);
180 txtInput.Clear();
181 }
182 }
183 }
184  
185 }