using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
namespace Shared.Phone.UserCenter
{
///
/// 房间组合或者设备组合的菜单控件(完成初始化后,会根据默认选择调用回调函数)
///
public class RoomDeviceGroupMenuControl : FrameLayout
{
#region ■ 变量声明___________________________
///
/// 房间选择事件
///
public Action SelectRoomEvent = null;
///
/// 设备选择事件
///
public Action> SelectDeviceEvent = null;
///
/// 当前选择的主键
///
public string nowSelectKeys
{
get
{
if (dicDevice != null)
{
return oldSelectKeys.ToString();
}
return dicRoom[oldSelectKeys].Id;
}
}
///
/// 当前选择的主键(内部使用)
///
private int oldSelectKeys = -1;
///
/// 房间对象
///
private Dictionary dicRoom = null;
///
/// 设备对象
///
private Dictionary> dicDevice = null;
///
/// 前回选择的控件
///
private FrameLayoutControl oldFrameBack = null;
///
/// 前回选择的控件
///
private NormalViewControl oldBtnText = null;
#endregion
#region ■ 初始化_____________________________
///
/// 房间组合的菜单控件(完成初始化后,会根据默认选择调用回调函数)
///
/// 房间列表
public RoomDeviceGroupMenuControl(List i_listRoom)
{
this.dicRoom = new Dictionary();
for (int i = 0; i < i_listRoom.Count; i++)
{
this.dicRoom[i] = i_listRoom[i];
}
this.Height = Application.GetRealHeight(204);
}
///
/// 设备组合的菜单控件
///
/// 设备列表,keys:R文件里面的数字编号
public RoomDeviceGroupMenuControl(Dictionary> i_dicDevice)
{
this.dicDevice = i_dicDevice;
this.Height = Application.GetRealHeight(204);
}
///
/// 初始化控件(事件要至于它之前)
///
public void InitControl()
{
var scrolContr = new HorizontalScrolViewLayout();
scrolContr.Gravity = Gravity.CenterVertical;
scrolContr.Height = this.Height;
this.AddChidren(scrolContr);
//弄个空的进去占位置
var frameTemp = new FrameLayout();
frameTemp.Height = this.Height;
frameTemp.Width = ControlCommonResourse.XXLeft / 2;
scrolContr.AddChidren(frameTemp);
var btnTempWidth = new ButtonBase();
btnTempWidth.TextSize = 12;
if (this.dicRoom != null)
{
//初始化房间模式的控件
foreach (int keys in dicRoom.Keys)
{
if (oldSelectKeys == -1)
{
//初始值
oldSelectKeys = keys;
}
//计算字符的真实宽度
btnTempWidth.Text = dicRoom[keys].Name;
this.DoInitControl(scrolContr, keys, dicRoom[keys].Name, btnTempWidth.GetRealWidthByText());
}
}
else
{
//初始化设备模式的控件
int index = 0;
foreach (int keys in dicDevice.Keys)
{
if (oldSelectKeys == -1)
{
//初始值
oldSelectKeys = keys;
}
//计算字符的真实宽度
btnTempWidth.Text = Language.StringByID(keys);
this.DoInitControl(scrolContr, keys, btnTempWidth.Text, btnTempWidth.GetRealWidthByText());
index++;
}
}
//让控件滑动到指定的索引位置
scrolContr.ScrollToViewIndex(oldSelectKeys + 1);
}
///
/// 初始化控件
///
/// 列表控件
/// 字典的主键
/// 显示文本
/// 文本真实宽度
private void DoInitControl(HorizontalScrolViewLayout scrolContr, int keys, string text, int textWidth)
{
//带底图控件的最小宽度
int minWidth = Application.GetRealWidth(255);
//带底图控件的真实宽度 <文字的宽度的计算 frameBack.Width - (int)(frameBack.Height * 0.47)>
int realWidth = textWidth + (int)(Application.GetRealHeight(159) * 0.47);
if (realWidth < minWidth)
{
realWidth = minWidth;
}
//带图片的
var frameBack = new FrameLayoutControl();
frameBack.UseClickStatu = false;
frameBack.Gravity = Gravity.Center;
frameBack.Height = Application.GetRealHeight(159);
frameBack.Width = realWidth;
if (oldSelectKeys == keys)
{
frameBack.BackgroundImagePath = "Item/RoomIconBackgroundSelected.png";
oldFrameBack = frameBack;
}
else
{
frameBack.BackgroundImagePath = "Item/RoomIconBackground.png";
}
scrolContr.AddChidren(frameBack);
//文字
var btnText = new NormalViewControl(textWidth, frameBack.Height, false);
btnText.Gravity = Gravity.CenterHorizontal;
btnText.Text = text;
btnText.TextSize = 12;
btnText.TextAlignment = TextAlignment.Center;
if (oldSelectKeys == keys)
{
btnText.TextColor = UserCenterColor.Current.White;
oldBtnText = btnText;
}
else
{
btnText.TextColor = UserCenterColor.Current.TextGrayColor1;
}
frameBack.AddChidren(btnText, ChidrenBindMode.BindEventOnly);
frameBack.ButtonClickEvent += (sender, e) =>
{
if (oldSelectKeys == keys)
{
//同一个东西
return;
}
oldSelectKeys = keys;
//状态变更
frameBack.BackgroundImagePath = "Item/RoomIconBackgroundSelected.png";
btnText.TextColor = UserCenterColor.Current.White;
oldFrameBack.BackgroundImagePath = "Item/RoomIconBackground.png";
oldBtnText.TextColor = UserCenterColor.Current.TextGrayColor1;
oldFrameBack = frameBack;
oldBtnText = btnText;
//调用回调函数
if (this.dicDevice != null)
{
this.SelectDeviceEvent?.Invoke(this.dicDevice[keys]);
}
else if (this.dicRoom != null)
{
this.SelectRoomEvent?.Invoke(this.dicRoom[keys]);
}
};
//调用回调函数
if (oldSelectKeys == keys)
{
if (this.dicDevice != null)
{
this.SelectDeviceEvent?.Invoke(this.dicDevice[keys]);
}
else if (this.dicRoom != null)
{
this.SelectRoomEvent?.Invoke(this.dicRoom[keys]);
}
}
}
#endregion
#region ■ 控件摧毁___________________________
///
/// 控件摧毁
///
public override void RemoveFromParent()
{
this.SelectDeviceEvent = null;
this.SelectRoomEvent = null;
base.RemoveFromParent();
}
#endregion
#region ■ 一般方法___________________________
///
/// 设置初始选择(请在初始化完成之前调用)
///
/// 房间ID 或者 R文件里面的数值编号(转为字符串类型)
public void SetDefultIndex(string keys)
{
if (dicRoom != null)
{
foreach (int index in dicRoom.Keys)
{
if (dicRoom[index].Id == keys)
{
oldSelectKeys = index;
break;
}
}
}
else if (keys != string.Empty)
{
int i = 0;
int textId = Convert.ToInt32(keys);
foreach (int rId in dicDevice.Keys)
{
if (rId == textId)
{
oldSelectKeys = i;
break;
}
i++;
}
}
}
#endregion
}
}