comparison mde/content/Items.d @ 128:41582439a42b

Added support for dynamic EnumContent loading and saving, with translation loading. WMScreen.init removed; code moved to this() since class is now created by main() instead of a static this(). Fix for SwitchWidget not passing events. Still some resizing bugs evident in SwitchWidget :-(
author Diggory Hardy <diggory.hardy@gmail.com>
date Wed, 14 Jan 2009 20:24:14 +0000
parents c9843fbaac88
children 264028f4115a
comparison
equal deleted inserted replaced
127:3328c6fb77ca 128:41582439a42b
39 * 39 *
40 * E.g. get ("Options.MiscOptions.L10n") returns miscOpts.L10n, 40 * E.g. get ("Options.MiscOptions.L10n") returns miscOpts.L10n,
41 * Items.get ("Options.MiscOptions") returns a ContentList of all misc options. */ 41 * Items.get ("Options.MiscOptions") returns a ContentList of all misc options. */
42 Content get (char[] item) { 42 Content get (char[] item) {
43 assert (currentL10n is miscOpts.L10n(), "must call loadTranslation (code error)"); 43 assert (currentL10n is miscOpts.L10n(), "must call loadTranslation (code error)");
44 44 char[] orig = item; // item is modified by head()
45
45 char[] h = head (item); 46 char[] h = head (item);
46 if (h == "Options") { 47 if (h == "Options") {
47 if (item is null) 48 if (item is null)
48 return Options.allContentList; 49 return Options.allContentList;
49 50
61 h = head (item); 62 h = head (item);
62 if (h == "menu" && item is null) 63 if (h == "menu" && item is null)
63 return imde.menu; 64 return imde.menu;
64 else if (h == "quit" && item is null) 65 else if (h == "quit" && item is null)
65 return imde.quit; 66 return imde.quit;
66 else if (h == "sw" && item is null) 67 } else if (h == "dynamic") {
67 return imde.sw; 68 auto i = head (item) in items;
68 } 69 if (i) return *i;
69 logger.warn ("Bad content specifier: {}",h); 70 }
70 return new ErrorContent ("Error: bad content specifier",h); 71
72 return new ErrorContent ("Error: bad content specifier", orig);
71 } 73 }
72 74
73 /** Creates some content on first run (required by get()). 75 /** Creates some content on first run (required by get()).
74 * 76 *
75 * If the correct translation strings are not loaded, this loads them. */ 77 * If the correct translation strings are not loaded, this loads them. */
86 list[i++] = opts.contentList; 88 list[i++] = opts.contentList;
87 } 89 }
88 Options.allContentList = new ContentList ("Options", list); 90 Options.allContentList = new ContentList ("Options", list);
89 } 91 }
90 92
93 Translation trl;
94 Translation.Entry trle;
95
91 // Translate Options: 96 // Translate Options:
92 Translation.Entry trle;
93 with (Options.allContentList) { 97 with (Options.allContentList) {
94 trle = Translation.get (symbol).getStruct (symbol); 98 trle = Translation.get (symbol).getStruct (symbol);
95 name (trle.name, trle.desc); 99 name (trle.name, trle.desc);
96 } 100 }
97 foreach (n,opts; Options.optionsClasses) { 101 foreach (n,opts; Options.optionsClasses) {
98 Translation trl;
99 trl = Translation.get (n); 102 trl = Translation.get (n);
100 trle = trl.getStruct (n); 103 trle = trl.getStruct (n);
101 opts.contentList.name (trle.name, trle.desc); 104 opts.contentList.name (trle.name, trle.desc);
102 foreach (s, v; opts.content) { 105 foreach (s, v; opts.content) {
103 trle = trl.getStruct (s); 106 trle = trl.getStruct (s);
111 } 114 }
112 } 115 }
113 } 116 }
114 117
115 // Translate imde: 118 // Translate imde:
116 trle = Translation.get ("imde").getStruct ("menu"); 119 trl = Translation.get ("imde");
120 trle = trl.getStruct ("menu");
117 imde.menu.name (trle.name, trle.desc); 121 imde.menu.name (trle.name, trle.desc);
118 trle = Translation.get ("imde").getStruct ("quit"); 122 trle = trl.getStruct ("quit");
119 imde.quit.name (trle.name, trle.desc); 123 imde.quit.name (trle.name, trle.desc);
124
125 // Translate dynamic content:
126 if (items.length) {
127 trl = Translation.get ("dynamic");
128 foreach (n,item; items) {
129 trle = trl.getStruct (n);
130 item.name (trle.name, trle.desc);
131 IContentList cl = cast(IContentList) item;
132 if (cl) {
133 foreach (i,c; cl.list) {
134 trle = trl.getStruct (c.symbol);
135 c.name (trle.name, trle.desc);
136 }
137 }
138 }
139 }
120 140
121 currentL10n = miscOpts.L10n(); 141 currentL10n = miscOpts.L10n();
122 } 142 }
123 143
144 /** Add content c with name c.symbol, so that translations are loaded for it and it can be
145 * returned by get ("dynamic."~c.symbol). */
146 Content addContent (Content c) {
147 items[c.symbol] = c;
148 return c;
149 }
150
124 private: 151 private:
152 // NOTE: possibly add all content to this list. Lookups would be faster.
153 Content[char[]] items; // dynamically added content
154
125 /** Takes the string "head.tail" where tail may contain '.' but head does not, returns "head", 155 /** Takes the string "head.tail" where tail may contain '.' but head does not, returns "head",
126 * with str set to "tail". */ 156 * with str set to "tail". */
127 char[] head (ref char[] str) { 157 char[] head (ref char[] str) {
128 size_t i = 0; 158 size_t i = 0;
129 while (i < str.length && str[i] != '.') 159 while (i < str.length && str[i] != '.')