Creating a custom rendering routine

From uGFX Wiki
Revision as of 12:39, 20 August 2023 by Tectu (Talk | contribs) (Fix typo)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Every widget comes with a custom render interface. The default style in which a widget is drawn is very basic and minimalistic in order to make it run on the even lowest performance systems smoothly. However, the custom render interface allows you to submit your own rendering routines. This does not only provide a very flexible way to render a widget matching to your systems performance, but it gives you also the possibility to render a widget matching your applications style.

The interface

Every widget provides a function to submit a custom rendering function:

void gwinSetCustomDraw(GHandle gh, CustomWidgetDrawFunction fn, void* param);

The CustomWidgetDrawFunction is a typedef'ed function pointer:

typedef void (*CustomWidgetDrawFunction)(GWidgetObject* gw, void* param);

The param parameter can be used to pass a custom parameter such as an image pointer in case of you're rendering routine needs to draw an image. However, in most of the cases, this parameter will be NULL.

Note: The pointer to the custom rendering routine can also be passed through the initialization struct (the customDraw field).

Note: Do never use the gwinDrawXxx() calls inside a rendering routine as this would lock the widget again. Use gdispDrawXxx() instead

Widget information

In order to render the widget you need some information about it (eg. position, size, font, ...). All these information can be fetched from the GWidgetObject struct which is passed as a parameter to the custom rendering function. This is the only time where you're not only allowed to but even obligated to directly access the struct members directly instead of using the equivalent GWIN API calls to retrieve these information. Do never access struct members directly outside of the custom rendering routine!

Important: Do never use the gwinDrawXxx() calls inside a rendering routine as this would lock the widget again. Use gdispDrawXxx() instead.

To retrieve all the widget specific information the GWidgetObject struct needs to be casted to the actual widget type object.

Important: All drawing operations must take place inside of the widget area. Do not draw outside of the widget area.

Example

We are now going to show on a real world example how a custom rendering routine can be implemented. We will write a custom rendering routine for the button widget that will allow to display an icon on the left side of the text. We will keep it as simple as possible: We won't do color blending for gradients and other things that would make this look nicer in order to focus on the things that are actually important. The expected result can be seen in the image on the right.

The expected result. The Settings button on the right side is being pressed

Implementation

  1. void myButtonRendering(GWidgetObject* gw, void* param)
  2. {
  3. 	const GColorSet* colors;
  4.  
  5. 	// Get the icon/image. User must open the image beforehand.
  6. 	gdispImage* icon = (gdispImage*)param;
  7. 	if (!icon) {
  8. 		return;
  9. 	}
  10.  
  11. 	// Get the appropriate color pallete from the widget style
  12. 	if (!gwinGetEnabled((GHandle)gw))
  13. 		colors = &gw->pstyle->disabled;
  14. 	else if ((gw->g.flags & GBUTTON_FLG_PRESSED))
  15. 		colors = &gw->pstyle->pressed;
  16. 	else
  17. 		colors = &gw->pstyle->enabled;
  18.  
  19. 	// Draw the basic rectangle with border
  20. 	gdispGFillArea(gw->g.display, gw->g.x+1, gw->g.y+1, gw->g.width-2, gw->g.height-2, colors->fill);
  21. 	gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, colors->edge);
  22.  
  23. 	// Draw the string. Use StringBox() for proper justification and word-wrapping support
  24. 	gdispGDrawStringBox(gw->g.display, gw->g.x+gw->g.height, gw->g.y, gw->g.width-gw->g.height, gw->g.height, gw->text, gw->g.font, colors->text, justifyLeft);
  25.  
  26. 	// Draw the image
  27. 	gdispGImageDraw(gw->g.display, icon, gw->g.x+(gw->g.height-icon->width)/2, gw->g.y+(gw->g.height-icon->height)/2, icon->width, icon->height, 0, 0);
  28. }

Usage

The following program will show how the same rendering routine can be applied to two buttons that are using two different icons:

  1. #include "gfx.h"
  2.  
  3. GHandle ghButton1;
  4. GHandle ghButton2;
  5. gdispImage iconWrench;
  6. gdispImage iconUgfx;
  7. font_t font1;
  8. font_t font2;
  9.  
  10. void myButtonRendering(GWidgetObject* gw, void* param)
  11. {
  12. 	const GColorSet* colors;
  13.  
  14. 	// Get the icon/image
  15. 	gdispImage* icon = (gdispImage*)param;
  16. 	if (!icon) {
  17. 		return;
  18. 	}
  19.  
  20. 	// Get the appropriate color pallete from the widget style
  21. 	if (!gwinGetEnabled((GHandle)gw))
  22. 		colors = &gw->pstyle->disabled;
  23. 	else if ((gw->g.flags & GBUTTON_FLG_PRESSED))
  24. 		colors = &gw->pstyle->pressed;
  25. 	else
  26. 		colors = &gw->pstyle->enabled;
  27.  
  28. 	// Draw the basic rectangle with border
  29. 	gdispGFillArea(gw->g.display, gw->g.x+1, gw->g.y+1, gw->g.width-2, gw->g.height-2, colors->fill);
  30. 	gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, colors->edge);
  31.  
  32. 	// Draw the string. Use StringBox() for proper justification and word-wrapping support
  33. 	gdispGDrawStringBox(gw->g.display, gw->g.x+gw->g.height, gw->g.y, gw->g.width-gw->g.height, gw->g.height, gw->text, gw->g.font, colors->text, justifyLeft);
  34.  
  35. 	// Draw the image
  36. 	gdispGImageDraw(gw->g.display, icon, gw->g.x+(gw->g.height-icon->width)/2, gw->g.y+(gw->g.height-icon->height)/2, icon->width, icon->height, 0, 0);
  37. }
  38.  
  39. static void createWidgets(void) {
  40. 	GWidgetInit	wi;
  41.  
  42. 	// Prepare the images
  43. 	gdispImageOpenFile(&iconWrench, "settings.gif");
  44. 	gdispImageOpenFile(&iconUgfx, "padlock.gif");
  45.  
  46. 	// Apply some default values for GWIN
  47. 	gwinWidgetClearInit(&wi);
  48. 	wi.g.show = TRUE;
  49.  
  50. 	// Create a regular button
  51. 	wi.g.width = 180;
  52. 	wi.g.height = 52;
  53. 	wi.g.y = 10;
  54. 	wi.g.x = 10;
  55. 	wi.customParam = &iconWrench;
  56. 	wi.customDraw = myButtonRendering;
  57. 	wi.text = "Setting";
  58. 	ghButton1 = gwinButtonCreate(0, &wi);
  59.  
  60. 	// Create a regular button
  61. 	wi.g.width = 180;
  62. 	wi.g.height = 52;
  63. 	wi.g.y = 70;
  64. 	wi.g.x = 10;
  65. 	wi.customParam = &iconUgfx;
  66. 	wi.customDraw = myButtonRendering;
  67. 	wi.text = "Lock";
  68. 	ghButton2 = gwinButtonCreate(0, &wi);
  69. }
  70.  
  71. int main(void)
  72. {
  73. 	gfxInit();
  74.  
  75. 	gwinSetDefaultFont(gdispOpenFont("*"));
  76. 	gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE);
  77. 	gdispClear(White);
  78.  
  79. 	createWidgets();
  80.  
  81. 	while(1) {
  82. 		gfxSleepMilliseconds(250);
  83. 	}
  84. }