Hey! So I’m trying to make a text editor in processing something similar to Notepad++ but simpler. Here is what I got so far:
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;
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("SourceCodePro-Regular-48.vlw");
textFont(font);
//save = loadImage("saveIcon.png");
//open = loadImage("openIcon.png");
//brush = loadImage("brushIcon2.png");
lines = new ArrayList<Line>();
}
void draw() {
background(theme1);
//themes
if (darkt == true) {
theme1 = color(70);
theme2 = color(30);
}
for (int i = 1; i <= 50; i++) {
lines.add(new Line(i * 20 + 30, i));
Line l = lines.get(i - 1);
l.y = sb.setScrollObjectY(0, 650) + (i * 20 + 30);
}
for (int i = 1; i <= 50; i++) {
Line l = lines.get(i - 1);
l.display();
}
t.display();
sb.display();
r.display();
}
void mousePressed() {
sb.mousePress();
r.mousePress();
for (int i = 1; i <= 50; i++) {
Line l = lines.get(i - 1);
l.mousePress();
}
}
void mouseReleased() {
sb.mouseRelease();
}
void keyPressed() {
for (int i = 1; i <= 50; i++) {
Line l = lines.get(i - 1);
l.keyPress();
}
}
void mouseDragged() {
for (int i = 1; i <= 50; i++) {
Line l = lines.get(i - 1);
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);
}
int getn() {
if (selected == true) return n;
else return -1;
}
void mousePress() {
if (selected == true && 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) selected = true;
else if (mouseX < 585) selected = false;
} else if (mouseButton == CENTER) {
if (mouseY > 50 && mouseY > y && mouseY < y + 20 && mouseX < 585) selected = true;
}
}
}
void keyPress() {
if (selected == true) {
if (key != ENTER) {
if (key==BACKSPACE) {
if (text.length()>0) {
text=text.substring(0, text.length()-1);
}
} 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) {
selected = true;
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
I have a question about the base of the sketch. Right now the base is basically: there is a line class which holds a string that you can edit if the line is selected. There are bunch of lines and you can select multiple lines at the same time.
Well it works but I feel like it doesn’t have much potential for other text editor stuff like selecting text or moving the line between characters.And it currently doesn’t go to the next line when you hit enter, I tried stuff but couldn’t really figured it out.
My question is what kind of a base should I use? Does this system have potential should I stick with it or should I hold all the text in a single string or should I hold all the characters typed in an array or something else? How does notepad++ do it for example?
Also @Brastin , in your Processing OS (which is absolutely amazing btw) you have a text editor. How did you do the base?
Thanks!