Gspöt - retained GUI lib

Showcase your libraries, tools and other projects that help your fellow love users.
fsc1
Prole
Posts: 3
Joined: Wed Jun 29, 2016 12:53 pm

Re: Gspöt - retained GUI lib

Post by fsc1 » Mon Mar 12, 2018 5:53 am

Hey guys, I have been trying to create a little IRC client with Love2D, LuaIRC (https://github.com/JakobOvrum/LuaIRC) and Gspot, but before I go any further, I need some advice, or some clear direction, maybe I am even going off on the wrong path here, but my goal is to have every line input on IRC on its own press-able event, so I can tap it on my tablet, and be able to do something with it (double tap and it'll automatically quote the person I want to respond to, etc).

Can someone give me some bit of guidance on this idea?

Code: Select all

gui = require('Gspot')
local irc = require "irc"
local sleep = require "socket".sleep
local s = irc.new{nick = "test"}
msgs = ""
nickname = ""

s:hook("OnChat", function(user, channel, message)
print(("[%s] %s: %s"):format(channel, user.nick, message)) --debug
msgs = message
nickname = user.nick
print(msgs)
end)

function love.load()
  scrollgroup = gui:scrollgroup(nil, {0, gui.style.unit, 460, 200},group)
	s:connect("localhost")
  s:join("#test")
end

love.update = function(dt)
  if msgs then scrollgroup:addchild(gui:text(msgs, {w = 400}), 'grid')
    print(msgs) --debug
    msgs=nil
  end
  gui:update(dt)  
  s:think()
end

love.draw = function()  
  --love.graphics.setColor(0, 255, 0, 255)
  --love.graphics.print(("%s: %s"):format(nickname, msgs))
  gui:draw()

end

love.keypressed = function(key, code, isrepeat)
end

love.textinput = function(key)
end

love.mousepressed = function(x, y, button)
	gui:mousepress(x, y, button) -- pretty sure you want to register mouse events
end
love.mousereleased = function(x, y, button)
	gui:mouserelease(x, y, button)
end
love.wheelmoved = function(x, y)
	gui:mousewheel(x, y)
end

love.quit = function()  
  s:disconnect("bye")
end

User avatar
pgimeno
Party member
Posts: 1011
Joined: Sun Oct 18, 2015 2:58 pm

Re: Gspöt - retained GUI lib

Post by pgimeno » Mon Mar 12, 2018 12:22 pm

The demo already does most of what you need. By removing the unnecessary stuff and with very few extra mods, I put this together:

Code: Select all

-- Original author: https://github.com/trubblegum
-- This is a modified version of https://github.com/trubblegum/Gspot/blob/cf0a49d7d2073686d7ddb32a4fa04e90593d36c4/main.lua
-- The original program did not include a copyright notice.
-- Modifications © Copyright 2015 Pedro Gimeno Fortea.
--
-- This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
-- Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
-- 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
-- 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
-- 3. This notice may not be removed or altered from any source distribution.

if love._version_major == 0 and love._version_minor < 9 then
	error("This library needs love2d 0.9.0 or above")
end

gui = require('Gspot') -- import the library
--mainmenu = gui() -- create a gui instance. don't have to do this, but you may want a gui for each gamestate so they can talk to each other, and you won't have to recontsruct the gui every time you enter a state

love.load = function()
	
	-- another group, with various behaviours
	group2 = gui:group('Group 2', {gui.style.unit, 128, 256, 256})
	group2.drag = true
	-- scrollgroup's children, excepting its scrollbar, will scroll
	scrollgroup = gui:scrollgroup(nil, {0, gui.style.unit, 256, 256}, group2) -- scrollgroup will create its own scrollbar
	scrollgroup.scrollh.tip = 'Scroll (mouse or wheel)' -- scrollgroup.scrollh is the horizontal scrollbar
	scrollgroup.scrollh.style.hs = scrollgroup.style.unit*2
	scrollgroup.scrollv.tip = scrollgroup.scrollh.tip -- scrollgroup.scrollv is the vertical scrollbar
	--scrollgroup.scroller:setshape('circle') -- to set a round handle
	scrollgroup.scrollv.style.hs = "auto"
	
	-- additional scroll controls
	button = gui:button('up', {group2.pos.w, 0}, group2) -- a small button attached to the scrollgroup's group, because all of a scrollgroup's children scroll
	button.click = function(this)
		local scroll = scrollgroup.scrollv
		scroll.values.current = math.max(scroll.values.min, scroll.values.current - scroll.values.step) -- decrement scrollgroup.scrollv.values.current by scrollgroup.scrollv.values.step, and the slider will go up a notch
	end
	button = gui:button('dn', {group2.pos.w, group2.pos.h + gui.style.unit}, group2)
	button.click = function(this)
		local scroll = scrollgroup.scrollv
		scroll.values.current = math.min(scroll.values.max, scroll.values.current + scroll.values.step) -- this one increment's the scrollbar's values.current, moving the slider down a notch
	end
	
	local function addHistory(nick, text)
		local nickwidget = gui:text(nick, {w=32})
		nickwidget.click = function(this) gui:feedback(this.label) end
		local textwidget = gui:text(text, {w=128})
		textwidget.click = function(this) gui:feedback(this.label) end
		local box = gui:hidden()
		box:addchild(nickwidget, 'horizontal')
		box:addchild(textwidget, 'horizontal')
		box.pos.h = math.max(nickwidget.pos.h, textwidget.pos.h)
		scrollgroup:addchild(box, 'vertical')
		scrollgroup.scrollv.values.current = scrollgroup.scrollv.values.max
	end

	-- text input
	input = gui:input('Chat', {64, love.graphics.getHeight() - 32, 256, gui.style.unit})
	input.keyrepeat = true -- this is the default anyway
	input.done = function(this) -- Gspot calls element:done() when you hit enter while element has focus. override this behaviour with element.done = false
		addHistory('Me', this.value)
		this.value = ''
		this.Gspot:unfocus()
	end
	button = gui:button('Speak', {input.pos.w + gui.style.unit, 0, 64, gui.style.unit}, input) -- attach a button
	button.click = function(this)
		this.parent:done()
	end
end

love.update = function(dt)
	gui:update(dt)
end

love.draw = function()
	gui:draw()
end

love.keypressed = function(key, code, isrepeat)
	if gui.focus then
		gui:keypress(key) -- only sending input to the gui if we're not using it for something else
	elseif key == 'return'then -- binding enter key to input focus
		input:focus()
	end
end

love.textinput = function(key)
	if gui.focus then
		gui:textinput(key) -- only sending input to the gui if we're not using it for something else
	end
end

-- deal with 0.10 mouse API changes
love.mousepressed = function(x, y, button)
	gui:mousepress(x, y, button) -- pretty sure you want to register mouse events
end
love.mousereleased = function(x, y, button)
	gui:mouserelease(x, y, button)
end
love.wheelmoved = function(x, y)
	gui:mousewheel(x, y)
end
I'm a bit puzzled myself at the need of a hidden element. I initially used a group element, but the children were partially hidden behind it rather than being inside it.

(Edited to remove a leftover function call that was causing crashes)

fsc1
Prole
Posts: 3
Joined: Wed Jun 29, 2016 12:53 pm

Re: Gspöt - retained GUI lib

Post by fsc1 » Tue Mar 13, 2018 12:11 am

pgimeno, thanks for that addHistory function, I would have never guessed to use the hidden element to get the children to fit. I tried all night trying to get a box group working as a child to the scrollgroup. Not sure how you figured that out! But thanks.

User avatar
pgimeno
Party member
Posts: 1011
Joined: Sun Oct 18, 2015 2:58 pm

Re: Gspöt - retained GUI lib

Post by pgimeno » Tue Mar 13, 2018 1:57 am

You're welcome!

Well, I just remembered this: https://github.com/pgimeno/Gspot/issues ... -264350329

Short version: drawing order depends on creation order, therefore creating the group first works.

There are plans to change that among many other things: https://github.com/pgimeno/Gspot/issues/6

There's still some issue with label widgets, because in my machine at least, when I write 'y', the bottom pixel row of the 'y' disappears under the next box when I enter a new line, making it look like a 'v' meaning the height is miscalculated.

User avatar
pgimeno
Party member
Posts: 1011
Joined: Sun Oct 18, 2015 2:58 pm

Re: Gspöt - retained GUI lib

Post by pgimeno » Sat Apr 07, 2018 5:22 pm

Adaptation to LÖVE 11.0 is finished and pending in a pull request: https://github.com/pgimeno/Gspot/pull/9

I'd appreciate feedback on the approach taken before I merge it. Lacking any feedback, I will merge it in 3 days.

Edit: Pushed that and https://github.com/pgimeno/Gspot/pull/8 to fix the text element miscalculation.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests