custom UI controls?

The place to post your WML questions and answers.

Moderators: Forum Moderators, Developers

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.

Re: custom UI controls?

Postby Celtic_Minstrel » August 24th, 2017, 1:10 am

Maybe it's just because there are no other modifications to the income field? Try local new_income = old_income() or {}.

Also, return new_income needs to be inside the function (ie, before the end).
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.
User avatar
Celtic_Minstrel
Developer
 
Posts: 754
Joined: August 3rd, 2012, 11:26 pm

Re: custom UI controls?

Postby lea » August 24th, 2017, 3:05 pm

Celtic_Minstrel, thanks
managed to make it work, at least in basic way.
"income" is indeed lua table, still wonder why it was nil in my old code
for posterity, here is what I managed to get to working conditions:
Code: Select all
    [event]
        name=start #also tried preload and other one-time events
        first_time_only=yes
        [lua]
            code = <<
                local old_income = wesnoth.theme_items.income
                function wesnoth.theme_items.income()
                    local new_income = old_income()
                    local new_income_text = tostring(new_income[1][2]["text"])
                    if string.find(new_income_text, " g") == nil then
                        new_income_text = new_income_text .. " gold"
                    end
                    new_income = { { "element", { text = new_income_text } } }
                    return new_income
                end
            >>
        [/lua]
    [/event]
if ... then chunk is needed to avoid adding " gold" line multiple times - it seems that Wesnoth repeatedly calls wesnoth.theme_items.income() with value returned by previous call of this same function as its argument
author of: Altered Era/Ruleset (on BfW 1.12 and BfW 1.10 add-ons servers)
lea
 
Posts: 124
Joined: October 1st, 2016, 7:25 pm

Re: custom UI controls?

Postby enclave » August 24th, 2017, 5:05 pm

Hi lea, I usually don't read forums, so never seen your question..
I have some experience with wesnoth theme items and maybe I say soemthing useful maybe not...
When playing around with theme items keep in mind:
1) If you modifying the income take into account the colors... for example the positive income may be without colors (don't remember for sure) but the negative would be in red color (may be wrong, dont remember, havent needed it for long time) and with added something like <span foreground="#777777">-5</span> or it also changes when it's not your turn into gray.. etc. So need to use something like string.gsub(bcd1, '<span foreground="#777777">', "") to remove the front thing and then string.gsub(bcd2, "</span>", "") to remove the back thing.. I was using the code like that:
Code: Select all
local whole_wml = wesnoth.theme_items.income()
local element_subtag = helper.get_child(whole_wml, "element")
local bcd1 = element_subtag.text
local bcd2 = string.gsub(bcd1, '<span foreground="#777777">', "")
local abc = string.gsub(bcd2, "</span>", "")

to get rid of income color mess... and then you can use "abc" as a pure number..
Maybe it was only needed for "when not your turn"... I don't remember.
2) Yes as far as I know it calls this function every milisecond or second.. in short always when you move mouse cursor or something.. when something changes.. so it changes terrain display etc. And if you erase income via wesnoth theme items then you will not be able to get anything out of it except nil.. the values not stored anywhere that I'm aware of, there are stored where you see them.. so if you clear them then you can't use them. Or something like that.. So best thing for you would be to use income from another container, for example like so (may contain errors):
local team = wesnoth.sides[1] and then use it in your code like so:
Code: Select all
                    local new_income_text = team.total_income
                        new_income_text = new_income_text .. " gold"
                    end

but better always use checking if its nil or not.. because if somehow it becomes nil there will be an error..
3) Don't use it in start event, use it in preload first_time_only=no because if you load a saved game then you will not get this theme hack to work.

Well I hope these tips were any helpful... For more example I could offer you to download my "New Settlers" add-on and look into macros/2016oct/resources_lua.cfg, it contains my code.. also there is a hack for status table in 2017feb/ranged_attacks.cfg which adds some text and icon for archers or mages.. Here is a link to my very old forum thread where I was asking same things as you do now: viewtopic.php?f=58&t=41803&start=105 and some people gave great answers.
And last, luckily I recently told my friend how to do a basic theme items hack so I can paste it here, you already know how to do it but still may be helpful (here is almost my whole post, so keep in mind it is personalized to what my friend asked me):

