Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Default value of type-report not possible?
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ModsCentral




PostPosted: Thu Mar 29, 2012 4:53 pm   Post subject: Default value of type-report not possible?

I am trying to finish off this TextArea module that I am having a great deal of success with, now I've run into 1 minor issue.
Currently whoever uses my module can simply create a TextArea using the following

Turing:

import TextArea

var TestField : TextArea
TextArea.setDefault(TestField) %Without calling this, unless setting every property, program will crash due to "variable has no value"

TestField.x := 20
TestField.y := 300 %Loads of properties to be modified

TextArea.create (TestField) %From here on out, you can modify properties and the field will be updated oncall.


As you can see above, my issue is that the user must call "setDefault(nameOfField)" in order for this program to actually work.
This is because I'm using a type - report as a collection for the properties, and so when the module goes to initiate the textarea using the properties, there is no default value. What I need is a way to simple set a default value to the type variables like so.
Turing:

%What I currently have which I need to replace
type TextArea : record
    text : string
    x : real
    y : real
end record
%What I need basically, but is not supported...
type TextArea : record
    text : string := ""
    x : real := 10
    y : real := 10
end record


So, does anyone have any ideas on how I can go about this? Without having to create a procedure to call to set the default values (Currently what I'm doing)



ALSO
I am also trying to find a way to change the cursor to the text selection cursor for this module, as far I know there is no way to remove the cursor.
I'm thinking of creating a set of vb programs to change the mouse cursor as a process, maybe that should do it.

Thanks in advance
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Mar 29, 2012 5:02 pm   Post subject: RE:Default value of type-report not possible?

if you make TextArea be a class instead of a record, then it can contain all the same fields + methods (procedures). Some of those methods (a constructor) will run by default, whenever a new instance (variable) is created, so it can set the defaults on its own.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ModsCentral




PostPosted: Thu Mar 29, 2012 5:07 pm   Post subject: Re: Default value of type-report not possible?

How would I go about creating a procedure to run when a new variable of that type is instantiated?
Tony




PostPosted: Thu Mar 29, 2012 5:12 pm   Post subject: RE:Default value of type-report not possible?

a constructor is automatically executed on creation. It can be used to trigger any other actions that need to take place.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ModsCentral




PostPosted: Thu Mar 29, 2012 5:25 pm   Post subject: Re: Default value of type-report not possible?

My apologies but I'm still lost, how can I create a constructor? If I were to use this would it automatically call my procedure to set default values and create the filed as I want?
Tony




PostPosted: Thu Mar 29, 2012 5:31 pm   Post subject: RE:Default value of type-report not possible?

see class and new
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
ModsCentral




PostPosted: Thu Mar 29, 2012 5:32 pm   Post subject: Re: Default value of type-report not possible?

OK I'm trying to get my head wrapped around the differences between classes and modules. So if I use a class, and I create a textarea, should I use new, and if so will this then make the defined variables within the class unique to that specific variable? It's hard for me to explain this, but if this is the case then I can simply just remove type and have a list of variables with initial values without the type record, yes?
ModsCentral




PostPosted: Thu Mar 29, 2012 5:43 pm   Post subject: Re: Default value of type-report not possible?

Ok basically what I'm doing is using a module, with a type within it, and in my main program, I'm creating a new variable of that datatype, however there is no default value, what would you suggest (Can you provide sample code?)
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Mar 29, 2012 5:53 pm   Post subject: RE:Default value of type-report not possible?

I would suggest picking up some OOP concepts. http://en.wikipedia.org/wiki/Object-oriented_programming
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Raknarg




PostPosted: Thu Mar 29, 2012 6:56 pm   Post subject: RE:Default value of type-report not possible?

Well, I like to use this example for explaining the difference.

Lets say you're going to make a module to control the movement of a character in an RPG. This would be good for a module, because you have a bunch of procedures and functions that relate to a general task, and it doesnt need to act differently upon different situations. Your character movement (in functionality) will never change.

Classes, on the other hand, are fairly different because you are actually creating a new object with different properties, and it will act differently than other objects even if they are from the same class, because they will complete a different task.

This is the reason you would not create a button module, because you want to create many buttons that do different things. Or, in your case, a text area that will act independantly from other text areas.

Idk if that makes sense to you, but I think about it sort of that way. Generalization vs specific tasks.
Raknarg




PostPosted: Thu Mar 29, 2012 6:58 pm   Post subject: RE:Default value of type-report not possible?

Tony, if you would like to chip in or completely negate my argument, that would be acceptable Razz

Also, I made a pretty weak example, but an example none the less

Turing:

%This module will handle movement for the used character
module movement
    export all
   
    fcn can_move
       
    end can_move
   
    proc move
       
    end move
   
    proc anyhting_else
       
    end anyhting_else
end movement

%This class allows you to create new characters with the same functionality, but different attributes
class character
    var health, power, speed, etc : something
    var anythingElse : something
end character


or, in your case:
Turing:

class TextArea
    export all
    var text : string := ""
    var x, y : real := 10 
end TextArea

var newTextArea : pointer to TextArea % also can be ": ^ TextArea
new TextArea, newTextArea

put newTextArea -> x
%or, and my prefered way:
put TextArea (newTextArea).x
ModsCentral




PostPosted: Thu Mar 29, 2012 7:22 pm   Post subject: Re: Default value of type-report not possible?

That's the example I was looking for, see my issue is I'm trying to relate the coding the user will use as close to actionscript 3 (flash) as possible hereby relating the properties to this.
Turing:

import TextArea
var field : TextArea %Hoping to have a textarea instantly created with default values
field.x := 20 %Moves the field to x(20) when called (Now)
field.somthingElse %and so on


Currently I have that with the following module
Turing:

unit
module textArea
export unqualified TextArea, create, update, setDefault

type TextArea : record
    text : string
    x : real
    y : real
    width : real
    height : real
    backgroundColor : int
    multiline : boolean
    borderThickness : real
    borderColor : int
    borderStyle : string
    font : string
    bold : boolean
    fontColor : int
    fontSize : int
    padding : real
    focused : boolean
    cursorSpeed : int
    resetCursor : boolean
end record

procedure setDefault (var curObj : TextArea)
    curObj.text := ""
    curObj.width := 100
    curObj.height := 14
    curObj.x := maxx div 2 - (curObj.width div 2)
    curObj.y := maxy div 2 - (curObj.height div 2)
    curObj.backgroundColor := 0
    curObj.multiline := false
    curObj.borderThickness := 1
    curObj.borderColor := 7
    curObj.borderStyle := "solid"
    curObj.font := "Arial"
    curObj.bold := false
    curObj.fontColor := 7
    curObj.fontSize := 12
    curObj.padding := 1
    curObj.focused := false
    curObj.cursorSpeed := 400
    curObj.resetCursor := false
end setDefault


procedure updateText (passedObj : TextArea)
    var curObj : TextArea := passedObj
    var x : int := floor(curObj.x)
    var y : int := floor(curObj.y) + 1
    var width : int := floor(curObj.width)
    var height : int := floor(curObj.height)
    var font : int := Font.New (curObj.font + ":" + intstr(curObj.fontSize))
    var bT : int := floor(curObj.borderThickness)
    var padding : int := floor(curObj.padding)
    Font.Draw (curObj.text, x, y, font, curObj.fontColor)
    Font.Free (font)
end updateText

process flashCursor (passedObj : TextArea)
    var curObj : TextArea := passedObj
    %curObj.resetCursor := false
    var font : int := Font.New (curObj.font + ":" + intstr(curObj.fontSize))
    var fW : int := Font.Width (curObj.text,  font)
    var x : int := floor(curObj.x) + fW + 1
    var y : int := floor(curObj.y)
    var width : int := floor(curObj.width)
    var height : int := floor(curObj.height)
    var sP : int := 0
    loop
        curObj := passedObj
        Draw.FillBox (x + 1, y, x + 1 + 0, y + height, black)
        exit when curObj.focused = false or curObj.resetCursor = true
        delay (curObj.cursorSpeed div 1.2)
        Draw.FillBox (x + 1, y, x + 1 + 0, y + height, curObj.backgroundColor)
        exit when curObj.focused = false or curObj.resetCursor = true
        delay (curObj.cursorSpeed)
        put curObj.resetCursor
        exit when curObj.focused = false or curObj.resetCursor = true
    end loop
    Draw.FillBox (x + 1, y, x + 1 + 0, y + height, curObj.backgroundColor)
end flashCursor

procedure update (passedObj : TextArea)
    var curObj : TextArea := passedObj
    var x : int := floor(curObj.x)
    var y : int := floor(curObj.y)
    var width : int := floor(curObj.width)
    var height : int := floor(curObj.height)
    var bT : int := floor(curObj.borderThickness)
    var padding : int := floor(curObj.padding)
    Draw.FillBox (x - padding - bT, y - padding - bT, x + width + padding + bT, y + height + padding + bT, curObj.borderColor)
    Draw.FillBox (x - padding, y - padding, x + width + padding, y + height + padding, curObj.backgroundColor)
    updateText (curObj)
    curObj.resetCursor := true
    %fork flashCursor (curObj)
end update

process getKeyDown (passedObj : TextArea)
    var curObj : TextArea := passedObj
    var curLetter : string (1)
    loop
        if hasch then
            getch (curLetter)
            if ord(curLetter) = 8 then
                curObj.text := curObj.text (1 .. length (curObj.text) - 1)
            elsif ord(curLetter) >= 32 and ord(curLetter) <= 126 then
                curObj.text := curObj.text + curLetter
            end if
            update (curObj)
        end if
       
        %exit when curObj.focused = false
    end loop
end getKeyDown

process getMouseClick (passedObj : TextArea)
    var curObj : TextArea := passedObj
    var x : int := floor(curObj.x) - floor(curObj.padding)
    var y : int := floor(curObj.y) - floor(curObj.padding)
    var width : int := floor(curObj.width) + floor(curObj.padding * 2)
    var height : int := floor(curObj.height) + floor(curObj.padding * 2)
    var mX, mY, mB : int
 
    loop
        Mouse.Where(mX, mY, mB)
        if mX >= x and mX <= x + width and mY >= y and mY <= y + height then %MOUSE ENTERED OBJECT
            if mB = 1 then %MOUSE CLICKED OBJECT, CALL SPECIFIED CALLBACK FUNCTION (For now focus textarea)
                if curObj.focused = false then %OBJECT NOT ALREADY FOCUSED
                    curObj.focused := true
                    fork flashCursor (curObj)
                    fork getKeyDown (curObj)   
                end if
            elsif mB = 0 then
                %MOUSE UP... DO NOTHING FOR NOW
            end if
        else %MOUSE LEFT OBJECT
            if mB = 1 then %MOUSE CLICKED AWAY FROM OBJECT... (For now unfocus textarea)
                curObj.focused := false
            end if
        end if
    end loop
end getMouseClick

procedure create (passedObj : TextArea)
    var curObj : TextArea := passedObj
    var x : int := floor(curObj.x)
    var y : int := floor(curObj.y)
    var width : int := floor(curObj.width)
    var height : int := floor(curObj.height)
    var bT : int := floor(curObj.borderThickness)
    var padding : int := floor(curObj.padding)
    Draw.FillBox (x - padding - bT, y - padding - bT, x + width + padding + bT, y + height + padding + bT, curObj.borderColor)
    Draw.FillBox (x - padding, y - padding, x + width + padding, y + height + padding, curObj.backgroundColor)
    fork getMouseClick (curObj)
    updateText (curObj)
end create




end textArea




and the main program >>


Turing:

import textArea

View.Set("graphics:max,max,nobuttonbar,title:Test Textarea")

var testField : TextArea
textArea.setDefault(testField)
testField.backgroundColor := 30
testField.text := "awesome"
textArea.create(testField)



This works so far, except I would like to just eliminate the whole create and setdefault functions to be called.
Also I know processes are sort of deprecated but they run individually (Just having an issue with cancelling the cursor flash process)
I'll give your code a shot, thanks Smile[/syntax]
ModsCentral




PostPosted: Thu Mar 29, 2012 7:25 pm   Post subject: Re: Default value of type-report not possible?

Also, is it possible to edit the variables outside the class?

EDIT #1 Just realized Turing throws an error "... readonly" yet if you select run anyway, the variable is hereby modified. Weird?
EDIT #2 So far what you have is what i need, except now I'm stuck with my alternate issue, trying to get the function create to get called automatically. Can I use forward's or something along with the procedure to call it within the class?

EDIT #3
Turing:

class TextArea
    export unqualified all
    var text : string := ""
    var pervasive x, y : real := 10

    procedure create
   
    end create
    create
   
end TextArea

var newTextArea : ^ TextArea
new newTextArea

newTextArea -> x := 2
put newTextArea -> x

Is what I have now. I don't want to call the create function manually ;/ is that required?
Raknarg




PostPosted: Thu Mar 29, 2012 7:44 pm   Post subject: RE:Default value of type-report not possible?

Yes, a class is not really a procedure. Something will have to be called for anything to work. Create, or initialize as I usually call it, will have to be called at the initial instance of the objects use.
ModsCentral




PostPosted: Thu Mar 29, 2012 7:54 pm   Post subject: Re: Default value of type-report not possible?

Alright modified my code to work with classes. This definitely makes them 100% independent with all others objects which is nice, and also you won't need to pass in the object to initiate it. However, are you saying you must call for example > textfield -> load, in order for it to show any changes made to the properties? Is there no way for the class to detect any modifications to the variables hence call the load proc itself? Maybe a process by which checks each value with a temporarily saved value, and if different then reload?
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: