Crendgrim's Lua Thread

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

Moderators: Forum Moderators, Developers

User avatar
Crendgrim
Moderator Emeritus
Posts: 1328
Joined: October 15th, 2010, 10:39 am
Location: Germany

Re: Crendgrim's Lua Thread

Post by Crendgrim » January 31st, 2012, 12:03 pm

And I'm back with yet another problem.
I finished the questlog dialog, but wanted to improve it now by adding images in front of the labels. With vultraz' help I came up with the following:

Code: Select all

-- Show a questlog
function wml_actions.qow_show_quests(cfg)
  
  local quest_data = {}
  local id = 0
  
  local quests = helper.get_variable_array "qow_quest"
  
  local dialogue = {
    T.helptip { id = "tooltip_large" }, -- mandatory field
    T.tooltip { id = "tooltip_large" }, -- mandatory field
    T.linked_group { id = "labels", fixed_width = "yes" },
--     T.linked_group { id = "images", fixed_width = "yes" },
    maximum_height = 480,
    maximum_width = 500,
    width = 500,
    T.grid {
      T.row {
        T.column { vertical_alignment = "center", horizontal_alignment = "center", border = "all", border_size = 5,
          T.label { definition = "title", label = _"Questlog" }
        },
        T.column { T.spacer { definition = "default" } }
      },
      T.row {
        T.column { min_width = 200, default_width = 200,
          T.listbox { id = "scroll_quests", horizontal_scrollbar_mode = "never",
            T.list_definition { T.row { T.column {
              T.toggle_panel { T.grid { T.row {
--                   T.column { T.image { id = "listbox_image", linked_group = "images" } },
                  T.column { T.label { id = "listbox_text", linked_group = "labels" } }
                } } }
            } } }
          }
        },
        T.column { border = "all", border_size = 5, vertical_alignment = top, horizontal_alignment = left,
          T.scroll_label { id = "quest_details" }
        }
      },
      T.row { T.column { T.spacer { width = 150 } },
        T.column { T.spacer { width = 300 } }
      },
      T.row {
        T.column { T.button { id = "close_button", return_value = 1, label = _"Close" } },
        T.column { T.button { id = "close_button", return_value = 2, label = _"Follow" } }
      }
    }
  }
  
  local function preshow()
  
    for i = 1, #quests do
      local pnr = quests[i].pnr
      local finished = wesnoth.get_variable("qow_q_" .. quests[i].id .. "_" .. quests[i][pnr][2].id)
      local description = ""
      local status = "open"
      for j = 1, pnr, -1 do
        description = description .. "\n\n [" .. j .. "] " .. quests[i][j][2].description
      end
      if finished then
  
        description = description .. _"Quest solved."
        status = "solved"
        
      end
      quest_data[i] = description
--       if status == "open" then
--         wesnoth.set_dialog_value("misc/quest_open.png", "scroll_quests", i, "listbox_image")
--       elseif status == "solved" then
--         wesnoth.set_dialog_value("misc/quest_solved.png", "scroll_quests", i, "listbox_image")
--       end
      
      -- NOTE the following line is the one that causes the crash.
      wesnoth.set_dialog_value(quests[i].name, "scroll_quests", i, "listbox_text")
      wesnoth.set_dialog_value(quest_data[1], "quest_details")
    end
    
    
    local function display_selected_quest_details()
      local i = wesnoth.get_dialog_value("scroll_quests")
      wesnoth.set_dialog_value(quest_data[i], "quest_details")
    end
    
    wesnoth.set_dialog_callback(display_selected_quest_details, "scroll_quests")
  
  end
  
  local function postshow()
    local i = wesnoth.get_dialog_value("scroll_quests")
    id = quests[i].id
  end
  
  local function sync()
    local return_value = wesnoth.show_dialog(dialogue, preshow, postshow)
    return { return_value = return_value }
  end
  
  local return_table = wesnoth.synchronize_choice(sync)
  if return_table.return_value == 2 then
    wesnoth.set_variable("qow_current_quest", id)
    wml_actions.qow_mark_quest {}
  end
  