"So for your question of how to display "1-2-3-teststring-XXX" instead of terrain...
Code: Select all
[event]
name=preload
first_time_only=no
[lua]
code=<<
local old_display_terrain = wesnoth.theme_items.terrain
wesnoth.theme_items.terrain = function()
local oldvalue = old_display_terrain()
     local newtag = T.element { text="1-2-3-teststring-XXX",  tooltip="a tooltip that shows when you mouseover this strange text"}
     table.insert(oldvalue,1,newtag)
     table.remove(oldvalue,2)
  return oldvalue
end
>>
[/lua]
[/event]
table.remove(oldvalue,2) means it will remove terrain info and only display your text..

if you want to insert your text before terrain info then dont use table.remove and use table.insert(oldvalue,newtag) instead of table.insert(oldvalue, 1,newtag)
in this example code oldvalue is a table (what you may want to call object lol, more info here: https://www.lua.org/pil/19.html) and we just add/remove elements from it before displaying it.. the newtag is an element of this array.. and is an array itself kind of... lua tables are strange things.. really really strange things.. instead of text you could use image.. local newtag1 = T.element { image="1-2-3-teststring-XXX.png", tooltip="a tooltip that shows when you mouseover this strange image"} then you could add this image to the table displayed.. like so table.insert(oldvalue,2,newtag1) where number would be a.. lets call it a position of insertion.. play around with it.. you will figure it out =]
And last thing.. to insert this things into terrain field you would use wesnoth.theme_items.terrain, but you can do same to almost any part of display.. for example instead of unit_type you could display whatever you like.. I could make it show Scholar instead of mage if I wanted (never thought of it until now LOL), wesnoth.theme_items.unit_type, or you could use income field like wesnoth.theme_items.income (more here: https://wiki.wesnoth.org/LuaWML/Display#wesnoth.theme_items), but you won't be able to change icons for income or some other things.. you may change them using [theme] tag https://wiki.wesnoth.org/ThemeWML but I don't know how to do it.. so can't help. I tried long time ago and I failed and quit that silly thing.. Or maybe I didnt try to table.remove actually.. I don't remember.. maybe it could work.. Try.. "

good luck..
enclave
 
Posts: 532
Joined: December 15th, 2007, 8:52 am

Re: custom UI controls?

Postby lea » August 24th, 2017, 7:30 pm

thanks, enclave, if it is not useful now then it might happen to be useful later )

enclave wrote:I usually don't read forums
same for me ;) except I sign for topics which I create or respond to so that I am notified via email when my questions are answered

enclave wrote:1) If you modifying the income take into account the colors...
I want to add to income's text instead of editing/replacing it so it is unlikely to be an issue for me

enclave wrote:And if you erase income via wesnoth theme items then you will not be able to get anything out of it except nil..
it explains why I got error until "return" clause was moved to be before "end" clause after Celtic_Minstrel's suggestion

enclave wrote:3) Don't use it in start event, use it in preload first_time_only=no because if you load a saved game then you will not get this theme hack to work.
I would discover this weeks or months later if you would not warn me...

thanks for terrain indicator example as well since I consider making it shorter for some compound terrain types so that it would always fit in one line on side panel (for now it truncates long terrain descriptions)
author of: Altered Era/Ruleset (on BfW 1.12 and BfW 1.10 add-ons servers)
lea
 
Posts: 124
Joined: October 1st, 2016, 7:25 pm

Re: custom UI controls?

Postby Zap-Zarap » August 25th, 2017, 6:47 pm

Hello lea.
It seems, that we both are trying to solve similar problems right now, these days.
I started this thread http://forums.wesnoth.org/viewtopic.php?f=21&t=46730 a few days ago.

I think, what maybe might be interesting for you is, that the wiki (and all examples above here in your thread), all tell us how to override an existing theme_icon, but not how to add more to the existing ones.

I've been testing it, it worked.

INFO BOXES in Top Panel.PNG

Here you can see my top panel boxes, i've deleted the income and upkeep completely, and added boxes to display "food, wood, gold and stone" resources (hello, enclave :D ). Those four are variables in my WML, passed to a Lua skript somewhere outside, so, as i'm planning for a SP-campaign, i can reuse the skript for displaying this from all scenarioes easily.

