Text editor base?

Hi, I made a small new version of you editor with less of computer ressources.
I create a “selectLine” variable to not use a for loop in keyPressed function.
Now ENTER and BACKSPACE touch works.

PFont font;

PImage save;
PImage open;
PImage brush;

color theme1 = color(0, 8, 48);
color theme2 = color(0, 7, 30);

boolean bluet = true;
boolean darkt = false;

int scrolli = 0;
int selectLine = 0;
float scrolli2;

Line l1 = new Line(40, 1);
Toolbar t = new Toolbar();
Scrollbar sb = new Scrollbar(585, 50, 350, 60);
RMBMenu r = new RMBMenu();

ArrayList<Line> lines;

void setup() {
    size(600, 400);
    surface.setTitle("EpicTextEditor v0.1");
    font = loadFont("TeXGyreAdventor-Bold-48.vlw");
    textFont(font);
    //save = loadImage("saveIcon.png");
    //open = loadImage("openIcon.png");
    //brush = loadImage("brushIcon2.png");
    lines = new ArrayList<Line>();
    for (int i = 0; i < 50; i++) {
        lines.add(new Line(i * 20 + 30, i));
    }
}

void draw() {
    background(theme1);
    //themes
    if (darkt) {
        theme1 = color(70);
        theme2 = color(30);
    }
    for (int i = 0; i < 50; i++) {
        Line l = lines.get(i);
        l.y = sb.setScrollObjectY(0, 650) + (i * 20 + 30);
    }
    for (int i = 0; i < 50; i++) {
        Line l = lines.get(i);
        l.display();
    }
    t.display();
    sb.display();
    r.display();
}

void mousePressed() {
    sb.mousePress();
    r.mousePress();

    for (int i = 0; i < 50; i++) {
        Line l = lines.get(i);
        l.mousePress();
    }
}

void mouseReleased() {
    sb.mouseRelease();
}

void keyPressed() {
    lines.get(selectLine).keyPress();
}

void mouseDragged() {
    for (int i = 0; i < 50; i++) {
        Line l = lines.get(i);
        l.mouseDrag();
    }
}

void mouseWheel(MouseEvent event) {
    scrolli = constrain(scrolli + event.getCount(), 0, 50);
    scrolli2 = map(scrolli, 0, 30, 70, 220);
    sb.sy = int(scrolli2);
}

//--------------------------------------------------------------------------

class Line {

    String text = "";

    boolean selected;

    float yCtrl;
    float y;
    int n;

    boolean blink = true;
    int t = 0;

    Line(float y1, int n1) {
        y = y1;
        n = n1;
    }


    void display() {
        fill(theme1);
        noStroke();
        rect(0, y, width, 20);

        fill(theme2);
        rect(0, y, 40, 20);

        if (selected == true) {
            fill(0, 110);
            rect(0, y, width, 20);

            //blinking line
            fill(255);
            textSize(14);
            t++;
            if (t > 30) {
                blink = !blink;
                t = 0;
            }
            if (blink == true) text("|", 43 + textWidth(text), y + 15);
            if (keyPressed) {
                blink = true;
                t = 0;
            }
        } else t = 0;

        fill(150);
        textSize(14);
        text(nf(n, 3), 7, y + 15);

        fill(255);
        textSize(14);
        text(text, 43, y + 15);
    }
    void selectOn() {
        selected = true;
        selectLine = n;
    }
    int getn() {
        if (selected == true) return n;
        else return -1;
    }

    void mousePress() {
        if (selectLine == n && r.displayb == true && mouseX > r.x && mouseX < r.x + 160 && mouseY > r.y + 42 && mouseY < r.y + 58) text = "";

        if (r.displayb == false) {
            if (mouseButton == LEFT) {
                if (mouseY > 50 && mouseY > y && mouseY < y + 20 && mouseX < 585) selectOn(); 
                else if (mouseX < 585) selected = false;
            } else if (mouseButton == CENTER) {
                if (mouseY > 50 && mouseY > y && mouseY < y + 20 && mouseX < 585) selectOn();
            }
        }
    }
    void keyPress() {
        if (selectLine == n) {   
            if ((key==ENTER || keyCode==DOWN) && n<49) {
                selected = false;
                lines.get(selectLine+1).selectOn();
            } else if (key==BACKSPACE) {
                if (text.length()>0) {
                    text=text.substring(0, text.length()-1);
                } else if(n>1) {
                    selected = false;
                    lines.get(selectLine-1).selectOn();
                }
            } else if (key != CODED && textWidth(text) < 532) {
                text+=key;
            }
        }
    }

    void mouseDrag() {
        if (r.displayb == false) {
            if (mouseButton == CENTER || mouseButton == LEFT) {
                if (sb.scroll == false && mouseX < 585 && mouseY > y && mouseY < y + 20) {
                    selectOn();
                    blink = true;
                }
            }
        }
    }
}

//-----------------------------------------------------------------------------

class RMBMenu {

    int x = 0;
    int y = 0;

