Skip to content

Commit

Permalink
Added Selector Component
Browse files Browse the repository at this point in the history
  • Loading branch information
CommandJoo committed Nov 26, 2024
1 parent f844738 commit f556270
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/main/java/de/curses/NativeCurses.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,23 @@ public int defineColor(int color, int r, int g, int b) {
public native void drawCorner(int x, int y, int type);
private native void drawTee(int x, int y, int type);
public void drawTee(int x, int y, int type, int color) {
//0 RIGHT POINTING
//1 LEFT POINTING
//2 UP POINTING
//3 DOWN POINTING
//4 CROSS
setColor(color);
drawTee(x,y, type);
}
private native void drawArrow(int x, int y, int type);
public void drawArrow(int x, int y, int type, int color) {
//0 RIGHT POINTING
//1 LEFT POINTING
//2 UP POINTING
//3 DOWN POINTING
setColor(color);
drawArrow(x,y,type);
}

public native void refresh();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/curses/WindowManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void handleKey() throws InterruptedException {
break;
}
for(Window window : windows.values()) {
window.draw();
window.handleKey((char)in);
}
}
}).start();
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/de/curses/window/components/Selector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.curses.window.components;

import de.curses.NativeCurses;
import de.curses.window.Component;

public class Selector extends Component {

private String[] values;
private int index;

public Selector(Window parent, int x, int y, int width, int color, String... values) {
super(parent, x, y, width, 2, color);
this.values = values;
}

@Override
public void draw() {
drawBox(-1);
NativeCurses.instance().drawArrow(x+1, y+1, 0, color);
NativeCurses.instance().drawArrow(x+width-1, y+1, 1, color);
drawCenteredString((width/2), 1, current().substring(0, Math.min(current().length(), width-5)), color);
}

public String current() {
return values[index%values.length];
}

@Override
public boolean handleKey(char ch) {
if(ch == 260) {
if(index-1 <= 0) index=values.length;
index--;
return true;
}else if(ch==261) {
index++;
return true;
}
return false;
}
}
4 changes: 3 additions & 1 deletion src/main/java/de/curses/window/components/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ public int compare(Button o1, Button o2) {
});
return buttons;
}

public Component getComponent(int id) {
return components.getOrDefault(id, null);
}

public Button selected() {
return selected;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/de/johannes/snake/StatsWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.curses.NativeCurses;
import de.curses.util.ColorBuilder;
import de.curses.window.components.Selector;
import de.curses.window.components.Window;

import javax.lang.model.element.Name;
Expand All @@ -12,13 +13,19 @@ public StatsWindow(NativeCurses curses) {
super(null, curses.getWidth() / 2 - (curses.getWidth() / 3)-20, curses.getHeight() / 2 - (curses.getHeight() / 3), 20, curses.getHeight()/3*2, ColorBuilder.create().defineForeground("#7731AF").build());
}

@Override
public void init() {
addComponent(0, new Selector(this, 1,1, width-2, color, "Value1234567890", "Value2345678901", "Value3456789", "Bacon", "Cheese", "Value4567890"));
}

@Override
public void draw() {

}

@Override
public boolean handleKey(char ch) {
this.handleKeyForSub(getComponent(0), ch);
return false;
}
}
23 changes: 23 additions & 0 deletions src/native/de_curses_NativeCurses.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,29 @@ JNIEXPORT void JNICALL Java_de_curses_NativeCurses_drawTee(JNIEnv *, jobject, ji
}
}

JNIEXPORT void JNICALL Java_de_curses_NativeCurses_drawArrow(JNIEnv *, jobject, jint x, jint y, jint type) {
switch(type) {
case 0:
mvaddch(y,x, ACS_LARROW);
break;
case 1:
mvaddch(y,x, ACS_RARROW);
break;
case 2:
mvaddch(y,x, ACS_UARROW);
break;
case 3:
mvaddch(y,x, ACS_DARROW);
break;
default:
break;



}
}


JNIEXPORT void JNICALL Java_de_curses_NativeCurses_refresh(JNIEnv *, jobject) {
refresh();
}
Expand Down
9 changes: 9 additions & 0 deletions src/native/de_curses_NativeCurses.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified src/native/de_curses_NativeCurses.o
Binary file not shown.
Binary file modified src/native/libcurses.so
Binary file not shown.

0 comments on commit f556270

Please sign in to comment.