Btw: right now, I'm trying to mimic a Recruit Menu, that looks the same as the usual 1.12.x menu, but displays prices not only involving gold, but food, wood, gold and stone. Sadly, i couldn't find any information or the code, that does the layout of BfW's original Recruit Menu, so i have to do try-and-error to get the right sizes, fonts and such
I like beavers.
User avatar
Zap-Zarap
 
Posts: 102
Joined: March 16th, 2015, 2:18 pm

Re: custom UI controls?

Postby gfgtdf » August 25th, 2017, 7:21 pm

Zap-Zarap wrote:right now, I'm trying to mimic a Recruit Menu, that looks the same as the usual 1.12.x menu, but displays prices not only involving gold, but food, wood, gold and stone. Sadly, i couldn't find any information or the code, that does the layout of BfW's original Recruit Menu, so i have to do try-and-error to get the right sizes, fonts and such


I assume you are using wesnoth.show_dialog? This became easier in 1.13 where you have a custom gui2 widget [unit_preview_pane] that shows the units image/stats just like in the recruit dialog.
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.
gfgtdf
Developer
 
Posts: 899
Joined: February 10th, 2013, 2:25 pm

Re: custom UI controls?

Postby Zap-Zarap » August 25th, 2017, 7:33 pm

gfgtdf wrote:I assume you are using wesnoth.show_dialog?

Exactly true. There was an example somewhere, that i could adapt to my needs.
gfgtdf wrote:This became easier in 1.13 where you have a custom gui2 widget [unit_preview_pane] that shows the units image/stats just like in the recruit dialog.

Cool. I've just started to experiment with WML an Lua, doing some very first steps, so, until my campaign will be even only half finished, probably 1.14. will be the stable release. So anyway i'm going to use that, what you've mencioned..

I'm still wondering, where to find the recruit menu dialogue in 1.12. (core-)files. Or is it created directly from C++ engine, and has no WML at all?

Celtic_Minstrel wrote:I know of no way to add a scrollbar, though.

Not sure 100%, but i think scrollbars somehow appear automatically, when you use "horizontal_grow = true" somewhere in your grid.
I like beavers.
User avatar
Zap-Zarap
 
Posts: 102
Joined: March 16th, 2015, 2:18 pm

Re: custom UI controls?

Postby gfgtdf » August 25th, 2017, 8:23 pm

Zap-Zarap wrote:Cool. I've just started to experiment with WML an Lua, doing some very first steps, so, until my campaign will be even only half finished, probably 1.14. will be the stable release. So anyway i'm going to use that, what you've mencioned..

I'm still wondering, where to find the recruit menu dialogue in 1.12. (core-)files. Or is it created directly from C++ engine, and has no WML at all?


yes, in 1.12 this is created directly in the c++ engine.
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.
gfgtdf
Developer
 
Posts: 899
Joined: February 10th, 2013, 2:25 pm

Re: custom UI controls?

Postby Celtic_Minstrel » August 26th, 2017, 2:28 am

lea wrote:if ... then chunk is needed to avoid adding " gold" line multiple times - it seems that Wesnoth repeatedly calls wesnoth.theme_items.income() with value returned by previous call of this same function as its argument
That's weird, sounds like a bug. The engine should be generating its own income report and then passing that to your function every time it updates the display.

Zap-Zarap wrote:Hello lea.
I think, what maybe might be interesting for you is, that the wiki (and all examples above here in your thread), all tell us how to override an existing theme_icon, but not how to add more to the existing ones.
Um, the custom statuses example involves adding more to existing reports, so I really don't know what you're talking about here.

Zap-Zarap wrote:
gfgtdf wrote:I assume you are using wesnoth.show_dialog?

Celtic_Minstrel wrote:I know of no way to add a scrollbar, though.

Not sure 100%, but i think scrollbars somehow appear automatically, when you use "horizontal_grow = true" somewhere in your grid.

In GUI2, yes. In ThemeWML reports, no. There are no scrollbars in the ThemeWML.

Eventually the ThemeWML will also be GUI2, but we're not there yet. It'll probably not be until 1.16 / 2.0 (whichever we decide to label it).
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.
User avatar
Celtic_Minstrel
Developer
 
