options with boxes

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

Moderator: Forum Moderators

User avatar
hermestrismi
Posts: 626
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

options with boxes

Post by hermestrismi »

I used this code

Code: Select all

--! #textdomain "wesnoth-test"
--

-- local _ = wesnoth.textdomain "wesnoth-test"
function showOptionsDialog()
	local options = {
		{ message = "Scroll to Unit", image = "units/deer.png", callback = function()
			-- Code to scroll to a unit and show its abilities
			local unit = wesnoth.get_units({ side = sideNumber, controller = "human" }) -- Replace 1 with a unit filter later
			if unit then
				wesnoth.scroll_to_tile(unit.x, unit.y)
				wesnoth.show_unit_description(unit)
			end
		end },
		{ message = "List Units", image = "units/deer.png", callback = function()
			-- Code to list all units of a chosen side with their HP and movements
			local sideNumber = wesnoth.get_user_choice("Choose a side", "Choose Side", wesnoth.sides)
			if sideNumber then
				local units = wesnoth.get_units({ side = sideNumber, controller = "human" })
				local unitList = ""
				for i, unit in ipairs(units) do
					unitList = unitList .. "Unit " .. i .. ": " .. unit.hitpoints .. " HP, " .. unit.moves .. " moves\n"
				end
				wesnoth.message("Unit List", unitList)
			end
		end },
		-- Add more options later
	}
	
	local dialog = wesnoth.show_dialog()
	dialog:add_message("Choose an option:")
	for i, option in ipairs(options) do
		local button = dialog:add_button(option.message, option.callback)
		button.tooltip = option.message
		button.image = option.image
		button:resize(200, 100) -- Adjust the size 
		button:set_alignment("right") -- Align the buttons from right to left
		if i < #options then
			dialog:add_spacing(9) -- Add space between each option
		end
	end
	dialog:show()
end
then, I inserted it into a menu:

Code: Select all

[event]
name=prestart
[set_menu_item]
id=choix_army22
description=_"Choisir une action pour l'arme stocke"
[command]
[lua]
code=<< showOptionsDialog() >>
[/lua]
[/command]
[/set_menu_item]
[/event]
but, it kept raising errors for tooltip id and empty fields.
can someone fix it or direct me to my errors and how to fix it?
User avatar
Celtic_Minstrel
Developer
Posts: 2233
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: options with boxes

Post by Celtic_Minstrel »

Um… your code is completely nonsensical. There's no way to "fix" it, it needs to be rewritten from scratch. I suggest you start by reading the documentation and looking at examples in built-in campaigns and core Wesnoth code.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
hermestrismi
Posts: 626
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: options with boxes

Post by hermestrismi »

Celtic_Minstrel wrote: December 17th, 2023, 8:41 pm Um… your code is completely nonsensical. There's no way to "fix" it, it needs to be rewritten from scratch. I suggest you start by reading the documentation and looking at examples in built-in campaigns and core Wesnoth code.
to that point? 😆
well, I am not surprised. I need more practice
User avatar
Celtic_Minstrel
Developer
Posts: 2233
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: options with boxes

Post by Celtic_Minstrel »

Well, let me point out just a few of the things that are nonsensical…
  • Your "options" variable looks like a list of options for a [message] tag, but then you want to use that with wesnoth.show_dialog somehow.
  • Even though I say that, you can't use a Lua callback in a WML tag.
  • You couldn't've looked at the documentation for wesnoth.show_dialog. Calling it with no arguments is never correct.
  • Pretty much every line of your loop has absolutely no support in the documentation. None of those functions you're calling exist. Where did you even get the idea that they would exist?
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
hermestrismi
Posts: 626
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: options with boxes

Post by hermestrismi »

but, instead of show_dialog, what function can be used to control the display of an option inside the menu item if possible? and how can be called properly?
User avatar
hermestrismi
Posts: 626
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: options with boxes

Post by hermestrismi »

Celtic_Minstrel wrote: December 17th, 2023, 8:58 pm Well, let me point out just a few of the things that are nonsensical…
  • Your "options" variable looks like a list of options for a [message] tag, but then you want to use that with wesnoth.show_dialog somehow.
  • Even though I say that, you can't use a Lua callback in a WML tag.
  • You couldn't've looked at the documentation for wesnoth.show_dialog. Calling it with no arguments is never correct.
  • Pretty much every line of your loop has absolutely no support in the documentation. None of those functions you're calling exist. Where did you even get the idea that they would exist?
I understand now what the italic letters in the documentation. they are keys to the function and not pre-determined values
User avatar
Celtic_Minstrel
Developer
Posts: 2233
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: options with boxes

Post by Celtic_Minstrel »

