From 9e7e803e384ed7bf0f5ccf9a4c381dd13b6a01d1 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Mon, 25 Feb 2008 00:35:35 +0000 Subject: [PATCH] - added support for rounded rectangles for use in the menu SVN-Revision: 5340 --- src/gui/menu.cpp | 11 +++--- src/video/drawing_context.cpp | 16 ++++++--- src/video/drawing_context.hpp | 1 + src/video/drawing_request.hpp | 3 +- src/video/gl_renderer.cpp | 79 +++++++++++++++++++++++++++++++++---------- 5 files changed, 83 insertions(+), 27 deletions(-) diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index 89835157e..9a1635473 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -705,13 +705,14 @@ Menu::draw(DrawingContext& context) } float menu_height = get_height(); - float menu_width = get_width(); + float menu_width = get_width(); /* Draw a transparent background */ - context.draw_filled_rect( - Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10), - Vector(menu_width,menu_height + 20), - Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-10); + context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10), + Vector(pos_x + menu_width/2, pos_y - 24*items.size()/2 + 10 + menu_height)), + Color(0.6f, 0.7f, 0.8f, 0.5f), + 16.0f, + LAYER_GUI-10); for(unsigned int i = 0; i < items.size(); ++i) { diff --git a/src/video/drawing_context.cpp b/src/video/drawing_context.cpp index 7ca3e2634..02712fd10 100644 --- a/src/video/drawing_context.cpp +++ b/src/video/drawing_context.cpp @@ -219,6 +219,7 @@ DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size, fillrectrequest->size = size; fillrectrequest->color = color; fillrectrequest->color.alpha = color.alpha * transform.alpha; + fillrectrequest->radius = 0.0f; request->request_data = fillrectrequest; requests->push_back(request); @@ -228,12 +229,18 @@ void DrawingContext::draw_filled_rect(const Rect& rect, const Color& color, int layer) { + draw_filled_rect(rect, color, 0.0f, layer); +} + +void +DrawingContext::draw_filled_rect(const Rect& rect, const Color& color, float radius, int layer) +{ DrawingRequest* request = new(obst) DrawingRequest(); request->target = target; - request->type = FILLRECT; - request->pos = transform.apply(rect.p1); - request->layer = layer; + request->type = FILLRECT; + request->pos = transform.apply(rect.p1); + request->layer = layer; request->drawing_effect = transform.drawing_effect; request->alpha = transform.alpha; @@ -242,9 +249,10 @@ DrawingContext::draw_filled_rect(const Rect& rect, const Color& color, fillrectrequest->size = Vector(rect.get_width(), rect.get_height()); fillrectrequest->color = color; fillrectrequest->color.alpha = color.alpha * transform.alpha; + fillrectrequest->radius = radius; request->request_data = fillrectrequest; - requests->push_back(request); + requests->push_back(request); } void diff --git a/src/video/drawing_context.hpp b/src/video/drawing_context.hpp index c8f90127e..73713f794 100644 --- a/src/video/drawing_context.hpp +++ b/src/video/drawing_context.hpp @@ -78,6 +78,7 @@ public: void draw_filled_rect(const Vector& topleft, const Vector& size, const Color& color, int layer); void draw_filled_rect(const Rect& rect, const Color& color, int layer); + void draw_filled_rect(const Rect& rect, const Color& color, float radius, int layer); /// Processes all pending drawing requests and flushes the list. void do_drawing(); diff --git a/src/video/drawing_request.hpp b/src/video/drawing_request.hpp index 24e0c0204..5eeb71197 100644 --- a/src/video/drawing_request.hpp +++ b/src/video/drawing_request.hpp @@ -94,8 +94,9 @@ struct GradientRequest struct FillRectRequest { - Color color; + Color color; Vector size; + float radius; }; struct DrawingRequest diff --git a/src/video/gl_renderer.cpp b/src/video/gl_renderer.cpp index 3eee8f118..54e94633a 100644 --- a/src/video/gl_renderer.cpp +++ b/src/video/gl_renderer.cpp @@ -241,23 +241,68 @@ namespace GL const FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data; - float x = request.pos.x; - float y = request.pos.y; - float w = fillrectrequest->size.x; - float h = fillrectrequest->size.y; - - glDisable(GL_TEXTURE_2D); - glColor4f(fillrectrequest->color.red, fillrectrequest->color.green, - fillrectrequest->color.blue, fillrectrequest->color.alpha); - - glBegin(GL_QUADS); - glVertex2f(x, y); - glVertex2f(x+w, y); - glVertex2f(x+w, y+h); - glVertex2f(x, y+h); - glEnd(); - glEnable(GL_TEXTURE_2D); - glColor4f(1, 1, 1, 1); + if (fillrectrequest->radius != 0.0f) + { + // draw round rect + // Keep radius in the limits, so that we get a circle instead of + // just graphic junk + float radius = std::min(fillrectrequest->radius, + std::min(fillrectrequest->size.x/2, + fillrectrequest->size.y/2)); + + // inner rectangle + Rect irect(request.pos.x + radius, + request.pos.y + radius, + request.pos.x + fillrectrequest->size.x - radius, + request.pos.y + fillrectrequest->size.y - radius); + + glDisable(GL_TEXTURE_2D); + glColor4f(fillrectrequest->color.red, fillrectrequest->color.green, + fillrectrequest->color.blue, fillrectrequest->color.alpha); + + + int n = 8; + glBegin(GL_QUAD_STRIP); + for(int i = 0; i <= n; ++i) + { + float x = sinf(i * (M_PI/2) / n) * radius; + float y = cosf(i * (M_PI/2) / n) * radius; + + glVertex2f(irect.get_left() - x, irect.get_top() - y); + glVertex2f(irect.get_right() + x, irect.get_top() - y); + } + for(int i = 0; i <= n; ++i) + { + float x = cosf(i * (M_PI/2) / n) * radius; + float y = sinf(i * (M_PI/2) / n) * radius; + + glVertex2f(irect.get_left() - x, irect.get_bottom() + y); + glVertex2f(irect.get_right() + x, irect.get_bottom() + y); + } + glEnd(); + glEnable(GL_TEXTURE_2D); + glColor4f(1, 1, 1, 1); + } + else + { + float x = request.pos.x; + float y = request.pos.y; + float w = fillrectrequest->size.x; + float h = fillrectrequest->size.y; + + glDisable(GL_TEXTURE_2D); + glColor4f(fillrectrequest->color.red, fillrectrequest->color.green, + fillrectrequest->color.blue, fillrectrequest->color.alpha); + + glBegin(GL_QUADS); + glVertex2f(x, y); + glVertex2f(x+w, y); + glVertex2f(x+w, y+h); + glVertex2f(x, y+h); + glEnd(); + glEnable(GL_TEXTURE_2D); + glColor4f(1, 1, 1, 1); + } } void -- 2.11.0