Page 1 of 2

Creating a Text Editor

Posted: Sun Nov 30, 2008 4:42 pm
by Zeggy
I'm creating a simple text editor with Love, called Create Love...

I don't expect it to get anywhere, but I think it will be very useful for me to learn.
This is my first time using lua/love, so please point out mistakes or better ways to do things :)


Here is what I have so far:

Code: Select all

Removed to shorten post
Basically, I'm able to type text in lower case/upper case, add spaces, tabs and line breaks, and basic symbols (like .,/';[]).
I'll add more cases to check, like holding shift with a number to give !@#$%^&*(). Of course, different keyboards might have different symbols, so this will probably work best with just my keyboard :P

However, I need some help since like I said, I'm new.

How can I manipulate strings to take away the last character? I want to do this for backspace so I can delete characters.

Thanks!



EDIT: Oh wait, I notice that there are also constants for exclamation marks, question marks, etc. When are these constants returned?
Because when I use the keypressed() function, and I press 'shift+1', I get a 'shift' and a '1' returned, not a '!'.

Re: Creating a Text Editor

Posted: Sun Nov 30, 2008 6:04 pm
by Kuromeku
That switch thing is ugly, why are you doing that? It's pointless, just do the normal 'if then else' statements.

And, you can use string.sub(), http://lua-users.org/wiki/StringLibraryTutorial.

Re: Creating a Text Editor

Posted: Sun Nov 30, 2008 7:14 pm
by Dvondrake
---

Re: Creating a Text Editor

Posted: Sun Nov 30, 2008 9:58 pm
by Zeggy
Thanks for the help!

I used sub like this:

Code: Select all

text = string.sub(text, 0, (string.len(text)-1))
I thought sub(text, 0, -1) might work as a shorter way, but that didn't seem to do anything. :P


btw, as for the switch, I think it's a lot eaiser to read than ifs and elseifs.
I've changed my code to have the defaul case do nothing, and I added cases for each letter instead. So here's how it looks:

Code: Select all

			[love.key_a]			=	function (x) return letter(x) end,
			[love.key_b]			=	function (x) return letter(x) end,
			[love.key_c]			=	function (x) return letter(x) end,
			[love.key_d]			=	function (x) return letter(x) end,
			[love.key_e]			=	function (x) return letter(x) end,
			[love.key_f]			=	function (x) return letter(x) end,
			[love.key_g]			=	function (x) return letter(x) end,
			[love.key_h]			=	function (x) return letter(x) end,
			[love.key_i]			=	function (x) return letter(x) end,
			[love.key_j]			=	function (x) return letter(x) end,
			[love.key_k]			=	function (x) return letter(x) end,
			[love.key_l]			=	function (x) return letter(x) end,
			[love.key_m]			=	function (x) return letter(x) end,
			[love.key_n]			=	function (x) return letter(x) end,
			[love.key_o]			=	function (x) return letter(x) end,
			[love.key_p]			=	function (x) return letter(x) end,
			[love.key_q]			=	function (x) return letter(x) end,
			[love.key_r]			=	function (x) return letter(x) end,
			[love.key_s]			=	function (x) return letter(x) end,
			[love.key_t]			=	function (x) return letter(x) end,
			[love.key_u]			=	function (x) return letter(x) end,
			[love.key_v]			=	function (x) return letter(x) end,
			[love.key_w]			=	function (x) return letter(x) end,
			[love.key_x]			=	function (x) return letter(x) end,
			[love.key_y]			=	function (x) return letter(x) end,
			[love.key_z]			=	function (x) return letter(x) end,
Now imagine that with ifs and elseifs :D


