PyOgre: A Simple Text Interface

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
Kris
Kobold
Posts: 34
Joined: Sun Aug 28, 2005 6:23 am
Contact:

PyOgre: A Simple Text Interface

Post by Kris »

While I await the coming of Crazy Eddie to PyOgre, I've been working on a crude way to input text in my program. This is what I've got:

Code: Select all

##In the program code:
OGRE_LETTER_KEYCODES = [30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44] ## Why such a weirdly ordered list for A-Z?

## In the FrameListener code:
def showTestOverlay(self, show=True):
    """Turns KS' test overlay on or off. Based on debug overlay."""
    overlay = ogre.OverlayManager.getSingleton().getByName('Niss/NissOverlay')
    if overlay is None:
        raise ogre.Exception(111, "Could not find overlay Niss/NissOverlay", "SampleFramework.py")
    if show:
        overlay.show()
    else:
        overlay.hide()

## In FrameListener._processUnbufferedKeyInput():
        ## Test all letter keys.
        for n in range(len(OGRE_LETTER_KEYCODES)):
            keycode = OGRE_LETTER_KEYCODES[n]
            if self._isToggleKeyDown(keycode,0.2):
                key_pressed = "abcdefghijklmnopqrstuvwxyz"[n]
                self.UpdateInterface("Niss/TextInput",key_pressed,True)

        if self._isToggleKeyDown(ogre.KC_SPACE,0.2):
            self.UpdateInterface("Niss/TextInput"," ",True)
        if self._isToggleKeyDown(ogre.KC_RETURN,0.2):
            text_input = self.GetTextInput()
            print "Output sent nowhere: " + text_input
            self.UpdateInterface("Niss/TextInput","")
            self.UpdateInterface("Niss/TextOutput","You typed: " + text_input)

## Also under FrameListener:
    def UpdateInterface(self,where,text,add_to_existing_text=False):
        """Change part of the text in the interface."""
        element = ogre.OverlayManager.getSingleton().getOverlayElement(where, False)
        if add_to_existing_text:
            element.caption += text
        else:
            element.caption = text

    def GetTextInput(self):
        element = ogre.OverlayManager.getSingleton().getOverlayElement("Niss/TextInput", False)
        return element.caption

And finally, a custom overlay file, "Niss.overlay," in C:\Python24\pyogre\demos\media\overlays:

Niss/NissOverlay
{
	zorder 500
	container BorderPanel(Niss/NissPanel)
	{
		metrics_mode pixels
		vert_align bottom
		left 5 
		top -150
		width 790
		height 145
		material Core/StatsBlockCenter
        	border_size 1 1 1 1
        	border_material Core/StatsBlockBorder
        	border_topleft_uv     0.0000 1.0000 0.0039 0.9961
	    	border_top_uv         0.0039 1.0000 0.9961 0.9961
	    	border_topright_uv    0.9961 1.0000 1.0000 0.9961
	    	border_left_uv        0.0000 0.9961 0.0039 0.0039
	    	border_right_uv       0.9961 0.9961 1.0000 0.0039
	    	border_bottomleft_uv  0.0000 0.0039 0.0039 0.0000
        	border_bottom_uv      0.0039 0.0039 0.9961 0.0000
	    	border_bottomright_uv 0.9961 0.0039 1.0000 0.0000
	}
	container TextArea(Niss/TextOutput)
	{
			metrics_mode pixels
			left 10
			top 452
			width 790
			height 100
			font_name TrebuchetMSBold
			char_height 19
			caption The time has come, the walrus said, to speak of many things. Of shoes and ships and sealing wax, of cabbages and kings. And why the sea is boiling hot, and whether pigs have wings. -Lewis Carroll
			colour_top 1 1 0.7
			colour_bottom 1 1 0.7
	}
	container Panel(Niss/BreakPanel)
	{
			metrics_mode pixels
			left 10
			top 560
			width 780
			height 1
			material Core/StatsBreak
	}
	container TextArea(Niss/TextInput)
	{
			metrics_mode pixels
			left 10
			top 562
			width 790
			height 30
			font_name TrebuchetMSBold
			char_height 19
			caption Input from the keyboard will go here.
			colour_top 1 1 0.7
			colour_bottom 1 1 0.7
	}
}
The effect is that by typing, text appears on the screen, and by pressing Enter, the text you've typed gets fetched and can be sent elsewhere, including to another interface element.
Obvious problems:
-It's not CEGUI.
-Lowercase letters and spaces only; no numbers etc.
-No word wrap; see example output caption.
-Caught between too-fast key repeats and "key jams" where nothing registers for 0.2 seconds after another keypress.

Any advice on making such a system practical, or (hint hint) making CEGUI do the same thing in PyOgre?
Srekel
Halfling
Posts: 89
Joined: Tue Jun 08, 2004 8:33 pm
Contact:

Post by Srekel »

Very nice. :) I might just use that when I get to implementing a high score. I assume its under public domain or something like that?
User avatar
Kris
Kobold
Posts: 34
Joined: Sun Aug 28, 2005 6:23 am
Contact:

Post by Kris »

Sure. The above code is hereby public domain, subject to any restrictions (none, I think) on the OGRE/PyOgre example code it's based on.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Post by jacmoe »

