From b18ec8ccd3fa56ee851ad2cd83ae056f6c5f1702 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Thu, 26 Jan 2023 10:55:15 -0500 Subject: [PATCH] Support for custom menu widget --- BeefLibs/Beefy2D/src/theme/dark/DarkMenu.bf | 44 ++++++++++++++++++--- BeefLibs/Beefy2D/src/widgets/Menu.bf | 25 ++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/BeefLibs/Beefy2D/src/theme/dark/DarkMenu.bf b/BeefLibs/Beefy2D/src/theme/dark/DarkMenu.bf index 91ef38dc..dea78e05 100644 --- a/BeefLibs/Beefy2D/src/theme/dark/DarkMenu.bf +++ b/BeefLibs/Beefy2D/src/theme/dark/DarkMenu.bf @@ -22,6 +22,9 @@ namespace Beefy.theme.dark { base.Draw(g); + if (mMenuItem.mWidget != null) + return; + if (mMenuItem.mLabel == null) g.DrawButton(DarkTheme.sDarkTheme.GetImage(DarkTheme.ImageIdx.MenuSepHorz), GS!(28), 0, mWidth - GS!(32)); else @@ -107,6 +110,16 @@ namespace Beefy.theme.dark public override void Update() { base.Update(); + + for (var item in mMenuItem.mItems) + { + if (item.mWidget != null) + { + if (item.mWidget.mMouseOver) + mDeselectedTicks = 0; + } + } + if (mMenuItem.mItems.Count > 0) { if (mIndex == mMenuWidget.mSelectIdx) @@ -319,6 +332,10 @@ namespace Beefy.theme.dark /*using (g.PushColor(0xFFFF0000)) g.FillRect(0, 0, mWidth, mHeight);*/ + for (var item in mItemWidgets) + if (item.mMenuItem.mWidget != null) + return; + float mDrawHeight = mHeight; g.DrawButtonVert(DarkTheme.sDarkTheme.GetImage(DarkTheme.ImageIdx.MenuSepVert), GS!(18), GS!(2), mDrawHeight - GS!(4)); @@ -359,12 +376,20 @@ namespace Beefy.theme.dark float curY = GS!(2); for (MenuItemWidget item in mItemWidgets) { - if (item.mMenuItem.mLabel != null) - maxWidth = Math.Max(maxWidth, mFont.GetWidth(item.mMenuItem.mLabel)); - - item.Resize(0, curY, mWidth - GS!(8), mItemSpacing); - item.mMouseInsets = new Insets(0, GS!(6), 0, 0); - curY += mItemSpacing; + if (item.mMenuItem.mWidget != null) + { + item.mMenuItem.mWidget.mX = GS!(4); + item.mMenuItem.mWidget.mY = curY; + } + else + { + if (item.mMenuItem.mLabel != null) + maxWidth = Math.Max(maxWidth, mFont.GetWidth(item.mMenuItem.mLabel)); + + item.Resize(0, curY, mWidth - GS!(8), mItemSpacing); + item.mMouseInsets = new Insets(0, GS!(6), 0, 0); + curY += mItemSpacing; + } } } @@ -373,6 +398,13 @@ namespace Beefy.theme.dark float maxWidth = 0; for (MenuItemWidget item in mItemWidgets) { + if (item.mMenuItem.mWidget != null) + { + mWidth = item.mMenuItem.mWidget.mWidth + GS!(6); + mHeight = item.mMenuItem.mWidget.mHeight + GS!(4); + return; + } + if (item.mMenuItem.mLabel != null) maxWidth = Math.Max(maxWidth, mFont.GetWidth(item.mMenuItem.mLabel)); } diff --git a/BeefLibs/Beefy2D/src/widgets/Menu.bf b/BeefLibs/Beefy2D/src/widgets/Menu.bf index 382d0256..c10850fe 100644 --- a/BeefLibs/Beefy2D/src/widgets/Menu.bf +++ b/BeefLibs/Beefy2D/src/widgets/Menu.bf @@ -21,8 +21,22 @@ namespace Beefy.widgets public this(Menu menuItem) { mMenuItem = menuItem; + if (mMenuItem.mWidget != null) + AddWidget(mMenuItem.mWidget); } + public ~this() + { + + } + + protected override void RemovedFromWindow() + { + base.RemovedFromWindow(); + if (mMenuItem.mWidget != null) + RemoveWidget(mMenuItem.mWidget); + } + public override void MouseEnter() { base.MouseEnter(); @@ -570,11 +584,13 @@ namespace Beefy.widgets public Event mOnMenuItemSelected ~ _.Dispose(); public Event mOnMenuItemUpdate ~ _.Dispose(); public Event mOnMenuClosed ~ _.Dispose(); + public Event mOnChildOpen ~ _.Dispose(); public Object mThemeData; public bool mDisabled; public bool mBold; public IDrawable mIconImage; public bool mForceParent; + public Widget mWidget ~ delete _; public ~this() { @@ -613,6 +629,15 @@ namespace Beefy.widgets return item; } + public virtual Menu AddWidgetItem(Widget widget) + { + Menu item = new Menu(); + item.mParent = this; + item.mWidget = widget; + mItems.Add(item); + return item; + } + public void SetDisabled(bool disabled) { mDisabled = disabled;