Skip to content

Commit

Permalink
Fixed Slider rendering when container is closed, made KeybindComponen…
Browse files Browse the repository at this point in the history
…t use different renderTitle method, added option for bottom border, added parameter to getHeight for whether a container is open
  • Loading branch information
lukflug committed Jan 4, 2021
1 parent d7eaf88 commit 9f3b016
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 36 deletions.
14 changes: 7 additions & 7 deletions src/main/java/com/lukflug/panelstudio/CollapsibleContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public void render (Context context) {
if (rect!=null) context.getInterface().restore();
if (subContext.isHovered()) context.setDescription(subContext.getDescription());
context.setHeight(getRenderHeight(subContext.getSize().height));
scrollPosition=renderer.renderScrollBar(context,hasFocus(context),isActive(),scroll,childHeight,scrollPosition);
if (scrollPosition>childHeight-containerHeight) scrollPosition=childHeight-containerHeight;
if (scrollPosition<0) scrollPosition=0;
}
scrollPosition=renderer.renderScrollBar(context,hasFocus(context),isActive(),scroll,childHeight,scrollPosition);
if (scrollPosition>childHeight-containerHeight) scrollPosition=childHeight-containerHeight;
if (scrollPosition<0) scrollPosition=0;
renderer.renderBorder(context,hasFocus(context),isActive(),open.getValue()!=0);
}

