Implemented drag and drop for production menu

Only partially, and also rightclick to show resource need (graphics needed)
Maikel de Vries 2012-01-05 23:36:41 +01:00
parent 9f855c6067
commit b622ea4834
5 changed files with 97 additions and 18 deletions

View File

@ -15,10 +15,11 @@ protected func Construction()
crew = nil;
// parallaxity
this["Parallaxity"] = [0,0];
this.Parallaxity = [0,0];
// visibility
this["Visibility"] = VIS_None;
this.Visibility = VIS_None;
// mouse drag
this.MouseDrag = MD_DragSource | MD_DropTarget;
}
public func MouseSelectionAlt(int plr)
@ -95,7 +96,7 @@ public func MouseDrop(int plr, obj)
public func SetObject(object obj)
{
this["Visibility"] = VIS_Owner;
this.Visibility = VIS_Owner;
myobject = obj;
@ -105,12 +106,12 @@ public func SetObject(object obj)
{
SetGraphics(nil,nil,1);
SetName("$TxtEmpty$");
this["MouseDragImage"] = nil;
this.MouseDragImage = nil;
}
else
{
SetGraphics(nil,nil,1,GFXOV_MODE_ObjectPicture,0,0,myobject);
this["MouseDragImage"] = myobject;
this.MouseDragImage = myobject;
SetName(myobject->GetName());

View File

@ -47,10 +47,11 @@ protected func Construction()
subselector = nil;
// parallaxity
this["Parallaxity"] = [0,0];
this.Parallaxity = [0,0];
// visibility
this["Visibility"] = VIS_None;
this.Visibility = VIS_None;
// mouse drag
this.MouseDrag = MD_DragSource | MD_DropTarget;
}
protected func Destruction()
@ -273,7 +274,7 @@ public func ClearMessage()
public func SetObject(object obj, int type, int pos, int hot)
{
this["Visibility"] = VIS_Owner;
this.Visibility = VIS_Owner;
// remove effect that checks whether the object to which this selector
// refers to is still existant because now this selector gets a new
@ -290,7 +291,7 @@ public func SetObject(object obj, int type, int pos, int hot)
SetGraphics("None");
SetGraphics(nil,nil,1);
SetName(Format("$TxtSlot$",pos+1));
this["MouseDragImage"] = nil;
this.MouseDragImage = nil;
if(subselector)
subselector->RemoveObject();
}
@ -299,7 +300,7 @@ public func SetObject(object obj, int type, int pos, int hot)
SetGraphics();
SetGraphics(nil,nil,1,GFXOV_MODE_ObjectPicture, 0, 0, myobject);
SetName(Format("$TxtSelect$",myobject->GetName()));
this["MouseDragImage"] = myobject;
this.MouseDragImage = myobject;
// if object has extra slot, show it
if(myobject->~HasExtraSlot())

View File

@ -312,7 +312,7 @@ public func MouseDrop(int plr, obj)
if (!IsDragDropMenu()) return false;
// Forward command to commander.
return menu_commander->OnItemDropped(this, obj, nil);
return menu_commander->~OnItemDropped(this, obj, nil);
}
// Called when another item has been dropped on an item in this menu.

View File

