Hi. Im trying to manually call a button click function by pressing the enter key, but for some reason i dont get it working.
Also Id like to know what "GUI Control *control" parameter exactly does and what I should write at that section.
Im quite tired now so I hope Im not just missing something stupid, but I couldnt find clear answer from search or manual.
I have this in "on_key_press" function:
if (keycode == eKeyReturn && gDevelop.Visible == true && TxtBoxDevelop.Enabled == true) BtnDevelopOK_OnClick(BtnDevelopOK, eMouseLeft);
And this function somewhere above it:
function BtnDevelopOK_OnClick(GUIControl *control, MouseButton button)
{
...Some code...
}
I have tried this with and without putting the "OnClick" function to import and export (Do I need to?). And tried also without "gDevelop.Visible == true && TxtBoxDevelop.Enabled == true" inside if section, but none of those didnt work.
Did you try ProcessClick (int x, int y, CursorMode) ?
Quote from: Billbis on Thu 07/02/2013 20:25:43
Did you try ProcessClick (int x, int y, CursorMode) ?
I saw that function while I was looking for answer and I guess it would work too. But from what I red for example here (http://www.adventuregamestudio.co.uk/forums/index.php?topic=27400.msg348127#msg348127), I understood that it should be possible to do by calling the "OnClick" function and Id like to use that way if just possible, because it just seems more "right" way to do it.
@Billbis: Unfortunatley I was just looking that up, apparently it ignores all interfaces, which includes the buttons on them.
@TMuh: I don't know whether there is another way of calling that function, but what you could do is take the code in the BtnDevelopOK_OnClick function, and put it in another function which both instances can call. Like:
function Do_Stuff()
{
...Some code...
}
//Then later on
function BtnDevelopOK_OnClick(GUIControl *control, MouseButton button)
{
Do_Stuff()
}
//Then in on_key_press()
if (keycode == eKeyReturn && gDevelop.Visible == true && TxtBoxDevelop.Enabled == true) Do_Stuff();
I don't know if there are other ways to simulate button presses, but that should do it.
Hope that helps!
Quote from: geork@Billbis: Unfortunatley I was just looking that up, apparently it ignores all interfaces, which includes the buttons on them.
Oups, now I feel stupid. :X
I should RTFM before giving bad advises.
Quote from: TMuh on Thu 07/02/2013 19:52:24
I have this in "on_key_press" function:
if (keycode == eKeyReturn && gDevelop.Visible == true && TxtBoxDevelop.Enabled == true) BtnDevelopOK_OnClick(BtnDevelopOK, eMouseLeft);
And this function somewhere above it:
function BtnDevelopOK_OnClick(GUIControl *control, MouseButton button)
{
...Some code...
}
I have tried this with and without putting the "OnClick" function to import and export (Do I need to?). And tried also without "gDevelop.Visible == true && TxtBoxDevelop.Enabled == true" inside if section, but none of those didnt work.
Hi TMuh.
The code you wrote there is pretty much OK, and should work. The event handlers (like OnClick) are exactly same functions as all others, and should be callable from other function too. If it doesn't work, there's something else here.
What exactly the problem is: the code does not compile, or fail to run?
What code do you have inside "OnClick" function? What will happen if you put some "Display" call in the beginning of OnClick, showing test message?
Also, what other code do you have in "on_key_press"? In other modules maybe (if you have any)? Is the Return key is being handled earlier in the function somehow?
PS. You don't need to import/export unless you are making calls from other modules.
Quote from: TMuh on Thu 07/02/2013 19:52:24
Also Id like to know what "GUI Control *control" parameter exactly does and what I should write at that section.
This is a pointer for control related to event. In this case - it should be a pointer to button being pressed. This parameter is not used all the time, but usually in the cases when one event handler function is connected with numerous buttons - this way you will know what button is pressed exactly. If only one button calls this function, you may just use buttons name.
TMuh:
You don't need an OK button nor to script the handling of the enter keypress; if you press enter while a textbox is active, its OnActivate function gets called.
Just double-click the textbox or create/link the function in its events pane, like you would with a button.
The reason I suspect why what you tried doesn't work:
If the GUI's visibility is set to "pause game when shown", the default on_key_press function will exit at about midway (due to the game being paused), so if you added your line near the end, it never got processed in the first place.
Btw, you used the GUIControl* control parameter exactly as it's supposed to be used; if the developer decides to use one function to handle multiple GUI events, they can determine the control that was clicked using that parameter.
Thank you all.
Quote from: geork on Thu 07/02/2013 20:35:32
Hope that helps!
Seems like a good idea. Thanks.
Quote from: Khris on Thu 07/02/2013 21:05:07
You don't need an OK button nor to script the handling of the enter keypress; if you press enter while a textbox is active, its OnActivate function gets called.
Good point, I guess I could do this also without the button. But Im still interested why the original code didnt work.
Quote from: Khris on Thu 07/02/2013 21:05:07
The reason I suspect why what you tried doesn't work:
If the GUI's visibility is set to "pause game when shown", the default on_key_press function will exit at about midway (due to the game being paused), so if you added your line near the end, it never got processed in the first place.
GUI's visibility is set to "Normal, initially off". At the point where enter key is pressed GUI is visible.
Quote from: Crimson Wizard on Thu 07/02/2013 20:57:50
What exactly the problem is: the code does not compile, or fail to run?
Sry, forgot to mention that. So the code compiles just fine, but the problem is that game seems to ignore the function call. (if (keycode == eKeyReturn) player.Say("Hello.")) worked right.
Quote from: Crimson Wizard on Thu 07/02/2013 20:57:50
What code do you have inside "OnClick" function? What will happen if you put some "Display" call in the beginning of OnClick, showing test message?
On click works when I click the button with mouse (Also displays the test message) but when I press enter, nothing happens.
BtnDevelopOK is in gDevelop gui and it processes different actions depending the value of DevelopGUI variable. There is a textbox inside this gui and when you press the OK button, it converts Textbox.Text to Integer.
Code:
function BtnDevelopOK_OnClick(GUIControl *control, MouseButton button)
{
if (DevelopGUI == 1) {
Cash = TxtBoxDevelop.Text.AsInt;
LblDevelop.Text = ("Done!");
DevelopGUI = 0;
TxtBoxDevelop.Text = "";
TxtBoxDevelop.Enabled = false;
SetUpInventory();
}
else if (DevelopGUI == 2) {
if (TxtBoxDevelop.Text.AsInt < 1 || TxtBoxDevelop.Text.AsInt > 639) DevelopX = player.x;
else DevelopX = TxtBoxDevelop.Text.AsInt;
LblDevelop.Text = "Set Y (0 to keep current Y):";
DevelopGUI = 3;
TxtBoxDevelop.Text = "";
}
else if (DevelopGUI == 3) {
if (TxtBoxDevelop.Text.AsInt < 20 || TxtBoxDevelop.Text.AsInt > 479) DevelopY = player.y;
else DevelopY = TxtBoxDevelop.Text.AsInt;
LblDevelop.Text = "Set room:";
DevelopGUI = 4;
TxtBoxDevelop.Text = "";
}
else if (DevelopGUI == 4) {
if (TxtBoxDevelop.Text.AsInt > 0 || TxtBoxDevelop.Text.AsInt < 4) {
LblDevelop.Text = "";
DevelopGUI = 0;
player.ChangeRoom(TxtBoxDevelop.Text.AsInt, DevelopX, DevelopY);
TxtBoxDevelop.Text = "";
TxtBoxDevelop.Enabled = false;
}
}
}
Quote from: Crimson Wizard on Thu 07/02/2013 20:57:50
Also, what other code do you have in "on_key_press"? In other modules maybe (if you have any)? Is the Return key is being handled earlier in the function somehow?
Here is the on_key_press (No other modules there):
function on_key_press(eKeyCode keycode)
{
if (IsGamePaused()) keycode = 0; // game paused, so don't react to keypresses
if (keycode == eKeyCtrlQ) QuitGame(1); // Ctrl-Q
if (keycode == eKeyF9) RestartGame(); // F9
if (keycode == eKeyF12) SaveScreenShot("scrnshot.pcx"); // F12
if (keycode == eKeyCtrlS) Debug(0,0); // Ctrl-S, give all inventory
if (keycode == eKeyCtrlV) Debug(1,0); // Ctrl-V, version
if (keycode == eKeyCtrlA) Debug(2,0); // Ctrl-A, show walkable areas
if (keycode == eKeyCtrlX) Debug(3,0); // Ctrl-X, teleport to room
if (keycode == eKeyX && gDevelop.Visible == false) {gDevelop.Visible = true; DevelopGUI = 0;TxtBoxDevelop.Enabled = false;}
else if (keycode == eKeyX && gDevelop.Visible == true) gDevelop.Visible = false;
if (keycode == eKeyReturn && gDevelop.Visible == true && TxtBoxDevelop.Enabled == true) BtnDevelopOK_OnClick(BtnDevelopOK, eMouseLeft);
if (keycode == eKeyI) {
if (gInventory.Visible == true) CloseInventory();
else {
SetUpInventory();
LastMouseMode = mouse.Mode;
mouse.Mode = eModeWalk;
mouse.DisableMode(eModeInteract);
mouse.DisableMode(eModeTalkto);
LblInvInfo.Text = "";
gInventory.Visible = true;
}
}
}
Quote from: Khris on Thu 07/02/2013 21:05:07
You don't need an OK button nor to script the handling of the enter keypress; if you press enter while a textbox is active, its OnActivate function gets called.
Just double-click the textbox or create/link the function in its events pane, like you would with a button.
Okey, my bad. Didnt read this carefully enought. The original script didnt work because OnActivate function overrides the on_key_press function. Now its working, when I moved the code to the right place.
Thanks.