godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.07k stars 20.19k forks source link

Is it possible to have a option to Align Icon for Button? #11380

Closed alexzheng closed 3 years ago

alexzheng commented 7 years ago

Operating system or device, Godot version, GPU Model and driver (if graphics related): ALL

Issue description:

The icon is aligned left, in most cases it is expected to align center. Although it's possible to align it center by make the size of the icon the same as the button.

Steps to reproduce:

Link to minimal example project:

alexzheng commented 5 years ago

For buttons without text the icon is expected in the center.

jitspoe commented 5 years ago

I agree. I have a grid of buttons for key binding purposes. For controller buttons, I'm using icons, but for keys, I'm using text. I want them all aligned in the center, but only the text is centered. Messing around in the source, I was able to center the icons in button.cpp like this:

_icon->draw(ci, style->get_offset() + Point2(Math::floor((halign - _icon->get_width()) * 0.5), Math::floor((valign - _icon->get_height()) / 2.0)), color_icon);

Need to hook up some actual settings to determine the behavior. I think it would be nice to be able to align the button on the left, center, or right, as you can with the text.

One thing I'm not sure of is what sets this internal margin: _icon->draw(ci, style->get_offset() + Point2(_internal_margin[MARGIN_LEFT] + get_constant("hseparation"), Math::floor((valign - _icon->get_height()) / 2.0)), color_icon);

Need to make sure I don't break any existing behavior.

jitspoe commented 4 years ago

I implemented a change that allows the icons to be aligned in the same manner as the text (right, left, and center). I've never done a pull request so I'm not sure how to proceed, and it looks like somebody has already created a pull request for a similar change (though mine is a bit different) which will cause conflicts, but here's what it looks like:

image

Here's the change: https://github.com/jitspoe/godot/commit/1217628a7b920766c5383397ba5bba0bfcdb8c9d

I'm currently working out of the 3.1 branch for stability reasons, but I assume that can be pulled over to main and such. I'm a git noob.

jitspoe commented 4 years ago

I've created a pull request for this. Sorry for the delay. 3.2 changed some icon behavior to allow expanding (which is cool!), but this wasn't trivial to bring over. https://github.com/godotengine/godot/pull/37181