view mde/gui/widget/PopupMenu.d @ 119:d28aea50c6da

Basic edit cursor placement using the mouse. Moved textContent.d's contents into TextWidget.d.
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 01 Jan 2009 14:52:09 +0000
parents aba2dd815a1f
children 5b37d0400732
line wrap: on
line source

/* 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.TextWidget;
import mde.gui.widget.layout;

import mde.content.miscContent;
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,1, content);
	subWidget = mgr.makeWidget (data.strings[0], content);
	
	adapter = mgr.renderer.getAdapter;
	adapter.text = content.toString (1);
	adapter.getDimensions (mw, mh);
	w = mw;
	h = mh;
	super (mgr, id, data);
    }
    
    override 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;
    }
    
    override void popupClose () {
	pushed = false;
    }
    override bool popupParentClick () {
        pushed = false;
        return true;
    }
    
    override 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;
}

/*************************************************************************************************
 * Widget which pops up a sub-menu based on a content on mouse-over.
 *************************************************************************************************/
class SubMenuWidget : PopupMenuWidget
{
    this (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
        super (mgr, id, data, c);
    }
    
    override int clickEvent (wdabs, wdabs, ubyte b, bool state) {
        return 0;
    }
    
    override void highlight (bool state) {
        if (state && !pushed) {
            pushed = true;
            mgr.addPopup (this, subWidget, 1);	// causes redraw
        }
    }
}

/*************************************************************************************************
 * A function which returns a ContentListWidget or MenuButtonContentWidget.
 *************************************************************************************************/
IChildWidget flatMenuContent (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
    if (c is null) throw new ContentException;
    if (cast(IContentList) c)
	return new ContentListWidget(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 function which returns a SubMenuWidget or MenuButtonContentWidget.
 *************************************************************************************************/
IChildWidget subMenuContent (IWidgetManager mgr, widgetID id, WidgetData data, IContent c) {
    if (c is null) throw new ContentException;
    if (cast(IContentList) c)
	return new SubMenuWidget(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);
    }
    
    override 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
    }
    
    override int clickEvent (wdabs, wdabs, ubyte b, bool state) {
	if (b == 1) {	// on up or down click
	    pushed = false;
	    mgr.requestRedraw;
	    content.endEvent;
	}
	return 0;
    }
    
    override void highlight (bool state) {
	pushed = state;
    }
    
    override void draw () {
	mgr.renderer.drawButton (x,y, w,h, pushed);
	adapter.draw (x,y);
    }
    
protected:
    EventContent content;
    bool pushed;
}