Return value from dynamic rollout?

Return value from dynamic rollout?

senorpablo
Advocate Advocate
2,017 Views
9 Replies
Message 1 of 10

Return value from dynamic rollout?

senorpablo
Advocate
Advocate

I need to create a simple color picker dialog. The idea is a button is pressed in the tool, and a model dialog opens with about 50 little color swatches(using colored checkbutton controls). When a user selects a swatch, the color palette should close and return the color to the tool that called it.

 

I want to use the dynamic rollout creation so I can add a large number of color swatches in an automated way. I've searched around and tried numerous things to try and get a return value, but I haven't been able to get anything to work. There are examples of how to do this with predefined rollouts and controls, but not using a dynamic rollout.

 

Any ideas? Sample code below:

 

 

rci = rolloutCreator "Color_Palette_Picker" "Color Palette"
rci.begin()
rci.addLocal "retval" init:-1
rci.addControl #checkbutton #checkbutton_p1 ""
rci.addHandler #checkbutton_p1 #changed paramStr:"val" codeStr:"MessageBox @Isn't this cool@ title:@Wow@"
rci.addControl #checkbutton #checkbutton_p2 ""
rci.addHandler #checkbutton_p2 #changed paramStr:"val" codeStr:"print retval; retval = 999; print retval; result = retval; destroydialog Color_Palette_Picker"
nr=rci.end()

createDialog (nr) modal:true

0 Likes
Accepted solutions (1)
2,018 Views
9 Replies
Replies (9)
Message 2 of 10

denisT.MaxDoctor
Advisor
Advisor

Can you describe the scenario in more detail?
The color picker dialog has a modal, not a modal state, it can be confirmed or canceled. Could you provide a scenario for all cases?

0 Likes
Message 3 of 10

senorpablo
Advocate
Advocate

What I need is a modal color swatch dialog that returns the color value of the swatch that was clicked, and then closes. It's not really an eye-dropper style picker.

 

The final dialog would look something like this:

ColorSwatches.JPG

 

There are a large number of swatches based on a data file, so I want to create them dynamically. I'm using the checkbutton control and setting the highight color to be the individual color swatches. That part all works well. I just can't figure out how to return the color of the checkbutton that was pressed. If there's a way to set a variable in the parent control, that could work too.

 

0 Likes
Message 4 of 10

denisT.MaxDoctor
Advisor
Advisor

hre is how I would do it... 

there are two versions (one is commented):

# colorpicker controls solution

# imgtag controls solution

 

get it as an idea...

 

global PickColorRollout

/*
fn makeRollout dimX:8 dimY:4 = 
(
	
	ss_source  = "rollout PickColorRollout \"Pick Color...\" width:191\n"
	ss_source += "(\n"
	ss_source += "	local picked_control\n"
	ss_source += "	local picked_color\n"
	ss_source += "\n"
	ss_source += "	fn applyColor control: value: =\n"
	ss_source += "	(\n"
	ss_source += "		format \"control: % value: %\\n\" control value\n"
	ss_source += "\n"
	ss_source += "		picked_control = control\n"
	ss_source += "		picked_color = value\n"
	ss_source += "	)\n"
	ss_source += "\n"
	ss_source += "--CONTROLS--\n"
	ss_source += ")\n"

	bt_pos = [4,4]
	bt_size = [21,22]
	bt_spacing = [1,2]

	cc_source  = "	colorpicker cp_xx fieldwidth:% height:% pos:(% + [%,%]) color:% modal:off\n"
	cc_source += "	on cp_xx changed val do applyColor control:cp_xx value:val\n"

	cc = ""
	for y=0 to dimY-1 do for x=0 to dimX-1 do
	(	
		ss = stringstream ""
		col = random white black
		format cc_source bt_size.x bt_size.y bt_pos (x * (bt_size.x + bt_spacing.x)) (y * (bt_size.y + bt_spacing.y)) col to:ss

		str = substitutestring ss "xx" (y as string + x as string)
		append cc str
	)

	rol_str = substitutestring ss_source "--CONTROLS--" cc

	execute rol_str
)
*/


