Rick Posted January 22, 2010 Share Posted January 22, 2010 So I'm working on a textbox thingoid. You drag it into your scene and it creates a 2D textbox. You can control the location and size via the settings. Basically what this does is create a flip hook. My question is, the way I'm currently getting a given instance of the textbox from inside this hook is looping through the object table and checking a variable. I would prefer not to have to loop through the object table since I think it has every object in your scene, and was wondering if anyone can think of another way to do this. Ideally I would just be able to loop through just these object instances and not every object in the scene. I could probably create a global table, but was just curious if there is a way to loop through just certain models of like type? For example if I place 10 monstertruck models in my scene, would there be a table that I can loop through that just returns the monstertruck instances and not all the other models in my scene, and how could I get that into a hook function? If anything I'd like to just get some dialog going on it. Here is an example of what I'm talking about: function Pi_TextBox_Flip() -- we want to find textboxes so we can check their properties and draw them according to their settings -- ideally I would be able to just loop through the textbox objects and not every object in the scene for k,v in pairs(objecttable) do if v.type ~= nil then if v.type == "textbox" then SetBlend(1) SetColor(v.color) --DrawText("Hello", 0, 100) DrawRect(v.location.x, v.location.y, v.size.x, v.size.y) SetColor(Vec4(1, 1, 1, 1)) SetBlend(0) end end end end function class:CreateObject(model) local object=self.super:CreateObject(model) object.model = model object.name = object.model:GetKey("name") object.type = "textbox" object.location = Vec2(0,0) object.size = Vec2(100, 50) object.color = Vec4(1, 0, 0, 1) -- Add the drawing hook AddHook("Flip", Pi_TextBox_Flip) ... /code] Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 22, 2010 Share Posted January 22, 2010 Thats realy nice Rick, I can see the some cool potentials here. Quote Link to comment Share on other sites More sharing options...
gordonramp Posted January 23, 2010 Share Posted January 23, 2010 This looks useful, but is this working code and how would I go about turning it into an object that could be dragged into a scene? Quote AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64. Link to comment Share on other sites More sharing options...
Rick Posted January 23, 2010 Author Share Posted January 23, 2010 Here is the entire lua file. I also put in a little example of how you can get input. The drawing of the textbox is just the DrawRec(). I didn't put much time into making the textbox look better. You would also need to make text saved per instance and displaying it. This is just to get people thinking differently about GUI programming in lua. I just copy the light objects and rename them to TextBox. require("scripts/class") require("scripts/hooks") --- create the class local class=CreateClass(...) function class:InitDialog(grid) self.super:InitDialog(grid) group = grid:AddGroup("TextBox") group:AddProperty("location", PROPERTY_VEC2, "0,0", "Location") group:AddProperty("size", PROPERTY_VEC2, "100,50", "Size") end function Pi_TextBox_Flip() -- we want to find textboxes so we can check their properties and draw them according to their settings for k,v in pairs(objecttable) do if v.type ~= nil then if v.type == "textbox" then SetBlend(1) SetColor(v.color) --DrawText("Hello", 0, 100) DrawRect(v.location.x, v.location.y, v.size.x, v.size.y) SetColor(Vec4(1, 1, 1, 1)) SetBlend(0) end end end end function class:CreateObject(model) local object=self.super:CreateObject(model) object.model = model object.name = object.model:GetKey("name") object.type = "textbox" object.location = Vec2(0,0) object.size = Vec2(100, 50) object.color = Vec4(1, 0, 0, 1) -- Add the drawing hook AddHook("Flip", Pi_TextBox_Flip) function object:Free(model) -- Must be sure to remove the hook or it'll still draw RemoveHook("Flip", Pi_TextBox_Flip) self.super:Free() end function object:SetKey(key,value) if key == "location" then object.location = StringToVec2(value) elseif key == "size" then object.size = StringToVec2(value) else return self.super:SetKey(key,value) end return 1 end function object:GetKey(key,value) if key=="location" then return Vec2ToString(object.location) elseif key=="size" then return Vec2ToString(object.size) else return self.super:GetKey(key,value) end return value end function object:Initialize() --Notify(object.model:GetKey("name")) -- could store off all textbox objects here for k,v in pairs(objecttable) do if v.type ~= nil then if v.type == "textbox" then end end end end function object:Update() if GetGlobalString("mode") == "GAME_MODE" then if MouseHit(1) == 1 then if MouseX() > object.location.x and MouseX() < object.location.x + object.size.x and MouseY() > object.location.y and MouseY() < object.location.y + object.size.y then object.color = Vec4(0, 1, 0, 1) end end else end end end Quote Link to comment Share on other sites More sharing options...
gordonramp Posted January 24, 2010 Share Posted January 24, 2010 Had a go at this and the text box appears in the Editor but when I run the program it doesn't appear. Quote AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64. Link to comment Share on other sites More sharing options...
Rick Posted January 24, 2010 Author Share Posted January 24, 2010 Yeah, sorry, you have to use my Pi-Main object which you can find in the download section, OR basically you have to create a global variable inside you main lua file (the one with the main loop) GetGlobalString("mode") == "GAME_MODE". http://leadwerks.com/werkspace/index.php?app=downloads&showfile=72 I do plan on making these a little more functional also. Just takes some time. I just thought it would be nice to get the general idea out there to see what others thought and could come up with. Quote Link to comment Share on other sites More sharing options...
gordonramp Posted January 24, 2010 Share Posted January 24, 2010 Using the 'Pi-Main object', I can move the Textbox to a position on the screen in the Object Properties and when I run the program I can click on the object and it will turn green. Is that it , do I just put text over the top of it, or is it for input output? Quote AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64. Link to comment Share on other sites More sharing options...
Rick Posted January 24, 2010 Author Share Posted January 24, 2010 That's it for now. I didn't add any keyboard input to it. I will though. It just requires some work. Quote Link to comment Share on other sites More sharing options...
gordonramp Posted January 24, 2010 Share Posted January 24, 2010 Ok, nice.. Quote AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64. Link to comment Share on other sites More sharing options...
VicToMeyeZR Posted January 25, 2010 Share Posted January 25, 2010 function object:Update() if GetGlobalString("mode") == "GAME_MODE" then if MouseHit(1) == 1 then if MouseX() > object.location.x and MouseX() < object.location.x + object.size.x and MouseY() > object.location.y and MouseY() < object.location.y + object.size.y then object.color = Vec4(0, 1, 0, 1) end end else end end This part right there. Thats what sets the color when you click on it. Quote AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD Link to comment Share on other sites More sharing options...
gordonramp Posted January 25, 2010 Share Posted January 25, 2010 VicToMeyeZR, I'm not sure why you pointed out that piece of code but it could be useful to click on an object in a 3D scene. This is something I have been trying to figure out how to do. if MouseHit(1) == 1 then if MouseX() > object.location.x and MouseX() < object.location.x + object.size.x and MouseY() > object.location.y and MouseY() < object.location.y + object.size.y then object.color = Vec4(0, 1, 0, 1) end end The question is.. how can I adapt this to click on an object in a 3D scene so that an event can take place? It has x and y reference for the mouse but no z. Could it be something like.. if MouseHit(1) == 1 then if MouseX() > object.location.x and MouseX() < object.location.x + object.size.x and MouseY() > object.location.y and MouseY() < object.location.y + object.size.y and MouseZ() > object.location.z and MouseZ() < object.location.z + object.size.z then object.color = Vec4(0, 1, 0, 1) end end But if that would work, how could I relate it to a particular object in the scene? Quote AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64. Link to comment Share on other sites More sharing options...
Rick Posted January 25, 2010 Author Share Posted January 25, 2010 I think it's CameraPick() that you are looking for. Look on the forums for that. Quote Link to comment Share on other sites More sharing options...
VicToMeyeZR Posted January 25, 2010 Share Posted January 25, 2010 @gordonramp I pointed that out, so you could play with it. You asked about it changing colors when you clicked on it, so I thought I would point out the part that did it, so you could mess around and experiment with the code. Quote AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD Link to comment Share on other sites More sharing options...
Rick Posted January 25, 2010 Author Share Posted January 25, 2010 One of the cool things about this is that it could be part of a bigger input/output system that is all Thingoid based. Properties on a button control could say send the data from a textbox to this other Thingoid so it can act on it somehow. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.