Creating a Text Editor

Showcase your libraries, tools and other projects that help your fellow love users.
Zeggy
Prole
Posts: 13
Joined: Sun Nov 30, 2008 4:38 pm

Creating a Text Editor

Post 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 '!'.
Last edited by Zeggy on Sun Nov 30, 2008 11:04 pm, edited 1 time in total.
User avatar
Kuromeku
Party member
Posts: 166
Joined: Sun Jul 20, 2008 5:45 pm

Re: Creating a Text Editor

Post 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.
Dvondrake
Prole
Posts: 29
Joined: Sun Oct 26, 2008 5:53 pm

Re: Creating a Text Editor

Post by Dvondrake »

---
Last edited by Dvondrake on Mon Jan 21, 2019 10:20 pm, edited 1 time in total.
Zeggy
Prole
Posts: 13
Joined: Sun Nov 30, 2008 4:38 pm

Re: Creating a Text Editor

Post 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 500 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 '!'.
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Creating a Text Editor

Post by rude »

Funny how people use LÖVE for things it clearly isn't designed for. :D
Zeggy
Prole
Posts: 13
Joined: Sun Nov 30, 2008 4:38 pm

Re: Creating a Text Editor

Post 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 499 times
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Creating a Text Editor

Post 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. ^^
User avatar
Borsty
Citizen
Posts: 66
Joined: Sun Oct 19, 2008 10:29 pm
Location: Hamburg, Germany
Contact:

Re: Creating a Text Editor

Post 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 :)
User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: Creating a Text Editor

Post 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
User avatar
farvardin
Party member
Posts: 167
Joined: Sat Jun 28, 2008 6:46 pm

Re: Creating a Text Editor

Post by farvardin »

åccented letters are not supported.

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

Who is online

Users browsing this forum: No registered users and 57 guests