fn makeRollout dimX:8 dimY:4 = 
(
	
	ss_source  = "rollout PickColorRollout \"Pick Color...\" width:191\n"
	ss_source += "(\n"
	ss_source += "	local picked_control\n"
	ss_source += "	local picked_color\n"
	ss_source += "\n"
	ss_source += "	local white_bmp = bitmap 1 1 color:white\n"
	ss_source += "	local black_bmp = bitmap 1 1 color:(black + 1)\n"
	ss_source += "\n"
	ss_source += "	fn applyColor control: value: =\n"
	ss_source += "	(\n"
	ss_source += "		format \"control: % value: %\\n\" control value\n"
	ss_source += "\n"
	ss_source += "		cc = PickColorRollout.controls\n"
	ss_source += "		k = finditem cc picked_control\n"
	ss_source += "		if k > 0 do cc[k-1].bitmap = black_bmp\n"
	ss_source += "		k = finditem cc control\n"
	ss_source += "		if picked_control != undefined do control\n"
	ss_source += "		cc[k-1].bitmap = white_bmp\n"
	ss_source += "\n"
	ss_source += "		c = getpixels control.bitmap [0,0] 1\n"
	ss_source += "		v = colorpickerdlg c[1] control.name\n"
	ss_source += "\n"
	ss_source += "		if v != undefined do\n"
	ss_source += "		(\n"
	ss_source += "			setpixels control.bitmap [0,0] #(v)\n"
	ss_source += "			control.bitmap = control.bitmap\n"
	ss_source += "		)\n"
	ss_source += "		picked_control = control\n"
	ss_source += "		picked_color = v\n"
	ss_source += "	)\n"
	ss_source += "\n"
	ss_source += "--CONTROLS--\n"
	ss_source += ")\n"

	bt_pos = [8,5]
	bt_size = [21,22]
	bt_spacing = [1,2]

	cc_source  = "	imgtag cp_xx_x width:% height:% pos:(% + [%,%]) bitmap:black_bmp enabled:off\n"
	cc_source += "	imgtag cp_xx width:(cp_xx_x.width-2) height:(cp_xx_x.height-2) pos:(cp_xx_x.pos + [1,1]) bitmap:(bitmap 1 1 color:%)\n"
	cc_source += "	on cp_xx mousedown do applyColor control:cp_xx value:val\n"

	cc = ""
	for y=0 to dimY-1 do for x=0 to dimX-1 do
	(	
		ss = stringstream ""
		col = random white black
		format cc_source bt_size.x bt_size.y bt_pos (x * (bt_size.x + bt_spacing.x)) (y * (bt_size.y + bt_spacing.y)) col to:ss

		str = substitutestring ss "xx" (y as string + x as string)
		append cc str
	)

	rol_str = substitutestring ss_source "--CONTROLS--" cc

	execute rol_str
)


try(destroydialog ::PickColorRollout) catch()

PickColorRollout = makeRollout dimX:8 dimY:8

createdialog PickColorRollout modal:on
format ">> control: % value: %\n" PickColorRollout.picked_control PickColorRollout.picked_color
0 Likes
Message 5 of 10

denisT.MaxDoctor
Advisor
Advisor

using second solution (imgtags) you can add destroyDialog in applyColor function if you want, but I don't see any sense for it.

0 Likes
Message 6 of 10

senorpablo
Advocate
Advocate

Thanks Denis!

 

It looks amazing. The palette part is perfect. It doesn't return the RGB values of the swatch that was clicked however.

 

I probably wasn't clear on this, but I don't need the max color picker at all. When a user clicks on the swatch, I need the RGB values returned, and the dialog to close. The dialog is for selecting an existing color from a fixed palette, not choosing arbitrary colors. 

 

I'll try and see if I can make this work tomorrow. 

0 Likes
Message 7 of 10

denisT.MaxDoctor
Advisor
Advisor

Oh! no colorpicking?! it makes everything so easy ... i will make the change tomorrow

0 Likes
Message 8 of 10

denisT.MaxDoctor
Advisor
Advisor
fn customColorPicker dimX:8 dimY:4 = 
(
	
	ss_source  = "rollout _custColorPicker \"Pick Color...\" width:191\n"
	ss_source += "(\n"
	ss_source += "	local picked_control\n"
	ss_source += "	local picked_color\n"
	ss_source += "\n"
	ss_source += "	local white_bmp = bitmap 1 1 color:white\n"
	ss_source += "	local black_bmp = bitmap 1 1 color:(black + 1)\n"
	ss_source += "\n"
	ss_source += "	fn prepickedColor control: =\n"
	ss_source += "	(\n"
	ss_source += "		cc = _custColorPicker.controls\n"
	ss_source += "		k = finditem cc control\n"
	ss_source += "		cc[k-1].bitmap = white_bmp\n"
	ss_source += "		picked_control = control\n"
	ss_source += "		picked_color = (getpixels control.bitmap [0,0] 1)[1]\n"
	ss_source += "	)\n"
	ss_source += "	fn pickedColor = destroyDialog _custColorPicker\n"
	ss_source += "\n"
	ss_source += "--CONTROLS--\n"
	ss_source += ")\n"

	bt_pos = [8,5]
	bt_size = [21,22]
	bt_spacing = [1,2]

	cc_source  = "	imgtag cp_xx_x width:% height:% pos:(% + [%,%]) bitmap:black_bmp enabled:off\n"
	cc_source += "	imgtag cp_xx width:(cp_xx_x.width-2) height:(cp_xx_x.height-2) pos:(cp_xx_x.pos + [1,1]) bitmap:(bitmap 1 1 color:%)\n"
	cc_source += "	on cp_xx mousedown do prepickedColor control:cp_xx\n"
	cc_source += "	on cp_xx click do pickedColor()\n"

	cc = ""
	for y=0 to dimY-1 do for x=0 to dimX-1 do
	(	
		ss = stringstream ""
		col = random white black
		format cc_source bt_size.x bt_size.y bt_pos (x * (bt_size.x + bt_spacing.x)) (y * (bt_size.y + bt_spacing.y)) col to:ss

		str = substitutestring ss "xx" (y as string + x as string)
		append cc str
	)

	rol_str = substitutestring ss_source "--CONTROLS--" cc

	rol = execute rol_str
	createdialog rol modal:on
		
	#(rol.picked_color, if rol.picked_control != undefined do (filterstring rol.picked_control.name "_")[2] as integer) 
)

