From 51493cde8cc8aca702df2dd7d797d7646a73f27d Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 17 Feb 2017 20:02:26 +0100 Subject: [PATCH] - scriptified DPlayerMenu::MenuEvent. --- src/menu/playermenu.cpp | 241 ++++++++++------------ wadsrc/static/zscript/menu/playermenu.txt | 147 +++++++++++++ 2 files changed, 254 insertions(+), 134 deletions(-) diff --git a/src/menu/playermenu.cpp b/src/menu/playermenu.cpp index 8049349d97..21498ec2c1 100644 --- a/src/menu/playermenu.cpp +++ b/src/menu/playermenu.cpp @@ -81,7 +81,6 @@ public: void PlayerNameChanged(DMenuItemBase *li); void ColorSetChanged (DMenuItemBase *li); void ClassChanged (DMenuItemBase *li); - void AutoaimChanged (DMenuItemBase *li); void SkinChanged (DMenuItemBase *li); @@ -90,7 +89,6 @@ public: DPlayerMenu() {} void Init(DMenu *parent, DListMenuDescriptor *desc); bool Responder (event_t *ev); - bool MenuEvent (int mkey, bool fromcontroller); }; IMPLEMENT_CLASS(DPlayerMenu, false, false) @@ -403,20 +401,22 @@ void DPlayerMenu::UpdateSkins() //============================================================================= // -// +// access to the player config is done natively, so that broader access +// functions do not need to be exported. // //============================================================================= -void DPlayerMenu::PlayerNameChanged(DMenuItemBase *li) +DEFINE_ACTION_FUNCTION(DPlayerMenu, PlayerNameChanged) { - char pp[MAXPLAYERNAME+1]; - const char *p; - if (li->GetString(0, pp, MAXPLAYERNAME)) - { - FString command("name \""); + PARAM_SELF_PROLOGUE(DPlayerMenu); + PARAM_STRING(s); + const char *pp = s; + FString command("name \""); + if (self == DMenu::CurrentMenu) + { // Escape any backslashes or quotation marks before sending the name to the console. - for (p = pp; *p != '\0'; ++p) + for (auto p = pp; *p != '\0'; ++p) { if (*p == '"' || *p == '\\') { @@ -425,8 +425,9 @@ void DPlayerMenu::PlayerNameChanged(DMenuItemBase *li) command << *p; } command << '"'; - C_DoCommand (command); + C_DoCommand(command); } + return 0; } //============================================================================= @@ -435,28 +436,15 @@ void DPlayerMenu::PlayerNameChanged(DMenuItemBase *li) // //============================================================================= -void DPlayerMenu::ColorSetChanged (DMenuItemBase *li) +DEFINE_ACTION_FUNCTION(DPlayerMenu, ColorSetChanged) { - int sel; - - if (li->GetValue(0, &sel)) + PARAM_SELF_PROLOGUE(DPlayerMenu); + PARAM_INT(sel); + if (self == DMenu::CurrentMenu) { - int mycolorset = -1; - - if (sel > 0) mycolorset = PlayerColorSets[sel-1]; - - DMenuItemBase *red = GetItem(NAME_Red); - DMenuItemBase *green = GetItem(NAME_Green); - DMenuItemBase *blue = GetItem(NAME_Blue); - - // disable the sliders if a valid colorset is selected - if (red != NULL) red->Enable(mycolorset == -1); - if (green != NULL) green->Enable(mycolorset == -1); - if (blue != NULL) blue->Enable(mycolorset == -1); - - players[consoleplayer].userinfo.ColorSetChanged(mycolorset); - UpdateTranslation(); + players[consoleplayer].userinfo.ColorSetChanged(sel); } + return 0; } //============================================================================= @@ -493,6 +481,18 @@ void DPlayerMenu::ClassChanged (DMenuItemBase *li) } } +DEFINE_ACTION_FUNCTION(DPlayerMenu, ClassChanged) +{ + PARAM_SELF_PROLOGUE(DPlayerMenu); + PARAM_OBJECT(sel, DMenuItemBase); + if (self == DMenu::CurrentMenu) + { + self->ClassChanged(sel); + } + return 0; +} + + //============================================================================= // // @@ -514,7 +514,6 @@ void DPlayerMenu::SkinChanged (DMenuItemBase *li) sel = PlayerSkins[sel]; players[consoleplayer].userinfo.SkinNumChanged(sel); UpdateTranslation(); - cvar_set ("skin", skins[sel].name); li = GetItem(NAME_Playerdisplay); if (li != NULL) @@ -524,22 +523,24 @@ void DPlayerMenu::SkinChanged (DMenuItemBase *li) } } -//============================================================================= -// -// -// -//============================================================================= - -void DPlayerMenu::AutoaimChanged (DMenuItemBase *li) +DEFINE_ACTION_FUNCTION(DPlayerMenu, SkinChanged) { - int sel; - - if (li->GetValue(0, &sel)) + PARAM_SELF_PROLOGUE(DPlayerMenu); + PARAM_OBJECT(sel, DMenuItemBase); + if (self == DMenu::CurrentMenu) { - autoaim = (float)sel; + self->SkinChanged(sel); } + return 0; } + +//============================================================================= +// +// +// +//============================================================================= + DEFINE_ACTION_FUNCTION(DPlayerMenu, AutoaimChanged) { PARAM_SELF_PROLOGUE(DPlayerMenu); @@ -551,108 +552,80 @@ DEFINE_ACTION_FUNCTION(DPlayerMenu, AutoaimChanged) } return 0; } + //============================================================================= // // // //============================================================================= -bool DPlayerMenu::MenuEvent (int mkey, bool fromcontroller) +DEFINE_ACTION_FUNCTION(DPlayerMenu, TeamChanged) { - int v; - if (mDesc->mSelectedItem >= 0) + PARAM_SELF_PROLOGUE(DPlayerMenu); + PARAM_INT(val); + // only allow if the menu is active to prevent abuse. + if (self == DMenu::CurrentMenu) { - DMenuItemBase *li = mDesc->mItems[mDesc->mSelectedItem]; - if (li->MenuEvent(mkey, fromcontroller)) - { - FName current = li->GetAction(NULL); - switch(current) - { - // item specific handling comes here - - case NAME_Playerbox: - if (mkey == MKEY_Input) - { - PlayerNameChanged(li); - } - break; - - case NAME_Team: - if (li->GetValue(0, &v)) - { - team = v==0? TEAM_NONE : v-1; - } - break; - - case NAME_Color: - ColorSetChanged(li); - break; - - case NAME_Red: - if (li->GetValue(0, &v)) - { - uint32 color = players[consoleplayer].userinfo.GetColor(); - SendNewColor (v, GPART(color), BPART(color)); - } - break; - - case NAME_Green: - if (li->GetValue(0, &v)) - { - uint32 color = players[consoleplayer].userinfo.GetColor(); - SendNewColor (RPART(color), v, BPART(color)); - } - break; - - case NAME_Blue: - if (li->GetValue(0, &v)) - { - uint32 color = players[consoleplayer].userinfo.GetColor(); - SendNewColor (RPART(color), GPART(color), v); - } - break; - - case NAME_Class: - ClassChanged(li); - break; - - case NAME_Skin: - SkinChanged(li); - break; - - case NAME_Gender: - if (li->GetValue(0, &v)) - { - cvar_set ("gender", v==0? "male" : v==1? "female" : "other"); - } - break; - - case NAME_Autoaim: - AutoaimChanged(li); - break; - - case NAME_Switch: - if (li->GetValue(0, &v)) - { - neverswitchonpickup = !!v; - } - break; - - case NAME_AlwaysRun: - if (li->GetValue(0, &v)) - { - cl_run = !!v; - } - break; - - default: - break; - } - return true; - } + team = val == 0 ? TEAM_NONE : val - 1; } - return Super::MenuEvent(mkey, fromcontroller); + return 0; } +//============================================================================= +// +// +// +//============================================================================= + +DEFINE_ACTION_FUNCTION(DPlayerMenu, GenderChanged) +{ + PARAM_SELF_PROLOGUE(DPlayerMenu); + PARAM_INT(v); + // only allow if the menu is active to prevent abuse. + if (self == DMenu::CurrentMenu) + { + cvar_set("gender", v == 0 ? "male" : v == 1 ? "female" : "other"); + } + return 0; +} + +//============================================================================= +// +// +// +//============================================================================= + +DEFINE_ACTION_FUNCTION(DPlayerMenu, SwitchOnPickupChanged) +{ + PARAM_SELF_PROLOGUE(DPlayerMenu); + PARAM_INT(v); + // only allow if the menu is active to prevent abuse. + if (self == DMenu::CurrentMenu) + { + neverswitchonpickup = !!v; + } + return 0; +} + +//============================================================================= +// +// +// +//============================================================================= + +DEFINE_ACTION_FUNCTION(DPlayerMenu, AlwaysRunChanged) +{ + PARAM_SELF_PROLOGUE(DPlayerMenu); + PARAM_INT(v); + // only allow if the menu is active to prevent abuse. + if (self == DMenu::CurrentMenu) + { + cl_run = !!v; + } + return 0; +} + + DEFINE_FIELD(DPlayerMenu, mRotation) DEFINE_FIELD_NAMED(DPlayerMenu, PlayerClass, mPlayerClass) +DEFINE_FIELD(DPlayerMenu, PlayerColorSets) diff --git a/wadsrc/static/zscript/menu/playermenu.txt b/wadsrc/static/zscript/menu/playermenu.txt index 0226b58495..3b08480804 100644 --- a/wadsrc/static/zscript/menu/playermenu.txt +++ b/wadsrc/static/zscript/menu/playermenu.txt @@ -3,9 +3,20 @@ class PlayerMenu : ListMenu native { native int mRotation; native PlayerClass mPlayerClass; + native Array PlayerColorSets; + // All write function for the player config are native to prevent abuse. protected native void AutoaimChanged(float val); + protected native void TeamChanged(int val); + protected native void AlwaysRunChanged(int val); + protected native void GenderChanged(int val); + protected native void SwitchOnPickupChanged(int val); protected native void ColorChanged(int red, int green, int blue); + protected native void ColorSetChanged(int red); + protected native void PlayerNameChanged(String name); + + protected native void ClassChanged(ListMenuItem it); + protected native void SkinChanged (ListMenuItem li); protected void UpdateTranslation() { @@ -24,6 +35,142 @@ class PlayerMenu : ListMenu native // //============================================================================= + override bool MenuEvent (int mkey, bool fromcontroller) + { + int v; + bool res; + String s; + if (mDesc.mSelectedItem >= 0) + { + let li = mDesc.mItems[mDesc.mSelectedItem]; + if (li.MenuEvent(mkey, fromcontroller)) + { + Name ctrl = li.GetAction(); + switch(ctrl) + { + // item specific handling comes here + + case 'Playerbox': + if (mkey == MKEY_Input) + { + [res, s] = li.GetString(0); + if (res) PlayerNameChanged(s); + } + break; + + case 'Team': + [res, v] = li.GetValue(0); + if (res) TeamChanged(v); + break; + + case 'Color': + [res, v] = li.GetValue(0); + if (res) + { + int mycolorset = -1; + + if (v > 0) mycolorset = PlayerColorSets[v - 1]; + + let red = GetItem('Red'); + let green = GetItem('Green'); + let blue = GetItem('Blue'); + + // disable the sliders if a valid colorset is selected + if (red != NULL) red.Enable(mycolorset == -1); + if (green != NULL) green.Enable(mycolorset == -1); + if (blue != NULL) blue.Enable(mycolorset == -1); + + ColorSetChanged(v - 1); + UpdateTranslation(); + } + break; + + case 'Red': + [res, v] = li.GetValue(0); + if (res) + { + Color colr = players[consoleplayer].GetColor(); + SendNewColor (v, colr.g, colr.b); + } + break; + + case 'Green': + [res, v] = li.GetValue(0); + if (res) + { + Color colr = players[consoleplayer].GetColor(); + SendNewColor (colr.r, v, colr.b); + } + break; + + case 'Blue': + [res, v] = li.GetValue(0); + if (res) + { + Color colr = players[consoleplayer].GetColor(); + SendNewColor (colr.r, colr.g, v); + } + break; + + case 'Class': + [res, v] = li.GetValue(0); + if (res) + { + ClassChanged(li); + } + break; + + case 'Skin': + SkinChanged(li); + break; + + case 'Gender': + [res, v] = li.GetValue(0); + if (res) + { + GenderChanged(v); + } + break; + + case 'Autoaim': + [res, v] = li.GetValue(0); + if (res) + { + AutoaimChanged(v); + } + break; + + case 'Switch': + [res, v] = li.GetValue(0); + if (res) + { + SwitchOnPickupChanged(v); + } + break; + + case 'AlwaysRun': + [res, v] = li.GetValue(0); + if (res) + { + AlwaysRunChanged(v); + } + break; + + default: + break; + } + return true; + } + } + return Super.MenuEvent(mkey, fromcontroller); + } + + //============================================================================= + // + // + // + //============================================================================= + override bool MouseEvent(int type, int x, int y) { let li = mFocusControl;