Skip to content

Commit f007a28

Browse files
authored
Merge pull request #310 from rigo128/example_data_list_matrix_basic
Add data_list_matrix_basic example
2 parents c87f133 + d98c3c2 commit f007a28

File tree

5 files changed

+267
-0
lines changed

5 files changed

+267
-0
lines changed

example/druid.gui

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,6 +2863,49 @@ nodes {
28632863
parent: "data_list_cache_with_component/button_component/root"
28642864
template_node_child: true
28652865
}
2866+
nodes {
2867+
type: TYPE_TEMPLATE
2868+
id: "data_list_matrix_basic"
2869+
parent: "data_list"
2870+
inherit_alpha: true
2871+
template: "/example/examples/data_list/basic/data_list_matrix_basic.gui"
2872+
}
2873+
nodes {
2874+
type: TYPE_BOX
2875+
id: "data_list_matrix_basic/root"
2876+
parent: "data_list_matrix_basic"
2877+
template_node_child: true
2878+
}
2879+
nodes {
2880+
type: TYPE_BOX
2881+
id: "data_list_matrix_basic/view"
2882+
parent: "data_list_matrix_basic/root"
2883+
template_node_child: true
2884+
}
2885+
nodes {
2886+
type: TYPE_BOX
2887+
id: "data_list_matrix_basic/content"
2888+
parent: "data_list_matrix_basic/view"
2889+
template_node_child: true
2890+
}
2891+
nodes {
2892+
type: TYPE_BOX
2893+
id: "data_list_matrix_basic/prefab"
2894+
parent: "data_list_matrix_basic/content"
2895+
template_node_child: true
2896+
}
2897+
nodes {
2898+
type: TYPE_BOX
2899+
id: "data_list_matrix_basic/panel"
2900+
parent: "data_list_matrix_basic/prefab"
2901+
template_node_child: true
2902+
}
2903+
nodes {
2904+
type: TYPE_TEXT
2905+
id: "data_list_matrix_basic/text"
2906+
parent: "data_list_matrix_basic/prefab"
2907+
template_node_child: true
2908+
}
28662909
nodes {
28672910
type: TYPE_BOX
28682911
texture: "druid_example/empty"
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
fonts {
2+
name: "text_bold"
3+
font: "/example/assets/fonts/text_bold.font"
4+
}
5+
textures {
6+
name: "druid_example"
7+
texture: "/example/assets/druid_example.atlas"
8+
}
9+
nodes {
10+
type: TYPE_BOX
11+
texture: "druid_example/empty"
12+
id: "root"
13+
inherit_alpha: true
14+
size_mode: SIZE_MODE_AUTO
15+
visible: false
16+
}
17+
nodes {
18+
position {
19+
y: 350.0
20+
}
21+
size {
22+
x: 400.0
23+
y: 700.0
24+
}
25+
color {
26+
x: 0.173
27+
y: 0.184
28+
z: 0.204
29+
}
30+
type: TYPE_BOX
31+
texture: "druid_example/pixel"
32+
id: "view"
33+
pivot: PIVOT_N
34+
parent: "root"
35+
inherit_alpha: true
36+
clipping_mode: CLIPPING_MODE_STENCIL
37+
}
38+
nodes {
39+
size {
40+
x: 400.0
41+
y: 700.0
42+
}
43+
type: TYPE_BOX
44+
texture: "druid_example/empty"
45+
id: "content"
46+
pivot: PIVOT_N
47+
parent: "view"
48+
inherit_alpha: true
49+
visible: false
50+
}
51+
nodes {
52+
position {
53+
y: -350.0
54+
}
55+
size {
56+
x: 100.0
57+
y: 100.0
58+
}
59+
type: TYPE_BOX
60+
id: "prefab"
61+
parent: "content"
62+
inherit_alpha: true
63+
visible: false
64+
}
65+
nodes {
66+
size {
67+
x: 90.0
68+
y: 90.0
69+
}
70+
color {
71+
x: 0.631
72+
y: 0.843
73+
z: 0.961
74+
}
75+
type: TYPE_BOX
76+
texture: "druid_example/ui_circle_32"
77+
id: "panel"
78+
parent: "prefab"
79+
inherit_alpha: true
80+
slice9 {
81+
x: 16.0
82+
y: 16.0
83+
z: 16.0
84+
w: 16.0
85+
}
86+
}
87+
nodes {
88+
size {
89+
x: 50.0
90+
y: 50.0
91+
}
92+
color {
93+
x: 0.31
94+
y: 0.318
95+
z: 0.322
96+
}
97+
type: TYPE_TEXT
98+
text: "1"
99+
font: "text_bold"
100+
id: "text"
101+
outline {
102+
x: 1.0
103+
y: 1.0
104+
z: 1.0
105+
}
106+
shadow {
107+
x: 1.0
108+
y: 1.0
109+
z: 1.0
110+
}
111+
parent: "prefab"
112+
inherit_alpha: true
113+
outline_alpha: 0.0
114+
shadow_alpha: 0.0
115+
}
116+
material: "/builtins/materials/gui.material"
117+
adjust_reference: ADJUST_REFERENCE_PARENT
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
local event = require("event.event")
2+
3+
---@class examples.data_list_matrix_basic: druid.widget
4+
---@field prefab node
5+
---@field scroll druid.scroll
6+
---@field grid druid.grid
7+
---@field data_list druid.data_list
8+
---@field on_item_click event
9+
local M = {}
10+
11+
12+
function M:init()
13+
self.prefab = self:get_node("prefab")
14+
gui.set_enabled(self.prefab, false)
15+
16+
self.scroll = self.druid:new_scroll("view", "content")
17+
self.grid = self.druid:new_grid("content", self.prefab, 4)
18+
self.data_list = self.druid:new_data_list(self.scroll, self.grid, self.create_item_callback) --[[@as druid.data_list]]
19+
20+
local data = {}
21+
for index = 1, 1000 do
22+
table.insert(data, {})
23+
end
24+
self.data_list:set_data(data)
25+
26+
self.on_item_click = event.create()
27+
end
28+
29+
30+
---@param item_data table
31+
---@param index number
32+
---@return node, druid.component
33+
function M:create_item_callback(item_data, index)
34+
local nodes = gui.clone_tree(self.prefab)
35+
36+
local root = nodes[self:get_template() .. "/prefab"]
37+
local text = nodes[self:get_template() .. "/text"]
38+
gui.set_enabled(root, true)
39+
gui.set_text(text, tostring(index))
40+
41+
local button = self.druid:new_button(root, self.on_button_click, index)
42+
return root, button
43+
end
44+
45+
46+
function M:on_button_click(index)
47+
self.on_item_click:trigger(index)
48+
end
49+
50+
51+
---@param output_list output_list
52+
function M:on_example_created(output_list)
53+
self.on_item_click:subscribe(function(index)
54+
output_list:add_log_text("Item clicked: " .. index)
55+
end)
56+
end
57+
58+
59+
---@param properties_panel properties_panel
60+
function M:properties_control(properties_panel)
61+
local view_node = self.scroll.view_node
62+
local is_stencil = gui.get_clipping_mode(view_node) == gui.CLIPPING_MODE_STENCIL
63+
64+
properties_panel:add_checkbox("ui_clipping", is_stencil, function(value)
65+
gui.set_clipping_mode(view_node, value and gui.CLIPPING_MODE_STENCIL or gui.CLIPPING_MODE_NONE)
66+
end)
67+
68+
properties_panel:add_slider("ui_scroll", 0, function(value)
69+
self.scroll:scroll_to_percent(vmath.vector3(0, 1 - value, 0), true)
70+
end)
71+
end
72+
73+
74+
---@return string
75+
function M:get_debug_info()
76+
local data_list = self.data_list
77+
78+
local data = data_list:get_data()
79+
local info = ""
80+
info = info .. "Data length: " .. #data .. "\n"
81+
info = info .. "First Visual Index: " .. data_list.top_index .. "\n"
82+
info = info .. "Last Visual Index: " .. data_list.last_index .. "\n"
83+
84+
local s = self.scroll
85+
info = info .. "\n"
86+
info = info .. "View Size Y: " .. gui.get(s.view_node, "size.y") .. "\n"
87+
info = info .. "Content Size Y: " .. gui.get(s.content_node, "size.y") .. "\n"
88+
info = info .. "Content position Y: " .. math.ceil(s.position.y) .. "\n"
89+
info = info .. "Content Range Y: " .. s.available_pos.y .. " - " .. s.available_pos.w .. "\n"
90+
91+
return info
92+
end
93+
94+
95+
return M

example/examples/data_list/examples_list.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ function M.get_examples()
2121
widget_class = require("example.examples.data_list.basic.data_list_horizontal_basic"),
2222
},
2323

24+
{
25+
name_id = "ui_example_data_list_matrix_basic",
26+
information_text_id = "ui_example_data_list_matrix_basic_description",
27+
template = "data_list_matrix_basic",
28+
root = "data_list_matrix_basic/root",
29+
code_url = "example/examples/data_list/basic/data_list_matrix_basic.lua",
30+
widget_class = require("example.examples.data_list.basic.data_list_matrix_basic"),
31+
},
32+
2433
{
2534
name_id = "ui_example_data_list_add_remove_clear",
2635
information_text_id = "ui_example_data_list_add_remove_clear_description",

example/locales/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@
147147
"ui_example_data_list_horizontal_basic": "Data List Horizontal Basic",
148148
"ui_example_data_list_horizontal_basic_description": "How to make a horizontal data list",
149149

150+
"ui_example_data_list_matrix_basic": "Data List Matrix Basic",
151+
"ui_example_data_list_matrix_basic_description": "How to make a matrix data list",
152+
150153
"ui_example_data_list_add_remove_clear": "Data List Add Remove Clear",
151154
"ui_example_data_list_add_remove_clear_description": "How the add, remove and clear functions work in the data list",
152155

0 commit comments

Comments
 (0)