Writing my own texteditor...

Discuss any general programming issues here
Post Reply
phipslk
Posts: 34
Joined: Sun Oct 29, 2023 7:21 pm

Writing my own texteditor...

Post by phipslk »

I have the idea to create a LetterMaker, like these for C64 and Amiga existed. You type in a text, and all your moves get recorded and replayed in the final exe like a notice, the reader can watch, how you typed.

But I have problems with the "Delete/Backspace" routine. I just don't get how best to store the typed data to be able, to remove chars from screen where you hit "DEL/BACKSPACE".

Code: Select all

/*
** Letter Maker
*/
@VERSION 10,0		; requires Hollywood 10.0

@OPTIONS {DPIAware = True}

@APPTITLE "Letter Maker"
@APPVERSION "$VER: alpha 1 (25.09.24)"
@APPCOPYRIGHT "(C) Philipp Lonke"
@APPAUTHOR "Philipp Lonke"
@APPDESCRIPTION "Tool for making a nice letter with music and effects"

Const #DEBUG = True
Const #SCRWD = 640
Const #SCRHT = 480

@DISPLAY 1,{Title = "LetterMaker alpha", X = #CENTER, Y = #CENTER, Width = #SCRWD, Height = #SCRHT, Color = #BLACK, hidden = True, layers = True}

x,y,t,oldt,count=0 

textlist=CreateList()
savetext=CreateList()
objlist=CreateList()

w$=""

Function textedit(msg)
;	HideLayer("cursor")
;	Box(x,y,font.size,font.size,#BLACK)
;	Cls()
	Switch msg.key
	Case "ESC"
		For i,v In Pairs(savetext)
			w$=w$ .. v.key
		Next
		r$=RightStr(w$,1)
		w$=r$..UnleftStr(w$,1)
		If #DEBUG=True Then DebugPrint(w$)
		End
	Case "DOWN"
		y=y+font.size
		If y>#SCRHT-font.size Then y=#SCRHT-font.size
		a={key="^DOWN",x=x,y=y,width=0, cnt=0}
		InsertItem(savetext,a)

	Case "UP"
		y=y-font.size
		If y<0 Then y=0
		a={key="^UP",x=x,y=y,width=0, cnt=0}
		InsertItem(savetext,a)
	Case "RIGHT"
		x=x+info
		If x>#SCRWD 
			x=0
			y=y+font.size
		EndIf
		a={key="^RIGHT",x=x,y=y,width=0, cnt=0}
		InsertItem(savetext,a)
	Case "LEFT"
		x=x-info
		If x<0 
			x=#SCRWD-info
			y=y-font.size
			If y<0 Then y=y+font.size
		EndIf
		
		a={key="^LEFT",x=x,y=y,width=0, cnt=0}
		InsertItem(savetext,a)
	Case "ENTER"
		x=0
		y=y+font.size
		a={key="^ENTER",x=x,y=y,width=0, cnt=0}
		InsertItem(savetext,a)		
	Case "RETURN"
		x=0
		y=y+font.size
		a={key="^RETURN",x=x,y=y,width=0, cnt=0}
		InsertItem(savetext,a)
	Case "BACKSPACE"
		count=count-1
		If count<0 Then count=0
		x=x-GetAttribute(#TEXTOBJECT,objlist[count],#ATTRWIDTH)
;		RemoveSprite(textlist[count].cnt)
;		Undo(#TEXTOBJECT,ToNumber(objlist[count],#LIGHTUSERDATA))
		RemoveLayer(objlist[count])
		RemoveItem(textlist)
		a={key="^BACKSPACE",x=x,y=y,width=0, cnt=0}
		InsertItem(savetext,a)
	Default
		Local t1=CreateTextObject(Nil,msg.key)
		ConvertToBrush(#TEXTOBJECT,t1,Nil)
		SetLayerName(Nil,ToString(t1))
		charwd=GetAttribute(#TEXTOBJECT,t1, #ATTRWIDTH)
		If x < 0 Then x = 0
		If x > 640-charwd x = 0 y = y + font.size EndIf 
		If y < 0 Then y = 0
		If y >480 Then y = 480
		
		If #DEBUG Then DebugPrint(ToNumber(t1,#LIGHTUSERDATA))
		
;		DisplayTextObject(t1,x,y,{Name=ToString(t1)})
;		InsertItem(objlist,t1)
		DisplayBrush(id,x,y)
		InsertItem(objlist,GetAttribute(#BRUSH,id,#ATTRLAYERID))
		count=count+1
		
		If msg.key="^"
			Local a={key="^"..msg.key,x=x,y=y,width=charwd, cnt=t}
		Else
			Local a={key=msg.key,x=x,y=y,width=charwd, cnt=t}
		EndIf
			
		InsertItem(textlist,a)
		InsertItem(savetext,a)
		x=x+charwd
	EndSwitch
	a={}
	ShowLayer("cursor",x,y)
	
EndFunction

font=FontRequest("Zeichensatz auswählen",{x=#CENTER,y=#CENTER})
SetFont(font.name,font.size)
SetFontColor(font.color)
CreateBrush(1,font.size/2,font.size,font.color)		; cursor
DisplayBrush(1,0,0,{name="cursor"})					; show it

col = ColorRequest("Hintergrundfarbe",{x=#CENTER,y=#CENTER})
SetDisplayAttributes({color = col})
OpenDisplay(1)
ActivateDisplay(1)

InstallEventHandler({OnKeyDown = textedit})

Repeat
  WaitEvent
Forever
plouf
Posts: 584
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Writing my own texteditor...

Post by plouf »

Even thought is very good to provide source code to explain
Providing big code is difficult for other to interpet (too much reading)

Anyway your example fail after pushing any key except arrows . So cant realy see it eitherway :-)
Christos
amyren
Posts: 373
Joined: Thu May 02, 2019 11:53 am

Re: Writing my own texteditor...

Post by amyren »

I also tried to run your script but it fails at line 104
Is this the exact same script you are talking about, since like it is it never gets to the backspace issue.

The error I get is at line 104

Code: Select all

DisplayBrush(id,x,y)
It fails because find brush id 0
And at line 92 you use ConvertToBrush with Nil for autoassign the ID, there is no defined handle for the brush so how did you plan to use that brush later?

Then if the line 104 error is resolved it will also fail at ine 105

Code: Select all

InsertItem(objlist,GetAttribute(#BRUSH,id,#ATTRLAYERID))
According to the docs the #ATTRLAYERID attribute is to be used with layers, and probably not with brushes.
phipslk
Posts: 34
Joined: Sun Oct 29, 2023 7:21 pm

Re: Writing my own texteditor...

Post by phipslk »

thanks, I've noticed that too. My main problem with the IDs is, that I can't find them out. When givin "Nil" as ID when creating an object, it should get an automatic ID, but somehow I can't put them in an array for later delete them, when the user types backspace.
Post Reply