diff mde/gui/widget/ParentContent.d @ 143:2ac3e0012788

Added a simple Slider widget. Moved SwitchWidget into PopupMenuWidget's module and renamed the module ParentContent. Made editContent create DisplayContentWidgets for DebugContent (instead of ButtonContentWidgets).
author Diggory Hardy <diggory.hardy@gmail.com>
date Mon, 09 Feb 2009 23:27:41 +0000
parents mde/gui/widget/PopupMenu.d@6f69a9c111eb
children 66c58e5b0062
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mde/gui/widget/ParentContent.d	Mon Feb 09 23:27:41 2009 +0000
@@ -0,0 +1,229 @@
+/* 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/>. */
+
+/******************************************************************************
+ * A pop-up widget and a switch (tab) widget (both parent widgets using
+ * content).
+ *****************************************************************************/
+module mde.gui.widget.ParentContent;
+
+import mde.gui.widget.AParentWidget;
+import mde.gui.widget.layout;
+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.ParentContent");
+    }
+}
+
+/******************************************************************************
+ * Widget which pops up a ContentListWidget created with its content.
+ * 
+ * Is a button displaying a content string, just like DisplayContentWidget.
+ * 
+ * Popped up widget is a ContentListWidget created from the same creation data.
+ *****************************************************************************/
+class PopupMenuWidget : APopupParentWidget
+{
+    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData data, IContent c) {
+	content = cast(Content)c;
+	WDCMinCheck (data, 3,1, content);
+        super (mgr, parent, id);
+        
+        //popup = mgr.makeWidget (this, data.strings[0], content);
+        popup = new ContentListWidget (mgr, this, id, data, c);
+        subWidgets = [popup];
+	
+        cIndex = data.ints[2];
+        if (cIndex == 0)
+            content.addCallback (&updateVal);
+        adapter = mgr.renderer.getAdapter;
+	adapter.text = content.toString (cIndex);
+	adapter.getDimensions (mw, mh);
+	w = mw;
+	h = mh;
+    }
+    
+    override void recursionCheck (widgetID wID, IContent c) {
+        if (wID is id && c is content)
+            throw new WidgetRecursionException (wID);
+        parent.recursionCheck (wID, c);
+    }
+
+    override int clickEvent (wdabs, wdabs, ubyte b, bool state) {
+	if (b == 1 && state == true) {
+	    if (!pushed) {
+                parentIPPW.addChildIPPW (this);
+                parentIPPW.menuActive = true;
+                if (popup.width != w && popup.minWidth <= w)
+                    popup.setWidth (w, -1);		// neatness
+                mgr.positionPopup (this, popup);
+                pushed = true;
+            } else if (!parentIPPW.parentMenuActive) {	// if not a submenu
+                parentIPPW.removeChildIPPW (this);
+            }
+	}
+	return 0;
+    }
+    
+    override void removedIPPW () {
+        super.removedIPPW;
+	pushed = false;
+    }
+    
+    override void underMouse (bool state) {
+        if (state && !pushed && parentIPPW.menuActive) {
+            parentIPPW.addChildIPPW (this);
+            menuActive = true;
+            if (parentIPPW.parentMenuActive)
+                mgr.positionPopup (this, popup, 1);
+            else {
+                if (popup.width != w && popup.minWidth <= w)
+                    popup.setWidth (w, -1);		// neatness
+                mgr.positionPopup (this, popup);
+            }
+            pushed = true;
+        }
+    }
+    
+    override void draw () {
+	mgr.renderer.drawButton (x,y, w,h, pushed);
+	adapter.draw (x,y);
+    }
+    
+protected:
+    void updateVal (Content) {	// callback
+        adapter.text = content.toString(cIndex);
+        wdim omw = mw, omh = mh;
+        adapter.getDimensions (mw, mh);
+        if (omw != mw)
+            parent.minWChange (this, mw);
+        if (omh != mh)
+            parent.minHChange (this, mh);
+    }
+    bool pushed = false;
+    IRenderer.TextAdapter adapter;
+    Content content;
+    int cIndex;
+}
+
+/** A "tab" widget: it doesn't display the tabs, but shows one of a number of
+ * widgets dependant on an EnumContent.
+ *
+ * Sizability is set once. Min-size is updated when switching. */
+class SwitchWidget : AParentWidget
+{
+    this (IWidgetManager mgr, IParentWidget parent, widgetID id, WidgetData data, IContent c) {
+        super (mgr, parent, id);
+        content = cast(EnumContent) c;
+        if (content is null || (subWidgets.length = content.list.length) == 0)
+            throw new ContentException (this);
+        WDCheck (data, 1, subWidgets.length);
+        
+        foreach (i,sc; content.list)
+            subWidgets[i] = mgr.makeWidget (this, data.strings[i], sc);
+        currentW = subWidgets[content()];
+        
+        content.addCallback (&switchWidget);
+    }
+    
+    override bool setup (uint n, uint flags) {
+        bool r = super.setup (n, flags);
+        if (r) {
+            mw = currentW.minWidth;
+            mh = currentW.minHeight;
+            w = currentW.width;
+            h = currentW.height;
+            static if (SIZABILITY & SIZABILITY_ENUM.START_TRUE)
+                    isWS = isHS = true;
+            foreach (i,sc; content.list) {
+                static if (SIZABILITY == SIZABILITY_ENUM.ANY_SUBWIDGETS) {
+                    isWS |= subWidgets[i].isWSizable;
+                    isHS |= subWidgets[i].isHSizable;
+                } else static if (SIZABILITY == SIZABILITY_ENUM.ALL_SUBWIDGETS) {
+                    isWS &= subWidgets[i].isWSizable;
+                    isHS &= subWidgets[i].isHSizable;
+                }
+            }
+        }
+        return r;
+    }
+    
+    override void minWChange (IChildWidget widget, wdim nmw) {
+        if (widget !is currentW) return;
+        mw = nmw;
+        parent.minWChange (this, nmw);
+    }
+    override void minHChange (IChildWidget widget, wdim nmh) {
+        if (widget !is currentW) return;
+        mh = nmh;
+        parent.minHChange (this, nmh);
+    }
+    
+    override bool isWSizable () {
+        return isWS;
+    }
+    override bool isHSizable () {
+        return isHS;
+    }
+    
+    override void setWidth (wdim nw, int dir) {
+	w = (nw >= mw ? nw : mw);
+        currentW.setWidth (w, dir);
+    }
+    override void setHeight (wdim nh, int dir) {
+        h = (nh >= mh ? nh : mh);
+        currentW.setHeight (h, dir);
+    }
+    
+    override void setPosition (wdim nx, wdim ny) {
+        x = nx;
+        y = ny;
+        currentW.setPosition (nx,ny);
+    }
+    
+    override IChildWidget getWidget (wdim cx, wdim cy) {
+        return currentW.getWidget (cx, cy);
+    }
+    
+    override void draw () {
+        currentW.draw;
+    }
+    
+protected:
+    // callback on content
+    void switchWidget (Content) {
+        currentW = subWidgets[content()];
+        mw = currentW.minWidth;
+        mh = currentW.minHeight;
+        parent.minWChange (this, mw);
+        parent.minHChange (this, mh);
+        // Parent may change size. If it doesn't, we must set child's size.
+        // We can't tell if it did, so do it (call will be fast if size isn't
+        // changed anyway).
+        currentW.setWidth (w, -1);
+        currentW.setHeight (h, -1);
+        currentW.setPosition (x,y);
+    }
+    
+    IChildWidget currentW;
+    EnumContent content;
+    
+    bool isWS, isHS;	// no infrastructure for changing sizability, so need to fix it.
+}