Changeset 93:08a4ae11454b

Show
Ignore:
Timestamp:
10/21/08 06:35:15 (3 months ago)
Author:
Diggory Hardy <diggory.hardy@gmail.com>
branch:
default
Message:

Widgets now save dimensions without preventing structural changes in the base config file from applying.

Widget dimensional data separated from other data in files, hence above change.
Moved TextAdapter? from TextWidget? to IRenderer.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • data/conf/gui.mtt

    r92 r93  
    55<WidgetData|root={0:[0xC100,0,3,3],1:["square","blank","square","blank","content","blank","square","blank","square"]}> 
    66<WidgetData|square={0:[0x1,6,6]}> 
    7 <WidgetData|content={0:[0xC100,0,4,2],1:["floating","button","blank","blank","blank","opts","blank","blank"]}> 
     7<WidgetData|content={0:[0xC100,0,4,2],1:["floating","button","blank","blank","f2","opts","blank","blank"]}> 
    88<WidgetData|button={0:[0x10,50,50]}> 
    99<WidgetData|blank={0:[0x2]}> 
     
    1616<WidgetData|optSep={0:[0x21, 0xff],1:["="]}> 
    1717<WidgetData|floating={0:[0x8200,20,20],1:["text"]}> 
     18<WidgetData|f2={0:[0x8200,50,20],1:["button"]}> 
    1819<WidgetData|text={0:[0x21,0xFF0000],1:["Floating text"]}> 
    1920{Basic} 
  • mde/gui/WidgetDataSet.d

    r91 r93  
    5151        if (tp == "WidgetData" && (id in widgetData) is null) { 
    5252            widgetData[id] = deserialize!(WidgetData) (dt); 
     53        } else if (tp == "WDims" && (id in dimData) is null) { 
     54            dimData[id] = cast(wdims) deserialize!(int[]) (dt); 
    5355        } 
    5456    } 
     
    6769    } 
    6870     
     71    /** Get the widget dimensions for widget i (null if none). */ 
     72    wdims dims (widgetID id) { 
     73        auto p = id in dimData; 
     74        return p ? *p : null; 
     75    } 
     76     
    6977protected: 
    70     WidgetData[widgetID] widgetData;    // Per-widget data: 
     78    WidgetData[widgetID] widgetData;    // Per-widget data 
     79    wdims[widgetID] dimData;            // Per-widget sizes 
    7180} 
    7281 
     
    91100        foreach (id,data; widgetData) 
    92101            dlg ("WidgetData", id, serialize!(WidgetData) (data)); 
     102        foreach (id,dim; dimData) 
     103            dlg ("WDims", id, serialize!(int[]) (cast(int[]) dim)); 
    93104    } 
    94105    //END Mergetag code 
     
    101112    } 
    102113     
     114    /** Set the widget dimensions for widget i. */ 
     115    void setDims (widgetID id, wdims d) { 
     116        dimData[id] = d; 
     117        base.dimData[id] = d; 
     118    } 
     119     
    103120    /** Do any changes exist? True if no changes have been stored. */ 
    104121    bool none () { 
  • mde/gui/WidgetManager.d

    r92 r93  
    334334         
    335335        // Make all widgets save any changed data; return if no changes: 
    336         if (!child.saveChanges ("root")
     336        if (!child.saveChanges
    337337            return; 
    338338         
     
    426426    } 
    427427     
    428     /** For making changes. */ 
     428    /** Get dimension data for a widget. */ 
     429    wdims dimData (widgetID id) { 
     430        return curData.dims (id); 
     431    } 
     432     
     433    /** For making changes to widget structure. */ 
    429434    void setData (widgetID id, WidgetData d) { 
    430435        changes[id] = d;        // also updates WidgetDataSet in data. 
     436    } 
     437     
     438    /** For making changes to widget dimensions. */ 
     439    void setDimData (widgetID id, wdims d) { 
     440        changes.setDims(id, d);    // also updates WidgetDataSet in data. 
    431441    } 
    432442     
  • mde/gui/renderer/IRenderer.d

    r80 r93  
    1818 
    1919public import mde.gui.types; 
     20import mde.font.font; 
    2021 
    2122/** Interface for renderers. 
     
    9192    /** Draws a button frame, in if pushed == true. */ 
    9293    void drawButton (wdim x, wdim y, wdim w, wdim h, bool pushed); 
     94     
     95    /** Get a TextAdapter to draw some text. */ 
     96    TextAdapter getAdapter (char[] text, int colour); 
     97     
     98    /** For drawing text - one instance per string. 
     99     * 
     100     * NOTE: currently inflexible. Could use (function) pointers, class interfaces or struct 
     101     * interfaces when available to allow flexibility. */ 
     102    struct TextAdapter { 
     103        void set (char[] c, int col) { 
     104            content = c; 
     105            colour = Colour (col); 
     106        } 
     107         
     108        void getDimensions (out wdsize w, out wdsize h) { 
     109            font.updateBlock (content, textCache); 
     110            w = cast(wdim) textCache.w; 
     111            h = cast(wdim) textCache.h; 
     112        } 
     113         
     114        void draw (wdabs x, wdabs y) { 
     115            font.textBlock (x,y, content, textCache, colour); 
     116        } 
     117         
     118        char[] content; 
     119        TextBlock textCache; 
     120        Colour colour; 
     121        FontStyle font; 
     122    } 
    93123    //END draw routines 
    94124} 
  • mde/gui/renderer/SimpleRenderer.d

    r91 r93  
    2020 
    2121import gl = mde.gl.basic; 
     22import mde.font.font; 
    2223 
    2324/** Interface for renderers. 
     
    3031class SimpleRenderer : IRenderer 
    3132{ 
     33    this () { 
     34        defaultFont = FontStyle.get("default"); 
     35    } 
     36     
    3237    BorderDimensions setSizable (bool wS, bool hS) { 
    3338        wSizable = wS; 
     
    115120    } 
    116121     
     122    TextAdapter getAdapter (char[] text, int col) { 
     123        TextAdapter a; 
     124        a.font = defaultFont; 
     125        a.set (text, col); 
     126        return a; 
     127    } 
     128     
    117129protected: 
    118130    bool wSizable, hSizable; 
    119131    BorderDimensions border; 
    120132    BorderDimensions resize; 
     133    FontStyle defaultFont; 
    121134} 
  • mde/gui/types.d

    r80 r93  
    4848} 
    4949 
     50/// Used to save column sizes, etc. 
     51alias wdim[] wdims; 
     52 
    5053 
    5154/************************************************************************************************* 
  • mde/gui/widget/Ifaces.d

    r92 r93  
    6262    IChildWidget makeWidget (widgetID id, IContent content = null); 
    6363     
     64    /// Get dimensional data. 
     65    wdims dimData (widgetID id); 
     66     
    6467    /** Record some changes, for saving. Should only be called from IWidget.saveChanges() to avoid 
    65      * multiple calls for instanced widgets of same id. */ 
     68     * multiple calls for instanced widgets of same id. 
     69     *  
     70     * WidgetData is for most data, dimensional data (wdims) is for dimensions. */ 
    6671    void setData (widgetID id, WidgetData); 
     72    void setDimData (widgetID id, wdims d);     /// ditto 
    6773     
    6874    // Rendering: 
     
    160166     * If the widget has subwidgets, it should also be recursively called on these (passing their  
    161167     * ids). */ 
    162     // FIXME - no longer necessary to pass id! 
    163     bool saveChanges (widgetID id); 
     168    bool saveChanges (); 
    164169     
    165170    /** Called when the renderer is changed (at least when the changes affect dimensions). 
  • mde/gui/widget/TextWidget.d

    r91 r93  
    2525import mde.gui.content.Content; 
    2626 
    27 import mde.font.font; 
    28  
    29 /// Adapter to ease use of ContentLabelWidget 
    30 struct TextAdapter { 
    31     void set (char[] c, int col) { 
    32         //FIXME tie font to renderer or so 
    33         if (font is null) font = FontStyle.get("default"); 
    34          
    35         content = c; 
    36         colour = Colour (col); 
    37     } 
    38      
    39     void getDimensions (out wdsize w, out wdsize h) { 
    40         font.updateBlock (content, textCache); 
    41         w = cast(wdim) textCache.w; 
    42         h = cast(wdim) textCache.h; 
    43     } 
    44      
    45     void draw (wdabs x, wdabs y) { 
    46         font.textBlock (x,y, content, textCache, colour); 
    47     } 
    48      
    49     char[] content; 
    50     TextBlock textCache; 
    51     Colour colour; 
    52     static FontStyle font; 
    53 } 
    54  
    5527/// Basic text widget 
    5628class TextLabelWidget : Widget 
     
    6436    this (IWidgetManager mgr, widgetID id, WidgetData data) { 
    6537        WDCheck (data, 2, 1); 
    66         adapter.set (data.strings[0], data.ints[1]); 
     38        adapter = mgr.renderer.getAdapter (data.strings[0], data.ints[1]); 
    6739        adapter.getDimensions (mw, mh); 
    6840        super (mgr, id, data); 
     
    7547     
    7648protected: 
    77     TextAdapter adapter; 
     49    IRenderer.TextAdapter adapter; 
    7850} 
    7951 
     
    8557        content = c; 
    8658        index = data.ints[1]; 
    87         adapter.set (content.toString(index), data.ints[2]); 
     59        adapter = mgr.renderer.getAdapter (content.toString(index), data.ints[2]); 
    8860        adapter.getDimensions (mw, mh); 
    8961        super (mgr, id,data); 
     
    9668     
    9769protected: 
    98     TextAdapter adapter; 
     70    IRenderer.TextAdapter adapter; 
    9971    IContent content; 
    10072    int index; 
  • mde/gui/widget/Widget.d

    r92 r93  
    4646//BEGIN Load and save 
    4747    // Base this() for child Widgets. 
    48     this (IWidgetManager mgr, widgetID, WidgetData) { 
     48    this (IWidgetManager mgr, widgetID id, WidgetData) { 
    4949        this.mgr = mgr; 
     50        this.id = id; 
    5051    } 
    5152     
     
    6061     
    6162    // Don't save any data: fine for many widgets. 
    62     // FIXME: implementation could be added to ParentWidget 
    63     bool saveChanges (widgetID) { 
     63    bool saveChanges () { 
    6464        return false; 
    6565    } 
     
    145145    } 
    146146     
     147    widgetID id;                // The widget's ID, used for saving data 
    147148    IWidgetManager mgr;     // the enclosing window 
    148149    wdim x, y;          // position 
     
    163164    IChildWidget[] children () { 
    164165        return subWidgets; 
     166    } 
     167     
     168    bool saveChanges () { 
     169        bool c = false; 
     170        foreach (w; subWidgets) 
     171            c |= w.saveChanges; 
     172        return c; 
    165173    } 
    166174     
  • mde/gui/widget/layout.d

    r92 r93  
    6262        // Get grid size and check data 
    6363        // Check sufficient data for type, align-flags, rows, cols, and possibly row/col widths. 
    64         if (data.ints.length < 4) throw new WidgetDataException (this); 
     64        if (data.ints.length != 4) throw new WidgetDataException (this); 
    6565         
    6666        rows = data.ints[2]; 
    6767        cols = data.ints[3]; 
    68         // Check: at least one sub-widget, ints length == 3 or also contains row & col widths, 
    69         // strings' length is correct: 
    70         if (rows < 1 || cols < 1 || 
    71             (data.ints.length != 4 && data.ints.length != 4 + rows + cols) || 
    72             data.strings.length != rows * cols) 
     68        // Check: at least one sub-widget, ints length == 3, strings' length is correct: 
     69        if (rows < 1 || cols < 1 || data.ints.length != 4 || data.strings.length != rows * cols) 
    7370            throw new WidgetDataException (this); 
    74         this.data = data; 
    7571         
    7672        // Get all sub-widgets 
     
    8076        } 
    8177         
    82         if (data.ints.length == 4 + rows + cols) 
    83             initWidths = cast(wdim[]) data.ints[4..$]; 
     78        initWidths = mgr.dimData (id);  // may be null, tested later 
    8479         
    8580        super (mgr, id, data); 
     
    8782     
    8883    // Save column/row sizes. Currently always do so. 
    89     bool saveChanges (widgetID id) { 
    90         with (data) { 
    91             foreach (i, widget; subWidgets) // recurse on subwidgets 
    92                 widget.saveChanges (strings[i]); 
    93              
    94             ints = ints[0..4] ~ cast(int[])col.width ~ cast(int[])row.width; 
    95         } 
    96         mgr.setData (id, data); 
     84    bool saveChanges () { 
     85        foreach (widget; subWidgets) // recurse on subwidgets 
     86            widget.saveChanges (); 
     87         
     88        mgr.setDimData (id, col.width ~ row.width); 
    9789        return true; 
    9890    } 
    9991protected: 
    100     WidgetData data; 
    10192} 
    10293 
     
    115106        rows = optsList.list.length; 
    116107        cols = 1; 
    117         sWId = data.strings[0]; 
    118108         
    119109        // Get all sub-widgets 
    120110        subWidgets.length = rows*cols; 
    121111        foreach (i, c; optsList.list) { 
    122             subWidgets[i] = mgr.makeWidget (sWId, c); 
     112            subWidgets[i] = mgr.makeWidget (data.strings[0], c); 
    123113        } 
    124114        super (mgr, id, data); 
    125115    } 
    126116     
    127     bool saveChanges (widgetID id) { 
     117    bool saveChanges () { 
    128118        // Since all sub-widgets have the same id, it only makes sense to call on one 
    129119        if (subWidgets is null) 
    130120            return false; 
    131         return subWidgets[0].saveChanges (sWId)
     121        return subWidgets[0].saveChanges
    132122    } 
    133123     
    134124private: 
    135125    OptionList optsList; 
    136     widgetID sWId;      // sub-widget's ID, for calling saveChanges FIXME no longer pass? 
    137126} 
    138127 
     
    188177     * As such, this must be the first function called after this(). */ 
    189178    void finalize () { 
    190         if (initWidths) { 
    191             debug assert (initWidths.length == cols + rows, "initWidths provided but has bad length"); 
     179        logger.trace ("initWidths.length: {}", initWidths.length); 
     180        if (initWidths.length == cols + rows) { 
    192181            col.setWidths (initWidths[0..cols]); 
    193182            row.setWidths (initWidths[cols..$]); 
    194             initWidths = null;  // free 
    195183        } else { 
    196184            col.setWidths; 
    197185            row.setWidths; 
    198186        } 
     187        initWidths = null;  // free 
    199188         
    200189        mw = col.mw;