Added image UI element.

This commit is contained in:
2021-09-02 10:10:00 -07:00
parent 5c4fc01b43
commit c2aea93766
17 changed files with 215 additions and 45 deletions

View File

@ -66,8 +66,10 @@
// User Interface Objects
#include "ui/frame.h"
#include "ui/image.h"
#include "ui/label.h"
#include "ui/menu.h"
#include "ui/menulist.h"
// Utility Objects
#include "util/array.h"

View File

@ -25,19 +25,22 @@
#define FONT_FILL_MODE 1
/** Passed to STBTT for scaling the font, essentially the font resolution */
#define FONT_TEXTURE_SIZE 64
#define FONT_TEXTURE_SIZE 64.0f
/** The global scale, just used to provide fine control of font sizes */
#define FONT_GLOBAL_SCALE 0.5f;
/** Default Font Size (on which all font scales are based) */
#define FONT_SIZE_DEFAULT (FONT_TEXTURE_SIZE / 2)
#define FONT_SIZE_DEFAULT 16.0f
// Chars
#define FONT_NEWLINE '\n'
#define FONT_SPACE ' '
// Heights
#define FONT_LINE_HEIGHT FONT_TEXTURE_SIZE*0.75f
#define FONT_INITIAL_LINE FONT_LINE_HEIGHT*0.75f
#define FONT_SPACE_SIZE FONT_TEXTURE_SIZE*0.45f
#define FONT_LINE_HEIGHT FONT_TEXTURE_SIZE * 0.75f
#define FONT_INITIAL_LINE FONT_LINE_HEIGHT * 0.75f
#define FONT_SPACE_SIZE FONT_TEXTURE_SIZE * 0.45f
/** Maximum number of characters a font text info can hold. */
#define FONT_TEXT_INFO_CHARS_MAX 1024

18
include/dawn/ui/image.h Normal file
View File

@ -0,0 +1,18 @@
/**
* 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;
texture_t *texture;
primitive_t quad;
float u0, v0;
float u1, v1;
} image_t;

View File

@ -7,6 +7,7 @@
#pragma once
#include "../libs.h"
#include "frame.h"
#include "label.h"
#include "menu.h"
@ -18,7 +19,7 @@ typedef struct {
float x, y;
char *items[MENULIST_ITEMS_MAX];
label_t labels[MENULIST_ITEMS_MAX];
frame_t frame;
menu_t menu;
uint8_t count;
uint8_t selected;
} menulist_t;

View File

@ -15,7 +15,7 @@
/** Amount of characters scrolled, per second */
#define VN_TEXTBOX_SCROLL_SPEED 60
#define VN_TEXTBOX_FONT_SIZE 16.0
#define VN_TEXTBOX_FONT_SIZE FONT_SIZE_DEFAULT
#define VN_TEXTBOX_STATE_CLOSED flagDefine(0)

View File

@ -45,7 +45,7 @@ void fontDispose(font_t *font) {
}
float fontGetScale(float fontSize) {
return fontSize / FONT_SIZE_DEFAULT;
return fontSize / FONT_SIZE_DEFAULT * FONT_GLOBAL_SCALE;
}
void fontTextBuffer(font_t *font, primitive_t *primitive, fonttextinfo_t *info){
@ -117,7 +117,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
info->lines[info->lineCount].start = info->realLength;
info->lines[info->lineCount].length = 0;
y += FONT_LINE_HEIGHT * scale;
y += FONT_LINE_HEIGHT;
x = 0;
}
wordX = x;
@ -132,7 +132,7 @@ void fontTextClamp(font_t *font, fonttextinfo_t *info, char *text,
info->lines[info->lineCount].length = 0;
wordStart = info->realLength;
y += FONT_LINE_HEIGHT * scale;
y += FONT_LINE_HEIGHT;
x = 0;
continue;
}

View File