end
Well, as you see the lines for the images are still commented out, as this code does not work (see the line with the NOTE comment).

Code: Select all

20120131 12:32:40 error general: An error due to possibly invalid WML occurred
The error message is :
Image doesn't fit on canvas.
 
When reporting the bug please include the following error message :
Condition 'static_cast<int>(x) >= 0' failed at src/gui/auxiliary/canvas.cpp:1089 in function 'draw'. Extra development information: Image 'misc/selection2-border-topright.png', x = -1.
What's wrong with that code?
UMC Story Images — Story images for your campaign!

Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: Crendgrim's Lua Thread

Post by Exasperation » January 31st, 2012, 4:43 pm

Try this:

Code: Select all

T.column { T.image { id = "listbox_image", linked_group = "images", label = "misc/quest_open.png" } },

Code: Select all

if status == "solved" then -- don't need the case for quest_open, since it now defaults to that
   wesnoth.set_dialog_value("misc/quest_solved.png", "scroll_quests", i, "listbox_image")
end
Many (most?) widgets are not designed to dynamically resize themselves when their contents change (widgets which have scrollbars built in are an exception to this; listboxes, scroll_labels, etc.). So when your window is being built, it looks at the definition of the image, sees that there's no image file specified, and thinks "Oh, I only need to reserve a 0x0 area for this image". Then when you try to insert an image later, it fails. There are various ways to get around this; you can specify a default image (as above), add it to a linked group with an image that does have an initial image, or build the wml table for your list data dynamically before the call to wesnoth.show_dialog (so that all the entries have the correct value for the image before the window is built). Which method works best depends on what you're doing with your dialog.

User avatar
Crendgrim
Moderator Emeritus
Posts: 1328
Joined: October 15th, 2010, 10:39 am
Location: Germany

Re: Crendgrim's Lua Thread

Post by Crendgrim » January 31st, 2012, 5:39 pm

I already thought something alike. Sadly, your proposal is no solution for my problem, as I commented out all the image-related code (see above). The line wesnoth.set_dialog_value(quests[i].name, "scroll_quests", i, "listbox_text") causes the error. :?
UMC Story Images — Story images for your campaign!

Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: Crendgrim's Lua Thread

Post by Exasperation » January 31st, 2012, 6:02 pm

Hmmm... you may need to give that grid cell permission to grow. Try this:

Code: Select all

T.column { vertical_grow = "true", horizontal_grow = "true", T.label { id = "listbox_text", linked_group = "labels" } }

User avatar
Crendgrim
Moderator Emeritus
Posts: 1328
Joined: October 15th, 2010, 10:39 am
Location: Germany

Re: Crendgrim's Lua Thread

Post by Crendgrim » January 31st, 2012, 6:36 pm

That I also tried before, works neither.
I have no ideas any more...
UMC Story Images — Story images for your campaign!

Exasperation
Posts: 462
Joined: June 8th, 2006, 3:25 am

Re: Crendgrim's Lua Thread

Post by Exasperation » January 31st, 2012, 9:42 pm

Hmmm... what's pnr? I don't know if it's related to the problem, but "for j = 1, pnr, -1 do" seems suspicious to me. If pnr >= 1 (which is suggested by its use as an index for a wml table earlier), I would think either "for j = 1, pnr do" or "for j = pnr, 1, -1 do" would make more sense there (or possibly "for j = 1, pnr - 1 do").

User avatar
Crendgrim
Moderator Emeritus
Posts: 1328
Joined: October 15th, 2010, 10:39 am
Location: Germany

Re: Crendgrim's Lua Thread

Post by Crendgrim » February 1st, 2012, 4:03 pm

Ehem. That... you did not see!
Yes, you're right. :oops:

However, it's unrelated to the problem; I can even replace the variable with some string, and it still gives the same error. :(
UMC Story Images — Story images for your campaign!

Post Reply