Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Getter with filters!
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ericfourfour




PostPosted: Sat Dec 16, 2006 11:46 pm   Post subject: Getter with filters!

Edit: A new version is available down the page.

I think its about time someone posted something like this. This class allows you to get input from the keyboard anywhere on the screen. You can set the font and colour as well. Another bit of functionality it adds is the ability to filter certain characters. This means you can filter white space, numbers, etc. if you feel like it.

This can also be expanded upon. It still needs a cursor, the ability to highlight, undo, copy, paste, a way to manage multiple getter's, and the list goes on.

code:
fcn textFilter (key : char) : boolean
    result key < ' ' or key > '~'
end textFilter

class Getter
    import Input, textFilter
    export addFilter, getString, move, start, activate, deactivate, setFont,
        setColour, update

    var done : boolean
    var x, y : int
    var chars : flexible array 0 .. -1 of char
    var bound : int
    var font : int := defFontID
    var clr : int := black
    var active : boolean
    var text : string

    type filterFunction : fcn filter (key : char) : boolean
    var filters : flexible array 0 .. -1 of filterFunction
    var filterBound : int := 0
    new filters, filterBound
    filters (0) := textFilter

    fcn getString () : string
        result text
    end getString

    proc addFilter (newFilter : filterFunction)
        filterBound += 1
        new filters, filterBound
        filters (filterBound) := newFilter
    end addFilter

    proc makeText ()
        text := ""
        for i : 0 .. bound
            text += chars (i)
        end for
    end makeText

    proc move (newX, newY : int)
        x := newX
        y := newY
    end move

    proc start (x, y : int)
        move (x, y)
        done := false
        bound := -1
        active := true
        text := ""
    end start

    proc activate ()
        active := true
    end activate

    proc deactivate ()
        active := false
    end deactivate

    proc setFont (fontID : int)
        font := fontID
    end setFont

    proc setColour (newColour : int)
        clr := newColour
    end setColour

    fcn filtered (key : char) : boolean
        for i : 0 .. filterBound
            if filters (i) (key) then
                result true
            end if
        end for
        result false
    end filtered

    proc parseKey (key : char)
        if key = KEY_ENTER then
            done := true

        elsif key = KEY_BACKSPACE then
            if bound > -1 then
                bound -= 1
            end if

        elsif not filtered (key) then
            bound += 1
            new chars, bound
            chars (bound) := key
        end if

        makeText ()
    end parseKey

    proc display ()
        Font.Draw (text, x, y, defFontID, clr)
    end display

    fcn update () : boolean
        if not done and active then
            if Input.hasch () then
                parseKey (Input.getchar ())
            end if
        end if
        display ()
        result not done
    end update
end Getter


Here is a sample program:
code:
var getter : ^Getter
new Getter, getter
getter -> start (100, 100)
loop
    exit when not getter -> update ()
    delay (10)
    cls
end loop
put getter -> getString ()


Here is another sample using pretty much everything:
code:
fcn numFilter (key : char) : boolean
    result key >= '0' and key <= '9'
end numFilter

var getter : ^Getter
new Getter, getter
getter -> start (100, 100)
getter -> addFilter (numFilter)
getter -> setFont (Font.New ("Arial:20"))
getter -> setColour (blue)
loop
    exit when not getter -> update ()
    delay (10)
    cls
end loop
put getter -> getString ()
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: Sun Dec 17, 2006 7:04 pm   Post subject: (No subject)

wow, this is pretty useful. i wish i was more skilled with classes, and string manipulation stuff. oh yea, shouldnt text filter be in side the class?
ericfourfour




PostPosted: Sun Dec 17, 2006 10:59 pm   Post subject: (No subject)

Unfortunately, functions and procedures cannot be variables or parameters if they are inside the class. I have actually done a lot of work on this and it does have a cursor but I've still got a bit of tweaking to do.
Clayton




PostPosted: Sun Dec 17, 2006 11:31 pm   Post subject: (No subject)

You can kind of screw around with the passing of functions and still having some portability. Simply stick the functions you wish to pass in a module, and import the module into your class and pass the functions from there. It's not pretty, but it gets the job done.
ericfourfour




PostPosted: Sun Dec 17, 2006 11:36 pm   Post subject: (No subject)

Good idea Freakman! At its current state it is around 3 or 4 classes so being able to separate it should make it easier to work with.
ericfourfour




PostPosted: Wed Dec 20, 2006 4:24 pm   Post subject: (No subject)

I updated it and it is looking great! It has been separated into units so now you can use it by simply typing:
code:
import Getter
at the top of your program. Most of the code has been reorganized so it is easier to understand and work with.

This version has a flashing cursor. I think it alternates every 500 milliseconds.

I added a module called Filters, which contains various filters. Take a look at it. It is quite simple to use.

As usual there is a sample program called "test.t".



Getter.zip
 Description:

Download
 Filename:  Getter.zip
 Filesize:  3.44 KB
 Downloaded:  104 Time(s)

Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: