Problems with RemoveItem and text wrapping [SOLVED]

Started by Grundislav, Thu 10/08/2017 19:55:05

Previous topic - Next topic

Grundislav

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 for removing the items.

Today, I used monkey0506's code from this thread 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?

dayowlron

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.
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Grundislav

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.

dayowlron

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.
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

dayowlron

Unable to test this, but here is some code that should work.
Call RemoveListboxItemByName(listboxcontrol,stringtoremove)
and
AddListboxItemByName(listboxcontrol,stringtoremove)
Code: ags

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));
    }
}

Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

selmiak

maybe store the strings in array like
Code: ags
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...

Grundislav

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.

SMF spam blocked by CleanTalk