Merge pull request #2118 from Areloch/MenuAndPopups

Refactors the Popup menus and GuiMenuBars
This commit is contained in:
Areloch 2018-01-27 18:41:00 -06:00 committed by GitHub
commit c23c99dbea
30 changed files with 995 additions and 3116 deletions

View file

@ -1,127 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "platform/menus/menuBar.h"
#include "platform/menus/popupMenu.h"
#include "gui/core/guiCanvas.h"
#include "console/engineAPI.h"
//-----------------------------------------------------------------------------
// Constructor/Destructor
//-----------------------------------------------------------------------------
MenuBar::MenuBar()
{
createPlatformPopupMenuData();
mCanvas = NULL;
}
MenuBar::~MenuBar()
{
removeFromCanvas();
deletePlatformPopupMenuData();
}
IMPLEMENT_CONOBJECT(MenuBar);
ConsoleDocClass( MenuBar,
"@brief Used for rendering platform menu bars\n\n"
"Internal use only\n\n"
"@internal"
);
//-----------------------------------------------------------------------------
// Public Methods
//-----------------------------------------------------------------------------
void MenuBar::addObject(SimObject *obj)
{
Parent::addObject(obj);
updateMenuBar(dynamic_cast<PopupMenu *>(obj));
}
void MenuBar::removeObject(SimObject *obj)
{
Parent::removeObject(obj);
updateMenuBar(dynamic_cast<PopupMenu *>(obj));
}
void MenuBar::insertObject(SimObject *obj, S32 pos)
{
Parent::addObject(obj);
if(pos >= size())
pos = size() - 1;
if(pos < size())
{
if(pos < 0) pos = 0;
Parent::reOrder(obj, at(pos));
}
updateMenuBar(dynamic_cast<PopupMenu *>(obj));
}
void MenuBar::pushObject(SimObject *obj)
{
Parent::pushObject(obj);
updateMenuBar(dynamic_cast<PopupMenu *>(obj));
}
void MenuBar::popObject()
{
Parent::popObject();
updateMenuBar();
}
bool MenuBar::reOrder(SimObject *obj, SimObject *target /*= 0*/)
{
bool ret = Parent::reOrder(obj, target);
if(ret)
updateMenuBar(dynamic_cast<PopupMenu *>(obj));
return ret;
}
//-----------------------------------------------------------------------------
// Console Methods
//-----------------------------------------------------------------------------
DefineConsoleMethod(MenuBar, attachToCanvas, void, (const char *canvas, S32 pos), , "(GuiCanvas, pos)")
{
object->attachToCanvas(dynamic_cast<GuiCanvas*>(Sim::findObject(canvas)), pos);
}
DefineConsoleMethod(MenuBar, removeFromCanvas, void, (), , "()")
{
object->removeFromCanvas();
}
//-----------------------------------------------------------------------------
DefineConsoleMethod(MenuBar, insert, void, (SimObject* pObject, S32 pos), ,"(object, pos) insert object at position")
{
if(pObject)
object->insertObject(pObject, pos);
}

View file

@ -1,71 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "console/simBase.h"
#ifndef _MENUBAR_H_
#define _MENUBAR_H_
// Forward Refs
class PlatformMenuBarData;
class PopupMenu;
class GuiCanvas;
class MenuBar : public SimSet
{
typedef SimSet Parent;
protected:
PlatformMenuBarData *mData;
GuiCanvas *mCanvas;
/// Update the native menu bar to ensure consistency with the set
void updateMenuBar(PopupMenu *menu = NULL);
void createPlatformPopupMenuData();
void deletePlatformPopupMenuData();
public:
MenuBar();
virtual ~MenuBar();
DECLARE_CONOBJECT(MenuBar);
/// Attach this menu bar to the native menu bar
void attachToCanvas(GuiCanvas *owner, S32 pos);
/// Remove this menu bar from the native menu bar
void removeFromCanvas();
/// Returns true if this menu is attached to the menu bar
bool isAttachedToCanvas() { return mCanvas != NULL; }
virtual void insertObject(SimObject *obj, S32 pos);
// Overridden SimSet methods to ensure menu bar consistency when attached
virtual void addObject(SimObject *obj);
virtual void removeObject(SimObject *obj);
virtual void pushObject(SimObject *obj);
virtual void popObject();
virtual bool reOrder(SimObject *obj, SimObject *target = 0);
};
#endif // _MENUBAR_H_

View file

