Lua:Class:Listview

From Cheat Engine
Jump to navigation Jump to search

I recently had occasion to use this creating an Upgrade editor for Shadow Warrior 2 for the great table by Zanzer...

I have a function that opens a form in LUA, clears the list view items, and listens for the user to click on an item:

 local lv = FormEditUpgrade.listViewProperties
 lv.items.clear()
 lv.setOnClick(FormEditUpgrade_listViewProperties_OnClick)

So the form defines the list view with 3 columns. I changed the following properties when designing the form:

  • ColumnClick = False - ??
  • ReadOnly = True - don't allow edits directly in the table
  • RowSelect = True - select a whole row when clicking
  • ViewStyle = vsReport - shows multiple columns with headings

When the last upgrade changes, I analyze the memory and create an array in LUA of the attributes of that upgrade. I then clear the items array on the list view and recreate them.

You can see here that item.Caption controls what is displayed in the first column. I set the other columns using SubItems.text by setting it to the values to put in those columns separated by CRLF. SubItems is a 'Strings' object and there may be a better way to do that, but this was easy enough for me.

 local function LoadUpgrade(address)
   CURRENT_UPGRADE = Upgrade(address)
   local lv = FormEditUpgrade.listViewProperties
   local items = lv.items
   items.clear()
   for i,attr in ipairs(CURRENT_UPGRADE.attributes) do
     local item = items:add()
     item.Caption = attr.name
     item.SubItems.text = table.concat({ attr.value or 'nil', attr.type }, '\r\n')
   end
 end

When the user clicks on a row, I use ItemIndex and match it to the attribute in my LUA array. The SelectAttribute function handles using 'inputQuery' to pop up a window for changing the value, changing the value, and reloading the updated Upgrade.

 local function FormEditUpgrade_listViewProperties_OnClick()
   if CURRENT_UPGRADE == nil or CURRENT_UPGRADE.attributes == nil or #CURRENT_UPGRADE.attributes == 0 then return end
   local lv = FormEditUpgrade.listViewProperties
   local selectedIndex = lv.ItemIndex
   if selectedIndex < 0 or selectedIndex >= #CURRENT_UPGRADE.attributes then return end
   SelectAttribute(CURRENT_UPGRADE.attributes[selectedIndex+1]) -- +1 for 1 based LUA
 end