PDA

View Full Version : While myButton pressed do()........Help!


josiah
11-08-2004, 12:28 AM
While myButton pressed do()

I know this doesnt work with this event handler, but it illustrates what I want to do. Can this be done? The only event for a button is "pressed". Is there a way you can check the state of a button?

Bobo
11-08-2004, 12:38 AM
While myButton pressed do()

I know this doesnt work with this event handler, but it illustrates what I want to do. Can this be done? The only event for a button is "pressed". Is there a way you can check the state of a button?
Not directly, but if the button is in a dialog, you could employ the dialog's own handlers that give you mouse position and left, middle and right buttons pressed and released events.
So you could know whether the mouse is over the respective button and whether its buttons are pressed or released. Theoretically, you could go without a button at all - you could use ANY UI control, bitmap, whatever and check its boundaries against the mouse position and buttons.

josiah
11-08-2004, 12:59 AM
Genius! Thanks Bobo!

josiah
11-08-2004, 06:11 PM
Darn it anyway, I can't seem to get it to work that way either.

rollout test_roll "Test Dia"
(
local MyFlag = 0
on test_roll lbuttondown value do
(
MyFlag = 1
while MyFlag != 0 do
(
on test_roll lbuttonup value do ( MyFlag = 0 )
)
)
)
createDialog test_roll width:100 height:30
registerViewWindow test_roll


I cant seem to nest the event handler in the while loop as shown here, and if I put it outside
of the while loop, flow gets trapped in the while loop and the "on Lbuttonup" event is never
evaluated.

Zbuffer
11-12-2004, 02:34 PM
Hi,

how about a checkbutton ? (it has a .state property)

josiah
11-12-2004, 09:39 PM
I couldn't get that to work either, locks max up. I guess the only way to do something like
this is with a spinner with a zero size field. Check its value each time it changes, and reset it
when it approches its limit. Not very elegant, especially if you only need to go one way.
Thanks for the suggestion though.

Example;

rollout testRoll ""
(
spinner moveMe "Move Selected " range:[-1000,1000,0] fieldWidth:0
local oldValue = 0
on moveMe changed value do
(
if value > oldvalue then
(
move $ [0,0,1]
)
if value < oldvalue then
(
move $ [0,0,-1]
)
if (value > 990) or (value < -990) then (moveMe.value = 0)
oldvalue = value
)
)
test_float = NewRolloutFloater "" 125 150
addrollout testRoll test_float

Bobo
11-12-2004, 10:16 PM
Darn it anyway, I can't seem to get it to work that way either.

I cant seem to nest the event handler in the while loop as shown here, and if I put it outside
of the while loop, flow gets trapped in the while loop and the "on Lbuttonup" event is never
evaluated. Dude, why do you need that WHILE inside the code?!? It will repeat forever!

rollout test_roll "Test Dia"
(
local MyFlag = 0
label outputText "Ready."
on test_roll lbuttondown value do
(
MyFlag = 1
)
on test_roll lbuttonup value do
(
MyFlag = 0
)
on test_roll mousemove pos do
(
case MyFlag of
(
0: outputText.text = "LBM Released"
1: outputText.text = "LMB Pressed"
)
)
)
createDialog test_roll width:200 height:100
registerViewWindow test_roll

Bobo
11-12-2004, 10:30 PM
Here is a slightly more complex test:


rollout test_roll "Test Dia"
(
local MyFlag = #none
label outputText "Ready."
groupbox test_field "Group" pos:[50,50] height:30 width:100
fn insideTheBox pos = ( pos.x <= 150 and pos.x > 50 and pos.y <= 80 and pos.y >= 50 )

on test_roll lbuttondown pos do
(
if insideTheBox pos then
MyFlag = #pressedInsideBox
else
MyFlag = #pressedOutsideBox

)
on test_roll lbuttonup pos do
(
if insideTheBox pos then
MyFlag = #releasedInsideBox
else
MyFlag = #releasedOutsideBox
)
on test_roll mousemove pos do
(
case MyFlag of
(
default: if insideTheBox pos then txt = "Moving Inside..." else txt = "Moving Outside..."
#pressedInsideBox: txt = "LBM Pressed Inside The Box"
#pressedOutsideBox: txt = "LBM Pressed OUTSIDE The Box"
#releasedInsideBox: txt = "LBM Released Inside The Box"
#releasedOutsideBox: txt = "LBM Released OUTSIDE The Box"
)
outputText.text = txt + " Pos:" + pos as string
)
on test_roll rbuttondown pos do MyFlag = #none
)
createDialog test_roll width:300 height:100
registerViewWindow test_roll


Move the mouse in the rollout - the label will tell you whether you are inside or outside the box and where exactly.
Press the Left mouse button - it will tell you whether you pressed while outside or inside.
Release the Left mouse button - it will tell you whether you released while being outside or inside. You could use this to decide whether to do something - if both press and release were inside, do something. If not, you could simulate drag and drop etc.
Right-click to reset the display to show you the Moving... message.

