Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Grundislav on Thu 10/08/2017 19:55:05

Title: Problems with RemoveItem and text wrapping [SOLVED]
Post by: Grundislav on Thu 10/08/2017 19:55:05
Hello,

I've got a Casebook in my game which features a list of objectives that are added and removed as progress is made throughout investigations. Until now, I had coded some pretty basic functions to add the objective text to a listbox, as well as removing it by name.

For reference, I used the help of this thread (http://www.adventuregamestudio.co.uk/forums/index.php?topic=26052.msg328819#msg328819) for removing the items.

Today, I used monkey0506's code from this thread (http://www.adventuregamestudio.co.uk/forums/index.php?topic=32871.msg425131#msg425131) so that my text would wrap, as up until this point it had been trailing off the screen.

Now the issue I'm having is that removing the text no longer works if the string wraps. I'm assuming this is because the function to make it wrap is changing the string text when truncating and appending, so referring to it by its original name no longer causes it to match.

Is there any way to still remove the longer strings by name?
Title: Re: Problems with RemoveItem and text wrapping
Post by: dayowlron on Thu 10/08/2017 20:12:05
It appears that the word wrapping function is dividing up the strings into multiple strings and adding each individual item.
so if you had "phrase1 phrase2" and you added it to the list box then it is splitting the phrases and adding "phrase1" as 1 entry then "phrase2" as the second entry. Then when you try to find the item to be removed you are looking for 1 entry that has "phrase1 phrase2" and not finding it in the listbox.
What needs to be done is in the removing of the item from the list box you need to do the same splitting you did when you added the entry and then search the list for each item. I could help with the coding there but not right now. I am still at work.
Title: Re: Problems with RemoveItem and text wrapping
Post by: Grundislav on Fri 11/08/2017 14:19:09
I've managed to figure out half the problem. Using my remove function and inputting the text from the second string (the one created when the text wraps) works fine, now it's just a matter of figuring out exactly what the characters in the top string are.
Title: Re: Problems with RemoveItem and text wrapping
Post by: dayowlron on Fri 11/08/2017 14:39:10
every place you find the code lstRoomDesc.AddItem add in the code for the remove call passing in the same string. I saw it in 3 separate places. Perhaps you missed one of them? probably the first one.
Title: Re: Problems with RemoveItem and text wrapping
Post by: dayowlron on Fri 11/08/2017 14:49:21
Unable to test this, but here is some code that should work.
Call RemoveListboxItemByName(listboxcontrol,stringtoremove)
and
AddListboxItemByName(listboxcontrol,stringtoremove)

function RemoveItemByName ( GUIControl* listbox, String name )
{
int i = 0;
while ( i < listbox.AsListBox.ItemCount )
{
if ( listbox.AsListBox.Items[i] == name )
listbox.AsListBox.RemoveItem ( i );

i++;
}
}

function AddListboxItemByName(GUIControl* listbox, String desc)
{
  int i = 0;
  String buffer = "";
  while (i < desc.Length) {
    if (GetTextWidth(buffer.AppendChar(desc.Chars[i]), listbox.Font) <= listbox.Width) buffer = buffer.AppendChar(desc.Chars[i]);
    else {
      while ((buffer.Contains(" ") != -1) && (buffer.Chars[buffer.Length - 1] != ' ')) {
        buffer = buffer.Truncate(buffer.Length - 1); // since there's no reverse-contains, take off one character at a time
        i--;
        }
      listbox.AddItem(buffer);
      i--;
      buffer = "";
      if (listbox.Items[listbox.ItemCount - 1].Contains("[") != -1) {
        int temp = listbox.Items[listbox.ItemCount - 1].Contains("[");
        buffer = listbox.Items[listbox.ItemCount - 1].Substring(temp + 1, listbox.Items[listbox.ItemCount - 1].Length);
        listbox.Items[listbox.ItemCount - 1] = listbox.Items[listbox.ItemCount - 1].Substring(0, temp);
        }
      }
    i++;
    }
  listbox.AddItem(buffer);
  if (buffer.Contains("[") != -1) {
    int temp = buffer.Contains("[");
    listbox.Items[listbox.ItemCount - 1] = buffer.Truncate(temp);
    listbox.AddItem(buffer.Substring(temp + 1, buffer.Length));
    }
}

function RemoveListboxItemByName(GUIControl* listbox, String desc)
{
  int i = 0;
  String buffer = "";
  while (i < desc.Length) {
    if (GetTextWidth(buffer.AppendChar(desc.Chars[i]), listbox.Font) <= listbox.Width) buffer = buffer.AppendChar(desc.Chars[i]);
    else {
      while ((buffer.Contains(" ") != -1) && (buffer.Chars[buffer.Length - 1] != ' ')) {
        buffer = buffer.Truncate(buffer.Length - 1); // since there's no reverse-contains, take off one character at a time
        i--;
        }
      RemoveItemByName(listbox, buffer);
      i--;
      buffer = "";
      if (listbox.Items[listbox.ItemCount - 1].Contains("[") != -1) {
        int temp = listbox.Items[listbox.ItemCount - 1].Contains("[");
        buffer = listbox.Items[listbox.ItemCount - 1].Substring(temp + 1, listbox.Items[listbox.ItemCount - 1].Length);
        listbox.Items[listbox.ItemCount - 1] = listbox.Items[listbox.ItemCount - 1].Substring(0, temp);
        }
      }
    i++;
    }
  RemoveItemByName(listbox, buffer);
  if (buffer.Contains("[") != -1) {
    int temp = buffer.Contains("[");
    listbox.Items[listbox.ItemCount - 1] = buffer.Truncate(temp);
    RemoveItemByName(listbox, buffer.Substring(temp + 1, buffer.Length));
    }
}

Title: Re: Problems with RemoveItem and text wrapping
Post by: selmiak on Sat 12/08/2017 07:51:14
maybe store the strings in array like
Code (ags) Select
arrayname[item1][line1]
arrayname[item1][line2]

arrayname[item2][line1]
arrayname[item2][line2]
arrayname[item2][line3]

and then iterate through the arrayname[itemX] subvalues and remove them all...
Title: Re: Problems with RemoveItem and text wrapping
Post by: Grundislav on Sat 12/08/2017 13:41:30
Thanks! I wound up finding a pretty simple solution. It turns out the format of the first string just adds a space after the last word before it cuts off to wrap, so manually removing both strings works perfectly.