view mde/gui/widget/miscContent.d @ 161:e3fe6acc16fb

Replaced WidgetManager's click and motion callbacks with a drag event system. This is less flexible, but much closer to what is required (and is simpler and less open to bugs through unintended use). The widget under the mouse is now passed (although could just as easily have been before).
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 21 May 2009 22:15:40 +0200
parents 17438f17bfb5
children 2476790223b8
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/>. */

/******************************************************************************
 * Some non-parent widgets for displaying or editing content.
 * 
 * (There are other non-parent content widgets in TextWidget).
 *****************************************************************************/
module mde.gui.widget.miscContent;

import mde.gui.widget.AChildWidget;
import mde.content.AStringContent;
import mde.gui.exception;


debug {
    import tango.util.log.Log : Log, Logger;
    private Logger logger;
    static this () {
	logger = Log.getLogger ("mde.gui.widget.miscContent");
    }
}

/// Editable boolean widget
class BoolContentWidget : AButtonWidget
{
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData, IContent c) {
	content_ = cast(BoolContent) c;
        if (content_ is null) throw new ContentException (this);
        super (mgr, parent, id);
        wdimPair s = mgr.renderer.getToggleSize;
        w = mw = s.x;
        h = mh = s.y;
        content_.addCallback (&contentRedraw);
    }
    
    override IContent content () {
        return content_;
    }
    
    override void draw () {
        mgr.renderer.drawToggle (x,y, content_(), pushed);
    }
    
    override void activated () {
        content_ = !content_();
    }
    
protected:
    BoolContent content_;
}

/// A button connected to an EventContent
class ButtonContentWidget : AButtonWidget
{
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData, IContent c) {
	content_ = cast(Content) c;
        if (content_ is null) throw new ContentException (this);
        adapter = mgr.renderer.getAdapter ();
        super (mgr, parent, id);
    }
    
    override bool setup (uint n, uint flags) {
	if (!(flags & 3)) return false;	// string or renderer (and possibly font) changed
	adapter.text = content_.toString(1);
	adapter.getDimensions (mw, mh);
	if (mw != w || mh != h) {
	    w = mw;
	    h = mh;
	}
	return true;
    }
    
    override IContent content () {
        return content_;
    }
    
    override void draw () {
	super.draw();
	adapter.draw (x,y);
    }
    
    override void activated () {
	content_.endEvent;
    }
    
protected:
    IRenderer.TextAdapter adapter;
    Content content_;
    int index;
}

/// Display a double in a progress bar/slider. Non-editable.
class SliderContentWidget : AChildWidget
{
    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData, IContent c) {
	content_ = cast(DoubleContent) c;
        if (content_ is null) throw new ContentException (this);
        super (mgr, parent, id);
        wdimPair s = mgr.renderer.getSliderSize;
        w = mw = s.x;
        h = mh = s.y;
        content_.addCallback (&contentRedraw);
    }
    
    override IContent content () {
        return content_;
    }
    
    override bool isWSizable () {
        return true;
    }
    
    override void draw () {
        mgr.renderer.drawSlider (x,y, w, content_());
    }
    
protected:
    DoubleContent content_;
}