...
 
Commits (11)
using System.ComponentModel;
using Tangine.Network.Protocol;
namespace Tangine.Controls
{
public class ScheduleTickEventArgs : CancelEventArgs
{
private readonly TanjiSchedulerView.HSchedule _schedule;
public int CurrentCycle { get; }
public int Cycles => _schedule.Cycles;
public double Interval
{
get { return _schedule.Interval; }
set { _schedule.Interval = value; }
}
public HPacket Packet
{
get { return _schedule.Packet; }
set { _schedule.Packet = value; }
}
public ScheduleTickEventArgs(TanjiSchedulerView.HSchedule schedule, int currentCycle)
{
_schedule = schedule;
CurrentCycle = currentCycle;
}
}
}
\ No newline at end of file
using System.Windows.Forms;
using Tangine.Habbo;
namespace Tangine.Controls
{
public class TileEventArgs : MouseEventArgs
{
public HPoint Tile { get; }
public TileEventArgs(HPoint tile, MouseButtons button, int clicks, int x, int y, int delta)
: base(button, clicks, x, y, delta)
{
Tile = tile;
}
}
}
\ No newline at end of file
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
namespace Tangine.Controls
{
[DesignerCategory("Code")]
public class TanjiButton : Control, IButtonControl
{
private bool _isPressed;
private const int GRAD_HEIGHT = 10;
/// <summary>
/// Gets or sets a value that is returned to the parent form when the button is clicked.
/// </summary>
[DefaultValue(DialogResult.None)]
public DialogResult DialogResult { get; set; }
[Browsable(false)]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
[Browsable(false)]
public override Color ForeColor
{
get { return base.ForeColor; }
set { base.ForeColor = value; }
}
[Browsable(false)]
public override Image BackgroundImage
{
get { return base.BackgroundImage; }
set { base.BackgroundImage = value; }
}
[Browsable(false)]
public override ImageLayout BackgroundImageLayout
{
get { return base.BackgroundImageLayout; }
set { base.BackgroundImageLayout = value; }
}
[SettingsBindable(true)]
[Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
public override string Text
{
get { return base.Text; }
set { base.Text = value; Invalidate(); }
}
[Localizable(true)]
[DefaultValue(typeof(Size), "100, 20")]
new public Size Size
{
get { return base.Size; }
set { base.Size = value; }
}
private Color _skin = Color.SteelBlue;
[DefaultValue(typeof(Color), "SteelBlue")]
public Color Skin
{
get { return _skin; }
set { _skin = value; Invalidate(); }
}
public TanjiButton()
{
SetStyle((ControlStyles)2050, true);
DoubleBuffered = true;
Size = new Size(100, 20);
BackColor = Color.Transparent;
}
public void PerformClick()
{
if (Enabled)
{
Focus();
base.OnClick(EventArgs.Empty);
}
}
public void NotifyDefault(bool value)
{
Invalidate();
}
protected override void OnClick(EventArgs e)
{ }
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Enabled ?
Skin : SystemColors.Control);
using (var pen = new Pen(Color.FromArgb(50, Color.Black)))
{
e.Graphics.DrawRectangle(pen, ClientRectangle.X, ClientRectangle.Y,
ClientRectangle.Width - 1, ClientRectangle.Height - 1);
}
using (var format = new StringFormat())
{
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
if (Enabled)
{
int textOffset = 0;
var clickShadow = Color.FromArgb(25, Color.Black);
var textShadow = Color.FromArgb(_isPressed ? 150 : 100, Color.Black);
if (_isPressed)
{
textOffset++;
var r1 = new Rectangle(0, 0, Width, GRAD_HEIGHT);
using (var clickShadowGradient = new LinearGradientBrush(r1, clickShadow, Color.Transparent, 90))
e.Graphics.FillRectangle(clickShadowGradient, r1);
var r2 = new Rectangle(0, Height - GRAD_HEIGHT, Width, GRAD_HEIGHT);
using (var clickShadowGradient = new LinearGradientBrush(r2, clickShadow, Color.Transparent, 270))
e.Graphics.FillRectangle(clickShadowGradient, r2);
}
using (var textShadowBrush = new SolidBrush(textShadow))
{
e.Graphics.DrawString(Text, Font, textShadowBrush,
new Rectangle(textOffset + 1, textOffset + 1, Width, Height), format);
}
e.Graphics.DrawString(Text, Font, Brushes.White,
new Rectangle(textOffset, textOffset, Width, Height), format);
}
else
{
using (var solidBrush = new SolidBrush(Color.FromArgb(150, Color.Black)))
{
e.Graphics.DrawString(Text, Font, solidBrush,
new Rectangle(0, 0, Width, Height), format);
}
}
}
base.OnPaint(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
bool isLeft = (e.Button == MouseButtons.Left);
if (isLeft)
{
_isPressed = false;
Focus();
Invalidate();
}
base.OnMouseUp(e);
if (isLeft &&
ClientRectangle.Contains(e.Location))
{
base.OnClick(e);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_isPressed = true;
Focus();
Invalidate();
}
base.OnMouseDown(e);
}
}
}
\ No newline at end of file
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using Tangine.Network.Protocol;
namespace Tangine.Controls
{
[DesignerCategory("Code")]
public class TanjiConstructView : TanjiListView
{
private HPacket _cachedPacket;
private readonly List<object> _valuesWritten;
public IReadOnlyList<object> ValuesWritten { get; }
public TanjiConstructView()
{
_valuesWritten = new List<object>();
ValuesWritten = _valuesWritten.AsReadOnly();
}
public void WriteInteger(int value)
{
WriteInteger(value, 1);
}
public void WriteInteger(int value, int amount)
{
WriteValue("Integer", value,
HEncoding.BigEndian.GetBytes(value), amount);
}
public void WriteBoolean(bool value)
{
WriteBoolean(value, 1);
}
public void WriteBoolean(bool value, int amount)
{
WriteValue("Boolean", value,
HEncoding.BigEndian.GetBytes(value), amount);
}
public void WriteString(string value)
{
WriteString(value, 1);
}
public void WriteString(string value, int amount)
{
WriteValue("String", value,
HEncoding.BigEndian.GetBytes(value), amount);
}
protected virtual void MoveValue(int oldIndex, int newIndex)
{
lock (_valuesWritten)
{
object value = _valuesWritten[oldIndex];
_valuesWritten.RemoveAt(oldIndex);
_valuesWritten.Insert(newIndex, value);
_cachedPacket = null;
}
}
protected virtual void WriteValue(string type, object value, byte[] data, int amount)
{
lock (_valuesWritten)
{
BeginUpdate();
string encoded = null;// HPacket.ToString(data); // TODO
for (int i = 0; i < amount; i++)
{
_valuesWritten.Add(value);
ListViewItem item = null;
if (i != (amount - 1))
{
item = AddItem(type, value, encoded);
}
else item = AddFocusedItem(type, value, encoded);
}
EndUpdate();
_cachedPacket = null;
}
}
protected override void MoveItemUp(ListViewItem item)
{
int oldIndex = item.Index;
base.MoveItemUp(item);
MoveValue(oldIndex, item.Index);
}
protected override void MoveItemDown(ListViewItem item)
{
int oldIndex = item.Index;
base.MoveItemDown(item);
MoveValue(oldIndex, item.Index);
}
protected override void RemoveItem(ListViewItem item, bool selectNext)
{
lock (_valuesWritten)
{
_valuesWritten.RemoveAt(item.Index);
base.RemoveItem(item, selectNext);
_cachedPacket = null;
}
}
public void UpdateSelectedValue(object value)
{
if (HasSelectedItem)
UpdateValue(SelectedItem, value);
}
protected virtual void UpdateValue(ListViewItem item, object value)
{
lock (_valuesWritten)
{
byte[] data = null;
switch (item.SubItems[0].Text)
{
case "String": data = HEncoding.BigEndian.GetBytes((string)value); break;
case "Integer": data = HEncoding.BigEndian.GetBytes((int)value); break;
case "Boolean": data = HEncoding.BigEndian.GetBytes((bool)value); break;
}
string encoded = null; //HPacket.ToString(data); // TODO
item.SubItems[1].Text = value.ToString();
item.SubItems[2].Text = encoded;
_valuesWritten[item.Index] = value;
_cachedPacket = null;
}
}
public HPacket GetPacket(ushort header)
{
// TODO
return null;
//lock (_valuesWritten)
//{
// if (_cachedPacket == null)
// {
// _cachedPacket =
// new HMessage(header, _valuesWritten.ToArray());
// }
// _cachedPacket.Header = header;
// return _cachedPacket;
//}
}
public string GetStructure(ushort header)
{
lock (_valuesWritten)
{
string structure = $"{{l}}{{u:{header}}}";
foreach (object value in _valuesWritten)
{
char type = Type.GetTypeCode(
value.GetType()).ToString().ToLower()[0];
structure += $"{{{type}:{value}}}";
}
return structure;
}
}
}
}
\ No newline at end of file
using System;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using Tangine.Network.Protocol;
namespace Tangine.Controls
{
[DesignerCategory("Code")]
public class TanjiInjectionMenu : ContextMenuStrip
{
private TextBox _inputBox;
[Browsable(false)]
public TextBox InputBox
{
get { return _inputBox; }
set
{
if (value != null)
{
_inputBox = value;
_inputBox.ContextMenuStrip = this;
}
}
}
#region Menu Items
protected ToolStripMenuItem RealignBtn { get; }
protected ToolStripMenuItem InsertParametersBtn { get; }
protected ToolStripMenuItem StringBtn { get; }
protected ToolStripMenuItem IntegerBtn { get; }
protected ToolStripMenuItem ByteBooleanBtn { get; }
protected ToolStripSeparator TopSeparator { get; }
protected ToolStripMenuItem CutBtn { get; }
protected ToolStripMenuItem CopyBtn { get; }
protected ToolStripMenuItem PasteBtn { get; }
protected ToolStripSeparator BottomSeparator { get; }
protected ToolStripMenuItem UndoBtn { get; }
protected ToolStripMenuItem SelectAllBtn { get; }
#endregion
public TanjiInjectionMenu(IContainer container)
: base(container)
{
RealignBtn = CreateItem("Realign", Keys.R);
RealignBtn.Click += RealignBtn_Click;
InsertParametersBtn = CreateItem("Insert Parameter(s)");
TopSeparator = new ToolStripSeparator();
StringBtn = CreateItem("String", Keys.S);
StringBtn.Click += StringBtn_Click;
IntegerBtn = CreateItem("Integer", Keys.I);
IntegerBtn.Click += IntegerBtn_Click;
ByteBooleanBtn = CreateItem("Byte/Boolean", Keys.B);
ByteBooleanBtn.Click += ByteBooleanBtn_Click;
InsertParametersBtn.DropDownItems.Add(StringBtn);
InsertParametersBtn.DropDownItems.Add(IntegerBtn);
InsertParametersBtn.DropDownItems.Add(ByteBooleanBtn);
CutBtn = CreateItem("Cut", Keys.X);
CutBtn.Click += CutBtn_Click;
CopyBtn = CreateItem("Copy", Keys.C);
CopyBtn.Click += CopyBtn_Click;
PasteBtn = CreateItem("Paste", Keys.V);
PasteBtn.Click += PasteBtn_Click;
BottomSeparator = new ToolStripSeparator();
UndoBtn = CreateItem("Undo", Keys.Z);
UndoBtn.Click += UndoBtn_Click;
SelectAllBtn = CreateItem("Select All", Keys.A);
SelectAllBtn.Click += SelectAllBtn_Click;
Items.AddRange(new ToolStripItem[]
{
RealignBtn,
InsertParametersBtn,
TopSeparator,
CutBtn,
CopyBtn,
PasteBtn,
BottomSeparator,
UndoBtn,
SelectAllBtn
});
}
public void InsertString()
{
InsertParameter("s");
}
public void InsertInteger()
{
InsertParameter("i");
}
public void InsertByteBoolean()
{
InsertParameter("b");
}
protected void InsertParameter(string parameterName)
{
if (InputBox == null) return;
string selectedValue = InputBox.SelectedText;
object parameterValue = null;
byte[] parameterData = HPacket.ToBytes(HEncoding.BigEndian, selectedValue);
switch (parameterName)
{
case "i":
{
if (parameterData.Length == 4)
{
parameterValue = HEncoding.BigEndian.ReadInt32(parameterData, 0);
}
break;
}
case "s":
{
if (parameterData.Length >= 2 &&
(HEncoding.BigEndian.ReadUInt16(parameterData, 0) == (parameterData.Length - 2)))
{
parameterValue = Encoding.UTF8.GetString(
parameterData, 2, parameterData.Length - 2);
}
break;
}
case "b":
{
if (parameterData.Length == 1)
{
parameterValue = HEncoding.BigEndian.ReadBoolean(parameterData, 0);
}
break;
}
}
InsertParameter(parameterName, parameterValue);
}
protected virtual void InsertParameter(string parameterName, object parameterValue)
{
string formattedParameter = $"{{{parameterName}:{parameterValue}}}";
int parameterStart = (parameterValue == null ?
InputBox.TextLength : InputBox.SelectionStart);
if (parameterValue != null)
{
InputBox.Text = InputBox.Text.Remove(
parameterStart, InputBox.SelectionLength);
}
InputBox.Text = InputBox.Text.Insert(
parameterStart, formattedParameter);
InputBox.Select(parameterStart + 3,
(parameterValue?.ToString() ?? string.Empty).Length);
}
private void RealignBtn_Click(object sender, EventArgs e)
{
if (InputBox == null) return;
int packetHeaderEnd = 0;
if (!InputBox.Text.StartsWith("{l}{u:"))
{
ushort packetHeader = 0;
byte[] packetData = HModern.ToBytes(InputBox.Text);
if (packetData.Length >= 6)
{
int packetLength = HEncoding.BigEndian.ReadInt32(packetData, 0);
packetHeader = HEncoding.BigEndian.ReadUInt16(packetData, 4);
byte[] headerData = new byte[6];
Buffer.BlockCopy(packetData, 0, headerData, 0, 6);
if (packetLength == (packetData.Length - 4))
{
string formattedHeader = null; //HMessage.ToString(headerData); TODO
InputBox.Text = InputBox.Text.Remove(0, formattedHeader.Length);
}
}
InputBox.Text = InputBox.Text.Insert(0, $"{{l}}{{u:{packetHeader}}}");
packetHeaderEnd = packetHeader.ToString().Length;
}
else
{
string formattedHeader = InputBox.Text
.GetChild("{l}{u:").GetParent("}");
packetHeaderEnd = formattedHeader.Length;
}
InputBox.Select(6, packetHeaderEnd);
InputBox.ScrollToCaret();
}
private void StringBtn_Click(object sender, EventArgs e)
{
InsertString();
}
private void IntegerBtn_Click(object sender, EventArgs e)
{
InsertInteger();
}
private void ByteBooleanBtn_Click(object sender, EventArgs e)
{
InsertByteBoolean();
}
private void CutBtn_Click(object sender, EventArgs e)
{
InputBox?.Cut();
}
private void CopyBtn_Click(object sender, EventArgs e)
{
InputBox?.Copy();
}
private void PasteBtn_Click(object sender, EventArgs e)
{
InputBox?.Paste();
}
private void UndoBtn_Click(object sender, EventArgs e)
{
InputBox?.Undo();
}
private void SelectAllBtn_Click(object sender, EventArgs e)
{
InputBox?.SelectAll();
}
protected ToolStripMenuItem CreateItem(string text)
{
return CreateItem(text, Keys.None);
}
protected virtual ToolStripMenuItem CreateItem(string text, Keys ctrlModifier)
{
var item = new ToolStripMenuItem(text);
if (ctrlModifier != Keys.None && ctrlModifier != Keys.Control)
{
item.ShortcutKeys = (Keys.Control | ctrlModifier);
}
return item;
}
protected override void OnOpening(CancelEventArgs e)
{
UndoBtn.Enabled = (InputBox?.CanUndo ?? false);
base.OnOpening(e);
}
}
}
\ No newline at end of file
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
namespace Tangine.Controls
{
[DesignerCategory("Code")]
public class TanjiLabel : Control
{
private int _borderWidth = 1;
[DefaultValue(1)]
public int BorderWidth
{
get { return _borderWidth; }
set { _borderWidth = value; Invalidate(); }
}
private bool _isBorderVisible = true;
[DefaultValue(true)]
public bool IsBorderVisible
{
get { return _isBorderVisible; }
set { _isBorderVisible = value; Invalidate(); }
}
private Color _skin = Color.SteelBlue;
[DefaultValue(typeof(Color), "SteelBlue")]
public Color Skin
{
get { return _skin; }
set { _skin = value; Invalidate(); }
}
[DefaultValue(typeof(Color), "White")]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
public override string Text
{
get { return base.Text; }
set
{
base.Text = value;
Invalidate();
}
}
public TanjiLabel()
{
SetStyle((ControlStyles)2050, true);
BackColor = Color.White;
DoubleBuffered = true;
Height = 20;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(BackColor);
if (IsBorderVisible)
{
using (var brush = new SolidBrush(Skin))
{
e.Graphics.FillRectangle(brush, 0, 0, BorderWidth, Height);
e.Graphics.FillRectangle(brush, Width - BorderWidth, 0, BorderWidth, Height);
TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor);
}
}
base.OnPaint(e);
}
}
}
\ No newline at end of file
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
namespace Tangine.Controls
{
[DesignerCategory("Code")]
public class TanjiLabelBox : Control
{
private Rectangle _textRect;
[Browsable(false)]
public TextBox Box { get; }
[DefaultValue(typeof(Color), "White")]
public override Color ForeColor
{
get { return base.ForeColor; }
set { base.ForeColor = value; }
}
[DefaultValue(typeof(Color), "SteelBlue")]
public override Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}
private int _textPaddingWidth = 10;
[DefaultValue(10)]
public int TextPaddingWidth
{
get { return _textPaddingWidth; }
set
{
_textPaddingWidth = value;
Text = Text;
}
}
[DefaultValue(typeof(HorizontalAlignment), "Left")]
public HorizontalAlignment ValueAlign
{
get { return Box.TextAlign; }
set { Box.TextAlign = value; }
}
[DefaultValue(false)]
public bool ValueReadOnly
{
get { return Box.ReadOnly; }
set { Box.ReadOnly = value; }
}
public string Value
{
get { return Box.Text; }
set { Box.Text = value; }
}
public override string Text
{
get { return base.Text; }
set
{
base.Text = value;
using (Graphics gfx = CreateGraphics())
using (var format = new StringFormat())
{
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
Size titleSize = gfx.MeasureString(value, Font).ToSize();
titleSize.Height = Height;
titleSize.Width += (1 + _textPaddingWidth);
_textRect = new Rectangle(new Point(0, 0), titleSize);
}
Box.Location = new Point(_textRect.Right, 0);
Box.Size = new Size(Width - _textRect.Width, Height);
Invalidate();
}
}
public TanjiLabelBox()
{
SetStyle((ControlStyles)2050, true);
DoubleBuffered = true;
Box = new TextBox();
Box.Anchor = (AnchorStyles.Left | AnchorStyles.Right);
ForeColor = Color.White;
BackColor = Color.SteelBlue;
Size = new Size(200, 20);
Controls.Add(Box);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
if (!string.IsNullOrWhiteSpace(Text))
{
using (var backColor = new SolidBrush(BackColor))
e.Graphics.FillRectangle(backColor, _textRect);
using (var shadow = new Pen(Color.FromArgb(50, Color.Black)))
{
e.Graphics.DrawRectangle(shadow, 0, 0,
_textRect.Width, _textRect.Height - 1);
}
using (var format = new StringFormat())
using (var foreColor = new SolidBrush(ForeColor))
{
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
using (var textShadowBrush = new SolidBrush(Color.FromArgb(100, Color.Black)))
{
e.Graphics.DrawString(Text, Font, textShadowBrush,
new Rectangle(1, 1, _textRect.Width, _textRect.Height), format);
}
e.Graphics.DrawString(Text, Font, foreColor, _textRect, format);
}
}
base.OnPaint(e);
}
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width, 20, specified);
}
protected override void Dispose(bool disposing)
{
if (!IsDisposed && disposing)
{
Box.Dispose();
}
base.Dispose(disposing);
}
}
}
\ No newline at end of file
using System;
using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
namespace Tangine.Controls
{
[DesignerCategory("Code")]
public class TanjiListView : ListView
{
private bool _lastSelectionState;
private ListViewItem _previouslySelectedItem, _expectedSelection;
/// <summary>
/// Occurs when an item's selection state differs from the previous state.
/// </summary>
public event EventHandler ItemSelectionStateChanged;
protected virtual void OnItemSelectionStateChanged(EventArgs e)
{
if (_lastSelectionState != HasSelectedItem)
{
_lastSelectionState = HasSelectedItem;
ItemSelectionStateChanged?.Invoke(this, e);
}
if (!_lastSelectionState)
_previouslySelectedItem = null;
OnItemSelected(e);
}
/// <summary>
/// Occurs when an item has been selected for the first time.
/// </summary>
public event EventHandler ItemSelected;
protected virtual void OnItemSelected(EventArgs e)
{
if (HasSelectedItem &&
(SelectedItem != _previouslySelectedItem))
{
_previouslySelectedItem = SelectedItem;
ItemSelected?.Invoke(this, e);
}
}
[DefaultValue(true)]
public bool LockColumnWidth { get; set; }
[Browsable(false)]
public bool HasSelectedItem => (SelectedItems.Count > 0);
[Browsable(false)]
public ListViewItem SelectedItem =>
(HasSelectedItem ? SelectedItems[0] : null);
[Browsable(false)]
public bool CanMoveSelectedItemUp
{
get
{
if (!HasSelectedItem) return false;
return (SelectedItem.Index >= 1);
}
}
[Browsable(false)]
public bool CanMoveSelectedItemDown
{
get
{
if (!HasSelectedItem) return false;
return (SelectedItem.Index != (Items.Count - 1));
}
}
public TanjiListView()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
GridLines = true;
View = View.Details;
MultiSelect = false;
FullRowSelect = true;
HideSelection = false;
LockColumnWidth = true;
ShowItemToolTips = true;
UseCompatibleStateImageBehavior = false;
HeaderStyle = ColumnHeaderStyle.Nonclickable;
}
public void ClearItems()
{
var items = new ListViewItem[Items.Count];
Items.CopyTo(items, 0);
ClearItems(items);
}
protected virtual void ClearItems(IEnumerable<ListViewItem> items)
{
try
{
BeginUpdate();
foreach (ListViewItem item in items)
RemoveItem(item, false);
}
finally { EndUpdate(); }
}
public void RemoveSelectedItem()
{
if (HasSelectedItem)
RemoveItem(SelectedItem, true);
}
public void RemoveItem(ListViewItem item)
{
RemoveItem(item, true);
}
protected virtual void RemoveItem(ListViewItem item, bool selectNext)
{
int index = item.Index;
selectNext = (Items.Count - 1 > 0 && selectNext);
Items.RemoveAt(index);
if (selectNext)
{
if (index >= Items.Count)
index = Items.Count - 1;
item = Items[index];
item.Selected = true;
EnsureVisible(item.Index);
}
OnItemSelectionStateChanged(EventArgs.Empty);
}
public void MoveSelectedItemUp()
{
if (HasSelectedItem)
MoveItemUp(SelectedItem);
}
protected virtual void MoveItemUp(ListViewItem item)
{
int oldIndex = item.Index;
if (oldIndex < 1) return;
_previouslySelectedItem = null;
BeginUpdate();
Items.RemoveAt(oldIndex);
Items.Insert(oldIndex - 1, item);
EndUpdate();
item.Selected = true;
OnItemSelectionStateChanged(EventArgs.Empty);
int index = item.Index;
EnsureVisible(index <= 4 ? 0 : index - 4);
}
public void MoveSelectedItemDown()
{
if (HasSelectedItem)
MoveItemDown(SelectedItem);
}
protected virtual void MoveItemDown(ListViewItem item)
{
int oldIndex = item.Index;
if (oldIndex == (Items.Count - 1)) return;
_previouslySelectedItem = null;
BeginUpdate();
Items.RemoveAt(oldIndex);
Items.Insert(oldIndex + 1, item);
EndUpdate();
item.Selected = true;
OnItemSelectionStateChanged(EventArgs.Empty);
int index = item.Index;
EnsureVisible(index + 4 >= Items.Count ? Items.Count - 1 : index + 4);
}
public void AddFocusedItem(ListViewItem item)
{
AddItem(item);
item.Selected = true;
OnItemSelectionStateChanged(EventArgs.Empty);
}
public ListViewItem AddFocusedItem(params object[] items)
{
ListViewItem item = AddItem(items);
item.Selected = true;
OnItemSelectionStateChanged(EventArgs.Empty);
return item;
}
public void AddItem(ListViewItem item)
{
Focus();
Items.Add(item);
item.EnsureVisible();
}
public ListViewItem AddItem(params object[] items)
{
string[] stringItems = items
.Select(i => i.ToString()).ToArray();
var item = new ListViewItem(stringItems);
AddItem(item);
return item;
}
protected override void OnMouseDown(MouseEventArgs e)
{
_expectedSelection = GetItemAt(e.X, e.Y);
base.OnMouseDown(e);
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
if (_expectedSelection == SelectedItem)
{
OnItemSelectionStateChanged(EventArgs.Empty);
}
base.OnSelectedIndexChanged(e);
}
protected override void OnColumnWidthChanging(ColumnWidthChangingEventArgs e)
{
if (LockColumnWidth && !DesignMode)
{
e.Cancel = true;
e.NewWidth = Columns[e.ColumnIndex].Width;
}
base.OnColumnWidthChanging(e);
}
}
}
\ No newline at end of file
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using Tangine.Habbo;
namespace Tangine.Controls
{
[DesignerCategory("Code")]
[DefaultEvent(nameof(TileClick))]
public class TanjiMap : Control
{
private string[] _rows;
private RectangleF _enteredRegion;
private readonly List<RectangleF> _tileRegions;
private readonly IDictionary<char, Color> _markers;
private readonly Dictionary<RectangleF, HPoint> _tiles;
private readonly IDictionary<int, IDictionary<int, Color>> _paints;
public event EventHandler<TileEventArgs> TileClick;
protected virtual void OnTileClick(TileEventArgs e)
{
TileClick?.Invoke(this, e);
}
public event EventHandler<TileEventArgs> TileDoubleClick;
protected virtual void OnTileDoubleClick(TileEventArgs e)
{
TileDoubleClick?.Invoke(this, e);
}
public event EventHandler<TileEventArgs> TileEnter;
protected virtual void OnTileEnter(TileEventArgs e)
{
TileEnter?.Invoke(this, e);
}
public event EventHandler<TileEventArgs> TileLeave;
protected virtual void OnTileLeave(TileEventArgs e)
{
TileLeave?.Invoke(this, e);
}
private Color _voidColor = Color.Black;
[DefaultValue(typeof(Color), "Black")]
public Color VoidColor
{
get { return _voidColor; }
set
{
_voidColor = value;
_markers['x'] = value;
Invalidate();
}
}
public TanjiMap()
{
SetStyle((ControlStyles)2050, true);
DoubleBuffered = true;
_tileRegions = new List<RectangleF>();
_markers = new Dictionary<char, Color>();
_tiles = new Dictionary<RectangleF, HPoint>();
_paints = new Dictionary<int, IDictionary<int, Color>>();
SetDefaultMarkers();
}
public void PaintMap(HMap map)
{
PaintMap(map.ToString());
}
public virtual void PaintMap(string map)
{
_paints.Clear();
_rows = HMap.Adjust(map).Split(new[] { '\r' },
StringSplitOptions.RemoveEmptyEntries);
Invalidate();
}
public void PaintTile(HPoint tile, Color marker)
{
PaintTile(tile.X, tile.Y, marker);
}
public virtual void PaintTile(int x, int y, Color marker)
{
if (!_paints.ContainsKey(y))
_paints[y] = new Dictionary<int, Color>();
_paints[y][x] = marker;
Invalidate();
}
public void SetMarker(double z, Color marker)
{
SetMarker(HPoint.ToLevel(z), marker);
}
public virtual void SetMarker(char level, Color marker)
{
_markers[level] = marker;
Invalidate();
}
public void SetLevel(int x, int y, double z)
{
SetLevel(x, y, HPoint.ToLevel(z));
}
public virtual void SetLevel(int x, int y, char level)
{
_rows[y] = _rows[y].Remove(x, 1)
.Insert(x, level.ToString());
Invalidate();
}
protected override void OnResize(EventArgs e)
{
Invalidate();
base.OnResize(e);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(BackColor);
if (_rows?.Length > 0)
{
_tiles.Clear();
_tileRegions.Clear();
float tileWidth = (ClientRectangle.Width -
(float)(_rows[0].Length - 1)) / _rows[0].Length;
float tileHeight = (ClientRectangle.Height -
(float)(_rows.Length - 1)) / _rows.Length;
for (int y = 0; y < _rows.Length; y++)
{
string row = _rows[y];
for (int x = 0; x < row.Length; x++)
{
var tileRegion = new RectangleF(x * (tileWidth + (x != 0 ? 1 : 0)),
y * (tileHeight + (y != 0 ? 1 : 0)), tileWidth, tileHeight);
Color marker = _markers[row[x]];
if (_paints.ContainsKey(y) && _paints[y].ContainsKey(x))
marker = _paints[y][x];
if (BackColor != marker)
{
using (var brush = new SolidBrush(marker))
e.Graphics.FillRectangle(brush, tileRegion);
}
_tileRegions.Add(tileRegion);
_tiles.Add(tileRegion, new HPoint(x, y, row[x]));
}
}
}
base.OnPaint(e);
}
protected override void OnMouseLeave(EventArgs e)
{
_enteredRegion = RectangleF.Empty;
base.OnMouseLeave(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (_enteredRegion.IsEmpty)
{
_enteredRegion = GetTileRegion(e.X, e.Y);
OnTileAction(_enteredRegion, OnTileEnter, e);
}
else if (!_enteredRegion.Contains(e.Location))
{
RectangleF region = GetTileRegion(e.X, e.Y);
OnTileAction(_enteredRegion, OnTileLeave, e);
OnTileAction(region, OnTileEnter, e);
_enteredRegion = region;
}
base.OnMouseMove(e);
}
protected override void OnMouseClick(MouseEventArgs e)
{
OnTileAction(OnTileClick, e);
base.OnMouseClick(e);
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
OnTileAction(OnTileDoubleClick, e);
base.OnMouseDoubleClick(e);
}
protected virtual void SetDefaultMarkers()
{
_markers['x'] = VoidColor;
_markers['0'] = Color.FromArgb(0, 101, 255);
_markers['1'] = Color.FromArgb(0, 145, 255);
_markers['2'] = Color.FromArgb(0, 188, 255);
_markers['3'] = Color.FromArgb(1, 232, 255);
_markers['4'] = Color.FromArgb(1, 255, 234);
_markers['5'] = Color.FromArgb(1, 255, 191);
_markers['6'] = Color.FromArgb(1, 255, 147);
_markers['7'] = Color.FromArgb(1, 255, 104);
_markers['8'] = Color.FromArgb(1, 255, 61);
_markers['9'] = Color.FromArgb(1, 255, 17);
_markers['a'] = Color.FromArgb(25, 255, 0);
_markers['b'] = Color.FromArgb(68, 255, 0);
_markers['c'] = Color.FromArgb(68, 255, 0);
_markers['d'] = Color.FromArgb(112, 255, 0);
_markers['e'] = Color.FromArgb(155, 255, 0);
_markers['f'] = Color.FromArgb(198, 255, 0);
_markers['g'] = Color.FromArgb(242, 255, 0);
_markers['h'] = Color.FromArgb(255, 224, 0);
_markers['i'] = Color.FromArgb(255, 181, 0);
_markers['j'] = Color.FromArgb(255, 137, 0);
_markers['k'] = Color.FromArgb(255, 94, 0);
_markers['l'] = Color.FromArgb(255, 50, 0);
_markers['m'] = Color.FromArgb(255, 7, 0);
_markers['n'] = Color.FromArgb(255, 0, 35);
_markers['o'] = Color.FromArgb(255, 0, 79);
_markers['p'] = Color.FromArgb(255, 0, 122);
_markers['q'] = Color.FromArgb(255, 0, 165);
_markers['r'] = Color.FromArgb(255, 0, 209);
_markers['s'] = Color.FromArgb(214, 0, 255);
_markers['t'] = Color.FromArgb(170, 0, 255);
}
protected virtual RectangleF GetTileRegion(int mouseX, int mouseY)
{
RectangleF[] tileRegions = _tileRegions.ToArray();
foreach (RectangleF tileRegion in tileRegions)
{
if (tileRegion.Contains(mouseX, mouseY))
return tileRegion;
}
return RectangleF.Empty;
}
private void OnTileAction(Action<TileEventArgs> tileAction, MouseEventArgs e)
{
RectangleF region = GetTileRegion(e.X, e.Y);
OnTileAction(region, tileAction, e);
}
protected virtual void OnTileAction(RectangleF region, Action<TileEventArgs> tileAction, MouseEventArgs e)
{
if (!region.IsEmpty)
{
HPoint tile = _tiles[region];
tileAction(new TileEventArgs(
tile, e.Button, e.Clicks, e.X, e.Y, e.Delta));
}
}
}
}
\ No newline at end of file
using System;
using System.Timers;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using Tangine.Network.Protocol;
namespace Tangine.Controls
{
[DesignerCategory("Code")]
public class TanjiSchedulerView : TanjiListView
{
private readonly Action<HSchedule> _updateItem;
private readonly Dictionary<HSchedule, ListViewItem> _items;
public event EventHandler<ScheduleTickEventArgs> ScheduleTick;
protected virtual void OnScheduleTick(object sender, ScheduleTickEventArgs e)
{
bool isDirty = false;
var schedule = (HSchedule)sender;
try
{
string oldpacket = e.Packet.ToString();
ScheduleTick?.Invoke(this, e);
string newPacket = e.Packet.ToString();
if (oldpacket != newPacket ||
e.Interval > 0 && schedule.Interval != e.Interval)
{
isDirty = true;
}
}
catch { e.Cancel = true; }
finally
{
schedule.IsRunning = !e.Cancel;
if (isDirty || e.Cancel)
Invoke(_updateItem, schedule);
}
}
public HSchedule SelectedSchedule
{
get
{
if (!HasSelectedItem)
return null;
var schedule =
(SelectedItem.Tag as HSchedule);
return schedule;
}
}
public TanjiSchedulerView()
{
_updateItem = UpdateItem;
_items = new Dictionary<HSchedule, ListViewItem>();
CheckBoxes = true;
}
public ListViewItem AddSchedule(HPacket packet, int interval, int cycles, bool autoStart)
{
ListViewItem item = AddFocusedItem(
packet, "TODO", interval, cycles);
var schedule = new HSchedule(packet, interval, cycles);
schedule.ScheduleTick += OnScheduleTick;
_items.Add(schedule, item);
item.Tag = schedule;
item.Checked = autoStart;
return item;
}
protected virtual void UpdateItem(HSchedule schedule)
{
ListViewItem item = _items[schedule];
if (item.Checked != schedule.IsRunning)
item.Checked = schedule.IsRunning;
item.SubItems[0].Text = schedule.Packet.ToString(); // TODO
//item.SubItems[1].Text = schedule.Packet.Destination.ToString();
item.SubItems[2].Text = schedule.Interval.ToString();
item.SubItems[3].Text = schedule.Cycles.ToString();
}
protected override void OnItemChecked(ItemCheckedEventArgs e)
{
var schedule = (e.Item.Tag as HSchedule);
if (schedule != null)
{
bool isRunning = e.Item.Checked;
schedule.IsRunning = isRunning;
}
base.OnItemChecked(e);
}
protected override void RemoveItem(ListViewItem item, bool selectNext)
{
var schedule = (item.Tag as HSchedule);
if (schedule != null)
{
if (_items.ContainsKey(schedule))
_items.Remove(schedule);
schedule.Dispose();
}
base.RemoveItem(item, selectNext);
}
public class HSchedule : IDisposable
{
private int _cycles;
private int _currentCycle;
private readonly System.Timers.Timer _ticker;
public event EventHandler<ScheduleTickEventArgs> ScheduleTick;
protected virtual void OnScheduleTick(ScheduleTickEventArgs e)
{
try
{
if (Cycles != 0 && _currentCycle >= Cycles)
{
IsRunning = false;
e.Cancel = true;
}
ScheduleTick?.Invoke(this, e);
}
catch { e.Cancel = true; }
finally
{
if (e.Cancel)