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

Avoid null pointer exception in the UIScaler #839

Merged
merged 2 commits into from
Apr 13, 2020

Conversation

kvakvs
Copy link
Collaborator

@kvakvs kvakvs commented Apr 13, 2020

Fixes #838

Bug

UIScaler was using uiCamera's pixel size, while either UIView.instance or UIView.instance.uiCamera was null. This is now cached and double checked.
Potentially if UI never changes from 1920x1080, we can replace that entire code with a pair of const

Solution

A null check was added for both UIView.instance and UIView.instance.uiCamera, cached width and height are stored in case those are ever null.
Also: Change to WorldToScreenPoint has been reverted as it always been using Screen.height not GUI height.

get {
UIView uiView = Singleton<UIView>.instance;
if (uiView != null) {
Camera uiCamera = uiView.uiCamera;
Copy link
Collaborator

Choose a reason for hiding this comment

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

just a minor note. it would be more compact to write like this:

UIView uiView = Singleton<UIView>.instance;
Camera uiCamera = uiView?.uiCamera;
if(uiCamera !=null) 
  ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

But this does not eliminate the null check, in your code null check will happen twice.

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah its the same code just more compact.

Copy link
Member

Choose a reason for hiding this comment

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

Code compactness is nice, but not at the expense of obscuring clarity of intent of the code.

Copy link
Collaborator

@kianzarrin kianzarrin left a comment

Choose a reason for hiding this comment

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

I didn't manage to reproduce the problem using TMPE labs. So I cannot test if it is fixed now. the code looks good.

TLM/TLM/U/UIScaler.cs Outdated Show resolved Hide resolved
@kvakvs
Copy link
Collaborator Author

kvakvs commented Apr 13, 2020

Pushed the suggested change.

@krzychu124 krzychu124 self-requested a review April 13, 2020 21:18
Copy link
Member

@krzychu124 krzychu124 left a comment

Choose a reason for hiding this comment

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

👍

@kvakvs kvakvs merged commit 1a71c45 into CitiesSkylinesMods:master Apr 13, 2020
@originalfoo originalfoo added the UI User interface updates label Apr 13, 2020
@originalfoo originalfoo added this to the 11.3.2 milestone Apr 13, 2020
@originalfoo originalfoo added the Overlays Overlays, data vis, etc. label Apr 13, 2020
@originalfoo
Copy link
Member

I'll see if this fixes any of the issues I'd been seeing in game.

@originalfoo originalfoo linked an issue Apr 17, 2020 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Overlays Overlays, data vis, etc. UI User interface updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Icons not showing up when selecting node after choosing TMPE. UI imporvments of RoadWorldInfoPanel
4 participants