From 07ba75762b4516a7ed0c0286da1b590afce53c89 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 10 Feb 2017 00:25:50 +0100 Subject: [PATCH] - the first menu function has been scriptified. --- src/menu/colorpickermenu.cpp | 99 ++--------------- src/menu/menu.cpp | 17 ++- src/menu/menu.h | 3 + src/menu/menuinput.cpp | 10 +- src/menu/messagebox.cpp | 2 +- src/menu/optionmenu.cpp | 2 +- src/menu/optionmenuitems.h | 2 +- src/v_draw.cpp | 9 ++ wadsrc/static/zscript.txt | 1 + wadsrc/static/zscript/base.txt | 5 + .../static/zscript/menu/colorpickermenu.txt | 102 ++++++++++++++++++ wadsrc/static/zscript/menu/menu.txt | 24 ++++- 12 files changed, 176 insertions(+), 100 deletions(-) create mode 100644 wadsrc/static/zscript/menu/colorpickermenu.txt diff --git a/src/menu/colorpickermenu.cpp b/src/menu/colorpickermenu.cpp index def67d44b..7970382bb 100644 --- a/src/menu/colorpickermenu.cpp +++ b/src/menu/colorpickermenu.cpp @@ -55,6 +55,8 @@ class DColorPickerMenu : public DOptionMenu { DECLARE_CLASS(DColorPickerMenu, DOptionMenu) +public: + float mRed; float mGreen; float mBlue; @@ -66,8 +68,6 @@ class DColorPickerMenu : public DOptionMenu FColorCVar *mCVar; -public: - DColorPickerMenu(DMenu *parent, const char *name, DOptionMenuDescriptor *desc, FColorCVar *cvar) { mStartItem = desc->mItems.Size(); @@ -124,93 +124,6 @@ public: // //============================================================================= - bool MenuEvent (int mkey, bool fromcontroller) - { - int &mSelectedItem = mDesc->mSelectedItem; - - switch (mkey) - { - case MKEY_Down: - if (mSelectedItem == mStartItem+6) // last valid item - { - S_Sound (CHAN_VOICE | CHAN_UI, "menu/cursor", snd_menuvolume, ATTN_NONE); - mGridPosY = 0; - // let it point to the last static item so that the super class code still has a valid item - mSelectedItem = mStartItem+7; - return true; - } - else if (mSelectedItem == mStartItem+7) - { - if (mGridPosY < 15) - { - S_Sound (CHAN_VOICE | CHAN_UI, "menu/cursor", snd_menuvolume, ATTN_NONE); - mGridPosY++; - } - return true; - } - break; - - case MKEY_Up: - if (mSelectedItem == mStartItem+7) - { - if (mGridPosY > 0) - { - S_Sound (CHAN_VOICE | CHAN_UI, "menu/cursor", snd_menuvolume, ATTN_NONE); - mGridPosY--; - } - else - { - S_Sound (CHAN_VOICE | CHAN_UI, "menu/cursor", snd_menuvolume, ATTN_NONE); - mSelectedItem = mStartItem+6; - } - return true; - } - break; - - case MKEY_Left: - if (mSelectedItem == mStartItem+7) - { - S_Sound (CHAN_VOICE | CHAN_UI, "menu/cursor", snd_menuvolume, ATTN_NONE); - if (--mGridPosX < 0) mGridPosX = 15; - return true; - } - break; - - case MKEY_Right: - if (mSelectedItem == mStartItem+7) - { - S_Sound (CHAN_VOICE | CHAN_UI, "menu/cursor", snd_menuvolume, ATTN_NONE); - if (++mGridPosX > 15) mGridPosX = 0; - return true; - } - break; - - case MKEY_Enter: - if (mSelectedItem == mStartItem+7) - { - // Choose selected palette entry - int index = mGridPosX + mGridPosY * 16; - mRed = GPalette.BaseColors[index].r; - mGreen = GPalette.BaseColors[index].g; - mBlue = GPalette.BaseColors[index].b; - S_Sound (CHAN_VOICE | CHAN_UI, "menu/choose", snd_menuvolume, ATTN_NONE); - return true; - } - break; - } - if (mSelectedItem >= 0 && mSelectedItem < mStartItem+7) - { - if (mDesc->mItems[mDesc->mSelectedItem]->MenuEvent(mkey, fromcontroller)) return true; - } - return Super::MenuEvent(mkey, fromcontroller); - } - - //============================================================================= - // - // - // - //============================================================================= - bool MouseEvent(int type, int mx, int my) { int olditem = mDesc->mSelectedItem; @@ -355,3 +268,11 @@ DMenu *StartPickerMenu(DMenu *parent, const char *name, FColorCVar *cvar) } } + +DEFINE_FIELD(DColorPickerMenu, mRed); +DEFINE_FIELD(DColorPickerMenu, mGreen); +DEFINE_FIELD(DColorPickerMenu, mBlue); +DEFINE_FIELD(DColorPickerMenu, mGridPosX); +DEFINE_FIELD(DColorPickerMenu, mGridPosY); +DEFINE_FIELD(DColorPickerMenu, mStartItem); +DEFINE_FIELD(DColorPickerMenu, mCVar); diff --git a/src/menu/menu.cpp b/src/menu/menu.cpp index c3bc81a10..8b9094f72 100644 --- a/src/menu/menu.cpp +++ b/src/menu/menu.cpp @@ -55,6 +55,7 @@ #include "r_utility.h" #include "menu/menu.h" #include "textures/textures.h" +#include "virtual.h" // // Todo: Move these elsewhere @@ -208,6 +209,18 @@ DEFINE_ACTION_FUNCTION(DMenu, MenuEvent) ACTION_RETURN_BOOL(self->MenuEvent(key, fromcontroller)); } +bool DMenu::CallMenuEvent(int mkey, bool fromcontroller) +{ + IFVIRTUAL(DMenu, MenuEvent) + { + VMValue params[] = { (DObject*)this, mkey, fromcontroller }; + int retval; + VMReturn ret(&retval); + GlobalVMStack.Call(func, params, 3, &ret, 1, nullptr); + return retval; + } + else return MenuEvent(mkey, fromcontroller); +} //============================================================================= // // @@ -260,7 +273,7 @@ bool DMenu::MouseEventBack(int type, int x, int y) if (mBackbuttonSelected && type == MOUSE_Release) { if (m_use_mouse == 2) mBackbuttonSelected = false; - MenuEvent(MKEY_Back, true); + CallMenuEvent(MKEY_Back, true); } return mBackbuttonSelected; } @@ -661,7 +674,7 @@ bool M_Responder (event_t *ev) { MenuButtonTickers[mkey] = KEY_REPEAT_DELAY; } - DMenu::CurrentMenu->MenuEvent(mkey, fromcontroller); + DMenu::CurrentMenu->CallMenuEvent(mkey, fromcontroller); return true; } } diff --git a/src/menu/menu.h b/src/menu/menu.h index 854eab43f..a3c1f86ea 100644 --- a/src/menu/menu.h +++ b/src/menu/menu.h @@ -242,6 +242,9 @@ public: virtual bool TranslateKeyboardEvents(); virtual void Close(); virtual bool MouseEvent(int type, int x, int y); + + bool CallMenuEvent(int mkey, bool fromcontroller); + bool MouseEventBack(int type, int x, int y); void SetCapture(); void ReleaseCapture(); diff --git a/src/menu/menuinput.cpp b/src/menu/menuinput.cpp index 71646410d..ab372a6ae 100644 --- a/src/menu/menuinput.cpp +++ b/src/menu/menuinput.cpp @@ -138,7 +138,7 @@ bool DTextEnterMenu::Responder(event_t *ev) { DMenu *parent = mParentMenu; Close(); - parent->MenuEvent(MKEY_Abort, false); + parent->CallMenuEvent(MKEY_Abort, false); return true; } else if (ch == '\r') @@ -151,7 +151,7 @@ bool DTextEnterMenu::Responder(event_t *ev) DMenu *parent = mParentMenu; Close(); - parent->MenuEvent(MKEY_Input, false); + parent->CallMenuEvent(MKEY_Input, false); return true; } } @@ -183,7 +183,7 @@ bool DTextEnterMenu::MouseEvent(int type, int x, int y) InputGridY = (y - screen_y) / cell_height; if (type == DMenu::MOUSE_Release) { - if (MenuEvent(MKEY_Enter, true)) + if (CallMenuEvent(MKEY_Enter, true)) { S_Sound (CHAN_VOICE | CHAN_UI, "menu/choose", snd_menuvolume, ATTN_NONE); if (m_use_mouse == 2) InputGridX = InputGridY = -1; @@ -210,7 +210,7 @@ bool DTextEnterMenu::MenuEvent (int key, bool fromcontroller) { if (key == MKEY_Back) { - mParentMenu->MenuEvent(MKEY_Abort, false); + mParentMenu->CallMenuEvent(MKEY_Abort, false); return Super::MenuEvent(key, fromcontroller); } if (fromcontroller) @@ -262,7 +262,7 @@ bool DTextEnterMenu::MenuEvent (int key, bool fromcontroller) { DMenu *parent = mParentMenu; Close(); - parent->MenuEvent(MKEY_Input, false); + parent->CallMenuEvent(MKEY_Input, false); return true; } } diff --git a/src/menu/messagebox.cpp b/src/menu/messagebox.cpp index 86b7c3bfe..8494d2192 100644 --- a/src/menu/messagebox.cpp +++ b/src/menu/messagebox.cpp @@ -157,7 +157,7 @@ void DMessageBoxMenu::HandleResult(bool res) { if (mAction == NAME_None) { - mParentMenu->MenuEvent(res? MKEY_MBYes : MKEY_MBNo, false); + mParentMenu->CallMenuEvent(res? MKEY_MBYes : MKEY_MBNo, false); Close(); } else diff --git a/src/menu/optionmenu.cpp b/src/menu/optionmenu.cpp index 07168bffe..40524ac0c 100644 --- a/src/menu/optionmenu.cpp +++ b/src/menu/optionmenu.cpp @@ -496,7 +496,7 @@ bool DOptionMenuItem::MouseEvent(int type, int x, int y) { if (Selectable() && type == DMenu::MOUSE_Release) { - return DMenu::CurrentMenu->MenuEvent(MKEY_Enter, true); + return DMenu::CurrentMenu->CallMenuEvent(MKEY_Enter, true); } return false; } diff --git a/src/menu/optionmenuitems.h b/src/menu/optionmenuitems.h index d64fa5866..904bf8ff8 100644 --- a/src/menu/optionmenuitems.h +++ b/src/menu/optionmenuitems.h @@ -418,7 +418,7 @@ public: menuactive = MENU_On; SetMenuMessage(0); Close(); - mParentMenu->MenuEvent((ev->data1 == KEY_ESCAPE)? MKEY_Abort : MKEY_Input, 0); + mParentMenu->CallMenuEvent((ev->data1 == KEY_ESCAPE)? MKEY_Abort : MKEY_Input, 0); return true; } return false; diff --git a/src/v_draw.cpp b/src/v_draw.cpp index 6f0e8330a..4b9072015 100644 --- a/src/v_draw.cpp +++ b/src/v_draw.cpp @@ -103,6 +103,15 @@ DEFINE_ACTION_FUNCTION(_Screen, GetHeight) ACTION_RETURN_INT(screen->GetHeight()); } +DEFINE_ACTION_FUNCTION(_Screen, PaletteColor) +{ + PARAM_PROLOGUE; + PARAM_INT(index); + if (index < 0 || index > 255) index = 0; + else index = GPalette.BaseColors[index]; + ACTION_RETURN_INT(index); +} + static int PalFromRGB(uint32 rgb) { if (LastPal >= 0 && LastRGB == rgb) diff --git a/wadsrc/static/zscript.txt b/wadsrc/static/zscript.txt index 48ffc102f..ea6d581e1 100644 --- a/wadsrc/static/zscript.txt +++ b/wadsrc/static/zscript.txt @@ -8,6 +8,7 @@ #include "zscript/menu/menu.txt" //#include "zscript/menu/menuitembase.txt" +#include "zscript/menu/colorpickermenu.txt" #include "zscript/inventory/inventory.txt" #include "zscript/inventory/inv_misc.txt" diff --git a/wadsrc/static/zscript/base.txt b/wadsrc/static/zscript/base.txt index 5ba744a8e..269e32ef2 100644 --- a/wadsrc/static/zscript/base.txt +++ b/wadsrc/static/zscript/base.txt @@ -133,6 +133,7 @@ struct Screen native NUM_TEXT_COLORS }; + native static Color PaletteColor(int index); native static int GetWidth(); native static int GetHeight(); native static void DrawHUDTexture(TextureID tex, double x, double y); @@ -170,6 +171,10 @@ struct DamageTypeDefinition native native static bool IgnoreArmor(Name type); } +struct CVar native +{ +} + struct GameInfoStruct native { // will be extended as needed. diff --git a/wadsrc/static/zscript/menu/colorpickermenu.txt b/wadsrc/static/zscript/menu/colorpickermenu.txt new file mode 100644 index 000000000..017c89796 --- /dev/null +++ b/wadsrc/static/zscript/menu/colorpickermenu.txt @@ -0,0 +1,102 @@ + +class ColorpickerMenu : Menu native +{ + native float mRed; + native float mGreen; + native float mBlue; + + native int mGridPosX; + native int mGridPosY; + + native int mStartItem; + + native CVar mCVar; + + //============================================================================= + // + // + // + //============================================================================= + + override bool MenuEvent (int mkey, bool fromcontroller) + { + switch (mkey) + { + case MKEY_Down: + if (mDesc.mSelectedItem == mStartItem+6) // last valid item + { + MenuSound ("menu/cursor"); + mGridPosY = 0; + // let it point to the last static item so that the super class code still has a valid item + mDesc.mSelectedItem = mStartItem+7; + return true; + } + else if (mDesc.mSelectedItem == mStartItem+7) + { + if (mGridPosY < 15) + { + MenuSound ("menu/cursor"); + mGridPosY++; + } + return true; + } + break; + + case MKEY_Up: + if (mDesc.mSelectedItem == mStartItem+7) + { + if (mGridPosY > 0) + { + MenuSound ("menu/cursor"); + mGridPosY--; + } + else + { + MenuSound ("menu/cursor"); + mDesc.mSelectedItem = mStartItem+6; + } + return true; + } + break; + + case MKEY_Left: + if (mDesc.mSelectedItem == mStartItem+7) + { + MenuSound ("menu/cursor"); + if (--mGridPosX < 0) mGridPosX = 15; + return true; + } + break; + + case MKEY_Right: + if (mDesc.mSelectedItem == mStartItem+7) + { + MenuSound ("menu/cursor"); + if (++mGridPosX > 15) mGridPosX = 0; + return true; + } + break; + + case MKEY_Enter: + if (mDesc.mSelectedItem == mStartItem+7) + { + // Choose selected palette entry + int index = mGridPosX + mGridPosY * 16; + color col = Screen.PaletteColor(index); + mRed = col.r; + mGreen = col.g; + mBlue = col.b; + MenuSound ("menu/choose"); + return true; + } + break; + } + if (mDesc.mSelectedItem >= 0 && mDesc.mSelectedItem < mStartItem+7) + { + if (mDesc.mItems[mDesc.mSelectedItem].MenuEvent(mkey, fromcontroller)) return true; + } + return Super.MenuEvent(mkey, fromcontroller); + } + + +} \ No newline at end of file diff --git a/wadsrc/static/zscript/menu/menu.txt b/wadsrc/static/zscript/menu/menu.txt index c47c93d05..2dbfa6e79 100644 --- a/wadsrc/static/zscript/menu/menu.txt +++ b/wadsrc/static/zscript/menu/menu.txt @@ -1,4 +1,25 @@ +enum EMenuKey +{ + MKEY_Up, + MKEY_Down, + MKEY_Left, + MKEY_Right, + MKEY_PageUp, + MKEY_PageDown, + MKEY_Enter, + MKEY_Back, + MKEY_Clear, + NUM_MKEYS, + + // These are not buttons but events sent from other menus + + MKEY_Input, + MKEY_Abort, + MKEY_MBYes, + MKEY_MBNo, +} + class Menu : Object native { //native static int MenuTime(); @@ -25,7 +46,8 @@ class MenuItemBase : object native native Name mAction; native bool mEnabled; - native virtual bool MenuEvent (int mkey, bool fromcontroller); + // making this virtual now would require exporting all classes at once. + native /*virtual*/ bool MenuEvent (int mkey, bool fromcontroller); }