Change x,y->Point w,h->Extent#40
Conversation
a2e78ad to
276f709
Compare
276f709 to
a47e263
Compare
| this->pos_ = pos; | ||
| this->size_ = size; |
There was a problem hiding this comment.
please put those directly into the constructors initializer list for new code.
In this case it doesnt change any relevant order.
| { | ||
| // cursor is on the button (and mouse button not pressed while moving on the button) | ||
| if((motion.x >= x_) && (motion.x < x_ + w) && (motion.y >= y_) && (motion.y < y_ + h)) | ||
| if((motion.x >= pos_.x) && (motion.x < pos_.x + size_.x) && (motion.y >= pos_.y) && (motion.y < pos_.y + size_.y)) |
There was a problem hiding this comment.
I thought we also had something like this in the code IsPointInExtent or so
There was a problem hiding this comment.
IsPointInExtent is now used
| } | ||
| } else if(button_text) | ||
| CFont::writeText(Surf_Button, button_text, (int)w / 2, (int)((h - 11) / 2), FontSize::Medium, button_text_color, | ||
| CFont::writeText(Surf_Button, button_text, Position((int)size_.x / 2, (int)((size_.y - 11) / 2)), FontSize::Medium, button_text_color, |
There was a problem hiding this comment.
C cast still necessary? if so, replace by static_cast instead
There was a problem hiding this comment.
cast removed.
| #pragma once | ||
|
|
||
| #include "SdlSurface.h" | ||
| #include "defines.h" |
There was a problem hiding this comment.
It's needed because CPicture.h uses Point16 and Extent16 types for the new pos_ and size_ member
variables:
Point16 pos_;
Extent16 size_;These typedefs are defined in defines.h:
using Point16 = Point<Sint16>;
using Extent16 = Point<Uint16>;There was a problem hiding this comment.
Can now be removed again, can't it?
There was a problem hiding this comment.
removed
There was a problem hiding this comment.
this can't be rendered on github - what the heck happened?
There was a problem hiding this comment.
It works for me. But it's very large diff :-D
| } | ||
| } else if(button_text) | ||
| CFont::writeText(Surf_Button, button_text, Position((int)size_.x / 2, (int)((size_.y - 11) / 2)), | ||
| CFont::writeText(Surf_Button, button_text, Position(static_cast<int>(size_.x) / 2, static_cast<int>(size_.y - 11) / 2), |
There was a problem hiding this comment.
cast removed.
AI loves adding casts unfortunately.
| { | ||
| // cursor is on the button (and mouse button not pressed while moving on the button) | ||
| if((motion.x >= x_) && (motion.x < x_ + w) && (motion.y >= y_) && (motion.y < y_ + h)) | ||
| if(IsPointInRect(Position(motion.x, motion.y), Rect(Position(pos_), Extent(size_)))) |
There was a problem hiding this comment.
Why the cast (Position(pos_))? Should work directly, doesn't it?
Similar below?
If it cannot convert the Point16 to Position automatically then I'd say we should store them as Position/Extent. I mentioned that earlier that in almost all cases the default is fine instead of the sized-variants. As this seems to be an automatic conversion there is no real need to change that too, but if it causes issues like here(?) then it makes sense to change it at least for this class
There was a problem hiding this comment.
CButton.cpp: size_ is Extent16 = Point. Uint16 implicitly promotes to int for arithmetic (standard
integral promotion), and Position(int, int) accepts the promoted values directly. So Position(size_.x / 2,
(size_.y - 11) / 2) works fine.
There was a problem hiding this comment.
Do you want to go ahead and change ALL Point16 to Position in the codebase and remove Point16 type?
There was a problem hiding this comment.
At least where it requires extra work like here.
I'm not sure if there is any use of the sized Point types, maybe in some file operations. But generally the ~4 extra bytes per control/window don't matter (anymore), so yes it makes sense to replace them to make them easier to handle and reason about
There was a problem hiding this comment.
Ok went all the way and pushed usage of Point/Extent in UI mainly all the way to the C SDL functions.
|
Can we get this one out of the way? It's a pure refactor that doesn't make any changes and there's no reason for it to sit in queue. I stupidly based another branch on this thinking it would be quick to merge. |
|
There is an open question |
Flamefire
left a comment
There was a problem hiding this comment.
Better, thanks. 2 things:
- Offsets should be signed. It could use a typedef like
using Offset = Point<int>for clarity. - Lot's of introduced casts. Double-check where they are actually required. Many look uneccessary to me.
| // read start addresses | ||
| for(int y = 0; y < shadowArray->h; y++) | ||
| CHECK_READ(libendian::le_read_us(&starts[y], fp)); | ||
| for(Position pos{0, 0}; pos.y < shadowArray->h; pos.y++) |
There was a problem hiding this comment.
You don't like Position in for loop?
Was originally going to leave for loops alone, but there's a lot of x, y usage in them and this is actually nice I think.
But hmm this is CFile, not UI. Maybe should not convert CFile. There was bug with unitialized pos from changes here.
And why is CFile in same folder as all UI classes anyway.
There was a problem hiding this comment.
In this loop you loop over y only, so that's not a position
There was a problem hiding this comment.
ok reverted the y-only loop. Kept Positio for x,y loops.
| // position in the Surface 'Surf_Button' | ||
| Uint16 pos_x = 0; | ||
| Uint16 pos_y = 0; | ||
| unsigned pos_x = 0; |
| #pragma once | ||
|
|
||
| #include "SdlSurface.h" | ||
| #include "defines.h" |
There was a problem hiding this comment.
Can now be removed again, can't it?
| // position in the Surface 'Surf_Button' | ||
| Uint16 pos_x = 0; | ||
| Uint16 pos_y = 0; | ||
| unsigned pos_x = 0; |
There was a problem hiding this comment.
Converted
| @@ -340,45 +340,45 @@ bool CTextfield::render() | |||
| // black frame is left and up | |||
| // draw vertical line | |||
| pos_x = 0; | |||
There was a problem hiding this comment.
Those variable values can be inlined (Position(0, y))
There was a problem hiding this comment.
inlined to expressions
- pos_x = size_.x - 1;
for(unsigned y = 0; y < size_.y; y++)
- CSurface::DrawPixel_RGB(Surf_Text, Position(pos_x, y), 0, 0, 0);
+ CSurface::DrawPixel_RGB(Surf_Text, Position(size_.x - 1, y), 0, 0, 0);f179a30 to
89c0f88
Compare
|
(force pushed to apply clang-format to previously pushed commit) |
9583ae4 to
384b53d
Compare
|
Please can we keep pushing this one as it will cause conflicts with other PRs |
Based on this comment #37 (review)
Replace raw x,y and w,h parameters with Position and Extent types across
CMap, CSurface, CControlContainer, CButton, CPicture, CTextfield, and
CFont to make X/Y vs W/H semantics explicit and prevent copy-paste errors.
Position posinsteadof separate
int x, int y/int VertexX, VertexY/Uint16 MouseX, MouseYtake
Point16 pos/Extent16 size(consistent with addSelectBox)Point16 pos/Extent16 sizewith internal storage renamed topos_/size_