1
0
Fork 0

Control, Context, LinkList, et al: Restructuring

LinkList: Rename Lth_LinkList* functions to Lth_List*
LinkList: Split Lth_LinkListInsert
LinkList: Add Lth_ListLinkTail

Control: Make controls associated with a context and font

Context: Fix mapped windows with unspecified position being placed wrong
Context: Fix mapped windows being added backwards

Draw: Fix rectangles being drawn wrong
master
Marrub 2016-10-18 13:59:02 -04:00
parent a93f60355f
commit 47acf3cadd
10 changed files with 134 additions and 58 deletions

Binary file not shown.

View File

@ -21,9 +21,9 @@
Lth_X(SIGDESTROY, destroy, void, struct Lth_Control *)
Lth_X(SIGCLICK , click, void, struct Lth_Control *)
Lth_X(SIGDRAW , draw, void, struct Lth_Context *, struct Lth_Control *)
Lth_X(SIGPSTDRAW, postdraw, void, struct Lth_Context *, struct Lth_Control *)
Lth_X(SIGUPDATE , update, void, struct Lth_Context *, struct Lth_Control *)
Lth_X(SIGDRAW , draw, void, struct Lth_Control *)
Lth_X(SIGPSTDRAW, postdraw, void, struct Lth_Control *)
Lth_X(SIGUPDATE , update, void, struct Lth_Control *)
#undef Lth_X
@ -58,7 +58,6 @@ Lth_X(SIGUPDATE , update, void, struct Lth_Context *, struct Lth_Control *)
// Type Definitions |
//
struct Lth_Context;
struct Lth_Control;
typedef void (*Lth_Callback_t)(void);

View File

@ -23,6 +23,7 @@
//
// internal data
// clip
// lastmap
// map
//
// read-only
@ -30,16 +31,19 @@
// h: height of screen
//
// read-write
// hid: HUD ID range
// hid: HUD ID range
// mapspace: space between newly mapped windows that are auto-positioned
//
typedef struct Lth_Context
{
struct { Lth_Rect rects[16]; int num; } clip;
Lth_LinkList *map;
struct { int x, y; } lastmap;
struct { Lth_LinkList *head, *tail; } map;
int w, h;
Lth_HIDRange hid;
struct { int x, y; } mapspace;
} Lth_Context;

View File