This could be expanded to deal with dozens of "hot spots" and various combinations like dragging and dropping from one field into the other...

Bobo
11-12-2004, 10:41 PM
Ok, this one is even more fun!


rollout test_roll "Test Dia"
(
local MyFlag = #none
label outputText "Ready."
groupbox test_field1 "Group 1" pos:[50,50] height:30 width:100
groupbox test_field2 "Group 2" pos:[180,50] height:30 width:100
label test_label1 "Gold" pos:[70,65]
label test_label2 "" pos:[200,65]

fn insideTheBox pos which =
(
case which of
(
1: pos.x <= 150 and pos.x > 50 and pos.y <= 80 and pos.y >= 50
2: pos.x <= 280 and pos.x > 180 and pos.y <= 80 and pos.y >= 50
)
)

on test_roll lbuttondown pos do
(
if insideTheBox pos 1 then
(
MyFlag = #pressedInsideBox1
outputText.text = "Dragging from Group 1"
)
if insideTheBox pos 2 then
(
MyFlag = #pressedInsideBox2
outputText.text = "Dragging from Group 2"
)
)
on test_roll lbuttonup pos do
(
if insideTheBox pos 1 and MyFlag == #pressedInsideBox2 then
(
test_label1.text = test_label2.text
test_label2.text = ""
outputText.text = "Dropping from Group 2 into Group 1"
)
else
(
if insideTheBox pos 2 and MyFlag == #pressedInsideBox1 then
(
test_label2.text = test_label1.text
test_label1.text = ""
outputText.text = "Dropping from Group 1 into Group 2"
)
else
(
MyFlag = #none
outputText.text = "Illegal Dropping!"
)
)
)
)
createDialog test_roll width:300 height:100


You can drag the gold from the one box into the other. You cannot drop it into the same box our outside the box. If you drag from the empty into the full box, you will become poor instantly! :)

josiah
11-13-2004, 01:27 AM
Thanks Bobo. The while loop is in there because I want the function to loop as long as the
button is held down, not just once, and exit when the button is up. So far no luck, even with
a dialogs ability to track the mouse button presses and location.

Thank you for all these wonderful dialog examples, they really have opened new ideas for me!

Bobo
11-13-2004, 06:44 PM
Thanks Bobo. The while loop is in there because I want the function to loop as long as the
button is held down, not just once, and exit when the button is up. So far no luck, even with
a dialogs ability to track the mouse button presses and location.

Thank you for all these wonderful dialog examples, they really have opened new ideas for me!
A while loop locks MAXScript, it is not executed WHILE Max is running, it is executed exclusively and releases controls to Max when it is done (in your case - never).
The code to be executed while the button is pressed should be in something like a Timer UI control's handler which ticks once every few ms and only does something when the flag is set.

Alternatively, you could enable and disable the timer, but if you have lots of different flag states, it might be a bad idea.

here is an example - the counter theCounter is incremented 100 times per second (10 ms interval) if the left mouse button is pressed over the dialog:

rollout test_roll "Test Dia"
(
local MyFlag = 0
local theCounter = 0
label outputText ""
timer theTimer active:true interval:10
on test_roll lbuttondown value do MyFlag = 1
on test_roll lbuttonup value do MyFlag = 0
on theTimer tick do
(
case MyFlag of
(
0: outputText.text = (theCounter = 0) as string
1: outputText.text = (theCounter += 1) as string
)
)
)
createDialog test_roll width:200 height:100

josiah
11-13-2004, 07:55 PM
WOW! Thats Amazing! It works! I have been pulling hair out on this for a while, kinda has become an obsession! Thanks Bobo!

For asthetic reasons, I would like to use a button. However, since a button isnt part of the client area of the dialog, you can't get the status of the mouse buttons with lbuttondown, over a button right?

Bobo
11-13-2004, 10:30 PM
WOW! Thats Amazing! It works! I have been pulling hair out on this for a while, kinda has become an obsession! Thanks Bobo!

For asthetic reasons, I would like to use a button. However, since a button isnt part of the client area of the dialog, you can't get the status of the mouse buttons with lbuttondown, over a button right?

Right.
But you could put a bitmap in the background of the dialog with all the buttons you want DRAWN in it.
Or you can use a large bitmap UI control (see my TreeMatoGraph which uses a large bitmap and draws icons dynamically as you use the mouse).
Ot you could use a separate bitmap UI control for each "button" and swap the content of the bitmap based on the mouse state...

Be creative!

Cheers,
Bobo

josiah
11-14-2004, 12:08 AM
I like the switching out bitmaps idea! I believe that is what happens graphically when you use
a button anyway.

BIG Thanks Bobo! I really appreciate you sharing your knowledge and time in providing me
with these maxscript solutions.

CGTalk Moderation
01-19-2006, 11:00 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.