Think of an RPG game where you might need to present a list of buttons. A user might enter a room where they have to select from a series of options (buttons). Is there a type of container/panel that would show clickable buttons horizontally, but wrap if needed?
The best analogy I can think of to picture the situation is, Imagine needing to click on an item in a backpack, but each item is potentially a different width. (I can make them all the same height but the width then varies)
.---[My Backpack]------.
| aaa bbb cccc ddd |
| ee fff g |
| |
| |
`----------------------'
(The options come from a database, so its unknown at compile time how many options might be in a room, so I am going to need to programmatically add options.)
The very bottom of this godot document introduces custom container layouts, but it's unclear to me how this would work in practice
Flow container for Godot 3.5 or newer
Godot 3.5 (currently in beta) introduces HFlowContainer and VFlowContainer that will serve the propuse described.
The HFlowContainer will fill a row and when they overflow, it will add a new row and continue there. The VFlowContainer will work on a similar fashion but with columns.
Flow containers before Godot 3.5
For older versions of Godot you can use the HFlowContainer addon which you can find it in the asset library (here). Note that there is no VFlowContainer counterpart.
As everything on the asset library it is free and open source, so feel free to read the code and modify it, which can be serve as starting point if you want to make your own custom Container.
Making your own custom Container
The gist of making a custom Container is that it must position its children.
For that effect you react to NOTIFICATION_SORT_CHILDREN in the _notification method. You might also want to react to NOTIFICATION_RESIZED.
You can have a method - which I'll call layout - that you call when you get the notifications:
func _notification(what):
if what == NOTIFICATION_SORT_CHILDREN:
layout()
And also call layout from the setters (setget) of the properties that define how the Container must organize its children. To call layout from anywhere other than _notification, you might want to use call_deferred("layout") to prevent any possible re-layout loops from hanging or crashing the game.
The layout method would iterate over the visible children Controls and use get_combined_minimum_size to figure out their size.
Something like this:
func layout() -> void:
# …
for child in get_children():
var control := child as Control
if not is_instance_valid(control) or not control.visible:
continue
var size := control.get_combined_minimum_size()
# …
Then using that information compute the position and size for the children Controls. When there is room for the Controls to grow, you may want to split it among them according to their size_flags_stretch_ratio.
Once you have the position and size for a Control decided, use fit_child_in_rect to position them, which will take into account grow and size flags.
Thus - barring the simplest Containers - you will need to iterate over the children Controls twice. And for that you might find useful to have an auxiliary data structure to temporarily store them.
Related
I'm developing a custom table component for very large and very custom content. Therefore I decided not to go with TableView, but start from scratch (i.e. Control). I adopted the idea of the VirtualFlow to create and manage only the visible part of the table and reuse cells that have become invisible. However I needed virtual rows and columns.
My CustomVirtualFlow implements layoutChildren(). From the component's size and scrollbar positions I know which cells are visible. If necessary, I add cells to the VirtualFlow. I then update the content and css pseudo class states (selected, focused, ...).
This works almost fine ... currently, my only problem is that the css styles are sometimes lagging: newly created cells are shown with e.g. wrong backgrounds for a moment and then get correcteted with the next pulse.
My explanation for this is that JavaFX performs layout in two passes:
first a css pass and secondly the layout pass that calls layoutChildren().
The css of newly added children (during layoutChildren) is therefore not processes correctly.
I tried to call applyCss() directly, which helps, but seems to do too much because it takes a lot of time.
My question is:
How is the correct way to add nodes during layout, i.e. if the size of the component makes it neccessary to use further nodes?
If it is not during layoutChildren(), where else should I do it?
I'm working on an upgrade of a legacy app with multiple UOs and DWs all contained within 1 window.
Our clients are getting monitor upgrades (19" monitors!!! modern resolutions!!) and most of them will be 1280x1024, but there are still a few stations that are using the old 1024x768 resolutions while some executives get to use their fancy laptops with a 1366x768 resolution.
I've been trying to make our interface as responsive as possible but I still have to hardcore fixed values for every single uo and dw.
I'm trying to work it down to where I can simply use something like dw.height = env.height - dw.x-position.
Is this possible?
Currently I use something like this
// height = base height - details ( - buttons)
w_cms.uo_pat.uo_pat_appt.tab_1.tab_os_aptinfo.dw_os_aptinfo.Height = G_uo_height - 420
w_cms.uo_pat.uo_aps_book.uo_aps_cal.dw_free_slot.height = ll_base - 1230
To get the true position, you have to go up the chain of parent objects and add all their coordinates together.
A better approach to this problem is to resize your controls to the size of the window, not just the size of the screen. Not only is this easier, but it also means your application is responsive to window resizings, not just resolution differences. (This also dodges issues with varying sizes of chrome, like task bar size, scroll bar size, window border size, etc...)
The PFC has resize services for windows and DataWindows that do this, and the downloads have documentation on how these functions are used. There used to be articles on how to decouple PFC code and use it with a non-PFC architecture, but as long as you have ancestors for all your windows and DataWindow controls, it's not rocket science.
Good luck,
Terry.
Swing had LayoutManager's separate from the containers. So far as I can tell, JavaFX doesn't do that.
I have a complex structure of nodes that I want the user to be able to toggle between several different layout approaches. Something equivalent to the user specifying flow and all containers are converted to the equivalent of FlowPanes. Then they could choose vertical and everything is laid out vertically.
Is there a way to do this other than swapping out the nodes/recreating the whole structure?
I should note: the hierarchy changes at runtime and it is deeply nested.
I mention Swing because this is straightforward to do in Swing by maintaining a list of all containers in the entire hierarchy, and with a simple loop (or tree traversal without the list) setting a new LayoutManager on them. JavaFX doesn't seem to have this possibility because the layout behavior appears to be internal to the nodes.
Isn't something like this working ?
AnchorPane main=new AnchorPane();
AnchorPane sub1=new AnchorPane();
sub1.getChildren().add(btn);
main.getChildren().add(sub1);
When you want to switch the layout
AnchorPane sub2=new AnchorPane();
main.getChildren().remove(sub1);
main.getChildren().add(sub2);
Edit
I guess I missed how you are doing layouts. This is how I envisioned it.
Definitions to Various Components
MainLayout
-> CustomLayout 1
-> References to various components. ( Essentially you are not creating all the components for every layout, rather you are referring to them in each layout )
-> CustomLayout 2
-> References to various components. ( More or less a different arrangement of the same components, adds some component references and removes some )
-> CustomLayout 3
-> References to various components.
Making a bold statement, but if there were a tool in JavaFX, how would it do this automatically ? Each template needs to know where it should render a particular component and arguably the easiest way to do this is just create a new template that arranges the components in a different layout and swap the template when user wants to see a different layout.
This is not easy to do in Swing. You would still need to set all the layout constraints on the individual components again, unless your layout is very straightforward.
I don't see how there is much difference in this between swing and javaFX. In JavaFX you would need to add the same controls to a different container (VBox, HBox etc.) but you still don't need to recreate the controls every time. It is a little awkward to find the containers in the middle of the node hierarchy but I'm sure there is some kind of elegant recursive solution :)
I have been trying to find an example or some hints on how to create an app that I could drag, resize, rotate images onto a UIView and then save the individual pieces (including their size, rotation and placement) and the entire UIView into CoreData. Kind of like the omnigraffle app.
Any tutorials, examples or anything on any piece of the app would be greatly appreciated.
for dragging a view
http://www.cocoacontrols.com/platforms/ios/controls/tkdragview
for roting a view http://www.cocoacontrols.com/platforms/ios/controls/ktonefingerrotationgesturerecognizer
for resizing a view
http://www.cocoacontrols.com/platforms/ios/controls/spuserresizableview
What respects to core data, its actually pretty straightforward just, gather the classes in one view, see the properties you need to save, and the new one you will need for your app and thats it.
Like:
Object Canvas containing a many relationship to morphlingViews wich contain all the properties as center, color, width, height, angle, UIPath (if you plan to create custom shapes) layer position (so it gets drawn correctly) and if you plan to connect the views as omnigraffle add a many realtionship to self (in morphlingViews) so you can take the center of different morphlingViews and add a simple line between them. (and a string if you plan to add drawInRect method to allow users to write in the objects, then it will be a good idea to add the text properties as well).
You can also add Quartz Composer drawing styles properties to the object, as shadow, shadowColor, shadowOffset, or add patterColor to add resizable background.
Form containerForm=new Form(filename);
StringItem label1 = new StringItem("","Test\nTest2\nTest3");
StringItem label2 = new StringItem("","Test\nTest2\nTest3");
label2.setLayout(Item.LAYOUT_LEFT|Item.LAYOUT_TOP|Item.LAYOUT_SHRINK );
label2.setLayout(Item.LAYOUT_RIGHT|Item.LAYOUT_TOP|Item.LAYOUT_SHRINK );
containerForm.append(label1);
containerForm.append(label2);
display.setCurrent(containerForm);
This code produces this:
I want it so the two StringItems are next to eachother, and test is lined up with test, and so on.
I've also tried combining the two stringitems into one and adding a tab (\t) between them. The tab turned into nothing, however.
if you're on MIDP 2 it would be safer to also set LAYOUT_2 in addition to directives you already have set. Per my recollection of API spec, without this flag device may (or maybe even should) fall to MIDP 1 layout style ignoring your precious TOP and SHRINK.
Consider also using Item.setPreferredWidth(containerForm.getWidth()/3) to explicitly indicate that you want it to be not too wide. There seem to be small (but non-zero) chance that stupid device believes that preferred width of your string items is that of the screen which in turn somehow overrules shrink directive.
Java ME uses a row layout, which means every item you add is added in the next row of the GUI. You need something like a GridLayout which is not available in Java ME. Maybe you have to write your own Item which contains both labels. Similar to here. Here is a stackoverflow question on Gridlayouts in Java ME: Table or gridlayout in J2ME
Use the GridLayout using LWUIT
http://lwuit.java.net/tutorial/layouts.html