Tree View example?

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
Smok
Posts: 53
Joined: June 14th, 2016, 11:52 am

Tree View example?

Post by Smok »

I realy need some kind of example, may be this https://wiki.wesnoth.org/LuaWML/Display ... how_dialog made using tree view insted of listbox. There is in practice no documentation for tree view, and im stuck with this code

Code: Select all

local helper = wesnoth.require "lua/helper.lua"
local T = helper.set_wml_tag_metatable {}
local _ = wesnoth.textdomain "wesnoth"

local dialog = {
  T.tooltip { id = "tooltip_large" },
  T.helptip { id = "tooltip_large" },
  T.grid { T.row {
    T.column { T.grid {
      T.row { T.column { horizontal_grow = true, T.tree_view { id = "the_list",
		T.tree_view_definition { T.row { T.column {
			T.node { id = "node1",
			T.node_definition { T.row { T.column { horizontal_grow = true,
			  T.toggle_panel { T.grid { T.row {
				T.column { horizontal_alignment = "left", T.label { id = "the_label" } },
				T.column { T.image { id = "the_icon" } }
			  } } }
        } } } } } } } 
      } } },
      T.row { T.column { T.grid { T.row {
        T.column { T.button { id = "ok", label = _"OK" } },
        T.column { T.button { id = "cancel", label = _"Cancel" } }
      } } } }
    } },
    T.column { T.image { id = "the_image" } }
  } }
}

local function preshow()
    local t = { "Ancient Lich", "Ancient Wose", "Elvish Avenger" }
    local function select()
        local i = wesnoth.get_dialog_value "the_list"
        local ut = wesnoth.unit_types[t[i]].__cfg
        wesnoth.set_dialog_value(string.gsub(ut.profile, "([^/]+)$", "transparent/%1"), "the_image")
    end
    wesnoth.set_dialog_callback(select, "the_list")
    for i,v in ipairs(t) do
        local ut = wesnoth.unit_types[v].__cfg
        wesnoth.set_dialog_value(ut.name, "the_list", i, "the_label")
        wesnoth.set_dialog_value(ut.image, "the_list", i, "the_icon")
    end
    wesnoth.set_dialog_value(2, "the_list")
    select()
end

local li = 0
local function postshow()
    li = wesnoth.get_dialog_value "the_list"
end

local r = wesnoth.show_dialog(dialog, preshow, postshow)
wesnoth.message(string.format("Button %d pressed. Item %d selected.", r, li))
and "Not defined tree nodes" error. I want to make dialog like settings, where is list of items on left side, and when you choose one item the right side of dialog changes.

EDIT: It seems to work without T.tree_view_definition, but i dont know how to use wesnoth.set_dialog_value(ut.name, "the_list", i, "the_label") for tree view.
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Tree View example?

Post by gfgtdf »

you cannot add new nodes to a treeview with wesnoth.set_dialog_value. To add a node to a treeview you have to use the wesnoth.add_dialog_tree_node which is only available in wesnoth 1.13

conclusion, you cannot really use treeviews in 1.12


If you work on 1.13 i might be able to give you an example code on how to use treeviews form lua.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
Smok
Posts: 53
Joined: June 14th, 2016, 11:52 am

Re: Tree View example?

Post by Smok »

I work with dev version now.
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: Tree View example?

Post by gfgtdf »

I'll try to find my old code that i used for testing when implemeting the wesnoth.add_dialog_tree_node function.

Other than that, the wesnoth.add_dialog_tree_node is defined as wesnoth.add_dialog_tree_node(type, index, path, to, widget, id) where type is the id as specified in [node], so in your code it'd be

Code: Select all

wesnoth.set_dialog_value(ut.name, "the_list", i, "the_label")
wesnoth.set_dialog_value(ut.image, "the_list", i, "the_icon")
replaced with

Code: Select all

wesnoth.add_dialog_tree_node("node1", i, the_list)
wesnoth.set_dialog_value(ut.name, "the_list", i, "the_label")
wesnoth.set_dialog_value(ut.image, "the_list", i, "the_icon")
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.
Post Reply