mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 10:27:47 +02:00
Separate text on text and locale components. Auto adjust text sizing
This commit is contained in:
parent
8f32b61ddc
commit
c3752dba54
47
druid/base/locale.lua
Normal file
47
druid/base/locale.lua
Normal file
@ -0,0 +1,47 @@
|
||||
--- Component to handle all GUI texts
|
||||
-- Good working with localization system
|
||||
-- @module base.text
|
||||
|
||||
local const = require("druid.const")
|
||||
local settings = require("druid.settings")
|
||||
local helper = require("druid.helper")
|
||||
|
||||
local M = {}
|
||||
M.interest = {
|
||||
const.ON_CHANGE_LANGUAGE,
|
||||
}
|
||||
|
||||
|
||||
function M.init(self, node, lang_id)
|
||||
self.druid = helper.get_druid(self)
|
||||
self.text = self.druid:new_text(node)
|
||||
self:translate(lang_id)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
function M.raw_set(self, text)
|
||||
self.last_locale = false
|
||||
self.text:set_to(text)
|
||||
end
|
||||
|
||||
|
||||
--- Translate the text by locale_id
|
||||
-- @function text:translate
|
||||
-- @tparam table self Component instance
|
||||
-- @tparam string locale_id Locale id
|
||||
function M.translate(self, locale_id)
|
||||
self.last_locale = locale_id or self.last_locale
|
||||
self.text:set_to(settings.get_text(self.last_locale))
|
||||
end
|
||||
|
||||
|
||||
function M.on_change_language(self)
|
||||
if self.last_locale then
|
||||
M.translate(self)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return M
|
@ -2,57 +2,44 @@
|
||||
-- Good working with localization system
|
||||
-- @module base.text
|
||||
|
||||
local const = require("druid.const")
|
||||
local settings = require("druid.settings")
|
||||
local helper = require("druid.helper")
|
||||
|
||||
local M = {}
|
||||
M.interest = {
|
||||
const.ON_CHANGE_LANGUAGE,
|
||||
}
|
||||
M.interest = {}
|
||||
|
||||
|
||||
function M.init(self, node, value, is_locale, max_width)
|
||||
self.max_width = max_width
|
||||
function M.init(self, node, value, no_text_adjust)
|
||||
self.node = helper.node(node)
|
||||
self.start_scale = gui.get_scale(self.node)
|
||||
self.start_pivot = gui.get_pivot(self.node)
|
||||
|
||||
self.text_area = gui.get_size(self.node)
|
||||
self.text_area.x = self.text_area.x * self.start_scale.x
|
||||
self.text_area.y = self.text_area.y * self.start_scale.y
|
||||
|
||||
self.is_no_text_adjust = no_text_adjust
|
||||
self.scale = self.start_scale
|
||||
self.last_color = gui.get_color(self.node)
|
||||
|
||||
if is_locale then
|
||||
self:translate(value)
|
||||
else
|
||||
self:set_to(value or 0)
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- Translate the text by locale_id
|
||||
-- @function text:translate
|
||||
-- @tparam table self Component instance
|
||||
-- @tparam string locale_id Locale id
|
||||
function M.translate(self, locale_id)
|
||||
self.last_locale = locale_id or self.last_locale
|
||||
self:set_to(settings.get_text(self.last_locale))
|
||||
end
|
||||
|
||||
|
||||
function M.on_change_language(self)
|
||||
if self.last_locale then
|
||||
M.translate(self)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--- Setup scale x, but can only be smaller, than start text scale
|
||||
local function setup_max_width(self)
|
||||
local function update_text_area_size(self)
|
||||
local max_width = self.text_area.x
|
||||
local max_height = self.text_area.y
|
||||
|
||||
local metrics = gui.get_text_metrics_from_node(self.node)
|
||||
local cur_scale = gui.get_scale(self.node)
|
||||
|
||||
local scale_modifier = self.max_width / metrics.width
|
||||
local scale_modifier = max_width / metrics.width
|
||||
scale_modifier = math.min(scale_modifier, self.start_scale.x)
|
||||
|
||||
local scale_modifier_height = max_height / metrics.height
|
||||
scale_modifier = math.min(scale_modifier, scale_modifier_height)
|
||||
|
||||
local new_scale = vmath.vector3(scale_modifier, scale_modifier, cur_scale.z)
|
||||
gui.set_scale(self.node, new_scale)
|
||||
self.scale = new_scale
|
||||
@ -67,8 +54,8 @@ function M.set_to(self, set_to)
|
||||
self.last_value = set_to
|
||||
gui.set_text(self.node, set_to)
|
||||
|
||||
if self.max_width then
|
||||
setup_max_width(self)
|
||||
if not self.is_no_text_adjust then
|
||||
update_text_area_size(self)
|
||||
end
|
||||
end
|
||||
|
||||
@ -103,4 +90,9 @@ function M.set_scale(self, scale)
|
||||
end
|
||||
|
||||
|
||||
function M.set_pivot(self, pivot)
|
||||
|
||||
end
|
||||
|
||||
|
||||
return M
|
||||
|
@ -24,6 +24,7 @@ M.comps = {
|
||||
blocker = require("druid.base.blocker"),
|
||||
back_handler = require("druid.base.back_handler"),
|
||||
text = require("druid.base.text"),
|
||||
locale = require("druid.base.locale"),
|
||||
timer = require("druid.base.timer"),
|
||||
progress = require("druid.base.progress"),
|
||||
grid = require("druid.base.grid"),
|
||||
|
Loading…
x
Reference in New Issue
Block a user