More widgets, additional adjusts for text

This commit is contained in:
Insality
2024-11-20 01:42:12 +02:00
parent c35dfc7066
commit 37d7168bd6
15 changed files with 698 additions and 72 deletions

View File

@@ -1,6 +1,6 @@
local color = require("druid.color")
---@class property_button: druid.widget
---@class widget.property_button: druid.widget
---@field root node
---@field container druid.container
---@field text_name druid.text
@@ -12,6 +12,8 @@ local M = {}
function M:init()
self.root = self:get_node("root")
self.text_name = self.druid:new_text("text_name")
:set_text_adjust("trim_left")
self.selected = self:get_node("selected")
gui.set_alpha(self.selected, 0)
@@ -33,7 +35,7 @@ end
---@param text string
---@return property_button
---@return widget.property_button
function M:set_text_button(text)
self.text_button:set_text(text)
return self

View File

@@ -1,4 +1,4 @@
---@class property_checkbox: druid.widget
---@class widget.property_checkbox: druid.widget
---@field root node
---@field druid druid_instance
---@field text_name druid.text

View File

@@ -36,7 +36,7 @@ nodes {
z: 0.49
}
type: TYPE_TEXT
text: "Button"
text: "Input"
font: "text_bold"
id: "text_name"
pivot: PIVOT_W

View File

@@ -1,4 +1,4 @@
---@class property_input: druid.widget
---@class widget.property_input: druid.widget
---@field root node
---@field container druid.container
---@field text_name druid.text

View File

