This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kartoffelbrot
Kartoffelbrot wrote ...

2013/9/27

Deselecting by clicking away

Kartoffelbrot Kartoffelbrot

2013/9/27

#
I have a textbox, which should dehighlight it's position, when you click not on it. The methods work except of the method to check the clicks:
/**
     * The box will be activated, when you click into it.
     */
    private void checkOnActivation(){
        if(Greenfoot.mouseClicked(this)){
            active = true;
            highlightPos();
        }else if(Greenfoot.mouseClicked(null)){
            active = false;
            deHighlightPos();
        }
    }
The problem isn't in the highlight methods. I checked on that. Thanks for help.
danpost danpost

2013/9/27

#
You missed the part where you are not clicking on it. change line 8 to:
}else if(Greenfoot.mouseClicked(null) && !Greenfoot.mouseClicked(this)){
Kartoffelbrot Kartoffelbrot

2013/9/27

#
Thanks for help, but it doesn't work, too.
davmac davmac

2013/9/27

#
I have tested the code you posted and it works fine. The problem is elsewhere.
Kartoffelbrot Kartoffelbrot

2013/9/28

#
Both of you are right. I had that code some time before and it didn't worked there, too. I tried it now again, which led to my last comment. If you want to help me I can post the code of the class here, so that you can try it in greenfoot. I didn't found the Problem yet. If someone of you is able to fix it, you are allowed to use the class if you want. It's a text field.
danpost danpost

2013/9/28

#
Go ahead and post away.
Kartoffelbrot Kartoffelbrot

2013/9/28

#
import greenfoot.*;
import java.awt.Color;
public class GTextField extends Interface
{
    private boolean active, posHighlighted, binaryInput;
    private int width, height, pos;
    private String text, displayedText;

    /**
     * creates a new GTextField with the given dimensions
     */
    public GTextField(int width, int height){
        this.width = width;
        this.height = height;
        active = false;
        text = "";
        pos = 0;
        posHighlighted = false;
        displayText(text);
    }

    /**
     * used to create an input for: 0, 1, X
     */
    public GTextField(boolean binaryInput){
        this.binaryInput = binaryInput;
        width = 30;
        height = 30;
        active = false;
        text = "";
        pos = 0;
        posHighlighted = false;
        displayText(text);
    }

    public void act(){
        setPosAtClick();
        if(active)
            input();
        checkOnActivation();
    }

    /**
     * @param: the text, that should be in the box
     */
    public void setText(String text){
        this.text = text;
        displayText(text);
    }

    /**
     * @return: the text in the field
     */
    public String getText(){
        deHighlightPos();
        String r = text;
        highlightPos();
        return r;
    }

    /**
     * @param: the text will be displayed in the box
     */
    private void displayText(String text){
        GreenfootImage t = new GreenfootImage(text, height-4, Color.BLACK, new Color(0,0,0,0));

        GreenfootImage i = new GreenfootImage(width, height);
        i.setColor(Color.WHITE);
        i.fill();
        i.setColor(Color.BLACK);
        i.drawRect(0,0,width-1, height-1);

        //scrolling to the right position
        if(t.getWidth()>i.getWidth()-2){
            int wL = pos;
            int wR = text.length()-pos;
            double leftsPerRight = 1, rightsPerLeft = 1;
            if(wR!=0)
                leftsPerRight = wL/wR;
            if(wL!=0)
                rightsPerLeft = wR/wL;
            double lPRcount = 0;
            while(t.getWidth()>i.getWidth()-2){
                char[] c = text.toCharArray();
                String a = "";
                if(lPRcount>=leftsPerRight){
                    for(int b = 0; b < c.length-1; b++)
                        a+=c[b];
                    lPRcount-=1;//alternative: leftsPerRight
                }else{
                    for(int b = 1; b < c.length; b++)
                        a+=c[b];
                    lPRcount+=rightsPerLeft;
                }
                text = a;
                t = new GreenfootImage(text, height-4, Color.BLACK, new Color(0,0,0,0));
            }
        }
        displayedText = text;
        i.drawImage(t, 2, 2);
        setImage(i);
    }

    /**
     * This method allows the ability to type text into the box, to scroll through it and to
     * remove letters.
     */
    private void input(){
        deHighlightPos();
        String n = Greenfoot.getKey();
        if(n=="backspace"){
            char[] c = text.toCharArray();
            String a = "";
            for(int i = 0; i < c.length; i++){
                if(i==pos-1)
                    i++;
                if(i<c.length)
                    a+=c[i];
            }
            text = a;
            pos--;
        }else if(n=="delete"){
            char[] c = text.toCharArray();
            String a = "";
            for(int i = 0; i < c.length; i++){
                if(i==pos)
                    i++;
                if(i<c.length)
                    a+=c[i];
            }
            text = a;
            pos--;
        }else if(n=="left"){
            if(pos>0)
                pos--;
        }else if(n=="right"){
            if(pos<text.length())
                pos++;
        }else if(n!=null && n!="shift" && n!="enter" && n!="up" && n!="down" &&n!="tab" &&
        n!="escape" && n!="control"){
            if(!binaryInput){
                if(n=="space")
                    n=" ";
                //inserting new symbol into text
                char[] c = text.toCharArray();
                String a = "";
                for(int i = 0; i < c.length; i++){
                    if(pos==0&&i==0)
                        a+=n;
                    a+=c[i];
                    if(i==pos-1)
                        a+=n;
                }
                if(c.length==0)
                    a+=n;
                pos++;
                text = a;
            }else{
                if(n=="space")
                    n="";
                //inserting new symbol into text
                char[] c = text.toCharArray();
                if(c.length<1){
                    if(n.equals("x"))
                        n="X";
                    else if(n.equals("1"))
                    ;
                    else if(n.equals("0"))
                    ;
                    else
                        n="";
                    text = n;
                    pos++;
                }
            }
        }
        highlightPos();
        displayText(text);
    }

    /**
     * highlights the writimg position with a |
     */
    private void highlightPos(){
        if(!posHighlighted){
            char[] c = text.toCharArray();
            String a = "";
            for(int i = 0; i <= c.length; i++){
                if(i==pos)
                    a+="|";
                if(i<c.length)
                    a+=c[i];
            }
            text = a;
            posHighlighted = true;
        }
    }

    /**
     * dehighlights the writimg position
     */
    private void deHighlightPos(){
        if(posHighlighted){
            char[] c = text.toCharArray();
            String a = "";
            for(int i = 0; i < c.length; i++){
                if(i==pos)
                    i++;
                if(i<c.length)
                    a+=c[i];
            }
            text = a;
            posHighlighted = false;
        }
    }

    /**
     * The box will be activated, when you click into it.
     */
    private void checkOnActivation(){
        if(Greenfoot.mouseClicked(this)){
            active = true;
            highlightPos();
        }else if(Greenfoot.mouseClicked(null) && !Greenfoot.mouseClicked(this)){
            deHighlightPos();
            active = false;
        }
    }

    /**
     * sets the position at the place you click (nearly)
     */
    private void setPosAtClick(){
        deHighlightPos();
        if(Greenfoot.mouseClicked(this)){
            MouseInfo m = Greenfoot.getMouseInfo();
            if(m!=null){
                //calculate the relative position of the mouse to this object
                int x = m.getX()-(getX()-getWidth()/2);
                //finding the place in the displayed text, where the mouse was clicked
                int posOfLetter = 0;
                char[] c = displayedText.toCharArray();
                int posOfLetterInC=0;
                for(int i = 0; i < displayedText.length(); i++){
                    GreenfootImage t = new GreenfootImage(
                            c[i]+"", height-4, Color.BLACK, new Color(0,0,0,0));
                    posOfLetter+=t.getWidth();
                    if(posOfLetter<x+20&&posOfLetter>x-20){
                        posOfLetterInC = i;
                        continue;
                    }
                }
                //finding the place int the text to set the position
                char[] o = text.toCharArray();
                int posOfDisplayedText=0;
                for(int i = 0; i < o.length-c.length;i++){
                    if(o[i]==c[0]){
                        boolean found = true;
                        for(int j = 0; j < c.length; j++){
                            if(o[j+i]!=c[j])
                                found = false;
                        }
                        if(found){
                            posOfDisplayedText = i;
                            continue;
                        }
                    }
                }
                pos = posOfDisplayedText+posOfLetterInC;
            }
        }
        highlightPos();
    }
}
danpost danpost

2013/9/28

#
Try putting 'checkOnActivation' first in the act method (just a thought).
Kartoffelbrot Kartoffelbrot

2013/9/28

#
I have tried that earlier. It's the same Problem :/ .
danpost danpost

2013/9/28

#
What about moving line 272 up a line (inside the 'if' block)?
Kartoffelbrot Kartoffelbrot

2013/9/29

#
Good idea, but it doesn't make a difference.
davmac davmac

2013/9/29

#
You didn't supply the code for the "Interface" class so I had to guess a bit, but: it looks like the problem is that you have both "displayedText" and "text" variables with a very similar purpose. In the "deHighlightPos" method you adjust the text variable, but not the displayedText variable, so the cursor is still displayed.
Kartoffelbrot Kartoffelbrot

2013/9/29

#
Yes you are right. Thank you very much! danpost, too! The Interface class is empty (It's only used for sorting the classes in greenfoot).
You need to login to post a reply.