Skip to content

Commit

Permalink
Move identical structures in a common location.
Browse files Browse the repository at this point in the history
  • Loading branch information
fullset committed Dec 24, 2019
1 parent 978233f commit 37c8d9d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 109 deletions.
46 changes: 46 additions & 0 deletions src/ui/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,52 @@ class UiWindow
protected:
virtual void _display();

struct PointToDraw
{
PointToDraw( const Point2d & point_ = Point2d(), const PaintColor & color_ = PaintColor() )
: point( point_ )
, color( color_ )
{
}

Point2d point;
PaintColor color;
};

struct LineToDraw
{
LineToDraw( const Point2d & start_ = Point2d(), const Point2d & end_ = Point2d(), const PaintColor & color_ = PaintColor() )
: start( start_ )
, end ( end_ )
, color( color_ )
{
}

Point2d start;
Point2d end;
PaintColor color;
};

struct EllipseToDraw
{
EllipseToDraw( const Point2d & topLeft_ = Point2d(), double width_ = 0.0, double height_ = 0.0, const PaintColor & color_ = PaintColor() )
: topLeft( topLeft_ )
, width( width_ )
, height( height_ )
, color( color_ )
{}

Point2d topLeft;
double width;
double height;
PaintColor color;
};

std::vector<PointToDraw> _point;
std::vector<LineToDraw> _lines;
std::vector<EllipseToDraw> _ellipses;


PenguinV_Image::Image _image; // we store a copy of image
std::string _title;
bool _shown;
Expand Down
11 changes: 6 additions & 5 deletions src/ui/win/win_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ namespace WindowsUi
}

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

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

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

_display();
}
Expand Down
48 changes: 0 additions & 48 deletions src/ui/win/win_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,54 +28,6 @@ class UiWindowWin : public UiWindow
HWND _window; // Windows OS window handle (just an unique ID)
BITMAPINFO * _bmpInfo; // bitmap structure needed for drawing

struct PointToDraw
{
PointToDraw( const Point2d & point_ = Point2d(), const PaintColor & color_ = PaintColor() )
: point( point_ )
, color( color_ )
{
}

Point2d point;
PaintColor color;
};

struct LineToDraw
{
LineToDraw( const Point2d & start_ = Point2d(), const Point2d & end_ = Point2d(), const PaintColor & color_ = PaintColor() )
: start( start_ )
, end ( end_ )
, color( color_ )
{
}

Point2d start;
Point2d end;
PaintColor color;
};

struct EllipseToDraw
{
EllipseToDraw( double left_ = 0, double top_ = 0, double right_ = 0, double bottom_ = 0, const PaintColor & color_ = PaintColor() )
: left ( left_ )
, top ( top_ )
, right ( right_ )
, bottom( bottom_ )
, color ( color_ )
{
}

double left;
double top;
double right;
double bottom;
PaintColor color;
};

std::vector < PointToDraw > _point;
std::vector < LineToDraw > _line;
std::vector < EllipseToDraw > _ellipse;

friend class WindowsUi::UiWindowWinInfo;

void _free();
Expand Down
30 changes: 18 additions & 12 deletions src/ui/x11/x11_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ void UiWindowX11::_display()

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

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

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

XSetForeground( _uiDisplay, defaultGC, foreground );
XDrawArc( _uiDisplay, _window, defaultGC, static_cast<int>( position.x ), static_cast<int>( position.y ), static_cast<int>( width ),
static_cast<int>( height ), 0, 360 * 64 );
XDrawArc( _uiDisplay, _window, defaultGC, static_cast<int>( position.x ), static_cast<int>( position.y ), static_cast<uint32_t>( width ),
static_cast<uint32_t>( height ), 0, 360 * 64 );
}

for ( size_t i = 0u; i < _rectangles.size(); ++i ) {
const Point2d & topLeft = _rectangles[i].topLeft;
const double & width = _rectangles[i].width;
const double & height = _rectangles[i].height;
const uint32_t & foreground = _rectangles[i].color;
const uint32_t foreground = UiWindowX11::_convertColor( _rectangles[i].color );

XSetForeground( _uiDisplay, defaultGC, foreground );
XDrawRectangle( _uiDisplay, _window, defaultGC, static_cast<int>( topLeft.x ), static_cast<int>( topLeft.y ), static_cast<int>( width ),
static_cast<int>( height ) );
XDrawRectangle( _uiDisplay, _window, defaultGC, static_cast<int>( topLeft.x ), static_cast<int>( topLeft.y ), static_cast<uint32_t>( width ),
static_cast<uint32_t>( height ) );
}
}
else if ( (e.type == ClientMessage) && (static_cast<unsigned int>(e.xclient.data.l[0]) == _deleteWindowEvent) )
break;
}
}

uint32_t UiWindowX11::_convertColor(const PaintColor &color)
{
return static_cast<uint32_t>( ( color.red << 16 ) + ( color.green << 8 ) + color.blue );
}

void UiWindowX11::_setupImage( const PenguinV_Image::Image & image )
{
if ( image.empty() )
Expand Down Expand Up @@ -141,25 +147,25 @@ void UiWindowX11::_setupImage( const PenguinV_Image::Image & image )

void UiWindowX11::drawPoint( const Point2d & point, const PaintColor & color )
{
_point.push_back( PointToDraw( point, ( color.red << 16 ) + ( color.green << 8 ) + color.blue ) );
_point.push_back( PointToDraw( point, color ) );
}

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

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

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

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

#endif
47 changes: 3 additions & 44 deletions src/ui/x11/x11_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,9 @@ class UiWindowX11 : public UiWindow
uint32_t _width;
uint32_t _height;

struct PointToDraw
{
PointToDraw( const Point2d & point_ = Point2d(), uint32_t color_ = 0 )
: point( point_ )
, color( color_ )
{}

Point2d point;
uint32_t color;
};

struct LineToDraw
{
LineToDraw( const Point2d & start_ = Point2d(), const Point2d & end_ = Point2d(), uint32_t color_ = 0 )
: start( start_ )
, end( end_ )
, color( color_ )
{}

Point2d start;
Point2d end;
uint32_t color;
};

struct EllipseToDraw
{
EllipseToDraw( const Point2d & topLeft_ = Point2d(), double width_ = 0.0, double height_ = 0.0, uint32_t color_ = 0 )
: topLeft( topLeft_ )
, width( width_ )
, height( height_ )
, color( color_ )
{}

Point2d topLeft;
double width;
double height;
uint32_t color;
};

struct RectangleToDraw
{
RectangleToDraw( const Point2d & topLeft_ = Point2d(), double width_ = 0.0, double height_ = 0.0, uint32_t color_ = 0 )
RectangleToDraw( const Point2d & topLeft_ = Point2d(), double width_ = 0.0, double height_ = 0.0, const PaintColor & color_ = PaintColor() )
: topLeft( topLeft_ )
, width( width_ )
, height( height_ )
Expand All @@ -81,15 +42,13 @@ class UiWindowX11 : public UiWindow
Point2d topLeft;
double width;
double height;
uint32_t color;
PaintColor color;
};

std::vector<PointToDraw> _point;
std::vector<LineToDraw> _lines;
std::vector<EllipseToDraw> _ellipses;
std::vector<RectangleToDraw> _rectangles;

void _setupImage( const PenguinV_Image::Image & image );
static uint32_t _convertColor(const PaintColor & color = PaintColor());
};

#endif

0 comments on commit 37c8d9d

Please sign in to comment.