GRAPHICS: Implement macpopupmenu while reusing macmenu

Added functions for menu drawing/selections, this uses the base functions of macmenu to implement mac-styled popupmenu's, also return selected item index/text.

Used by 'Popup Menu' in theapartment: With this changes the popupmenu is working with both item selectors, doesn't implement the icon selector.
This commit is contained in:
Harishankar Kumar 2023-05-18 17:20:48 +05:30 committed by Eugene Sandulenko
parent 014712bbc5
commit e12d3053cc
5 changed files with 157 additions and 2 deletions

View File

@ -114,6 +114,8 @@ MacMenu::MacMenu(int id, const Common::Rect &bounds, MacWindowManager *wm)
_activeItem = -1;
_activeSubItem = -1;
_lastActiveItem = -1;
_lastActiveSubItem = -1;
_ccallback = NULL;
_unicodeccallback = NULL;
@ -685,7 +687,7 @@ void MacMenu::createSubMenuFromString(int id, const char *str, int commandId) {
submenu = addSubMenu(nullptr, id);
for (uint i = 0; i < string.size(); i++) {
while (i < string.size() && string[i] != ';') // Read token
while (i < string.size() && (string[i] != ';' && string[i] != '\r')) // Read token, consume \r for popup menu (MacPopUp)
item += string[i++];
if (item.lastChar() == ']') { // we have command id
@ -1455,6 +1457,10 @@ bool MacMenu::mouseRelease(int x, int y) {
}
}
// Set last active items and subitems before leaving!
_lastActiveItem = _activeItem;
_lastActiveSubItem = _activeSubItem;
// if the mode is not win95, or the click position is outside of the menu, then we close it
if (!(_wm->_mode & kWMModeWin95) || !contains(x, y) || haveCallBack)
closeMenu();

View File

@ -165,6 +165,8 @@ public:
void setAction(MacMenuItem *menuItem, int actionId);
int getAction(MacMenuItem *menuItem);
int getLastSelectedMenuItem() { return _lastActiveItem; };
int getLastSelectedSubmenuItem() { return _lastActiveSubItem; };
protected:
Common::Rect _bbox;
@ -175,6 +177,7 @@ protected:
Common::Array<MacMenuSubMenu *> _menustack;
void renderSubmenu(MacMenuSubMenu *menu, bool recursive = true);
void calcSubMenuBounds(MacMenuSubMenu *menu, int x, int y);
private:
ManagedSurface _tempSurface;
@ -191,7 +194,6 @@ private:
void processSubmenuTabs(MacMenuSubMenu *submenu);
int calcSubMenuWidth(MacMenuSubMenu *menu);
void calcSubMenuBounds(MacMenuSubMenu *menu, int x, int y);
bool keyEvent(Common::Event &event);
bool mouseRelease(int x, int y);
@ -212,6 +214,9 @@ private:
int _activeItem;
int _activeSubItem;
int _lastActiveItem;
int _lastActiveSubItem;
void (*_ccallback)(int action, Common::String &text, void *data);
void (*_unicodeccallback)(int action, Common::U32String &text, void *data);
void *_cdata;

View File

@ -0,0 +1,95 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "graphics/macgui/macpopupmenu.h"
#include "graphics/macgui/macwindowmanager.h"
namespace Graphics {
MacPopUp::MacPopUp(int id, const Common::Rect &bounds, MacWindowManager *wm, const char *string) : MacMenu(id, bounds, wm) {
_menuItemId = addMenuItem(nullptr, "");
createSubMenuFromString(0, string, 0);
wm->addMenu(id, this);
_menuId = id;
}
bool MacPopUp::draw(ManagedSurface *g, bool forceRedraw) {
if (!_isVisible)
return false;
if (_dimensionsDirty)
calcSubMenuBounds(_items[0]->submenu, _mouseX, _mouseY);
if (!_contentIsDirty && !forceRedraw)
return false;
_contentIsDirty = false;
_screen.clear(_wm->_colorGreen);
renderSubmenu(_items[0]->submenu, false);
if (g)
g->transBlitFrom(_screen, _wm->_colorGreen);
if (!(_wm->_mode & kWMModalMenuMode) && g)
g_system->copyRectToScreen(g->getPixels(), g->pitch, 0, 0, g->w, g->h);
return true;
}
uint32 MacPopUp::drawAndSelectMenu(int x, int y, int item) {
_mouseX = x;
_mouseY = y;
// If menu is not active, then activate it!
if (!_active)
_wm->activateMenu();
setActive(true);
_contentIsDirty = true; // Set true to force refresh menu open changes
// Push our submenu to stack
_menustack.clear();
_menustack.push_back(_items[0]->submenu);
// Display menu and update according to events
this->draw(_wm->_screen);
eventLoop();
// Close menu
closeMenu();
int activeSubItem = getLastSelectedSubmenuItem();
if (activeSubItem == -1)
return item;
// Return one indexed item!
return activeSubItem + 1;
}
Common::String MacPopUp::getItemText(int item) {
// Convert 1-indexed item to 0 indexed
item = item - 1;
MacMenuItem *menu = getMenuItem(_menuItemId);
MacMenuItem *submenu = getSubMenuItem(menu, item);
return getName(submenu);
}
} // end of namespace Graphics

View File

@ -0,0 +1,48 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef GRAPHICS_MACGUI_MACPOPUPMENU_H
#define GRAPHICS_MACGUI_MACPOPUPMENU_H
#include "graphics/macgui/macmenu.h"
namespace Graphics {
class MacMenu;
class MacWindowManager;
class MacPopUp : public MacMenu {
public:
MacPopUp(int id, const Common::Rect &bounds, MacWindowManager *wm, const char *string);
uint32 drawAndSelectMenu(int x, int y, int item);
Common::String getItemText(int item);
bool draw(ManagedSurface *g, bool forceRedraw = false) override;
private:
int _mouseX;
int _mouseY;
int _menuItemId;
int _menuId;
bool mouseClicked(int x, int y);
};
} // End of namespace Graphics
#endif

View File

@ -27,6 +27,7 @@ MODULE_OBJS := \
macgui/macdialog.o \
macgui/macfontmanager.o \
macgui/macmenu.o \
macgui/macpopupmenu.o \
macgui/mactext.o \
macgui/mactextwindow.o \
macgui/macwidget.o \