Checking if a GUI button has been pushed in a room script. [SOLVED]

Started by rich, Sun 07/10/2007 04:24:41

Previous topic - Next topic

rich

I have no problem checking if a button in a GUI has been pressed in the Global Script. All I have to do is utilize the function interface_click and see if that particular button has been pressed of the GUI/interface. No problems there. But, I have a GUI which is used only in one room, so, I thought it would be cleaner to process it in the script of that room only. The global script is already cluttered. If I can avoid adding more there... the better. Here is where I run into problems.

Putting function interface_click in a room doesn't work like say putting function on_mouse_click in a room which processes automatically. So, that appears to be out. So, I thought I'd check if the button was pressed in the on_mouse_click function itself. Apparantly, there is no such thing as Button.GetAtScreenXY, so, I can't use that. GUIControl.GetAtScreenXY doesn't seem to actually work either. (I named my button with a scriptoname in the GUI pane and made sure it matched). So, I've run out of ideas.

Anyone have any idea of how to make this work? Or, am I stuck scripting this GUI in the global script?
I'm so excited!

Khris

You shouldn't use interface_click anymore, it's obsolete. Use button_Click instead. (Double click the GUI button or "On click" to have it generated.)

To move code to a room script, use CallRoomScript() and on_call().

Code: ags
// global script
function ButtonName_Click(...) {
  CallRoomScript(1);
}

// room script
function on_call(int p) {
  if (p==1) {
    // code here
  }
}


(Using GUIControl.GetAtScreenXY(int x, int y) should work in theory, so it's possible to detect the button click directly in the room script, but that's not recommended.)

rich

Okay, that works in one way... although it's still messier than I'd like, but that's okay.

One problem I still haveI have though... that the button_Click is not activated unless you press AND release the mouse button. That's normally fine. However, an on_mouse_click used with a standard GetAtScreenXY is activated when the mouse is pressed but not necessarilly released. Why would I want this?

In the case of a GUI, I'm trying to program it so if you click a certain part of the GUI and hold the mouse down, you can actually drag it around, until you release the mouse. I have all the code in place, except it doesn't seem to get activated using the standard GUI button_click until I release the mouse which does me no good. My code works with an inscreen object because the Object.GetAtScreenXY does work. But, GUIControl.GetAtScreenXY does NOT work.

So, have any more ideas?
I'm so excited!

Ashen

GUIControl.GetAtScreenXY and mouse.IsButtonPressed, used in repeatedly_execute.

Code: ags

int XOff = -1, YOff = -1; // Offsets for mouse position realtive to GUI

function repeatedly_execute() {
  if (mouse.IsButtonDown(eMouseLeft)) {
    if (GUIControl.GetAtScreenXY(mouse.x, mouse.y) == btnButton && XOff == -1) {
      // When left button is first pressed over 'btnButton', set X/YOff values
      XOff = mouse.x - btnButton.OwningGUI.X;
      YOff = mouse.y - btnButton.OwningGUI.Y;
    }
    else if (XOff != -1) btnButton.OwningGUI.SetPosition(mouse.x - XOff, mouse.y - YOff);
    // while left button is held, move GUI with mouse
  }
  else {
    // Reset X/YOff when left button is released.
    XOff = -1;
    YOff = -1;
  } 
}


It's probably not perfect (just thrown together), but it seems to work OK. (You can replace btnButton.OwningGUI with the actual GUI name, to make the code a little neater.)
I know what you're thinking ... Don't think that.

Khris

Quote from: rich on Mon 08/10/2007 00:15:24But, GUIControl.GetAtScreenXY does NOT work.
Yes, it does work fine.

I believe I found the mistake you've made.
Did you declare a repeatedly_execute function in the room script?
Instead of adding a RunScript-action to the room's "Repeatedly execute"-event?

A manually added rep_ex in a room script is simply ignored.

If you get the impression that a command doesn't work, make sure the function is called first.

rich

No, I didn't manually add rep_ex... I know that doesn't work.

I was checking the GUIControl.GetAtScreenXY inside the manually added on_mouse_press(). I still am not sure why it didn't work. I guess it doesn't matter now.

Regardless, Ashen's code there worked well. I just had to change it slightly for my purposes. I didn't need to use on_mouse_press() at all for this. Thanks for all the help guys. I guess it really ended up being two seperate questions. The powers that be can switch this one to Solved.
I'm so excited!

Khris

That would be you, just edit your first post.

Did you really use "on_mouse_press()"?
Because the function is called "on_mouse_click".

Radiant

Quote from: KhrisMUC on Mon 08/10/2007 03:24:52
A manually added rep_ex in a room script is simply ignored.
Yes, but a manually added repeatedly_execute_always should work just fine.

Quote
Code: ags

function ButtonName_Click(...) {
  CallRoomScript(1);
}

If you're going to do this a lot, I would suggest doing this instead:

Code: ags

// global HEADER
enum _stuff {
  GUICLICK = 1,
  STAND_ON_HEAD = 2,
  SOME_OTHER_EVENT = 3
};

// global SCRIPT
function ButtonName_Click(...) {
  CallRoomScript(GUICLICK);
}


Ashen

Quote
I was checking the GUIControl.GetAtScreenXY inside the manually added on_mouse_press(). I still am not sure why it didn't work.

As Khris said, the function is on_mouse_click (or on_key_press, which wouldn't be what you want), but that could just be a mistake in your post. Even with the right function name, it wouldn't work (as you discovered) because on_mouse_click isn't called for clicks over GUIs (and by extention GUI controls) - GUI clicks are handled internally by AGS as a seperate process (calling any associated control function). The exceptions are if the GUI is set to non-clickable*, and sometimes Inventory Items (but only if you've checked the 'Handle inventory clicks in script' option).

* You click through the GUI, as if it wasn't there, and if there isn't another GUI behind it on_mouse_click is run. However the GUI (and it's Controls) can't be detected by GetAtScreenXY, so it still wouldn't 'work' for you.
I know what you're thinking ... Don't think that.

rich

Yes, I meant to say on_mouse_click... that was just a typo before.

Well, I guess that explains it. on_mouse_click won't check for GUIs. Then, I guess it's a good thing that you can use IsMousePressed with GetAtScreenXY inside the repeatedly_execute. Thanks for the help, guys.
I'm so excited!

SMF spam blocked by CleanTalk