Added selection rectangle.

This commit is contained in:
2021-09-02 10:30:25 -07:00
parent 21af7906c4
commit 6e0d594056
7 changed files with 100 additions and 5 deletions

View File

@ -70,6 +70,7 @@
#include "ui/label.h"
#include "ui/menu.h"
#include "ui/menulist.h"
#include "ui/rectangle.h"
// Utility Objects
#include "util/array.h"

View File

@ -10,6 +10,10 @@
#include "frame.h"
#include "label.h"
#include "menu.h"
#include "rectangle.h"
/** Color of the menulist selection rectangle */
#define MENULIST_SELECTION_COLOR ((pixel_t){ .r=255, .g=255, .b=255, .a=150 })
/** Maximum number of items that the list supports */
#define MENULIST_ITEMS_MAX 32
@ -21,5 +25,6 @@ typedef struct {
label_t labels[MENULIST_ITEMS_MAX];
frame_t frame;
menu_t menu;
rectangle_t selection;
uint8_t count;
} menulist_t;

View File

@ -0,0 +1,17 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "../libs.h"
#include "../display/primitive.h"
typedef struct {
float x, y;
float width, height;
texture_t texture;
primitive_t quad;
} rectangle_t;

View File

@ -14,6 +14,11 @@ void menuListInit(menulist_t *ml, texture_t *texture) {
ml->y = 0;
ml->count = 0;
menuInit(&ml->menu);
rectangleInit(&ml->selection);
rectangleSetColor(&ml->selection, MENULIST_SELECTION_COLOR);
frameInit(&ml->frame);
ml->frame.texture = texture;
}
@ -57,6 +62,9 @@ void menuListUpdate(menulist_t *ml, engine_t *engine) {
void menuListRender(menulist_t *ml, shader_t *shader) {
uint8_t i;
label_t *label;
float fontScale;
fontScale = fontGetScale(ml->labels->fontSize);
// Render the frame
ml->frame.x = ml->x;
@ -64,15 +72,19 @@ void menuListRender(menulist_t *ml, shader_t *shader) {
frameRender(&ml->frame, shader);
// Render selection box.
ml->selection.x = ml->x + FRAME_BORDER_SIZE;
ml->selection.y = ml->y + FRAME_BORDER_SIZE +(
ml->menu.selected * FONT_LINE_HEIGHT * fontScale
);
ml->selection.width = 100;
ml->selection.height = FONT_LINE_HEIGHT * fontScale;
rectangleRender(&ml->selection, shader);
// Render each labels
// Render each label.
for(i = 0; i < ml->count; i++) {
label = ml->labels + i;
label->x = FRAME_BORDER_SIZE;
if(ml->menu.selected == i) label->x += 4;
label->y = FRAME_BORDER_SIZE + (
i * FONT_LINE_HEIGHT * fontGetScale(label->fontSize)
);
label->y = FRAME_BORDER_SIZE + i * FONT_LINE_HEIGHT * fontScale;
labelRender(label, shader);
}
}

View File

@ -10,6 +10,7 @@
#include "label.h"
#include "menu.h"
#include "frame.h"
#include "rectangle.h"
void menuListInit(menulist_t *ml, texture_t *texture);

38
src/ui/rectangle.c Normal file
View File

@ -0,0 +1,38 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "rectangle.h"
void rectangleInit(rectangle_t *rectangle) {
rectangle->x = 0;
rectangle->y = 0;
rectangle->width = 32;
rectangle->height = 32;
textureInit(&rectangle->texture, 1, 1, NULL);
quadInit(&rectangle->quad, 0, 0,0,0,0, 1,1,1,1);
}
void rectangleSetColor(rectangle_t *rectangle, pixel_t color) {
textureBufferPixels(&rectangle->texture,
0, 0, 1, 1, &color
);
}
void rectangleRender(rectangle_t *rectangle, shader_t *shader) {
shaderUsePositionAndScale(shader,
rectangle->x, rectangle->y, 0,
0, 0, 0,
rectangle->width, rectangle->height, 1
);
shaderUseTexture(shader, &rectangle->texture);
primitiveDraw(&rectangle->quad, 0, -1);
}
void rectangleDispose(rectangle_t *rectangle) {
primitiveDispose(&rectangle->quad);
textureDispose(&rectangle->texture);
}

21
src/ui/rectangle.h Normal file
View File

@ -0,0 +1,21 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <dawn/dawn.h>
#include "../display/texture.h"
#include "../display/shader.h"
#include "../display/primitive.h"
#include "../display/primitives/quad.h"
void rectangleInit(rectangle_t *rectangle);
void rectangleSetColor(rectangle_t *rectangle, pixel_t color);
void rectangleRender(rectangle_t *rectangle, shader_t *shader);
void rectangleDispose(rectangle_t *rectangle);