@ -1,269 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/menus/popupMenu.h"
#include "console/consoleTypes.h"
#include "console/engineAPI.h"
#include "gui/core/guiCanvas.h"
#include "core/util/safeDelete.h"
static U32 sMaxPopupGUID = 0;
PopupMenuEvent PopupMenu::smPopupMenuEvent;
bool PopupMenu::smSelectionEventHandled = false;
/// Event class used to remove popup menus from the event notification in a safe way
class PopUpNotifyRemoveEvent : public SimEvent
{
public:
void process(SimObject *object)
{
PopupMenu::smPopupMenuEvent.remove((PopupMenu *)object, &PopupMenu::handleSelectEvent);
}
};
//-----------------------------------------------------------------------------
// Constructor/Destructor
//-----------------------------------------------------------------------------
PopupMenu::PopupMenu() : mCanvas(NULL)
{
createPlatformPopupMenuData();
mSubmenus = new SimSet;
mSubmenus->registerObject();
mBarTitle = StringTable->EmptyString();
mIsPopup = false;
mPopupGUID = sMaxPopupGUID++;
}
PopupMenu::~PopupMenu()
{
// This searches the menu bar so is safe to call for menus
// that aren't on it, since nothing will happen.
removeFromMenuBar();
SimSet::iterator i;
while((i = mSubmenus->begin()) != mSubmenus->end())
{
(*i)->deleteObject();
}
mSubmenus->deleteObject();
deletePlatformPopupMenuData();
PopupMenu::smPopupMenuEvent.remove(this, &PopupMenu::handleSelectEvent);
}
IMPLEMENT_CONOBJECT(PopupMenu);
ConsoleDocClass( PopupMenu,
"@brief PopupMenu represents a system menu.\n\n"
"You can add menu items to the menu, but there is no torque object associated "
"with these menu items, they exist only in a platform specific manner.\n\n"
"@note Internal use only\n\n"
"@internal"
);
//-----------------------------------------------------------------------------
void PopupMenu::initPersistFields()
{
addField("isPopup", TypeBool, Offset(mIsPopup, PopupMenu), "true if this is a pop-up/context menu. defaults to false.");
addField("barTitle", TypeCaseString, Offset(mBarTitle, PopupMenu), "the title of this menu when attached to a menu bar");
Parent::initPersistFields();
}
//-----------------------------------------------------------------------------
bool PopupMenu::onAdd()
{
if(! Parent::onAdd())
return false;
createPlatformMenu();
Con::executef(this, "onAdd");
return true;
}
void PopupMenu::onRemove()
{
Con::executef(this, "onRemove");
Parent::onRemove();
}
//-----------------------------------------------------------------------------
void PopupMenu::onMenuSelect()
{
Con::executef(this, "onMenuSelect");
}
//-----------------------------------------------------------------------------
void PopupMenu::handleSelectEvent(U32 popID, U32 command)
{
if (popID == mPopupGUID && canHandleID(command))
if (handleSelect(command))
smSelectionEventHandled = true;
}
//-----------------------------------------------------------------------------
void PopupMenu::onAttachToMenuBar(GuiCanvas *canvas, S32 pos, const char *title)
{
mCanvas = canvas;
// Attached menus must be notified of menu events
smPopupMenuEvent.notify(this, &PopupMenu::handleSelectEvent);
// Pass on to sub menus
for(SimSet::iterator i = mSubmenus->begin();i != mSubmenus->end();++i)
{
PopupMenu *mnu = dynamic_cast<PopupMenu *>(*i);
if(mnu == NULL)
continue;
mnu->onAttachToMenuBar(canvas, pos, title);
}
// Call script
if(isProperlyAdded())
Con::executef(this, "onAttachToMenuBar", Con::getIntArg(canvas ? canvas->getId() : 0), Con::getIntArg(pos), title);
}
void PopupMenu::onRemoveFromMenuBar(GuiCanvas *canvas)
{
mCanvas = NULL;
// We are no longer interested in select events, remove ourselves from the notification list in a safe way
Sim::postCurrentEvent(this, new PopUpNotifyRemoveEvent());
// Pass on to sub menus
for(SimSet::iterator i = mSubmenus->begin();i != mSubmenus->end();++i)
{
PopupMenu *mnu = dynamic_cast<PopupMenu *>(*i);
if(mnu == NULL)
continue;
mnu->onRemoveFromMenuBar(canvas);
}
// Call script
if(isProperlyAdded())
Con::executef(this, "onRemoveFromMenuBar", Con::getIntArg(canvas ? canvas->getId() : 0));
}
//-----------------------------------------------------------------------------
bool PopupMenu::onMessageReceived(StringTableEntry queue, const char* event, const char* data)
{
return Con::executef(this, "onMessageReceived", queue, event, data);
}
bool PopupMenu::onMessageObjectReceived(StringTableEntry queue, Message *msg )
{
return Con::executef(this, "onMessageReceived", queue, Con::getIntArg(msg->getId()));
}
//-----------------------------------------------------------------------------
// Console Methods
//-----------------------------------------------------------------------------
DefineConsoleMethod(PopupMenu, insertItem, S32, (S32 pos, const char * title, const char * accelerator, const char* cmd), ("", "", ""), "(pos[, title][, accelerator][, cmd])")
{
return object->insertItem(pos, title, accelerator, cmd);
}
DefineConsoleMethod(PopupMenu, removeItem, void, (S32 pos), , "(pos)")
{
object->removeItem(pos);
}
DefineConsoleMethod(PopupMenu, insertSubMenu, S32, (S32 pos, String title, String subMenu), , "(pos, title, subMenu)")
{
PopupMenu *mnu = dynamic_cast<PopupMenu *>(Sim::findObject(subMenu));
if(mnu == NULL)
{
Con::errorf("PopupMenu::insertSubMenu - Invalid PopupMenu object specified for submenu");
return -1;
}
return object->insertSubMenu(pos, title, mnu);
}
DefineConsoleMethod(PopupMenu, setItem, bool, (S32 pos, const char * title, const char * accelerator, const char *cmd), (""), "(pos, title[, accelerator][, cmd])")
{
return object->setItem(pos, title, accelerator, cmd);
}
//-----------------------------------------------------------------------------
DefineConsoleMethod(PopupMenu, enableItem, void, (S32 pos, bool enabled), , "(pos, enabled)")
{
object->enableItem(pos, enabled);
}
DefineConsoleMethod(PopupMenu, checkItem, void, (S32 pos, bool checked), , "(pos, checked)")
{
object->checkItem(pos, checked);
}
DefineConsoleMethod(PopupMenu, checkRadioItem, void, (S32 firstPos, S32 lastPos, S32 checkPos), , "(firstPos, lastPos, checkPos)")
{
object->checkRadioItem(firstPos, lastPos, checkPos);
}
DefineConsoleMethod(PopupMenu, isItemChecked, bool, (S32 pos), , "(pos)")
{
return object->isItemChecked(pos);
}
DefineConsoleMethod(PopupMenu, getItemCount, S32, (), , "()")
{
return object->getItemCount();
}
//-----------------------------------------------------------------------------
DefineConsoleMethod(PopupMenu, attachToMenuBar, void, (const char * canvasName, S32 pos, const char * title), , "(GuiCanvas, pos, title)")
{
object->attachToMenuBar(dynamic_cast<GuiCanvas*>(Sim::findObject(canvasName)), pos, title);
}
DefineConsoleMethod(PopupMenu, removeFromMenuBar, void, (), , "()")
{
object->removeFromMenuBar();
}
//-----------------------------------------------------------------------------
DefineConsoleMethod(PopupMenu, showPopup, void, (const char * canvasName, S32 x, S32 y), ( -1, -1), "(Canvas,[x, y])")
{
GuiCanvas *pCanvas = dynamic_cast<GuiCanvas*>(Sim::findObject(canvasName));
object->showPopup(pCanvas, x, y);
}

View file

@ -1,189 +0,0 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "console/simBase.h"
#include "core/util/tVector.h"
#include "util/messaging/dispatcher.h"
#include "gui/core/guiCanvas.h"
#ifndef _POPUPMENU_H_
#define _POPUPMENU_H_
// Forward ref used by the platform code
struct PlatformPopupMenuData;
class MenuBar;
// PopupMenu represents a menu.
// You can add menu items to the menu, but there is no torque object associated
// with these menu items, they exist only in a platform specific manner.
class PopupMenu : public SimObject, public virtual Dispatcher::IMessageListener
{
typedef SimObject Parent;
friend class MenuBar;
private:
/// Used by MenuBar to attach the menu to the menu bar. Do not use anywhere else.
void attachToMenuBar(GuiCanvas *owner, S32 pos);
protected:
PlatformPopupMenuData *mData;
SimSet *mSubmenus;
SimObjectPtr<GuiCanvas> mCanvas;
StringTableEntry mBarTitle;
U32 mPopupGUID;
bool mIsPopup;
public:
PopupMenu();
virtual ~PopupMenu();
void createPlatformPopupMenuData();
void deletePlatformPopupMenuData();
DECLARE_CONOBJECT(PopupMenu);
static void initPersistFields();
virtual bool onAdd();
virtual void onRemove();
static PopupMenuEvent smPopupMenuEvent;
static bool smSelectionEventHandled; /// Set to true if any menu or submenu handles a selection event
/// Creates the platform specific menu object, a peer to this object.
/// The platform menu *must* exist before calling any method that manipulates
/// menu items or displays the menu.
/// implementd on a per-platform basis.
void createPlatformMenu();
void setBarTitle(const char * val) { mBarTitle = StringTable->insert(val, true); }
StringTableEntry getBarTitle() const { return mBarTitle; }
/// pass NULL for @p title to insert a separator
/// returns the menu item's ID, or -1 on failure.
/// implementd on a per-platform basis.
/// TODO: factor out common code
S32 insertItem(S32 pos, const char *title, const char* accelerator, const char* cmd);
/// Sets the name title and accelerator for
/// an existing item.
bool setItem(S32 pos, const char *title, const char* accelerator, const char* cmd);
/// pass NULL for @p title to insert a separator
/// returns the menu item's ID, or -1 on failure.
/// adds the submenu to the mSubmenus vector.
/// implemented on a per-platform basis.
/// TODO: factor out common code
S32 insertSubMenu(S32 pos, const char *title, PopupMenu *submenu);
/// remove the menu item at @p itemPos
/// if the item has a submenu, it is removed from the mSubmenus list.
/// implemented on a per-platform basis.
/// TODO: factor out common code
void removeItem(S32 itemPos);
/// implemented on a per-platform basis.
void enableItem(S32 pos, bool enable);
/// implemented on a per-platform basis.
void checkItem(S32 pos, bool checked);
/// All items at positions firstPos through lastPos are unchecked, and the
/// item at checkPos is checked.
/// implemented on a per-platform basis.
void checkRadioItem(S32 firstPos, S32 lastPos, S32 checkPos);
bool isItemChecked(S32 pos);
/// Returns the number of items in the menu.
U32 getItemCount();
/// Returns the popup GUID
U32 getPopupGUID() { return mPopupGUID; }
//-----------------------------------------------------------------------------
// New code should not use these methods directly, use the menu bar instead.
//
// They remain for compatibility with old code and will be changing/going away
// once the existing code is moved over to the menu bar.
//-----------------------------------------------------------------------------
/// Places this menu in the menu bar of the application's main window.
/// @param owner The GuiCanvas that owns the PlatformWindow that this call is associated with
/// @param pos The relative position at which to place the menu.
/// @param title The name of the menu
void attachToMenuBar(GuiCanvas *owner, S32 pos, const char *title);
/// Removes this menu from the menu bar.
void removeFromMenuBar();
//-----------------------------------------------------------------------------
/// Called when the menu has been attached to the menu bar
void onAttachToMenuBar(GuiCanvas *canvas, S32 pos, const char *title);
/// Called when the menu has been removed from the menu bar
void onRemoveFromMenuBar(GuiCanvas *canvas);
/// Returns the position index of this menu on the bar.
S32 getPosOnMenuBar();
/// Returns true if this menu is attached to the menu bar
bool isAttachedToMenuBar() { return mCanvas != NULL; }
/// Displays this menu as a popup menu and blocks until the user has selected
/// an item.
/// @param canvas the owner to show this popup associated with
/// @param x window local x coordinate at which to display the popup menu
/// @param y window local y coordinate at which to display the popup menu
/// implemented on a per-platform basis.
void showPopup(GuiCanvas *owner, S32 x = -1, S32 y = -1);
/// Returns true iff this menu contains an item that matches @p iD.
/// implemented on a per-platform basis.
/// TODO: factor out common code
bool canHandleID(U32 iD);
/// A menu item in this menu has been selected by id.
/// Submenus are given a chance to respond to the command first.
/// If no submenu can handle the command id, this menu handles it.
/// The script callback this::onSelectItem( position, text) is called.
/// If @p text is null, then the text arg passed to script is the text of
/// the selected menu item.
/// implemented on a per-platform basis.
/// TODO: factor out common code
bool handleSelect(U32 command, const char *text = NULL);
void onMenuSelect();
/// Helper function to allow menu selections from signal events.
/// Wraps canHandleID() and handleSelect() in one function
/// without changing their internal functionality, so
/// it should work regardless of platform.
void handleSelectEvent(U32 popID, U32 command);
virtual bool onMessageReceived(StringTableEntry queue, const char* event, const char* data );
virtual bool onMessageObjectReceived(StringTableEntry queue, Message *msg );
};
#endif // _POPUPMENU_H_