with base you mean something like “basic concept” or “general approach”, right?
Remark 1
Woe, you can’t do this inside draw() :
for (int i = 1; i <= 50; i++) {
lines.add(new Line(i * 20 + 30, i));
Within seconds you have 19000 lines !
Do it in setup()
Remark 2
Your variable / object names are often too short: t, sb, r - it saves you a tiny bit of typing time but will cost you a lot of time to understand your code later!
Remark 3
This is not good:
for (int i = 1; i <= lines.size(); i++) { // ??????
Line l = lines.get(i - 1);
l.mousePress();
ArrayLists start at 0 so just start i with 0 and get rid of -1 in get(i - 1) also (and < instead of <=)
for (int i = 0; i < lines.size(); i++) {
Line l = lines.get( i );
l.mousePress();
OR just :
void keyPressed() {
for (Line l : lines) {
l.keyPress();
}
}
Remark 4
if (darkt == true) {
same as
if (darkt) {
Remark 5
if (darkt == false ) {
same as
if ( ! darkt ) {
Remark 6
Instead of searching the selected line number
for (Line l : lines) {
l.keyPress(); // only the selected line does something here!!!!
}
we could store the line number of the selected line.
No for-loop needed:
lines.get( numberOfTheSelectedLine ).keyPress();
Remark 7
I made a new version below.
I made a few changes.
I did key ENTER and UP and DOWN ( LEFT and RIGHT have to be done inside the class )
Warm regards,
Chrisir
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 rmbMenu = new RMBMenu();
ArrayList<Line> lines;
final int distFromTop = 60;
// ------------------------------------------------------------------------------------
void setup() {
size(600, 400);
surface.setTitle("EpicTextEditor v0.1");
font = createFont("SourceCodePro-Regular-48.vlw", 14);
textFont(font);
//save = loadImage("saveIcon.png");
//open = loadImage("openIcon.png");
//brush = loadImage("brushIcon2.png");
lines = new ArrayList<Line>();
for (int i = 0; i <= 5; i++) {
lines.add(new Line(i * 20 + distFromTop, i));
}
}//setup
void draw() {
// themes
if (darkt == true) {
theme1 = color(70);
theme2 = color(30);
}
background(theme1);
for (int i = 0; i < lines.size(); i++) {
Line l = lines.get(i);
l.yPos = sb.setScrollObjectY(0, 650) + (i * 20 + distFromTop);
}
for (int i = 0; i < lines.size(); i++) {
Line l = lines.get(i);
l.display();
}
t.display();
sb.display();
// rmbMenu.display();
fill(255);
text ("lines: "
+ lines.size(),
33, 33 ) ;
}//draw
// ------------------------------------------------------------------------------------
// Inputs mouse
void mousePressed() {
sb.mousePress();
rmbMenu.mousePress();
for (int i = 0; i < lines.size(); i++) {
Line l = lines.get(i);
l.mousePress();
}
}
void mouseReleased() {
sb.mouseRelease();
}
void mouseDragged() {
for (int i = 0; i < lines.size(); 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);
}
//--------------------------------------------------------------------------
// Inputs keyboard
void keyPressed() {
// We eval key ENTER and UP and DOWN (LEFT and RIGHT have to be done inside the class) here (basically ALL keys that change the current line number we are in)
if (key == ENTER || key == RETURN) {
// ENTER
key=0;
for (int i = 0; i < lines.size(); i++) {
if ( lines.get(i).selected ) {
// we have the selected line
lines.get(i).selected = false;
if (i==lines.size()-1) {
// add at end
lines.add(new Line(i * 20 + 30, i+1));
lines.get(i+1).selected=true;
} else {
// add in the middle of ArrayList (at position i)
lines.get(i).selected = false;
lines.add(i+1, new Line(i * 20 + 30, i+1));
lines.get(i+1).selected=true;
// when inserting, increase the line numbers below
for (int i2 = i+1; i2 < lines.size(); i2++) {
lines.get(i2).n=i2;
}
}
return; // leave
}
}
return; // leave
}
if (keyCode == UP) {
// UP
for (int i = 0; i < lines.size(); i++) {
if ( lines.get(i).selected ) {
// we have the selected line
if (i>0) {
// act now
lines.get(i).selected = false;
lines.get(i-1).selected = true;
return; // leave
}
}
}
return; // leave
} else if (keyCode == DOWN) {
for (int i = 0; i < lines.size(); i++) {
if ( lines.get(i).selected ) {
if (i<lines.size()-1) {
// act now
lines.get(i).selected = false;
lines.get(i+1).selected = true;
return; // leave
}
}
}
return; // leave
}
// other keys are passed into the object
for (Line l : lines) {
l.keyPress();
}//for
}//func
// ===========================================================================
// classes
class Line {
String text = "";
boolean selected;
float yCtrl;
float yPos;
int n;
boolean blink = true;
int t = 0;
Line(float y1, int n1) {
yPos = y1;
n = n1;
}
void display() {
fill(theme1);
noStroke();
rect(0, yPos, width, 20);
fill(theme2);
rect(0, yPos, 40, 20);
if (selected) {
fill(0, 110);
rect(0, yPos, width, 20);
//blinking line
fill(255);
textSize(14);
t++;
if (t > 30) {
blink = !blink;
t = 0;
}
if (blink)
text("|",
43 + textWidth(text), yPos + 15);
if (keyPressed) {
blink = true;
t = 0;
}
} else t = 0;
fill(150);
textSize(14);
textAlign(RIGHT);
text(nfWithSpace(n, 3), 33, yPos + 15);
textAlign(LEFT);
fill(255);
textSize(14);
text(text, 43, yPos + 15);
}
String nfWithSpace(int n_, int len) {
String s = str(n_);
s=trim(s);
trim(s);
if (s.length()==1)
return " "+s;
if (s.length()==2)
return " "+s;
//
return s;
}
int getn() {
if (selected) return n;
else return -1;
}
void mousePress() {
if (selected &&
rmbMenu.displayb == true &&
mouseX > rmbMenu.x &&
mouseX < rmbMenu.x + 160 &&
mouseY > rmbMenu.y + 42 &&
mouseY < rmbMenu.y + 58) {
text = "";
}
if (! rmbMenu.displayb) {
if (mouseButton == LEFT) {
if (mouseY > 50 &&
mouseY > yPos &&
mouseY < yPos + 20 &&
mouseX < 585)
selected = true;
else if (mouseX < 585) selected = false;
} else if (mouseButton == CENTER) {
if (mouseY > 50 &&
mouseY > yPos &&
mouseY < yPos + 20 &&
mouseX < 585)
selected = true;
}
}
}
void keyPress() {
if (selected) {
// eval key
if (key==BACKSPACE) {
//BACKSPACE
if (text.length()>0) {
text=text.substring(0, text.length()-1);
}
} else if (key != CODED && textWidth(text) < 532) {
// add Text
text+=key;
}
//
}// if (selected == true) {
}//method
void mouseDrag() {
if (rmbMenu.displayb == false) {
if (mouseButton == CENTER || mouseButton == LEFT) {
if (sb.scroll == false &&
mouseX < 585 &&
mouseY > yPos &&
mouseY < yPos + 20) {
selected = true;
blink = true;
}
}
}
}
}
//-----------------------------------------------------------------------------
class RMBMenu {
int x = 0;
int y = 0;
boolean displayb = false;
RMBMenu() {
}
void display() {
if (displayb) {
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);
}
}//class
//end