@@ -0,0 +1,212 @@
fonts {
name: "text_bold"
font: "/druid/fonts/text_bold.font"
}
textures {
name: "druid"
texture: "/druid/druid.atlas"
}
nodes {
size {
x: 400.0
y: 40.0
}
type: TYPE_BOX
texture: "druid/empty"
id: "root"
adjust_mode: ADJUST_MODE_STRETCH
inherit_alpha: true
visible: false
}
nodes {
position {
x: -200.0
}
scale {
x: 0.5
y: 0.5
}
size {
x: 360.0
y: 40.0
}
color {
x: 0.463
y: 0.475
z: 0.49
}
type: TYPE_TEXT
text: "Left Right Selector"
font: "text_bold"
id: "text_name"
pivot: PIVOT_W
outline {
x: 1.0
y: 1.0
z: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
}
parent: "root"
inherit_alpha: true
outline_alpha: 0.0
shadow_alpha: 0.0
}
nodes {
position {
x: 200.0
}
size {
x: 200.0
y: 40.0
}
type: TYPE_BOX
id: "E_Anchor"
pivot: PIVOT_E
parent: "root"
inherit_alpha: true
size_mode: SIZE_MODE_AUTO
visible: false
}
nodes {
position {
x: -180.0
}
size {
x: 40.0
y: 40.0
}
color {
x: 0.463
y: 0.475
z: 0.49
}
type: TYPE_BOX
texture: "druid/rect_round2_width2"
id: "button_left"
parent: "E_Anchor"
inherit_alpha: true
slice9 {
x: 5.0
y: 5.0
z: 5.0
w: 5.0
}
}
nodes {
rotation {
z: 180.0
}
color {
x: 0.722
y: 0.741
z: 0.761
}
type: TYPE_BOX
texture: "druid/icon_arrow"
id: "icon_left"
parent: "button_left"
inherit_alpha: true
size_mode: SIZE_MODE_AUTO
}
nodes {
position {
x: -20.0
}
size {
x: 40.0
y: 40.0
}
color {
x: 0.463
y: 0.475
z: 0.49
}
type: TYPE_BOX
texture: "druid/rect_round2_width2"
id: "button_right"
parent: "E_Anchor"
inherit_alpha: true
slice9 {
x: 5.0
y: 5.0
z: 5.0
w: 5.0
}
}
nodes {
color {
x: 0.722
y: 0.741
z: 0.761
}
type: TYPE_BOX
texture: "druid/icon_arrow"
id: "icon_right"
parent: "button_right"
inherit_alpha: true
size_mode: SIZE_MODE_AUTO
}
nodes {
position {
x: -100.0
y: -20.0
}
size {
x: 120.0
y: 4.0
}
color {
x: 0.894
y: 0.506
z: 0.333
}
type: TYPE_BOX
texture: "druid/pixel"
id: "selected"
pivot: PIVOT_S
adjust_mode: ADJUST_MODE_STRETCH
parent: "E_Anchor"
inherit_alpha: true
}
nodes {
position {
x: -100.0
}
scale {
x: 0.5
y: 0.5
}
size {
x: 220.0
y: 40.0
}
color {
x: 0.463
y: 0.475
z: 0.49
}
type: TYPE_TEXT
text: "42"
font: "text_bold"
id: "text_value"
outline {
x: 1.0
y: 1.0
z: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
}
parent: "E_Anchor"
inherit_alpha: true
outline_alpha: 0.0
shadow_alpha: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT

View File

@@ -0,0 +1,204 @@
local event = require("druid.event")
---@class widget.property_left_right_selector: druid.widget
---@field root node
---@field druid druid_instance
---@field text_name druid.text
---@field button druid.button
---@field selected node
---@field value string|number
local M = {}
function M:init()
self.root = self:get_node("root")
self.selected = self:get_node("selected")
gui.set_alpha(self.selected, 0)
self.text_name = self.druid:new_text("text_name")
self.text_value = self.druid:new_text("text_value")
self.button_left = self.druid:new_button("button_left", self.on_button_left)
self.button_right = self.druid:new_button("button_right", self.on_button_right)
self.on_change_value = event.create()
self.container = self.druid:new_container(self.root)
self.container:add_container("text_name")
self.container:add_container("E_Anchor")
end
function M:set_text(text)
self.text_name:set_text(text)
return self
end
---Helper to cycle number in range
---@param value number Current value
---@param min number Min range value
---@param max number Max range value
---@param step number Step size
---@param is_loop boolean Is looped
---@return number Cycled value
local function step_number(value, min, max, step, is_loop)
local range = max - min + 1
if is_loop then
-- Normalize step within range
local effective_step = step
if math.abs(step) >= range then
effective_step = step % range
if effective_step == 0 then
effective_step = step > 0 and range or -range
end
end
value = value + effective_step
-- Handle wrapping
if max then
while value > max do
value = min + (value - max - 1)
end
end
if min then
while value < min do
value = max - (min - value - 1)
end
end
else
-- Clamp values
value = value + step
if max and value > max then
return max
elseif min and value < min then
return min
end
end
return value
end
---Helper to cycle array index with proper step wrapping
---@param array table Array to cycle through
---@param current_value any Current value to find index for
---@param step number Step direction
---@param is_loop boolean If true, cycle values. If false, clamp at ends
---@return any Next value in cycle
local function step_array(array, current_value, step, is_loop)
local index = 1
for i, v in ipairs(array) do
if v == current_value then
index = i
break
end
end
if is_loop then
-- Normalize step within array length
local range = #array
local effective_step = step
if math.abs(step) >= range then
effective_step = step % range
if effective_step == 0 then
effective_step = step > 0 and range or -range
end
end
index = index + effective_step
-- Handle wrapping
while index > range do
index = 1 + (index - range - 1)
end
while index < 1 do
index = range - (1 - index - 1)
end
else
-- Clamp values
index = index + step
if index > #array then
index = #array
elseif index < 1 then
index = 1
end
end
return array[index]
end
function M:on_button_left()
self:add_value(true)
end
function M:on_button_right()
self:add_value(false)
end
function M:add_value(is_left)
local koef = is_left and -1 or 1
local array_type = self.array_type
if array_type then
local value = self.value
local new_value = step_array(array_type.array, value, koef * array_type.steps, array_type.is_loop)
self:set_value(new_value)
return
end
local number_type = self.number_type
if number_type then
local value = tonumber(self.value) --[[@as number]]
local new_value = step_number(value, number_type.min, number_type.max, koef * number_type.steps, number_type.is_loop)
self:set_value(new_value)
return
end
end
function M:set_number_type(min, max, is_loop, steps)
self.number_type = {
min = min,
max = max,
steps = steps or 1,
is_loop = is_loop,
}
return self
end
function M:set_array_type(array, is_loop, steps)
self.array_type = {
array = array,
steps = steps or 1,
is_loop = is_loop,
}
return self
end
---@param value string|number
function M:set_value(value, is_instant)
if self.value == value then
return
end
self.value = value
self.text_value:set_text(tostring(value))
self.on_change_value:trigger(value)
if not is_instant then
gui.set_alpha(self.selected, 1)
gui.animate(self.selected, "color.w", 0, gui.EASING_INSINE, 0.16)
end
end
---@return string|number
function M:get_value()
return self.value
end
return M

View File

@@ -1,4 +1,4 @@
---@class property_slider: druid.widget
---@class widget.property_slider: druid.widget
---@field root node
---@field container druid.container
---@field druid druid_instance

View File

@@ -1,4 +1,4 @@
---@class property_text: druid.widget
---@class widget.property_text: druid.widget
---@field root node
---@field container druid.container
---@field text_name druid.text
@@ -8,6 +8,7 @@ local M = {}
function M:init()
self.root = self:get_node("root")
self.text_name = self.druid:new_text("text_name")
:set_text_adjust("scale_when_trim_left", 0.3)
self.text_right = self.druid:new_text("text_right", "")
self.container = self.druid:new_container(self.root)
@@ -21,15 +22,15 @@ end
---@param text string
---@return property_text
---@return widget.property_text
function M:set_text(text)
self.text_name:set_text(text)
return self
end
---@param text string
---@return property_text
---@param text string|nil
---@return widget.property_text
function M:set_right_text(text)
self.text_right:set_text(text or "")
return self