Skip to content

Commit 587df2d

Browse files
committed
- MenuItem Labels can now be empty. Fixes #30
- more debug info on MenuItem parsing errors - removed an irrelevant import (how did it get there)
1 parent 495d2a7 commit 587df2d

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

djFlixel/ui/menu/MItemData.hx

+49-39
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@
4949
- Additionaly {link} has a Confirmation functionality which presents the user
5050
with a question in a popup or a new menu before triggering an action
5151
52-
- You can enable a Confirmation to a link by setting the optional field like this:]
53-
| ?fs=Are you sure?:Yes:No | or by :
54-
| ?pop=Sure?:Yes:No |
52+
- You can enable a Confirmation to a link by setting the optional field like this:
53+
`| ?fs=Are you sure?:Yes:No |`
54+
`| ?pop=Sure?:Yes:No |`
5555
56-
Notice it is one string with = and : used as a separators "?askid=QUESTION:YES:NO"
56+
Notice it is one string with = and : used as a separators `?askid=QUESTION:YES:NO`
5757
`?fs` means that the question will be presented as a new menu page (autogenerated)
58-
and `?pop` will open a small popup over the item and ask (good for short things)
58+
`?pop` will open a small popup over the item and ask (good for short things)
5959
60-
! :TIP: You can skip the question in the "?pop" style by ommiting the question.
61-
e.g. | ?pop=:Yes:No |
60+
- You can *skip the question* by entering an empty string , like so
61+
`?pop=:Yes:No` , notice the `=:`
6262
63-
! :Supports \n newlines in the ASK string, and it will generate multiple labels
64-
e.g. | ?fs=First Line\nSecond Line?:yes:no
63+
- *Supports* `\n` newlines in the ASK string, and it will generate multiple labels
64+
e.g. `?fs=First Line\nSecond Line?:yes:no`
6565
> will produce two labels for the question.
6666
> This is for cases where the question does not fit the screen
6767
@@ -148,21 +148,20 @@
148148
149149
>> {label}
150150
Creates a text element on the menu that is not selectable. Cursor will jump past it
151-
151+
- the `ID` field is *optional*
152152
examples >>>>>>>>>>>>>>>>>>>>
153153
154154
"Advanced Options | label " ; Note that I didn't put an ID
155155
"Advanced Options | label | id_lbl " ; But if you want, you can declare one
156156
"Advanced Options | label | id_lbl |U" ; Make this label Unselectable. The Cursor will JUMP over it
157-
158157
-------------------------------------------------------------
159158
160159
=== For more usage examples, check <MPageData.hx> and <FlxMenu.hx>
161160
162161
***********************************************************************/
163162
package djFlixel.ui.menu;
164163

165-
import flixel.input.actions.FlxActionInput.FlxInputDevice;
164+
import haxe.Exception;
166165
import haxe.EnumTools;
167166

168167
enum MItemType {
@@ -224,46 +223,58 @@ class MItemData
224223

225224
/**
226225
Creates a new Item Data from an encoded String
226+
Encoded String Example: "New Game | link | ng+ | optionA | optionB "
227227
@param str Encoded String. Check <MItemData.hx> header comments for more info on formatting
228228
**/
229229
public function new(str:String)
230230
{
231-
parse(str);
231+
try { parse(str); }
232+
catch(e:String) {
233+
var m = 'Error Parsing line\n"${str}"\n::${e}';
234+
throw new Exception(m);
235+
}
232236
}//---------------------------------------------------;
233237

238+
// @throws <string> errors
234239
function parse(S:String)
235240
{
236-
// Split " A | B | | | C |" into ['A','B','C']
237-
var F = S.split('|').map((i)->StringTools.trim(i)).filter((i)->i.length > 0);
238-
/**
239-
F[0] = Label
240-
F[1] = Type
241-
F[2] = ID
242-
**/
243-
244-
// Get : Label, ItemType, ItemID - which are required for all ItemTypes -
241+
/* DEVNOTE:
242+
- The string should be in this format
243+
LABEL | TYPE | ID | <optional A> | <optional B>
244+
- The string may end with a `|` it will be ignored
245+
- The first two fields are mandatory (label does not need the third ID field)
246+
*/
247+
248+
var F = S.split('|').map((i)->StringTools.trim(i));
249+
250+
if(F.length<2) throw "Insufficient fields";
251+
252+
// Allowing empty Labels, I am just going to warn
245253
label = F.shift();
246-
if (label == null) throw 'MenuItem Must have a Label';
247-
254+
if(label=="") trace('Warning: Empty label for "${S}"');
255+
248256
try{
249257
type = EnumTools.createByName(MItemType, F.shift());
250-
} catch (_) throw 'Wrong MenuItemType at "$label" Typo?';
258+
} catch (_)
259+
throw 'Undefined TYPE, typo?';
251260

252261
ID = F.shift();
253-
if (ID == null && type != MItemType.label) throw 'ItemData "$label" must have an ID';
262+
if ( (ID == null || ID == "") && type != MItemType.label )
263+
throw 'No ID defined';
254264

255-
// -- Must be called after types have finished getting mandatory fields
265+
// Processes all optional fields one by one in a custom function
266+
// func(key,value)
256267
function feed(fn:String->String->Void)
257268
{
258269
for (i in F)
259270
{
260-
// Check for universal parameters:
261-
// Currently : { |D| , |U| , |AF| }
262-
263-
271+
// Handle strings ending with an |
272+
if(i=="") continue;
273+
274+
// Check for universal options first ::
275+
264276
// |AF| , for autofocusing
265-
if (i == "AF")
266-
{
277+
if (i == "AF") {
267278
P.autofocus = true;
268279
continue;
269280
}
@@ -283,15 +294,15 @@ class MItemData
283294
// All other Fields must be in this format "KEY=VALUE"
284295
var KV = i.split('=');
285296
if (KV.length != 2){
286-
throw 'Illegal MenuItem Parameter for $ID';
297+
throw 'Illegal Parameter for "${i}"';
287298
}
288299

289300
// |I=Information Text| for attaching some text to the item
290301
if (KV[0] == "I") {
291302
info = KV[1];
292303
continue;
293304
}
294-
305+
295306
// Pass everything else to type specific code
296307
fn(KV[0], KV[1]);
297308
}
@@ -317,14 +328,13 @@ class MItemData
317328
if (k == "fs") P.ltype = 3;
318329
P.ask = v.split(":");
319330
if (P.ask.length != 3)
320-
throw 'Improper Ask Format. "Question:Yes:No" - Skip the Question with ":Yes:No"';
331+
throw 'Improper Ask Format';
321332
}
322333
});
323334

324-
325335

326336
case range:
327-
if (F[0] == null) throw 'Range Item $ID needs a range';
337+
if (F[0] == null) throw 'Must define a Range';
328338
var _n = F.shift().split(',');
329339
P.c = 0.0; // I need it to be Float so I can increment with a float later
330340
P.step = 1;
@@ -346,7 +356,7 @@ class MItemData
346356
}
347357

348358
case list:
349-
if (F[0] == null) throw 'List Item $ID needs a list';
359+
if (F[0] == null) throw 'Must define a List';
350360
P.c = 0;
351361
P.loop = false;
352362
P.list = F.shift().split(',');

0 commit comments

Comments
 (0)