This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

Object dependence in case of a GUI framework

Started by Sewbacca, 24 May 2018 - 04:43 PM
Sewbacca #1
Posted 24 May 2018 - 06:43 PM
Imagine you have a class called Widget. Now you create a object from Widget, and you would append it to the global Widget.


local wid = Widget()
wid:resize(10, 10)
-- #  wid:getSize() would return the witdth and the height of wid
-- #  imagine globalWidget would be the screen
globalWidget:addChild(wid)

Now, you want to reposition the widget. What do you think is better and/or more intuitive? Please give a reason (e. g. just more intuitive or an real advantage).

globalWidget:repositionChild(wid, 5, 5)
-- #  Or
wid:reposition(5, 5)

I am not sure, because the position of an object is an information that doesn't belongs to that (it doesn't defines the state of this object).
Edited on 24 May 2018 - 05:40 PM
Windows10User #2
Posted 24 May 2018 - 06:55 PM
I think wid:reposition is better.
Sewbacca #3
Posted 24 May 2018 - 07:08 PM
I think wid:reposition is better.
Okay, but can you tell me please why? Is it just more intuitive or has it an advantage?
EveryOS #4
Posted 24 May 2018 - 07:14 PM
It has reliance on less variables
Plus, it is better off to leave an object to modify it's own internals, in case they are changed
Edited on 24 May 2018 - 05:14 PM
Sewbacca #5
Posted 24 May 2018 - 07:26 PM
It has reliance on less variables
Plus, it is better off to leave an object to modify it's own internals, in case they are changed

It is a matter of perspective, because the parent object will hold the information for all pixels, to form the image. Or do you have another solution?
Bomb Bloke #6
Posted 24 May 2018 - 11:16 PM
I'd tell the child to move. Otherwise, what happens if you have children of children? You'd have to figure out parents of parents.
KingofGamesYami #7
Posted 24 May 2018 - 11:59 PM
In my experience with existing frameworks (eg. Qt), each "layer" of objects will have its own reposition-like method. That method shifts itself and all of its children.
Sewbacca #8
Posted 25 May 2018 - 07:48 AM
I'd tell the child to move. Otherwise, what happens if you have children of children? You'd have to figure out parents of parents.

Think this is a good reason, i didn't thought in that way. Thank you!
EveryOS #9
Posted 25 May 2018 - 12:24 PM
Also, what if you change the parent of the child? Sometimes it is possible to do so.
Exerro #10
Posted 25 May 2018 - 03:52 PM
I'd actually make it configurable. Giving parents control over child positioning lets you do cool things like auto-arranging grids/lists, and HTML-like 'flow' layouts. Of course, sometimes you want to just say "put this here exactly" so that's why I'd not say either one is the best standalone solution.
EveryOS #11
Posted 25 May 2018 - 04:15 PM
But what if you're not making a layout manager? Then the other way is easier

What about a compromise: we do both
Edited on 25 May 2018 - 02:16 PM
Jummit #12
Posted 25 May 2018 - 09:07 PM
globalWidget:repositionChild(wid, 5, 5)
If I would implement the reposition function in the parent, I would do it like this:
globalWidget:getChild(wid):reposition(5, 5)
This would also fix the issue Bomb Bloke pointed out:
I'd tell the child to move. Otherwise, what happens if you have children of children? You'd have to figure out parents of parents.
Edited on 25 May 2018 - 07:07 PM
Luca_S #13
Posted 25 May 2018 - 10:53 PM
If I would implement the reposition function in the parent, I would do it like this:
globalWidget:getChild(wid):reposition(5, 5)
Tbh that doesn't really make sense because wid already is the child which was appended so globalWidget:getChild(wid) should return wid(If it returns anything else it would be pretty strange) and if it returns wid that would be the same as:

wid:reposition(5, 5)
Jummit #14
Posted 26 May 2018 - 08:46 AM
If I would implement the reposition function in the parent, I would do it like this:
globalWidget:getChild(wid):reposition(5, 5)
Tbh that doesn't really make sense because wid already is the child which was appended so globalWidget:getChild(wid) should return wid(If it returns anything else it would be pretty strange) and if it returns wid that would be the same as:

wid:reposition(5, 5)
Oh, I did something wrong there. I meant it like this:

globalWidget.wid = widget(1, 1)
globalWidget.wid:reposition(5, 5)