NörmalTyping lib

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: NörmalTyping lib

Post by kikito »

If I may:

Code: Select all

local function _handleSpecial(self, key)
  for keyname,methodName in ipairs({ backspace='Backspace', return='Return', left='Left', right='Right', up='Up', down='Down' }) do
    if key == keyname then
      self[methodName](self)
      return true
    end
  end
  return false
end

function goo.textinput:keypressed(key,unicode)
   if not self.focus then return false end
   if not _handleSpecial(self, key) and unicode ~= 0 and unicode < 1000 then self:keyText(key,unicode) end
   if self.onKeypressed then self:onKeypressed( key, unicode ) end
   return true
end
When I write def I mean function.
User avatar
ljdp
Party member
Posts: 209
Joined: Sat Jan 03, 2009 1:04 pm
Contact:

Re: NörmalTyping lib

Post by ljdp »

Readability vs Efficiency?
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: NörmalTyping lib

Post by The Burrito »

This is what I use:

Code: Select all

function love.keypressed(k,u)
	if textIn and u then --this is for text input 
		if k == "backspace" or k == "delete" and string.len(textInput) > 0then
			textInput = string.sub(textInput,1,string.len(textInput) - 1)
		elseif string.len(k) == 1 then
			textInput = textInput..string.char(u)
		end
	else
--stuff unrelated to text input

I'm pretty sure I got at least part of it off the forum somewhere... I can't remember but it seems to work perfectly fine.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: NörmalTyping lib

Post by bartbes »

Few notes, string.len should be replaced by the # operator and the string.sub should get -2 instead of len-1.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: NörmalTyping lib

Post by kikito »

ljdp wrote:Readability vs Efficiency?
Readability leads to understanding, understanding leads to separation of concepts, separation of concepts leads to good efficiency.

Lack of readability is the path of the Dark Side. Lack of readability leads to fear, fear leads to anger, anger leads to hate, hate leads ... to suffering.
When I write def I mean function.
User avatar
Luiji
Party member
Posts: 396
Joined: Mon May 17, 2010 6:59 pm

Re: NörmalTyping lib

Post by Luiji »

If you can't read your code, your more likely to make awful mistakes, the kind that leads to many, many bugs. I made this awful mistake with Space Attackers. Maybe I should post it after all, as a lesson.
Good bye.
User avatar
Tesselode
Party member
Posts: 555
Joined: Fri Jul 23, 2010 7:55 pm

Re: NörmalTyping lib

Post by Tesselode »

Here's a new version of the code. It looks a lot like knorke's:

Code: Select all

function normaltyping(currentstring,key)
	if key=="backspace" then --backspace logic
		if #currentstring>1 then
			currentstring=string.sub(currentstring,1,#currentstring-1)
		else
			currentstring=""
		end
	elseif key>="a" and key<="z" then --letters
		if love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") then --uppercase
			currentstring=currentstring..string.upper(key)
		else
			currentstring=currentstring..key --lowercase
		end
	elseif key>="0" and key<="9" then --numbers
		currentstring=currentstring..key
	elseif key==" " then --spaces
		currentstring=currentstring..key
	end
	
	return currentstring
end
For whatever reason, though, all of the keys are getting added to the string.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 55 guests