Posts: 754
Joined: August 3rd, 2012, 11:26 pm

Re: custom UI controls?

Postby lea » August 26th, 2017, 5:54 am

Hello Zap-Zarap,

yes, and I already managed to append the value of wml variable to the value shown by "income" indicator by default - and even to make this indicator show correct values for different players simultaneously. going to release it as part of next version of my era, during this or maybe next month.

I do not intend to change icons so far but others might want to do so - maybe you can post your code that does it? or add it to the wiki?

Zap-Zarap wrote:Here you can see my top panel boxes, i've deleted the income and upkeep completely, and added boxes to display "food, wood, gold and stone" resources (hello, enclave :D ).
what "Enclave" game are you talking about? google searches return links to third-person slasher from the times of first x-box while your mentions of resources like wood and stone suggest real time strategy - could you please post a link to review or official web site of this Enclave RTS?

Zap-Zarap wrote:Btw: right now, I'm trying to mimic a Recruit Menu, that looks the same as the usual 1.12.x menu, but displays prices not only involving gold, but food, wood, gold and stone. Sadly, i couldn't find any information or the code, that does the layout of BfW's original Recruit Menu, so i have to do try-and-error to get the right sizes, fonts and such
I could not find it so I constructed my "recruit in settlement" menu from WML messages with options...

Celtic_Minstrel wrote:
lea wrote:if ... then chunk is needed to avoid adding " gold" line multiple times - it seems that Wesnoth repeatedly calls wesnoth.theme_items.income() with value returned by previous call of this same function as its argument
That's weird, sounds like a bug. The engine should be generating its own income report and then passing that to your function every time it updates the display.
I guess it was made to pass old value (taken from some kind of internal cache) on subsequent calls until "genuine" update comes in and replaces old value in "internal cache"... in any case I hesitate to dig into the C++ code to find it out for sure - my curiosity has limits :)
author of: Altered Era/Ruleset (on BfW 1.12 and BfW 1.10 add-ons servers)
lea
 
Posts: 124
Joined: October 1st, 2016, 7:25 pm

Re: custom UI controls?

Postby Zap-Zarap » August 26th, 2017, 6:54 am

lea wrote:1)... maybe you can post your code that does it? or add it to the wiki? ...

2) ...what "Enclave" game are you talking about? ...


1) Sure, I will. Right now my code is quite a mess, full of out-commented lines and other rubbish, but as soon, as i can manage to tidy it up a little, i will post some snippets here.
Concerning Wiki edits: I'm so noob to WML and all this, that i don't dare to write edits into those nice Wiki pages, but one day, when i'm a bit more selfconfident, i will.

2) "enclave" is a BfW mod developer (see enclave's posts above in this thread, no google search needed, just scroll up a little ;) ). One important add-on enclave did, is "New Settlers Mod", it has peasants building villages and mages doing research (similar to "A New Land"), but instead of only gold, you have 4 kinds of resources to deal with (e.g. recruiting a Bowman doesn't cost 14 gold as in normal Wesnoth, instead it costs "15 food, 20 wood, 0 stone, 0 gold", so i need menues and top panel to display those things).
I am trying to develope a SP-campaign for New Settlers.
I like beavers.
User avatar
Zap-Zarap
 
Posts: 102
Joined: March 16th, 2015, 2:18 pm

Re: custom UI controls?

Postby Zap-Zarap » August 26th, 2017, 8:54 am

Here is my example code for 1.12.6, i hope people can understand, what i mean.

To achieve this:
toppanel.PNG

which is an additional custom status box right of the timer box, you need to add to your theme three (*) things (referring to 1.12. default theme):

