From f0e925c5a79728922adf8a85b954125c9713ccea Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 12 Feb 2017 20:20:47 +0100 Subject: [PATCH] - scripted color picker fully working. --- src/CMakeLists.txt | 1 - src/menu/colorpickermenu.cpp | 151 ------------------ src/menu/menu.cpp | 23 +++ src/menu/optionmenu.cpp | 7 + src/scripting/zscript/zcc_compile.cpp | 1 + wadsrc/static/zscript.txt | 1 + .../static/zscript/menu/colorpickermenu.txt | 75 +++++++-- wadsrc/static/zscript/menu/menu.txt | 47 +++++- .../static/zscript/menu/optionmenuitems.txt | 69 +++++--- 9 files changed, 186 insertions(+), 189 deletions(-) delete mode 100644 src/menu/colorpickermenu.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9448006e4..b41b87cc1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -839,7 +839,6 @@ set( FASTMATH_PCH_SOURCES SkylineBinPack.cpp intermission/intermission.cpp intermission/intermission_parse.cpp - menu/colorpickermenu.cpp menu/joystickmenu.cpp menu/listmenu.cpp menu/loadsavemenu.cpp diff --git a/src/menu/colorpickermenu.cpp b/src/menu/colorpickermenu.cpp deleted file mode 100644 index bbd52f438..000000000 --- a/src/menu/colorpickermenu.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* -** colorpickermenu.cpp -** The color picker menu -** -**--------------------------------------------------------------------------- -** Copyright 2010 Christoph Oelckers -** All rights reserved. -** -** Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions -** are met: -** -** 1. Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** 2. Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in the -** documentation and/or other materials provided with the distribution. -** 3. The name of the author may not be used to endorse or promote products -** derived from this software without specific prior written permission. -** -** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR -** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES -** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. -** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, -** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT -** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -**--------------------------------------------------------------------------- -** -*/ -#include - -#include "menu/menu.h" -#include "c_dispatch.h" -#include "w_wad.h" -#include "sc_man.h" -#include "v_font.h" -#include "g_level.h" -#include "d_player.h" -#include "v_video.h" -#include "gi.h" -#include "i_system.h" -#include "c_bind.h" -#include "v_palette.h" -#include "d_event.h" -#include "d_gui.h" - -class DColorPickerMenu : public DOptionMenu -{ - DECLARE_CLASS(DColorPickerMenu, DOptionMenu) - -public: - - float mRed; - float mGreen; - float mBlue; - - int mGridPosX; - int mGridPosY; - - int mStartItem; - - FColorCVar *mCVar; - - DColorPickerMenu(DMenu *parent, const char *name, DOptionMenuDescriptor *desc, FColorCVar *cvar) - { - mStartItem = desc->mItems.Size(); - mRed = (float)RPART(DWORD(*cvar)); - mGreen = (float)GPART(DWORD(*cvar)); - mBlue = (float)BPART(DWORD(*cvar)); - mGridPosX = 0; - mGridPosY = 0; - mCVar = cvar; - - // This menu uses some featurs that are hard to implement in an external control lump - // so it creates its own list of menu items. - desc->mItems.Resize(mStartItem+8); - desc->mItems[mStartItem+0] = CreateOptionMenuItemStaticText(name, false); - desc->mItems[mStartItem+1] = CreateOptionMenuItemStaticText(" ", false); - desc->mItems[mStartItem+2] = CreateOptionMenuSliderVar("Red", 0, 0, 255, 15, 0); - desc->mItems[mStartItem+3] = CreateOptionMenuSliderVar("Green", 1, 0, 255, 15, 0); - desc->mItems[mStartItem+4] = CreateOptionMenuSliderVar("Blue", 2, 0, 255, 15, 0); - desc->mItems[mStartItem+5] = CreateOptionMenuItemStaticText(" ", false); - desc->mItems[mStartItem+6] = CreateOptionMenuItemCommand("Undo changes", "undocolorpic"); - desc->mItems[mStartItem+7] = CreateOptionMenuItemStaticText(" ", false); - for (auto &p : desc->mItems) - { - GC::WriteBarrier(p); - } - desc->mSelectedItem = mStartItem + 2; - Init(parent, desc); - desc->mIndent = 0; - desc->CalcIndent(); - } - - void OnDestroy() override - { - if (mStartItem >= 0) - { - mDesc->mItems.Resize(mStartItem); - UCVarValue val; - val.Int = MAKERGB(int(mRed), int(mGreen), int(mBlue)); - if (mCVar != NULL) mCVar->SetGenericRep (val, CVAR_Int); - mStartItem = -1; - } - } - - void Reset() - { - mRed = (float)RPART(DWORD(*mCVar)); - mGreen = (float)GPART(DWORD(*mCVar)); - mBlue = (float)BPART(DWORD(*mCVar)); - } - -}; - -IMPLEMENT_CLASS(DColorPickerMenu, true, false) - -CCMD(undocolorpic) -{ - if (DMenu::CurrentMenu != NULL && DMenu::CurrentMenu->IsKindOf(RUNTIME_CLASS(DColorPickerMenu))) - { - static_cast(DMenu::CurrentMenu)->Reset(); - } -} - - -DMenu *StartPickerMenu(DMenu *parent, const char *name, FColorCVar *cvar) -{ - DMenuDescriptor **desc = MenuDescriptors.CheckKey(NAME_Colorpickermenu); - if (desc != NULL && (*desc)->IsKindOf(RUNTIME_CLASS(DOptionMenuDescriptor))) - { - return new DColorPickerMenu(parent, name, (DOptionMenuDescriptor*)(*desc), cvar); - } - else - { - return NULL; - } -} - - -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 0653cc9ec..595563c0d 100644 --- a/src/menu/menu.cpp +++ b/src/menu/menu.cpp @@ -107,6 +107,14 @@ IMPLEMENT_CLASS(DMenuDescriptor, false, false) IMPLEMENT_CLASS(DListMenuDescriptor, false, false) IMPLEMENT_CLASS(DOptionMenuDescriptor, false, false) +DEFINE_ACTION_FUNCTION(DMenuDescriptor, GetDescriptor) +{ + PARAM_PROLOGUE; + PARAM_NAME(name); + DMenuDescriptor **desc = MenuDescriptors.CheckKey(name); + auto retn = desc ? *desc : nullptr; + ACTION_RETURN_POINTER(retn); +} size_t DListMenuDescriptor::PropagateMark() { @@ -1139,6 +1147,21 @@ CCMD(reset2saved) } +// This really should be in the script but we can't do scripted CCMDs yet. +CCMD(undocolorpic) +{ + if (DMenu::CurrentMenu != NULL) + { + IFVIRTUALPTR(DMenu::CurrentMenu, DMenu, ResetColor) + { + VMValue params[] = { (DObject*)DMenu::CurrentMenu }; + GlobalVMStack.Call(func, params, countof(params), nullptr, 0, nullptr); + } + } +} + + + //native void OptionMenuDescriptor.CalcIndent(); //native OptionMenuItem OptionMenuDescriptor.GetItem(Name iname); diff --git a/src/menu/optionmenu.cpp b/src/menu/optionmenu.cpp index bc4c7974e..57248f891 100644 --- a/src/menu/optionmenu.cpp +++ b/src/menu/optionmenu.cpp @@ -487,6 +487,13 @@ void DOptionMenuDescriptor::CalcIndent() mIndent = widest + 4; } +DEFINE_ACTION_FUNCTION(DOptionMenuDescriptor, CalcIndent) +{ + PARAM_SELF_PROLOGUE(DOptionMenuDescriptor); + self->CalcIndent(); + return 0; +} + //============================================================================= // // diff --git a/src/scripting/zscript/zcc_compile.cpp b/src/scripting/zscript/zcc_compile.cpp index 517f62654..500336df5 100644 --- a/src/scripting/zscript/zcc_compile.cpp +++ b/src/scripting/zscript/zcc_compile.cpp @@ -589,6 +589,7 @@ void ZCCCompiler::CreateClassTypes() { Error(c->cls, "Class name %s already exists", c->NodeName().GetChars()); } + else DPrintf(DMSG_SPAMMY, "Created class %s with parent %s\n", c->Type()->TypeName.GetChars(), c->Type()->ParentClass->TypeName.GetChars()); } catch (CRecoverableError &err) { diff --git a/wadsrc/static/zscript.txt b/wadsrc/static/zscript.txt index 8774f2a7b..637d5b66e 100644 --- a/wadsrc/static/zscript.txt +++ b/wadsrc/static/zscript.txt @@ -10,6 +10,7 @@ #include "zscript/menu/menu.txt" #include "zscript/menu/listmenuitems.txt" #include "zscript/menu/optionmenuitems.txt" +#include "zscript/menu/colorpickermenu.txt" #include "zscript/menu/joystickmenu.txt" #include "zscript/menu/playerdisplay.txt" #include "zscript/menu/playermenu.txt" diff --git a/wadsrc/static/zscript/menu/colorpickermenu.txt b/wadsrc/static/zscript/menu/colorpickermenu.txt index ff21e1b8e..93a2399a5 100644 --- a/wadsrc/static/zscript/menu/colorpickermenu.txt +++ b/wadsrc/static/zscript/menu/colorpickermenu.txt @@ -43,10 +43,11 @@ class OptionMenuSliderVar : OptionMenuSliderBase { int mIndex; - void Init(String label, int index, double min, double max, double step, int showval) + OptionMenuSliderVar Init(String label, int index, double min, double max, double step, int showval) { Super.Init(label, min, max, step, showval); mIndex = index; + return self; } override double GetSliderValue() @@ -60,19 +61,18 @@ class OptionMenuSliderVar : OptionMenuSliderBase } } - -class ColorpickerMenu : Menu native +class ColorpickerMenu : OptionMenu { - native float mRed; - native float mGreen; - native float mBlue; + float mRed; + float mGreen; + float mBlue; - native int mGridPosX; - native int mGridPosY; + int mGridPosX; + int mGridPosY; - native int mStartItem; + int mStartItem; - native CVar mCVar; + CVar mCVar; double GetColor(int index) { @@ -86,6 +86,40 @@ class ColorpickerMenu : Menu native else if (index == 1) mGreen = val; else mBlue = val; } + + //============================================================================= + // + // + // + //============================================================================= + + void Init(Menu parent, String name, OptionMenuDescriptor desc, CVar cv) + { + Super.Init(parent, desc); + + mStartItem = mDesc.mItems.Size(); + mCVar = cv; + + ResetColor(); + mGridPosX = 0; + mGridPosY = 0; + + // This menu uses some features that are hard to implement in an external control lump + // so it creates its own list of menu items. + mDesc.mItems.Resize(mStartItem+8); + mDesc.mItems[mStartItem+0] = new ("OptionMenuItemStaticText").Init(name, false); + mDesc.mItems[mStartItem+1] = new ("OptionMenuItemStaticText").Init(" ", false); + mDesc.mItems[mStartItem+2] = new ("OptionMenuSliderVar").Init("Red", 0, 0, 255, 15, 0); + mDesc.mItems[mStartItem+3] = new ("OptionMenuSliderVar").Init("Green", 1, 0, 255, 15, 0); + mDesc.mItems[mStartItem+4] = new ("OptionMenuSliderVar").Init("Blue", 2, 0, 255, 15, 0); + mDesc.mItems[mStartItem+5] = new ("OptionMenuItemStaticText").Init(" ", false); + mDesc.mItems[mStartItem+6] = new ("OptionMenuItemCommand").Init("Undo changes", "undocolorpic"); + mDesc.mItems[mStartItem+7] = new ("OptionMenuItemStaticText").Init(" ", false); + mDesc.mSelectedItem = mStartItem + 2; + mDesc.mIndent = 0; + mDesc.CalcIndent(); + } + //============================================================================= // // @@ -297,5 +331,24 @@ class ColorpickerMenu : Menu native screen.DrawText (SmallFont, Font.CR_WHITE, x+(48+24-SmallFont.StringWidth("New")/2)*CleanXfac_1, y, "New", DTA_CleanNoMove_1, true); } - + override void OnDestroy() + { + if (mStartItem >= 0) + { + mDesc.mItems.Resize(mStartItem); + if (mCVar != null) mCVar.SetInt(Color(int(mRed), int(mGreen), int(mBlue))); + mStartItem = -1; + } + } + + override void ResetColor() + { + if (mCVar != null) + { + Color clr = Color(mCVar.GetInt()); + mRed = clr.r; + mGreen = clr.g; + mBlue = clr.b; + } + } } \ No newline at end of file diff --git a/wadsrc/static/zscript/menu/menu.txt b/wadsrc/static/zscript/menu/menu.txt index 2f07ffed0..add8c8cb9 100644 --- a/wadsrc/static/zscript/menu/menu.txt +++ b/wadsrc/static/zscript/menu/menu.txt @@ -99,6 +99,7 @@ class Menu : Object native virtual void SetFocus(MenuItemBase fc) {} virtual bool CheckFocus(MenuItemBase fc) { return false; } virtual void ReleaseFocus() {} + virtual void ResetColor() {} native virtual bool Responder(InputEvent ev); native virtual bool MenuEvent (int mkey, bool fromcontroller); @@ -125,6 +126,8 @@ class MenuDescriptor : Object native native Name mMenuName; native String mNetgameMessage; native Class mClass; + + native static MenuDescriptor GetDescriptor(Name n); } class ListMenuDescriptor : MenuDescriptor native @@ -185,7 +188,7 @@ class OptionMenuDescriptor : MenuDescriptor native native int mPosition; native bool mDontDim; - //native void CalcIndent(); + native void CalcIndent(); //native OptionMenuItem GetItem(Name iname); void Reset() { @@ -199,24 +202,64 @@ class OptionMenuDescriptor : MenuDescriptor native class OptionMenu : Menu native { + native OptionMenuDescriptor mDesc; native bool CanScrollUp; native bool CanScrollDown; native int VisBottom; native OptionMenuItem mFocusControl; - native OptionMenuDescriptor mDesc; + + //============================================================================= + // + // + // + //============================================================================= + + void Init(Menu parent, OptionMenuDescriptor desc) + { + mParentMenu = parent; + mDesc = desc; + if (mDesc != NULL && mDesc.mSelectedItem == -1) mDesc.mSelectedItem = FirstSelectable(); + + } + + //============================================================================= + // + // + // + //============================================================================= + + int FirstSelectable() + { + if (mDesc != NULL) + { + // Go down to the first selectable item + int i = -1; + do + { + i++; + } + while (i < mDesc.mItems.Size() && !mDesc.mItems[i].Selectable()); + if (i>=0 && i < mDesc.mItems.Size()) return i; + } + return -1; + } + override void SetFocus(MenuItemBase fc) { mFocusControl = OptionMenuItem(fc); } + override bool CheckFocus(MenuItemBase fc) { return mFocusControl == fc; } + override void ReleaseFocus() { mFocusControl = NULL; } + } diff --git a/wadsrc/static/zscript/menu/optionmenuitems.txt b/wadsrc/static/zscript/menu/optionmenuitems.txt index 69860af57..338c36599 100644 --- a/wadsrc/static/zscript/menu/optionmenuitems.txt +++ b/wadsrc/static/zscript/menu/optionmenuitems.txt @@ -37,7 +37,7 @@ class OptionMenuItem : MenuItemBase String mLabel; bool mCentered; - void Init(String label, String command, bool center = false) + protected void Init(String label, String command, bool center = false) { Super.Init(0, 0, command); mLabel = label; @@ -92,10 +92,11 @@ class OptionMenuItem : MenuItemBase class OptionMenuItemSubmenu : OptionMenuItem { int mParam; - void Init(String label, Name command, int param = 0) + OptionMenuItemSubmenu Init(String label, Name command, int param = 0) { Super.init(label, command); mParam = param; + return self; } override int Draw(OptionMenuDescriptor desc, int y, int indent, bool selected) @@ -120,9 +121,10 @@ class OptionMenuItemSubmenu : OptionMenuItem class OptionMenuItemCommand : OptionMenuItemSubmenu { - void Init(String label, Name command) + OptionMenuItemCommand Init(String label, Name command) { Super.Init(label, command); + return self; } override bool Activate() @@ -145,10 +147,11 @@ class OptionMenuItemSafeCommand : OptionMenuItemCommand String mPrompt; - void Init(String label, Name command, String prompt = "") + OptionMenuItemSafeCommand Init(String label, Name command, String prompt = "") { Super.Init(label, command); mPrompt = prompt; + return self; } override bool MenuEvent (int mkey, bool fromcontroller) @@ -289,10 +292,11 @@ class OptionMenuItemOption : OptionMenuItemOptionBase { CVar mCVar; - void Init(String label, Name command, Name values, CVar graycheck = null, int center = 0) + OptionMenuItemOption Init(String label, Name command, Name values, CVar graycheck = null, int center = 0) { Super.Init(label, command, values, graycheck, center); mCVar = CVar.FindCVar(mAction); + return self; } //============================================================================= @@ -417,7 +421,7 @@ class OptionMenuItemControlBase : OptionMenuItem int mInput; bool mWaiting; - void Init(String label, Name command, KeyBindings bindings) + protected void Init(String label, Name command, KeyBindings bindings) { Super.init(label, command); mBindings = bindings; @@ -486,17 +490,19 @@ class OptionMenuItemControlBase : OptionMenuItem class OptionMenuItemControl : OptionMenuItemControlBase { - void Init(String label, Name command) + OptionMenuItemControl Init(String label, Name command) { Super.Init(label, command, Bindings); + return self; } } class OptionMenuItemMapControl : OptionMenuItemControlBase { - void Init(String label, Name command) + OptionMenuItemMapControl Init(String label, Name command) { Super.Init(label, command, AutomapBindings); + return self; } } @@ -511,18 +517,20 @@ class OptionMenuItemStaticText : OptionMenuItem int mColor; // this function is only for use from MENUDEF, it needs to do some strange things with the color for backwards compatibility. - void Init(String label, int cr = -1) + OptionMenuItemStaticText Init(String label, int cr = -1) { Super.Init(label, 'None', true); mColor = OptionMenuSettings.mFontColor; if ((cr & 0xffff0000) == 0x12340000) mColor = cr & 0xffff; else if (cr > 0) mColor = OptionMenuSettings.mFontColorHeader; + return self; } - void InitDirect(String label, int cr) + OptionMenuItemStaticText InitDirect(String label, int cr) { Super.Init(label, 'None', true); mColor = cr; + return self; } override int Draw(OptionMenuDescriptor desc, int y, int indent, bool selected) @@ -551,7 +559,7 @@ class OptionMenuItemStaticTextSwitchable : OptionMenuItem int mCurrent; // this function is only for use from MENUDEF, it needs to do some strange things with the color for backwards compatibility. - void Init(String label, String label2, Name command, int cr = -1) + OptionMenuItemStaticTextSwitchable Init(String label, String label2, Name command, int cr = -1) { Super.Init(label, command, true); mAltText = label2; @@ -560,14 +568,16 @@ class OptionMenuItemStaticTextSwitchable : OptionMenuItem mColor = OptionMenuSettings.mFontColor; if ((cr & 0xffff0000) == 0x12340000) mColor = cr & 0xffff; else if (cr > 0) mColor = OptionMenuSettings.mFontColorHeader; + return self; } - void InitDirect(String label, String label2, Name command, int cr) + OptionMenuItemStaticTextSwitchable InitDirect(String label, String label2, Name command, int cr) { Super.Init(label, command, true); mColor = cr; mAltText = label2; mCurrent = 0; + return self; } override int Draw(OptionMenuDescriptor desc, int y, int indent, bool selected) @@ -764,10 +774,11 @@ class OptionMenuItemSlider : OptionMenuSliderBase { CVar mCVar; - void Init(String label, Name command, double min, double max, double step, int showval = 1) + OptionMenuItemSlider Init(String label, Name command, double min, double max, double step, int showval = 1) { Super.Init(label, min, max, step, showval); mCVar =CVar.FindCVar(command); + return self; } override double GetSliderValue() @@ -803,12 +814,13 @@ class OptionMenuItemColorPicker : OptionMenuItem const CPF_RESET = 0x20001; - void Init(String label, Name command) + OptionMenuItemColorPicker Init(String label, Name command) { Super.Init(label, command); CVar cv = CVar.FindCVar(command); if (cv != null && cv.GetRealType() != CVar.CVAR_Color) cv = null; mCVar = cv; + return self; } //============================================================================= @@ -840,20 +852,26 @@ class OptionMenuItemColorPicker : OptionMenuItem if (mCVar != null) { Menu.MenuSound("menu/choose"); - /* - Menu *picker = StartPickerMenu(Menu.CurrentMenu, mLabel, mCVar); - if (picker != null) + let desc = MenuDescriptor.GetDescriptor('Colorpickermenu'); + // New color pickers must inherit from the internal one to work here. + if (desc != NULL && (desc.mClass == null || desc.mClass is "ColorPickerMenu")) { - M_ActivateMenu(picker); - return true; + let odesc = OptionMenuDescriptor(desc); + if (odesc != null) + { + let cls = desc.mClass; + if (cls == null) cls = "ColorpickerMenu"; + let picker = ColorpickerMenu(new(cls)); + picker.Init(Menu.GetCurrentMenu(), mLabel, odesc, mCVar); + picker.ActivateMenu(); + return true; + } } - */ } return false; } } - class OptionMenuItemScreenResolution : OptionMenuItem { String mResTexts[3]; @@ -868,11 +886,12 @@ class OptionMenuItemScreenResolution : OptionMenuItem SRL_HIGHLIGHT = 0x30004, }; - void Init(String command) + OptionMenuItemScreenResolution Init(String command) { Super.Init("", command); mSelection = 0; mHighlight = -1; + return self; } override bool SetValue(int i, int v) @@ -1064,10 +1083,11 @@ class OptionMenuFieldBase : OptionMenuItem class OptionMenuItemTextField : OptionMenuFieldBase { - void Init (String label, Name command, CVar graycheck = null) + OptionMenuItemTextField Init (String label, Name command, CVar graycheck = null) { Super.Init(label, command, graycheck); mEntering = false; + return self; } override String Represent() @@ -1132,12 +1152,13 @@ class OptionMenuItemTextField : OptionMenuFieldBase class OptionMenuItemNumberField : OptionMenuFieldBase { - void Init (String label, Name command, float minimum = 0, float maximum = 100, float step = 1, CVar graycheck = null) + OptionMenuItemNumberField Init (String label, Name command, float minimum = 0, float maximum = 100, float step = 1, CVar graycheck = null) { Super.Init(label, command, graycheck); mMinimum = min(minimum, maximum); mMaximum = max(minimum, maximum); mStep = max(1, step); + return self; } override String Represent()