Modal/Pausing GUI

Started by Calin Leafshade, Tue 08/05/2012 04:04:40

Previous topic - Next topic

Calin Leafshade

Anyone any ideas for easily and sensibly implementing modal GUIs?

Ideally i'd like to do this:

Code: ags

Display("Well hi there! Click Ok on the next GUI you see!");
WaitWhileWeShowGUIModally();
Display("You clicked OK! Good for you!");


This keeps it neat by keeping the code inline and not fucking with program flow.

However, GUI Buttons can't be clicked while the game is blocked so i guess i'd have to check mouse position and stuff manually in a wait loop?

Is that how you guys would do it?

Sephiroth

#1
I needed something like that for a dialog gui, if I'm not misunderstanding it should be similar to your idea, and this is how I did it, although it's not really neat/tidy imho.
This code is for a listbox with up to three entries, acting as a dialog choice system so you can use it like this in the end:

int result = GetDialogChoice(); //wait until you click on one of the dialog options and return its id

Code: ags

//GuiDialogChoice - the Gui
//ChoicesBox - the Listbox

//global variable
  int DialogChoice;
 
//repeatedly execute always
 if(GuiDialogChoice.Visible)
 {
  int base_height = ChoicesBox.Height/3;
  if(mouse.x > GuiDialogChoice.X + ChoicesBox.X &&
     mouse.x < GuiDialogChoice.X + ChoicesBox.X + ChoicesBox.Width)
  {
    if(mouse.y > GuiDialogChoice.Y + ChoicesBox.Y &&
       mouse.y < GuiDialogChoice.Y + ChoicesBox.Y + base_height)
    {
      ChoicesBox.SelectedIndex=0;
      if(mouse.IsButtonDown(eMouseLeft))
        DialogChoice = 1;
    }
    else if(mouse.y > GuiDialogChoice.Y + ChoicesBox.Y + base_height &&
            mouse.y < GuiDialogChoice.Y + ChoicesBox.Y + (base_height *2) &&
            ChoicesBox.ItemCount > 1)
    {
      ChoicesBox.SelectedIndex=1;
      if(mouse.IsButtonDown(eMouseLeft))
        DialogChoice = 2;
    }
    else if(mouse.y > GuiDialogChoice.Y + ChoicesBox.Y + (base_height*2) &&
            mouse.y < GuiDialogChoice.Y + ChoicesBox.Y + (base_height*3) &&
            ChoicesBox.ItemCount > 2)
    {
      ChoicesBox.SelectedIndex=2;
      if(mouse.IsButtonDown(eMouseLeft))
        DialogChoice = 3;
    }
  }
 }
  

//the blocking function
int GetDialogChoice()
{
  DialogChoice = 0;
  GuiDialogChoice.Visible = true;

  while(!DialogChoice)
    Wait(GetGameSpeed()/10);

  GuiDialogChoice.Visible = false;
  return DialogChoice;
}


But then you will have to handle all the other events in rep_ex_always when your function is blocking.

Calin Leafshade

Thats probably the way I would do it, except i'd put all the checking inside the GetResult function like this:

pseudo:
Code: ags


int GetResult()
{

  int result = -1;
  while (result < 0)
  {
     if (GUIControlUnderMouse == btnOK && IsMouseDown(eLeft))  //repeat for other buttons
     {
        result = 1
     }
     Wait(1)

  }
  return result;

}



I just wondered if anyone else had come up with something a little more elegant.

SMF spam blocked by CleanTalk