GUI activated function in module?

Started by Rocco, Wed 26/03/2008 00:04:34

Previous topic - Next topic

Rocco

I trying to make a module with user input - gui element.

but im struggling with the GUI activate function.
It seems that this function only works in the global script.
Is this true, and is there a possibility to make this function also callable in the module?

Khris

Afaik, no. (Workaround: check for GUI clicks in the module's rep_ex, then ClaimEvent())

Just do:
Code: ags
function whatever_onClick(...) {
  myModule.myFunction();
}


I'm sure you're going to publish the module together with the GUI, and the nice thing about GUIs is that if exported, their onClick functions end up in the file as well :)

naltimari

#2
In my modules, I check for GUI-related events:

Code: ags

function on_event(EventType event, int data)
{
  // check if user clicked inside myGui
  if (event == eEventGUIMouseUp && data == myGui.ID) {
    // check which control the user clicked onto
    GUIControl* ctrl = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
    if (ctrl != null) {
      // user clicked over 'ctrl'
    }
    else {
      // user clicked inside GUI, but not on a specific control
    }
  }
}


eEventGUIMouseUp is triggered when the mouse button is released. There's also eEventGUIMouseDown, which is triggered when the mouse button is pressed. I usually choose to respond to the click in MouseUp, so the user can 'cancel' the click if he releases the button outside of the control in which he clicked...

Hope this helps.

Rocco

thx, i will check your suggestions tomorrow.

problem is, there is no need for an mouse click event on this gui,
cause its a text input box, where the player have to type his name and finish with enter.

Khris

Just use the standard [textbox_name]_OnActivate function in the global script. From within that, call the module's function. The _OnActivate one will end up in the exported GUI.

naltimari

You can surely use Kris' suggestion, it will work and it is very straightforward.

However, if you are willing to keep all your code inside the module, which personally I find very interesting, there's a way. You must check for IsKeyPressed(13) on your module's rep_ex(), since on_key_press() won't get called whenever a textbox is present (and Enabled).

Code: ags

// this is the rep_ex that is inside your module
function repeatedly_execute()
{
  if (yourGUI.Visible && IsKeyPressed(13)) {
    // ok, the user has pressed 'ENTER' while your GUI was being shown;
    // provided you have only one text box inside your GUI, this check is enough
    // otherwise, you still have other things to check...
    ...
  }
}


Personally, I dont like the way text boxes are implemented. There are some issues that really should be addressed soon:


  • If you place more than one text box inside a GUI, all of them receive the keyboard events at the same time; this is certainly not what you might expect, and you must write code to handle this case (there are modules that help, though, like this one by SSH);

  • The text box does not 'wrap' text, so you can't have much text inside it; I had to write (well, I am still writing) code to handle this, because in my game there is a 'logbook' in which the user can enter long texts; My solution is to write a whole new 'textbox' based on two ordinary labels: one for the text and another one, on top of it, for the cursor.

Rocco

big thanks for your help, should be no problem to get the module to work now.  :)

khris suggestion works, the only little glitch is that i have to save a few variables global,
but this is no great problem.

@naltimare:
i think also its better to have all code in the module, so i will check your suggestion today.


SMF spam blocked by CleanTalk