251 lines
8.5 KiB
C
251 lines
8.5 KiB
C
/******
|
|
******/
|
|
#include "state_modules.h"
|
|
#include "state_test.h"
|
|
#include "../../common/fio.h"
|
|
#include "../sdl/spritesheets.h" // FIXME: implicit declaration problems.
|
|
#include <unistd.h> // access()
|
|
|
|
/* allocate memory for menu */
|
|
void initModulesState() {
|
|
s_modules_elements = newElementList();
|
|
s_modules_elements->user = g_screen;
|
|
s_active_element = NULL;
|
|
|
|
if (access("modules", F_OK) != -1) {
|
|
strcpy(s_modules_dir, "modules");
|
|
} else if (access("../modules", F_OK) != -1) {
|
|
strcpy(s_modules_dir, "../modules");
|
|
}
|
|
|
|
struct Dimension dimen = { 0, 0, 128, 32};
|
|
struct Element *element;
|
|
struct LList *llist = g_modules_list;
|
|
int i = 0;
|
|
while(llist) {
|
|
element = newElement(E_TYPE_BUTTON, 0, g_r_setupElement, dimen);
|
|
setElementFont(element, g_font_large);
|
|
element->dimen.x = g_video_width/2 - (element->dimen.w/2);
|
|
element->sy = i * 0.25f;
|
|
setElementText(element, (char*)llist->data);
|
|
setElementEvent(element, E_EVENT_ACTIVE, &ModulesState_loadModule);
|
|
setElementKey(element, '1', 0);
|
|
addElementToList(s_modules_elements, element);
|
|
llist = llist->next;
|
|
i++;
|
|
}
|
|
element = newElement(E_TYPE_BUTTON, 0, g_r_setupElement, dimen);
|
|
element->sx = 0.05f;
|
|
element->sy = 1.5f;
|
|
setElementFont(element, g_font_large);
|
|
setElementText(element, "QUIT");
|
|
setElementKey(element, 'Q', 0);
|
|
setElementEvent(element, E_EVENT_ACTIVE, &stopRunning);
|
|
addElementToList(s_modules_elements, element);
|
|
|
|
g_freeState = freeModulesState;
|
|
g_handleState = handleModulesState;
|
|
g_processState = processModulesState;
|
|
g_renderState = renderModulesState;
|
|
}
|
|
|
|
/* free memory for menu */
|
|
void freeModulesState() {
|
|
freeElementList(s_modules_elements);
|
|
}
|
|
|
|
void processModulesState(int delta) {
|
|
}
|
|
|
|
void renderModulesState() {
|
|
g_renderSprite(menu_bg, g_screen, 0, 0, 0);
|
|
g_renderElements(s_modules_elements);
|
|
}
|
|
|
|
/* handles events */
|
|
void handleModulesState(struct TSEvent event) {
|
|
int x, y;
|
|
struct Element *current;
|
|
struct Element *next;
|
|
switch(event.type) {
|
|
case TS_KEYBOARD:
|
|
handleModulesKeyInput(event.keyboard);
|
|
break;
|
|
case TS_MOUSECLICK:
|
|
switch(event.mouseclick.state) {
|
|
case 0: // mouse up
|
|
if (s_active_element != NULL) {
|
|
x = getElementX(s_active_element);
|
|
y = getElementY(s_active_element);
|
|
if (x <= event.mouseclick.x && x+s_active_element->dimen.w >= event.mouseclick.x) {
|
|
if (y <= event.mouseclick.y && y+s_active_element->dimen.h >= event.mouseclick.y) {
|
|
printf("activate callback here :3\n");
|
|
if (s_active_element->onEvent[E_EVENT_ACTIVE] != NULL) {
|
|
s_active_element->onEvent[E_EVENT_ACTIVE](s_active_element);
|
|
}
|
|
}
|
|
}
|
|
if (s_active_element != NULL) {
|
|
s_active_element->state = E_STATE_NORMAL;
|
|
s_active_element->flags |= E_FLAG_UPDATE;
|
|
s_active_element = NULL;
|
|
}
|
|
}
|
|
break;
|
|
case 1: // mouse down
|
|
current = s_modules_elements->last;
|
|
while(current) {
|
|
next = current->prev;
|
|
x = getElementX(current);
|
|
y = getElementY(current);
|
|
if (x <= event.mouseclick.x && current->dimen.w+x >= event.mouseclick.x) {
|
|
if (y <= event.mouseclick.y && current->dimen.h+y >= event.mouseclick.y) {
|
|
current->state = E_STATE_ACTIVE;
|
|
current->flags |= E_FLAG_UPDATE;
|
|
s_active_element = current;
|
|
break;
|
|
}
|
|
}
|
|
current = next;
|
|
}
|
|
break;
|
|
}
|
|
printf("button: %d, state: %d, x: %d, y: %d\n", event.mouseclick.button, event.mouseclick.state, event.mouseclick.x, event.mouseclick.y);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void handleModulesKeyInput(struct TSEvent_Keyboard key_event) {
|
|
if (key_event.key.mod & KMOD_CTRL) {
|
|
if (key_event.key.sym == 113)
|
|
g_running = 0;
|
|
}
|
|
if (key_event.key.mod & KMOD_SHIFT) {
|
|
printf("shift is held\n");
|
|
}
|
|
// TODO: if not in some sort of text area
|
|
// TODO: we need to operate on the top-level of elements so shortcuts don't interfere with eachother
|
|
// TODO: elements should have multiple shortcut keys
|
|
struct Element *current = s_modules_elements->first;
|
|
while(current != NULL) {
|
|
struct Element *next = current->next;
|
|
if (current->key == key_event.key.unicode) {
|
|
if (current->onEvent[E_EVENT_ACTIVE] != NULL) {
|
|
current->onEvent[E_EVENT_ACTIVE](current);
|
|
}
|
|
break;
|
|
}
|
|
current = next;
|
|
}
|
|
|
|
printf("state is %d, scan is %d, sym is %d(%c), and unicode is %d(%c)\n", key_event.state, key_event.key.scancode, key_event.key.sym, key_event.key.sym, key_event.key.unicode, key_event.key.unicode);
|
|
};
|
|
|
|
void ModulesState_loadModule(struct Element *element) {
|
|
char *buffer = NULL;
|
|
char *module_name = getElementText(element);
|
|
char temp[255]; // used to store module path + item to load
|
|
int size = 0;
|
|
|
|
/* load module/CONF */
|
|
sprintf(temp, "%s/%s/CONF", s_modules_dir, module_name);
|
|
printf("attempting to load %s\n", temp);
|
|
if (g_module_conf == NULL) {
|
|
g_module_conf = newTable(16);
|
|
}
|
|
if ((size = fileToMemory(&buffer, temp)) >= 0) {
|
|
int offset = 0;
|
|
loadConfig_r(g_module_conf, buffer, size, &offset);
|
|
} else {
|
|
printf("ERR: could not load module's CONF!\n");
|
|
free(buffer);
|
|
return;
|
|
}
|
|
free(buffer);
|
|
printf("%s loaded!\n", getTablePairValueString(g_module_conf, "name"));
|
|
|
|
float scale_x = getTablePairValueFloat(g_module_conf, "scale_x");
|
|
float scale_y = getTablePairValueFloat(g_module_conf, "scale_y");
|
|
|
|
struct Table *sprites_table = getTablePairValueTable(g_module_conf, "sprites");
|
|
if (sprites_table == NULL) {
|
|
printf("WARN: module does not define sprites!\n");
|
|
} else {
|
|
struct Table *sprites_conf = NULL;
|
|
// Load our spritesheet CONF
|
|
sprintf(temp, "%s/%s/sprites/CONF", s_modules_dir, module_name);
|
|
printf("attempting to load %s\n", temp);
|
|
if (sprites_conf == NULL) {
|
|
sprites_conf = newTable(16);
|
|
}
|
|
if ((size = fileToMemory(&buffer, temp)) >= 0) {
|
|
int offset = 0;
|
|
loadConfig_r(sprites_conf, buffer, size, &offset);
|
|
} else {
|
|
printf("ERR: could not load spritesheet CONF!\n");
|
|
free(buffer);
|
|
return;
|
|
}
|
|
free(buffer);
|
|
|
|
// continue on!
|
|
int i = 0;
|
|
// manually roll through the sprites Table since we expect it to be an array.
|
|
struct TablePair *pair = sprites_table->pair[i];
|
|
while(pair != NULL) {
|
|
// bail at first non-STRING value
|
|
if (pair->type != T_STRING) {
|
|
}
|
|
i++;
|
|
pair = sprites_table->pair[i];
|
|
}
|
|
// i should be equal to spritesheet count
|
|
g_module_spritesheets = malloc(sizeof(struct Spritesheet*)*i);
|
|
while(i > 0) {
|
|
sprintf(temp, "%s/%s/sprites/%s", s_modules_dir, module_name, (char*)sprites_table->pair[i-1]->value);
|
|
printf(" attempting to load %s\n", temp);
|
|
g_module_spritesheets[i-1] = newSpritesheet();
|
|
struct Table *sheet_conf = getTablePairValueTable(sprites_conf, (char*)sprites_table->pair[i-1]->value);
|
|
int sheet_width = 16;
|
|
int sheet_height = 32;
|
|
int sheet_columns = 8;
|
|
if (sheet_conf == NULL) {
|
|
printf(" ERR, width, height, and columns undefined for spritesheet!\n");
|
|
} else {
|
|
sheet_width = getTablePairValueInt(sheet_conf, "width");
|
|
sheet_height = getTablePairValueInt(sheet_conf, "height");
|
|
sheet_columns = getTablePairValueInt(sheet_conf, "columns");
|
|
}
|
|
// lol, since Spritesheet is interface specific, we do not include ".png" - that is handled by the Spritesheet load function itself.
|
|
//if (loadSpritesheetFromFile(g_module_spritesheets[i-1], temp, sheet_width, sheet_height, sheet_columns) == 0) {
|
|
if (g_loadSpritesheet(g_module_spritesheets[i-1], temp, sheet_width, sheet_height, sheet_columns) == 0) {
|
|
setSpritesheetScale(g_module_spritesheets[i-1], scale_x, scale_y);
|
|
} else {
|
|
printf(" ERR on loading spritesheet!\n");
|
|
}
|
|
i--;
|
|
}
|
|
}
|
|
|
|
sprintf(temp, "%s/%s/%s.tsd", s_modules_dir, module_name, module_name);
|
|
printf("attempting to load %s\n", temp);
|
|
size = fileToMemory(&buffer, temp);
|
|
if (size > 0) {
|
|
if (g_tile_data != NULL) {
|
|
freeData(g_tile_data);
|
|
g_tile_data = loadDataFromMemory(buffer, size);
|
|
} else {
|
|
g_tile_data = loadDataFromMemory(buffer, size);
|
|
}
|
|
printf("loaded :)\n");
|
|
|
|
g_switch_state = 1;
|
|
g_initState = initTestState;
|
|
} else {
|
|
printf("COULD NOT LOAD :(\n");
|
|
}
|
|
free(buffer);
|
|
}
|