@ -17,6 +17,7 @@
#include "Lth_linklist.h"
#include "Lth_types.h"
#include "Lth_context.h"
#include "Lth_font.h"
//----------------------------------------------------------------------------|
@ -29,7 +30,7 @@
// Has members of Lth_Rect (read-write).
//
// internal data
// desclink
// link
// descendants
// cb
//
@ -37,19 +38,22 @@
// parent: control's parent (NULL if none)
//
// read-write
// userdata: opaque pointer to associate with this control
// userdata: opaque pointer for the user to use
// font: default font for drawing
// ctx: pointer to associated context
//
//
typedef struct Lth_Control
{
Lth_CallbackSet cb;
Lth_LinkList desclink;
Lth_LinkList *descendants;
Lth_LinkList link;
Lth_LinkList *descendants;
struct Lth_Control *parent;
int lx, ly;
void *userdata;
void *userdata;
Lth_Font *font;
Lth_Context *ctx;
Lth_Mixin(Lth_Rect);
} Lth_Control;
@ -58,8 +62,8 @@ typedef struct Lth_Control
// Lth_LayoutControl
//
// read-only
// lx: layout x
// ly: layout y
// lx: layout x
// ly: layout y
//
typedef struct Lth_LayoutControl
{
@ -72,8 +76,10 @@ typedef struct Lth_LayoutControl
// Extern Functions |
//
void Lth_ControlRun(Lth_Context *ctx, void *ctrl_);
Lth_Font *Lth_ControlFont(void *ctrl_);
void Lth_ControlRun(void *ctrl_);
void Lth_ControlConnect(void *ctrl_, Lth_Signal signal, Lth_Callback_t cb);
void Lth_ControlDestroy(void *ctrl_);
void Lth_ControlAttach(void *ctrlsrc_, void *ctrldst_);
#endif//lithos3__Lth_control_h

View File

@ -24,6 +24,24 @@
for(Lth_LinkList *list = (lst); list;) \
__with(Lth_LinkList *next = list->next, **prev = list->prev; \
tmpv = list->owner;)
#define Lth_ListInsert(list, object, head) \
(void) \
( \
Lth_ListLink((list), (head)), \
(list)->owner = object \
)
#define Lth_ListInsertTail(list, object, head, tail) \
(void) \
( \
Lth_ListLinkTail((list), (head), (tail)), \
(list)->owner = object \
)
#define Lth_ListMove(list, head) \
(void) \
( \
Lth_ListRemove((list)), \
Lth_ListLink((list), (head)) \
)
//----------------------------------------------------------------------------|
@ -45,14 +63,12 @@ typedef struct Lth_LinkList
//
//
// Lth_LinkListInsert
// Lth_ListLink
//
static inline void Lth_LinkListInsert(Lth_LinkList *list, void *object,
Lth_LinkList **head)
static inline void Lth_ListLink(Lth_LinkList *list, Lth_LinkList **head)
{
Lth_assert(list != NULL);
Lth_assert(head != NULL);
Lth_assert(*head != NULL);
Lth_LinkList *next = *head;
@ -61,14 +77,33 @@ static inline void Lth_LinkListInsert(Lth_LinkList *list, void *object,
list->prev = head;
*head = list;
list->owner = object;
}
//
// Lth_LinkListRemove
// Lth_ListLinkTail
//
static inline void Lth_LinkListRemove(Lth_LinkList *list)
static inline void Lth_ListLinkTail(Lth_LinkList *list, Lth_LinkList **head,
Lth_LinkList **tail)
{
Lth_assert(list != NULL);
Lth_assert(head != NULL);
Lth_assert(tail != NULL);
Lth_LinkList *prev = *tail;
if(*(list->prev = head))
list->prev = &prev->next;
else
*head = list;
prev->next = list;
*tail = list;
}
//
// Lth_ListRemove
//
static inline void Lth_ListRemove(Lth_LinkList *list)
{
Lth_assert(list != NULL);

View File

@ -22,13 +22,15 @@
//
// Lth_ButtonDraw
//
static void Lth_ButtonDraw(Lth_Context *ctx, Lth_Button *ctrl)
static void Lth_ButtonDraw(Lth_Button *ctrl)
{
ACS_SetFont(s"CONFONT");
ACS_BeginPrint();
Lth_PrintString(ctrl->label);
Lth_HudMessagePlain(ctx->hid.cur--, ctrl->x - 8 + Lth_A_Lef,
ctrl->y - 8 + Lth_A_Top);
Lth_DrawRect(ctx, ctrl->x, ctrl->y, ctrl->w, ctrl->h, 0.5k);
Lth_HudMessagePlain(ctrl->ctx->hid.cur--,
ctrl->x - 8 + Lth_A_Cen,
ctrl->y - 8 + Lth_A_Cen);
Lth_DrawRect(ctrl->ctx, ctrl->x, ctrl->y, ctrl->w, ctrl->h, 0.5k);
}
//

View File

@ -31,12 +31,28 @@ Lth_Context *Lth_ContextNew(int w, int h, Lth_HID hidBase, Lth_HID hidEnd)
ctx->w = w;
ctx->h = h;
ctx->lastmap.x = ctx->lastmap.y = -1;
ctx->hid.cur = ctx->hid.base = hidBase;
ctx->hid.end = hidEnd;
ctx->mapspace.x = 24;
ctx->mapspace.y = 16;
return ctx;
}
//
// Lth_ContextSetup
//
void Lth_ContextSetup(Lth_Context *ctx, void *ctrl_)
{
Lth_Control *ctrl = ctrl_;
Lth_ListForEach(Lth_Control *owner, ctrl->descendants)
Lth_ContextSetup(ctx, owner);
ctrl->ctx = ctx;
}
//
// Lth_ContextMap
//
@ -45,16 +61,14 @@ void Lth_ContextMap(Lth_Context *ctx, Lth_Window *ctrl)
Lth_assert(ctx != NULL);
Lth_assert(ctrl != NULL);
if(ctrl->x == -1) ctrl->x = ctx->w / 2;
if(ctrl->y == -1) ctrl->y = ctx->h / 2;
if(ctrl->x == -1) ctrl->x = ctx->lastmap.x += 16;
else ctx->lastmap.x = ctrl->x;
if(ctx->map)
Lth_LinkListInsert(&ctrl->desclink, ctrl, &ctx->map);
else
{
ctx->map = &ctrl->desclink;
ctx->map->owner = ctrl;
}
if(ctrl->y == -1) ctrl->y = ctx->lastmap.y += 16;
else ctx->lastmap.y = ctrl->y;
Lth_ContextSetup(ctx, ctrl);
Lth_ListInsertTail(&ctrl->link, ctrl, &ctx->map.head, &ctx->map.tail);
}
//
@ -65,11 +79,11 @@ void Lth_ContextDestroy(Lth_Context *ctx)
if(ctx == NULL)
return;
if(ctx->map)
if(ctx->map.head)
{
Lth_ListFor(Lth_Control *owner, ctx->map)
Lth_ListFor(Lth_Control *owner, ctx->map.head)
{
Lth_LinkListRemove(list);
Lth_ListRemove(list);
if(owner) Lth_ControlDestroy(owner);
list = next;
}
@ -88,9 +102,9 @@ void Lth_ContextRun(Lth_Context *ctx)
ACS_SetHudSize(ctx->w, ctx->h, true);
ctx->hid.cur = ctx->hid.base;
if(ctx->map)
Lth_ListForEach(Lth_Control *owner, ctx->map)
Lth_ControlRun(ctx, owner);
if(ctx->map.head)
Lth_ListForEach(Lth_Control *owner, ctx->map.head)
Lth_ControlRun(owner);
}
//

View File

@ -24,16 +24,16 @@
//
// Lth_ControlRun
//
void Lth_ControlRun(Lth_Context *ctx, void *ctrl_)
void Lth_ControlRun(void *ctrl_)
{
Lth_assert(ctx != NULL);
Lth_assert(ctrl_ != NULL);
Lth_Control *ctrl = ctrl_;
Lth_ControlCall(ctrl, update, ctx, ctrl);
Lth_ControlCall(ctrl, draw, ctx, ctrl);
Lth_ControlCall(ctrl, postdraw, ctx, ctrl);
Lth_assert(ctrl != NULL);
Lth_assert(ctrl->ctx != NULL);
Lth_ControlCall(ctrl, update, ctrl);
Lth_ControlCall(ctrl, draw, ctrl);
Lth_ControlCall(ctrl, postdraw, ctrl);
}
//
@ -75,7 +75,7 @@ void Lth_ControlDestroy(void *ctrl_)
}
Lth_CallReverse(ctrl->cb.destroy, ctrl);
Lth_LinkListRemove(&ctrl->desclink);
Lth_ListRemove(&ctrl->link);
#define Lth_X(sig, name, ret, ...) \
free(ctrl->cb.name.data);
@ -84,5 +84,17 @@ void Lth_ControlDestroy(void *ctrl_)
free(ctrl);
}
//
// Lth_ControlAttach
//
void Lth_ControlAttach(void *ctrlsrc_, void *ctrldst_)
{
Lth_assert(ctrlsrc_ != NULL);
Lth_assert(ctrldst_ != NULL);
Lth_Control *ctrlsrc = ctrlsrc_, *ctrldst = ctrldst_;
Lth_ListInsert(&ctrlsrc->link, ctrlsrc, &ctrldst->descendants);
ctrlsrc->ctx = ctrldst->ctx;
}
// EOF

