PyOgreSimpleTextInterface

From Ogre Wiki

Jump to: navigation, search

This is a piece of code taken from this thread: http://www.ogre3d.org/phpBB2/viewtopic.php?t=13586

It describes how to read text input, for instance to use with a highscore table. "Kris" is the author, although he has released it under public domain.

##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.
Personal tools
administration