hermestrismi wrote: December 17th, 2023, 9:07 pm but, instead of show_dialog, what function can be used to control the display of an option inside the menu item if possible? and how can be called properly?
I'm not even sure what you're asking for, so it's difficult to answer this.
hermestrismi wrote: December 17th, 2023, 9:10 pm I understand now what the italic letters in the documentation. they are keys to the function and not pre-determined values
The main page of the documentation has a section explaining the typographical conventions used. Did you not read that section?
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Celtic_Minstrel
Developer
Posts: 2233
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: options with boxes

Post by Celtic_Minstrel »

hermestrismi wrote: December 17th, 2023, 9:07 pm but, instead of show_dialog, what function can be used to control the display of an option inside the menu item if possible? and how can be called properly?
I guess I'll be nice and point you to the Lua function that underlies the [message] tag: show_narration. I'm not really sure if that's what you're looking for, but perhaps you can read it and figure something out.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
hermestrismi
Posts: 626
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: options with boxes

Post by hermestrismi »

Celtic_Minstrel wrote: December 17th, 2023, 9:24 pm
hermestrismi wrote: December 17th, 2023, 9:07 pm but, instead of show_dialog, what function can be used to control the display of an option inside the menu item if possible? and how can be called properly?
I'm not even sure what you're asking for, so it's difficult to answer this.
hermestrismi wrote: December 17th, 2023, 9:10 pm I understand now what the italic letters in the documentation. they are keys to the function and not pre-determined values
The main page of the documentation has a section explaining the typographical conventions used. Did you not read that section?
for one, I mean a function to add some attributes like style to an option within an item menu.
for two, unfortunately not before now. I jumped directly to the functions page. that was a mistake of course
User avatar
hermestrismi
Posts: 626
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: options with boxes

Post by hermestrismi »

the purpose of the code is to show the options not as bars/lines but as boxes with a specific distance between them
User avatar
Celtic_Minstrel
Developer
Posts: 2233
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: options with boxes

Post by Celtic_Minstrel »

hermestrismi wrote: December 17th, 2023, 9:55 pm the purpose of the code is to show the options not as bars/lines but as boxes with a specific distance between them
I don't know what that means exactly, but it's probably not possible unless you want to remake your option selection dialog from scratch. In that case, show_dialog is indeed the correct function to use, though the fact remains that the way you tried to use it is nowhere close to how it is supposed to be used.
Last edited by Celtic_Minstrel on December 17th, 2023, 11:32 pm, edited 1 time in total.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
hermestrismi
Posts: 626
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: options with boxes

Post by hermestrismi »

Celtic_Minstrel wrote: December 17th, 2023, 10:23 pm
hermestrismi wrote: December 17th, 2023, 9:55 pm the purpose of the code is to show the options not as bars/lines but as boxes with a specific distance between them
I don't know what that means exactly, but it's probably not possible unless you want to remake your option selection dialog from scratch. In that case, show_dialog is indeed the correct function to use, though the fact remains that the way you tried to use it is nowhere close to have it is supposed to be used.
thank you for the explanation. I will try again
User avatar
hermestrismi
Posts: 626
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: options with boxes

Post by hermestrismi »

well, after many tries, I succed to found a beginning step. now, I want to know how to call a macro wml by a Lua function properly?
I mean, the part of selected choices exist already as wml macros. should I built them all on lua? if not, what is the best function to call a macro in a Lua file?

Code: Select all

# on _main.cfg add this:
[lua]
    code="wesnoth.require '~add-ons/test_fresh/lua/tester.lua'"
[/lua]

# on lua/tester.lua add this:
local T = wml.tag
local wml_actions = wesnoth.wml_actions
local _ = wesnoth.textdomain "wesnoth-test_fresh"

function wml_actions.select_menu()
	local menu_selection_dialog = wml.load '~/add-ons/test_fresh/gui/menu_selection.cfg'
	local dialog_wml = wml.get_child(menu_selection_dialog, 'resolution')

	local result = wesnoth.sync.evaluate_single(function()
		return { value = gui.show_dialog(dialog_wml) }
	end)
	local selected_topic = result.value
	-- if selected_topic == 1 then
	-- add any action you want after selection of button
	-- else
	-- end
	-- if selected_topic == 2 then
	-- add any action you want after selection of button
	-- else
	-- end 
	-- and so on
	wesnoth.redraw {}
