libtcod / python-tcod

A high-performance Python port of libtcod. Includes the libtcodpy module for backwards compatibility with older projects.
BSD 2-Clause "Simplified" License
410 stars 36 forks source link

Unexpected behaviour for print_box / print_rect alignment #66

Closed FractalWire closed 5 years ago

FractalWire commented 5 years ago

Hey,

I hope your week was good ;)

So, I'm trying to use print_box, and I'm having an unexpected result with alignment for print_box / print_rect. So I'm just checking if this is the expected behaviour.

When using print_box with a non-default alignment, the text origin coordinate changes to either x - width//2 for alignment CENTER or x - width for alignment RIGHT.

What I was expecting was that the string stayed contained inside the box formed by (x,y, x+width,y+height), but only changed the alignment, e.g., be pushed to the middle evenly or to the right of the box.

Both print_box and print_rect have this behaviour, so I'm not sure if this is to be expected or not, but if it is, the documentation is ambiguous on that :

Print a string constrained to a rectangle and return the height.

Print a string constrained to a rectangle.

HexDecimal commented 5 years ago

This is print_box unintentionally inheriting the behavior of how libtcod usually handles printing.

The rectangle made by (x, y, width, height) being respected regardless of alignment is what I intend, otherwise alignment has too much of an effect on the other parameters.

I just need to fix it with without changing how the older functions work. They share a lot of the same code internally.

FractalWire commented 5 years ago

Hum, you could create 2 new flags RRIGHT and CCENTER (name are just indicative), and then in your master function, you just need to add the text width to x if you stumble upon those 2 flags.

Something like that.

HexDecimal commented 5 years ago

This appears to be fixed. There's also a new get_height_rect function in the tcod.console module.

FractalWire commented 5 years ago

This works great, thanks !