Implemented button detection / presses and scaling controller image

template. Setup framework for configuration window.
dev
sigonasr2 7 years ago
parent 42ab9d857f
commit 42673882c9
  1. BIN
      sigIRCv2.jar
  2. 2
      src/sig/BackgroundColorButton.java
  3. 2
      src/sig/ColorPanel.java
  4. 4
      src/sig/MyPanel.java
  5. 69
      src/sig/modules/Controller/Button.java
  6. 18
      src/sig/modules/Controller/ClickableButton.java
  7. 10
      src/sig/modules/Controller/ControlConfigurationWindow.java
  8. 10
      src/sig/modules/Controller/EditMode.java
  9. 23
      src/sig/modules/Controller/clickablebutton/AddClickableButton.java
  10. 24
      src/sig/modules/Controller/clickablebutton/CopyClickableButton.java
  11. 182
      src/sig/modules/ControllerModule.java
  12. 4
      src/sig/utils/DrawUtils.java

Binary file not shown.

@ -44,10 +44,12 @@ public class BackgroundColorButton {
if (ev.getX()>=x && ev.getX()<=x+buttonimg.getWidth() &&
ev.getY()>=y && ev.getY()<=y+buttonimg.getHeight()) {
sigIRC.backgroundcol=sigIRC.colorpanel.getBackgroundColor();
if (sigIRC.backgroundcol!=null) {
sigIRC.config.setProperty("backgroundColor", Integer.toString(sigIRC.backgroundcol.getRGB()));
sigIRC.config.saveProperties();
sigIRC.panel.setBackground(sigIRC.backgroundcol);
}
}
}
}
}

@ -12,7 +12,7 @@ public class ColorPanel extends JPanel{
}
public Color getBackgroundColor() {
return JColorChooser.showDialog(this, "Background Color Picker", Color.CYAN);
return JColorChooser.showDialog(this, "Background Color Picker", sigIRC.backgroundcol);
}
public Dimension getPreferredSize() {

@ -39,8 +39,8 @@ public class MyPanel extends JPanel implements MouseListener, ActionListener, Mo
final public static Font programFont = new Font(sigIRC.messageFont,0,24);
final public static Font userFont = new Font(sigIRC.usernameFont,0,16);
final public static Font smallFont = new Font(sigIRC.touhoumotherConsoleFont,0,12);
int lastMouseX = 0;
int lastMouseY = 0;
public int lastMouseX = 0;
public int lastMouseY = 0;
public MyPanel() {
//setBorder(BorderFactory.createLineBorder(Color.black));

@ -2,9 +2,12 @@ package sig.modules.Controller;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import net.java.games.input.Component.Identifier;
import net.java.games.input.Component;
import net.java.games.input.Controller;
import sig.sigIRC;
import sig.modules.ControllerModule;
public class Button {
@ -18,11 +21,19 @@ public class Button {
ControllerModule parent;
boolean square;
public Button(double pct_x, double pct_width, double pct_y, double pct_height, Controller parent_controller, Identifier button_identifier, Color col, ControllerModule module) {
this(pct_x,pct_width,pct_y,pct_height,parent_controller,button_identifier,col,module,false);
public Button(Rectangle2D.Double rect, Controller parent_controller, Identifier button_identifier, Color col, ControllerModule module) {
this(rect.getX(),rect.getY(),rect.getWidth(),rect.getHeight(),parent_controller,button_identifier,col,module,false);
}
public Button(double pct_x, double pct_width, double pct_y, double pct_height, Controller parent_controller, Identifier button_identifier, Color col, ControllerModule module, boolean square) {
public Button(Rectangle2D.Double rect, Controller parent_controller, Identifier button_identifier, Color col, ControllerModule module, boolean square) {
this(rect.getX(),rect.getY(),rect.getWidth(),rect.getHeight(),parent_controller,button_identifier,col,module,square);
}
public Button(double pct_x, double pct_y, double pct_width, double pct_height, Controller parent_controller, Identifier button_identifier, Color col, ControllerModule module) {
this(pct_x,pct_y,pct_width,pct_height,parent_controller,button_identifier,col,module,false);
}
public Button(double pct_x, double pct_y, double pct_width, double pct_height, Controller parent_controller, Identifier button_identifier, Color col, ControllerModule module, boolean square) {
this.pct_x = pct_x;
this.pct_y = pct_y;
this.pct_width=pct_width;
@ -39,12 +50,56 @@ public class Button {
Color col_identity = g.getColor();
g.setColor(pressed_col);
g.fillOval((int)(parent.getPosition().getX()
+parent.getPosition().getWidth()*pct_x)
+parent.getControllerImage().getWidth(sigIRC.panel)*pct_x)
,(int)(parent.getPosition().getY()
+parent.getPosition().getWidth()*pct_y)
,(int)(parent.getPosition().getWidth()*pct_width),
(int)(parent.getPosition().getWidth()*pct_height));
+parent.getControllerImage().getHeight(sigIRC.panel)*pct_y)
,(int)(parent.getControllerImage().getWidth(sigIRC.panel)*pct_width),
(int)(parent.getControllerImage().getHeight(sigIRC.panel)*pct_height));
g.setColor(col_identity);
}
}
public String getSaveString() {
StringBuilder sb = new StringBuilder();
sb.append(pct_x);sb.append(",");
sb.append(pct_y);sb.append(",");
sb.append(pct_width);sb.append(",");
sb.append(pct_height);sb.append(",");
sb.append(ident.getName());sb.append(",");
sb.append(pressed_col.getRed());sb.append(",");
sb.append(pressed_col.getGreen());sb.append(",");
sb.append(pressed_col.getBlue());sb.append(",");
sb.append(pressed_col.getAlpha());sb.append(",");
sb.append(square);
return sb.toString();
}
public static Button loadFromString(String s, Controller controller, ControllerModule module) {
String[] split = s.split(",");
return new Button(
Double.parseDouble(split[0]),
Double.parseDouble(split[1]),
Double.parseDouble(split[2]),
Double.parseDouble(split[3]),
controller,
GrabIdentifierFromString(split[4],controller),
new Color(
Integer.parseInt(split[5]),
Integer.parseInt(split[6]),
Integer.parseInt(split[7]),
Integer.parseInt(split[8])
),
module,
Boolean.parseBoolean(split[9]));
}
private static Identifier GrabIdentifierFromString(String string, Controller controller) {
for (Component cp : controller.getComponents()) {
Identifier id = cp.getIdentifier();
if (id.getName().equals(string)) {
return id;
}
}
return null;
}
}

@ -11,9 +11,9 @@ import sig.utils.DrawUtils;
import sig.utils.TextUtils;
public class ClickableButton {
int x,y,width,height;
String label;
ControllerModule module;
protected int x,y,width,height;
protected String label;
protected ControllerModule module;
public ClickableButton(Rectangle position, String button_label, ControllerModule parent_module) {
this.x = (int)position.getX();
@ -25,14 +25,14 @@ public class ClickableButton {
}
public void onClickEvent(MouseEvent ev) {
if (mouseInsideBounds(ev)) {
System.out.println("Click performed!");
}
/*if (mouseInsideBounds(ev)) {
//System.out.println("Click performed!");
}*/
}
private boolean mouseInsideBounds(MouseEvent ev) {
return ev.getX()>=x && ev.getX()<=x+width &&
ev.getY()>=y && ev.getY()<=y+height;
protected boolean mouseInsideBounds(MouseEvent ev) {
return ev.getX()>=module.getPosition().getX()+x && ev.getX()<=module.getPosition().getX()+x+width &&
ev.getY()>=module.getPosition().getY()+y && ev.getY()<=module.getPosition().getY()+y+height;
}
public void draw(Graphics g) {

@ -0,0 +1,10 @@
package sig.modules.Controller;
import javax.swing.JFrame;
public class ControlConfigurationWindow extends JFrame{
public ControlConfigurationWindow() {
this.setVisible(true);
}
}

@ -0,0 +1,10 @@
package sig.modules.Controller;
public enum EditMode {
DRAGSELECTION, //Drag to set size of button.
SELECTION, //Selects a button for editing
DELETESELECTION, //Delete a button.
BUTTONSET, //Asks for a controller button to set this button to.
COLORSET,
DEFAULT;
}

@ -0,0 +1,23 @@
package sig.modules.Controller.clickablebutton;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import sig.modules.ControllerModule;
import sig.modules.Controller.ClickableButton;
import sig.modules.Controller.EditMode;
public class AddClickableButton extends ClickableButton{
public AddClickableButton(Rectangle position, String button_label, ControllerModule parent_module) {
super(position, button_label, parent_module);
}
public void onClickEvent(MouseEvent ev) {
super.onClickEvent(ev);
if (mouseInsideBounds(ev)) {
module.setMode(EditMode.DRAGSELECTION);
module.resetDragPoints();
}
}
}

@ -0,0 +1,24 @@
package sig.modules.Controller.clickablebutton;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import sig.modules.ControllerModule;
import sig.modules.Controller.ClickableButton;
import sig.modules.Controller.ControlConfigurationWindow;
import sig.modules.Controller.EditMode;
public class CopyClickableButton extends ClickableButton{
ControlConfigurationWindow window;
public CopyClickableButton(Rectangle position, String button_label, ControllerModule parent_module) {
super(position, button_label, parent_module);
}
public void onClickEvent(MouseEvent ev) {
super.onClickEvent(ev);
if (mouseInsideBounds(ev)) {
window = new ControlConfigurationWindow();
}
}
}

@ -1,8 +1,10 @@
package sig.modules;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
@ -22,6 +24,9 @@ import sig.Module;
import sig.sigIRC;
import sig.modules.Controller.Button;
import sig.modules.Controller.ClickableButton;
import sig.modules.Controller.EditMode;
import sig.modules.Controller.clickablebutton.AddClickableButton;
import sig.modules.Controller.clickablebutton.CopyClickableButton;
import sig.utils.DrawUtils;
import sig.utils.FileUtils;
@ -32,6 +37,13 @@ public class ControllerModule extends Module{
double imgratio = 1;
List<Button> buttons = new ArrayList<Button>();
List<ClickableButton> click_buttons = new ArrayList<ClickableButton>();
EditMode MODE = EditMode.DEFAULT;
String status = "";
Point start_drag,end_drag;
Rectangle2D.Double stored_rect;
Identifier stored_controller_button;
Color buttoncol;
Controller controller;
public ControllerModule(Rectangle2D bounds, String moduleName) {
super(bounds, moduleName);
@ -49,27 +61,98 @@ public class ControllerModule extends Module{
}
try {
controller_img = ImageIO.read(new File(CONTROLLERPATH+"controller_template.png")).getScaledInstance((int)position.getWidth(), -1, 0);
//System.out.println("Size of controller: "+controller_img.getWidth(sigIRC.panel)+","+controller_img.getHeight(sigIRC.panel));
} catch (IOException e) {
e.printStackTrace();
}
buttons.add(new Button(0.1,0.05,0.1,0.05,controllers.get(0),Identifier.Button._3,Color.RED,this));
//buttons.add(new Button(0.1,0.05,0.1,0.05,controllers.get(0),Identifier.Button._3,Color.RED,this));
LoadButtonData();
click_buttons.add(new AddClickableButton(new Rectangle(
0,(int)position.getHeight()-41,96,20),"Add Button",this));
click_buttons.add(new CopyClickableButton(new Rectangle(
97,(int)position.getHeight()-41,96,20),"Copy Button",this));
click_buttons.add(new ClickableButton(new Rectangle(
0,0,96,20),"Test",this));
0,(int)position.getHeight()-20,96,20),"Delete Button",this));
click_buttons.add(new ClickableButton(new Rectangle(
97,(int)position.getHeight()-20,96,20),"Edit Button",this));
}
public void resetDragPoints() {
this.start_drag=null;
this.end_drag=null;
}
public void setDragPoints(Point startpoint,Point endpoint) {
this.start_drag=startpoint;
this.end_drag=endpoint;
}
public EditMode getMode() {
return MODE;
}
public void setMode(EditMode mode) {
MODE = mode;
}
public void ApplyConfigWindowProperties() {
sigIRC.controllermodule_X=(int)position.getX();
sigIRC.controllermodule_Y=(int)position.getY();
sigIRC.config.setInteger("CONTROLLER_module_X", sigIRC.controllermodule_X);
sigIRC.config.setInteger("CONTROLLER_module_Y", sigIRC.controllermodule_Y);
}
public void mousePressed(MouseEvent ev) {
if (mouseInsideBounds(ev)) {
switch (MODE) {
case DRAGSELECTION:{
if (start_drag==null) {
start_drag = new Point((int)(ev.getX()-getPosition().getX()),(int)(ev.getY()-getPosition().getY()));
}
}break;
}
}
super.mousePressed(ev);
if (MODE==EditMode.DEFAULT) {
for (ClickableButton cb : click_buttons) {
cb.onClickEvent(ev);
}
}
}
public void mouseReleased(MouseEvent ev) {
super.mouseReleased(ev);
if (mouseInsideBounds(ev)) {
switch (MODE) {
case DRAGSELECTION:{
if (start_drag!=null) {
end_drag = new Point((int)(ev.getX()-getPosition().getX()),(int)(ev.getY()-getPosition().getY()));
double width = (end_drag.getX()-start_drag.getX())/controller_img.getWidth(sigIRC.panel);
double height = (end_drag.getY()-start_drag.getY())/controller_img.getHeight(sigIRC.panel);
stored_rect = new Rectangle2D.Double(
(width<0)?end_drag.getX()/controller_img.getWidth(sigIRC.panel):start_drag.getX()/controller_img.getWidth(sigIRC.panel),
(height<0)?end_drag.getY()/controller_img.getHeight(sigIRC.panel):start_drag.getY()/controller_img.getHeight(sigIRC.panel),
Math.abs((end_drag.getX()-start_drag.getX())/controller_img.getWidth(sigIRC.panel)),
Math.abs((end_drag.getY()-start_drag.getY())/controller_img.getHeight(sigIRC.panel)));
//buttons.add(new Button(pct_rect.getX(),pct_rect.getY(),pct_rect.getWidth(),pct_rect.getHeight(),controllers.get(0),Identifier.Button._3,Color.RED,this));
//resetDragPoints();
MODE=EditMode.BUTTONSET;
}
}
}
}
}
protected boolean mouseInsideBounds(MouseEvent ev) {
return ev.getX()>=getPosition().getX() && ev.getX()<=getPosition().getX()+getPosition().getWidth() &&
ev.getY()>=getPosition().getY() && ev.getY()<=getPosition().getY()+getPosition().getHeight();
}
public Rectangle2D getPosition() {
return position;
}
public void run() {
super.run();
for (Controller c : controllers) {
//System.out.println("Data for "+c.getName()+" ("+c.getType()+"):");
c.poll();
@ -82,6 +165,53 @@ public class ControllerModule extends Module{
}
}*/
}
switch (MODE) {
case DRAGSELECTION:{
int cursortype = sigIRC.panel.getCursor().getType();
if (cursortype!=Cursor.CROSSHAIR_CURSOR) {
sigIRC.panel.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
status="Drag a button onto the controller template.";
}break;
case BUTTONSET:{
sigIRC.panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
status="Press controller button to set button";
}break;
case COLORSET:{
status="Select a color from the panel.";
}break;
default:{
status="";
int cursortype = sigIRC.panel.getCursor().getType();
if (cursortype!=Cursor.DEFAULT_CURSOR) {
sigIRC.panel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
}
super.run();
if (MODE==EditMode.BUTTONSET) {
stored_controller_button=null;
for (Controller c : controllers) {
for (Component cp : c.getComponents()) {
if (!cp.isAnalog() && cp.getPollData()==1.0f) {
stored_controller_button = cp.getIdentifier();
controller=c;
MODE=EditMode.COLORSET;
buttoncol = PopupColorPanel();
AddButton();
MODE=EditMode.DEFAULT;
break;
}
}
if (stored_controller_button!=null) {
break;
}
}
}
}
public Image getControllerImage() {
return controller_img;
}
public void draw(Graphics g) {
@ -95,12 +225,56 @@ public class ControllerModule extends Module{
}
}
}
g.drawImage(controller_img, (int)(position.getX()+1), (int)(position.getY()+8), sigIRC.panel);
g.drawImage(controller_img, (int)(position.getX()), (int)(position.getY()), sigIRC.panel);
DrawUtils.drawText(g, position.getX(), position.getY()+8, Color.BLACK, status);
for (Button b : buttons) {
b.draw(g);
}
for (ClickableButton cb : click_buttons) {
cb.draw(g);
}
if (MODE==EditMode.DRAGSELECTION) {
if (start_drag!=null) {
Color color_identity = g.getColor();
g.setColor(Color.GRAY);
int width = sigIRC.panel.lastMouseX-
((int)position.getX()+(int)start_drag.getX());
int height = sigIRC.panel.lastMouseY-
((int)position.getY()+(int)start_drag.getY());
g.fillOval(
(width<0)?sigIRC.panel.lastMouseX:(int)position.getX()+(int)start_drag.getX(),
(height<0)?sigIRC.panel.lastMouseY:(int)position.getY()+(int)start_drag.getY(),
Math.abs(width), Math.abs(height));
g.setColor(color_identity);
}
}
}
private void LoadButtonData() {
String[] buttondata = FileUtils.readFromFile(CONTROLLERPATH+"button_data.txt");
if (controllers.size()>0) {
for (String s : buttondata) {
if (s.length()>0) {
buttons.add(Button.loadFromString(s, controllers.get(0), this));
}
}
}
}
private void AddButton() {
buttons.add(new Button(stored_rect,controller,stored_controller_button,buttoncol,this));
StringBuilder sb = new StringBuilder();
for (Button b : buttons) {
sb.append(b.getSaveString()+"\n");
}
FileUtils.writetoFile(new String[]{sb.toString()}, CONTROLLERPATH+"button_data.txt");
}
private Color PopupColorPanel() {
Color col=null;
do {
col=sigIRC.colorpanel.getBackgroundColor();
} while (col==null);
return col;
}
}

@ -43,17 +43,21 @@ public class DrawUtils {
g2.drawString(as.getIterator(),(int)x,(int)y);
}
public static void drawText(Graphics g, double x, double y, Color color, String message) {
if (message.length()>0) {
AttributedString as = new AttributedString(message);
as.addAttribute(TextAttribute.FONT, MyPanel.programFont);
g.setColor(color);
g.drawString(as.getIterator(),(int)x,(int)y);
}
}
public static void drawTextFont(Graphics g, Font font, double x, double y, Color color, String message) {
if (message.length()>0) {
AttributedString as = new AttributedString(message);
as.addAttribute(TextAttribute.FONT, font);
g.setColor(color);
g.drawString(as.getIterator(),(int)x,(int)y);
}
}
public static void drawHealthbar(Graphics g, Rectangle bounds, double pct, Color healthbarcol) {
g.setColor(Color.BLACK);
g.draw3DRect((int)bounds.getX(), (int)bounds.getY(), (int)bounds.getWidth(), (int)bounds.getHeight(), true);

Loading…
Cancel
Save