Skip to content

Commit

Permalink
Progress #210 Add set_max_gui_upscale for layout component
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Dec 3, 2022
1 parent dad54d8 commit 4f7dbf4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
35 changes: 30 additions & 5 deletions druid/extended/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ function Layout.init(self, node, mode, on_size_changed_callback)

self._min_size = nil
self._max_size = nil
self._current_size = vmath.vector3(0)
self._inited = false

self._max_gui_upscale = nil
self._fit_node = nil

self.mode = mode or const.LAYOUT_MODE.FIT
Expand Down Expand Up @@ -66,6 +67,13 @@ function Layout.on_window_resized(self)

local x_koef, y_koef = helper.get_screen_aspect_koef()

local revert_scale = 1
if self._max_gui_upscale then
revert_scale = self._max_gui_upscale / helper.get_gui_scale()
revert_scale = math.min(revert_scale, 1)
end
gui.set_scale(self.node, vmath.vector3(revert_scale))

if self._fit_node then
self.fit_size = gui.get_size(self._fit_node)
self.fit_size.x = self.fit_size.x / x_koef
Expand All @@ -77,11 +85,17 @@ function Layout.on_window_resized(self)

local new_size = vmath.vector3(self.origin_size)

if self.mode == const.LAYOUT_MODE.STRETCH_X or self.mode == const.LAYOUT_MODE.STRETCH then
new_size.x = new_size.x * x_koef
if self.mode == const.LAYOUT_MODE.STRETCH then
new_size.x = new_size.x * x_koef / revert_scale
new_size.y = new_size.y * y_koef / revert_scale
end
if self.mode == const.LAYOUT_MODE.STRETCH_Y or self.mode == const.LAYOUT_MODE.STRETCH then
new_size.y = new_size.y * y_koef

if self.mode == const.LAYOUT_MODE.STRETCH_X then
new_size.x = new_size.x * x_koef / revert_scale
end

if self.mode == const.LAYOUT_MODE.STRETCH_Y then
new_size.y = new_size.y * y_koef / revert_scale
end

-- Fit to the stretched container (node size or other defined)
Expand All @@ -100,6 +114,7 @@ function Layout.on_window_resized(self)
new_size.x = math.min(new_size.x, self._max_size.x)
new_size.y = math.min(new_size.y, self._max_size.y)
end
self._current_size = new_size
gui.set_size(self.node, new_size)

self.position.x = self.origin_position.x + self.origin_position.x * (x_koef - 1)
Expand Down Expand Up @@ -152,6 +167,16 @@ function Layout.set_origin_size(self, new_origin_size)
end


--- Set max gui upscale for FIT adjust mode (or side). It happens on bigger render gui screen
-- @tparam Layout self @{Layout}
-- @tparam number max_gui_upscale
-- @treturn Layout @{Layout}
function Layout.set_max_gui_upscale(self, max_gui_upscale)
self._max_gui_upscale = max_gui_upscale
self:on_window_resized()
end


--- Set size for layout node to fit inside it
-- @tparam Layout self @{Layout}
-- @tparam vector3 target_size
Expand Down
41 changes: 34 additions & 7 deletions druid/helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ function M.get_screen_aspect_koef()
end


function M.get_gui_scale()
local window_x, window_y = window.get_size()
return math.min(window_x / gui.get_width(),
window_y / gui.get_height())
end


function M.step(current, target, step)
if current < target then
return math.min(current + step, target)
Expand Down Expand Up @@ -183,6 +190,23 @@ function M.is_enabled(node)
end


--- Return current node scene scale
-- @function helper.is_enabled
-- @tparam node node Gui node
-- @tparam bool include_passed_node_scale True if add current node scale to result
-- @treturn vector3 The scene node scale
function M.get_scene_scale(node, include_passed_node_scale)
local scale = include_passed_node_scale and gui.get_scale(node) or vmath.vector3(1)
local parent = gui.get_parent(node)
while parent do
scale = vmath.mul_per_elem(scale, gui.get_scale(parent))
parent = gui.get_parent(parent)
end

return scale
end


--- Return closest non inverted clipping parent node for node
-- @function helper.get_closest_stencil_node
-- @tparam node node Gui node
Expand Down Expand Up @@ -280,20 +304,23 @@ function M.get_border(node, offset)
end


function M.get_text_metrics_from_node(node)
local font_resource = gui.get_font_resource(gui.get_font(node))
--- Get text metric from gui node. Replacement of previous gui.get_text_metrics_from_node function
-- @tparam Node text_node
-- @treturn table {width, height, max_ascent, max_descent}
function M.get_text_metrics_from_node(text_node)
local font_resource = gui.get_font_resource(gui.get_font(text_node))
local options = {
tracking = gui.get_tracking(node),
line_break = gui.get_line_break(node),
tracking = gui.get_tracking(text_node),
line_break = gui.get_line_break(text_node),
}

-- Gather other options only if it used in node
if options.line_break then
options.width = gui.get_size(node).x
options.leading = gui.get_leading(node)
options.width = gui.get_size(text_node).x
options.leading = gui.get_leading(text_node)
end

return resource.get_text_metrics(font_resource, gui.get_text(node), options)
return resource.get_text_metrics(font_resource, gui.get_text(text_node), options)
end


Expand Down

0 comments on commit 4f7dbf4

Please sign in to comment.