Jump to content

textbox thingoid


Rick
 Share

Recommended Posts

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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

post-35-12643590694438_thumb.jpg

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

       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.

AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD

76561197984667096.png

Link to comment
Share on other sites

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?

AMD Athlon x2 7750 2.7ghz, 6gb ddr2 ram, Galaxy9800GT 1gig ddr2 video card, Windows 7,64.

Link to comment
Share on other sites

@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. :D

AMD Phenom II x6 1100T - 16GB RAM - ATI 5870 HD - OCZ Vertex 2 60GB SSD

76561197984667096.png

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...