Okay, now for the hard part... Navigating text with the arrow keys... :(

Does anybody have any clue how to do this? I don't have any ideas on this one.
I currently store all the text as a single string... was that a bad start? Does it make navigating the text really difficult?

EDIT: Actually, I figured it out! Also using string.sub :)
Although, you can only navigate left and right.
I've attached the love file so you can see what I've got so far. I would love any suggestions you might have.
createlove011.love
Create Love v0.1.1 - text editor made with Love
(20.13 KiB) Downloaded 550 times
What I want to add next is getting symbols (punctuation) to work...
I still have a question from my original post:
Oh wait, I notice that there are also constants for exclamation marks, question marks, etc. When are these constants returned?
Because when I use the keypressed() function, and I press 'shift+1', I get a 'shift' and a '1' returned, not a '!'.

Re: Creating a Text Editor

Posted: Mon Dec 01, 2008 12:19 am
by rude
Funny how people use LÖVE for things it clearly isn't designed for. :D

Re: Creating a Text Editor

Posted: Mon Dec 01, 2008 12:40 am
by Zeggy
Hehe, yeah. I do intend to create a game eventually.

Like I said, this is just a way for me to learn how to use lua and love, and so far I've learnt so much! :D
I'm not really a programmer (ie. I know syntax/structures and stuff, but I never programmed anything before), so pretty much everything is new for me :)

Here's 0.1.3 of my text editor. It's been improved quite a bit, with some bug fixes.
createlove013.love
(20.81 KiB) Downloaded 555 times

Re: Creating a Text Editor

Posted: Mon Dec 01, 2008 12:50 am
by rude
Creating a text editor is probably a poor introduction to LÖVE, with love.keyboard not having functions for getting the "right" input character and everything. Ah, well. That's what you get for using a game engine for normal application programming. :D

EDIT: That said, it works pretty well. ^^

Re: Creating a Text Editor

Posted: Mon Dec 01, 2008 1:34 am
by Borsty
Ah yes, multiline edits are a pain to do, my multiline edit for lgui ( which i'm porting to envy's gui system currently ) is like 800 lines of code, all the selection, replacing, removing etc.

I'm using an array to store "real" strings and one for the actual output. I do it this way because word wrapping strings everytime you draw it is just too slow.
I also store string Id and character offset in the output buffer.

For selecting and moving the cursor you only need some maths.

You'll be able to look at it once I've got my gui pack for envy done :)

Re: Creating a Text Editor

Posted: Mon Dec 01, 2008 5:19 pm
by appleide
might be a bit late:

Code: Select all

	local chars={} --when keys shifted
	chars["1"]="!";chars["a"]="A";chars["k"]="K";chars["u"]="U";chars["]"]="}"
	chars["2"]="@";chars["b"]="B";chars["l"]="L";chars["v"]="V";chars["\\"]="|"
	chars["3"]="#";chars["c"]="C";chars["m"]="M";chars["w"]="W";chars[";"]=":"
	chars["4"]="$";chars["d"]="D";chars["n"]="N";chars["x"]="X";chars["'"]="\""
	chars["5"]="%";chars["e"]="E";chars["o"]="O";chars["y"]="Y";chars[","]="<"
	chars["6"]="^";chars["f"]="F";chars["p"]="P";chars["z"]="Z";chars["."]=">"
	chars["7"]="&";chars["g"]="G";chars["q"]="Q";chars["`"]="~";chars["/"]="?"
	chars["8"]="*";chars["h"]="H";chars["r"]="R";chars["-"]="_";
	chars["9"]="(";chars["i"]="I";chars["s"]="S";chars["="]="+";
	chars["0"]=")";chars["j"]="J";chars["t"]="T";chars["["]="{";

	 local checkShift=function(keycode)
			if not (love.keyboard.isDown(love.key_lshift) or  love.keyboard.isDown(love.key_rshift)) then
				return string.char(keycode)
				
			end
			return chars[string.char(keycode)]
		end

Re: Creating a Text Editor

Posted: Mon Dec 01, 2008 6:46 pm
by farvardin
åccented letters are not supported.

Beware, some Vikings here mæy feel øffendeð!