    boolean displayb = false;

    RMBMenu() {
    }

    void display() {
        if (displayb == true) {   
            stroke(150);
            fill(200);
            rect(x, y, 160, 60);

            line(x + 20, y + 2, x + 20, y + 58);

            //copy
            fill(0);
            textSize(14);
            text("Copy", x + 26, y + 15);

            noStroke();
            fill(0, 142, 237, 100);
            if (mouseX > x && mouseX < x + 160 && mouseY > y + 2 && mouseY < y + 18) rect(x, y + 2, 160, 16);

            //paste
            fill(0);
            textSize(14);
            text("Paste", x + 26, y + 35);

            noStroke();
            fill(0, 142, 237, 100);
            if (mouseX > x && mouseX < x + 160 && mouseY > y + 22 && mouseY < y + 38) rect(x, y + 22, 160, 16);

            //delete
            fill(0);
            textSize(14);
            text("Delete", x + 26, y + 55);

            noStroke();
            fill(0, 142, 237, 100);
            if (mouseX > x && mouseX < x + 160 && mouseY > y + 42 && mouseY < y + 158) rect(x, y + 42, 160, 16);
        }
    }

    void mousePress() {
        if (mouseX > x && mouseX < x + 160 && mouseY > y + 42 && mouseY < y + 58);

        if (mouseButton == RIGHT) {
            x = mouseX;
            y = mouseY;
            displayb = true;
        }

        if (mouseButton == LEFT) {
            if (displayb == true && mouseX > x && mouseY > y && mouseX < x + 160 && mouseY < y + 60) {
            } else displayb = false;
        }
    }
}

//------------------------------------------------------------------------

class Scrollbar {
    public int x;
    public int y;
    public int h;

    public int sy;
    public int sh;

    public color c = color(130);

    public boolean scroll = false;

    Scrollbar(int x1, int y1, int h1, int sh1) {
        x = x1;
        y = y1;
        h = h1;
        sy = y1 + 20;
        sh = sh1;
    }

    void display() {
        //base
        fill(200);
        noStroke();
        rect(x, y, 15, h);

        //up and down arrows
        fill(130);
        triangle(x + 3, y + 12, x + 12, y + 12, x + 8, y + 6);
        triangle(x + 3, y + h - 12, x + 12, y + h - 12, x + 8, y + h - 6);

        fill(160, 140);
        if (scroll == true || mouseX > x && mouseX < x + 15 && mouseY > y && mouseY < y + 20) rect(x, y, 15, 20);
        if (scroll == true || mouseX > x && mouseX < x + 15 && mouseY > y + h - 20 && mouseY < y + h) rect(x, y + h - 20, 15, 20);

        //scrollbar
        fill(c);
        noStroke();
        rect(x, sy, 15, sh);
        strokeWeight(1);
        stroke(80);
        line(x + 4, sy + sh/2 - 5, x + 11, sy + sh/2 - 5);
        line(x + 4, sy + sh/2, x + 11, sy + sh/2);
        line(x + 4, sy + sh/2 + 5, x + 11, sy + sh/2 + 5);

        if (mouseX > x && mouseX < x + 15 && mouseY > sy && mouseY < sy + sh || scroll == true) c = color(160);
        else c = color(130);
        if (scroll == true) {
            sy = mouseY - sh/2;
            scrolli = int(map(sy, 70, 220, 0, 30));
        }
        if (sy <= y + 20) sy = y + 20;
        if (sy + sh >= y + h - 20) sy = (y + h - 20) - sh;
    }

    void mouseScroll() {
    }

    void mousePress() {
        if (sy != y + 20 && mouseX > x && mouseX < x + 15 && mouseY > y && mouseY < y + 20) sy = sy - 20;
        if (sy != (y + h - 20) - sh && mouseX > x && mouseX < x + 15 && mouseY > y + h - 20 && mouseY < y + h) sy = sy + 20;

        if (mouseX > x && mouseX < x + 15 && mouseY > sy && mouseY < sy + sh ) {
            scroll = true;
        }
    }

    void mouseRelease() {
        scroll = false;
    }

    int getY() {
        return(sy - 20);
    }

    float setScrollObjectY(int y1, int y2) {
        return(-map(getY(), y, (y + h) - (sh + 40), y1, y2));
    }
}

//------------------------------------------------------------------------------

class Toolbar {

    Toolbar() {
    }


    void display() {
        fill(theme2);
        noStroke();
        rect(0, 0, width, 50);

        stroke(theme1);
        strokeWeight(5);
        line(0, 41, width, 41);

        ////save button
        //image(save,10,7,25,25);

        ////open button
        //image(open,50,6,27,25);

        ////seperator1
        //stroke(130);
        //strokeWeight(1);
        //line(87,7,87,32);

        ////brush button
        //image(brush,97,6,27,27);
    }
}

//end

to @ qewer3322 ---- I would like to use your code to create an alternative free, easy-to-use and basic of powerpoint with Processing. Do you agree ?

1 Like