ScriptSpot

Forum topic · General Scripting

listbox to multilistbox convert

By JokerMartini · 2011-05-18

Description

Below I have a listbox that works exactly the way I want it to. Allowing users to move objects up and down, remove, add and clear. However some of those features to not work when changed over to a multilist box. Which is what I need because it allows for multiple objects to be selected. I just cant figure out how to get the moving up and down to work as well as the remove with multilist.
Here is what i have that works for listbox.


(
local AnimObjs = #()

rollout rlD ""
(
ListBox lbxAnimatedObjects "Animated Objects" pos:[10,7] width:120 height:12
button btnAddAnimObj "+" pos:[10,185] width:24 height:24
button btnRemoveAnimObjs "-" pos:[34,185] width:24 height:24
button btnClearAnimObjs "X" pos:[58,185] width:24 height:24
button btnMoveUpAnimObj"^" pos:[82,185] width:24 height:24
button btnMoveDownAnimObj "v" pos:[106,185] width:24 height:24

on btnAddAnimObj pressed do -- Add Objects
(
userSel = getCurrentSelection()
if userSel.count >= 1 do
(
for o in userSel do (appendIfUnique AnimObjs o)
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
)
)

on btnRemoveAnimObjs pressed do -- Removes Object fromt list
(
local currSel = lbxAnimatedObjects.selection
for i = lbxAnimatedObjects.items.count to 1 by -1 where currSel[i] do (deleteItem AnimObjs i)
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
)

on btnMoveUpAnimObj pressed do
(
local itm = lbxAnimatedObjects.selection
if itm > 1 do
(
swap lbxAnimatedObjects.items[itm] lbxAnimatedObjects.items[itm-1]
lbxAnimatedObjects.items = lbxAnimatedObjects.items
swap AnimObjs[itm] AnimObjs[itm-1]
lbxAnimatedObjects.selection -= 1
)
)

on btnMoveDownAnimObj pressed do
(
local itm = lbxAnimatedObjects.selection
if itm < lbxAnimatedObjects.items.count do
(
swap lbxAnimatedObjects.items[itm] lbxAnimatedObjects.items[itm+1]
lbxAnimatedObjects.items = lbxAnimatedObjects.items
swap AnimObjs[itm] AnimObjs[itm+1]
lbxAnimatedObjects.selection += 1
)
print AnimObjs
)

on btnClearAnimObjs pressed do lbxAnimatedObjects.items = (AnimObjs=#()) -- Clear List
)
createDialog rlD 140 225
)

Comments (8)

JokerMartini · 2012-07-06

Yeah It was a bit of a challenge to get this working the way I wanted it to. It's a good snippet to have around.

MathisF · 2012-07-06

Thanks for the examples of a code, that's what I really need now. I'm a newbie to this and sometimes I just need to check the right example to figure out where my mistakes come from.
Cheers,
Mathis, free mov to avi converter

McGreed

JokerMartini · 2011-05-19

Hey thanks again for the help. I ended up fixing it this morning and got it working. This is what I came up with for a solution. I did not change the move down button yet but the move up works.

I think next it would be good to input an error check because it bugs out if you try to move the top item in the list up. As well as have the selection update as the items move up and down.
Great work on helping.

I feel like this is a good base script to have for anyone who wants to use these lists and whatnot. Also considering it will have the tools to modify the list as well.


(
local AnimObjs = #()

rollout rlD ""
(
multiListBox lbxAnimatedObjects "Animated Objects" pos:[10,7] width:120 height:12
button btnAddAnimObj "+" pos:[10,185] width:24 height:24
button btnRemoveAnimObjs "-" pos:[34,185] width:24 height:24
button btnClearAnimObjs "X" pos:[58,185] width:24 height:24
button btnMoveUpAnimObj"^" pos:[82,185] width:24 height:24
button btnMoveDownAnimObj "v" pos:[106,185] width:24 height:24

on btnAddAnimObj pressed do -- Add Objects
(
userSel = getCurrentSelection()
if userSel.count >= 1 do
(
for o in userSel do (appendIfUnique AnimObjs o)
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
)
)

on btnRemoveAnimObjs pressed do -- Removes Object fromt list
(
local currSel = lbxAnimatedObjects.selection
for i = lbxAnimatedObjects.items.count to 1 by -1 where currSel[i] do (deleteItem AnimObjs i)
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
)

on btnMoveUpAnimObj pressed do
(
local itm = lbxAnimatedObjects.selection as array
if itm.count == 0 then print "Nothing is selected" else
(
for i = 1 to itm.count do
(
itmNum = itm[i] as integer
swap AnimObjs[itmNum] AnimObjs[itmNum-1]
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
)
)
)

on btnMoveDownAnimObj pressed do
(
local itm = lbxAnimatedObjects.selection
if itm < lbxAnimatedObjects.items.count do
(
swap lbxAnimatedObjects.items[itm] lbxAnimatedObjects.items[itm+1]
lbxAnimatedObjects.items = lbxAnimatedObjects.items
swap AnimObjs[itm] AnimObjs[itm+1]
lbxAnimatedObjects.selection += 1
)
print AnimObjs
)

on btnClearAnimObjs pressed do lbxAnimatedObjects.items = (AnimObjs=#()) -- Clear List
)
createDialog rlD 140 225
)

McGreed · 2011-05-19

Here I fixed the Move Up thing:


		on btnMoveUpAnimObj pressed do 
		( 
			local itm = lbxAnimatedObjects.selection as array
			currSel = lbxAnimatedObjects.selection as array
			if itm.count > 0 do
			(
 				for i in currSel do
 				(
 					swap (lbxAnimatedObjects.items[i]) (lbxAnimatedObjects.items[(i-1)])
 					swap (AnimObjs[i]) (AnimObjs[(i-1)])
 				)
				lbxAnimatedObjects.items = lbxAnimatedObjects.items
				AnimObjs = AnimObjs
			)
			print (lbxAnimatedObjects.items as string)
		)

Should be able to do the rest with this. The AnimObjs = AnimObjs might look strange, but for some reason the list wouldn't update unless I did it.

nutJob approach:

Graph · 2011-05-19


(
 rollout RO ""
 (
  struct item (name, moveUp=false, moveDown=false, del=false)
  local objArr = for i = 1 to 10 collect (item name:("obj_"+i as string))
  
  fn sortEM arr =
  (
   for i = arr.count to 1 by -1 do 
   (
    case of
    (
     (arr[i].moveUp AND i > 1) : 
     (
      arr[i].moveUp = false
      swap arr[i] arr[i-1]
     )
     
     (arr[i].moveDown AND i < arr.count) : 
     (
      arr[i].moveDown = false
      swap arr[i] arr[i+1]
     )
     
     (arr[i].del) : (deleteItem arr i)
     
     default : ()
    )
   )
   return OK
  )
  
  multiListBox mlb "" items:(for obj in objArr collect obj.name)
   button up "UP" across:3
   button down "DOWN"
   button deleteItm "DEL"
  
  on up pressed do
  (
   for i = 1 to objArr.count where mlb.selection[i] do objArr[i].moveUp = true
   sortEM objArr
   mlb.items = for obj in objArr collect obj.name
  )--END up.pressed
  
  on down pressed do
  (
   for i = 1 to objArr.count where mlb.selection[i] do objArr[i].moveDown = true
   sortEM objArr
   mlb.items = for obj in objArr collect obj.name
  )--END down.pressed
  
  on deleteItm pressed do
  (
   for i = 1 to objArr.count where mlb.selection[i] do objArr[i].del = true
   sortEM objArr
   mlb.items = for obj in objArr collect obj.name
  )--END deleteItm.pressed
  
 )--END RO
 createDialog RO 
)

;)

McGreed · 2011-05-18

For removing stuff, you can use this:


currSel = lbxAnimatedObjects.selection as array
for i in currSel.count to 1 by -1 do
(
	lbxAnimatedObjects.items = deleteItem lbxAnimatedObjects.items (currSel[i])
)

And I think this would work for moving the items up:


on btnMoveUpAnimObj pressed do 
( 
	local itm = lbxAnimatedObjects.selection as array
	if itm.count > 0 do
	(
		for i in currSel.count do
		(
			swap lbxAnimatedObjects.items[i] lbxAnimatedObjects.items[i-1]
			swap AnimObjs[itm] AnimObjs[itm-1]
		)
	)
)

McGreed

JokerMartini · 2011-05-19

Hey McGreed, that you for the help. I really appreciate it.
As for your code. I feel like its going in the right direction but when I run it does not work. It errors out.
Try this out. Keep in mind this is a multilist not a listbox.


(
local AnimObjs = #()

rollout rlD ""
(
multiListBox lbxAnimatedObjects "Animated Objects" pos:[10,7] width:120 height:12
button btnAddAnimObj "+" pos:[10,185] width:24 height:24
button btnRemoveAnimObjs "-" pos:[34,185] width:24 height:24
button btnClearAnimObjs "X" pos:[58,185] width:24 height:24
button btnMoveUpAnimObj"^" pos:[82,185] width:24 height:24
button btnMoveDownAnimObj "v" pos:[106,185] width:24 height:24

on btnAddAnimObj pressed do -- Add Objects
(
userSel = getCurrentSelection()
if userSel.count >= 1 do
(
for o in userSel do (appendIfUnique AnimObjs o)
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
)
)

on btnRemoveAnimObjs pressed do -- Removes Object fromt list
(
local currSel = lbxAnimatedObjects.selection
for i = lbxAnimatedObjects.items.count to 1 by -1 where currSel[i] do (deleteItem AnimObjs i)
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
)

on btnMoveUpAnimObj pressed do
(
local itm = lbxAnimatedObjects.selection as array
currSel = lbxAnimatedObjects.selection as array
if itm.count > 0 do
(
for i in currSel.count do
(
swap lbxAnimatedObjects.items[i] lbxAnimatedObjects.items[i-1]
swap AnimObjs[itm] AnimObjs[itm-1]
)
)
)

on btnMoveDownAnimObj pressed do
(
local itm = lbxAnimatedObjects.selection
if itm < lbxAnimatedObjects.items.count do
(
swap lbxAnimatedObjects.items[itm] lbxAnimatedObjects.items[itm+1]
lbxAnimatedObjects.items = lbxAnimatedObjects.items
swap AnimObjs[itm] AnimObjs[itm+1]
lbxAnimatedObjects.selection += 1
)
print AnimObjs
)

on btnClearAnimObjs pressed do lbxAnimatedObjects.items = (AnimObjs=#()) -- Clear List
)
createDialog rlD 140 225
)