當鼠標在右鍵快捷按鈕圖標上點擊時,我將availableMagic中的所有對象以圖標的形式添加進名為rightButtonMagicList的WrapPanel中,由於此Demo中圖標的寬高均為27像素,因此我約束rightButtonMagicList寬為270,即一行如果超過10個圖標則自動換行。最後為它賦予相應的事件,當點中某個魔法技能,主角當前的右鍵默認魔法更改為此魔法,且右鍵快捷按鈕圖標的魔法也換成它:
……
//右鍵魔法選擇
rightButtonMagicIcon.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e) {
if (rightButtonMagicList == null) {
rightButtonMagicList = new WrapPanel() {
Orientation = OrIEntation.Horizontal,
Width = 270, //一行放10個圖標,每個27寬};
foreach (Magic magic in availableMagic) {
QXIcon magicIcon = new QXIcon(IconTypes.Highlights) {
BodySource = new ImageBrush() { ImageSource = Super.GetImage(string.Format("/Image/Magic/{0}/0.png", magic.Code)) },
NewSource = new ImageBrush() { ImageSource = Super.GetImage("/Image/Icon/34.png") },
Width = 27,
Height = 27,
Tip = magic.Name,
Tag = magic,
};
magicIcon.MouseLeftButtonDown += delegate(object ss, MouseButtonEventArgs ee) {
QXIcon icon = ss as QXIcon;
Magic m = icon.Tag as Magic;
rightButtonMagicIcon.BodySource = icon.BodySource;
rightButtonMagicIcon.Tip = m.Name;
Leader.CurrentMagic.Code = m.Code;
Leader.CurrentMagic.Level = m.Level;
rightButtonMagicList.Visibility = Visibility.Collapsed;
ee.Handled = true; };
rightButtonMagicList.Children.Add(magicIcon);
}
BottomMenu.Children.Add(rightButtonMagicList);
Canvas.SetLeft(rightButtonMagicList, 437); Canvas.SetTop(rightButtonMagicList, -Math.Ceiling(rightButtonMagicList.Children.Count / (rightButtonMagicList.Width / 27)) * 27);
} else {
if (rightButtonMagicList.Visibility == Visibility.Collapsed) {
rightButtonMagicList.Visibility = Visibility.Visible;
Canvas.SetTop(rightButtonMagicList, -rightButtonMagicList.ActualHeight);
} else {
rightButtonMagicList.Visibility = Visibility.Collapsed;
}
}
e.Handled = true;
……
具體邏輯大家仔細看就明白了~還算比較簡單的。實際效果如下:
另外的,大家不妨自行修改一下循環代碼,多加幾個魔法上去體驗一下WrapPanel的強大:
游戲主界面菜單已初具雛形,接下來的任務就是去完善它的各面板及相應功能~敬請關注。