

-----------------------------------

ModsCentral

Thu Mar 29, 2012 4:53 pm


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


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.

%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


-----------------------------------

Tony

Thu Mar 29, 2012 5:02 pm


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.


-----------------------------------

ModsCentral

Thu Mar 29, 2012 5:07 pm


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

Thu Mar 29, 2012 5:12 pm


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.


-----------------------------------

ModsCentral

Thu Mar 29, 2012 5:25 pm


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

Thu Mar 29, 2012 5:31 pm


RE:Default value of type-report not possible?

-----------------------------------

see [tdoc]class[/tdoc] and [tdoc]new[/tdoc]


-----------------------------------

ModsCentral

Thu Mar 29, 2012 5:32 pm


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

Thu Mar 29, 2012 5:43 pm


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?)


-----------------------------------

Tony

Thu Mar 29, 2012 5:53 pm


RE:Default value of type-report not possible?

-----------------------------------

I would suggest picking up some OOP concepts. http://en.wikipedia.org/wiki/Object-oriented_programming


-----------------------------------

Raknarg

Thu Mar 29, 2012 6:56 pm


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

Thu Mar 29, 2012 6:58 pm


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 :P

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


%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:

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

Thu Mar 29, 2012 7:22 pm


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.

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

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) = x and mX = y and mY >



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 :)[/syntax]


-----------------------------------

ModsCentral

Thu Mar 29, 2012 7:25 pm


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

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

Thu Mar 29, 2012 7:44 pm


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

Thu Mar 29, 2012 7:54 pm


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?


-----------------------------------

Tony

Thu Mar 29, 2012 8:00 pm


Re: Default value of type-report not possible?

-----------------------------------

Is there no way for the class to detect any modifications to the variables hence call the load proc itself?
This is typically accomplished by keeping the variables private, and exposing them through getter/setter methods. So instead of
[code]
myTextbox.x = 42
[/code]
you'd do
[code]
myTextbox.setX(42)
[/code]
which would internally assign 42 to variable x, but could also do other actions.

