using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone.UserCenter
{
///
/// 做成一个存在于右上角的菜单控件
///
public class TopRightMenuControl : FrameLayout
{
#region ■ 变量声明___________________________
///
/// 列表控件
///
private VerticalListControl listView = null;
///
/// 前回选择的行
///
private FrameRowControl oldRowFrame = null;
///
/// 行高度
///
private int RowHeight = 150;
///
/// 行宽度
///
private int RowWidth = 395;
///
/// 行数
///
private int RowCount = 0;
#endregion
#region ■ 初始化_____________________________
///
/// 做成一个存在于右上角的菜单控件
///
/// 一共有几行
/// 这个菜单有多宽
/// 标题文本(如果不为空,菜单模式变更为拥有标题的模式)
public TopRightMenuControl(int i_RowCount, int i_width = 395, string titleText = null)
{
this.RowCount = i_RowCount;
this.RowWidth = i_width;
//初始化画面的控件
this.InitFormControl(titleText);
}
///
/// 初始化画面的控件
///
private void InitFormControl(string titleText)
{
this.BackgroundColor = UserCenterColor.Current.DialogBackColor;
this.MouseUpEventHandler += (sender2, e2) =>
{
//关闭自身
this.RemoveFromParent();
};
var frame = (FrameLayout)UserView.HomePage.Instance.GetChildren(UserView.HomePage.Instance.ChildrenCount - 1);
frame.AddChidren(this);
//最大显示5个
int rowCount = this.RowCount > 5 ? 5 : this.RowCount;
//桌布
var frameTable = new FrameLayoutControl(false);
frameTable.X = Application.GetRealWidth(662 - (RowWidth - 395));
frameTable.Y = Application.GetRealHeight(158 + 15 + 1);
frameTable.Width = Application.GetRealWidth(RowWidth);
frameTable.Height = Application.GetRealHeight(RowHeight * rowCount);
frameTable.BackgroundColor = UserCenterColor.Current.White;
frameTable.BorderColor = UserCenterColor.Current.Transparent;
frameTable.RadiusEx = 6;
this.AddChidren(frameTable);
if (titleText != null)
{
var btnTitle = new NormalViewControl(frameTable.Width - Application.GetRealWidth(81), Application.GetRealHeight(58), false);
btnTitle.X = Application.GetRealWidth(81);
btnTitle.Y = Application.GetRealHeight(58);
btnTitle.Text = titleText;
frameTable.AddChidren(btnTitle);
}
//列表控件
this.listView = new VerticalListControl();
listView.Height = frameTable.Height;
if (rowCount == 5)
{
//连带标题,只能显示5行
listView.Height = frameTable.Height - Application.GetRealHeight(RowHeight);
}
if (titleText != null)
{
//拥有标题
listView.Y = Application.GetRealHeight(RowHeight);
}
listView.Radius = frameTable.Radius;
frameTable.AddChidren(listView);
//三角形图标
var btnTriangle = new PicViewControl(31, 15);
btnTriangle.X = Application.GetRealWidth(980);
btnTriangle.Y = Application.GetRealHeight(159);
btnTriangle.UnSelectedImagePath = "Item/UpperTriangle.png";
this.AddChidren(btnTriangle);
}
#endregion
#region ■ 添加菜单___________________________
///
/// 添加菜单行
///
/// 显示的文字
/// 图片
/// 图片
/// 单击菜单执行的事件
/// 单击的时候,关闭菜单
public void AddRowMenu(string TextValue, string unSelectPic, string selectPic, Action action, bool closeOnClick = true)
{
var rowFrame = new FrameRowControl();
rowFrame.LeftOffset = Application.GetRealWidth(81) - ControlCommonResourse.XXLeft;
rowFrame.Height = Application.GetRealHeight(RowHeight);
listView.AddChidren(rowFrame);
rowFrame.MainKeys = listView.ChildrenCount.ToString();
//图标
var btnIcon = rowFrame.AddLeftIcon(81);
btnIcon.UnSelectedImagePath = unSelectPic;
btnIcon.SelectedImagePath = selectPic;
//显示文字
var btnText = rowFrame.AddLeftCaption(TextValue, RowWidth - 173);
btnText.X = Application.GetRealWidth(173);
//底线
if (listView.ChildrenCount != this.RowCount)
{
var btnLine = rowFrame.AddBottomLine();
btnLine.X = Application.GetRealWidth(81);
}
if (listView.ChildrenCount == 1)
{
//第一个菜单默认设置为选择状态
this.SetRowSelectStatu(rowFrame, true);
this.oldRowFrame = rowFrame;
}
else
{
//其他菜单为灰色
btnIcon.IsSelected = false;
btnText.TextColor = UserCenterColor.Current.TextGrayColor1;
}
//选择状态
rowFrame.SelectStatuEvent += (statu) =>
{
//false为控件自身自动执行,这里不需要还原
if (statu == true)
{
//设置为选择状态
this.SetRowSelectStatu(rowFrame, true);
this.oldRowFrame = rowFrame;
}
};
//按键点击
rowFrame.ButtonClickEvent += (sender, e) =>
{
if (closeOnClick == true)
{
this.RemoveFromParent();
}
action?.Invoke();
};
}
#endregion
#region ■ 设置选择状态_______________________
///
/// 设置选择状态
///
/// 行控件
/// 选择的状态
private void SetRowSelectStatu(FrameRowControl frame, bool select)
{
//图标
var btnIcon = (IconViewControl)frame.GetChildren(0);
if (btnIcon != null)
{
btnIcon.IsSelected = select;
}
//文本
var btnText = (NormalViewControl)frame.GetChildren(1);
if (btnText != null)
{
btnText.TextColor = select == true ? UserCenterColor.Current.TextColor1 : UserCenterColor.Current.TextGrayColor1;
}
//前回选择的菜单为null,或者是相同的东西,则不处理
if (this.oldRowFrame == null || this.oldRowFrame.MainKeys == frame.MainKeys)
{
return;
}
//前回选择的行还原
this.SetRowSelectStatu(this.oldRowFrame, false);
}
#endregion
}
}