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

Refactor draw colored text #73538

Merged
merged 5 commits into from
May 20, 2024

Conversation

db48x
Copy link
Contributor

@db48x db48x commented May 6, 2024

Summary

Infrastructure "refactor cataimgui::draw_colored_text"

Purpose of change

Simplifies the function so that it doesn't keep track of a redundant color stack, and so that the base color argument is optional. This simplifies 99% of the callers.

Describe the solution

I also added a conversion function to the nc_color class so that instances can be used anywhere ImGui expects an ImVec4.

Testing

The easiest way to test is to look at the new item info window written by mqrause in #73456; it has lots of wrapped text with embedded colors.

Additional context

See also ocornut/imgui#2313 (comment), which has a nicer implementation of the same idea. Amongst several other improvements, it works with proportionally–spaced fonts, while our current solution only works with monospaced fonts.

@github-actions github-actions bot added Info / User Interface Game - player communication, menus, etc. [C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style json-styled JSON lint passed, label assigned by github actions astyled astyled PR, label is assigned by github actions labels May 6, 2024
@db48x db48x mentioned this pull request May 6, 2024
@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label May 6, 2024
src/cata_imgui.cpp Outdated Show resolved Hide resolved
src/cata_imgui.cpp Outdated Show resolved Hide resolved
@db48x db48x force-pushed the refactor-draw_colored_text branch from a5e4ef0 to ab4a3d5 Compare May 7, 2024 04:52
@Maleclypse
Copy link
Member

There is a conflict to resolve.

@db48x db48x force-pushed the refactor-draw_colored_text branch from ab4a3d5 to 24616b2 Compare May 15, 2024 15:54
@katemonster33
Copy link
Contributor

still conflicts, but i did pull this branch down and test it and did not notice anything wrong with it at a glance.

@katemonster33
Copy link
Contributor

0001-Added-code-to-guarantee-that-any-colors-pushed-to-th.patch

Added a patch with the change I wanted to see with my comment. If @db48x is okay with this patch then we can merge this.

@katemonster33
Copy link
Contributor

i actually figured out how to open a PR db48x#1

@db48x
Copy link
Contributor Author

db48x commented May 17, 2024

I don’t hate it. It would work for the case where a closing color tag has been left out, but not the case where too many closing tags exist. For that I have a partial patch of my own which I had only just started to test; I hit some snags and then got distracted.

However, instead of hiding the error wouldn’t it be better to generate an error report? I am not sure that the error report could be ignored, since it would happen every frame, but that’s the only way to keep this type of mistake from accumulating.

Finally, have you taken a look at the code I linked to? It has a really nice solution. Instead of embedding markup into a string, then parsing the string, it uses a vector of text segments. Each segment has a color and the code simply renders all of the segments, one after another, wrapping them as necessary. I have not yet adopted this code because the game has so many strings with markup, but I think that is really where we should be headed. I would probably be much more efficient than what we do now. And there would be no way to cause any of these problems.

It is past time for me to hit the sack, so I can’t possibly work on this for another 12 hours at least. If you want to strike while the iron is hot then go for it. If you get something you are satisfied with, I have given you access to my fork so that you can push to this branch. Or you can just make a new pull request, if you prefer.

db48x and others added 5 commits May 19, 2024 07:33
If you don't specify a color, it will use the current Text theme color
as the base. If you do specify it, it will push the color you specify
first and pop it after.

Also, remove the internal color stack, since ImGui already has a stack
of colors.
The conversion is not expensive enough to worry about, and we don't
have any functions that can take both an nc_color and an ImVec4 so it
isn’t ambiguous either.
@db48x db48x force-pushed the refactor-draw_colored_text branch from 4767a3d to dec5cf3 Compare May 19, 2024 15:47
@db48x
Copy link
Contributor Author

db48x commented May 19, 2024

Should be good to go now, unless I’ve missed something. In the end I skipped trying to report errors; it’s more annoying that it is worth at the moment.

@Maleclypse Maleclypse merged commit d272059 into CleverRaven:master May 20, 2024
26 checks passed
SurFlurer added a commit to SurFlurer/Cataclysm-DDA that referenced this pull request Aug 26, 2024
…_colored_text"

This reverts commit d272059, reversing
changes made to b7e7dec.

this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions [C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style Info / User Interface Game - player communication, menus, etc. json-styled JSON lint passed, label assigned by github actions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants