Plugin: Adds Standard Menus and Shortcuts too basic not to have (new box, save etc.)
| 25 | * Plugin: Adds Standard Menus and Shortcuts too basic not to have (new box, save etc.) |
| 26 | */ |
| 27 | public class DefaultMenus extends Box { |
| 28 | |
| 29 | static public final Dict.Prop<FunctionOfBox<Box>> newBox = new Dict.Prop<FunctionOfBox<Box>>("newBox").toCanon() |
| 30 | .doc("`_.newBox()` will create a new box that's a child of this one"); |
| 31 | |
| 32 | static public final Dict.Prop<BiFunctionOfBoxAnd<Class, Box>> newBoxOfClass = new Dict.Prop<BiFunctionOfBoxAnd<Class, Box>>("newBoxOfClass").toCanon() |
| 33 | .doc("`_.newBoxOfClass(c)` create a new box that's a child of this one, with a custom class `c`"); |
| 34 | |
| 35 | static public final Dict.Prop<BiFunctionOfBoxAnd<String, Box>> ensureChild = new Dict.Prop<FunctionOfBox<Box>>("ensureChild").toCanon() |
| 36 | .doc("`_.ensureChild('name')` creates a new box that's a child of this one, if there already isn't one with this `name`"); |
| 37 | |
| 38 | static public final Dict.Prop<Boolean> wasNew = new Dict.Prop<Boolean>("wasNew").toCanon() |
| 39 | .doc("`_.wasNew` is set to indicate that the box mentioned by calls like `_.ensureChild()` was actually created, not merely found"); |
| 40 | |
| 41 | static public final Dict.Prop<TriFunctionOfBoxAnd<String, Class, Box>> ensureChildOfClass = new Dict.Prop<BiFunctionOfBoxAnd<Class, Box>>("ensureChildOfClass").toCanon() |
| 42 | .doc("`_.ensureChildOfClass('name', Something.class)` create a new box that's a peer of this one, with a custom class `Something`, if one called `name` doesn't already exist"); |
| 43 | |
| 44 | static public final Dict.Prop<BiFunctionOfBoxAnd<Class, Box>> setClass = new Dict.Prop<FunctionOfBox<Box>>("setClass").toCanon() |
| 45 | .doc("`_ = _.setClass(Something.class)` sets the class 'this' box to be `Something`. This only does anything if `Something` is a valid subclass of `Box`"); |
| 46 | |
| 47 | static public final Dict.Prop<FunctionOfBox<Box>> deleteBox = new Dict.Prop<FunctionOfBox<Box>>("deleteBox").toCanon().type() |
| 48 | .doc("delete this box"); |
| 49 | |
| 50 | // this gets set if we successfully opened something |
| 51 | static public volatile boolean safeToSave = false; |
| 52 | |
| 53 | private final Box root; |
| 54 | private String filename; |
| 55 | boolean saveOnExit = true; |
| 56 | |
| 57 | public DefaultMenus(Box root, String filename) { |
| 58 | this.root = root; |
| 59 | this.filename = filename; |
| 60 | properties.put(MarkingMenus.menuSpecs, (event) -> { |
| 61 | if (isNothingSelected()) { |
| 62 | |
| 63 | MarkingMenus.MenuSpecification spec = new MarkingMenus.MenuSpecification(); |
| 64 | |
| 65 | MarkingMenus.MenuSpecification saveMenu = new MarkingMenus.MenuSpecification(); |
| 66 | saveMenu.items.put(MarkingMenus.Position.E, new MarkingMenus.MenuItem("Save", () -> { |
| 67 | save(); |
| 68 | })); |
| 69 | |
| 70 | saveMenu.items.put(MarkingMenus.Position.S, new MarkingMenus.MenuItem(saveOnExit ? "Save on exit (toggle)" : "Don't save on exit (toggle)", () -> { |
| 71 | saveOnExit = !saveOnExit; |
| 72 | })); |
| 73 | |
| 74 | spec.items.put(MarkingMenus.Position.E, new MarkingMenus.MenuItem("Save...", () -> { |
| 75 | save(); |
| 76 | }).setSubmenu(saveMenu)); |
| 77 | spec.items.put(MarkingMenus.Position.N, new MarkingMenus.MenuItem("New Box", () -> { |
| 78 | Vec2 at = convertCoordinateSystem(event.after); |
| 79 | newBox(at, root); |
| 80 | })); |
| 81 | |
| 82 | return spec; |
| 83 | } |
| 84 | return null; |