Expanded the drawtext example to allow for multiple examples and options in the examples. Our old makefiles wouldn't allow examples to be spread across multiple files like this, so yay cmake?

This commit is contained in:
Pietro Gagliardi 2017-01-20 13:25:21 -05:00
parent 2d09f22932
commit 6ef6ed8cde
4 changed files with 145 additions and 69 deletions

View File

@ -32,9 +32,12 @@ if(NOT WIN32)
endif() endif()
_add_example(drawtext _add_example(drawtext
drawtext/basic.c
drawtext/main.c drawtext/main.c
${_EXAMPLE_RESOURCES_RC} ${_EXAMPLE_RESOURCES_RC}
) )
target_include_directories(drawtext
PRIVATE drawtext)
add_custom_target(examples add_custom_target(examples
DEPENDS DEPENDS

81
examples/drawtext/basic.c Normal file
View File

@ -0,0 +1,81 @@
// 17 january 2017
#include "drawtext.h"
static const char *text =
"It is with a kind of fear that I begin to write the history of my life. "
"I have, as it were, a superstitious hesitation in lifting the veil that "
"clings about my childhood like a golden mist. The task of writing an "
"autobiography is a difficult one. When I try to classify my earliest "
"impressions, I find that fact and fancy look alike across the years that "
"link the past with the present. The woman paints the child's experiences "
"in her own fantasy. A few impressions stand out vividly from the first "
"years of my life; but \"the shadows of the prison-house are on the rest.\" "
"Besides, many of the joys and sorrows of childhood have lost their "
"poignancy; and many incidents of vital importance in my early education "
"have been forgotten in the excitement of great discoveries. In order, "
"therefore, not to be tedious I shall try to present in a series of "
"sketches only the episodes that seem to me to be the most interesting "
"and important."
"";
static char fontFamily[] = "Palatino";
// TODO should be const; look at constructor function
static uiDrawFontDescriptor defaultFont = {
.Family = fontFamily,
.Size = 18,
.Weight = uiDrawTextWeightNormal,
.Italic = uiDrawTextItalicNormal,
.Stretch = uiDrawTextStretchNormal,
};
static uiAttributedString *attrstr;
#define margins 10
static void draw(uiAreaDrawParams *p)
{
uiDrawPath *path;
uiDrawTextLayout *layout;
path = uiDrawNewPath(uiDrawFillModeWinding);
uiDrawPathAddRectangle(path, margins, margins,
p->AreaWidth - 2 * margins,
p->AreaHeight - 2 * margins);
uiDrawPathEnd(path);
uiDrawClip(p->Context, path);
uiDrawFreePath(path);
// TODO get rid of this later
path = uiDrawNewPath(uiDrawFillModeWinding);
uiDrawPathAddRectangle(path, -100, -100,
p->AreaWidth * 2,
p->AreaHeight * 2);
uiDrawPathEnd(path);
uiDrawBrush b;
b.Type = uiDrawBrushTypeSolid;
b.R = 0.0;
b.G = 1.0;
b.B = 0.0;
b.A = 1.0;
uiDrawFill(p->Context, path, &b);
uiDrawFreePath(path);
layout = uiDrawNewTextLayout(attrstr,
&defaultFont,
p->AreaWidth - 2 * margins);
uiDrawText(p->Context, layout, margins, margins);
uiDrawFreeTextLayout(layout);
}
static struct example basicExample;
struct example *mkBasicExample(void)
{
basicExample.name = "Basic Paragraph of Text";
basicExample.panel = uiControl(uiNewVerticalBox());
basicExample.draw = draw;
attrstr = uiNewAttributedString(text);
return &basicExample;
}
// TODO on GTK+ an area by itself in a window doesn't get expand properties set properly?

View File

@ -0,0 +1,17 @@
// 20 january 2017
#include <stdio.h>
#include <string.h>
#include "../../ui.h"
struct example {
const char *name;
uiControl *panel;
void (*draw)(uiAreaDrawParams *p);
// TODO mouse and key?
};
// main.c
extern void redraw(void);
// basic.c
extern struct example *mkBasicExample(void);

View File

@ -1,74 +1,32 @@
// 17 january 2017 // 17 january 2017
#include <stdio.h> #include "drawtext.h"
#include <string.h>
#include "../../ui.h"
uiWindow *mainwin; static uiWindow *mainwin;
uiArea *area; static uiBox *box;
uiAreaHandler handler; static uiCombobox *exampleList;
static uiArea *area;
static uiAreaHandler handler;
const char *text = #define nExamples 20
"It is with a kind of fear that I begin to write the history of my life. " static struct example *examples[nExamples];
"I have, as it were, a superstitious hesitation in lifting the veil that " static int curExample = 0;
"clings about my childhood like a golden mist. The task of writing an "
"autobiography is a difficult one. When I try to classify my earliest "
"impressions, I find that fact and fancy look alike across the years that "
"link the past with the present. The woman paints the child's experiences "
"in her own fantasy. A few impressions stand out vividly from the first "
"years of my life; but \"the shadows of the prison-house are on the rest.\" "
"Besides, many of the joys and sorrows of childhood have lost their "
"poignancy; and many incidents of vital importance in my early education "
"have been forgotten in the excitement of great discoveries. In order, "
"therefore, not to be tedious I shall try to present in a series of "
"sketches only the episodes that seem to me to be the most interesting "
"and important."
"";
char fontFamily[] = "Palatino";
// TODO should be const; look at constructor function
uiDrawFontDescriptor defaultFont = {
.Family = fontFamily,
.Size = 18,
.Weight = uiDrawTextWeightNormal,
.Italic = uiDrawTextItalicNormal,
.Stretch = uiDrawTextStretchNormal,
};
uiAttributedString *attrstr;
#define margins 10 static void onExampleChanged(uiCombobox *c, void *data)
{
uiControlHide(examples[curExample]->panel);
curExample = uiComboboxSelected(exampleList);
uiControlShow(examples[curExample]->panel);
redraw();
}
void redraw(void)
{
uiAreaQueueRedrawAll(area);
}
static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p) static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *p)
{ {
uiDrawPath *path; examples[curExample]->draw(p);
uiDrawTextLayout *layout;
path = uiDrawNewPath(uiDrawFillModeWinding);
uiDrawPathAddRectangle(path, margins, margins,
p->AreaWidth - 2 * margins,
p->AreaHeight - 2 * margins);
uiDrawPathEnd(path);
uiDrawClip(p->Context, path);
uiDrawFreePath(path);
// TODO get rid of this later
path = uiDrawNewPath(uiDrawFillModeWinding);
uiDrawPathAddRectangle(path, -100, -100,
p->AreaWidth * 2,
p->AreaHeight * 2);
uiDrawPathEnd(path);
uiDrawBrush b;
b.Type = uiDrawBrushTypeSolid;
b.R = 0.0;
b.G = 1.0;
b.B = 0.0;
b.A = 1.0;
uiDrawFill(p->Context, path, &b);
uiDrawFreePath(path);
layout = uiDrawNewTextLayout(attrstr,
&defaultFont,
p->AreaWidth - 2 * margins);
uiDrawText(p->Context, layout, margins, margins);
uiDrawFreeTextLayout(layout);
} }
static void handlerMouseEvent(uiAreaHandler *a, uiArea *area, uiAreaMouseEvent *e) static void handlerMouseEvent(uiAreaHandler *a, uiArea *area, uiAreaMouseEvent *e)
@ -109,6 +67,7 @@ int main(void)
{ {
uiInitOptions o; uiInitOptions o;
const char *err; const char *err;
int n;
handler.Draw = handlerDraw; handler.Draw = handlerDraw;
handler.MouseEvent = handlerMouseEvent; handler.MouseEvent = handlerMouseEvent;
@ -126,18 +85,34 @@ int main(void)
uiOnShouldQuit(shouldQuit, NULL); uiOnShouldQuit(shouldQuit, NULL);
attrstr = uiNewAttributedString(text);
mainwin = uiNewWindow("libui Text-Drawing Example", 640, 480, 1); mainwin = uiNewWindow("libui Text-Drawing Example", 640, 480, 1);
uiWindowOnClosing(mainwin, onClosing, NULL); uiWindowOnClosing(mainwin, onClosing, NULL);
box = uiNewVerticalBox();
uiWindowSetChild(mainwin, uiControl(box));
exampleList = uiNewCombobox();
uiBoxAppend(box, uiControl(exampleList), 0);
area = uiNewArea(&handler); area = uiNewArea(&handler);
// TODO on GTK+ this doesn't get expand properties set properly? uiBoxAppend(box, uiControl(area), 1);
uiWindowSetChild(mainwin, uiControl(area));
n = 0;
examples[n] = mkBasicExample();
uiComboboxAppend(exampleList, examples[n]->name);
uiBoxAppend(box, examples[n]->panel, 0);
n++;
// and set things up for the initial state
uiComboboxSetSelected(exampleList, 0);
uiComboboxOnSelected(exampleList, onExampleChanged, NULL);
// and set up the first one
onExampleChanged(NULL, NULL);
uiControlShow(uiControl(mainwin)); uiControlShow(uiControl(mainwin));
uiMain(); uiMain();
uiFreeAttributedString(attrstr);
// TODO free examples
uiUninit(); uiUninit();
return 0; return 0;
} }