@ -35,7 +35,7 @@ void pokerDiscussionGet(
// Round Start Conversations
case POKER_DISCUSSION_REASON_BLINDS_TAKEN:
discussion->count++;
discussion->messages[0] = "Blinds have been taken.";
discussion->messages[0] = "Blinds\nhave been taken.";
discussion->players[0] = POKER_DEALER_INDEX;
discussion->emotions[0] = VN_CHARACTER_EMOTION_BORED;
break;

View File

@ -7,8 +7,7 @@
#include "pokergame.h"
menu_t menu;
frame_t frame;
menulist_t ml;
bool pokerGameInit(game_t *game) {
pokergame_t *pokerGame = &game->pokerGame;
@ -32,18 +31,10 @@ bool pokerGameInit(game_t *game) {
pokerGameActionStartAdd(pokerGame);
queueNext(&pokerGame->scene.conversation.actionQueue);
frameInit(&frame);
frame.texture = &pokerGame->assets.testTexture;
menuitem_t *item;
menuInit(&menu);
item = menuAdd(&menu);
item->x = 0;
item->y = 0;
item = menuAdd(&menu);
item->x = 1;
item->y = 0;
// TESTING
menuListInit(&ml, &pokerGame->assets.testTexture);
menuListAdd(&ml, &pokerGame->assets.font, "One");
menuListAdd(&ml, &pokerGame->assets.font, "Two");
return true;
}
@ -68,21 +59,8 @@ void pokerGameUpdate(game_t *game) {
vnSceneRenderGui(&pokerGame->scene, &game->engine, &pokerGame->assets.shader);
pokerUiRender(pokerGame);
uint8_t i;
menuUpdate(&menu, &game->engine);
for(i = 0; i < menu.itemCount; i++) {
menuitem_t *item = menu.items + i;
frame.x = (float)item->x * (FRAME_BORDER_SIZE + FRAME_BORDER_SIZE);
frame.y = (float)item->y * (FRAME_BORDER_SIZE + FRAME_BORDER_SIZE);
if(menu.selected == i) frame.y -= FRAME_BORDER_SIZE;
frameSetSize(&frame,
(float)item->width * (FRAME_BORDER_SIZE + FRAME_BORDER_SIZE),
(float)item->height * (FRAME_BORDER_SIZE + FRAME_BORDER_SIZE)
);
frameRender(&frame, &pokerGame->assets.shader);
}
menuListUpdate(&ml, &game->engine);
menuListRender(&ml, &game->pokerGame.assets.shader);
}
void pokerGameDispose(game_t *game) {

View File

@ -17,8 +17,7 @@
#include "pokerworld.h"
#include "actions/start.h"
#include "../../ui/menu.h"
#include "../../ui/frame.h"
#include "../../ui/menulist.h"
/**
* Initializes the game state for the poker game.

34
src/ui/image.c Normal file
View File

@ -0,0 +1,34 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "image.h"
void imageInit(image_t *image, texture_t *texture) {
image->x = 0;
image->y = 0;
image->quad.verticeCount = -1;
imageSetTexture(image, texture);
}
void imageSetTexture(image_t *image, texture_t *texture) {
if(image->quad.verticeCount != -1) {
primitiveDispose(&image->quad);
image->quad.verticeCount = -1;
}
image->texture = texture;
quadInit(&image->quad, 0, 0,0,0,0, texture->width,texture->height,1,1);
}
void imageRender(image_t *image, shader_t *shader) {
shaderUsePosition(shader, image->x, image->y, 0, 0, 0, 0);
primitiveDraw(&image->quad, 0, -1);
}
void imageDispose(image_t *image) {
if(image->quad.verticeCount != -1) primitiveDispose(&image->quad);
}

43
src/ui/image.h Normal file
View File

@ -0,0 +1,43 @@
/**
* 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/primitive.h"
#include "../display/primitives/quad.h"
#include "../display/shader.h"
/**
* Initialize an image.
*
* @param image Image to initialize.
* @param texture Texture to use for initialization.
*/
void imageInit(image_t *image, texture_t *texture);
/**
* Set the texture for an image. This will also initialize the underlying quad.
*
* @param image Image to set the texture for.
* @param texture Texture to use.
*/
void imageSetTexture(image_t *image, texture_t *texture);
/**
* Render an image
*
* @param image Image to render.
* @param shader Shader to use while rendering.
*/
void imageRender(image_t *image, shader_t *shader);
/**
* Cleanup a previously initialized image.
*
* @param image Image to dispose.
*/
void imageDispose(image_t *image);

View File

@ -5,4 +5,83 @@
* https://opensource.org/licenses/MIT
*/
#include "menulist.h"
#include "menulist.h"
void menuListInit(menulist_t *ml, texture_t *texture) {
uint8_t i;
ml->x = 0;
ml->y = 0;
ml->count = 0;
frameInit(&ml->frame);
ml->frame.texture = texture;
}
uint8_t menuListAdd(menulist_t *ml, font_t *font, char *text) {
uint8_t i;
label_t *label;
menuitem_t *item;
// Get the variables.
i = ml->count;
label = ml->labels + i;
ml->items[i] = text;
// Initialize the label.
labelInit(label);
labelSetText(label, font, text);
// Reize the frame
frameSetInnerSize(
&ml->frame,
100,
FONT_LINE_HEIGHT * fontGetScale(label->fontSize) * (i+1)
);
// Add Menu Item
item = menuAdd(&ml->menu);
item->width = 1;
item->height = 1;
item->x = 0;
item->y = i;
ml->count++;
return i;
}
void menuListUpdate(menulist_t *ml, engine_t *engine) {
menuUpdate(&ml->menu, engine);
}
void menuListRender(menulist_t *ml, shader_t *shader) {
uint8_t i;
label_t *label;
// Render the frame
ml->frame.x = ml->x;
ml->frame.y = ml->y;
frameRender(&ml->frame, shader);
// Render selection box.
// Render each labels
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)
);
labelRender(label, shader);
}
}
void menuListDispose(menulist_t *ml) {
uint8_t i;
for(i = 0; i < ml->count; i++) {
labelDispose(ml->labels + i);
}
frameDispose(&ml->frame);
}

View File

@ -7,3 +7,16 @@
#pragma once
#include <dawn/dawn.h>
#include "label.h"
#include "menu.h"
#include "frame.h"
void menuListInit(menulist_t *ml, texture_t *texture);
uint8_t menuListAdd(menulist_t *ml, font_t *font, char *text);
void menuListUpdate(menulist_t *ml, engine_t *engine);
void menuListRender(menulist_t *ml, shader_t *shader);
void menuListDispose(menulist_t *ml);

View File

@ -7,7 +7,7 @@
#pragma once
#include <dawn/dawn.h>
#include "../gui/vntextbox.h"
#include "../ui/vntextbox.h"
#include "../../util/array.h"
#include "../../display/animation/timeline.h"
#include "../../display/animation/queue.h"

View File

@ -9,7 +9,7 @@
#include <dawn/dawn.h>
#include "vncharacter.h"
#include "conversation/vnconversation.h"
#include "gui/vntextbox.h"
#include "ui/vntextbox.h"
#include "../display/camera.h"
#include "../display/shader.h"