Public Domain code really should go into the Code Snippets Section of the Ogre Wiki - otherwise they will quickly turn into Forgotten Domain. :)
Srekel
Halfling
Posts: 89
Joined: Tue Jun 08, 2004 8:33 pm
Contact:

Post by Srekel »

jacmoe wrote:Public Domain code really should go into the Code Snippets Section of the Ogre Wiki - otherwise they will quickly turn into Forgotten Domain. :)

Fixed. :)

http://www.ogre3d.org/wiki/index.php/PyOgre under Code Snippets. I figured it would be more logical to have a separate one for PyOgre.
User avatar
Kris
Kobold
Posts: 34
Joined: Sun Aug 28, 2005 6:23 am
Contact:

Post by Kris »

Can upgrade this (elminnatig clnkky typng) once I know how to do buffered input in Python; have asked the question elsewhere in Help.
User avatar
Kris
Kobold
Posts: 34
Joined: Sun Aug 28, 2005 6:23 am
Contact:

Post by Kris »

What's the Ogre keycode for the backspace key? As in "ogre.KC_K."

(I've nearly got a usable buffered text system, thanks to help from another poster.)
User avatar
Kris
Kobold
Posts: 34
Joined: Sun Aug 28, 2005 6:23 am
Contact:

Post by Kris »

Here, for the moment, is the latest version of the text-interface demo. This version doesn't use the SampleFramework file (though it uses its classes; they're copied from it). It allows you to type text with smooth, buffered input and have it appear on the screen and get inputted when you hit Enter. You can delete characters by pressing Delete, because I don't know the Ogre keycode for the Backspace key. Because of that flaw it's not ready for Wiki.

Thanks to Dermont Gill; he sent me his own adaptation of the past version using buffered input, and I combined it with my demo to make this one.
dermont
Bugbear
Posts: 812
Joined: Thu Dec 09, 2004 2:51 am
x 42

Post by dermont »

The Ogre keycode for backspace is ogre.KC_BACK

Take a look at the site-packages\pyogre\ogre.py, the keys are listed there.
Srekel
Halfling
Posts: 89
Joined: Tue Jun 08, 2004 8:33 pm
Contact:

Post by Srekel »

Don't forget you're using Python!

Code: Select all

>>> for x in dir(ogre):
...     if x[0] == "K": print x
...
KC_0
KC_1
KC_2
KC_3
KC_4
KC_5
KC_6
KC_7
KC_8
KC_9
KC_A
KC_ABNT_C1
KC_ABNT_C2
KC_ADD
KC_APOSTROPHE
KC_APPS
KC_AT
KC_AX
KC_B
KC_BACK
KC_BACKSLASH
KC_C
KC_CALCULATOR
KC_CAPITAL
KC_COLON
KC_COMMA
KC_CONVERT
KC_D
KC_DECIMAL
KC_DELETE
KC_DIVIDE
KC_DOWN
KC_E
KC_END
KC_EQUALS
KC_ESCAPE
KC_F
KC_F1
KC_F10
KC_F11
KC_F12
KC_F13
KC_F14
KC_F15
KC_F2
KC_F3
KC_F4
KC_F5
KC_F6
KC_F7
KC_F8
KC_F9
KC_G
KC_GRAVE
KC_H
KC_HOME
KC_I
KC_INSERT
KC_J
KC_K
KC_KANA
KC_KANJI
KC_L
KC_LBRACKET
KC_LCONTROL
KC_LEFT
KC_LMENU
KC_LSHIFT
KC_LWIN
KC_M
KC_MAIL
KC_MEDIASELECT
KC_MEDIASTOP
KC_MINUS
KC_MULTIPLY
KC_MUTE
KC_MYCOMPUTER
KC_N
KC_NEXTTRACK
KC_NOCONVERT
KC_NUMLOCK
KC_NUMPAD0
KC_NUMPAD1
KC_NUMPAD2
KC_NUMPAD3
KC_NUMPAD4
KC_NUMPAD5
KC_NUMPAD6
KC_NUMPAD7
KC_NUMPAD8
KC_NUMPAD9
KC_NUMPADCOMMA
KC_NUMPADENTER
KC_NUMPADEQUALS
KC_O
KC_OEM_102
KC_P
KC_PAUSE
KC_PERIOD
KC_PGDOWN
KC_PGUP
KC_PLAYPAUSE
KC_POWER
KC_PREVTRACK
KC_Q
KC_R
KC_RBRACKET
KC_RCONTROL
KC_RETURN
KC_RIGHT
KC_RMENU
KC_RSHIFT
KC_RWIN
KC_S
KC_SCROLL
KC_SEMICOLON
KC_SLASH
KC_SLEEP
KC_SPACE
KC_STOP
KC_SUBTRACT
KC_SYSRQ
KC_T
KC_TAB
KC_U
KC_UNDERLINE
KC_UNLABELED
KC_UP
KC_V
KC_VOLUMEDOWN
KC_VOLUMEUP
KC_W
KC_WAKE
KC_WEBBACK
KC_WEBFAVORITES
KC_WEBFORWARD
KC_WEBHOME
KC_WEBREFRESH
KC_WEBSEARCH
KC_WEBSTOP
KC_X
KC_Y
KC_YEN
KC_Z
KeyEvent
KeyEventPtr
KeyFrame
KeyFramePtr
KeyListener
KeyListenerPtr
KeyTarget
KeyTargetPtr
>>>
:)
Post Reply