Forum

> > CS2D > Scripts > Hover Image
Forums overviewCS2D overview Scripts overviewLog in to reply

English Hover Image

5 replies
To the start Previous 1 Next To the start

old Re: Hover Image

Zeik
User Off Offline

Quote
I think that the only way to do that is by asking the position of the cursor all the time, resulting in lots of lag.

You can do it by using any timer hook (cs2d lua hook ms100, cs2d lua hook second, etc) to call the cs2d lua cmd reqcld function (in mode 0), which calls the cs2d lua hook clientdata's hook function. So, in the cs2d lua hook clientdata function you will have to ask where the cursor is, and if it's at the image's position, then you swap images. Then you would want to ask for when the cursor leaves the image so you change it back to the original.

I think it's not worth doing such thing. The only way I think it wouldn't lag is if you use the cs2d lua hook second hook, though it may look very unresponsive.

You can use this post's responses as reference as how to do this: LINK
It's basically the same thing.

old Re: Hover Image

Serigala
User Off Offline

Quote
Just check this code >

old Re: Hover Image

GeoB99
Moderator Off Offline

Quote
user Serigala's code works but on the 23 line it triggers an error due of an unfinished string. It can be fixed very easily by just removing the unnecessary quote.

old Re: Hover Image

Dousea
User Off Offline

Quote
Probably won't work as expected.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- You need to specify the position of the button manually
local buttonx, buttony = 320, 240 -- on the center of the screen
-- You need to specify the size of the button manually (based on the image)
local buttonwidth, buttonheight = 100, 50
local buttonexists, buttonimage, buttonhovered, mousepos = {}, {}, {}, {}

for id = 1, 32 do
	buttonexists[id] = false
	buttonimage[id] = 0
	buttonhovered[id] = false
	mousepos[id] = {0, 0}
end

addhook("spawn", "spawnhook")
addhook("clientdata", "clientdatahook")
addhook("ms100", "ms100hook")
addhook("die", "diehook")

function spawnhook(id)
	-- Button will appear and hoverable when spawning
	buttonexists[id] = true
	buttonimage[id] = image("path/to/your/button/image.png", buttonx, buttony, 2, id)
end

function clientdatahook(id, mode, data1, data2)
	if (mode == 0) then
		mousepos[id] = {data1, data2}
	end
end

function ms100hook()
	for index, id in ipairs(player(0, "tableliving")) do
		if (buttonexists[id]) then
			reqcld(id, 0)
			
			local x, y = unpack(mousepos[id])
			
			if ((x >= buttonx and x <= buttonx + buttonwidth) and (y >= buttony and y <= buttony + buttonheight)) then
				if (not buttonhovered[id]) then
					freeimage(buttonimage[id])
					
					buttonimage[id] = image("path/to/your/button/hovered/image.png", buttonx, buttony, 2, id)
					buttonhovered[id] = true
				end
			else
				if (buttonhovered[id]) then
					freeimage(buttonimage[id])
					
					buttonimage[id] = image("path/to/your/button/image.png", buttonx, buttony, 2, id)
					buttonhovered[id] = false
				end
			end
		end
	end
end

function diehook()
	-- Button will disappear when you die
	buttonexists[id] = false
	
	freeimage(buttonimage[id])
	
	buttonimage[id] = 0
	buttonhovered[id] = false
end

old Re: Hover Image

Rygna
User Off Offline

Quote
Thanks, i will test it
maybe, I'm making a project / mod for this.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview