closes #2874: improved ingame chat

This commit is contained in:
Sascha Biedermann
2013-03-28 18:39:26 +01:00
parent b4a2489874
commit 1eb7c62c62
16 changed files with 255 additions and 61 deletions

View File

@@ -0,0 +1,156 @@
#region Copyright & License Information
/*
* Copyright 2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Drawing;
using OpenRA.Widgets;
using OpenRA.Network;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class IngameChatLogic
{
internal World World;
internal readonly ContainerWidget ChatOverlay;
internal readonly ChatDisplayWidget ChatOverlayDisplay;
internal readonly ContainerWidget ChatChrome;
internal readonly ScrollPanelWidget ChatScrollPanel;
internal readonly ContainerWidget ChatTemplate;
internal readonly TextFieldWidget ChatText;
private bool teamChat = false;
internal bool TeamChat
{
get { return World.Observer ? false : teamChat; }
set { teamChat = value; }
}
[ObjectCreator.UseCtor]
public IngameChatLogic(Widget widget, OrderManager orderManager, World world)
{
World = world;
var chatPanel = (ContainerWidget) widget;
ChatOverlay = chatPanel.Get<ContainerWidget>("CHAT_OVERLAY");
ChatOverlayDisplay = ChatOverlay.Get<ChatDisplayWidget>("CHAT_DISPLAY");
ChatOverlay.Visible = false;
ChatChrome = chatPanel.Get<ContainerWidget>("CHAT_CHROME");
ChatChrome.Visible = true;
var chatMode = ChatChrome.Get<ButtonWidget>("CHAT_MODE");
chatMode.GetText = () => TeamChat ? "Team" : "All";
chatMode.OnClick = () => TeamChat = !TeamChat;
ChatText = ChatChrome.Get<TextFieldWidget>("CHAT_TEXTFIELD");
ChatText.OnTabKey = () => { TeamChat = !TeamChat; return true; };
ChatText.OnEnterKey = () =>
{
ChatText.Text = ChatText.Text.Trim();
if (ChatText.Text != "")
orderManager.IssueOrder(Order.Chat(TeamChat, ChatText.Text));
CloseChat();
return true;
};
ChatText.OnEscKey = () => {CloseChat(); return true; };
var chatClose = ChatChrome.Get<ButtonWidget>("CHAT_CLOSE");
chatClose.OnClick += () => CloseChat();
chatPanel.OnKeyPress = (e) =>
{
if (e.Event == KeyInputEvent.Up) return false;
if (!IsOpen && (e.KeyName == "enter" || e.KeyName == "return") )
{
var shift = e.Modifiers.HasModifier(Modifiers.Shift);
var toggle = Game.Settings.Game.TeamChatToggle ;
TeamChat = (!toggle && shift) || ( toggle && (TeamChat ^ shift) );
OpenChat();
return true;
}
return false;
};
ChatScrollPanel = ChatChrome.Get<ScrollPanelWidget>("CHAT_SCROLLPANEL");
ChatTemplate = ChatScrollPanel.Get<ContainerWidget>("CHAT_TEMPLATE");
Game.AddChatLine += AddChatLine;
Game.BeforeGameStart += UnregisterEvents;
CloseChat();
ChatOverlayDisplay.AddLine(Color.White, null, "Use RETURN key to open chat window...");
}
void UnregisterEvents()
{
Game.AddChatLine -= AddChatLine;
Game.BeforeGameStart -= UnregisterEvents;
}
public void OpenChat()
{
ChatText.Text = "";
ChatOverlay.Visible = false;
ChatChrome.Visible = true;
ChatText.TakeFocus(new MouseInput());
}
public void CloseChat()
{
ChatOverlay.Visible = true;
ChatChrome.Visible = false;
ChatText.LoseFocus();
}
public bool IsOpen { get { return ChatChrome.IsVisible(); } }
public void AddChatLine(Color c, string from, string text)
{
ChatOverlayDisplay.AddLine(c, from, text);
var template = ChatTemplate.Clone();
var nameLabel = template.Get<LabelWidget>("NAME");
var textLabel = template.Get<LabelWidget>("TEXT");
var name = "";
if (!string.IsNullOrEmpty(from))
name = from + ":";
var font = Game.Renderer.Fonts[nameLabel.Font];
var nameSize = font.Measure(from);
nameLabel.GetColor = () => c;
nameLabel.GetText = () => name;
nameLabel.Bounds.Width = nameSize.X;
textLabel.Bounds.X += nameSize.X;
textLabel.Bounds.Width -= nameSize.X;
// Hack around our hacky wordwrap behavior: need to resize the widget to fit the text
text = WidgetUtils.WrapText(text, textLabel.Bounds.Width, font);
textLabel.GetText = () => text;
var dh = font.Measure(text).Y - textLabel.Bounds.Height;
if (dh > 0)
{
textLabel.Bounds.Height += dh;
template.Bounds.Height += dh;
}
ChatScrollPanel.AddChild(template);
ChatScrollPanel.ScrollToBottom();
Sound.PlayNotification(null, "Sounds", "ChatLine", null);
}
}
}

View File

@@ -22,9 +22,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic
[ObjectCreator.UseCtor]
public IngameChromeLogic(World world)
{
Game.AddChatLine += AddChatLine;
Game.BeforeGameStart += UnregisterEvents;
var r = Ui.Root;
gameRoot = r.Get("INGAME_ROOT");
var optionsBG = gameRoot.Get("INGAME_OPTIONS_BG");
@@ -58,8 +55,10 @@ namespace OpenRA.Mods.RA.Widgets.Logic
moneybin.Get<OrderButtonWidget>("POWER_DOWN").GetKey = _ => Game.Settings.Keys.PowerDownKey;
moneybin.Get<OrderButtonWidget>("REPAIR").GetKey = _ => Game.Settings.Keys.RepairKey;
optionsBG.Get<ButtonWidget>("DISCONNECT").OnClick = () => LeaveGame(optionsBG, world);
var chatPanel = Game.LoadWidget(world, "CHAT_PANEL", Ui.Root, new WidgetArgs());
gameRoot.AddChild(chatPanel);
optionsBG.Get<ButtonWidget>("DISCONNECT").OnClick = () => LeaveGame(optionsBG, world);
optionsBG.Get<ButtonWidget>("SETTINGS").OnClick = () => Ui.OpenWindow("SETTINGS_MENU");
optionsBG.Get<ButtonWidget>("MUSIC").OnClick = () => Ui.OpenWindow("MUSIC_MENU");
optionsBG.Get<ButtonWidget>("RESUME").OnClick = () =>
@@ -110,16 +109,5 @@ namespace OpenRA.Mods.RA.Widgets.Logic
Ui.CloseWindow();
Ui.OpenWindow("MAINMENU_BG");
}
void UnregisterEvents()
{
Game.AddChatLine -= AddChatLine;
Game.BeforeGameStart -= UnregisterEvents;
}
void AddChatLine(Color c, string from, string text)
{
gameRoot.Get<ChatDisplayWidget>("CHAT_DISPLAY").AddLine(c, from, text);
}
}
}

View File

@@ -10,6 +10,7 @@
using OpenRA.Traits;
using OpenRA.Widgets;
using OpenRA.Network;
using System;
using System.Drawing;
using System.Linq;
@@ -24,9 +25,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic
[ObjectCreator.UseCtor]
public IngameObserverChromeLogic(World world)
{
Game.AddChatLine += AddChatLine;
Game.BeforeGameStart += UnregisterEvents;
var r = Ui.Root;
gameRoot = r.Get("OBSERVER_ROOT");
var optionsBG = gameRoot.Get("INGAME_OPTIONS_BG");
@@ -59,6 +57,12 @@ namespace OpenRA.Mods.RA.Widgets.Logic
Ui.Root.Get<ButtonWidget>("INGAME_STATS_BUTTON").OnClick = () => gameRoot.Get("OBSERVER_STATS").Visible ^= true;
if (!world.IsShellmap)
{
var chatPanel = Game.LoadWidget(world, "CHAT_PANEL", Ui.Root, new WidgetArgs());
gameRoot.AddChild(chatPanel);
}
var shroudSelector = Ui.Root.GetOrNull<DropDownButtonWidget>("SHROUD_SELECTOR");
if (shroudSelector != null)
{
@@ -91,17 +95,6 @@ namespace OpenRA.Mods.RA.Widgets.Logic
}
}
void UnregisterEvents()
{
Game.AddChatLine -= AddChatLine;
Game.BeforeGameStart -= UnregisterEvents;
}
void AddChatLine(Color c, string from, string text)
{
gameRoot.Get<ChatDisplayWidget>("CHAT_DISPLAY").AddLine(c, from, text);
}
class DropDownOption
{
public string Title;