Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu Manager #36

Merged
merged 14 commits into from
Aug 14, 2018
Merged

Menu Manager #36

merged 14 commits into from
Aug 14, 2018

Conversation

Mistrick
Copy link
Collaborator

Description

Implementation of menu manager

Motivation and Context

Fixes #28

How has this been tested?

#include <spmod>

public PluginInfo pluginInfo =
{
	name = "test",
    version = "0.0.0",
	author = "author",
	url = "https://github.com/Amaroq7/SPMod"
};

Menu m, m2, m3, m4, m5;
bool hide = false;

public void OnPluginInit()
{
	Command("^say /m", MenuTest);
	Command("^say /m2", MenuTest2);
	Command("^say /m3", MenuTest5);
	Command("^say /m4", MenuTest6);
	Command("^say /m5", MenuTest7);
	Command("^say /cm", MenuTest3);
	Command("^say /h", MenuTest4);

	m = Menu(TestHandler, MenuItemStyle, true);
	m.SetTitle("Title");
	m.AddItem("one");
	m.AddItem("two");
	m.AddItem("three");
	m.AddItem("four", 0, ItemHandler);
	m.AddItem("5");
	m.AddItem("6");
	m.AddItem("7");
	m.AddItem("8");
	m.AddItem("9", 0, ItemHandler);
	m.AddItem("10");
	m.AddItem("11");
	
	m2 = Menu(TestHandler, MenuItemStyle, true);
	
	m2.SetTitle("Test 2");
	m2.AddItem("one", 1);
	m2.AddItem("two", 2);
	m2.AddItem("three", 3);
	m2.AddItem("four", 4);
	m2.AddItem("5", 5, ItemHandler3);
	m2.AddItem("6", 6, ItemHandler3);
	m2.AddItem("7", 7, ItemHandler3);
	m2.AddItem("8", 9, ItemHandler3);
	m2.AddItem("9", 10);
	m2.AddItem("10", 10);
	m2.AddItem("11", 10);
	m2.AddItem("12", 10);

	m2.SetProp(MProp_NumberFormat, "\\r[#num]");
	m2.ItemsPerPage = 4;

	m3 = Menu(TestHandler, MenuItemStyle, true);
	
	m3.SetTitle("Test 3");
	m3.AddItem("one");
	m3.AddItem("two");
	m3.AddItem("three");
	m3.AddItem("four");
	m3.AddItem("5", 0, ItemHandler3);
	m3.AddItem("6", 3, ItemHandler3);
	m3.AddItem("7");

	m3.AddStaticItem(0, "static1", 23, ItemHandler2);
	m3.AddStaticItem(5, "static2", 23, ItemHandler2);

	m3.ItemsPerPage = 5;

	m4 = Menu(TestHandler2, MenuTextStyle);

	char text[] = "Title\n\n1. One\n2. Two\n\n3. Exit";
	m4.SetText(text);
	m4.SetKeys((1<<0)|(1<<1)|(1<<2));


	m5 = Menu(TestHandler, MenuItemStyle, true);

	m5.SetTitle("Test 5");
	m5.AddItem("one");
	m5.AddItem("two");
	m5.AddItem("three");
	m5.AddItem("four", 0, ItemHandler);
	m5.AddItem("5");
	m5.AddItem("6");
	m5.InsertItem(0, "7");
	m5.InsertItem(0, "8");
	m5.InsertItem(0, "9", 0, ItemHandler);
	m5.InsertItem(0, "10");
	m5.InsertItem(0, "11");

	m5.RemoveItem(1);
} 

public ItemStatus ItemHandler(Menu menu, MenuItem item, int player)
{
	PrintToServer("Item callback: menu %d, item %d, player %d", menu, item, player);
	item.SetName("changed");
	return ItemDisabled;
}

public ItemStatus ItemHandler2(Menu menu, MenuItem item, int player)
{
	char name[64];
	item.GetName(name, sizeof name);
	PrintToServer("Item callback2: menu %d, item %s[%d], player %d", menu, name, item, player);
	
	return ItemEnabled;
}
public ItemStatus ItemHandler3(Menu menu, MenuItem item, int player)
{
	PrintToServer("Item callback3: menu %d, item %d, player %d", menu, item, player);
	return hide ? ItemHide : ItemEnabled;
}

public PluginReturn MenuTest(int client, Command cid)
{
	m.Display(client, 0, 10);
}
public PluginReturn MenuTest2(int client, Command cid)
{
	m2.Display(client);
}
public PluginReturn MenuTest5(int client, Command cid)
{
	m3.Display(client);
}
public PluginReturn MenuTest6(int client, Command cid)
{
	m4.Display(client);
}
public PluginReturn MenuTest7(int client, Command cid)
{
	m5.Display(client);
}

public PluginReturn MenuTest3(int client, Command cid)
{
	CloseMenu(client);
}

public PluginReturn MenuTest4(int client, Command cid)
{
	hide = !hide;
}

public int TestHandler(Menu menu, MenuItem item, int player)
{
	PrintToServer("menu %d, item %d, item data %d, player %d", menu, item, item.GetData(), player);
	return 0;
}
public int TestHandler2(Menu menu, int key, int player)
{
	PrintToServer("menu %d, key %d, player %d", menu, key, player);
	return 0;
}

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Copy link
Owner

