Changeset 198

Show
Ignore:
Timestamp:
07/24/10 01:04:23 (2 years ago)
Author:
JoeCoder
Message:

More work on editable text--inserting line returns, correct cursor positioning when at the end of a line.
Updated UI skin.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/demo1/main.d

    r197 r198  
    180180    // Make a draggable window to show some useful info. 
    181181    auto info = view.addChild(new Surface()); 
    182     info.style.set("top: 5px; right: 5px; width: 130px; height: 100px; color: black; padding: 3px; " ~ 
    183         "border-width: 5px; border-image: url('gui/skin/clear2.png'); font-size: 12px"); 
     182    info.style.set("top: 5px; right: 12px; width: 110px; height: 100px; color: white; padding: -6px; " ~ 
     183        "border-width: 12px; border-image: url('gui/skin/panel1.png'); font-size: 12px"); 
    184184 
    185185    //window.style.backgroundImage = scene.camera.getTexture(); 
     
    199199    }; 
    200200    info.onMouseOver = delegate bool(Surface self, byte buttons, Vec2i coordinates) { 
    201         self.style.set("border-image: url('gui/skin/clear3.png')"); 
     201        self.style.set("border-image: url('gui/skin/panel2.png')"); 
    202202        return false; 
    203203    }; 
    204204    info.onMouseOut = delegate bool(Surface self, byte buttons, Vec2i coordinates) { 
    205         self.style.set("border-image: url('gui/skin/clear2.png')"); 
     205        self.style.set("border-image: url('gui/skin/panel1.png')"); 
    206206        return false; 
    207207    }; 
  • trunk/src/demo2/main.d

    r197 r198  
    1111 
    1212import tango.text.convert.Format; 
    13 import tango.io.Stdout
     13import tango.math.Math
    1414import tango.io.device.File; 
    1515import derelict.sdl.sdl; 
     
    3232    Scene scene = new Scene(); 
    3333    scene.play(); 
    34     scene.backgroundColor = "green"; 
     34    scene.backgroundColor = "gray"; 
    3535     
    3636    // Ship  
     
    6464    // For Testing 
    6565    auto info = view.addChild(new Surface()); 
    66     info.style.set("top: 40px; left: 40px; width: 500px; height: 260px; padding: 3px; color: brown; " ~ 
    67         "border-width: 5px; border-image: url('gui/skin/clear2.png'); " ~ 
    68         "font-family: url('Vera.ttf'); text-align: right; opacity: .8; overflow: hidden"); 
     66    info.style.set("top: 40px; left: 40px; width: 500px; height: 260px; padding: 3px; color: #ff8800; " ~ 
     67        "border-width: 12px; border-image: url('gui/skin/panel1.png'); " ~ 
     68        "font-family: url('Vera.ttf'); text-align: center; opacity: .8; overflow: hidden"); 
    6969    info.style.overflowX = Style.Overflow.HIDDEN; 
    7070    info.style.overflowY = Style.Overflow.HIDDEN; 
     
    8585    }; 
    8686    info.onMouseOver = delegate bool(Surface self, byte buttons, Vec2i coordinates) { 
    87         self.style.set("border-image: url('gui/skin/clear3.png')"); 
     87        //self.style.set("border-image: url('gui/skin/panel2.png')"); 
    8888        return false; 
    8989    }; 
    9090    info.onMouseOut = delegate bool(Surface self, byte buttons, Vec2i coordinates) { 
    91         self.style.set("border-image: url('gui/skin/clear2.png')"); 
     91        //self.style.set("border-image: url('gui/skin/panel1.png')"); 
    9292        return false; 
    9393    }; 
     
    108108    // Rendering / Input Loop 
    109109    int fps = 0; 
     110    Timer total = new Timer(true); 
    110111    Timer frame = new Timer(true); 
    111112    while(!System.isAborted()) 
     
    119120        // Print framerate 
    120121        fps++; 
    121         //info.style.transform = info.style.transform.move(Vec3f(-40, -40, 0)); 
    122         //info.style.transform *= Matrix().rotate(Vec3f(0, 0.0005, 0.0005)); 
    123         //info.style.transform = info.style.transform.move(Vec3f(40, 40, 0)); 
     122        info.style.transform = Matrix(); 
     123        info.style.transform = info.style.transform.move(Vec3f(-300, -20, 0)); 
     124        info.style.transform *= Matrix().rotate(Vec3f(0, sin(total.tell()/2)/2, sin(total.tell()/2)/5)); 
     125        info.style.transform = info.style.transform.move(Vec3f(300, 20, 0)); 
    124126         
    125127         
    126         if (frame.tell()>=.25f
     128        if (frame.tell()>=.25f && !(Surface.getFocusSurface() is info)
    127129        {    
    128130            info.innerHtml = `In a <s>traditional</s> <span style="color: green; text-decoration: overline; font-size:40px">`~ 
     
    137139            `Some metal type required in type-setting, such as varying sizes of inter-word spacing pieces and `~ 
    138140            `line-width spacers, were not part of a specific font in pre-digital usage, but were separate, `~ 
    139             `generic pieces.[1]             `~ Format.convert(` {} fps<br/>`, fps/frame.tell()); 
     141            `generic pieces.[1]             `~ Format.convert(` {} fps<br/>`, fps/frame.tell()); 
    140142             
    141143            frame.seek(0); 
  • trunk/src/yage/gui/surface.d

    r197 r198  
    3434    Style style; 
    3535     
    36     protected char[] text; /// This html text will be rendered inside the surface.     
     36    //protected char[] text; /// This html text will be rendered inside the surface.   
    3737    bool editable = false; /// The text of this surface is editable. 
    3838    bool mouseChildren = true; /// Allow the mouse to interact with this Surface's children. 
     
    263263    /// 
    264264    public char[] innerHtml() 
    265     {   return text
     265    {   return textBlock.getHtml()
    266266    } 
    267267    /// ditto 
    268268    public void innerHtml(char[] html) 
    269     {   this.text = html; 
     269    {   //this.text = html; 
    270270        textDirty = true; 
     271        Style cs = getComputedStyle(); 
     272        textBlock.update(html, cs, cast(int)width(), cast(int)height()); 
    271273    } 
    272274     
     
    373375         
    374376        // Text 
    375         if (text.length && (resizeDirty || textDirty)
     377        if (resizeDirty || textDirty
    376378        { 
    377379            int width = cast(int)width(); 
    378380            int height = cast(int)height(); 
    379381             
    380             textBlock.update(text, cs, width, height); 
     382            //textBlock.update(text, cs, width, height); 
     383            //textBlock.update(text, cs, width, height); 
     384            textBlock.update(textBlock.getHtml(), cs, width, height); 
    381385            Image textImage = textBlock.render(cs, true, editable && focusSurface is this ? &textCursor : null); // TODO: Change true to Probe.NextPow2 
    382             assert(textImage !is null); 
    383386             
    384             if (!textTexture) // create texture on first go 
    385                 textTexture = new Texture(textImage, Texture.Format.AUTO, false, "Surface Text", true); 
    386             else 
    387                 textTexture.setImage(textImage); 
    388             textTexture.padding = Vec2i(nextPow2(width)-width, -(nextPow2(height)-height)); 
     387            if (textImage) 
     388            {   if (!textTexture) // create texture on first go 
     389                    textTexture = new Texture(textImage, Texture.Format.AUTO, false, "Surface Text", true); 
     390                else 
     391                    textTexture.setImage(textImage); 
     392                textTexture.padding = Vec2i(nextPow2(width)-width, -(nextPow2(height)-height)); 
     393            } else 
     394                textTexture = null; 
    389395             
    390396            textDirty = false; 
    391397        } 
    392398         
    393         if (!text.length) 
    394           textTexture = null; 
     399        //if (!text.length) 
     400        //    textTexture = null; 
    395401         
    396402        geometry.setColors(style.backgroundColor, style.borderColor, style.opacity); 
     
    480486     *     unicode = unicode value of pressed key. */  
    481487    void keyPress(int key, int mod=ModifierKey.NONE, dchar unicode=0) 
    482     {   bool propagate = true; 
     488    {    
     489        bool propagate = true; 
    483490        if(onKeyPress) 
    484491            propagate = onKeyPress(this, key, mod);      
     
    487494            {    
    488495                textBlock.input(key, mod, unicode, textCursor, getComputedStyle()); 
    489                 text = textBlock.toString(); // TODO: Do this lazily? 
    490496                textDirty = true; 
    491497            } 
  • trunk/src/yage/gui/textblock.d

    r197 r198  
    5353    private ubyte[] imageLookaside; // TODO: Have the lookaside passed into Render 
    5454     
    55     private char[] text
     55    private char[] html
    5656    package InlineStyle style; // base style of entire text block 
    5757    private int width; 
     
    7474            Vec2i result; 
    7575            int line = cursorToLine(position); 
     76            //Log.trace(line, " ", position); 
    7677            for (int i=0; i<line; i++) 
    7778                result.y += lines[i].height; 
     
    102103         *     position = Character position from the beginning of the TextBlock 
    103104         *     After the function executes, this will be the position on the line returned. 
    104          * Returns:  The line number.  If position is after the last line, then the number of the last line +1 is returned.*/ 
     105         * Returns:  The line number.  If position is after the last line, then the number of the last line is returned.*/ 
    105106        int cursorToLine(inout int position) 
    106107        {   foreach (i, line; lines.data) 
    107             {   if (position < line.letters.length) 
     108            {   if (position <= line.letters.length) 
    108109                    return i; 
    109110                position -= line.letters.length; 
    110111            } 
    111             return lines.length
     112            return lines.length-1
    112113        } 
    113114         
     
    124125            return position; 
    125126        } 
    126         /* 
    127         // Get the x position in pixels of a character on a line.  TODO: Also y. 
    128         int positionToX(int line, int position) 
    129         {   assert (line < lines.length);                
    130             int x; 
    131             int last = min(lines[line].letters.length, position); 
    132             for (int i=0; i<last; i++) 
    133                 x+= lines[line].letters[i].advanceX; 
    134             return x; 
    135         } 
    136          
    137         // Get the nearest character position on a line from an x poxition in pixels 
    138         int xToPosition(int line, int x) 
    139         {    
    140             for (int i=0; i<lines[line].letters.length; i++) 
    141             {   x-= lines[line].letters[i].advanceX; 
    142                 if (x<0) // TODO: More accurate rounding 
    143                     return i; 
    144             } 
    145         } 
    146         */ 
     127    } 
     128     
     129    char[] getHtml() 
     130    {   return html; 
    147131    } 
    148132     
     
    158142    {    
    159143        style = InlineStyle(computedStyle); 
     144        //Log.trace(cursor.position, " " , letters.length); 
    160145        assert(cursor.position <= letters.length); 
    161146         
     
    198183                break; 
    199184            case SDLK_DELETE:   
    200                 if (cursor.position < letters.length)  // doesn't work 
     185                if (cursor.position < letters.length) 
    201186                    letters.splice(cursor.position, 1); 
    202187                break; 
    203188            // New letters 
    204             default:  
     189            default:                
    205190                if (unicode) 
    206191                {    
     192                    if (unicode=='\r') 
     193                        unicode='\n'; 
     194                    //Log.trace(cast(int)unicode); 
     195                     
    207196                    // Get the style to use for a new letter 
    208197                    InlineStyle *style; 
     
    219208                    cursor.position++; 
    220209                } 
    221             break;          
    222             // TODO: selection, ctrl+a, z, x, c, v 
    223             
    224          
    225          
     210            break; 
     211             
     212           
     213           // TODO: click to position cursor, selection, ctrl+a, z, x, c, v 
     214        } 
    226215         
    227216        lines = lettersToLines(letters.data, width, lines); 
    228          
    229         //return toString(); 
     217        //Log.trace(lines.length); 
    230218    } 
    231219     
     
    247235        Image result; 
    248236 
    249         if (text.length
     237        if (html.length || cursor
    250238        {    
    251239            // Get total height of all lines 
     
    272260 
    273261                foreach (letter; line.letters) 
    274                 {   InlineStyle* istyle = (cast(InlineStyle*)letter.extra); 
     262                {    
     263                    if (letter.letter < 32) 
     264                        continue; // skip non-printable letters. 
     265                     
     266                    InlineStyle* istyle = (cast(InlineStyle*)letter.extra); 
    275267                 
    276268                    // Calculate local coordinates 
     
    307299                    break; 
    308300            } 
    309         } 
    310          
    311         // Draw cursor 
    312         if (cursor) 
    313         {  Vec2i xy = cursorToXy(cursor.position); 
    314             int lineHeight = cast(int)style.lineHeight.toPx(0); 
    315             if (letters.length) 
    316             {  //xy.x += letters[cursor.position].advanceX; 
    317                 int position = cursor.position; // copy 
    318                 int line = min(cursorToLine(position), lines.length-1)
    319                 lineHeight = lines[line].height; 
    320             } 
    321             int hmin = max(0, xy.y), hmax = min(lineHeight+xy.y, height); 
    322             int wmin = max(0, xy.x), wmax = min(xy.x+2, width); 
    323             for (int h=hmin; h<hmax; h++) 
    324                 for (int w=wmin; w<wmax; w++) 
    325                    result[w, h] = style.color.ub; 
     301            
     302           // Draw cursor 
     303           if (cursor) 
     304           {   Vec2i xy = cursorToXy(cursor.position); 
     305               int lineHeight = cast(int)style.lineHeight.toPx(0); 
     306               if (letters.length) 
     307               {   //xy.x += letters[cursor.position].advanceX; 
     308                   int position = cursor.position; // copy 
     309                   int line = min(cursorToLine(position), lines.length-1); 
     310                   lineHeight = lines[line].height
     311                } 
     312               int hmin = max(0, xy.y), hmax = min(lineHeight+xy.y, height); 
     313               int wmin = max(0, xy.x), wmax = min(xy.x+2, width); 
     314               for (int h=hmin; h<hmax; h++) 
     315                   for (int w=wmin; w<wmax; w++) 
     316                       result[w, h] = style.color.ub; 
     317            } 
    326318        } 
    327319         
     
    394386     * Returns: True if the text will need to be re-rendered, false otherwise. 
    395387     * TODO: Should this be a constructor to maintain RAII? */ 
    396     bool update(char[] text, Style style, int width, int height) 
     388    bool update(char[] html, Style style, int width, int height) 
    397389    { 
    398390        InlineStyle istyle = InlineStyle(style); 
     
    400392        // If text has changed 
    401393        //bool newLetters = text != (*this).text || istyle != (*this).style; 
    402         bool newLetters = text != this.text || istyle != this.style; 
     394        bool newLetters = html != this.html || istyle != this.style; 
    403395        if (newLetters) 
    404396        {    
     
    406398            letters.length = 0;          
    407399            styles.length = 0; 
    408             HtmlParser.htmlToLetters(text, style, letters, styles); 
    409              
    410             this.text = text
     400            HtmlParser.htmlToLetters(html, style, letters, styles); 
     401             
     402            this.html = html
    411403            this.style = istyle; 
    412404        } 
     
    494486                //line.xOffsets.resize(lineEnd); 
    495487                 
    496                 if (line.letters[$-1].letter == '\n') 
    497                   line.letters.length = line.letters.length-1; 
     488                //if (line.letters[$-1].letter == '\n') 
     489                //    line.letters.length = line.letters.length-1; 
    498490 
    499491                // Calculate line.width 
  • trunk/src/yage/system/input.d

    r195 r198  
    1616 
    1717/** 
    18  * A class to handle keyboard, mouse, and eventually joystick input. 
     18 * A static class to handle keyboard, mouse, and eventually joystick input. 
    1919 * This polls SDL for input and passes it to the current surface. 
    2020 *  
    2121 * TODO: Ditch SDL!  SDL requires input processing to be in the same thread as the renderer. 
    2222 */  
    23 class Input 
     23abstract class Input 
    2424{ 
    2525    public static int KEY_DELAY = 500; /// milliseconds before repeating a call to keyPress after holding a key down. 
     
    3030 
    3131    protected static SDL_keysym lastKeyDown; // Used for manual key-repeat. 
    32     protected static uint lastKeyDownTime
     32    protected static uint lastKeyDownTime = uint.max
    3333     
    3434    /**  
     
    6060                    if(focus) 
    6161                        focus.keyUp(event.key.keysym.sym, event.key.keysym.mod); 
    62                         //focus.keyUp(event.key.keysym.unicode, event.key.keysym.mod); 
    6362                    lastKeyDownTime = uint.max; 
    6463                    break; 
     64                 
    6565                // Mouse 
    6666                case SDL_MOUSEBUTTONDOWN: