Skip to content

Commit

Permalink
Remove Focusable and FocusManager and implemented focus request in Co…
Browse files Browse the repository at this point in the history
…ntext and ClickGUI instead, added parameter to setPosition, made Context final
  • Loading branch information
lukflug committed Nov 11, 2020
1 parent cea932c commit db24baf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
46 changes: 30 additions & 16 deletions src/main/java/com/lukflug/panelstudio/ClickGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* All components should be a direct or indirect child of this objects.
* @author lukflug
*/
public class ClickGUI implements FocusManager {
public class ClickGUI {
/**
* List of direct child components (i.e. panels).
* The must all be {@link FixedComponent}.
Expand Down Expand Up @@ -55,9 +55,10 @@ public void addComponent (FixedComponent component) {
*/
public void render() {
int highest=0;
FixedComponent focusComponent=null;
for (int i=components.size()-1;i>=0;i--) {
FixedComponent component=components.get(i);
Context context=new Context(inter,this,width,component.getPosition(inter),true,true);
Context context=new Context(inter,width,component.getPosition(inter),true,true);
component.getHeight(context);
if (context.isHovered()) {
highest=i;
Expand All @@ -66,8 +67,13 @@ public void render() {
}
for (int i=0;i<components.size();i++) {
FixedComponent component=components.get(i);
Context context=new Context(inter,this,width,component.getPosition(inter),true,i>=highest);
Context context=new Context(inter,width,component.getPosition(inter),true,i>=highest);
component.render(context);
if (context.foucsRequested()) focusComponent=component;
}
if (focusComponent!=null) {
components.remove(focusComponent);
components.add(focusComponent);
}
}

Expand All @@ -79,11 +85,17 @@ public void render() {
*/
public void handleButton (int button) {
boolean highest=true;
FixedComponent focusComponent=null;
for (int i=components.size()-1;i>=0;i--) {
FixedComponent component=components.get(i);
Context context=new Context(inter,this,width,component.getPosition(inter),true,highest);
Context context=new Context(inter,width,component.getPosition(inter),true,highest);
component.handleButton(context,button);
if (context.isHovered()) highest=false;
if (context.foucsRequested()) focusComponent=component;
}
if (focusComponent!=null) {
components.remove(focusComponent);
components.add(focusComponent);
}
}

Expand All @@ -93,10 +105,16 @@ public void handleButton (int button) {
*/
public void handleKey (int scancode) {
boolean highest=true;
FixedComponent focusComponent=null;
for (FixedComponent component: components) {
Context context=new Context(inter,this,width,component.getPosition(inter),true,highest);
Context context=new Context(inter,width,component.getPosition(inter),true,highest);
component.handleKey(context,scancode);
if (context.isHovered()) highest=false;
if (context.foucsRequested()) focusComponent=component;
}
if (focusComponent!=null) {
components.remove(focusComponent);
components.add(focusComponent);
}
}

Expand All @@ -105,20 +123,16 @@ public void handleKey (int scancode) {
*/
public void exit() {
boolean highest=true;
FixedComponent focusComponent=null;
for (FixedComponent component: components) {
Context context=new Context(inter,this,width,component.getPosition(inter),true,highest);
Context context=new Context(inter,width,component.getPosition(inter),true,highest);
component.exit(context);
if (context.isHovered()) highest=false;
if (context.foucsRequested()) focusComponent=component;
}
if (focusComponent!=null) {
components.remove(focusComponent);
components.add(focusComponent);
}
}

/**
* Method to be called by a panel that requests focus within the GUI.
* Moves the panel requesting focus to the top.
*/
@Override
public void requestFocus (Focusable component) {
components.remove(component);
components.add((FixedComponent)component);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/lukflug/panelstudio/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* A class for the communication between a component and its parent.
* @author lukflug
*/
public class Context {
public final class Context {
/**
* The current {@link Interface}.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lukflug/panelstudio/DraggableContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Point getPosition (Interface inter) {
* Set the position of the panel.
*/
@Override
public void setPosition(final Point position) {
public void setPosition(Interface inter, Point position) {
this.position=new Point(position);
}

Expand All @@ -80,6 +80,6 @@ public void setPosition(final Point position) {
*/
@Override
protected void handleFocus (Context context, boolean focus) {
if (focus) context.getFocusManager().requestFocus(this);
if (focus) context.requestFocus();
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/lukflug/panelstudio/FixedComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* (i.e. the position isn't determined by the parent via {@link Context}).
* @author lukflug
*/
public interface FixedComponent extends Component,Focusable {
public interface FixedComponent extends Component {
/**
* Get the current component position.
* @param inter current interface
Expand All @@ -17,7 +17,8 @@ public interface FixedComponent extends Component,Focusable {

/**
* Set the current component position.
* @param inter current interface
* @param position new position
*/
public void setPosition (Point position);
public void setPosition (Interface inter, Point position);
}

0 comments on commit db24baf

Please sign in to comment.