col = customColorPicker dimX:8 dimY:8
format ">>>> picked color:% id:%\n" col[1] col[2]
0 Likes
Message 9 of 10

denisT.MaxDoctor
Advisor
Advisor
Accepted solution

here is a clearer version:

 

 

fn customColorPicker dimX:8 dimY:4 = 
(
	
	ss_source  = "rollout _custColorPicker \"Pick Color...\" width:191\n"
	ss_source += "(\n"
	ss_source += "	local over_control\n"
	ss_source += "	local picked_control\n"
	ss_source += "	local picked_color\n"
	ss_source += "\n"
	ss_source += "	local cc\n"
	ss_source += "\n"
	ss_source += "	local white_bmp = bitmap 1 1 color:white\n"
	ss_source += "	local black_bmp = bitmap 1 1 color:(black + 1)\n"
	ss_source += "\n"
	ss_source += "	fn mouseOverButton control: =\n"
	ss_source += "	(\n"
	ss_source += "		if over_control != control do\n"
	ss_source += "		(\n"
	ss_source += "			k = finditem cc over_control\n"
	ss_source += "			if k > 0 do cc[k-1].bitmap = black_bmp\n"
	ss_source += "			k = finditem cc control\n"
	ss_source += "			cc[k-1].bitmap = white_bmp\n"
	ss_source += "			over_control = control\n"
	ss_source += "		)\n"
	ss_source += "	)\n"
	ss_source += "	fn colorPicked control: =\n"
	ss_source += "	(\n"
	ss_source += "		picked_control = control\n"
	ss_source += "		picked_color = (getpixels control.bitmap [0,0] 1)[1]\n"
	ss_source += "		destroyDialog _custColorPicker\n"
	ss_source += "	)\n"
	ss_source += "\n"
	ss_source += "--CONTROLS--\n"
	ss_source += "	on _custColorPicker open do\n"
	ss_source += "	(\n"
	ss_source += "		cc = _custColorPicker.controls\n"
	ss_source += "	)\n"
	ss_source += ")\n"

	bt_pos = [8,5]
	bt_size = [21,22]
	bt_spacing = [1,2]

	cc_source  = "	imgtag cp_xx_x width:% height:% pos:(% + [%,%]) bitmap:black_bmp enabled:off\n"
	cc_source += "	imgtag cp_xx width:(cp_xx_x.width-2) height:(cp_xx_x.height-2) pos:(cp_xx_x.pos + [1,1]) bitmap:(bitmap 1 1 color:%)\n"
	cc_source += "	on cp_xx mouseover do mouseOverButton control:cp_xx\n"
	cc_source += "	on cp_xx click do colorPicked control:cp_xx\n"

	cc = ""
	for y=0 to dimY-1 do for x=0 to dimX-1 do
	(	
		ss = stringstream ""
		col = random white black
		format cc_source bt_size.x bt_size.y bt_pos (x * (bt_size.x + bt_spacing.x)) (y * (bt_size.y + bt_spacing.y)) col to:ss

		str = substitutestring ss "xx" (y as string + x as string)
		append cc str
	)

	rol_str = substitutestring ss_source "--CONTROLS--" cc

	rol = execute rol_str
	createdialog rol modal:on
		
	#(rol.picked_color, if rol.picked_control != undefined do (filterstring rol.picked_control.name "_")[2] as integer) 
)

col = customColorPicker dimX:8 dimY:8
format ">>>> picked color:% id:%\n" col[1] col[2]

 

 

of course you don't need "get color by bitmap" because you have to use a "button id" and know it's color

 

 

0 Likes
Message 10 of 10

senorpablo
Advocate
Advocate

Thank you Denis, it's fantastic. Always impressed how quickly you devise these elegant solutions.

 

0 Likes