GUI: dynamically created

Get technical support about the C++ source code and about Lua scripts for maps, entities, GUIs, the console, materials, etc. Also covered are the Cafu libraries and APIs, as well as compiling, linking, and the build system.
Post Reply
User avatar
Haimi
Posts:85
Joined:2011-11-23, 09:28
GUI: dynamically created

Post by Haimi » 2012-02-27, 17:45

Hi @ll,

I am trying to create an inventory with a variable Number of Slots.
So I tried to realize this using the following Code:

Code: Select all

function InitInventorySlots(SlotsCnt, ...)
	Inventory.SlotsCnt = SlotsCnt;
end

function Inventory:OnActivate()
    local i = 1;
    local ItemsPerLine = 5;
    Slots = {};
    for i = 1, self.SlotsCnt, 1 do
    	Slots[i] = gui:new("WindowT", "Slot" .. i);
    	Slots[i]:set("rect", 540 + ((i % ItemsPerLine) * 28), math.floor(i / ItemsPerLine) * 28, 25, 25);
	    Slots[i]:set("backColor", 0, 0.25, 0.7, 0.4);
	    Slots[i]:set("borderWidth", 0.5);
	    Slots[i]:set("borderColor", 0.7, 0.8, 0.9, 1);
		Inventory:AddChild(Slots[i]);
    end
end
The code used in Inventory:OnActivate() was in InitInventorySlots(SlotsCnt, ...) before, but my Inventory shows up without the Items. No error, just does not show up. Do I have to make an init method for every slot? And how can I realize this with dynamically created windows?
Project Status: Code architecture definition
6 Programmers, 1 Photographer, 1 Architect, 1 Game designer
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: GUI: dynamically created

Post by Carsten » 2012-02-28, 01:19

Could you please attach the complete *.cgui files for your GUI?
Things are hard to tell from that fragment alone.
Best regards,
Carsten
User avatar
Haimi
Posts:85
Joined:2011-11-23, 09:28

Re: GUI: dynamically created

Post by Haimi » 2012-02-28, 12:17

For sure I can :)
Inventory_init.cgui

Code: Select all

InvBg = gui:new("WindowT", "InvBg");

Inventory = gui:new("WindowT", "Inventory");

gui:SetRootWindow(InvBg);

-- Setup the window hierarchy.
-- ***************************
InvBg:AddChild(Inventory);

-- Initialization of the window contents ("constructor code").
-- ***********************************************************

function InvBg:OnInit()
    self:set("backColor", 1, 0, 0, 0.7);

    gui:activate      (false);
    gui:setInteractive(true);
    gui:showMouse     (true);
end

function Inventory:OnInit()
    self:set("rect", 400, 100, 100, 300);
    self:set("backColor", 0, 0.25, 0.7, 0.4);
    self:set("borderWidth", 0.5);
    self:set("borderColor", 0.7, 0.8, 0.9, 1);
	gui:setFocus(Inventory);
end
and my Inventory.cgui

Code: Select all

dofile("Games/obg/GUIs/Inventory_init.cgui");

function InitInventorySlots(SlotsCnt, ...)
    local i = 1;
    local ItemsPerLine = 5;
    Slots = {};
    for i = 1, SlotsCnt, 1 do
    	Slots[i] = gui:new("WindowT", "Slot" .. i);
    	Slots[i]:set("rect", 540 + ((i % ItemsPerLine) * 28), math.floor(i / ItemsPerLine) * 28, 25, 25);
	    Slots[i]:set("backColor", 0, 0.25, 0.7, 0.4);
	    Slots[i]:set("borderWidth", 0.5);
	    Slots[i]:set("borderColor", 0.7, 0.8, 0.9, 1);
		Inventory:AddChild(Slots[i]);
    end
end

function InvBg:OnChar(ch)
    if (ch == 0x1B or ch == 0x69) then -- ESC or I
    	gui:activate(false);
    	return true;
    end
    return false;
end
Project Status: Code architecture definition
6 Programmers, 1 Photographer, 1 Architect, 1 Game designer
User avatar
Carsten
Site Admin
Posts:2170
Joined:2004-08-19, 13:46
Location:Germany
Contact:

Re: GUI: dynamically created

Post by Carsten » 2012-02-29, 00:45

Well, in function InvBg:OnInit() a line like

Code: Select all

    self:set("rect", 0, 0, 640, 480);
is missing if you want it to be visible.

Function InitInventorySlots() is nowhere called in your code, thus it cannot generate the windows that you expect.

If I copy the contents of InitInventorySlots() into the Inventory_init.cgui file and add line local SlotsCnt = 3; as well, then I see three generated child windows when I load the GUI into the GUI Editor.
Note however that the position of child windows is relative to their parent, so in this case, an x-coordinate of 540 and more is way off screen. ;-)

Btw., best you make sure that InitInventorySlots() is only called once… e.g. just call if from the main body of the script:

Code: Select all

function InitInventorySlots(SlotsCnt, ...)
    -- ...
end

InitInventorySlots(3);
Best regards,
Carsten
User avatar
Haimi
Posts:85
Joined:2011-11-23, 09:28

Re: GUI: dynamically created

Post by Haimi » 2012-02-29, 08:59

AH, oh my god, just found it: InitInventorySlots() is called on button press from c++, but i did not realize that I need to place the items relatively :D Thanks for opening my eyes :D
Project Status: Code architecture definition
6 Programmers, 1 Photographer, 1 Architect, 1 Game designer
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests