Mercurial > projects > mde
diff mde/gui/widget/PopupMenu.d @ 113:9824bee909fd
Popup menu; works for simple menus except that clicking an item doesn't close it.
Revised popup support a bit; EnumContentWidget is broken and due to be replaced.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Fri, 19 Dec 2008 10:32:28 +0000 |
parents | mde/gui/widget/Popup.d@fe061009029d |
children | b16a534f5302 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mde/gui/widget/PopupMenu.d Fri Dec 19 10:32:28 2008 +0000 @@ -0,0 +1,168 @@ +/* LICENSE BLOCK +Part of mde: a Modular D game-oriented Engine +Copyright © 2007-2008 Diggory Hardy + +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 2 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/>. */ + +/************************************************************************************************* + * Pop-up menus based on content structures. + *************************************************************************************************/ +module mde.gui.widget.PopupMenu; + +import mde.gui.widget.Widget; +import mde.gui.widget.textContent; +import mde.gui.widget.TextWidget; +import mde.gui.widget.layout; + +import mde.content.Content; +import mde.gui.exception; + +debug { + import tango.util.log.Log : Log, Logger; + private Logger logger; + static this () { + logger = Log.getLogger ("mde.gui.widget.PopupMenu"); + } +} + +/************************************************************************************************* + * Widget which pops up a menu based on a content. + *************************************************************************************************/ +class PopupMenuWidget : AParentSingleWidget +{ + this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { + content = c; + WDCMinCheck (data, 1,0, content); + subWidget = menuContent (mgr, id, data, content); + + adapter = mgr.renderer.getAdapter; + adapter.text = content.toString (1); + adapter.getDimensions (mw, mh); + w = mw; + h = mh; + super (mgr, id, data); + } + + int clickEvent (wdabs, wdabs, ubyte b, bool state) { + if (b == 1 && state == true) { + // If active, the popup is closed by WidgetManager since the click isn't on the popup. + if (!pushed) { + pushed = true; + mgr.addPopup (this, subWidget); // causes redraw + mgr.addClickCallback (&openMenuCallback); // prevents first up-click from closing menu, if on self. + } + } + return 0; + } + + void popupRemoved () { + pushed = false; + } + + void draw () { + mgr.renderer.drawButton (x,y, w,h, pushed); + adapter.draw (x,y); + } + +protected: + bool openMenuCallback (wdabs cx, wdabs cy, ubyte b, bool state) { + if (b == 1 && state == false) { // receive first up-click + mgr.removeCallbacks (cast(void*) this); + if (cx >= x && cx < x+w && cy >= y && cy < y+h) + return true; // up-click is on self; don't close the menu + } + return false; + } + bool pushed = false; + IRenderer.TextAdapter adapter; + IContent content; +} + +/************************************************************************************************* + * A function which returns the most appropriate content menu widget. + *************************************************************************************************/ +IChildWidget menuContent (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { if (c is null) throw new ContentException; + if (cast(ContentList) c) + return new MenuContentListWidget(mgr,id,data,c); + else if (cast(EventContent) c) + return new MenuButtonContentWidget(mgr,id,data,c); + else // generic uneditable option + return new DisplayContentWidget(mgr,id,data,c); +} + +/** A menu content-button, like ButtonContentWidget, but which can be activated with the up-click. + */ +class MenuButtonContentWidget : ATextWidget +{ + this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) { + content = cast(EventContent) c; + WDCMinCheck (data, 1,0, content); + adapter = mgr.renderer.getAdapter (); + super (mgr, id, data); + } + + bool setup (uint n, uint flags) { + if (!(flags & 3)) return false; // string or renderer (and possibly font) changed + adapter.text = content.toString(1); + return super.setup (n, 3); // force redimensioning + } + + int clickEvent (wdabs, wdabs, ubyte b, bool state) { + if (b == 1) { // on up or down click + pushed = false; + mgr.requestRedraw; + content.endEvent; + } + return 0; + } + + void highlight (bool state) { + pushed = state; + } + + void draw () { + mgr.renderer.drawButton (x,y, w,h, pushed); + adapter.draw (x,y); + } + +protected: + EventContent content; + bool pushed; +} + +/// Similar to layout.ContentListWidget but creates sub-widgets with menuContent. +class MenuContentListWidget : GridWidget +{ + this (IWidgetManager mgr, widgetID id, WidgetData data, IContent content) { + cList = cast(ContentList) content; + WDCCheck (data, 2, 0, cList); + + cols = 1; + if ((rows = cList.list.length) > 0) { + subWidgets.length = rows; + foreach (i, c; cList.list) { + subWidgets[i] = menuContent (mgr,id,data,c); + } + } else { + rows = 1; + subWidgets = [mgr.makeWidget (id, new ErrorContent ("<empty list>"))]; + } + super (mgr, id, data); + } + + bool saveChanges () { + return false; // sub-widgets don't have an id + } + +private: + ContentList cList; +}