Hi Ruben,
I wanted to give you a new possibility with a little change to inventory.c. I added a new
occurrence to the slot click event in order to detect the mouse right button.
You need to add a new macro for the new
occurrence
#define INV_ITEM_RIGHT_CLICK 4 // 354th code line
The modified
slotOnClick function: the function added as event to every slot.
function slotOnClick(PANEL* clicked_panel)
{
Slot* clicked_slot = getSlotPtrForPanelPtr(clicked_panel);
if (clicked_slot == NULL) { diag("\n*** Error: panel not found\n"); }
if (global_floating_item_ptr->item == NULL) // player clicked on item in inventory while not floating an item
{
// 16/04/2014 Addition -----------------------------------------------------
if ( event_type == EVENT_CLICK ) // mouse left button event
{
// -------------------------------------------------------------------------
if (clicked_slot->item != NULL) // Player is grabbing an item out of the inventory
{
// optionally callback with action, bag_id, slot_id, item_id
if (on_click_callback_function_ptr)
on_click_callback_function_ptr(INV_ITEM_REMOVED,clicked_slot->bag_id,clicked_slot->id,0,clicked_slot->item->id);
// Create the floating panel
global_floating_item_ptr->floating_item_panel = pan_create("",floating_item_layer);
// Set the item and panel image of the floating item structure. This effectively "floats"
// the item that was stored in the inventory slot.
global_floating_item_ptr->item = clicked_slot->item;
global_floating_item_ptr->floating_item_panel->bmap = bmap_create(clicked_slot->item->floating_icon);
global_floating_item_ptr->floating_item_panel->flags |= VISIBLE;
// Set the item pointer of the clicked on slot to NULL
clicked_slot->item = NULL;
// Restore inventory slot image to be the default background image
clicked_slot->slot_panel->bmap = bmap_create(clicked_slot->background_image);
inv_floating_flag = 1;
_inv_do_float(global_floating_item_ptr);
}
// 16/04/2014 Addition -----------------------------------------------------
}
else if ( event_type == EVENT_RIGHTCLICK ) // mouse right button event
{
if (clicked_slot->item != NULL)
{
if (on_click_callback_function_ptr)
on_click_callback_function_ptr(INV_ITEM_RIGHT_CLICK,clicked_slot->bag_id,clicked_slot->id,clicked_slot->item->id,0);
}
}
// -------------------------------------------------------------------------
}
else
{
if ((clicked_slot->group_id == 0) || (clicked_slot->group_id == global_floating_item_ptr->item->group_id)) // Test to make sure that item can be placed in the slot
{
if (clicked_slot->item == NULL) // player is putting an item into an empty inventory slot
{
// optionally callback with action, bag_id, slot_id, item_id
if (on_click_callback_function_ptr) on_click_callback_function_ptr(INV_ITEM_PLACED,clicked_slot->bag_id,clicked_slot->id,global_floating_item_ptr->item->id,0);
// Place item in slot
clicked_slot->item = global_floating_item_ptr->item;
clicked_panel->bmap = bmap_create(global_floating_item_ptr->item->inventory_icon);
global_floating_item_ptr->item = NULL;
// End float
pan_remove(global_floating_item_ptr->floating_item_panel);
inv_floating_flag = 0;
}
else // player is swapping the floating item for the one in the inventory
{
// optionally callback with action, bag_id, slot_id, item_id
if (on_click_callback_function_ptr) on_click_callback_function_ptr(INV_ITEM_SWAPPED,clicked_slot->bag_id,clicked_slot->id,global_floating_item_ptr->item->id,clicked_slot->item->id);
// Backup the information of the clicked on slot
Item* clicked_slot_item_pointer = clicked_slot->item;
STRING* clicked_slot_inventory_icon = str_create(clicked_slot->item->inventory_icon);
// Set the slot to the floating item
clicked_slot->item = global_floating_item_ptr->item;
clicked_panel->bmap = bmap_create(global_floating_item_ptr->item->inventory_icon);
// Set the floating item to the slot
global_floating_item_ptr->item = clicked_slot_item_pointer;
global_floating_item_ptr->floating_item_panel->bmap = bmap_create(clicked_slot_inventory_icon);
}
}
}
}
Since every slot is a panel, they are not buttons, you must set enable_mouse to 2 in order to get the event call when you click with the right mouse button over a slot.
mouse_mode = 2; // or 4 or whatever you use
enable_mouse = 2;
At this point you should be able to detect the mouse right click over a slot inside your
slot_was_clicked function.
function slot_was_clicked ( int occurrence, int bag_id, int slot_id, int placed_item_id, int removed_item_id )
{
Bag *bagPanel;
if ( ( occurrence == INV_ITEM_REMOVED ) || ( occurrence == INV_ITEM_SWAPPED ) )
{
if (bag_id == MAIN_BAG_ID)
{
if (slot_id == ACTIV_WPN_SLOT_ID)
{
ent_remove ( active_weapon );
if(placed_item_id == NULL)
{
weapon_code = fist_code;
}
}
}
}
if ( ( occurrence == INV_ITEM_PLACED ) || ( occurrence == INV_ITEM_SWAPPED ) )
{
if (bag_id == MAIN_BAG_ID)
{
if (slot_id == ACTIV_WPN_SLOT_ID)
{
switch ( placed_item_id )
{
case SWORD: activate_sword(); break;
case MACE: activate_mace(); break;
default: weapon_code = fist_code; break;
}
}
}
}
if ( occurrence == INV_ITEM_RIGHT_CLICK )
{
snd_play ( sndExploDebug, 100, 0 );
}
}
This code let you hear the
sndExploDebug sound when you click over a filled slot and when you have not an item grabbed.
Imagine you want to drop an item at the player position when you click over a filled slot with the mouse right button. You would need to create a new entity in the scenery and clear the clicked slot. You know how to create a new entity but there is no function to clear a slot. I wrote it for you. Add it to inventory.c and use it into your
slot_was_clicked function.
function inv_clear_slot ( int bag_id, int slot_id )
{
Bag* bag;
int ii;
for ( ii=0; ii<inv_bag_count; ii++ )
{
bag = inv_bags_array[ii];
if ( bag->id != bag_id )
continue;
int i = 0;
for ( ; i<bag->_slot_count; i++ )
{
Slot *slot = bag->slots[i];
if ( slot->id != slot_id )
continue;
if ( slot->item == NULL )
continue;
if (on_click_callback_function_ptr)
on_click_callback_function_ptr(INV_ITEM_REMOVED,bag_id,slot_id,slot->item->id,slot->item->id);
slot->item = NULL;
if ( slot->slot_panel != NULL )
slot->slot_panel->bmap = bmap_create(slot->background_image);
}
}
}
This function gets slower the more bags exists but the item index management does not let me build it other way.
Hope it helps
Salud!