I have a quite complex scene graph which has led me to some resizing problems. I'd be glad if you could help me solve it.
My root node is a BorderPane that its center is filled with a ListView. Each cell of the ListView is filled with a customized zoomable ScrollPane as below:
public class ZoomableScrollPane extends ScrollPane {
Group zoomGroup;
Scale scaleTransform;
LineChart<Number , Number> content;
double scaleValue = 1.0;
double delta = 0.1;
public ZoomableScrollPane(LineChart<Number , Number> content, double height) {
this.content = content;
this.setPrefHeight(height);
Group contentGroup = new Group();
zoomGroup = new Group();
contentGroup.getChildren().add(zoomGroup);
zoomGroup.getChildren().add(content);
setContent(contentGroup);
scaleTransform = new Scale(scaleValue, scaleValue, 0, 0);
zoomGroup.getTransforms().add(scaleTransform);
}
}
As you can see there is a LineChart inside of each ZoomableScrollPane. I want to do two things with this chart. Firstly, to somehow bind its width with the root layout to get the desired result in case of resizing the window (not zooming, zooming is OK), and secondly to change the chart's width at run time whenever a button is pressed:
public void handleButton(ActionEvent actionEvent) {
MainController.lineChart1.setPrefWidth(MainController.lineChart1.getPrefWidth() + CONSTANT);
}
The problem is that here I face a conflict. Cause the LineChart is the child of a Group (not a pane), I just know one way of resizability and that is to bind its width manually with the root BorderPane's like this:
MainController.lineChart1.prefWidthProperty().bind(fourChannels.widthProperty().subtract(40));
And in that case, I cannot change the LineChart's width at run time and will get A bound value cannot be set Exception.
I guess one solution could be revising the ZoomableScrollPane class to somehow avoid the need of manual binding, but really have no idea how to do it.
Any help on this would be greatly appreciated.
You can detect if a property is bound and then unbind it before manipulating it:
if (content.prefWidthProperty().isBound()) {
content.prefWidthProperty().unbind();
}
...
Presumably when you are zoomed in on a chart and the window is resized the content will already be larger than the viewport, so a forced change in width shouldn't kick in until the window is resized beyond the zoomed in size of the chart?
Given the different conditions you have you might be better off monitoring the size of the BorderPane/ListView and when that changes adjust the size of the charts that need it.
Related
I have created a Scene with ILNumerics that consists of 3 PlotCubes and a Colorbar.
Screenshot of the ILPanel
I wanted to add a method that exports the scene as an image in two ways, the first being the screenshot you see above.
The second export is supposed to only show the centric cube.
I attempted to follow the guidelines of ILNumerics for scene management.
I wrote the following code:
public void ExportAsImage(int resolutionWidth, int resolutionHeight, string path, bool includeSubCubes)
{
using (ILScope.Enter())
{
ILGDIDriver backgroundDriver = new ILGDIDriver(resolutionWidth, resolutionHeight, ilPanel1.Scene);
if (includeSubCubes)
{
// code for standard export here
}
else
{
// setting left and top cube and color bar invisible and
// adjusting main cube size is affecting the ilPanel.Scene
backgroundDriver.Scene.First<ILColorbar>().Visible = false;
GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _leftCubeTag).Visible = false;
GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _topCubeTag).Visible = false;
GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _mainCubeTag).ScreenRect = new RectangleF(0, 0, 1, 1);
GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _mainCubeTag).DataScreenRect = new RectangleF.Empty;
backgroundDriver.Scene.Configure();
backgroundDriver.Render();
// save image
backgroundDriver.BackBuffer.Bitmap.Save(path,System.Drawing.Imaging.ImageFormat.Png);
// revert changes done to cubes and color bar
backgroundDriver.Scene.First<ILColorbar>().Visible = true;
GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _leftCubeTag).Visible = true;
GetElementByTag<ILPlotCube>(backgroundDriver.Scene, _topCubeTag).Visible = true;
AdjustCubeSizes();
}
}
}
Note: "GetElementByTag" is an own implementation to retrieve objects in the ILNumerics Scene.
I first expected that the new driver basically creates a copy of the Scene I can work on, but like the code shows I have to revert all changed after the export or the displayed ilPanel only shows the scene the way I exported it.
Is it possible at all to do export to image without affecting the real Scene? Am I just missing some details?
Regards,
Florian S.
Florian, it does make a copy. But you need to add the interesting part to a new scene. The magic is happening in the Add() method:
var scene4render = new ILScene();
scene4render.Add(oldScene.First<ILPlotCube>(mytag));
// ... configure scene4render here, it will be detached from the original scene
// with the exception of shared buffers.
// ... proceed with rendering
In order to also include + render interactive state changes to the original plot cube (let's say rotations by the users mouse) you'd use something like that:
scene4render.Add(panel.SceneSyncRoot.First<ILPlotCube>(mytag));
Also, I wonder what GetElementByTag does better than ILGroup.First<T>(tag, predicate) or ILGroup.Find<T>(...)?
See also: http://ilnumerics.net/scene-management.html
I'm trying to illustrate (and edit) an xml model with a GEF powered graphical editor in eclipse. My xml model can have up to five levels in its parent-child hierarchy. Each element in the heirarchy is its own EditPart (which looks like a box). Child elements will be represented as "box" EditParts contained within their parent's box, and so on...
Each one of my EditParts will have a draw2d Figure which, itself, will have at least two or three more (decorative) draw2d figures of its own. The decorative figures are things like Header Rectangle's, content Rectangles, label's etc. I'm seeing these decorative figures being drawn above the EditPart's child EditParts - meaning that I can't see any child EditParts.
I had a work around for this where I would manually force the child EditPart's figure to be moved to the top of its parent's EditPart's stack of figures:
#Override
protected void refreshVisuals() {
super.refreshVisuals();
IFigure figure = getFigure();
if(figure instanceof BaseElementFigure){
//Refresh the figure...
((BaseElementFigure) figure).refresh(this);
}
if (figure.getParent() != null) {
//This moves the figure to the top of its parent's stack so it is not drawn behind the parent's other (decorative) figures
figure.getParent().add(figure);
((GraphicalEditPart) getParent()).setLayoutConstraint(this, figure, getBounds());
}
refreshChildrenVisuals();
}
However, this only partially worked. The child EditPart was now being rendered above the Parent EditPart, but as far as Gef was concerned, it was beneath - some Gef events like drop listeners and tooltip would behave as if the child EditPart didn't exist.
Edit:
An EditPart's figure is created by as follows
#Override
protected IFigure createFigure() {
return new PageFigure(this);
}
Where PageFigure is a subclass of Figure which constructs it's own decorative child figures.
public class PageFigure extends Figure {
protected Label headerLabel;
protected RectangleFigure contentRectangle;
protected RectangleFigure headerRectangle;
private UiElementEditPart context;
public PageFigure(UiElementEditPart context) {
this.context = context;
setLayoutManager(new XYLayout());
this.contentRectangle = new RectangleFigure();
contentRectangle.setFill(false);
contentRectangle.setOpaque(false);
this.headerRectangle = new RectangleFigure();
headerRectangle.setFill(false);
headerRectangle.setOpaque(false);
this.headerLabel = new Label();
headerLabel.setForegroundColor(ColorConstants.black);
headerLabel.setBackgroundColor(ColorConstants.lightGray);
headerLabel.setOpaque(true);
headerLabel.setLabelAlignment(Label.LEFT);
headerLabel.setBorder(new MarginBorder(0, 5, 0, 0));
headerRectangle.add(headerLabel);
add(contentRectangle);
add(headerRectangle);
//Initializing the bounds for these figures (including this one)
setBounds(context.getBounds());
contentRectangle.setBounds(new Rectangle(this.getBounds().x, this.getBounds().y + 20, this.getBounds().width, this.getBounds().height - 20));
Rectangle headerBounds = new Rectangle(this.getBounds().x, this.getBounds().y, this.getBounds().width, 20);
headerRectangle.setBounds(headerBounds);
headerLabel.setBounds(new Rectangle(headerBounds.x + 30, headerBounds.y, headerBounds.width - 30, 20));
}
}
What is happening is that GEF is adding the child EditPart figures to the PageFigure, and the order of drawing in GEF starting from the last figure added and on top the figures added before, like a stack. This is why the figures of your child EditParts are below the child figures of the parent EditPart.
What you can do is add a content pane figure that will contain the child figures, and make it the first figure that is added to the EditPart's figure. Then override the figure's contentPane, so that all child figures are added on this figure.
If this doesn't help, check the tutorial on this subject some time ago. I may have forgotten some of the details: http://www.vainolo.com/2011/09/01/creating-an-opm-gef-editor-%E2%80%93-part-17-how-to-define-container-edit-parts/.
How to fix a ScrollPane scroll bar position at a specific position so that it displays for example the last item of it's child Table instead of the first item Table(default)? I try setScrollX and setScrollY but it doesn't work.
I have a game level menu(with many levels) and i would like to display the last unlocked level to the user when opening that menu.
You might have to call layout() on the ScrollPane yourself manually before trying to call setScrollX or setScrollY
When you want to avoid the ScrollPanes fling effect, use the following code in your show() method, so the ScrollPane immediately shows the exact position you specified.
scrollPane.layout();
scrollPane.setScrollPercentY(.5f);
scrollPane.updateVisualScroll();
Instead of setting the scrollPercent, you could also use
scrollPane.scrollTo(x, y, width, height);
or
scrollPane.setScrollY(y);
This works for me if you've got a vertically stacked pane:
setScrollPercentY(100);
This worked for me:
scrollPane.layout();
scrollPane.setScrollPercentY(100);
For me, all the previous answers did not work:
Even after calling scrollPane.layout() multiple times, the scroll pane still had an areaHeight (scrollHeight) of -2, 0, or sth else. Only later during subsequent layout calls the correct height would be set, causing an animation where the content would slide in from the top.
The only option that fixed it for my use case (scrolling to bottom initially after adding all the initial content) was to override the ScrollPane.layout() and scroll to bottom after each layout change:
open class BottomScrollingScrollPane(actor: Actor?, skin: Skin, style: String): ScrollPane(actor, skin, style) {
override fun layout() {
super.layout()
scrollTo(0f, 0f, 0f, 0f)
updateVisualScroll()
}
}
... and optionally, if you use libktx:
#Scene2dDsl
#OptIn(ExperimentalContracts::class)
inline fun <S> KWidget<S>.bottomScrollingScrollPane(
style: String = defaultStyle,
skin: Skin = Scene2DSkin.defaultSkin,
init: KBottomScrollingScrollPane.(S) -> Unit = {}
): KBottomScrollingScrollPane {
contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) }
return actor(KBottomScrollingScrollPane(skin, style), init)
}
class KBottomScrollingScrollPane(skin: Skin, style: String) : BottomScrollingScrollPane(null, skin, style), KGroup {
override fun addActor(actor: Actor?) {
this.actor == null || throw IllegalStateException("ScrollPane may store only a single child.")
this.actor = actor
}
}
One side effect is scrolling down again after each layout change, resize etc., but that's fine for me as the layout is very short-lived anyway.
this code is set the position, width and height of ScrollPane.
scroll.setBounds(0, 60, Gdx.graphics.getWidth(), 200);
i would like to design a component that look like a switch that has 3 position. One neutral position, and upper switch position, when the user click on the upper half of the component and a lower switch position, when the user click the lower half of the component. When the mouse is released the switch go back to neutral position. I have got 3 switch images for these position. I was thinking using a Button then check if the mouse click coordinate is in the upper half or the lower half then set images accordingly. I am looking for any better suggestion, if possible with the use of css for the images (i don't know if it is possible though), or any other suggestion with the use of another component if it is more suitable. Thank you.
Here is what i did, it works...
final Button rstButton = new Button();
final Image rstNeutral = new Image(MyClass.class.getResourceAsStream("images/switch_neutral.png"));
final Image rstUp = new Image(MyClass.class.getResourceAsStream("images/switch_on.png"));
final Image rstDown = new Image(MyClass.class.getResourceAsStream("images/switch_off.png"));
final ImageView rstImage = new ImageView();
rstImage.setImage(rstNeutral);
rstButton.setGraphic(rstImage);
rstButton.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent me) {
double mouseY = me.getY();
double buttonY = rstButton.getLayoutY();
double buttonHeight = rstButton.getHeight();
if(buttonY + mouseY > buttonY + (buttonHeight / 2)) {
rstImage.setImage(rstDown);
} else {
rstImage.setImage(rstUp);
}
}
});
rstButton.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent arg0) {
rstImage.setImage(rstNeutral);
}
});
I think you should use three component instead of one.
For exemple, you could use three instance of ToogleButton. This component is like a button in appearance, but have a toogle state (clicked/unclicked). I never use it in javaFX but I think you will find enough help on the oracle site (for example : http://docs.oracle.com/javafx/2/ui_controls/toggle-button.htm)
EDIT :
After comment, I think an image of a mechanical switch (http://docs.oracle.com/javafx/2/api/javafx/scene/image/Image.html) could be the solution. If you're showing the switch from the side you can change the rotation of the image according to the state of the switch.
For knowing where the user clicked, locating the position of the button could work.
Another solution could be to had three transparent region on top of the image and intercept the click event on these region, instead of the image. Having never used such a solution, I can really help you more.
Last solution I see could be the use of two another image component on the side of the switch image, showing two arrow, for pulling up or down the switch.
I update the source property of an image. When the image is loaded I want to redraw the border skin to fit the new size of the image.
newImgEdit.addEventListener(Event.COMPLETE, loadImgComplete);
newImgEdit.source = myurl_ressource;
private function loadImgComplete(evt:Event):void {
trace("redraw !!");
//invalidateDisplayList();
this.setStyle("borderSkin", ShapeContainerBorderOn);
var img:Image = evt.currentTarget as Image;
img.removeEventListener(Event.COMPLETE, loadImgComplete);
}
The trace "redraw" seems to happen once the image is loaded but the border still does not get redrawn with the correct height and width.
Do I need to remove the listener or will it be garbage-collected later?
You can manually force a component to update its layout by calling validateNow().