mirror of
https://github.com/Insality/druid.git
synced 2025-06-27 10:27:47 +02:00
Merge pull request #34 from AGulev/feature/druid-text-upgrade
Feature/druid text upgrade
This commit is contained in:
commit
44c7c0f52c
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, no_adjust)
|
||||
self.druid = helper.get_druid(self)
|
||||
self.text = self.druid:new_text(node, lang_id, no_adjust)
|
||||
self:translate(lang_id)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
function M.set_to(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
|
@ -3,56 +3,47 @@
|
||||
-- @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, value, is_locale, max_width)
|
||||
self.max_width = max_width
|
||||
function M.init(self, node, value, no_adjust)
|
||||
self.node = helper.node(node)
|
||||
self.start_pivot = gui.get_pivot(self.node)
|
||||
|
||||
self.start_pos = gui.get_position(self.node)
|
||||
self.pos = gui.get_position(self.node)
|
||||
|
||||
self.start_scale = gui.get_scale(self.node)
|
||||
self.scale = self.start_scale
|
||||
self.scale = gui.get_scale(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_adjust = no_adjust
|
||||
self.last_color = gui.get_color(self.node)
|
||||
|
||||
if is_locale then
|
||||
self:translate(value)
|
||||
else
|
||||
self:set_to(value or 0)
|
||||
end
|
||||
|
||||
self:set_to(value or 0)
|
||||
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 +58,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_adjust then
|
||||
update_text_area_size(self)
|
||||
end
|
||||
end
|
||||
|
||||
@ -103,4 +94,27 @@ function M.set_scale(self, scale)
|
||||
end
|
||||
|
||||
|
||||
--- Set text pivot. Text will re-anchor inside
|
||||
-- his text area
|
||||
-- @function text:set_pivot
|
||||
-- @tparam table self Component instance
|
||||
-- @tparam gui.pivot pivot Gui pivot constant
|
||||
function M.set_pivot(self, pivot)
|
||||
local prev_pivot = gui.get_pivot(self.node)
|
||||
local prev_offset = const.PIVOTS[prev_pivot]
|
||||
|
||||
gui.set_pivot(self.node, pivot)
|
||||
local cur_offset = const.PIVOTS[pivot]
|
||||
|
||||
local pos_offset = vmath.vector3(
|
||||
self.text_area.x * (cur_offset.x - prev_offset.x),
|
||||
self.text_area.y * (cur_offset.y - prev_offset.y),
|
||||
0
|
||||
)
|
||||
|
||||
self.pos = self.pos + pos_offset
|
||||
gui.set_position(self.node, self.pos)
|
||||
end
|
||||
|
||||
|
||||
return M
|
||||
|
@ -32,7 +32,7 @@ M.PIVOTS = {
|
||||
[gui.PIVOT_S] = vmath.vector3(0, -0.5, 0),
|
||||
[gui.PIVOT_SW] = vmath.vector3(-0.5, -0.5, 0),
|
||||
[gui.PIVOT_W] = vmath.vector3(-0.5, 0, 0),
|
||||
[gui.PIVOT_NW] = vmath.vector3(-0.5, -0.5, 0),
|
||||
[gui.PIVOT_NW] = vmath.vector3(-0.5, 0.5, 0),
|
||||
}
|
||||
|
||||
M.SIDE = {
|
||||
|
@ -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"),
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,27 +1,46 @@
|
||||
local druid = require("druid.druid")
|
||||
local bounce_style = require("druid.styles.bounce.style")
|
||||
|
||||
local empty_style = require("druid.styles.empty.style")
|
||||
local bounce_style = require("druid.styles.bounce.style")
|
||||
|
||||
local main_page = require("example.kenney.page.main")
|
||||
local text_page = require("example.kenney.page.texts")
|
||||
|
||||
local function on_control_button(self, side)
|
||||
print("Click on button side", side)
|
||||
local pages = {
|
||||
"main_page",
|
||||
"texts_page"
|
||||
}
|
||||
|
||||
local function on_control_button(self, delta)
|
||||
self.page = self.page + delta
|
||||
if self.page < 1 then
|
||||
self.page = #pages
|
||||
end
|
||||
if self.page > #pages then
|
||||
self.page = 1
|
||||
end
|
||||
|
||||
self.header:translate(pages[self.page])
|
||||
local node = gui.get_node("C_Anchor")
|
||||
gui.animate(node, "position.x", (self.page-1) * -600, gui.EASING_OUTSINE, 0.2)
|
||||
end
|
||||
|
||||
|
||||
local function init_top_panel(self)
|
||||
self.druid:new_button("button_left/button", on_control_button, "left")
|
||||
self.druid:new_button("button_right/button", on_control_button, "right")
|
||||
self.header = self.druid:new_text("text_header", "main_page", true)
|
||||
self.druid:new_button("button_left/button", on_control_button, -1)
|
||||
self.druid:new_button("button_right/button", on_control_button, 1)
|
||||
self.header = self.druid:new_locale("text_header", "main_page")
|
||||
end
|
||||
|
||||
|
||||
function init(self)
|
||||
druid.set_default_style(empty_style)
|
||||
druid.set_default_style(bounce_style)
|
||||
self.druid = druid.new(self)
|
||||
|
||||
init_top_panel(self)
|
||||
self.page = 1
|
||||
main_page.setup_page(self)
|
||||
text_page.setup_page(self)
|
||||
end
|
||||
|
||||
|
||||
|
@ -4,6 +4,7 @@ local M = {}
|
||||
|
||||
local en = {
|
||||
main_page = "Main page",
|
||||
texts_page = "Text page",
|
||||
ui_section_button = "Button",
|
||||
ui_section_text = "Text",
|
||||
ui_section_timer = "Timer",
|
||||
@ -17,6 +18,7 @@ local en = {
|
||||
|
||||
local ru = {
|
||||
main_page = "Основное",
|
||||
texts_page = "Текст",
|
||||
ui_section_button = "Кнопка",
|
||||
ui_section_text = "Текст",
|
||||
ui_section_timer = "Таймер",
|
||||
|
@ -26,17 +26,17 @@ end
|
||||
|
||||
|
||||
local function setup_texts(self)
|
||||
self.druid:new_text("text_button", "ui_section_button", true)
|
||||
self.druid:new_text("text_text", "ui_section_text", true)
|
||||
self.druid:new_text("text_timer", "ui_section_timer", true)
|
||||
self.druid:new_text("text_progress", "ui_section_progress", true)
|
||||
self.druid:new_text("text_slider", "ui_section_slider", true)
|
||||
self.druid:new_text("text_radio", "ui_section_radio", true)
|
||||
self.druid:new_text("text_checkbox", "ui_section_checkbox", true)
|
||||
self.druid:new_locale("text_button", "ui_section_button")
|
||||
self.druid:new_locale("text_text", "ui_section_text")
|
||||
self.druid:new_locale("text_timer", "ui_section_timer")
|
||||
self.druid:new_locale("text_progress", "ui_section_progress")
|
||||
self.druid:new_locale("text_slider", "ui_section_slider")
|
||||
self.druid:new_locale("text_radio", "ui_section_radio")
|
||||
self.druid:new_locale("text_checkbox", "ui_section_checkbox")
|
||||
|
||||
self.druid:new_locale("text_translated", "ui_text_example")
|
||||
self.druid:new_locale("text_button_lang", "ui_text_change_lang")
|
||||
self.druid:new_text("text_simple", "Simple")
|
||||
self.druid:new_text("text_translated", "ui_text_example", true)
|
||||
self.druid:new_text("text_button_lang", "ui_text_change_lang", true, 150 * 0.7)
|
||||
end
|
||||
|
||||
|
||||
|
55
example/kenney/page/texts.lua
Normal file
55
example/kenney/page/texts.lua
Normal file
@ -0,0 +1,55 @@
|
||||
local lang = require("example.kenney.lang")
|
||||
|
||||
local M = {}
|
||||
|
||||
local pivots = {
|
||||
gui.PIVOT_CENTER,
|
||||
gui.PIVOT_N,
|
||||
gui.PIVOT_NE,
|
||||
gui.PIVOT_E,
|
||||
gui.PIVOT_SE,
|
||||
gui.PIVOT_S,
|
||||
gui.PIVOT_SW,
|
||||
gui.PIVOT_W,
|
||||
gui.PIVOT_NW
|
||||
}
|
||||
|
||||
local function setup_texts(self)
|
||||
self.druid:new_text("text_inline", "Simple inline text")
|
||||
self.druid:new_text("text_multiline", "Simple multiline text with smth")
|
||||
local anchoring = self.druid:new_text("text_anchoring", "Anchoring")
|
||||
self.druid:new_text("text_no_adjust", "Without adjust size", true)
|
||||
self.druid:new_locale("text_locale", "ui_text_example")
|
||||
|
||||
local big_text = "Check max size"
|
||||
local width = self.druid:new_text("text_max_width", big_text)
|
||||
local height = self.druid:new_text("text_max_height", big_text)
|
||||
|
||||
local pivot_index = 1
|
||||
timer.delay(0.3, true, function()
|
||||
anchoring:set_pivot(pivots[pivot_index])
|
||||
|
||||
pivot_index = pivot_index + 1
|
||||
if pivot_index > #pivots then
|
||||
pivot_index = 1
|
||||
end
|
||||
end)
|
||||
|
||||
timer.delay(0.2, true, function()
|
||||
big_text = big_text .. " max"
|
||||
width:set_to(big_text)
|
||||
height:set_to(big_text)
|
||||
|
||||
if #big_text > 50 then
|
||||
big_text = "Check max size"
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
function M.setup_page(self)
|
||||
setup_texts(self)
|
||||
end
|
||||
|
||||
|
||||
return M
|
Loading…
x
Reference in New Issue
Block a user