Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added alignment options to icons on buttons. They can now be centered and right-aligned. #37181

Merged
merged 1 commit into from
Jun 29, 2021

Conversation

jitspoe
Copy link
Contributor

@jitspoe jitspoe commented Mar 20, 2020

Icons now have the same alignment options as text on buttons. Icons can be aligned on the left, right, or centered. Left is the same as the current behavior. Right is basically the same, but on the other side. Centered centers the icon and draws the text (if any) on top. Very useful if you want to have menu buttons that can sometimes have text OR an icon and you want them justified in the same way.

Fixes #11380

@YeldhamDev
Copy link
Member

A bit of documentation for the new methods would be nice.

@jitspoe
Copy link
Contributor Author

jitspoe commented Mar 21, 2020

A bit of documentation for the new methods would be nice.

Ah, good call. I forgot I added new functions for this. Should that be a separate pull request, or should it be squashed into this one? Never touched the documentation before.

@akien-mga
Copy link
Member

Squashed in this commit would be best. You can generate the template for the new functions with godot --doctool . (see https://docs.godotengine.org/en/latest/community/contributing/updating_the_class_reference.html). You might have a few unrelated changes to undo, especially removal of some Mono things if you're generating docs with a non-Mono build.

@jitspoe jitspoe requested a review from a team as a code owner March 31, 2020 03:15
@jitspoe jitspoe force-pushed the master.button_icon_alignment branch 2 times, most recently from 5e9ab0d to ef5228e Compare March 31, 2020 03:50
@jitspoe
Copy link
Contributor Author

jitspoe commented Mar 31, 2020

Ok, I think I have it all squared away now. For some reason, the doc generation did not work proprely using just "." as the output directory. Nothing changed. I then tried using a test directory, and it generate a bunch of stub xml files. Finally, I ended up just doing "testdir.." and that ended up working. Strange. This was on a window build.

Rebasing also got interesting with some merge conflicts. Always something new whenever I try to do a pull request.

@Calinou
Copy link
Member

Calinou commented Mar 31, 2020

Rebasing also got interesting with some merge conflicts. Always something new whenever I try to do a pull request.

The master branch is currently ongoing heavy refactoring (DisplayServer, node names, ...), so this is expected. Sorry for the inconvenience.

@jitspoe
Copy link
Contributor Author

jitspoe commented Nov 23, 2020

Merge conflicts resolved. Just waiting on a review, now.

@akien-mga akien-mga added this to the 4.0 milestone Nov 23, 2020
scene/gui/button.cpp Outdated Show resolved Hide resolved
Copy link
Member

@KoBeWi KoBeWi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok feature and code-wise (aside from that one nitpick about order).

We could rename align to text_align, so these properties are less confusing, but this could be done in another PR (and allow this one to be cherry-picked easier).

@sairam4123
Copy link

sairam4123 commented Jan 2, 2021

Having an option for drawing text on the bottom will be good too while the icon is centered.

@akien-mga
Copy link
Member

The implementation seems fine, but looking at the screenshot on #11380 (comment), I'm not sure how usable the CENTER align is when using text which will overlap, unless we also implement vertical positioning options for the text as suggested by @sairam4123.

@jitspoe
Copy link
Contributor Author

jitspoe commented Jan 9, 2021

I don't imagine people would really use the centered text AND centered icon. It's just there because that's a possible combination. I primarily wanted to just be able to center icons by themselves. Icons could maybe be used for backgrounds on buttons as well. We could always expand this later if there's a desire for it.

@akien-mga akien-mga requested a review from a team January 18, 2021 11:34
Copy link
Member

@RandomShaper RandomShaper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@akien-mga
Copy link
Member

@jitspoe Could you rebase to solve merge conflicts?

@jitspoe
Copy link
Contributor Author

jitspoe commented Jan 23, 2021

@jitspoe Could you rebase to solve merge conflicts?

Working on merging this. It's a pretty significant conflict with all the rtl changes.

@jitspoe
Copy link
Contributor Author

jitspoe commented Jan 23, 2021

I'm really confused by some of these changes and how to test them. For example,

					if (rtl) {
						if (_internal_margin[SIDE_RIGHT] > 0) {
							icon_ofs_region = _internal_margin[SIDE_RIGHT] + get_theme_constant("hseparation");
						}
					} else {
						if (_internal_margin[SIDE_LEFT] > 0) {
							icon_ofs_region = _internal_margin[SIDE_LEFT] + get_theme_constant("hseparation");
						}
					}

Why, if the text is RTL, would we be using the right side margin for the icon offset? The icon is still on the left, right? Wouldn't we always want to be using the left margin? Also, when does "rtl" get set? There's a "Text Direction" property on the button that can specify "RTL", but it appears to not change any behavior with the text. I even tried putting in some Arabic text to test. Nothing appears to change within the editor. I'm confused as to what all these checks are doing, and if they're even necessary. Seems to be making the code rather complex.

@akien-mga
Copy link
Member

akien-mga commented Jan 25, 2021

CC @bruvzg

You might find some explanations in https://godotengine.org/article/complex-text-layouts-progress-report-2 in the part about UI mirroring.

If RTL UI mirroring is enabled, then it would be expected that a Left icon would be shown to the Right. E.g. if your usage is "user avatar on the left, then name on the right", in Arabic this should be "user avatar on the right, then name on the left". Godot's Complex Text Layouts would handle this automagically based on the locale's preference.

@bruvzg
Copy link
Member

bruvzg commented Jan 26, 2021

Why, if the text is RTL, would we be using the right side margin for the icon offset? The icon is still on the left, right?

No, both text and icon are aligned to the right, as well as everything else. It's done to automatically flip layout for right-to-left languages without redesigning UI.

Screenshot 2021-01-26 at 10 04 21

For is_rtl branches just swap left and right alignment for both text and icon (ALIGN_LEFT aligns to the right and vise versa).

@jitspoe
Copy link
Contributor Author

jitspoe commented Jan 26, 2021

Ok, that's what the code looked like it was doing, but when I tried a nightly build and attempted to enable RTL on a button, it didn't seem to make a difference. How do I test this?

@bruvzg
Copy link
Member

bruvzg commented Feb 1, 2021

Ok, that's what the code looked like it was doing, but when I tried a nightly build and attempted to enable RTL on a button, it didn't seem to make a difference. How do I test this?

Layout direction is controlled by layout_direction property (under Control section), there's also text_direction property which controls BiDi reordering base direction (it won't have any effect unless you have text with mixing writing directions in the control).

Screenshot 2021-02-01 at 11 08 15

@jitspoe
Copy link
Contributor Author

jitspoe commented Jun 17, 2021

I rewrote some of the rtl checking logic a little bit because the way it was previously implemented would involve lots of copy/pasting code and be difficult to maintain. I believe everything is working properly. I'm unsure how to test the internal margins, though. What sets things like _internal_margin[SIDE_RIGHT]?

@jitspoe jitspoe force-pushed the master.button_icon_alignment branch from c117c0c to 92087c2 Compare June 17, 2021 03:01
@akien-mga akien-mga requested a review from bruvzg June 18, 2021 10:29
@akien-mga
Copy link
Member

It would be good if you can rebase on top of upstream master to remove the "Merge branch 'master'" commit (we use merge commits when merging PRs to master, but don't want them from in-development PR syncs).

Copy link
Member

@bruvzg bruvzg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With exception to min. size not updated on icon alignment change, everything seems to be working fine.

scene/gui/button.cpp Show resolved Hide resolved
They can now be centered and right-aligned.

Fixes godotengine#11380.
@akien-mga akien-mga force-pushed the master.button_icon_alignment branch from 92087c2 to e192eb0 Compare June 29, 2021 10:32
@akien-mga
Copy link
Member

Force pushed an update to rebase on master, add the missing minimum size update, and fix a few style issues.

@akien-mga akien-mga merged commit 90982d6 into godotengine:master Jun 29, 2021
@akien-mga
Copy link
Member

Thanks!

@jitspoe
Copy link
Contributor Author

jitspoe commented Jun 29, 2021

Thanks for taking care of the last couple changes. Should I make a pull request for 3.x as well? Will the RTL stuff be making its way to 3.x?

@Calinou
Copy link
Member

Calinou commented Jun 29, 2021

Should I make a pull request for 3.x as well?

Feel free to open a pull request for 3.x as well 🙂

Will the RTL stuff be making its way to 3.x?

No, as it's backwards-incompatible with existing projects.

@raulsntos
Copy link
Member

raulsntos commented Feb 5, 2022

@jitspoe Did you ever get around to backporting this to 3.x? It's totally fine if you are busy/unavailable and I'd be willing to backport it myself if you don't mind.

@jitspoe
Copy link
Contributor Author

jitspoe commented Feb 6, 2022

It's on my todo list but it's kinda far down. I actually wrote it for 3.x then rewrote it for 4.0, so I have some iteration of it here: https://github.com/jitspoe/godot/tree/3.x.jitspoe

I think I may have changed some variable names or something and wanted to make sure it was consistent with what was in 4.0. Got kind of a big milestone coming up, so I probably won't have time to look at it this month.

@raulsntos
Copy link
Member

I've opened PR #57771 to backport this to 3.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is it possible to have a option to Align Icon for Button?
10 participants