«MultiButtonView» by Dindoléon

on 15 Aug'21 13:04 in guitoolinterfacemenu

Demo of a custom modular menu, allowing you to switch between several buttons instances. Connect to a function using view.bindFunction( function ). Supports layout integration, colors customisation, orientation, states and current state reassignment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
(

var win = Window( "MultiButtonView", Rect( 0, 300, 600, 150 ) );

var multi_button_view;

// State = [ "Displayed Text", Text Color if selected, Border Color if selected, BG Color if selected,
// Text Color if unselected, Border Color if unselected, BG Color if unselected ]
var multi_button_view_states = [
	[ "Option 1", Color.black, Color.red, Color.red, Color.white, Color.white, Color.black ],
	[ "Option 2", Color.black, Color.red, Color.green, Color.white, Color.white, Color( 0.2, 0.2, 0.2 ) ],
	[ "Option 3", Color.black, Color.red, Color.blue, Color.white, Color.white, Color.black ]
].asList;

var demo_view = UserView();
var demo_string = "";
var demo_function;

~get_multi_button_view = { | states_array, default_state, orientation, font, has_inside_margin |

	var multi_button_view = UserView();
	var margin_min_max = Point( 2, 10 );
	var current_state = default_state;
	var states = states_array;
	var states_number = states.size;
	var binded_function = nil;

	multi_button_view.background_( Color.black );
	multi_button_view.drawFunc = { | view |

		var margin;
		var color_border_ratio = 0.5;
		var button_size;

		if( orientation == \horizontal, {
			margin = view.bounds.height * 0.02;
		}, {
			margin = view.bounds.width * 0.02;
		} );

		if( margin < margin_min_max.x, { margin = margin_min_max.x } );
		if( margin > margin_min_max.y, { margin = margin_min_max.y } );

		if( orientation == \horizontal, {
			button_size = view.bounds.width - ( margin * 2 ) - ( margin * ( states.size - 1 ) ) / states.size;
		}, {
			button_size = view.bounds.height - ( margin * 2 ) - ( margin * ( states.size - 1 ) ) / states.size;
		} );

		states.do( { | state, index |

			var string_rect;
			var string_color;

			if( current_state == index, {
				Pen.fillColor_( state[ 2 ] )
			}, {
				Pen.fillColor_( state[ 5 ] )
			} );

			if( orientation == \horizontal, {
				Pen.fillRect(
					Rect(
						margin + ( index * ( button_size + margin ) ),
						margin,
						button_size,
						view.bounds.height - ( margin * 2 )
					)
				)
			}, {
				Pen.fillRect(
					Rect(
						margin,
						margin + ( index * ( button_size + margin ) ),
						view.bounds.width - ( margin * 2 ),
						button_size
					)
				)
			} ); // End of first border

			if( current_state == index, {
				Pen.fillColor_(
					Color(
						state[ 2 ].red * color_border_ratio,
						state[ 2 ].green * color_border_ratio,
						state[ 2 ].blue * color_border_ratio,
					)
				)
			}, {
				Pen.fillColor_(
					Color(
						state[ 5 ].red * color_border_ratio,
						state[ 5 ].green * color_border_ratio,
						state[ 5 ].blue * color_border_ratio,
					)
				)
			} );

			if( orientation == \horizontal, {
				Pen.fillRect(
					Rect(
						margin * 1.5 + ( index * ( button_size + margin ) ),
						margin * 1.5,
						button_size - margin,
						view.bounds.height - ( margin * 3 )
					)
				)
			}, {
				Pen.fillRect(
					Rect(
						margin * 1.5,
						margin * 1.5 + ( index * ( button_size + margin ) ),
						view.bounds.width - ( margin * 3 ),
						button_size - margin
					)
				)
			} ); // End of second border

			if( has_inside_margin, {
				Pen.fillColor_( Color.black )
			}, {
				if( current_state == index, {
					Pen.fillColor_( state[ 3 ] )
				}, {
					Pen.fillColor_( state[ 6 ] )
				} );
			} );

			if( orientation == \horizontal, {
				Pen.fillRect(
					Rect(
						margin * 2 + ( index * ( button_size + margin ) ),
						margin * 2,
						button_size - ( margin * 2 ),
						view.bounds.height - ( margin * 4 )
					)
				)
			}, {
				Pen.fillRect(
					Rect(
						margin * 2,
						margin * 2 + ( index * ( button_size + margin ) ),
						view.bounds.width - ( margin * 4 ),
						button_size - ( margin * 2 )
					)
				)
			} ); // End of third fill

			if( has_inside_margin, {
				if( current_state == index, {
					Pen.fillColor_( state[ 3 ] )
				}, {
					Pen.fillColor_(	state[ 6 ] )
				} );

				if( orientation == \horizontal, {
					Pen.fillRect(
						Rect(
							margin * 3 + ( index * ( button_size + margin ) ),
							margin * 3,
							button_size - ( margin * 4 ),
							view.bounds.height - ( margin * 6 )
						)
					)
				}, {
					Pen.fillRect(
						Rect(
							margin * 3,
							margin * 3 + ( index * ( button_size + margin ) ),
							view.bounds.width - ( margin * 6 ),
							button_size - ( margin * 4 )
						)
					)
				} );
			} ); // End of background if has_inside_margin

			if( orientation == \horizontal, {
				string_rect = Rect(
					margin + ( index * ( button_size + margin ) ),
					margin,
					button_size,
					view.bounds.height - ( margin * 2 )
				)
			}, {
				string_rect = Rect(
					margin,
					margin + ( index * ( button_size + margin ) ),
					view.bounds.width - ( margin * 2 ),
					button_size
				)
			} );

			if( current_state == index, {
				string_color = state[1]
			}, {
				string_color = state[4]
			} );

			Pen.stringCenteredIn(
				state[0],
				string_rect,
				font,
				string_color
			);
		} );
	}; // End of drawFunc

	multi_button_view.mouseDownAction = { | view, x, y |

		var new_state;
		if( orientation == \horizontal, {
			new_state = x.linlin( 0, view.bounds.width, 0, states_number ).asInt;
		}, {
			new_state = y.linlin( 0, view.bounds.height, 0, states_number ).asInt;
		} );
		if( new_state != current_state, {
			current_state = new_state;
			if( binded_function != nil, { binded_function.value( current_state ) } );
			view.refresh;
		} );
	};

	// Custom methods :
	multi_button_view.addUniqueMethod( \bindFunction, { | object, function |
		binded_function = function
	} );

	multi_button_view.addUniqueMethod( \updateStates, { | object, new_states_array |
		states = new_states_array;
		states_number = states.size;
		if( current_state > states_number, { current_state = 0 } );
		multi_button_view.refresh;
	} );

	multi_button_view.addUniqueMethod( \setState, { | object, new_state |
		if( ( ( new_state >= 0 ) && ( new_state <= states_number ) ), {
			current_state = new_state;
			multi_button_view.refresh;
		} );
	} );

	multi_button_view.addUniqueMethod( \valueState, { | object, new_state |
		if( ( ( new_state >= 0 ) && ( new_state <= states_number ) ), {
			current_state = new_state;
			if( binded_function != nil, { binded_function.value( current_state ) } );
			multi_button_view.refresh;
		} );
	} );

	multi_button_view
};

multi_button_view = ~get_multi_button_view.value( multi_button_view_states, 0, \horizontal, Font.default, true );
multi_button_view_states.add( [ "Option 4", Color.black, Color.red, Color.cyan, Color.white, Color.white, Color( 0.2, 0.2, 0.2 ) ] );
multi_button_view.updateStates( multi_button_view_states );

demo_view.drawFunc = { | view |
	Pen.stringCenteredIn(
		demo_string,
		Rect(
			0,
			0,
			view.bounds.width,
			view.bounds.height
		)
	)
};

demo_function = { | index |
	demo_string = "You picked " ++ multi_button_view_states[ index ][ 0 ];
	demo_view.refresh
};

multi_button_view.bindFunction( demo_function );
multi_button_view.valueState( 2 );

win.layout_(
	VLayout(
		[ multi_button_view, stretch: 2 ],
		[ demo_view, stretch: 1 ]
	)
);
win.front;

)
raw 7420 chars (focus & ctrl+a+c to copy)
reception
comments