@ -40,9 +40,12 @@ global func CreateProductionMenu(object producer)
protected func Construction()
{
_inherited(...);
menu_queue = [];
SetPosition(600, 400);
return _inherited(...);
// Production menus are no drag targets.
this.MouseDrag = MD_NoClick;
return;
}
public func AddMenuProducts(object producer)
@ -53,6 +56,7 @@ public func AddMenuProducts(object producer)
if (!AddItem(item))
return item->RemoveObject();
item->SetSymbol(product);
item.MouseDrag = MD_DragSource;
}
return;
}
@ -81,6 +85,18 @@ public func UpdateMenuQueue(object producer)
return;
}
public func ShowProductInfo(object item)
{
var product_id = item->GetSymbol();
var costs = menu_commander->ProductionCosts(product_id);
var cost_msg = "@";
for (var comp in costs)
cost_msg = Format("%s %dx {{%i}}", cost_msg, comp[1], comp[0]);
CustomMessage(cost_msg, this, GetOwner(), 250, 270, nil, nil, nil, 1);
return;
}
/* Menu properties */
public func IsProductionMenu() { return true; }
@ -193,13 +209,11 @@ public func OnItemSelection(object item)
if (IsProductItem(item))
{
menu_commander->AddToQueue(item->GetSymbol(), 1);
//UpdateMenuQueue(menu_commander);
}
// If item is from queue, remove one from queue.
if (IsQueueItem(item))
{
menu_commander->RemoveFromQueue(GetQueueIndex(item), 1);
//UpdateMenuQueue(menu_commander);
}
return _inherited(item, ...);
}
@ -207,6 +221,16 @@ public func OnItemSelection(object item)
// Called when an item has been selected (right mouse button).
public func OnItemSelectionAlt(object item)
{
// Show the production cost of the selected production item.
if (IsProductItem(item))
{
ShowProductInfo(item);
}
// Show production status of the selected queue item.
if (IsQueueItem(item))
{
// TODO: Implement if considered necessary.
}
return _inherited(item, ...);
}
@ -219,6 +243,22 @@ public func MouseDrop(int plr, obj)
// Called when another item has been dropped on an item in this menu.
public func OnItemDropped(object drop_item, object on_item)
{
// This will only be called from queue items, since product items are only drag sources.
// If the dropped item is a product: insert product into queue.
if (IsProductItem(drop_item))
{
menu_commander->InsertIntoQueue(drop_item->GetSymbol(), 1, GetQueueIndex(on_item));
}
// If the dropped item is in the queue: shift item around in the queue.
if (IsQueueItem(drop_item))
{
// TODO: Fix me, then uncomment.
//var index = GetQueueIndex(on_item);
//var amount = drop_item->GetCount();
//menu_commander->InsertIntoQueue(drop_item->GetSymbol(), amount, index);
//menu_commander->RemoveFromQueue(index - 1, amount);
}
return _inherited(drop_item, on_item, ...);
}

View File

@ -124,7 +124,7 @@ public func NeedsRawMaterial(id rawmat_id)
@param item_id id of the item under consideration.
@return a list of objects and their respective amounts.
*/
private func ProductionCosts(id item_id)
public func ProductionCosts(id item_id)
{
/* NOTE: This may be overloaded by the producer */
var comp_list = [];
@ -143,7 +143,7 @@ private func ProductionCosts(id item_id)
/** Adds an item to the production queue.
@param product_id id of the item.
@param amount the amount of items of \c item_id which should be produced.
@return \c current position of the item in the production queue.
@return current position of the item in the production queue.
*/
public func AddToQueue(id product_id, int amount)
{
@ -164,6 +164,43 @@ public func AddToQueue(id product_id, int amount)
return pos;
}
/** Inserts an item into the production queue at the specified position.
@param product_id id of the item.
@param amount the amount of items of \c item_id which should be inserted.
@param pos the position at which the object should be inserted, the rest of the queue is shifted downwards.
@return current position of the item in the production queue.
*/
public func InsertIntoQueue(id product_id, int amount, int pos)
{
// Check if this producer can produce the requested item.
if (!IsProduct(product_id))
return nil;
// Make sure the position is valid.
BoundBy(pos, 0, GetLength(queue) - 1);
// Check if there is the same product at that position, for a possible merge.
if (amount != nil && queue[pos].Product == product_id)
queue[pos].Amount += amount;
// Check if there is the same product at the position before, for a possible merge.
else if (amount != nil && pos > 0 && queue[pos-1].Product == product_id)
queue[pos-1].Amount += amount;
// Otherwise insert a new item into the queue.
else
{
// First shift all queue items from that position up by one.
var length = GetLength(queue);
for (var i = length; i > pos; i--)
queue[i] = queue[i-1];
// Then create new item.
queue[pos] = { Product = product_id, Amount = amount };
}
// Notify all production menus open for this producer.
for (var menu in FindObjects(Find_ID(Library_ProductionMenu), Find_Func("HasCommander", this)))
menu->UpdateMenuQueue(this);
return pos;
}
/** Removes a item or some of it from from the production queue.
@param pos position of the item in the queue.
@param amount the amount of this item which should be removed.