changed the inventory bar to use the new custom gui system

inventory bar is now at bottom of screen
Controls
David Dormagen 2013-05-25 21:28:33 +02:00
parent 6681a2cd7d
commit 6fe8b1719e
11 changed files with 313 additions and 208 deletions

View File

@ -0,0 +1,4 @@
[DefCore]
id=GUI_Controller_InventoryBar
Version=5,2,0,1
Category=C4D_StaticBack | C4D_Environment

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 B

View File

@ -0,0 +1,274 @@
/**
ControllerInventoryBar
Controlls the inventory bar interaction and layout.
@author Zapper
*/
/*
inventory_slot contains an array of proplists with the following attributes:
ID: submenu ID. Unique in combination with the target == GetInventoryGuiTarget
obj: last object that was shown here
hand: bool, whether select with a hand
*/
static const GUI_Controller_InventoryBar_IconMarginScreenBottom = 5; // margin from left border of screen
static const GUI_Controller_InventoryBar_IconSize = 64;
static const GUI_Controller_InventoryBar_IconMargin = 20;
local inventory_slots;
local inventory_gui_target;
local inventory_gui_id;
local progress_bar_links;
func GetCurrentGuiID()
{
if (inventory_gui_id) return inventory_gui_id;
var menu =
{
Target = GetInventoryGuiTarget(),
Style = GUI_Multiple | GUI_IgnoreMouse | GUI_NoCrop,
Y = [1000, -(GUI_Controller_InventoryBar_IconMarginScreenBottom + GUI_Controller_InventoryBar_IconSize)],
OnClose = GuiAction_Call(this, "OnGuiClose")
};
inventory_gui_id = CustomGuiOpen(menu);
return inventory_gui_id;
}
func OnGuiClose()
{
inventory_gui_id = nil;
}
// this function returns a dummy object that is used as the custom GUI target by the inventory menu
func GetInventoryGuiTarget()
{
if (inventory_gui_target)
return inventory_gui_target;
inventory_gui_target = CreateObject(Dummy, AbsX(0), AbsY(0), GetOwner());
inventory_gui_target.Visibility = VIS_Owner;
return inventory_gui_target;
}
func Construction()
{
inventory_gui_target = nil;
progress_bar_links = [];
inventory_slots = [];
return _inherited(...);
}
func Destruction()
{
if (inventory_gui_target)
inventory_gui_target->RemoveObject();
var menu = GetCurrentGuiID();
if (menu)
{
CustomGuiClose(menu, nil, this);
}
return _inherited(...);
}
func OnClonkRecruitment(object clonk, int plr)
{
ScheduleUpdateInventory();
return _inherited(clonk, plr, ...);
}
public func OnCrewDisabled(object clonk)
{
ScheduleUpdateInventory();
return _inherited(clonk, ...);
}
public func OnCrewSelection(object clonk, bool deselect)
{
ScheduleUpdateInventory();
return _inherited(clonk, deselect, ...);
}
// call from HUDAdapter or inventory-buttons
public func OnHandSelectionChange(int old, int new, int handslot)
{
CustomGuiSetTag("Std", GetCurrentGuiID(), inventory_slots[old].ID + 1000, GetInventoryGuiTarget());
CustomGuiSetTag("Selected", GetCurrentGuiID(), inventory_slots[new].ID + 1000, GetInventoryGuiTarget());
OnSlotObjectChanged(handslot);
return _inherited(old, new, handslot, ...);
}
// call from HUDAdapter (Clonk)
public func OnSlotObjectChanged(int slot)
{
// refresh inventory
ScheduleUpdateInventory();
return _inherited(slot, ...);
}
// Updates the Inventory in 1 frame
func ScheduleUpdateInventory()
{
if (!GetEffect("UpdateInventory", this))
AddEffect("UpdateInventory", this, 1, 1, this);
}
func FxUpdateInventoryTimer()
{
UpdateInventory();
return -1;
}
/* Inventory stuff */
func UpdateInventory()
{
// only display if we have a clonk
var clonk = GetCursor(GetOwner());
if(!clonk)
{
GetInventoryGuiTarget().Visibility = VIS_None;
return;
}
GetInventoryGuiTarget().Visibility = VIS_Owner;
UpdateInventoryButtons(clonk);
// sort out old progress bars
if(GetLength(progress_bar_links))
{
var old_progress_bar_links = progress_bar_links[:];
progress_bar_links = [];
for(var bar in old_progress_bar_links)
{
if(!bar.effect) continue;
PushBack(progress_bar_links, bar);
}
}
// update inventory-slots
var hand_item_pos = clonk->GetHandItemPos(0);
for (var slot_info in inventory_slots)
{
var item = clonk->GetItem(slot_info.slot);
var needs_selection = hand_item_pos == slot_info.slot;
if ((item != slot_info.obj) || (needs_selection != slot_info.hand))
{
var update = { Symbol = item };
CustomGuiUpdate(update, GetCurrentGuiID(), 1000 + slot_info.ID, GetInventoryGuiTarget());
var tag = "Std";
if (needs_selection) tag = "Selected";
CustomGuiSetTag(tag, GetCurrentGuiID(), slot_info.ID, GetInventoryGuiTarget());
slot_info.hand = needs_selection;
}
//inventory[i]->ClearProgressBarLink();
// re-add progress bar if possible
for(var bar in progress_bar_links)
{
if(bar.obj != item) continue;
//inventory[i]->SetProgressBarLink(bar.effect);
break;
}
}
}
// sets the link of the progress bar for a certain slot
// the link is an effect that has the properties "max" and "current"
func SetProgressBarLinkForObject(object what, proplist e)
{
PushBack(progress_bar_links, {obj = what, effect = e});
ScheduleUpdateInventory();
}
func CalculateButtonPosition(int slot_number, int max_slots)
{
var pos_x_offset = -((GUI_Controller_InventoryBar_IconSize + GUI_Controller_InventoryBar_IconMargin) * max_slots - GUI_Controller_InventoryBar_IconMargin) / 2;
var pos_x = pos_x_offset + (GUI_Controller_InventoryBar_IconSize + GUI_Controller_InventoryBar_IconMargin) * slot_number;
var pos_y = - (GUI_Controller_InventoryBar_IconMarginScreenBottom + GUI_Controller_InventoryBar_IconSize);
var pos =
{
X = [500, pos_x], Y = [1000, pos_y],
Wdt = [500, pos_x + GUI_Controller_InventoryBar_IconSize],
Hgt = [1000, pos_y + GUI_Controller_InventoryBar_IconSize],
};
return pos;
}
// Insert an inventory slot into the inventory-bar
func CreateNewInventoryButton(int max_slots)
{
var slot_number = GetLength(inventory_slots);
var slot_info =
{
slot = slot_number,
ID = slot_number + 1,
hand = false,
obj = nil
};
PushBack(inventory_slots, slot_info);
// the gui already exists, only update it with a new submenu
var pos = CalculateButtonPosition(slot_number, max_slots);
var icon =
{
Target = GetInventoryGuiTarget(),
Style = GUI_NoCrop,
ID = slot_info.ID,
Symbol = {Std = Icon_Menu_Circle, Selected = Icon_Menu_CircleHighlight},
X = [pos.X[0], {Std = pos.X[1], Selected = pos.X[1] - 16}],
Y = [pos.Y[0], {Std = pos.Y[1], Selected = pos.Y[1] - 16}],
Wdt = [pos.Wdt[0], {Std = pos.Wdt[1], Selected = pos.Wdt[1] + 16}],
Hgt = [pos.Hgt[0], {Std = pos.Hgt[1], Selected = pos.Hgt[1] + 16}],
Text = Format("%2d", slot_info.slot + 1),
icon =
{
Target = GetInventoryGuiTarget(),
ID = 1000 + slot_info.ID,
X = [0, {Std = 0, Selected = -16}],
Y = [0, {Std = 0, Selected = -16}],
Wdt = [1000, {Std = 0, Selected = 16}],
Hgt = [1000, {Std = 0, Selected = 16}]
}
};
CustomGuiUpdate({new_icon = icon}, GetCurrentGuiID(), 0);
//return bt;
}
// sets the inventory size to the currently selected clonk
private func UpdateInventoryButtons(object clonk)
{
var max_contents_count = clonk->~MaxContentsCount();
var old_count = GetLength(inventory_slots);
// need to create more inventory buttons?
while (max_contents_count > GetLength(inventory_slots))
CreateNewInventoryButton(max_contents_count);
// need to remove some inventory buttons?
while (max_contents_count < GetLength(inventory_slots))
{
var slot_info = inventory_slots[-1];
CustomGuiClose(GetCurrentGuiID(), slot_info.ID, GetInventoryGuiTarget());
SetLength(inventory_slots, GetLength(inventory_slots)-1);
}
// modifications occured? Adjust position of old slots
if (old_count != max_contents_count)
{
var gui_id = GetCurrentGuiID();
var gui_target = GetInventoryGuiTarget();
for (var i = 0; i < old_count; ++i)
{
var slot_info = inventory_slots[i];
var update = CalculateButtonPosition(i, max_contents_count);
CustomGuiUpdate(update, gui_id, slot_info.ID, gui_target);
}
}
}

