# HG changeset patch # User David Demelier # Date 1602921706 -7200 # Node ID 22141b6b2e4308669bbdf6dd90e771ffb9ed84e6 # Parent e05a792f69107b04db7dedd1c95c7fb04a8ede21 rpg: remove inventory_dialog for now diff -r e05a792f6910 -r 22141b6b2e43 examples/CMakeLists.txt --- a/examples/CMakeLists.txt Fri Oct 16 18:44:05 2020 +0200 +++ b/examples/CMakeLists.txt Sat Oct 17 10:01:46 2020 +0200 @@ -43,17 +43,6 @@ ) molko_define_executable( - TARGET example-inventory - SOURCES example-inventory.c - ASSETS - ${examples_SOURCE_DIR}/assets/images/fish.png - ${examples_SOURCE_DIR}/assets/images/potion.png - ${examples_SOURCE_DIR}/assets/images/sword.png - FOLDER examples - LIBRARIES librpg -) - -molko_define_executable( TARGET example-label SOURCES example-label.c LIBRARIES libui diff -r e05a792f6910 -r 22141b6b2e43 examples/assets/images/fish.png Binary file examples/assets/images/fish.png has changed diff -r e05a792f6910 -r 22141b6b2e43 examples/assets/images/potion.png Binary file examples/assets/images/potion.png has changed diff -r e05a792f6910 -r 22141b6b2e43 examples/assets/images/sword.png Binary file examples/assets/images/sword.png has changed diff -r e05a792f6910 -r 22141b6b2e43 librpg/CMakeLists.txt --- a/librpg/CMakeLists.txt Fri Oct 16 18:44:05 2020 +0200 +++ b/librpg/CMakeLists.txt Sat Oct 17 10:01:46 2020 +0200 @@ -22,8 +22,6 @@ SOURCES ${librpg_SOURCE_DIR}/rpg/inventory.c ${librpg_SOURCE_DIR}/rpg/inventory.h - ${librpg_SOURCE_DIR}/rpg/inventory_dialog.c - ${librpg_SOURCE_DIR}/rpg/inventory_dialog.h ${librpg_SOURCE_DIR}/rpg/item.h ${librpg_SOURCE_DIR}/rpg/map.c ${librpg_SOURCE_DIR}/rpg/map.h diff -r e05a792f6910 -r 22141b6b2e43 librpg/rpg/inventory_dialog.c --- a/librpg/rpg/inventory_dialog.c Fri Oct 16 18:44:05 2020 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,321 +0,0 @@ -/* - * inventory_dialog.h -- dialog for items - * - * Copyright (c) 2020 David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "inventory.h" -#include "inventory_dialog.h" -#include "item.h" - -#define ITEM_SIZE 64 -#define ITEM_PADDING 32 - -#define GRID_WIDTH ((INVENTORY_COLS_MAX * ITEM_SIZE) + \ - ((INVENTORY_COLS_MAX + 1) * ITEM_PADDING)) -#define GRID_HEIGHT ((INVENTORY_ROWS_MAX * ITEM_SIZE) + \ - ((INVENTORY_ROWS_MAX + 1) * ITEM_PADDING)) - -#define LABEL_WIDTH (GRID_WIDTH) -#define LABEL_HEIGHT (64) - -#define BUTTON_HEIGHT (32) -#define BUTTON_WIDTH ((GRID_WIDTH) / 4) - -/* - * The frame looks like this: - * - * +----------------------------+ - * | [] [] [] [] [] [] [] [] [] | - * | [] [] [] [] [] [] [] [] [] | - * | [] [] [] [] [] [] [] [] [] | - * +----------------------------+ - * | Item name | - * | Item description | - * +----------------------------+ - * [ Sort ] - * - * Where space between cells is determined with ITEM_PADDING macro. - */ - -static unsigned int -total_width(void) -{ - return GRID_WIDTH; -} - -static unsigned int -total_height(void) -{ - return GRID_HEIGHT + LABEL_HEIGHT + BUTTON_HEIGHT; -} - -static void -compute_box_position(const struct inventory_dialog *dlg, int r, int c, int *x, int *y) -{ - assert(dlg); - assert(x); - assert(y); - - *x = dlg->fgrid.x + ((c * ITEM_SIZE) + ((c + 1) * ITEM_PADDING)); - *y = dlg->fgrid.y + ((r * ITEM_SIZE) + ((r + 1) * ITEM_PADDING)); -} - -static void -draw_grid_item_frame(int x, int y) -{ - struct frame frame = { - .x = x, - .y = y, - .w = ITEM_SIZE, - .h = ITEM_SIZE, - .style = FRAME_STYLE_BOX - }; - - frame_draw(&frame); -} - -static void -draw_grid_item_icon(struct item *item, int x, int y) -{ - struct texture *icon = item->icon; - - texture_scale(icon, 0, 0, icon->w, icon->h, x, y, ITEM_SIZE, ITEM_SIZE, 0.0); -} - -static void -draw_grid_item_amount(struct inventory_slot *slot, int x, int y) -{ - char nstr[16]; - struct label label = { - .text = nstr, - .x = x, - .y = y - }; - - /* Draw the number of item in this slot. */ - snprintf(nstr, sizeof (nstr), "%d", slot->amount); - label_draw(&label); -} - -static void -draw_grid_item(struct inventory_slot *slot, int x, int y, bool selected) -{ - draw_grid_item_frame(x, y); - - if (slot->item) { - draw_grid_item_icon(slot->item, x, y); - draw_grid_item_amount(slot, x, y); - } - - if (selected) { - x -= 16; - y += (ITEM_SIZE / 2) - 4; - painter_draw_circle(x, y, 8); - } -} - -static void -draw_grid_items(const struct inventory_dialog *dlg) -{ - int x, y; - bool selected; - - for (unsigned int r = 0; r < INVENTORY_ROWS_MAX; ++r) { - for (unsigned int c = 0; c < INVENTORY_COLS_MAX; ++c) { - selected = r == dlg->selrow && c == dlg->selcol; - compute_box_position(dlg, r, c, &x, &y); - draw_grid_item(&dlg->inv->items[r][c], x, y, selected); - } - } -} - -static void -draw_label(struct inventory_dialog *dlg) -{ - assert(dlg); - - struct item *item = dlg->inv->items[dlg->selrow][dlg->selcol].item; - - frame_draw(&dlg->fname); - frame_draw(&dlg->fdesc); - - if (item) { - dlg->lname.text = item->name; - dlg->ldesc.text = item->summary; - label_draw(&dlg->lname); - label_draw(&dlg->ldesc); - } -} - -void -inventory_dialog_open(struct inventory_dialog *dlg) -{ - assert(dlg); - assert(dlg->inv); - - int tw, th; - - dlg->state = INVENTORY_DIALOG_SHOWING; - - tw = total_width(); - th = total_height(); - - /* Grid frame position. */ - dlg->fgrid.w = GRID_WIDTH; - dlg->fgrid.h = GRID_HEIGHT; - dlg->fgrid.x = dlg->x; - dlg->fgrid.y = dlg->y; - - /* Name label. */ - dlg->fname.w = dlg->lname.w = LABEL_WIDTH; - dlg->fname.h = dlg->lname.h = LABEL_HEIGHT / 2; - dlg->fname.x = dlg->lname.x = dlg->x; - dlg->fname.y = dlg->lname.y = dlg->y + GRID_HEIGHT; - dlg->lname.x += ITEM_PADDING; - - /* Description label. */ - dlg->fdesc.w = dlg->ldesc.w = LABEL_WIDTH; - dlg->fdesc.h = dlg->ldesc.h = LABEL_HEIGHT / 2; - dlg->fdesc.x = dlg->ldesc.x = dlg->y; - dlg->fdesc.y = dlg->ldesc.y = dlg->y + GRID_HEIGHT + (LABEL_HEIGHT / 2); - dlg->ldesc.x += ITEM_PADDING; - - /* Button sort. */ - dlg->bsort.x = dlg->x; - dlg->bsort.y = dlg->y + GRID_HEIGHT + LABEL_HEIGHT; - dlg->bsort.w = BUTTON_WIDTH; - dlg->bsort.h = BUTTON_HEIGHT; - dlg->bsort.text = "Sort"; -} - -static void -handle_keydown(struct inventory_dialog *dlg, const struct event_key *ev) -{ - assert(ev && ev->type == EVENT_KEYDOWN); - - switch (ev->key) { - case KEY_LEFT: - if (dlg->selcol == 0) - dlg->selcol = INVENTORY_COLS_MAX - 1; - else - dlg->selcol--; - break; - case KEY_RIGHT: - dlg->selcol = (dlg->selcol + 1) % INVENTORY_COLS_MAX; - break; - case KEY_UP: - if (dlg->selrow == 0) - dlg->selrow = INVENTORY_ROWS_MAX - 1; - else - dlg->selrow--; - break; - case KEY_DOWN: - dlg->selrow = (dlg->selrow + 1) % INVENTORY_ROWS_MAX; - break; - default: - break; - } -} - -static void -handle_clickdown(struct inventory_dialog *dlg, const struct event_click *ev) -{ - assert(dlg); - assert(ev && ev->type == EVENT_CLICKDOWN); - - int x, y; - - for (int r = 0; r < INVENTORY_ROWS_MAX; ++r) { - for (int c = 0; c < INVENTORY_COLS_MAX; ++c) { - compute_box_position(dlg, r, c, &x, &y); - - if (maths_is_boxed(x, y, ITEM_SIZE, ITEM_SIZE, ev->x, ev->y)) { - dlg->selrow = r; - dlg->selcol = c; - } - } - } -} - -void -inventory_dialog_handle(struct inventory_dialog *dlg, const union event *event) -{ - assert(event); - - switch (event->type) { - case EVENT_KEYDOWN: - handle_keydown(dlg, &event->key); - break; - case EVENT_CLICKDOWN: - handle_clickdown(dlg, &event->click); - break; - default: - break; - } - - button_handle(&dlg->bsort, event); - - if (dlg->bsort.state == BUTTON_STATE_ACTIVATED) { - inventory_sort(dlg->inv); - button_reset(&dlg->bsort); - } -} - -void -inventory_dialog_update(struct inventory_dialog *dlg, unsigned int ticks) -{ - assert(dlg); - - (void)ticks; -} - -void -inventory_dialog_move(struct inventory_dialog *dlg, int x, int y) -{ - assert(dlg); - - dlg->x = x; - dlg->y = y; -} - -void -inventory_dialog_draw(struct inventory_dialog *dlg) -{ - assert(dlg); - - frame_draw(&dlg->fgrid); - draw_grid_items(dlg); - draw_label(dlg); - button_draw(&dlg->bsort); -} - -void -inventory_dialog_finish(struct inventory_dialog *dlg) -{ - assert(dlg); - - dlg->state = INVENTORY_DIALOG_NONE; -} diff -r e05a792f6910 -r 22141b6b2e43 librpg/rpg/inventory_dialog.h --- a/librpg/rpg/inventory_dialog.h Fri Oct 16 18:44:05 2020 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* - * inventory_dialog.h -- dialog for items - * - * Copyright (c) 2020 David Demelier - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef INVENTORY_DIALOG_H -#define INVENTORY_DIALOG_H - -#include -#include -#include - -union event; - -struct inventory; -struct theme; - -/** - * \brief Inventory dialog state. - */ -enum inventory_dialog_state { - INVENTORY_DIALOG_NONE, - INVENTORY_DIALOG_SHOWING -}; - -/** - * \brief Inventory dialog. - */ -struct inventory_dialog { - int x; /*!< (+) Position in x. */ - int y; /*!< (+) Position in y. */ - struct inventory *inv; /*!< (+&) Inventory to use. */ - struct theme *theme; /*!< (+&?) Theme to use. */ - struct button bsort; /*!< (-) Button sort. */ - struct frame fgrid; /*!< (-) Grid frame. */ - struct frame fname; /*!< (-) Frame for name. */ - struct frame fdesc; /*!< (-) Frame for description. */ - struct label lname; /*!< (-) Label for name. */ - struct label ldesc; /*!< (-) Label for description. */ - enum inventory_dialog_state state; /*!< (-) Current dialog state. */ - unsigned int selrow; /*!< (-) Current selected row. */ - unsigned int selcol; /*!< (-) Current selected column. */ -}; - -void -inventory_dialog_open(struct inventory_dialog *dlg); - -void -inventory_dialog_handle(struct inventory_dialog *dlg, const union event *event); - -void -inventory_dialog_update(struct inventory_dialog *dlg, unsigned int ticks); - -void -inventory_dialog_move(struct inventory_dialog *dlg, int x, int y); - -void -inventory_dialog_draw(struct inventory_dialog *dlg); - -void -inventory_dialog_finish(struct inventory_dialog *dlg); - -#endif /* !INVENTORY_DIALOG_H */