View File

@ -30,7 +30,7 @@ void Lth_DrawRectAndClip(Lth_Context *ctx, int x, int y, int w, int h,
for(int ix = 0; ix < w; ix += 128)
for(int iy = 0; iy < h; iy += 128)
Lth_DrawSpriteAlpha(s"lithos_gfx/Base/Black128.png", ctx->hid.cur--,
x + ix, y + iy, alpha);
x + ix + Lth_A_Lef, y + iy + Lth_A_Top, alpha);
}
//

View File

@ -22,21 +22,25 @@
//
// Lth_WindowDraw
//
static void Lth_WindowDraw(Lth_Context *ctx, Lth_Window *ctrl)
static void Lth_WindowDraw(Lth_Window *ctrl)
{
ACS_SetFont(s"CONFONT");
Lth_DrawRectAndClip(ctrl->ctx, ctrl->x, ctrl->y - 8, ctrl->w, 8, 0.35k);
ACS_SetFont(s"SMALLFNT");
ACS_BeginPrint();
Lth_PrintString(ctrl->title);
Lth_HudMessagePlain(ctx->hid.cur--, ctrl->x - 8 + Lth_A_Lef,
Lth_HudMessagePlain(ctrl->ctx->hid.cur--,
ctrl->x + Lth_A_Lef,
ctrl->y - 8 + Lth_A_Top);
Lth_DrawRectAndClip(ctx, ctrl->x, ctrl->y, ctrl->w, ctrl->h, 0.5k);
Lth_ContextClipPop(ctrl->ctx);
Lth_DrawRectAndClip(ctrl->ctx, ctrl->x, ctrl->y, ctrl->w, ctrl->h, 0.5k);
}
//
// Lth_WindowPostDraw
//
static void Lth_WindowPostDraw(Lth_Context *ctx, Lth_Window *ctrl)
static void Lth_WindowPostDraw(Lth_Window *ctrl)
{
Lth_ContextClipPop(ctx);
Lth_ContextClipPop(ctrl->ctx);
}
//
@ -67,7 +71,7 @@ Lth_Window *Lth_WindowNew(char const *title, int w, int h, int x, int y)
ctrl->y = y;
Lth_WindowSetTitle(ctrl, title);
Lth_ControlConnect(ctrl, Lth_SIGDRAW, Lth_Callback(Lth_WindowDraw));
Lth_ControlConnect(ctrl, Lth_SIGDRAW, Lth_Callback(Lth_WindowDraw));
Lth_ControlConnect(ctrl, Lth_SIGPSTDRAW, Lth_Callback(Lth_WindowPostDraw));
Lth_ControlConnect(ctrl, Lth_SIGDESTROY, Lth_Callback(Lth_WindowDestroy));