Skip to content

Commit a087eb9

Browse files
committed
Move identical structures in a common location.
1 parent 554a020 commit a087eb9

File tree

5 files changed

+72
-109
lines changed

5 files changed

+72
-109
lines changed

src/ui/ui.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,51 @@ class UiWindow
3333
protected:
3434
virtual void _display();
3535

36+
struct PointToDraw
37+
{
38+
PointToDraw( const Point2d & point_ = Point2d(), const PaintColor & color_ = PaintColor() )
39+
: point( point_ )
40+
, color( color_ )
41+
{
42+
}
43+
44+
Point2d point;
45+
PaintColor color;
46+
};
47+
48+
struct LineToDraw
49+
{
50+
LineToDraw( const Point2d & start_ = Point2d(), const Point2d & end_ = Point2d(), const PaintColor & color_ = PaintColor() )
51+
: start( start_ )
52+
, end ( end_ )
53+
, color( color_ )
54+
{
55+
}
56+
57+
Point2d start;
58+
Point2d end;
59+
PaintColor color;
60+
};
61+
62+
struct EllipseToDraw
63+
{
64+
EllipseToDraw( const Point2d & topLeft_ = Point2d(), double width_ = 0.0, double height_ = 0.0, const PaintColor & color_ = PaintColor() )
65+
: topLeft( topLeft_ )
66+
, width( width_ )
67+
, height( height_ )
68+
, color( color_ )
69+
{}
70+
71+
Point2d topLeft;
72+
double width;
73+
double height;
74+
PaintColor color;
75+
};
76+
77+
std::vector<PointToDraw> _point;
78+
std::vector<LineToDraw> _lines;
79+
std::vector<EllipseToDraw> _ellipses;
80+
3681
penguinV::Image _image; // we store a copy of image
3782
std::string _title;
3883
bool _shown;

src/ui/win/win_ui.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ namespace WindowsUi
6868
}
6969

