Skip to content

Commit

Permalink
Added resizable components, made color containers change color, added…
Browse files Browse the repository at this point in the history
… key controls to radio buttons
  • Loading branch information
lukflug committed Apr 25, 2021
1 parent c1eb0be commit 5325c67
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public CSGOLayout (ILabeled label, Point position, int width, int popupWidth, Su

@Override
public void populateGUI (IComponentAdder gui, IComponentGenerator components, IClient client, ITheme theme) {
Button title=new Button(label,theme.getButtonRenderer(Void.class,0,0,true));
Button<Void> title=new Button<Void>(label,()->null,theme.getButtonRenderer(Void.class,0,0,true));
HorizontalContainer window=new HorizontalContainer(label,theme.getContainerRenderer(0,horizontal?1:0,true));
IEnumSetting catSelect;
if (horizontal) {
Expand All @@ -82,7 +82,7 @@ public void populateGUI (IComponentAdder gui, IComponentGenerator components, IC
category.getModules().forEach(module->{
int graphicalLevel=1;
FocusableComponent moduleTitle;
if (module.isEnabled()==null) moduleTitle=new Button(module,theme.getButtonRenderer(Void.class,1,1,true));
if (module.isEnabled()==null) moduleTitle=new Button<Void>(module,()->null,theme.getButtonRenderer(Void.class,1,1,true));
else moduleTitle=new ToggleButton(module,module.isEnabled(),theme.getButtonRenderer(Boolean.class,1,1,true));
VerticalContainer moduleContainer=new VerticalContainer(module,theme.getContainerRenderer(1,graphicalLevel,false));
if (module.isEnabled()==null) util.addContainer(module,moduleTitle,moduleContainer,()->null,Void.class,categoryContent,gui,new ThemeTuple(theme,1,graphicalLevel),ChildMode.DOWN);
Expand All @@ -99,7 +99,7 @@ protected <T> void addSettingsComponent (ISetting<T> setting, VerticalContainer
IComponent component=components.getComponent(setting,animation,theme,colorLevel,isContainer);
if (component instanceof VerticalContainer) {
VerticalContainer colorContainer=(VerticalContainer)component;
Button button=new Button(setting,theme.getButtonRenderer(Void.class,colorType==ChildMode.DOWN));
Button<T> button=new Button<T>(setting,()->setting.getSettingState(),theme.getButtonRenderer(setting.getSettingClass(),colorType==ChildMode.DOWN));
util.addContainer(setting,button,colorContainer,()->setting.getSettingState(),setting.getSettingClass(),container,gui,new ThemeTuple(theme.theme,theme.logicalLevel,colorLevel),colorType);
if (setting.getSubSettings()!=null) setting.getSubSettings().forEach(subSetting->addSettingsComponent(subSetting,colorContainer,gui,components,new ThemeTuple(theme.theme,theme.logicalLevel+1,colorLevel+1)));
} else if (setting.getSubSettings()!=null) {
Expand Down Expand Up @@ -135,6 +135,12 @@ public IBoolean isVisible() {
public void increment() {
state=(state+1)%array.length;
}

@Override
public void decrement() {
state-=1;
if (state<0) state=array.length-1;
}

@Override
public String getValueName() {
Expand All @@ -156,7 +162,19 @@ public ILabeled[] getAllowedValues() {
return array;
}
};
RadioButton button=new RadioButton(setting,theme.getRadioRenderer(true),animation.get(),horizontal);
RadioButton button=new RadioButton(setting,theme.getRadioRenderer(true),animation.get(),horizontal) {
@Override
protected boolean isUpKey (int key) {
if (horizontal) return isLeftKey(key);
else return CSGOLayout.this.isUpKey(key);
}

@Override
protected boolean isDownKey (int key) {
if (horizontal) return isRightKey(key);
else return CSGOLayout.this.isDownKey(key);
}
};
window.addComponent(container.apply(button),visible);
return setting;
}
Expand All @@ -174,4 +192,20 @@ protected Void getState() {
}
},0,weight);
}

protected boolean isUpKey (int key) {
return false;
}

protected boolean isDownKey (int key) {
return false;
}

protected boolean isLeftKey (int key) {
return false;
}

protected boolean isRightKey (int key) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected <T> void addContainer (ILabeled label, IComponent title, IComponent co
case POPUP:
case DRAG_POPUP:
toggle=new SimpleToggleable(false);
Button button=new Button(new Labeled(label.getDisplayName(),label.getDescription(),()->drawTitle&&label.isVisible().isOn()),theme.getButtonRenderer(Void.class,true));
Button<T> button=new Button<T>(new Labeled(label.getDisplayName(),label.getDescription(),()->drawTitle&&label.isVisible().isOn()),state,theme.getButtonRenderer(stateClass,true));
if (popupType.dynamicPopup) popup=ClosableComponent.createDynamicPopup(button,container,state,animation.get(),new RendererTuple<T>(stateClass,theme),popupType.popupSize,toggle,width);
else popup=ClosableComponent.createStaticPopup(button,container,state,animation.get(),new RendererTuple<T>(stateClass,theme),popupType.popupSize,toggle,width,false,"");
parent.addComponent(new ComponentProxy<IComponent>(title) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public default IComponent getComponent (ISetting<?> setting, Supplier<Animation>
} else if (setting instanceof IKeybindSetting) {
return getKeybindComponent((IKeybindSetting)setting,animation,theme,colorLevel,isContainer);
} else {
return new Button(setting,theme.getButtonRenderer(Void.class,isContainer));
return new Button<Void>(setting,()->null,theme.getButtonRenderer(Void.class,isContainer));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lukflug.panelstudio.layout;

import java.awt.Dimension;
import java.awt.Point;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
Expand All @@ -23,7 +22,6 @@ public class PanelAdder implements IComponentAdder {
protected boolean open;
protected IBoolean isVisible;
protected UnaryOperator<String> configName;
protected Dimension minSize=new Dimension(20,20);

public PanelAdder (IContainer<? super IFixedComponent> container, boolean open, IBoolean isVisible, UnaryOperator<String> configName) {
this.container=container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void populateGUI (IComponentAdder gui, IComponentGenerator components, IC
Point pos=start;
AtomicInteger skipY=new AtomicInteger(this.skipY);
client.getCategories().forEach(category->{
Button categoryTitle=new Button(category,theme.getButtonRenderer(Void.class,0,0,true));
Button<Void> categoryTitle=new Button<Void>(category,()->null,theme.getButtonRenderer(Void.class,0,0,true));
VerticalContainer categoryContent=new VerticalContainer(category,theme.getContainerRenderer(0,0,false));
gui.addComponent(categoryTitle,categoryContent,new ThemeTuple(theme,0,0),new Point(pos),width,animation);
pos.translate(skipX,skipY.get());
Expand All @@ -53,7 +53,7 @@ public void populateGUI (IComponentAdder gui, IComponentGenerator components, IC
ChildMode mode=layoutType.apply(0);
int graphicalLevel=(mode==ChildMode.DOWN)?1:0;
FocusableComponent moduleTitle;
if (module.isEnabled()==null) moduleTitle=new Button(module,theme.getButtonRenderer(Void.class,1,1,mode==ChildMode.DOWN));
if (module.isEnabled()==null) moduleTitle=new Button<Void>(module,()->null,theme.getButtonRenderer(Void.class,1,1,mode==ChildMode.DOWN));
else moduleTitle=new ToggleButton(module,module.isEnabled(),theme.getButtonRenderer(Boolean.class,1,1,mode==ChildMode.DOWN));
VerticalContainer moduleContainer=new VerticalContainer(module,theme.getContainerRenderer(1,graphicalLevel,false));
if (module.isEnabled()==null) util.addContainer(module,moduleTitle,moduleContainer,()->null,Void.class,categoryContent,gui,new ThemeTuple(theme,1,graphicalLevel),layoutType.apply(0));
Expand All @@ -70,7 +70,7 @@ protected <T> void addSettingsComponent (ISetting<T> setting, VerticalContainer
IComponent component=components.getComponent(setting,animation,new ThemeTuple(theme.theme,theme.logicalLevel,theme.graphicalLevel),colorLevel,isContainer);
if (component instanceof VerticalContainer) {
VerticalContainer colorContainer=(VerticalContainer)component;
Button button=new Button(setting,theme.getButtonRenderer(Void.class,colorType.apply(theme.logicalLevel-1)==ChildMode.DOWN));
Button<T> button=new Button<T>(setting,()->setting.getSettingState(),theme.getButtonRenderer(setting.getSettingClass(),colorType.apply(theme.logicalLevel-1)==ChildMode.DOWN));
util.addContainer(setting,button,colorContainer,()->setting.getSettingState(),setting.getSettingClass(),container,gui,new ThemeTuple(theme.theme,theme.logicalLevel,colorLevel),colorType.apply(theme.logicalLevel-1));
if (setting.getSubSettings()!=null) setting.getSubSettings().forEach(subSetting->addSettingsComponent(subSetting,colorContainer,gui,components,new ThemeTuple(theme.theme,theme.logicalLevel+1,colorLevel+1)));
} else if (setting.getSubSettings()!=null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public StackedPanelAdder (IContainer<? super IFixedComponent> container, ILabele
content=new VerticalContainer(label,theme.getContainerRenderer(-1,-1,true));
IResizable size=getResizable(width);
IScrollSize scrollSize=getScrollSize(size);
container.addComponent(ResizableComponent.createResizableComponent(new Button(label,theme.getButtonRenderer(Void.class,-1,-1,true)),content,()->null,new AnimatedToggleable(new SimpleToggleable(true),animation.get()),new RendererTuple<Void>(Void.class,new ThemeTuple(theme,-1,-1)),theme.getResizeRenderer(),size,scrollSize,position,width,true,configName),isVisible);
container.addComponent(ResizableComponent.createResizableComponent(new Button<Void>(label,()->null,theme.getButtonRenderer(Void.class,-1,-1,true)),content,()->null,new AnimatedToggleable(new SimpleToggleable(true),animation.get()),new RendererTuple<Void>(Void.class,new ThemeTuple(theme,-1,-1)),theme.getResizeRenderer(),size,scrollSize,position,width,true,configName),isVisible);
util=new ChildUtil(width,animation,new PopupTuple(popupPos,false,this));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Setting representing a color.
* @author lukflug
*/
public interface IColorSetting extends ISetting<Void> {
public interface IColorSetting extends ISetting<Color> {
/**
* Get the current value for the color setting.
* @return the current color
Expand Down Expand Up @@ -62,12 +62,12 @@ public default boolean hasHSBModel() {
}

@Override
public default Void getSettingState() {
return null;
public default Color getSettingState() {
return getValue();
}

@Override
public default Class<Void> getSettingClass() {
return Void.class;
public default Class<Color> getSettingClass() {
return Color.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface IEnumSetting extends ISetting<String> {
*/
public void increment();

public void decrement();

/**
* Get the current value.
* @return the name of the current enum value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public void renderButton(Context context, String title, boolean focus, T state)
}
Color color=getFontColor(focus);
if (type==Boolean.class && (Boolean)state==true) color=getMainColor(focus,true);
else if (type==Color.class) color=(Color)state;
if (graphicalLevel>0) renderOverlay(context);
if (type==String.class) context.getInterface().drawString(new Point(context.getRect().x+padding,context.getRect().y+padding),height,title+separator+state,color);
else context.getInterface().drawString(new Point(context.getRect().x+padding,context.getRect().y+padding),height,title,color);
Expand Down Expand Up @@ -239,8 +240,22 @@ public int getDefaultHeight(ILabeled[] items, boolean horizontal) {

@Override
public IResizeBorderRenderer getResizeRenderer() {
// TODO Auto-generated method stub
return null;
return new IResizeBorderRenderer() {
@Override
public void drawBorder(Context context, boolean focus) {
Color color=getBackgroundColor(focus);
Rectangle rect=context.getRect();
context.getInterface().fillRect(new Rectangle(rect.x,rect.y,rect.width,getBorder()),color,color,color,color);
context.getInterface().fillRect(new Rectangle(rect.x,rect.y+rect.height-getBorder(),rect.width,getBorder()),color,color,color,color);
context.getInterface().fillRect(new Rectangle(rect.x,rect.y+getBorder(),getBorder(),rect.height-2*getBorder()),color,color,color,color);
context.getInterface().fillRect(new Rectangle(rect.x+rect.width-getBorder(),rect.y+getBorder(),getBorder(),rect.height-2*getBorder()),color,color,color,color);
}

@Override
public int getBorder() {
return 2;
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public GameSenseTheme (IColorScheme scheme, int height, int padding, int scroll,
scheme.createSetting(this,"Font Color","The main color for text.",false,true,new Color(255,255,255),false);
}

protected void fillBaseRect (Context context, boolean focus, boolean active, int logicalLevel, int graphicalLevel) {
protected void fillBaseRect (Context context, boolean focus, boolean active, int logicalLevel, int graphicalLevel, Color colorState) {
Color color=getMainColor(focus,active);
if (logicalLevel>1 && !active) color=getBackgroundColor(focus);
else if (graphicalLevel<=0 && active) color=ITheme.combineColors(getColor(scheme.getColor("Title Color")),scheme.getColor("Enabled Color"));
if (colorState!=null) color=ITheme.combineColors(colorState,scheme.getColor("Enabled Color"));
context.getInterface().fillRect(context.getRect(),color,color,color,color);
}

Expand Down Expand Up @@ -166,8 +167,9 @@ public <T> IButtonRenderer<T> getButtonRenderer (Class<T> type, int logicalLevel
return new IButtonRenderer<T>() {
@Override
public void renderButton(Context context, String title, boolean focus, T state) {
if (type==Boolean.class) fillBaseRect(context,focus,(Boolean)state,logicalLevel,graphicalLevel);
else fillBaseRect(context,focus,graphicalLevel<=0,logicalLevel,graphicalLevel);
if (type==Boolean.class) fillBaseRect(context,focus,(Boolean)state,logicalLevel,graphicalLevel,null);
else if (type==Color.class) fillBaseRect(context,focus,graphicalLevel<=0,logicalLevel,graphicalLevel,(Color)state);
else fillBaseRect(context,focus,graphicalLevel<=0,logicalLevel,graphicalLevel,null);
if (graphicalLevel<=0 && container) {
Color color=scheme.getColor("Outline Color");
context.getInterface().fillRect(new Rectangle(context.getPos().x,context.getPos().y+context.getSize().height-1,context.getSize().width,1),color,color,color,color);
Expand All @@ -189,7 +191,7 @@ public IButtonRenderer<String> getKeybindRenderer(int logicalLevel, int graphica
return new IButtonRenderer<String>() {
@Override
public void renderButton(Context context, String title, boolean focus, String state) {
fillBaseRect(context,focus,focus,logicalLevel,graphicalLevel);
fillBaseRect(context,focus,focus,logicalLevel,graphicalLevel,null);
renderOverlay(context);
context.getInterface().drawString(new Point(context.getRect().x+padding,context.getRect().y+padding),height,title+separator+(focus?"...":state),getFontColor(focus));
}
Expand Down Expand Up @@ -231,7 +233,7 @@ public void renderItem (Context context, ILabeled[] items, boolean focus, int ta
Rectangle rect=getItemRect(context,items,i,horizontal);
Context subContext=new Context(context.getInterface(),rect.width,rect.getLocation(),context.hasFocus(),context.onTop());
subContext.setHeight(rect.height);
fillBaseRect(subContext,focus,i==target,logicalLevel,graphicalLevel);
fillBaseRect(subContext,focus,i==target,logicalLevel,graphicalLevel,null);
renderOverlay(subContext);
context.getInterface().drawString(new Point(rect.x+padding,rect.y+padding),height,items[i].getDisplayName(),getFontColor(focus));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.lukflug.panelstudio.widget;

import java.util.function.Supplier;

import com.lukflug.panelstudio.base.Context;
import com.lukflug.panelstudio.component.FocusableComponent;
import com.lukflug.panelstudio.setting.ILabeled;
Expand All @@ -9,26 +11,28 @@
* Button widget class.
* @author lukflug
*/
public class Button extends FocusableComponent {
public class Button<T> extends FocusableComponent {
protected Supplier<T> state;
/**
* Renderer for this component.
*/
protected IButtonRenderer<Void> renderer;
protected IButtonRenderer<T> renderer;

/**
* Constructor.
* @param label the label for the component
* @param renderer the renderer for this component
*/
public Button(ILabeled label, IButtonRenderer<Void> renderer) {
public Button(ILabeled label, Supplier<T> state, IButtonRenderer<T> renderer) {
super(label);
this.renderer=renderer;
this.state=state;
}

@Override
public void render (Context context) {
super.render(context);
renderer.renderButton(context,getTitle(),hasFocus(context),null);
renderer.renderButton(context,getTitle(),hasFocus(context),state.get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.lukflug.panelstudio.setting.IEnumSetting;
import com.lukflug.panelstudio.theme.IRadioRenderer;

public class RadioButton extends FocusableComponent {
public abstract class RadioButton extends FocusableComponent {
protected IEnumSetting setting;
protected IRadioRenderer renderer;
protected AnimatedEnum animation;
Expand Down Expand Up @@ -40,10 +40,22 @@ public void handleButton (Context context, int button) {
}
}
}

@Override
public void handleKey (Context context, int key) {
super.handleKey(context,key);
if (context.hasFocus()) {
if (isUpKey(key)) setting.decrement();
else if (isDownKey(key)) setting.increment();
}
}

@Override
protected int getHeight() {
return renderer.getDefaultHeight(setting.getAllowedValues(),horizontal);
}


protected abstract boolean isUpKey (int key);

protected abstract boolean isDownKey (int key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,22 @@ public void handleButton (Context context, int button) {

@Override
public int getHeight (int height) {
return size.getSize().height+2*renderer.getBorder();
return height+2*renderer.getBorder();
}

@Override
public Context getContext (Context context) {
if (resizing[0]) {
getComponent().setPosition(context.getInterface(),new Point(attachRect.x,attachRect.y+context.getInterface().getMouse().y-attachPoint.y));
size.setSize(new Dimension(attachRect.width,attachRect.height-context.getInterface().getMouse().y+attachPoint.y));
getComponent().setPosition(context.getInterface(),new Point(getComponent().getPosition(context.getInterface()).x,attachRect.y+context.getInterface().getMouse().y-attachPoint.y));
size.setSize(new Dimension(size.getSize().width,attachRect.height-context.getInterface().getMouse().y+attachPoint.y));
} else if (resizing[1]) {
size.setSize(new Dimension(attachRect.width,attachRect.height+context.getInterface().getMouse().y-attachPoint.y));
size.setSize(new Dimension(size.getSize().width,attachRect.height+context.getInterface().getMouse().y-attachPoint.y));
}
if (resizing[2]) {
Point p=getComponent().getPosition(context.getInterface());
getComponent().setPosition(context.getInterface(),new Point(p.x+context.getInterface().getMouse().x-attachPoint.x,p.y));
size.setSize(new Dimension(attachRect.width-context.getInterface().getMouse().x+attachPoint.x,attachRect.height));
getComponent().setPosition(context.getInterface(),new Point(attachRect.x+context.getInterface().getMouse().x-attachPoint.x,getComponent().getPosition(context.getInterface()).y));
size.setSize(new Dimension(attachRect.width-context.getInterface().getMouse().x+attachPoint.x,size.getSize().height));
} else if (resizing[3]) {
size.setSize(new Dimension(attachRect.width+context.getInterface().getMouse().x-attachPoint.x,attachRect.height));
size.setSize(new Dimension(attachRect.width+context.getInterface().getMouse().x-attachPoint.x,size.getSize().height));
}
return new Context(context,context.getSize().width-2*renderer.getBorder(),new Point(renderer.getBorder(),renderer.getBorder()),true,true);
}
Expand Down

0 comments on commit 5325c67

Please sign in to comment.