Less mixin scope problems - css

I built a parametric mixin that finds the children of an element based on a "node-value" system that I created. What happens is the children are found by a loop function ".extractArrays" with ".deliverChild" inside it. Then the children (up to 4 of them) are put into ".calculateWidth", which returns the width of the child in a variable (ex. "calculatedWidth1").
The problem is: when there are no third and fourth children, #child3 and #child4 are unset, which creates an error. I set #child3 and #child4 to zero before the mixins, hoping that this would give them default values of 0 before the mixins are called. For some reason, however, when there is a third child, the mixin's returned value of #child3 won't override the #child3 that is set to 0. I am unsure why this is happening, but I feel like there are a couple things that throw me off about Less when it comes to mixins and scope.
#child3: none, 0 0, 0, 0;
#child4: none, 0 0, 0, 0;
.extractArrays(#index, #node, #node-value, #elements-array) when (#index <= #length-elements-array) {
#element-array: extract(#elements-array, #index);
#found-node: extract(#element-array, 2);
.deliverChild(#element-array, #found-node) when (#found-node = #child-node-value1) {
#child1: extract(#element-array, 1);
}
.deliverChild(#element-array, #found-node) when (#found-node = #child-node-value2) {
#child2: extract(#element-array, 1);
}
.deliverChild(#element-array, #found-node) when (#found-node = #child-node-value3) {
#child3: extract(#element-array, 1);
}
.deliverChild(#element-array, #found-node) when (#found-node = #child-node-value4) {
#child4: extract(#element-array, 1);
}
.deliverChild(#element-array, #found-node);
.extractArrays(#index + 1, #node, #node-value, #elements-array);
}
.extractArrays(1, #node, #node-value, #elements-array);
.calculateWidth(#child1, 1);
.calculateWidth(#child2, 2);
.calculateWidth(#child3, 3);
width: #calculatedWidth1 + #calculatedWidth2 + #calculatedWidth3 + #calculatedWidth4;

(Not an exact answer to the "scope" question above but an algorithm to use as a starting point in an alternative approach suggested in comments above):
A generic array filtering mixin would look like this (Less 1.7.5 or higher):
#array: // key value
a 1,
b 2,
c 3,
a 4,
d 5,
a 6,
c 7;
// usage:
usage {
.filter-array(#array, a);
result: #filter-array;
}
// impl.:
.filter-array(#array, #key, #key-index: 1) {
.-(length(#array));
.-(#i, #r...) when (#i > 0)
and not(#key = extract(extract(#array, #i), #key-index)) {
// skip item since key does not match
.-((#i - 1), #r);
}
.-(#i, #r...) when (length(#r) > 0)
and (#key = extract(extract(#array, #i), #key-index)) {
// key matches and #r is not empty so concat current item to #r
.-((#i - 1); extract(#array, #i), #r);
}
.-(#i, #r...) when (length(#r) = 0)
and (#key = extract(extract(#array, #i), #key-index)) {
// key matches and #r is empty so this is our first item
.-((#i - 1), extract(#array, #i));
}
.-(#i, #r...) when (#i = 0) {
// define "return" variable:
#filter-array: #r;
}
}
With resulting array being equal to:
#filter-array:
a 1,
a 4,
a 6;
This looks quite scary and verbose but still more clean and more controllable than the original approach.
Additionally it would make sense to provide a dedicated mixin to return a sum (or any other "transformation" with result in a single value) of certain values of the array items (that way the implementation can be simplified to a certain point since you won't need to concatenate anything and some of above conditions and/or mixin specializations become unnecessary).

Related

Issue with Deprecated Division warning in SASS

I am doing a large project converting old SASS code (specifically node-sass), into the newer SASS using the SASS npm package, and I am getting this error message
DEPRECATION WARNING: Using / for division is deprecated and will be removed in Dart Sass 2.0.0. Recommendation: math.div(3, 4)
However, it seems to relate to this line of code
$_this: nth($short, $_i);
I am finding this confusing as I am not trying to divide the numbers, and the aren't separated by a /, can someone explain please, or at least help me with a solution, as ignoring the line isn't working either?
If it helps anyone, I have included the whole statement now...
#function parse-span(
$short,
$key: span
) {
$_return: ();
#if type-of($short) == map {
$_return: $short;
} #else {
$_at: index($short, at);
#if $_at {
$_loci: $_at + 1;
$_location: nth($short, $_loci);
$_return: map-merge($_return, (location: $_location));
$short: set-nth($short, $_at, null);
$short: set-nth($short, $_loci, null);
}
$_i: 1;
$_span: ();
#while $_i <= length($short) {
$_this: nth($short, $_i); <--- faulty line?
#if type-of($_this) == number {
$_span: append($_span, $_this);
$short: set-nth($short, $_i, null);
} #else if $_this == of {
$short: set-nth($short, $_i, null);
$_i: length($short) + 1;
}
$_i: $_i + 1;
}
#if length($_span) > 0 {
$_span: if(length($_span) == 1, nth($_span, 1), $_span);
$_return: map-merge($_return, ($key: $_span));
}
$_return: map-merge($_return, parse-grid($short));
}
#return $_return;
}
$_gutters: parse-span($short, gutter-override);
I have tried the solution in the comments but to no resolution.
Here is an image of the actual warning

How to Print Text to DrawingArea in Gnome-Shell-Extension

I'm doing my first gnome-shell extension (gnome shell 41.3) and I seem to encounter the most common problems, finding the right manual for that...
What I try to achive is...
A Widget shown (above everything) on the desktop...
Draw something into the widget...
Write some Text to the widget...
What I already acomplished is...
Using a DrawingArea and Cairo (I assume) I can use .moveTo (), .lineTo (), etc..., .stroke() to draw something into my widget...
Whe widget is displayed above everything else and stuff...
My extension has a settings widget to configure...
What I am missing is...
Any clue on how to put text in addition to the drawing stuff onto the Drawing area...
I did 1.5 days of duckduckgoing into it but, again, I'm running in circles with not a single Idea on how to proceed...
Could anyone point me into the correct direction, plz...???
Pseudo Code goes something like this
const {St, GLib} = imports.gi;
var area = new St.DrawingArea({
style_class : 'bg-color',
reactive : true,
can_focus : true,
track_hover : true,
width: myWidth,
height: myHeight
});
area.connect('repaint', (area) => drawMyStuff(area));
Main.layoutManager.addChrome(area, {
affectsInputRegion : false,
affectsStruts : false,
trackFullscreen : false,
});
timeout = GLib.timeout_add(0, 500, () => {
this.area.queue_repaint();
return true;
});
function drawMyStuff(area) {
let cr = area.get_context();
cr.translate(area.width / 2, area.height / 2);
cr.scale(area.width / 2, area.height / 2);
cr.setSourceRGBA (1, 1, 1, 1);
cr.arc(0.0, 0.0, 1.0 - 0.05, 0, 2 * Math.PI);
cr.fill();
/*
* Would like to print some text here but obviously I am to stupid to do so...
* Help me StackExchangeCommunity, you're my only hope...
*
* Obviously it is not that easy:
* cr.setSourceRGBA (1, 0, 0, 1);
* cr.write(0.0, 0.0, "Not a moon")
* cr.fill();
*
*/
cr.$dispose();
return true;
}
It's kinda hard to find out and I had no luck finding any documentation on that but after digging into the GJS Code here (https://gitlab.gnome.org/GNOME/gjs/-/blob/HEAD/modules/cairo-context.cpp) I was able to understand a lot more on how to do cairo stuff like this (https://www.cairographics.org/manual/) from GJS...
The solution for my question stated above goes like this:
function drawMyStuff(area) {
let cr = area.get_context();
cr.translate(area.width / 2, area.height / 2);
cr.scale(area.width / 2, area.height / 2);
cr.setSourceRGBA (1, 1, 1, 1);
cr.arc(0.0, 0.0, 1.0 - 0.05, 0, 2 * Math.PI);
cr.fill();
/*
* Thank me, I'm my only hope...
*
* Obviously it is that easy:
*
*/
cr.moveTo(-0.5,-0.5);
cr.setSourceRGBA (1, 0, 0, 1);
cr.setFontSize(0.125);
cr.showText('No Moon');
cr.$dispose();
return true;
}

Player character won't go in front of buildings

Ok, so I managed to get all the buildings to stay in place, but now for some reason, the player character won't go in front of the buildings.
I tried switching the places in the code where the if the command for checking if the character's going behind the building and the if-else for checking if it's going in front of the code, but nothing changed.
here's the code:
var _dg = depth_grid;
var _inst_num = instance_number(obj_depth_buildings);
//below is for resizing the grid
ds_grid_resize(depth_grid,2, _inst_num);
//below adds instance info to grid
var _yy = 0;
with (obj_depth_buildings)
{
_dg[# 0,_yy] = id;
_dg[# 1,_yy] = y;
_yy++;
}
//below sorts the grid so that the ones with the biggest y variables end up at the top
ds_grid_sort(_dg,1,false);
//below goes through the grid and identifies everything
var _inst;
_yy = 0;
repeat (_inst_num)
{
//below pulls out an id
_inst = _dg[# 0, _yy];
//below gets the instance execute depth
with(_inst)
{
_inst.depth= layer_get_depth("collision") + _yy;
with (obj_nonbuilding_depths)
{
if object_index.y > _inst.y
{
object_index.depth = (_inst.depth + 1);
}
else if object_index.y <= _inst.y
{
object_index.depth = (_inst.depth - 1);
}
}
}
_yy++;
}
you should change the object depth by change the leyar depth in GMS2

How to create list output in less?

I am trying to create a list which is calculated from another list. For example, if I have a list like 1,2,3,4... then the output has to be 10,20,30,40... Is there any other way to create a list from another in less? Please refer the below codes.
#input: 1,2,3,4;
.for(#array) when (default()) {.for-impl_(length(#array))}
.for-impl_(#i) when (#i > 1) {.for-impl_((#i - 1))}
.for-impl_(#i) when (#i > 0) {.-each(extract(#array, #i), #i)}
.create-list(#input){
.for(#input); .-each(#value, #a) {
.output_#{a}(#a) { // Create dynamic mixin based on input list
#output_#{a}: #a * 10; // Create dynamic variable to store each calc
}
}
}
.create-list(#input);
.loop(#count) when (#count > 0){
#prev: #count - 1;
.loop(#prev);
.first() when (#count = 1){
.output_#{count}(); // call first mixin which created already
#res_#{count}: #output_#{count} // Store the result in another dynamic var
}
.first() when not (#count = 1){
.output_#{count}();
#res_#{count}: #res_#{prev}, #output_#{count}; // join prev and current result
}
.first();
}
.loop(4);
The above implementation similar I expect like below.
.a1(){
#o1: #fff;
}
.a2(){
#o2: #aaa;
}
.a3(){
#o3: #ccc;
}
.loop(#counter) when (#counter > 0) {
.loop((#counter - 1));
.a1();
#out1: #o1;
.a2();
#out2: #out1, #o2;
.a3();
#out: #out2, #o3;
div{
colors: #out;
}
}
.loop(1);
and the output is #fff, #aaa, #ccc.
You'll create a modified "list" by passing the concatenated result of each loop iteration to next iteration, thus the final iteration defines the actual result. E.g. (just illustrating the principle w/o adopting it to your use-case):
// usage:
#input: 1, 2, 3, 4;
result {
.modify-list(#input);
result: #result;
}
// impl:
.modify-list(#list) {
#n: length(#list);
.-(#n, extract(#list, #n) * 10);
.-(#i, #r) when (#i > 1) {
.-(#i - 1; extract(#list, #i - 1) * 10, #r);
}
.-(1, #r) {#result: #r}
}
Demo
Technically this code does not create a one dimensional list (i.e. the result there is not really equal to #result: 10, 20, 30, 40;) but since in final CSS it's rendered identically it would work just fine.
Also assuming your original use-case was How to apply less functions to gradient colors?, you don't really need a code like this (i.e. it's an "XY Problem" and instead of generating a new variable with the new list, direct generation of the corresponding gradient values would be much less verbose and much more readable. See the linked answer in comments of the mentioned question).

Confused by simplescalr preditor

now I am learning simplescalar source code. But I am confused by the predictor module. It is about bimod predictor.
Here is the initialization :
case BPred2bit:
if (!l1size || (l1size & (l1size-1)) != 0)
fatal("2bit table size, `%d', must be non-zero and a power of two",
l1size);
pred_dir->config.bimod.size = l1size;
if (!(pred_dir->config.bimod.table =
calloc(l1size, sizeof(unsigned char))))
fatal("cannot allocate 2bit storage");
/* initialize counters to weakly this-or-that */
flipflop = 1;
for (cnt = 0; cnt < l1size; cnt++)
{
pred_dir->config.bimod.table[cnt] = flipflop;
flipflop = 3 - flipflop;
}
break;
Here we use the PHT table:
case BPred2bit:
p = &pred_dir->config.bimod.table[BIMOD_HASH(pred_dir, baddr)];
break;
But what to my suprise is the PHT tale NEVER be updated!!!. I find the code nowhere, neither in the pred_update() funtion!!!.
Can you tell me the reason? What mechanism dose simplescalar use?
But it is updated. In bpred_update() you find this code
if (dir_update_ptr->pdir1)
{
if (taken)
{
if (*dir_update_ptr->pdir1 < 3)
++*dir_update_ptr->pdir1;
}
else
{ /* not taken */
if (*dir_update_ptr->pdir1 > 0)
--*dir_update_ptr->pdir1;
}
}
An entry of the table is either incremented or decremented depending on the outcome of the branch. That particular entry comes from the second code segment in your question. It's just a pointer to a 2-bit counter.

Resources