|
| 1 | +--[[ |
| 2 | +Minetest |
| 3 | +Copyright (C) 2023 v-rob, Vincent Robinson <[email protected]> |
| 4 | +
|
| 5 | +This program is free software; you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU Lesser General Public License as published by |
| 7 | +the Free Software Foundation; either version 2.1 of the License, or |
| 8 | +(at your option) any later version. |
| 9 | +
|
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU Lesser General Public License for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU Lesser General Public License along |
| 16 | +with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | +--]] |
| 19 | + |
| 20 | +ui._elem_types = {} |
| 21 | + |
| 22 | +function ui._new_type(base, type, type_id) |
| 23 | + local class = core.class(base) |
| 24 | + |
| 25 | + class._type = type |
| 26 | + class._type_id = type_id |
| 27 | + |
| 28 | + ui._elem_types[type] = class |
| 29 | + |
| 30 | + return class |
| 31 | +end |
| 32 | + |
| 33 | +ui.Elem = ui._new_type(nil, "elem", 0) |
| 34 | + |
| 35 | +function ui.Elem:new(props) |
| 36 | + self._id = props.id or ui.new_id() |
| 37 | + self._groups = {} |
| 38 | + self._boxes = {main = true} |
| 39 | + self._style = props.style or ui.Style{props = props} |
| 40 | + self._children = table.merge(props.children or props) |
| 41 | + |
| 42 | + -- Set by parent ui.Elem |
| 43 | + self._parent = nil |
| 44 | + self._index = nil |
| 45 | + self._rindex = nil |
| 46 | + |
| 47 | + -- Set by ui.Window |
| 48 | + self._window = nil |
| 49 | + |
| 50 | + assert(ui.is_id(self._id), "Element ID must be an ID string") |
| 51 | + |
| 52 | + for _, group in ipairs(props.groups or {}) do |
| 53 | + assert(ui.is_id(group), "Element group must be an ID string") |
| 54 | + self._groups[group] = true |
| 55 | + end |
| 56 | + |
| 57 | + for i, child in ipairs(self._children) do |
| 58 | + assert(child._parent == nil, "Element already has a parent") |
| 59 | + assert(not core.is_instance(child, ui.Root), |
| 60 | + "ui.Root can only be a root element") |
| 61 | + |
| 62 | + child._parent = self |
| 63 | + child._index = i |
| 64 | + child._rindex = #self._children - i + 1 |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | +function ui.Elem:_get_flat() |
| 69 | + local elems = {self} |
| 70 | + for _, child in ipairs(self._children) do |
| 71 | + table.insert_all(elems, child:_get_flat()) |
| 72 | + end |
| 73 | + return elems |
| 74 | +end |
| 75 | + |
| 76 | +function ui.Elem:_encode() |
| 77 | + return ui._encode("Bz S", self._type_id, self._id, self:_encode_fields()) |
| 78 | +end |
| 79 | + |
| 80 | +function ui.Elem:_encode_fields() |
| 81 | + local fl = ui._make_flags() |
| 82 | + |
| 83 | + if ui._shift_flag(fl, #self._children > 0) then |
| 84 | + local child_ids = {} |
| 85 | + for i, child in ipairs(self._children) do |
| 86 | + child_ids[i] = child._id |
| 87 | + end |
| 88 | + |
| 89 | + ui._encode_flag(fl, "Z", ui._encode_array("z", child_ids)) |
| 90 | + end |
| 91 | + |
| 92 | + self:_encode_box(fl, self._boxes.main) |
| 93 | + |
| 94 | + return ui._encode("S", ui._encode_flags(fl)) |
| 95 | +end |
| 96 | + |
| 97 | +function ui.Elem:_encode_box(fl, box) |
| 98 | + -- Element encoding always happens after styles are computed and boxes are |
| 99 | + -- populated with style indices. So, if this box has any styles applied to |
| 100 | + -- it, encode the relevant states. |
| 101 | + if not ui._shift_flag(fl, box.n > 0) then |
| 102 | + return |
| 103 | + end |
| 104 | + |
| 105 | + local box_fl = ui._make_flags() |
| 106 | + |
| 107 | + -- For each state, check if there is any styling. If there is, add it |
| 108 | + -- to the box's flags. |
| 109 | + for i = ui._STATE_NONE, ui._NUM_STATES - 1 do |
| 110 | + if ui._shift_flag(box_fl, box[i] ~= ui._NO_STYLE) then |
| 111 | + ui._encode_flag(box_fl, "I", box[i]) |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + ui._encode_flag(fl, "S", ui._encode_flags(box_fl)) |
| 116 | +end |
0 commit comments