
-----------------------------------
MagicIdiot
Tue Jun 22, 2010 8:11 am

Backspacing in my (text editor?)
-----------------------------------
What is it you are trying to achieve?
Well grade 11 is over and I'll be quite bored over the summer. So i decided to make a program to test how fast you can type.


What is the problem you are having?
When you have multiple rows of text, trying to go back to a previous row with backspace is my problem. I can't seem to get the x coordinate right. The way I'm determining x right now only works if I have a max of 2 rows of text. I need a new method to get x.


Describe what you have tried to solve this problem
Change the way i get X. The only way I can think of is to have an array that holds the width(px) on each row. But I don't know how many rows there will be. It can be a waste of space. But if thats the only way..


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)



% Ricky

View.Set ("graphics:650;650")

% For the location of next letter and the starting/ending x
var x, y, startX, endX : int := 0

% Font size
var fontSize : int := 20

% The font
var font : int := Font.New ("arial:" + intstr (fontSize))

% The entered character and word
var key : string (1) := ""
var sentence : string := ""

% Extra spacing between the numbers. Varies depending on font size
var spacing : int := fontSize div 4

startX := 30
endX := 500
x := startX
y := 550

loop
    % This does the cursor blink
    if Time.Elapsed div 500 mod 2 = 0 then
        Draw.Line (x, y - 3, x, y + fontSize + 3, 7)
    elsif Time.Elapsed div 500 mod 2 = 1 then
        Draw.Line (x, y - 3, x, y + fontSize + 3, 0)
    end if

    if hasch = true then
        getch (key)
        if key = KEY_BACKSPACE and sentence not= "" then
            % Erases cursor blinker
            Draw.Line (x, y - 3, x, y + fontSize + 3, 0)
            % Check if it needs to go back to previous row
            if x - (Font.Width (sentence (length (sentence)), font) + spacing) < startX - 1 then

                % ---------------------------------- Here -----------------------------------------------------------
                x := (spacing * (length (sentence))) + startX + (Font.Width (sentence (1 .. length (sentence)), font))
                % ---------------------------------------------------------------------------------------------------

                y += 50
            end if
            % Sets the coordinates
            x -= Font.Width (sentence (length (sentence)), font) + spacing
            % Draws the fillbox to erase the letter
            Draw.FillBox (x - (fontSize div 5), y - (fontSize div 3), x + Font.Width (sentence (length (sentence)), font) + 1, y + fontSize, 0)
            % sentence = sentence - 1 character
            sentence := sentence (1 .. length (sentence) - 1)
        elsif key not= KEY_BACKSPACE then
            % Erases cursor blinker
            Draw.Line (x, y - 3, x, y + fontSize + 3, 0)
            % Check if it needs to go to next row
            if x + Font.Width (key, font) > endX + 1 then
                x := startX
                y -= 50
            end if
            % Print the key
            Draw.Text (key, x, y, font, 7)
            % sentence = sentence + key
            sentence += key
            % Sets new coords
            x += Font.Width (key, font) + spacing
        end if
        exit when key = KEY_ENTER or length (sentence) >= 255
    end if
end loop
put sentence





Please specify what version of Turing you are using
4.1

-----------------------------------
copthesaint
Tue Jun 22, 2010 9:19 am

RE:Backspacing in my (text editor?)
-----------------------------------
I belive BackSpace is chr (7).  Try that, if it doesnt work, try 8,9,10. Those couple are space enter and backspace however I forget the order.

-----------------------------------
DemonWasp
Tue Jun 22, 2010 9:49 am

RE:Backspacing in my (text editor?)
-----------------------------------
If seems like you want a flexible array. That kind of array lets you resize it at runtime as needed with the new keyword.

Alternately, look for "collections" under the Turing Submissions section and you'll find a few implementations of Lists, which are what you actually want.

-----------------------------------
MagicIdiot
Tue Jun 22, 2010 11:07 am

Re: Backspacing in my (text editor?)
-----------------------------------
The flexible array seems to work. I didn't know about it. Thanks.