@Amaroq7 Amaroq7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Player class is on its way (#38) I want to merge it later today if there are no objections. Then you can adjust it to suit your needs, I mean removing m_playerMenu and m_playerPage.

MENU_BACK = -1
};

enum ItemStatus
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum class ItemStatus then its options can be named just Enabled, Disabled, Hide.

ItemHide
};

enum MenuStyle
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum class MenuStyle then options Item and Text.

std::size_t slot = 0;


auto addItem = [&](ItemStatus r, std::size_t s, std::string n)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string_view

{
std::shared_ptr<Menu> menu = std::make_shared<Menu>(m_mid++, std::forward<Args>(args)...);
m_menus.push_back(menu);
return menu;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be changed to emplace_back

while (*n);
}

std::string replace(const std::string& str, const std::string& from, const std::string& to)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string_view for params

return "";

if(item >= m_items.size())
return m_staticSlots[item - m_items.size()].get()->name;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

return false;

if(item >= m_items.size())
m_staticSlots[item - m_items.size()].get()->name = name.data();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use .get()

public:
struct MenuItem
{
MenuItem(std::string n,

This comment was marked as resolved.

int m_time;
std::size_t m_itemsPerPage;
int m_keys;
int m_slots[10];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::array

std::size_t m_itemsPerPage;
int m_keys;
int m_slots[10];
std::unique_ptr<MenuItem> m_staticSlots[10];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::array

plr = plrMngr->getPlayerCore(i);

if(!plr->isInGame()
|| !plrMngr->getPlayerCore(i)->getMenu()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use plr as well


if(!plr->isInGame()
|| !plrMngr->getPlayerCore(i)->getMenu()
|| plrMngr->getPlayerCore(i)->getMenu()->getId() == index)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant !=

int gmsgShowMenu = 0;
int gmsgVGUIMenu = 0;

Menu::MenuItem::MenuItem(std::string n,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string_view

);
};

typedef CallbackHandler = function ItemStatus (Menu menu, MenuItem item, int player);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use Player

public:
using MenuHandler = void (*)(IMenu *const menu, std::size_t item, int player);
virtual IMenu *registerMenu(MenuHandler handler, MenuStyle style, bool global) = 0;
virtual IMenu *registerMenu(SourcePawn::IPluginFunction *func, MenuStyle style, bool global) = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any use case registering menu in module and have callback in plugin?

* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

This comment was marked as resolved.

{
public:
using MenuItemCallback = ItemStatus (*)(IMenu *const menu, std::size_t item, int player);
using MenuHandler = void (*)(IMenu *const menu, std::size_t item, int player);

This comment was marked as resolved.

/*
* @brief Sets a item data.
*
* @param data Item datar.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo


virtual int getTime() const = 0;
virtual int getKeys() const = 0;
virtual int keyToSlot(int key) const = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it need to be exported?


virtual void setHandler(MenuHandler func) = 0;

virtual void execHandler(int player,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these execHandler and setHandler need to be in api


void setNumberFormat(std::string_view format);

void setHandler(std::variant<SourcePawn::IPluginFunction *, MenuHandler> &&func);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be removed

@@ -61,6 +66,9 @@ class Player : public IPlayer
std::string m_name;
std::string m_ip;
std::string m_steamID;

std::shared_ptr<Menu> m_menu;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::weak_ptr

std::variant<SourcePawn::IPluginFunction *, MenuItemCallback> &&callback,
std::variant<cell_t, void *> &&data);

bool setStaticItem(std::size_t positon,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be exported to modules api?

std::variant<SourcePawn::IPluginFunction *, MenuItemCallback> &&callback,
std::variant<cell_t, void *> &&data);

bool setItemName(std::size_t item,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. This could be useful to change name of item menu in item callback

Copy link
Owner

@Amaroq7 Amaroq7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the lack of input recently, been busy lately.

std::shared_ptr<Menu> findMenuCore(std::size_t index) const;

void destroyMenu(std::size_t index);
void clearMenus();

This comment was marked as resolved.

This comment was marked as resolved.

public:
using MenuHandler = void (*)(IMenu *const menu, std::size_t item, int player);
virtual IMenu *registerMenu(MenuHandler handler, MenuStyle style, bool global) = 0;
virtual IMenu *findMenu(std::size_t mid) const = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think modules need this exported. Also destroyMenu(IMenu *menu) is missing here.

std::variant<SourcePawn::IPluginFunction *, MenuItemCallback> &&callback,
std::variant<cell_t, void *> &&data)
{
if(position >= m_itemsPerPage)

This comment was marked as resolved.

{
public:
using MenuItemCallback = ItemStatus (*)(IMenu *const menu, std::size_t item, int player);
using MenuHandler = void (*)(IMenu *const menu, std::size_t item, int player);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMenuItem class could be created and all related item's funcs move there from IMenu
int Player could be IPlayer player

Copy link
Owner

@Amaroq7 Amaroq7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's the last review and it should be good to go.

MenuItemCallback callback,
void *data) override;

bool setStaticItem(std::size_t position,

This comment was marked as resolved.

@Amaroq7 Amaroq7 merged commit 4be32ea into Amaroq7:master Aug 14, 2018
@Mistrick Mistrick deleted the menus branch August 14, 2018 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Menu manager
2 participants