This wiki is out of date, use the continuation of this wiki instead

Tutorial:Textinput

From FenixWiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 01:46, 28 May 2007 (edit)
Sandman (Talk | contribs)
m
← Previous diff
Current revision (00:16, 9 August 2007) (edit) (undo)
Sandman (Talk | contribs)
m (Textinput moved to Tutorial:Textinput: tutorial namespace)
 
(2 intermediate revisions not shown.)
Line 1: Line 1:
[[Category:tutorials]] [[Category:tutorials]]
-This isn't much of a [[tutorial]], rather some example code, but anyway. This code shows that text input in [[Fenix]] is not hard at all: with just a little piece of code, much can be achieved. Note that this is code for Fenix versions [[0.93]] and up.+This isn't much of a [[tutorial]], rather some example code, but anyway. This code shows that text input in [[Fenix]] is not hard at all: with just a little piece of code, much can be achieved.
 + 
 +Note that this is code for Fenix versions [[0.93]] and up. Should the code work for other versions as well, be so kind to note this here.
== Example code == == Example code ==
Line 40: Line 42:
// pressing enter will end the loop // pressing enter will end the loop
Loop Loop
- if(ascii!=0&&last_ascii==ascii) // check if a key is pressed and if the same key was pressed last frame+ if(ascii!=0&&last_ascii==ascii) // check if a key is pressed and if the same key
- if(t==0||t>fps/4) // check if the key was just pressed or it has been pressed for 0.25 seconds+ // was pressed last frame
- if(t==0||t2>fps/30) // check if the key was just pressed or it has been pressed for the last 0.03 seconds+ if(t==0||t>fps/4) // check if the key was just pressed or it has been pressed
 + // for 0.25 seconds
 + if(t==0||t2>fps/30) // check if the key was just pressed or it has been pressed
 + // for the last 0.03 seconds
t2=0; t2=0;
switch(ascii) // handle input switch(ascii) // handle input

Current revision


This isn't much of a tutorial, rather some example code, but anyway. This code shows that text input in Fenix is not hard at all: with just a little piece of code, much can be achieved.

Note that this is code for Fenix versions 0.93 and up. Should the code work for other versions as well, be so kind to note this here.

[edit] Example code

Program texts;

Declare Function String textinput();
End

Begin

    set_fps(60,0);

    say("You typed: '" + textinput() + "'");

    Repeat
        frame;
    Until(key(_esc));

End

Function String textinput()
Private
    String str;
    int t;
    int t2;
    byte last_ascii;
    int txtid;
Begin

    // show what you type in top left corner
    txtid = write_string(0,0,0,0,&str);

    // clean the string
    str = "";

    // get input from the user
    // pressing enter will end the loop
    Loop
        if(ascii!=0&&last_ascii==ascii) // check if a key is pressed and if the same key
                                        // was pressed last frame
            if(t==0||t>fps/4) // check if the key was just pressed or it has been pressed
                              // for 0.25 seconds
                if(t==0||t2>fps/30) // check if the key was just pressed or it has been pressed
                                    // for the last 0.03 seconds
                    t2=0;
                    switch(ascii) // handle input
                        case 8: //backspace
                            str = substr(str,0,len(str)-1);
                        end
                        case 13: //enter
                            break;
                        end
                        default: //addkey
                            str+=chr(ascii);
                        end
                    end
                end
                t2++;
            end
            t++;
        else
            t = t2 = 0; // reset
        end
        last_ascii = ascii;
        frame;
    End

    // delete the text used
    delete_text(txtid);

    // return the typed string
    return str;

End

Used in example: set_fps(), say(), write_string(), substr(), chr(), declare, function, switch, ascii, fps

You'll find a nice ASCII table here.

Personal tools