[SUGGESTION] Intercepting variable character width in text field bounds

Started by subspark, Fri 26/02/2010 12:53:58

Previous topic - Next topic

subspark

We have a text field that is a certain width that it only allows letters that dont exceed a 2 pixel gap from the border of our text box. We have a blinking cursor that we want to turn off on the last character stroke.

Is there anyway we can intercept the function that decides when to cull a character based on its relative width? For instance an "M" will be culled where an "N" will not.

Thanks in advance,
Sparky.

RickJ

The on_key_press() event handler won't be activated if there is an active  text box so you will have to use repeatedly execute instead.

I just had an interesting thought though ... why not use a button instead of a text box?  You could use the on_key_press() event to accept user data and send it to which ever "input" had focus.  

Oh yeah! it would be possible to have multiple "input" buttons so that the one with focus would get the user input.  An "input" button would receive focus by clicking on it or pressing the TAB key.  Of course there could be just one "input" button that had focus all the time just like the current text box.

[edit]
Sparky,   Just noticed the [SUGGESTION] flag in the title, thought you were asking for help.  Anyway you did inspire the above idea for which I thank you.     

monkey0506

Another possible method of intercepting the input would be to use repeatedly_execute_always and IsKeyPressed. My tests seem to indicate that the text is being added to the textbox before IsKeyPress is being triggered, but you could manually control the input..something such as:

Code: ags
// global script
bool txtTextboxEnabled = false; // used in place of the actual TextBox.Enabled property
bool prevEnabled = false; // used to track the Enabled state from the last game loop

String GetActualText(this TextBox*) { // used to remove the simulated cursor
  if (prevEnabled) return this.Text.Truncate(this.Length - 1); // if the text has a cursor at the end of it
  return this.Text; // otherwise just return the text like normal
}

// game_start
txtTextbox.Enabled = false; // don't automatically accept input

// ...
gSomeGUI.Visible = true;
txtTextboxEnabled = true;
txtTextbox.Text = txtTextbox.Text.AppendChar('_'); // simulate the cursor

// rep_ex_always
if (!gSomeGUI.Visible) txtTextboxEnabled = false;
txtTextbox.Text = txtTextbox.GetActualText(); // remove cursor
if (txtTextboxEnabled) { // if the textbox has simulated focus
  int i = 32;
  while (i < 127) { // iterate displayable characters
    if (IsKeyPressed(i)) {
      // do checks and bounds here..
      txtTextbox.Text = txtTextbox.Text.AppendChar(i); // add text to the textbox
      i = 127; // end iteration when a pressed key is found
    }
    i++;
  }
  txtTextbox.Text = txtTextbox.Text.AppendChar('_'); // replace cursor
}
prevEnabled = txtTextboxEnabled;


You'd then have to use TextBox.GetActualText() in place of TextBox.Text and txtTextboxEnabled in place of txtTextbox.Enabled, but that should give you control over the input.

subspark

Thats actually quite brilliant Monkey. Thanks dude.

I'm glad I spawned a new idea into your brain Rick. :=
Hope it serves you well!

Cheers,
Sparky.

SMF spam blocked by CleanTalk