7070
for ( std::vector < UiWindowWin::EllipseToDraw >::const_iterator ellipse = _window->_ellipse.cbegin(); ellipse != _window->_ellipse.cend(); ++ellipse ) {
71-
const int left = static_cast<int>( ellipse->left * xFactor );
72-
const int top = static_cast<int>( ellipse->top * yFactor );
73-
const int right = static_cast<int>( ellipse->right * xFactor );
74-
const int bottom = static_cast<int>( ellipse->bottom * yFactor );
71+
const int left = static_cast<int>( ellipse->topLeft.x * xFactor );
72+
const int top = static_cast<int>( ellipse->topLeft.y * yFactor );
73+
const int right = static_cast<int>( ( ellipse->topLeft.x + width ) * xFactor );
74+
const int bottom = static_cast<int>( ( ellipse->topLeft.y + height ) * yFactor );
7575

7676
HPEN hPen = CreatePen( PS_SOLID, 1, RGB( ellipse->color.red, ellipse->color.green, ellipse->color.blue ) );
7777
HGDIOBJ hOldPen = SelectObject( hdc, hPen );
@@ -276,7 +276,8 @@ void UiWindowWin::drawLine( const Point2d & start, const Point2d & end, const Pa
276276

277277
void UiWindowWin::drawEllipse( const Point2d & center, double xRadius, double yRadius, const PaintColor & color )
278278
{
279-
_ellipse.emplace_back( center.x - xRadius, center.y - yRadius, center.x + xRadius, center.y + yRadius, color );
279+
const Point topLeft( center.x - xRadius, center.y - yRadius );
280+
_ellipse.emplace_back( topLeft, xRadius * 2, yRadius * 2, color );
280281

281282
_display();
282283
}

src/ui/win/win_ui.h

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,6 @@ class UiWindowWin : public UiWindow
2828
HWND _window; // Windows OS window handle (just an unique ID)
2929
BITMAPINFO * _bmpInfo; // bitmap structure needed for drawing
3030

31-
struct PointToDraw
32-
{
33-
PointToDraw( const Point2d & point_ = Point2d(), const PaintColor & color_ = PaintColor() )
34-
: point( point_ )
35-
, color( color_ )
36-
{
37-
}
38-
39-
Point2d point;
40-
PaintColor color;
41-
};
42-
43-
struct LineToDraw
44-
{
45-
LineToDraw( const Point2d & start_ = Point2d(), const Point2d & end_ = Point2d(), const PaintColor & color_ = PaintColor() )
46-
: start( start_ )
47-
, end ( end_ )
48-
, color( color_ )
49-
{
50-
}
51-
52-
Point2d start;
53-
Point2d end;
54-
PaintColor color;
55-
};
56-
57-
struct EllipseToDraw
58-
{
59-
EllipseToDraw( double left_ = 0, double top_ = 0, double right_ = 0, double bottom_ = 0, const PaintColor & color_ = PaintColor() )
60-
: left ( left_ )
61-
, top ( top_ )
62-
, right ( right_ )
63-
, bottom( bottom_ )
64-
, color ( color_ )
65-
{
66-
}
67-
68-
double left;
69-
double top;
70-
double right;
71-
double bottom;
72-
PaintColor color;
73-
};
74-
75-
std::vector < PointToDraw > _point;
76-
std::vector < LineToDraw > _line;
77-
std::vector < EllipseToDraw > _ellipse;
78-
7931
friend class WindowsUi::UiWindowWinInfo;
8032

8133
void _free();

src/ui/x11/x11_ui.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,16 @@ void UiWindowX11::_display()
5151

5252
for ( size_t i = 0u; i < _point.size(); ++i ) {
5353
const Point2d & point = _point[i].point;
54-
XSetForeground( _uiDisplay, defaultGC, _point[i].color );
54+
const uint32_t foreground = UiWindowX11::_convertColor( _point[i].color );
55+
XSetForeground( _uiDisplay, defaultGC, foreground );
5556
XDrawLine( _uiDisplay, _window, defaultGC, static_cast<int>( point.x - 1 ), static_cast<int>( point.y - 1 ), static_cast<int>( point.x + 1 ),
5657
static_cast<int>( point.y + 1 ) );
5758
}
5859

5960
for ( size_t i = 0u; i < _lines.size(); ++i ) {
6061
const Point2d & start = _lines[i].start;
6162
const Point2d & end = _lines[i].end;
62-
const uint32_t & foreground = _lines[i].color;
63+
const uint32_t foreground = UiWindowX11::_convertColor( _lines[i].color );
6364

6465
XSetForeground( _uiDisplay, defaultGC, foreground );
6566
XDrawLine( _uiDisplay, _window, defaultGC, static_cast<int>( start.x ), static_cast<int>( start.y ), static_cast<int>( end.x ),
@@ -70,29 +71,34 @@ void UiWindowX11::_display()
7071
const Point2d & position = _ellipses[i].topLeft;
7172
const double & width = _ellipses[i].width;
7273
const double & height = _ellipses[i].height;
73-
const uint32_t & foreground = _ellipses[i].color;
74+
const uint32_t foreground = UiWindowX11::_convertColor( _ellipses[i].color );
7475

7576
XSetForeground( _uiDisplay, defaultGC, foreground );
76-
XDrawArc( _uiDisplay, _window, defaultGC, static_cast<int>( position.x ), static_cast<int>( position.y ), static_cast<int>( width ),
77-
static_cast<int>( height ), 0, 360 * 64 );
77+
XDrawArc( _uiDisplay, _window, defaultGC, static_cast<int>( position.x ), static_cast<int>( position.y ), static_cast<uint32_t>( width ),
78+
static_cast<uint32_t>( height ), 0, 360 * 64 );
7879
}
7980

8081
for ( size_t i = 0u; i < _rectangles.size(); ++i ) {
8182
const Point2d & topLeft = _rectangles[i].topLeft;
8283
const double & width = _rectangles[i].width;
8384
const double & height = _rectangles[i].height;
84-
const uint32_t & foreground = _rectangles[i].color;
85+
const uint32_t foreground = UiWindowX11::_convertColor( _rectangles[i].color );
8586

8687
XSetForeground( _uiDisplay, defaultGC, foreground );
87-
XDrawRectangle( _uiDisplay, _window, defaultGC, static_cast<int>( topLeft.x ), static_cast<int>( topLeft.y ), static_cast<int>( width ),
88-
static_cast<int>( height ) );
88+
XDrawRectangle( _uiDisplay, _window, defaultGC, static_cast<int>( topLeft.x ), static_cast<int>( topLeft.y ), static_cast<uint32_t>( width ),
89+
static_cast<uint32_t>( height ) );
8990
}
9091
}
9192
else if ( (e.type == ClientMessage) && (static_cast<unsigned int>(e.xclient.data.l[0]) == _deleteWindowEvent) )
9293
break;
9394
}
9495
}
9596

97+
uint32_t UiWindowX11::_convertColor(const PaintColor &color)
98+
{
99+
return static_cast<uint32_t>( ( color.red << 16 ) + ( color.green << 8 ) + color.blue );
100+
}
101+
96102
void UiWindowX11::_setupImage( const penguinV::Image & image )
97103
{
98104
if ( image.empty() )
@@ -141,25 +147,25 @@ void UiWindowX11::_setupImage( const penguinV::Image & image )
141147

142148
void UiWindowX11::drawPoint( const Point2d & point, const PaintColor & color )
143149
{
144-
_point.push_back( PointToDraw( point, ( color.red << 16 ) + ( color.green << 8 ) + color.blue ) );
150+
_point.push_back( PointToDraw( point, color ) );
145151
}
146152

147153
void UiWindowX11::drawLine( const Point2d & start, const Point2d & end, const PaintColor & color )
148154
{
149-
_lines.push_back( LineToDraw( start, end, ( color.red << 16 ) + ( color.green << 8 ) + color.blue ) );
155+
_lines.push_back( LineToDraw( start, end, color ) );
150156
}
151157

152158
void UiWindowX11::drawEllipse( const Point2d & center, double xRadius, double yRadius, const PaintColor & color )
153159
{
154160
// XDrawArc needs x and y coordinates of the upper-left corner of the bounding rectangle but not the center of the ellipse.
155161
const Point2d position( center.x - xRadius, center.y - yRadius );
156162

157-
_ellipses.push_back( EllipseToDraw( position, xRadius * 2, yRadius * 2, ( color.red << 16 ) + ( color.green << 8 ) + color.blue ) );
163+
_ellipses.push_back( EllipseToDraw( position, xRadius * 2, yRadius * 2, color ) );
158164
}
159165

160166
void UiWindowX11::drawRectangle( const Point2d & topLeftCorner, double width, double height, const PaintColor & color )
161167
{
162-
_rectangles.push_back( RectangleToDraw( topLeftCorner, width, height, ( color.red << 16 ) + ( color.green << 8 ) + color.blue ) );
168+
_rectangles.push_back( RectangleToDraw( topLeftCorner, width, height, color ) );
163169
}
164170

165171
#endif

src/ui/x11/x11_ui.h

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,48 +30,9 @@ class UiWindowX11 : public UiWindow
3030
uint32_t _width;
3131
uint32_t _height;
3232

33-
struct PointToDraw
34-
{
35-
PointToDraw( const Point2d & point_ = Point2d(), uint32_t color_ = 0 )
36-
: point( point_ )
37-
, color( color_ )
38-
{}
39-
40-
Point2d point;
41-
uint32_t color;
42-
};
43-
44-
struct LineToDraw
45-
{
46-
LineToDraw( const Point2d & start_ = Point2d(), const Point2d & end_ = Point2d(), uint32_t color_ = 0 )
47-
: start( start_ )
48-
, end( end_ )
49-
, color( color_ )
50-
{}
51-
52-
Point2d start;
53-
Point2d end;
54-
uint32_t color;
55-
};
56-
57-
struct EllipseToDraw
58-
{
59-
EllipseToDraw( const Point2d & topLeft_ = Point2d(), double width_ = 0.0, double height_ = 0.0, uint32_t color_ = 0 )
60-
: topLeft( topLeft_ )
61-
, width( width_ )
62-
, height( height_ )
63-
, color( color_ )
64-
{}
65-
66-
Point2d topLeft;
67-
double width;
68-
double height;
69-
uint32_t color;
70-
};
71-
7233
struct RectangleToDraw
7334
{
74-
RectangleToDraw( const Point2d & topLeft_ = Point2d(), double width_ = 0.0, double height_ = 0.0, uint32_t color_ = 0 )
35+
RectangleToDraw( const Point2d & topLeft_ = Point2d(), double width_ = 0.0, double height_ = 0.0, const PaintColor & color_ = PaintColor() )
7536
: topLeft( topLeft_ )
7637
, width( width_ )
7738
, height( height_ )
@@ -81,15 +42,13 @@ class UiWindowX11 : public UiWindow
8142
Point2d topLeft;
8243
double width;
8344
double height;
84-
uint32_t color;
45+
PaintColor color;
8546
};
8647

87-
std::vector<PointToDraw> _point;
88-
std::vector<LineToDraw> _lines;
89-
std::vector<EllipseToDraw> _ellipses;
9048
std::vector<RectangleToDraw> _rectangles;
9149

9250
void _setupImage( const penguinV::Image & image );
51+
static uint32_t _convertColor(const PaintColor & color = PaintColor());
9352
};
9453

9554
#endif

0 commit comments

Comments
 (0)