Expand All @@ -101,7 +101,7 @@ public void render (Context context) {
*/
@Override
public void handleButton (Context context, int button) {
context.setHeight(renderer.getHeight());
context.setHeight(renderer.getHeight(open.getValue()!=0));
if (context.isClicked() && button==Interface.LBUTTON) {
if (toggle!=null) toggle.toggle();
} else if (context.isHovered() && button==Interface.RBUTTON && context.getInterface().getButton(Interface.RBUTTON)) {
Expand Down Expand Up @@ -205,7 +205,7 @@ protected boolean isActive() {
protected int getContainerOffset() {
if (scrollPosition>childHeight-containerHeight) scrollPosition=childHeight-containerHeight;
if (scrollPosition<0) scrollPosition=0;
return (int)(renderer.getHeight()-scrollPosition-(1-open.getValue())*containerHeight);
return (int)(renderer.getHeight(open.getValue()!=0)-scrollPosition-(1-open.getValue())*containerHeight);
}

/**
Expand All @@ -228,7 +228,7 @@ protected int getRenderHeight (int childHeight) {
scroll=childHeight>containerHeight;
if (scrollPosition>childHeight-containerHeight) scrollPosition=childHeight-containerHeight;
if (scrollPosition<0) scrollPosition=0;
return (int)(containerHeight*open.getValue()+renderer.getHeight());
return (int)(containerHeight*open.getValue()+renderer.getHeight(open.getValue()!=0)+renderer.getBottomBorder());
}

/**
Expand All @@ -238,7 +238,7 @@ protected int getRenderHeight (int childHeight) {
* @return the clipping rectangle
*/
protected Rectangle getClipRect (Context context, int height) {
return new Rectangle(context.getPos().x+renderer.getLeftBorder(scroll),context.getPos().y+renderer.getHeight(),context.getSize().width-renderer.getLeftBorder(scroll)-renderer.getRightBorder(scroll),getRenderHeight(height)-renderer.getHeight());
return new Rectangle(context.getPos().x+renderer.getLeftBorder(scroll),context.getPos().y+renderer.getHeight(open.getValue()!=0),context.getSize().width-renderer.getLeftBorder(scroll)-renderer.getRightBorder(scroll),getRenderHeight(height)-renderer.getHeight(open.getValue()!=0)-renderer.getBottomBorder());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public DraggableContainer(String title, String description, Renderer renderer, T
@Override
public void handleButton (Context context, int button) {
if (bodyDrag) super.handleButton(context, button);
else context.setHeight(renderer.getHeight());
else context.setHeight(renderer.getHeight(open.getValue()!=0));
if (context.isClicked() && button==Interface.LBUTTON) {
dragging=true;
attachPoint=context.getInterface().getMouse();
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/lukflug/panelstudio/FocusableComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public String getTitle() {
*/
@Override
public void render(Context context) {
context.setHeight(renderer.getHeight());
context.setHeight(renderer.getHeight(false));
context.setDescription(description);
}

Expand All @@ -58,7 +58,7 @@ public void render(Context context) {
*/
@Override
public void handleKey(Context context, int scancode) {
context.setHeight(renderer.getHeight());
context.setHeight(renderer.getHeight(false));
}

/**
Expand All @@ -67,7 +67,7 @@ public void handleKey(Context context, int scancode) {
*/
@Override
public void handleButton (Context context, int button) {
context.setHeight(renderer.getHeight());
context.setHeight(renderer.getHeight(false));
updateFocus(context,button);
}

Expand All @@ -76,31 +76,31 @@ public void handleButton (Context context, int button) {
*/
@Override
public void getHeight(Context context) {
context.setHeight(renderer.getHeight());
context.setHeight(renderer.getHeight(false));
}

/**
* Set the height of this component to the height specified by {@link Renderer}.
*/
@Override
public void handleScroll (Context context, int diff) {
context.setHeight(renderer.getHeight());
context.setHeight(renderer.getHeight(false));
}

/**
* Set the height of this component to the height specified by {@link Renderer}.
*/
@Override
public void enter (Context context) {
context.setHeight(renderer.getHeight());
context.setHeight(renderer.getHeight(false));
}

/**
* Set the height of this component to the height specified by {@link Renderer}.
*/
@Override
public void exit (Context context) {
context.setHeight(renderer.getHeight());
context.setHeight(renderer.getHeight(false));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lukflug/panelstudio/Slider.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void render (Context context) {
if (!context.getInterface().getButton(Interface.LBUTTON)) {
attached=false;
}
renderer.renderRect(context,"",hasFocus(context),false,new Rectangle(new Point(context.getPos().x+(int)(context.getSize().width*getValue()),context.getPos().y),new Dimension((int)(context.getSize().width*(1-getValue())),renderer.getHeight())),false);
renderer.renderRect(context,title,hasFocus(context),true,new Rectangle(context.getPos(),new Dimension((int)(context.getSize().width*getValue()),renderer.getHeight())),true);
renderer.renderRect(context,"",hasFocus(context),false,new Rectangle(new Point(context.getPos().x+(int)(context.getSize().width*getValue()),context.getPos().y),new Dimension((int)(context.getSize().width*(1-getValue())),renderer.getHeight(false))),false);
renderer.renderRect(context,title,hasFocus(context),true,new Rectangle(context.getPos(),new Dimension((int)(context.getSize().width*getValue()),renderer.getHeight(false))),true);
}

/**
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/lukflug/panelstudio/hud/HUDPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void handleScroll (Context context, int diff) {
@Override
public Point getPosition (Interface inter) {
position=component.getPosition(inter);
position.translate(0,-renderer.getHeight()-renderer.getOffset());
position.translate(0,-renderer.getHeight(open.getValue()!=0)-renderer.getOffset());
return super.getPosition(inter);
}

Expand All @@ -76,7 +76,7 @@ public Point getPosition (Interface inter) {
*/
@Override
public void setPosition (Interface inter, Point position) {
component.setPosition(inter,new Point(position.x,position.y+renderer.getHeight()+renderer.getOffset()));
component.setPosition(inter,new Point(position.x,position.y+renderer.getHeight(open.getValue()!=0)+renderer.getOffset()));
}

/**
Expand Down Expand Up @@ -143,8 +143,8 @@ public HUDRenderer (Renderer renderer, Toggleable guiOpen, int minBorder) {
* Returns the height defined by the base renderer.
*/
@Override
public int getHeight() {
return renderer.getHeight();
public int getHeight (boolean open) {
return renderer.getHeight(open);
}

/**
Expand All @@ -164,6 +164,14 @@ public int getOffset() {
public int getBorder() {
return Math.max(renderer.getBorder(),minBorder);
}

/**
* Returns the border defined by the base renderer.
*/
@Override
public int getBottomBorder() {
return renderer.getBottomBorder();
}

/**
* Returns the border defined by the base renderer.
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/lukflug/panelstudio/hud/ListComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void render (Context context) {
@Override
public Point getPosition (Interface inter) {
int width=getWidth(inter);
int height=renderer.getHeight()+(list.getSize()-1)*inter.getFontHeight();
int height=renderer.getHeight(false)+(list.getSize()-1)*inter.getFontHeight();
if (lastUp!=list.sortUp()) {
if (list.sortUp()) position.translate(0,height);
else position.translate(0,-height);
Expand All @@ -81,7 +81,7 @@ public Point getPosition (Interface inter) {
@Override
public void setPosition (Interface inter, Point position) {
int width=getWidth(inter);
int height=renderer.getHeight()+(list.getSize()-1)*inter.getFontHeight();
int height=renderer.getHeight(false)+(list.getSize()-1)*inter.getFontHeight();
if (list.sortUp()) {
if (list.sortRight()) this.position=new Point(position.x+width,position.y+height);
else this.position=new Point(position.x,position.y+height);
Expand All @@ -103,7 +103,7 @@ public int getWidth (Interface inter) {

@Override
public void getHeight (Context context) {
context.setHeight(renderer.getHeight()+(list.getSize()-1)*context.getInterface().getFontHeight());
context.setHeight(renderer.getHeight(false)+(list.getSize()-1)*context.getInterface().getFontHeight());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void render (Context context) {
super.render(context);
String text=title+keybind.getKeyName();
if (hasFocus(context)) text=title+"...";
renderer.renderTitle(context,text,hasFocus(context),hasFocus(context));
renderer.renderTitle(context,text,hasFocus(context));
}

/**
Expand All @@ -41,7 +41,7 @@ public void render (Context context) {
@Override
public void handleButton (Context context, int button) {
super.handleButton(context,button);
context.setHeight(renderer.getHeight());
context.setHeight(renderer.getHeight(false));
boolean isSelected=hasFocus(context);
super.handleButton(context,button);
if (isSelected && !hasFocus(context)) keybind.setKey(0);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/lukflug/panelstudio/theme/GameSenseTheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,25 @@ public void renderBorder (Context context, boolean focus, boolean active, boolea
}
if (level==0 || open) {
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x,context.getPos().y+context.getSize().height-1),new Dimension(context.getSize().width,1)),color,color,color,color);
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x,context.getPos().y+getHeight()-1),new Dimension(context.getSize().width,1)),color,color,color,color);
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x,context.getPos().y+getHeight(open)-1),new Dimension(context.getSize().width,1)),color,color,color,color);
}
}

@Override
public int renderScrollBar (Context context, boolean focus, boolean active, boolean scroll, int childHeight, int scrollPosition) {
if (scroll) {
int containerHeight=context.getSize().height-getHeight();
int containerHeight=context.getSize().height-getHeight(true);
int a=(int)(scrollPosition/(double)childHeight*containerHeight);
int b=(int)((scrollPosition+containerHeight)/(double)childHeight*containerHeight);
Color background=getMainColor(focus,false);
Color slider=getMainColor(focus,true);
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x+context.getSize().width-getRightBorder(true),context.getPos().y+getHeight()),new Dimension(getRightBorder(true),a)),background,background,background,background);
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x+context.getSize().width-getRightBorder(true),context.getPos().y+getHeight()+a),new Dimension(getRightBorder(true),b-a)),slider,slider,slider,slider);
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x+context.getSize().width-getRightBorder(true),context.getPos().y+getHeight()+b),new Dimension(getRightBorder(true),context.getSize().height-getHeight()-b)),background,background,background,background);
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x+context.getSize().width-getRightBorder(true),context.getPos().y+getHeight(true)),new Dimension(getRightBorder(true),a)),background,background,background,background);
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x+context.getSize().width-getRightBorder(true),context.getPos().y+getHeight(true)+a),new Dimension(getRightBorder(true),b-a)),slider,slider,slider,slider);
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x+context.getSize().width-getRightBorder(true),context.getPos().y+getHeight(true)+b),new Dimension(getRightBorder(true),context.getSize().height-getHeight(true)-b)),background,background,background,background);
Color color=getDefaultColorScheme().getOutlineColor();
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x+context.getSize().width-getRightBorder(true)-1,context.getPos().y+getHeight()),new Dimension(1,context.getSize().height-getHeight())),color,color,color,color);
context.getInterface().fillRect(new Rectangle(new Point(context.getPos().x+context.getSize().width-getRightBorder(true)-1,context.getPos().y+getHeight(true)),new Dimension(1,context.getSize().height-getHeight(true))),color,color,color,color);
if (context.isClicked() && context.getInterface().getMouse().x>=context.getPos().x+context.getSize().width-getRightBorder(true)) {
return (int)((context.getInterface().getMouse().y-context.getPos().y-getHeight())*childHeight/(double)containerHeight-containerHeight/2.0);
return (int)((context.getInterface().getMouse().y-context.getPos().y-getHeight(true))*childHeight/(double)containerHeight-containerHeight/2.0);
}
}
return scrollPosition;
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/lukflug/panelstudio/theme/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
public interface Renderer {
/**
* Returns the default height for components.
* @param open true if the component in question the the title bar for an open container
* @return the component height
*/
public int getHeight();
public int getHeight (boolean open);

/**
* Returns the vertical space between two components in a container.
Expand All @@ -23,11 +24,17 @@ public interface Renderer {
public int getOffset();

/**
* Returns the right horizontal border around a component in a container.
* Returns the horizontal border around a component in a container.
* @return the horizontal border
*/
public int getBorder();

/**
* Returns the border below a container.
* @return the bottom border
*/
public int getBottomBorder();

/**
* Returns the left horizontal border around an entire container.
* @param scroll whether the container was restricted due to scrolling
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/lukflug/panelstudio/theme/RendererBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public RendererBase (int height, int offset, int border, int left, int right) {
* Get default component height.
*/
@Override
public int getHeight() {
public int getHeight (boolean open) {
return height;
}

Expand All @@ -53,9 +53,18 @@ public int getOffset() {
/**
* Get default component horizontal border.
*/
@Override
public int getBorder() {
return border;
}

/**
* Returns zero.
*/
@Override
public int getBottomBorder() {
return 0;
}

/**
* Get default container left horizontal border.
Expand Down

0 comments on commit 9f3b016

Please sign in to comment.