Custom Restore – double click and enter support?

Started by Husky, Sun 09/07/2006 19:41:20

Previous topic - Next topic

Husky

I've made a custom Save GUI and a custom Restore GUI and they are both working great (thanks to examples in other people's past posts.)Ã,  Now, I'm trying to add a little extra finesse but I'm getting stumped.Ã,  Thus, I have 2 separate questions regarding my Restore GUI:

Question 1: How can I enable double clicking on the Restore List Box?

Details:Ã,  I am not using SelectionChanged to automatically restore the first game you click.Ã,  I have it set up so you first highlight the restore game you want, then you click a restore button to initiate the restoring.Ã,  (this is a style decision that I'm hoping to preserve)Ã,  However, when testing this out I notice that additionally I sometimes intuitively just want to double-click a restore game slot and have it just restore.Ã,  Is this possible â€" to register a double-click on a slot on a List Box?

I've been trying to modify the examples in this post, but I'm not having success.
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=12190.0

Here is the new code I've come up with, the good news is it doesn't crash, the bad news is it doesn't do anything either.

Code: ags

function on_mouse_click(MouseButton button) {
Ã,  // called when a mouse button is clicked. button is either LEFT or RIGHT
Ã,  if (IsGamePaused() == 1) {
Ã,  Ã,  // Game is paused, so do nothing (ie. don't allow mouse click)
}
Ã,  else if (button == eMouseLeft) {
Ã,  Ã,  GUIControl *theControl = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
Ã,  Ã,  Ã,  if (theControl == lboxLoad) {
Ã,  Ã,  Ã,  Ã,  int double_click;
Ã,  Ã,  Ã, //SetMouseCursor(GetCursorMode()); // this should stop the wait cursor from appearing however it is old version code so it needs to be updated to work.
Ã,  Ã,  Ã,  Ã,  double_click = WaitMouseKey(40)*Mouse.IsButtonDown(eMouseLeft);
Ã,  Ã,  Ã, //SetDefaultCursor(); // this should restore the cursor however  it is old version code so it needs to be updated to work.
Ã,  Ã,  Ã,  Ã,  Ã, if (double_click) {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  int LoadGame = lboxLoad.SelectedIndex;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  RestoreGameSlot(savegameindex[LoadGame]);
Ã,  Ã, }
}
Ã,  else {
Ã,  Ã,  ProcessClick(mouse.x, mouse.y, mouse.Mode );
Ã,  Ã, }
}

Thoughts?


Question 2:Ã,  How can I enable the “enter” key for my Restore GUI so it can initiate a restore on whatever game is highlighted?

I've been studying this post (the last two entries).Ã,  Ã, 
http://www.adventuregamestudio.co.uk/yabb/index.php?topic=25066.0
Here I learned how to add a similar functionality to my Save GUI.Ã,  When you press enter, it will save as whatever name is in the Text Box.Ã,  I was able to do this because a Text Box has an “Activate” function:

Code: ags

function tboxSave_Activate(GUIControl *control) {
Ã,  btnSaveSave_Click(btnSaveSave, eMouseLeft);Ã,  //Pressing enter triggers save game button
}


However, there is no Text Box on my Restore GUI, just a List Box, so the“activate” function is not available.Ã,  So, I've been trying to set this up in the On_Key_Press section of the Global Script.Ã,  I'm trying to get the enter key to trigger Restore Selected when the Restore GUI is open.Ã,  However, unfortunately, my code just triggers the following error:Ã,  parse error in expr near “gLoad”

Code: ags

if (keycode==13) {Ã,  // Enter Pressed
Ã,  if (gLoad.Visible=true) {Ã,  //Ã,  Restore selected game if Restore GUI is open
Ã,  Ã,  int LoadGame = lboxLoad.SelectedIndex;
Ã,  Ã,  RestoreGameSlot(savegameindex[LoadGame]);
Ã,  Ã, }
}


Any ideas on either question would be greatly, greatly appreciated.Ã,  Thank you in advance.

Ashen

QuoteHowever, there is no “activate” function in a listbox
Well, actually, there kind of is. It's the SelectionChanged function, that runs whenever, well, the selection is changed. This doesn't really help you with Q2, but it's the key to Q1.

What you'll need is an int declared outside of the function, which you use in the function to check which item has been selected. If it's already highlighted, you restore it - simple. I'd have been willing to swear it was actually covered in that thread you linked, or one like it, but apparently it isn't. The function would look something like (untested):

Code: ags

int PrevSelect; // Your int. Call it whatever you want

#sectionstart lboxLoad_SelectionChanged  // DO NOT EDIT OR REMOVE THIS LINE
function lboxLoad_SelectionChanged(GUIControl *control) {
 if (PrevSelect != lboxLoad.SelectedIndex) PrevSelect = lboxLoad.SelectedIndex;
 // If a NEW item is selected, change the int.
else RestoreGameSlot(savegameindex[PrevSelect]);
 // If the same item is clicked again, load it.
}
#sectionend lboxLoad_SelectionChanged  // DO NOT EDIT OR REMOVE THIS LINE


For question 2 - what if you add a TextBox object, but use script to set it's postion to outside the limits of the GUI - making it invisible, but still running the Activate function? However:
Quoteparse error in expr near “gLoad”

I'm guessing it's this line:
Code: ags
if(gLoad.Visible=true)


Probably just a typo, but remember == to check variables, = to set them. (i.e. It should be if(gLoad.Visible==true)). (You might need to move the condition BEFORE the if (IsGamePaused... condition in on_key_press.)

EDIT:
Fixed PrevSelect(ed) to all be same. And well spotted - I'd forgotten the first item will be selected by default.
I know what you're thinking ... Don't think that.

Husky

Ashen, thank you very much for the reply.

Regarding the answer to Question 1
Ã,  Ã,  Ã, Excellent!Ã,  Perfect solution.Ã,  I declared the int at the way top of the Global Script â€" and it works fine there.Ã,  The int in the untested code was sometimes called “PrevSelect” and sometimes called “PrevSelected”, so I just made sure they were the same in the final.
Ã,  Ã,  Ã, Additionally, I took things one step farther.Ã,  The top item was still responding to a single click because it is pre-selected when you open the Restore GUI.Ã,  To get the top item to wait for the second click, I put this code in the place where I open the GUI.

Code: ags

function btnMenuRestore_Click(GUIControl *control, MouseButton button) {
Ã, Ã,  Ã, lboxLoad.FillSaveGameList();		
Ã,  Ã,  if (PrevSelected == 0) PrevSelected = -1;Ã,  // makes sure top item needs 2 clicks to restore
Ã,  Ã,  gLoad.Visible=true;
Ã,  Ã, }
}


Regarding Ashen's answer to Question 2
Ã,  Ã,  Ã, Excellent again!Ã,  I put a Text Box in the Restore GUI and hid it off to the side.Ã,  I used the same code in the Restore Text Box's Activate function as I did before with the Save Text Box's Activate function.Ã,  Works like a charm.

Regarding the parse error in expr near “gLoad”
Ã,  Ã,  Ã, Yes, you spotted it, it was a typo, = instead of ==.

Thanks again Ashen, I appreciate it.

SMF spam blocked by CleanTalk