View File

@ -15,14 +15,13 @@
@authors Newton, Mimmo_O
*/
#include GUI_Controller_InventoryBar
// Local variables containing the GUI-Elements
local actionbar; // Array, action-buttons at the bottom
local inventory; // Array, inventory-buttons at the left side
local markers; // Array, the gui-markers.
local carryheavy; // Object, optional inventory-button only shown when clonk is carrying a carry-heavy object
local wealth; // Object, displays wealth of the player
local progress_bar_links;
static const GUI_MAX_ACTIONBAR = 10; // maximum amount of actionbar-slots
@ -30,8 +29,6 @@ protected func Construction()
{
actionbar = [];
markers = [];
inventory = [];
progress_bar_links = [];
// find all clonks of this crew which do not have a selector yet (and can have one)
for(var i=0; i < GetCrewCount(GetOwner()); ++i)
@ -53,8 +50,7 @@ protected func Construction()
wealth->SetPosition(-16-GUI_Wealth->GetDefHeight()/2,8+GUI_Wealth->GetDefHeight()/2);
wealth->Update();
// inventory display
MakeInventory();
return _inherited();
}
/* Destruction */
@ -73,13 +69,6 @@ protected func Destruction()
actionbar[i]->RemoveObject();
}
}
if(inventory)
for(var i=0; i<GetLength(inventory); ++i)
{
if(inventory[i])
inventory[i]->RemoveObject();
}
if(markers)
{
@ -97,32 +86,10 @@ protected func Destruction()
var crew = FindObjects(Find_ID(GUI_CrewSelector), Find_Owner(GetOwner()));
for(var o in crew)
o->RemoveObject();
return _inherited();
}
/* Inventory-Bar stuff */
// Updates the Inventory in 1 frame
private func ScheduleUpdateInventory()
{
ScheduleCall(this, "UpdateInventory", 1, 0);
}
private func MakeInventory()
{
// distance between slots
var d = 72;
// upper barrier
var y = 200;
// and the carry heavy slot
var bt = CreateObject(GUI_Backpack_Slot_Icon,0,0,GetOwner());
bt->SetHUDController(this);
bt->SetPosition(40+d, y);
bt->SetSlotId(-1);
bt.Visibility = VIS_None;
carryheavy = bt;
}
/*-- Wealth --*/
@ -185,82 +152,6 @@ global func AddHUDMarker(int player, picture, string altpicture, string text, in
return hud.markers[number];
}
/* Inventory stuff */
func UpdateInventory()
{
// only display if we have a clonk
var c = GetCursor(GetOwner());
if(!c) return 1;
// sort out old progress bars
if(GetLength(progress_bar_links))
{
var old_progress_bar_links = progress_bar_links[:];
progress_bar_links = [];
for(var bar in old_progress_bar_links)
{
if(!bar.effect) continue;
PushBack(progress_bar_links, bar);
}
}
// update inventory-slots
for(var i=0; i<GetLength(inventory); i++)
{
var item = c->GetItem(inventory[i]->GetSlotId());
inventory[i]->SetSymbol(item);
inventory[i]->SetUnselected();
inventory[i]->ClearProgressBarLink();
// re-add progress bar if possible
for(var bar in progress_bar_links)
{
if(bar.obj != item) continue;
inventory[i]->SetProgressBarLink(bar.effect);
break;
}
}
// update hand-indicator
if(c->IsCarryingHeavy())
{
carryheavy->SetSelected(-1);
}
else
for(var i=0; i < c->HandObjects(); ++i)
{
var handpos = c->GetHandItemPos(i);
if(inventory[handpos])
inventory[handpos]->SetSelected(i);
}
}
// sets the link of the progress bar for a certain slot
// the link is an effect that has the properties "max" and "current"
func SetProgressBarLinkForObject(object what, proplist e)
{
PushBack(progress_bar_links, {obj = what, effect = e});
ScheduleUpdateInventory();
}
// Shows the Carryheavy-Inventoryslot if obj is set
// Removes it if it's nil
func OnCarryHeavyChange(object obj)
{
carryheavy->SetSymbol(obj);
if(obj == nil)
{
carryheavy->SetUnselected();
carryheavy.Visibility = VIS_None;
}
else
this.Visibility = VIS_Owner;
UpdateInventory();
}
/* Hotkey Control */
// executes the mouseclick onto an actionbutton through hotkeys
@ -283,12 +174,7 @@ public func ControlHotkey(int hotindex)
// insert new clonk into crew-selectors on recruitment
protected func OnClonkRecruitment(object clonk, int plr)
{
// not my business
if(plr != GetOwner()) return;
if(!(clonk->HUDAdapter())) return;
{
// not enabled
if(!clonk->GetCrewEnabled()) return;
@ -313,24 +199,16 @@ protected func OnClonkRecruitment(object clonk, int plr)
// reorder the crew selectors
ReorderCrewSelectors();
// update
ScheduleUpdateInventory();
return _inherited(clonk, plr, ...);
}
protected func OnClonkDeRecruitment(object clonk, int plr)
{
// not my business
if(plr != GetOwner()) return;
if(!(clonk->HUDAdapter())) return;
OnCrewDisabled(clonk);
}
protected func OnClonkDeath(object clonk, int killer)
{
if(clonk->GetController() != GetOwner()) return;
if(!(clonk->~HUDAdapter())) return;
OnCrewDisabled(clonk);
}
@ -339,9 +217,6 @@ protected func OnClonkDeath(object clonk, int killer)
// called from engine on player eliminated
public func RemovePlayer(int plr, int team)
{
// not my business
if(plr != GetOwner()) return;
// at this point, we can assume that all crewmembers have been
// removed already. Whats left to do is to remove this object,
// the lower hud and the upper right hud
@ -358,15 +233,15 @@ public func OnCrewDisabled(object clonk)
selector->CrewGone();
ReorderCrewSelectors(clonk);
}
// update
ScheduleUpdateInventory();
return _inherited(clonk, ...);
}
public func OnCrewEnabled(object clonk)
{
CreateSelectorFor(clonk);
ReorderCrewSelectors();
return _inherited(clonk, ...);
}
@ -378,9 +253,6 @@ public func OnCrewSelection(object clonk, bool deselect)
{
// and start effect to monitor vehicles and structures...
AddEffect("IntSearchInteractionObjects",clonk,1,10,this,nil,0);
// clear inventory buttons
UpdateInventoryButtons(clonk);
}
else
{
@ -390,10 +262,8 @@ public func OnCrewSelection(object clonk, bool deselect)
// clear actionbuttons
ClearActionButtons();
// update
ScheduleUpdateInventory();
OnCarryHeavyChange(clonk->~GetCarryHeavy());
return _inherited(clonk, deselect, ...);
}
public func FxIntSearchInteractionObjectsEffect(string newname, object target)
@ -494,70 +364,9 @@ public func OnSelectionChanged(int old, int new)
actionbar[new]->UpdateSelectionStatus();
}
// call from HUDAdapter or inventory-buttons
public func OnHandSelectionChange(int old, int new, int handslot)
{
if(inventory[old])
inventory[old]->SetUnselected();
if(inventory[new])
inventory[new]->SetSelected(handslot);
OnSlotObjectChanged(handslot);
}
// call from HUDAdapter (Clonk)
public func OnSlotObjectChanged(int slot)
{
// refresh inventory
ScheduleUpdateInventory();
}
/** Helper Functions **/
// Insert an inventory slot into the inventory-bar
private func InventoryButton()
{
// distance between slots
var d = 72;
// upper barrier
var y = 200;
// index
var i = GetLength(inventory);
// create inventory slots
var bt = CreateObject(GUI_Backpack_Slot_Icon,0,0,GetOwner());
bt->SetHUDController(this);
bt->SetPosition(40, y + d*i);
bt->SetSlotId(i);
CustomMessage(Format("@%d.", i+1), bt, nil, -40, 54);
inventory[i] = bt;
return bt;
}
// sets the inventory size to the currently selected clonk
private func UpdateInventoryButtons(object clonk)
{
if(!clonk) return;
var size = clonk->~MaxContentsCount();
// need to create more inventory buttons?
if(size > GetLength(inventory))
for(var i=0; i < size; ++i)
if(!inventory[i])
InventoryButton();
// need to remove some inventory buttons?
if(size < GetLength(inventory))
for(i=GetLength(inventory)-1; i >= size; i--)
inventory[i]->RemoveObject();
SetLength(inventory, size);
}
// Insert a button into the actionbar at pos
private func ActionButton(object forClonk, int pos, object interaction, int actiontype, int hotkey, int num, proplist extra)
{

View File

@ -0,0 +1,8 @@
[DefCore]
id=Icon_Hand
Version=5,2,0,1
Category=C4D_StaticBack
Picture=0,0,32,32
Width=64
Height=64
Offset=-32,-32

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

@ -0,0 +1,7 @@
[DefCore]
id=Icon_Menu_Circle
Version=5,2,0,1
Category=C4D_StaticBack
Width=128
Height=128
Offset=-64,-64

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -0,0 +1,7 @@
[DefCore]
id=Icon_Menu_CircleHighlight
Version=5,2,0,1
Category=C4D_StaticBack
Width=64
Height=64
Offset=-32,-32

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -112,10 +112,7 @@ public func CarryHeavy(object target)
if(target->Contained() != this)
target->Enter(this);
// notify UI about carryheavy pickup
this->~OnCarryHeavyChange(carryheavy);
// Update attach stuff
this->~OnSlotFull();
@ -142,7 +139,6 @@ private func StopCarryHeavy()
return;
carryheavy = nil;
this->~OnCarryHeavyChange(nil);
this->~OnSlotEmpty();
}