1) around line 112 of themes/default.cfg (which for a start you can copy to your add-on's theme folder), add the new STATUS_BOX
Code: Select all
        {STATUS_BOX +5 =+0 +75 +15 turn     actions-menu           fixed fixed}
        {STATUS_BOX +3 =+0 +75 +15 gold     turn-box-topright      fixed fixed}
        {STATUS_BOX +3 =+0 +70 +15 villages gold-box-topright      fixed fixed}
        {STATUS_BOX +3 =+0 +70 +15 units    villages-box-topright  fixed fixed}
        {STATUS_BOX +3 =+0 +85 +15 upkeep   units-box-topright     fixed fixed}
        {STATUS_BOX +3 =+0 +85 +15 income   upkeep-box-topright    fixed fixed}
        {COUNTDOWN_THEME}
      
        ######## ADD YOUR CODE HERE  #1/3
        {STATUS_BOX +3 =+0 +85 +15 foobar_status  timeout-box-topright    fixed fixed}
        #################################

2) around line 208 of themes/default.cfg add the new label for the new STATUS_BOX
Code: Select all
        ######## ADD YOUR CODE HERE  #2/3
        [label]
            id=foobar_status-icon
            icon=images/foobar_status-icon.png
            text= _ "My custom tooltip text"
            ref=foobar_status-box-center
            rect="=+1,=-1,+25,+17"
            xanchor=fixed
            yanchor=fixed
        [/label]      
        ##################################

3) around line 348 of the original Default theme add your new status
Code: Select all
    [status]

         ............

        {COUNTDOWN_THEME_STATUS {DEFAULT_FONT_SMALL}}

        ######## ADD YOUR CODE HERE  #3/3         
        [foobar_status]
            id=foobar_status
            font_size={DEFAULT_FONT_SMALL}
            ref=foobar_status-icon
            rect="+4,=,+60,+16"
            xanchor=fixed
            yanchor=fixed
            prefix="" #wmllint: ignore
            prefix_literal=""
        [/foobar_status]
        ##################################   

        ............   

    [/status]


Now you can add an [event] in your scenarios, with a [lua] inside, doing
Code: Select all
local foobarstatusLUAvariable = wesnoth.get_variable("foobarstatusWMLvariable")

function  wesnoth.theme_items.foobar_status()
    local fbs = {}
    table.insert(fbs, 1, { "element", {text = tostring(foobarstatusLUAvariable),
                                       tooltip = "My custom tooltip text" }})
return fbs end


* There are some more adaptions necessary in the [partialresolution] part further down in the theme code, but i'll leave that out here for brevity reason. You can spot those easily.



EDIT: did some changes in the snippets according to Celtic_Ministrel's points in the next comment. :D
Last edited by Zap-Zarap on August 27th, 2017, 6:42 am, edited 7 times in total.
I like beavers.
User avatar
Zap-Zarap
 
Posts: 102
Joined: March 16th, 2015, 2:18 pm

Re: custom UI controls?

Postby lea » August 26th, 2017, 9:10 am

Zap-Zarap wrote:2) "enclave" is a BfW mod developer (see enclave's posts above in this thread, no google search needed, just scroll up a little ;) ).
oh, you are talking about user and his/her add-on :oops: now it came together )

and thanks for code snippets!
author of: Altered Era/Ruleset (on BfW 1.12 and BfW 1.10 add-ons servers)
lea
 
Posts: 124
Joined: October 1st, 2016, 7:25 pm

Re: custom UI controls?

Postby Celtic_Minstrel » August 26th, 2017, 2:05 pm

  1. You probably don't need the call to my_custom_status_old(), unless you expect other people to be overriding the same status. In fact, you really only need this if you're augmenting an existing report.
  2. I highly recommend calling it something other than my_custom_status.
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.
User avatar
Celtic_Minstrel
Developer
 
Posts: 754
Joined: August 3rd, 2012, 11:26 pm

Re: custom UI controls?

Postby Zap-Zarap » August 26th, 2017, 2:20 pm

Celtic_Minstrel wrote:You probably don't need the call to my_custom_status_old()

Ok.
I copied the code from the famous unit-status example in the Wiki (maybe without fully understanding what it does).
But i cannot imagine how to change that code without that call. If you don't mind, please give me a hint and post a code example of how you'd do it. :hmm:

Celtic_Minstrel wrote:I highly recommend calling it something other than my_custom_status.

:) I called it like this only here in the example code. I thought, that was the way, documentation and examples are supposed to be like (maybe i should use "foo" and "bar" in code examples?).
In my actual code, things have different names.
I like beavers.
User avatar
Zap-Zarap
 
Posts: 102
Joined: March 16th, 2015, 2:18 pm

PreviousNext

Return to WML Workshop

Who is online

Users browsing this forum: Google [Bot], Yandex [Bot] and 1 guest