From 3d5ac077becfb6490e0e3fbe80a784f489e50113 Mon Sep 17 00:00:00 2001 From: PeaAshMeter Date: Sat, 30 May 2026 00:14:29 +0300 Subject: [PATCH] rename 'private' fields in elements --- lib/simple_ui/core/builder.lua | 4 +-- lib/simple_ui/core/element.lua | 28 +++++++--------- lib/simple_ui/core/multi_child_element.lua | 14 ++++---- lib/simple_ui/core/single_child_element.lua | 6 ++-- lib/simple_ui/elements/center.lua | 12 +++---- lib/simple_ui/elements/flex.lua | 36 ++++++++++----------- lib/simple_ui/elements/padding.lua | 8 ++--- lib/simple_ui/elements/placeholder.lua | 12 ++++--- lib/simple_ui/elements/screen_area.lua | 8 ++--- lib/simple_ui/elements/sized_box.lua | 6 ++-- 10 files changed, 66 insertions(+), 68 deletions(-) diff --git a/lib/simple_ui/core/builder.lua b/lib/simple_ui/core/builder.lua index 5ec8c6e..f777fd9 100644 --- a/lib/simple_ui/core/builder.lua +++ b/lib/simple_ui/core/builder.lua @@ -59,13 +59,13 @@ function builder:build_step(cur) child = orphan if not child then return end - child.parent = cur + child._parent_ = cur cur.child = child self:build_step(cur.child) elseif cur.children then for _, child in ipairs(cur.children) do - child.parent = cur + child._parent_ = cur self:build_step(child) end diff --git a/lib/simple_ui/core/element.lua b/lib/simple_ui/core/element.lua index 075d85e..4c96a2e 100644 --- a/lib/simple_ui/core/element.lua +++ b/lib/simple_ui/core/element.lua @@ -2,19 +2,19 @@ local Constraints = require "lib.simple_ui.core.constraints" local Vec3 = require "lib.utils.vec3" --- @class UIElement ---- @field type string --- @field key? any Must be convertible to string ---- @field parent? UIElement ---- @field constraints Constraints ---- @field offset Vec3 Положение левого верхнего угла элемента в локальных координатах {x, y}. Устанавливается родительским элементом. ---- @field size Vec3 Размеры элемента {x, y} +--- @field type string +--- @field _parent_? UIElement +--- @field _constraints_ Constraints +--- @field _offset_ Vec3 Положение левого верхнего угла элемента в локальных координатах {x, y}. Устанавливается родительским элементом. +--- @field _size_ Vec3 Размеры элемента {x, y} --- @field build? fun(self, ctx: UIElement): UIElement local element = {} element.__index = element element.type = "Element" -element.constraints = Constraints {} -element.offset = Vec3 {} -element.size = Vec3 {} +element._constraints_ = Constraints {} +element._offset_ = Vec3 {} +element._size_ = Vec3 {} --- "Constraints go down. Sizes go up. Parent sets position." --- @@ -23,11 +23,7 @@ function element:layout() end function element:update(dt) end -function element:draw() - if self.type == "SizedBox" then - print(self.offset) - end -end +function element:draw() end --- @param values {[string]: any} --- @return UIElement @@ -42,9 +38,9 @@ end --- Обход заканчивается, если visitor возвращает false, или если родители кончились. --- @param visitor fun(element: UIElement): boolean function element:traverseUp(visitor) - if not self.parent then return end - if not visitor(self.parent) then return end - return self.parent:traverseUp(visitor) + if not self._parent_ then return end + if not visitor(self._parent_) then return end + return self._parent_:traverseUp(visitor) end return element diff --git a/lib/simple_ui/core/multi_child_element.lua b/lib/simple_ui/core/multi_child_element.lua index 2bf383c..a4da47a 100644 --- a/lib/simple_ui/core/multi_child_element.lua +++ b/lib/simple_ui/core/multi_child_element.lua @@ -14,7 +14,7 @@ end function element:draw() love.graphics.push("transform") - love.graphics.translate(self.offset.x, self.offset.y) + love.graphics.translate(self._offset_.x, self._offset_.y) for _, child in ipairs(self.children) do child:draw() end @@ -22,12 +22,12 @@ function element:draw() --- @TODO: сделать дебажный метод для отрисовки границ love.graphics.setColor(1, 0, 0) - love.graphics.line(0, 0, 0 + self.size.x, 0) - love.graphics.line(0, 0, 0, self.offset.y + 0) - love.graphics.line(0 + self.size.x, self.offset.y, 0 + self.size.x, - 0 + self.size.y) - love.graphics.line(0, 0 + self.size.y, 0 + self.size.x, - 0 + self.size.y) + love.graphics.line(0, 0, 0 + self._size_.x, 0) + love.graphics.line(0, 0, 0, self._offset_.y + 0) + love.graphics.line(0 + self._size_.x, self._offset_.y, 0 + self._size_.x, + 0 + self._size_.y) + love.graphics.line(0, 0 + self._size_.y, 0 + self._size_.x, + 0 + self._size_.y) love.graphics.setColor(1, 1, 1) love.graphics.pop() diff --git a/lib/simple_ui/core/single_child_element.lua b/lib/simple_ui/core/single_child_element.lua index 88c3094..962dbb4 100644 --- a/lib/simple_ui/core/single_child_element.lua +++ b/lib/simple_ui/core/single_child_element.lua @@ -16,9 +16,9 @@ function element:layout() --- получить назад размеры --- разместить ребенка if not self.child then return end - self.child.constraints = Constraints(self.constraints) + self.child._constraints_ = Constraints(self._constraints_) self.child:layout() - self.child.offset = Vec3 {} + self.child._offset_ = Vec3 {} end function element:update(dt) @@ -27,7 +27,7 @@ end function element:draw() love.graphics.push("transform") - love.graphics.translate(self.offset.x, self.offset.y) + love.graphics.translate(self._offset_.x, self._offset_.y) if self.child then self.child:draw() end love.graphics.pop() end diff --git a/lib/simple_ui/elements/center.lua b/lib/simple_ui/elements/center.lua index ab9f6a8..3f564a5 100644 --- a/lib/simple_ui/elements/center.lua +++ b/lib/simple_ui/elements/center.lua @@ -4,18 +4,18 @@ local SingleChildElement = require "lib.simple_ui.core.single_child_element" --- @class Center : SingleChildElement local element = setmetatable({}, SingleChildElement) element.__index = element -element.__type = "Center" +element.type = "Center" function element:layout() - self.size = Vec3 { self.constraints.maxWidth, self.constraints.maxHeight } + self._size_ = Vec3 { self._constraints_.maxWidth, self._constraints_.maxHeight } if not self.child then return end - self.child.constraints = Constraints(self.constraints) + self.child._constraints_ = Constraints(self._constraints_) self.child:layout() - self.child.offset = Vec3 { - (self.size.x - self.child.size.x) / 2, - (self.size.y - self.child.size.y) / 2, + self.child._offset_ = Vec3 { + (self._size_.x - self.child._size_.x) / 2, + (self._size_.y - self.child._size_.y) / 2, } end diff --git a/lib/simple_ui/elements/flex.lua b/lib/simple_ui/elements/flex.lua index 3c63cf0..0058143 100644 --- a/lib/simple_ui/elements/flex.lua +++ b/lib/simple_ui/elements/flex.lua @@ -17,54 +17,54 @@ function element:layout() local crossAxisSize = 0 if self.direction == "horizontal" then for _, child in ipairs(self.children) do - child.constraints = Constraints { maxHeight = self.constraints.maxHeight } + child._constraints_ = Constraints { maxHeight = self._constraints_.maxHeight } child:layout() - if child.size.y > crossAxisSize then crossAxisSize = child.size.y end - mainAxisSize = mainAxisSize + child.size.x + if child._size_.y > crossAxisSize then crossAxisSize = child._size_.y end + mainAxisSize = mainAxisSize + child._size_.x end local start = 0 if self.mainAxisAlignment == "center" then - start = self.constraints.maxWidth / 2 - mainAxisSize / 2 + start = self._constraints_.maxWidth / 2 - mainAxisSize / 2 elseif self.mainAxisAlignment == "end" then - start = self.constraints.maxWidth - mainAxisSize + start = self._constraints_.maxWidth - mainAxisSize end local shift = 0 for _, child in ipairs(self.children) do - child.offset = Vec3 { self.offset.x + start + shift, self.offset.y } - shift = shift + child.size.x + child._offset_ = Vec3 { self._offset_.x + start + shift, self._offset_.y } + shift = shift + child._size_.x end if self.mainAxisSize == "max" then - self.size = Vec3 { self.constraints.maxWidth, crossAxisSize } + self._size_ = Vec3 { self._constraints_.maxWidth, crossAxisSize } else - self.size = Vec3 { mainAxisSize, crossAxisSize } + self._size_ = Vec3 { mainAxisSize, crossAxisSize } end else for _, child in ipairs(self.children) do - child.constraints = Constraints { maxWidth = self.constraints.maxWidth } + child._constraints_ = Constraints { maxWidth = self._constraints_.maxWidth } child:layout() - if child.size.x > crossAxisSize then crossAxisSize = child.size.x end - mainAxisSize = mainAxisSize + child.size.y + if child._size_.x > crossAxisSize then crossAxisSize = child._size_.x end + mainAxisSize = mainAxisSize + child._size_.y end local start = 0 if self.mainAxisAlignment == "center" then - start = self.constraints.maxHeight / 2 - mainAxisSize / 2 + start = self._constraints_.maxHeight / 2 - mainAxisSize / 2 elseif self.mainAxisAlignment == "end" then - start = self.constraints.maxHeight - mainAxisSize + start = self._constraints_.maxHeight - mainAxisSize end local shift = 0 for _, child in ipairs(self.children) do - child.offset = Vec3 { self.offset.x, self.offset.y + start + shift } - shift = shift + child.size.y + child._offset_ = Vec3 { self._offset_.x, self._offset_.y + start + shift } + shift = shift + child._size_.y end if self.mainAxisSize == "max" then - self.size = Vec3 { crossAxisSize, self.constraints.maxHeight } + self._size_ = Vec3 { crossAxisSize, self._constraints_.maxHeight } else - self.size = Vec3 { crossAxisSize, mainAxisSize } + self._size_ = Vec3 { crossAxisSize, mainAxisSize } end end end diff --git a/lib/simple_ui/elements/padding.lua b/lib/simple_ui/elements/padding.lua index 7091525..9ced4ef 100644 --- a/lib/simple_ui/elements/padding.lua +++ b/lib/simple_ui/elements/padding.lua @@ -20,16 +20,16 @@ element.bottom = 0 --- as in https://api.flutter.dev/flutter/widgets/Padding-class.html function element:layout() if not self.child then return end - local c = Constraints(self.constraints) + local c = Constraints(self._constraints_) c.maxWidth = c.maxWidth - self.left - self.right c.maxHeight = c.maxHeight - self.top - self.bottom c.maxWidth = c.maxWidth > 0 and c.maxWidth or 0 c.maxHeight = c.maxHeight > 0 and c.maxHeight or 0 - self.child.constraints = c + self.child._constraints_ = c self.child:layout() - self.size = Vec3 { self.child.size.x + self.left + self.right, self.child.size.y + self.top + self.bottom } - self.child.offset = Vec3 { self.left, self.top } + self._size_ = Vec3 { self.child._size_.x + self.left + self.right, self.child._size_.y + self.top + self.bottom } + self.child._offset_ = Vec3 { self.left, self.top } end --- @return Padding diff --git a/lib/simple_ui/elements/placeholder.lua b/lib/simple_ui/elements/placeholder.lua index 10d0a9a..2146165 100644 --- a/lib/simple_ui/elements/placeholder.lua +++ b/lib/simple_ui/elements/placeholder.lua @@ -7,17 +7,19 @@ element.__index = element element.type = "Placeholder" function element:layout() - self.size = Vec3 { self.constraints.maxWidth, self.constraints.maxHeight } + self._size_ = Vec3 { self._constraints_.maxWidth, self._constraints_.maxHeight } if not self.child then return end - self.child.constraints = Constraints(self.constraints) + self.child._constraints_ = Constraints(self._constraints_) self.child:layout() end function element:draw() - love.graphics.rectangle("line", self.offset.x, self.offset.y, self.size.x, self.size.y) - love.graphics.line(self.offset.x, self.offset.y, self.offset.x + self.size.x, self.offset.y + self.size.y) - love.graphics.line(self.offset.x, self.offset.y + self.size.y, self.offset.x + self.size.x, self.offset.y) + love.graphics.rectangle("line", self._offset_.x, self._offset_.y, self._size_.x, self._size_.y) + love.graphics.line(self._offset_.x, self._offset_.y, self._offset_.x + self._size_.x, self._offset_.y + self._size_ + .y) + love.graphics.line(self._offset_.x, self._offset_.y + self._size_.y, self._offset_.x + self._size_.x, self._offset_ + .y) end --- @return Placeholder diff --git a/lib/simple_ui/elements/screen_area.lua b/lib/simple_ui/elements/screen_area.lua index 7cb8253..bab3124 100644 --- a/lib/simple_ui/elements/screen_area.lua +++ b/lib/simple_ui/elements/screen_area.lua @@ -8,16 +8,16 @@ element.type = "ScreenArea" function element:layout() local screenW, screenH = love.graphics.getWidth(), love.graphics.getHeight() - self.constraints = Constraints { + self._constraints_ = Constraints { maxWidth = screenW, maxHeight = screenH } - self.size = Vec3 { screenW, screenH } + self._size_ = Vec3 { screenW, screenH } if not self.child then return end - self.child.constraints = Constraints(self.constraints) + self.child._constraints_ = Constraints(self._constraints_) self.child:layout() - self.child.offset = Vec3 {} + self.child._offset_ = Vec3 {} end --- @return ScreenArea diff --git a/lib/simple_ui/elements/sized_box.lua b/lib/simple_ui/elements/sized_box.lua index 0b5194e..37444be 100644 --- a/lib/simple_ui/elements/sized_box.lua +++ b/lib/simple_ui/elements/sized_box.lua @@ -9,15 +9,15 @@ element.width = 0 element.height = 0 function element:layout() - self.size = Vec3 { self.width, self.height } + self._size_ = Vec3 { self.width, self.height } if not self.child then return end - self.child.constraints = Constraints { + self.child._constraints_ = Constraints { maxWidth = self.width, maxHeight = self.height, } self.child:layout() - self.child.offset = Vec3 {} + self.child._offset_ = Vec3 {} end --- @return SizedBox