end
# on gui/menu_selection.cfg add this:
[resolution]
    [helptip]
        id="tooltip_large"
    [/helptip]
    [tooltip]
        id="tooltip_large"
    [/tooltip]
    [grid]
        [row]
            [column]
                grow_factor = 1
                border = "all"
                border_size = 5
                horizontal_alignment = "center"
                [label]
                    definition = "title"
                    label = _"What menu do you want to discover?"
                [/label]
            [/column]
        [/row]
        [row]
            [column]
                grow_factor = 1
                border = "all"
                border_size = 5
                horizontal_alignment = "left"
                [label]
                    label = _"Select menu"
                [/label]
            [/column]
        [/row]
        [row]
            [column]
                [grid]
                    [row]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [image]
                                label = "themes/villages.png"
                            [/image]
                        [/column]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [image]
                                label = "themes/villages.png"
                            [/image]
                        [/column]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [image]
                                label = "themes/villages.png"
                            [/image]
                        [/column]
                    [/row]
                    [row]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [button]
                                label = _"Music Playlist"
                                return_value = 1
                            [/button]
                        [/column]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [button]
                                label = _"items"
                                return_value = 2
                            [/button]
                        [/column]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [button]
                                label = _"Secret Lore"
                                return_value = 3
                            [/button]
                        [/column]
                    [/row]
                [/grid]
            [/column]
        [/row]
        [row]
            [column]
                [grid]
                    [row]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [image]
                                label = "themes/villages.png"
                            [/image]
                        [/column]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [image]
                                label = "themes/villages.png"
                            [/image]
                        [/column]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [image]
                                label = "themes/villages.png"
                            [/image]
                        [/column]
                    [/row]
                    [row]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [button]
                                label = _"Library"
                                return_value = 4
                            [/button]
                        [/column]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [button]
                                label = _"Walkthrough"
                                return_value = 5
                            [/button]
                        [/column]
                        [column]
                            grow_factor = 1
                            border = "all"
                            border_size = 5
                            [button]
                                label = _"Leave"
                                return_value = 6
                            [/button]
                        [/column]
                    [/row]
                [/grid]
            [/column]
        [/row]
    [/grid]
[/resolution]

# on x(scenario name).cfg add this:
    [event]
        name=prestart
		[set_menu_item]
			id=GeneralMenu
			description= _ "General Menu"
			image="themes/villages.png"
			[command]
				[select_menu]
				[/select_menu]
			[/command]
		[/set_menu_item]
	[/event]

User avatar
Celtic_Minstrel
Developer
Posts: 2233
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: options with boxes

Post by Celtic_Minstrel »

If you want to use WML macro in a Lua script, the general approach is to use [args]. Note that this will not work for macros that use unbalanced tags, however. It'll look something like this:

_main.cfg:

Code: Select all

[lua]
    code="wesnoth.dofile('~add-ons/test_fresh/lua/tester.lua', ...)"
    [args]
        {OPTIONS} # A macro that expands to some [option] tags
    [/args]
[/lua]
tester.lua:

Code: Select all

local all_args = ...
local options = wml.child_array(all_args, 'option')
-- and now you can do stuff with the options
However, if I'm reading your code correctly, you probably want the options to be included into the dialog WML, right? That's assuming they're fixed and don't change, but if that's what you want, you can just include them like a normal macro, since it's a WML file. However, you also have to include the file that defines the macro. So it might look something like this (extremely simplified):

Code: Select all

{./macros/options.cfg} # define the macro

{OPTIONS} # use the macro
If you're using that approach you probably have to rewrite the options as GUI2 WML instead of DescriptionWML.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
hermestrismi
Posts: 626
Joined: February 6th, 2016, 11:28 pm
Location: Tunisia
Contact:

Re: options with boxes

Post by hermestrismi »

Celtic_Minstrel wrote: December 24th, 2023, 6:11 pm If you want to use WML macro in a Lua script, the general approach is to use [args]. Note that this will not work for macros that use unbalanced tags, however. It'll look something like this:

_main.cfg:

Code: Select all

[lua]
    code="wesnoth.dofile('~add-ons/test_fresh/lua/tester.lua', ...)"
    [args]
        {OPTIONS} # A macro that expands to some [option] tags
    [/args]
[/lua]
tester.lua:

Code: Select all

local all_args = ...
local options = wml.child_array(all_args, 'option')
-- and now you can do stuff with the options
However, if I'm reading your code correctly, you probably want the options to be included into the dialog WML, right? That's assuming they're fixed and don't change, but if that's what you want, you can just include them like a normal macro, since it's a WML file. However, you also have to include the file that defines the macro. So it might look something like this (extremely simplified):

Code: Select all

{./macros/options.cfg} # define the macro

{OPTIONS} # use the macro
If you're using that approach you probably have to rewrite the options as GUI2 WML instead of DescriptionWML.
thank you very much.
I understand the first part but I didn't understand the second part very well. can you provide me with an example? (specifically about how to implement it into the main file and the lua file) if possible.
also, I want to know if I can add my suggest as an example to the wiki page for complete beginner in lua and gui (like me). it took me a full day between testing and searching so I hope it can help other developers of umc to add some beautiful interface to their games.
other thing (I asked to much. sorry). in the 'resolution' tag, what subtag or key to add to make a message pop-up with the moving of mouse (without selecting) I mean like the description that pop-up when I move the mouse on any window in the main page
Post Reply